1: package compiler;
2:
3: /**
4: * 采用全局变量sym来存储符号码,并用全局变量id和num来传递语义值
5: *
6: * @author jiangnan
7: *
8: */
9: public class Symbol {
10:
11: //各类符号码
12: public static final int nul = 0; //NULL
13: public static final int ident = 1; //标识符
14: public static final int plus = 2; //加号+
15: public static final int minus = 3; //减号-
16: public static final int mul = 4; //乘号*
17: public static final int div = 5; //除号/
18: public static final int oddsym = 6; //odd
19: public static final int number = 7; //数字
20: public static final int eql = 8; //等于号=(equal)
21: public static final int neq = 9; //不等于<>(not equal)
22: public static final int lss = 10; //小于<(less)
23: public static final int geq = 11; //大于等于>=(greater or equal)
24: public static final int gtr = 12; //大于>(greater)
25: public static final int leq = 13; //小于等于<=(less or equal)
26: public static final int lparen = 14; //左括号(
27: public static final int rparen = 15; //右括号 )
28: public static final int comma = 16; //逗号,
29: public static final int semicolon = 17; //分号;
30: public static final int peroid = 18; //句号.
31: public static final int becomes = 19; //赋值符号 :=
32: public static final int beginsym = 20; //开始符号begin
33: public static final int endsym = 21; //结束符号end
34: public static final int ifsym = 22; //if
35: public static final int thensym = 23; //then
36: public static final int whilesym = 24; //while
37: public static final int writesym = 25; //write
38: public static final int readsym = 26; //read
39: public static final int dosym = 27; //do
40: public static final int callsym = 28; //call
41: public static final int constsym = 29; //const
42: public static final int varsym = 30; //var
43: public static final int procsym = 31; //procedure
44: public static final int elsesym = 32;
45: public static final int repeatsym=33;
46: public static final int untilsym=34;
47:
48: //符号码的个数
49: public static final int symnum = 35;
50:
51: //设置保留字名字,按照字母顺序,便于折半查找
52: public static final String[] word = new String[]{
53: "begin","call" , "const" , "do" ,
54: "else" ,"end" ,"if" , "odd",
55: "procedure", "read","repeat","then",
56: "until" , "var", "while" , "write" };
57: //保留字对应的符号值
58: public static final int[] wsym = new int[]{
59: beginsym, callsym, constsym, dosym,
60: elsesym, endsym, ifsym,oddsym,
61: procsym,readsym,repeatsym, thensym,
62: untilsym ,varsym,whilesym, writesym};
63:
64: //符号码
65: public int symtype;
66: //标志符号名字;
67: public String id;
68: //数值的大小
69: public int num;
70:
71: /**
72: * 构造具有特定符号码的符号
73: *
74: * @param stype
75: */
76: public Symbol(int stype) {
77: symtype = stype;
78: id = "";
79: num = 0;
80: }
81: }