Is there a good reason to use numbers to identify tokens now? I follow Crafting a Compiler
.
The code submitted by the author is here:
public class Token {
public final static int ID = 0, FLTDCL = 1, INTDCL = 2, PRINT = 3,
ASSIGN = 4, PLUS = 5, MINUS = 6, EOF = 7, INUM = 8, FNUM = 9;
public final static String[] token2str = new String[] { "id", "fltdcl",
"intdcl", "print", "assign", "plus", "minus", "$", "inum", "fnum" };
public final int type;
public final String val;
public Token(int type) {
this(type, "");
}
public Token(int type, String val) {
this.type = type;
this.val = val;
}
public String toString() {
return "Token type\t" + token2str[type] + "\tval\t" + val;
}
}
Instead of using ugly arrays, wouldn't it be wiser to modify the constructors to accept strings for a variable type
instead of integers? Then we could get rid of
public final static int ID = 0, FLTDCL = 1, INTDCL = 2, PRINT = 3,
ASSIGN = 4, PLUS = 5, MINUS = 6, EOF = 7, INUM = 8, FNUM = 9;
or is it needed later since using a string instead would be worse?