编译原理大作业。。。
- 注释在/* 和 */中
- 布尔类型
- 一维数组类型
- repeat until语句
- else 子句
- for语句
- 分离解释器与编译器
- 增加错误处理
- ++、--、+=、-=运算
- tra:将数组下标的范围入栈
- jug:判断数组下标范围是否合法
- wta:写数组数据
- rda:读数组数据
- opr 0 14: not指令,对bool型取反
- opr 0 15: and指令
- opr 0 16: or指令
- "Found ':=' when expexting '='.",
- "There must be a number to follow '='.",
- "There must be an '=' to follow the identifier.",
- "There must be an identifier to follow 'const', 'var', or 'procedure'.",
- "Missing ',' or ';'.",
- "Incorrect procedure name.",
- "Statement expected.",
- "Follow the statement is an incorrect symbol.",
- "'.' expected.",
- "';' expected.",
- "Undeclared identifier.",
- "Illegal assignment.",
- "':=' expected.",
- "There must be an identifier to follow the 'call'.",
- "A constant or variable can not be called.",
- "'then' expected.",
- "';' or 'end' expected.",
- "'do' expected.",
- "Incorrect symbol.",
- "Relative operators expected.",
- "Procedure identifier can not be in an expression.",
- "Missing ')'.",
- "The symbol can not be followed by a factor.",
- "The symbol can not be as the beginning of an expression.",
- "The number is too great.",
- "Redeclear identifier",
- "The array index should be constant.",
- "Missing ']'.",
- "The array index should be > 0 .",
- "Array index Illegal.",
- "There are too many levels."
"and ",
"begin ",
"bool ",
"call ",
"const ",
"do ",
"downto ",
"else ",
"end ",
"false ",
"for ",
"if ",
"not ",
"odd ",
"or ",
"procedure ",
"repeat ",
"then ",
"to ",
"true ",
"until ",
"var ",
"while "
分别用gcc编译编译器pl0.c和解释器interpreter.c 使用测试代码example.pl0:
const a = 0;
var x,y,b[5];
bool i, j;
begin
i := true;
j := i or false and false;
x := y;
y := 3;
y := (x--);
b[3] := 1;
b[1] := x;
b[2] := x;
/*call aaa;*/
/*edrmalfuei*/
end.
运行编译器生成中间代码,默认生成的中间代码文件名为“out.txt”:
中间代码:
0 jmp 0 1
1 int 0 7
2 lit 0 1
3 sto 0 5
4 lod 0 5
5 lit 0 0
6 opr 0 16
7 lit 0 0
8 opr 0 15
9 sto 0 6
10 lod 0 4
11 sto 0 3
12 lit 0 3
13 sto 0 4
14 lod 0 3
15 lit 0 1
16 opr 0 3
17 sto 0 3
18 lod 0 3
19 lit 0 1
20 opr 0 2
21 sto 0 4
22 lit 0 1
23 wta 0 5
24 lod 0 3
25 wta 0 5
26 lod 0 3
27 wta 0 5
28 opr 0 0
## 存在的问题 使用pl0.pdf中提供的测试程序时,解释器会多输出一段内容,编译器没问题
解释器输出:发现后面多了一段内容