PL/0编译器(java version) – SymbolTable.java
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: }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
PL/0编译器(java version) – SymbolTable.java的更多相关文章
- PL/0编译器(java version) - MainFrame.java
1: /* 2: * To change this license header, choose License Headers in Project Properties. 3: * To chan ...
- PL/0编译器实践---后记
花了几天时间,把清华版的<编译原理>一书中的PL/0编译器实践了一遍.颇有收获,记录如下: 理解代码的技巧,如何理解一份代码,比如这个程序,其逻辑相对于一般程序就比较复杂了,如何翻译,虚拟 ...
- PL/0编译器(java version)–Praser.java
1: package compiler; 2: 3: import java.io.IOException; 4: import java.util.BitSet; 5: 6: /** 7: ...
- PL/0编译器(java version)–PL0.java
1: package compiler; 2: 3: import java.io.BufferedWriter; 4: import java.io.FileWriter; 5: 6: /* ...
- PL/0编译器(java version) – Scanner.java
1: package compiler; 2: 3: import java.io.BufferedReader; 4: import java.io.FileNotFoundException; ...
- PL/0编译器(java version) - Interpreter.java
1: package compiler; 2: 3: import java.io.BufferedReader; 4: import java.io.BufferedWriter; 5: imp ...
- PL/0编译器(java version) - Err.java
1: package compiler; 2: 3: import java.io.BufferedWriter; 4: 5: public class Err { 6: 7: publi ...
- PL/0编译器(java version) – Symbol.java
1: package compiler; 2: 3: /** 4: * 采用全局变量sym来存储符号码,并用全局变量id和num来传递语义值 5: * 6: * @author jiangnan ...
- PL/0编译器(java version)–Pcode.java
1: package compiler; 2: 3: /** 4: * //虚拟机指令 5: * 6: * @author jiangnan 7: * 8: */ 9: public class ...
随机推荐
- node 学习笔记 - path 处理
本文同步自我的个人博客:http://www.52cik.com/2015/12/04/learn-node-path.html path 模块是 node 用于整理.转换.合并路径的神器,只要是路径 ...
- HTML5之创新的视频拼图剖析式学习之二
昨天我们剖析了一下翻阅体验的实现.今天要剖析另外一个很有意思的效果——视频拼图. 网站中第一部分第二页<月熊的标志>是月熊志中互动性较强的一页,页面上会随机分布9块视频碎片,用户可以通过鼠 ...
- 个人觉得目前 最好用的Taobao API的NodeJS封装
话说,Top API SDK默认只给了四种语言的SDK,没有我大NodeJS,这可怎么行,于是封装了一个. 参考地址 GitHub: https://github.com/xiaoppp/TopAPI ...
- [BZOJ2429][HAOI2006]聪明的猴子(MST)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2429 分析:要让最大的最小,所以就是最小生成树上的啦,于是问题就变成了有多少个猴子&g ...
- RHCE认证考试教材
前段时间考RHCE7,顺便给大家分享下RHCE6.7的中文教材!毕竟此书是官方的培训教材,还是值得看看!RHEL6.7承前启后的,给个赞! 下载:http://pan.baidu.com/s/1nu9 ...
- win7 IIS 部署-vs2012开发网站-全是问题啊。。。
1.文件夹权限everyone2.aspnet_regiis.exe -i 表现为:
- 【JavaEE企业应用实战学习记录】struts配置文件详细解析
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-/ ...
- Error: [ng:areq]
错误描述:Error: [ng:areq] http://errors.angularjs.org/1.4.8/ng/areq?p0=HelloCtrl&p1=not%20a%20functi ...
- Elasticsearch: Indexing SQL databases. The easy way
Elasticsearchis a great search engine, flexible, fast and fun. So how can I get started with it? Thi ...
- 【BZOJ 1043】【HNOI 2008】下落的圆盘 判断圆相交+线段覆盖
计算几何真的好暴力啊. #include<cmath> #include<cstdio> #include<cstring> #include<algorit ...