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 ...
随机推荐
- 分享:计算机图形学期末作业!!利用WebGL的第三方库three.js写一个简单的网页版“我的世界小游戏”
这几天一直在忙着期末考试,所以一直没有更新我的博客,今天刚把我的期末作业完成了,心情澎湃,所以晚上不管怎么样,我也要写一篇博客纪念一下我上课都没有听,还是通过强大的度娘完成了我的作业的经历.(当然作业 ...
- How to build windows azure PowerShell Source Code
Download any version source code of Windows Azure Powershell from https://github.com/Azure/azure-sdk ...
- [转]Win2003打不开https的问题
转自:http://ljl-xyf.iteye.com/blog/2269834 碰到客户做问题是能打开https://www.baidu.com 这个网页 打不开 https://sha256.al ...
- [CF#290 Div.1 C]Fox And Dinner(最大流)
题目:http://codeforces.com/contest/512/problem/C 题目大意:给你若干个数,让你分成k组,每组围成一个圆,使得相邻两个数和均为素数,且每组人数应>=3个 ...
- JavaScript的理解记录(4)
客户端JavaScript:客户端就是Web浏览器; 一. 前奏: Web文档(document):一些呈现静态信息的页面,虽然有的页面是会动的,但信息本身还是静态! Web应用:可以动态载入信息,相 ...
- js简易函数性能测试器
如果你不想用浏览器的js性能测试工具,可以用下面这个简单的函数测试一下(1毫秒一下的就测不出来了) function testFn(fn,param){ var start = new Date(). ...
- 1025关于explain的补充1
https://segmentfault.com/q/1010000004195469 我的困惑 http://www.cnblogs.com/BeginMan/p/3754322.html 可以指定 ...
- “Ceph浅析”系列之七——关于Ceph的若干想法
本篇文章的内容,主要是笔者在调研分析Ceph过程中产生的一些思考.因为其中的内容比较自由发散,且大多是笔者的个人见解,故此另启一文进行讨论. 关于Ceph的性能 目前为止,本系列的文章中没有涉及到Ce ...
- Java--笔记(1)
1.Swing 是在AWT的基础上构建的一套新的图形界面系统,它提供了AWT 所能够提供的所有功能,并且用纯粹的Java代码对AWT 的功能进行了大幅度的扩充.AWT 是基于本地方法的C/C++程序, ...
- mysql 数据库隔离级别
select @@tx_isolation; 4种隔离级别 1.read uncommitted 2.read committed 3.repeatable read(MySQL默认隔离级别) 4. ...