1: package compiler;
2: //竟然没有对符号表检查大小,会溢出的。
3:
4: import java.io.IOException;
5:
6: public class SymbolTable {
7:
8: /**
9: * 当前名字表项指针(有效的符号表大小)table size
10: */
11: public int tablePtr = 0;
12: /**
13: * 符号表的大小
14: */
15: public static final int tableMax = 100;
16: public static final int symMax = 10; //符号的最大长度
17: public static final int addrMax = 1000000; //最大允许的数值
18: public static final int levMax = 3; //最大允许过程嵌套声明层数[0,levmax]
19: public static final int numMax = 14; //number的最大位数
20: public static boolean tableswitch; //显示名字表与否
21: //名字表
22: public Item[] table = new Item[tableMax];
23:
24: public class Item {
25:
26: public static final int constant = 0;
27: public static final int variable = 1;
28: public static final int procedure = 2;
29: String name; //名字
30: int type; //类型,const var or procedur
31: int value; //数值,const使用
32: int lev; //所处层,var和procedur使用
33: int addr; //地址,var和procedur使用
34: int size; //需要分配的数据区空间,仅procedure使用
35:
36: public Item() {
37: super();
38: this.name = "";
39: }
40:
41: }
42:
43: /**
44: * 获得名字表某一项的内容
45: *
46: * @param i 名字表中的位置
47: * @return 名字表第i项的内容
48: */
49: public Item get(int i) {
50: if (table[i] == null) {
51: table[i] = new Item();
52: }
53: return table[i];
54: }
55:
56: /**
57: * 把某个符号登录到名字表中 名字表从1开始填,0表示不存在该项符号
58: *
59: * @param sym 要登记到名字表的符号
60: * @param k 该符号的类型:const,var,procedure
61: * @param lev 名字所在的层次
62: * @param dx 当前应分配的变量的相对地址,注意调用enter()后dx要加一
63: */
64: public void enter(Symbol sym, int type, int lev, int dx) {
65: tablePtr++;
66: Item item = get(tablePtr);
67: item.name = sym.id;
68: item.type = type;
69: switch (type) {
70: case Item.constant: //常量名字
71: item.value = sym.num; //记录下常数值的大小
72: break;
73: case Item.variable: //变量名字
74: item.lev = lev; //变量所在的层
75: item.addr = dx; //变量的偏移地址
76: break;
77: case Item.procedure: //过程名字
78: item.lev = lev;
79:
80: }
81: }
82:
83: /**
84: * 在名字表中查找某个名字的位置 查找符号表是从后往前查, 这样符合嵌套分程序名字定义和作用域的规定
85: *
86: * @param idt 要查找的名字
87: * @return 如果找到则返回名字项的下标,否则返回0
88: */
89: public int position(String idt) {
90: for (int i = tablePtr; i > 0; i--) //必须从后往前找
91: {
92: if (get(i).name.equals(idt)) {
93: return i;
94: }
95: }
96: return 0;
97: }
98:
99: /**
100: * 输出符号表内容,摘自block()函数
101: *
102: * @param start 当前符号表区间的左端
103: */
104: void debugTable(int start) {
105: if (tableswitch) //显示名字表与否
106: {
107: return;
108: }
109: System.out.println("**** Symbol Table ****");
110: if (start > tablePtr) {
111: System.out.println(" NULL");
112: }
113: for (int i = start + 1; i <= tablePtr; i++) {
114: try {
115: String msg = "unknown table item !";
116: switch (table[i].type) {
117: case Item.constant:
118: msg = " " + i + " const: " + table[i].name + " val: " + table[i].value;
119: break;
120: case Item.variable:
121: msg = " " + i + " var: " + table[i].name + " lev: " + table[i].lev + " addr: " + table[i].addr;
122: break;
123: case Item.procedure:
124: msg = " " + i + " proc: " + table[i].name + " lev: " + table[i].lev + " addr: " + table[i].size;
125: break;
126: }
127: System.out.println(msg);
128: PL0.tableWriter.write(msg + '\n');
129: } catch (IOException ex) {
130: ex.printStackTrace();
131: System.out.println("***write table intfo meet with error***");
132: }
133: }
134: }
135: }