import java.io.*;
interface Tokens {
int EOF = 0;
int NUM = EOF + 1;
int LT = NUM + 1;
int LTEQ = LT + 1;
int EQ = LTEQ + 1;
int NEQ = EQ + 1;
}
public class Scanner implements Tokens {
public Scanner(InputStream in) {
this.in = in;
this.buf = new StringBuffer();
nextCh();
}
public int token; // the current token class
public String chars; // token representation
private InputStream in; // the input stream
private char ch; // the current character
private final char EOF_CH = (char)-1; // the eof character
private StringBuffer buf;
void nextToken() {
...
}
void nextCh() {
try {
ch = (char)in.read();
} catch (IOException e) {
error(e.toString());
}
}
void error(String message) {
try {
close();
} catch (IOException e) {}
throw new Error(message);
}
void close() throws IOException {
in.close();
}
public String representation() {
return tokenClass(token) + ((token == NUM) ? "(" + chars + ")" : "");
}
public static String tokenClass(int token) {
switch (token) {
case EOF: return "";
case NUM: return "number";
case LT: return "<";
case LTEQ: return "<=";
case EQ: return "==";
case NEQ: return "/=";
default: return "";
}
}
}
class ScannerTest implements Tokens {
static public void main(String[] args) {
try {
Scanner s = new Scanner(new FileInputStream(args[0]));
s.nextToken();
while (s.token != EOF) {
System.out.println(s.representation());
s.nextToken();
}
s.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
|