问题描述
- 编译原理scanner的java代码
- package lexer;
public class Token {
public final int tag;public Token(int t) { tag = t;}public String toString() { return """" + (char) tag;}
}
package lexer;
public class Tag {
public final static int AND = 256 BASIC = 257 BREAK = 258 DO = 259
ELSE = 260 EQ = 261 FALSE = 262 GE = 263 ID = 264 IF = 265
INDEX = 266 LE = 267 MINUS = 268 NE = 269 NUM = 270 OR = 271
REAL = 272 TEMP = 273 TRUE = 274 WHILE = 275;
}package lexer;
public class Real extends Token {
public final float value;public Real(float v) { super(Tag.REAL); value = v;}public String toString() { return """" + value;}
}
package lexer;
public class Num extends Token {
public final int value;public Num(int v) { super(Tag.NUM); value = v;}public String toString() { return """" + value;}
}
package lexer;
public class Word extends Token {
public String lexeme = """";public Word(String s int tag) { super(tag); lexeme = s;}public String toString() { return lexeme;}public static final Word and = new Word(""&&"" Tag.AND) or = new Word(""||"" Tag.OR) eq = new Word(""=="" Tag.EQ) ne = new Word(""!="" Tag.NE) le = new Word(""<="" Tag.LE) ge = new Word("">="" Tag.GE) minus = new Word(""minus"" Tag.MINUS) True = new Word(""true"" Tag.TRUE) False = new Word(""false"" Tag.FALSE) temp = new Word(""t"" Tag.TEMP);
}
package lexer;
import java.io.IOException;
import java.util.Hashtable;public class Lexer {
public static int line = 1;char peek = ' ';Hashtable words = new Hashtable();void reserve(Word w) { words.put(w.lexeme w);}public Lexer() { reserve(new Word(""if"" Tag.IF)); reserve(new Word(""else"" Tag.ELSE)); reserve(new Word(""while"" Tag.WHILE)); reserve(new Word(""do"" Tag.DO)); reserve(new Word(""break"" Tag.BREAK)); reserve(Word.True); reserve(Word.False);}public void readch() throws IOException { peek = (char) System.in.read();}boolean readch(char c) throws IOException { readch(); if (peek != c) { return false; } peek = ' '; return true;}public Token scan() throws IOException { for (;; readch()) { if (peek == ' ' || peek == 't') continue; else if (peek == 'n') { line += 1; } else { break; } } switch (peek) { case '&': if (readch('&')) return Word.and; else return new Token('&'); case '|': if (readch('|')) return Word.or; else return new Token('|'); case '=': if (readch('=')) return Word.eq; else return new Token('='); case '!': if (readch('=')) return Word.ne; else return new Token('!'); case '<': if (readch('=')) return Word.le; else return new Token('<'); case '>': if (readch('=')) return Word.ge; else return new Token('>'); } if (Character.isDigit(peek)) { int v = 0; do { v = 10 * v + Character.digit(peek 10); readch(); } while (Character.isDigit(peek)); if (peek != '.') return new Num(v); float x = v; float d = 10; for (;;) { readch(); if (!Character.isDigit(peek)) break; x = x + Character.digit(peek 10) / d; d = d * 10; } return new Real(x); } if (Character.isLetter(peek)) { StringBuffer b = new StringBuffer(); do { b.append(peek); readch(); } while (Character.isLetterOrDigit(peek)); String s = b.toString(); Word w = (Word) words.get(s); if (w != null) return w; w = new Word(s Tag.ID); words.put(s w); return w; } Token tok = new Token(peek); peek = ' '; return tok;}public void out() { System.out.println(words.size());}public char getPeek() { return peek;}public void setPeek(char peek) { this.peek = peek;}
}
package main;
import java.io.IOException;
import java.util.Hashtable;import lexer.Lexer;
import lexer.Token;public class Main {
public static void main(String[] args) throws IOException {
Lexer lexer = new Lexer();
char c;
do {
Token token=lexer.scan();
switch (token.tag) {
case 270:
case 272:
System.out.println(""(NUM +token.toString()+"")"");
break;
case 264:
System.out.println(""(ID +token.toString()+"")"");
break;
case 256:
case 257:
case 258:
case 259:
case 260:
case 265:
case 274:
case 275:
System.out.println(""(KEY +token.toString()+"")"");
break;
case 13:
break;
default:
System.out.println(""(SYM +token.toString()+"")"");
break;
}} while (lexer.getPeek()!='n');}
}
怎么把上面从控制台输入的代码改成读取txt文件输入
解决方案
http://outofmemory.cn/code-snippet/2242/java-usage-BufferedReader-duqu-console-out