第一章 编译程序概述
1.1 什么是编译程序
编译程序是现代计算机系统的基本组成部分之一,而且多数计算机系统都含有不止一个高级语言的编译程序。对有些高级语言甚至配置了几个不同性能的编译程序。
1.2编译过程概述和编译程序的结构
编译程序完成从源程序到目标程序的翻译工作,是一个复杂的整体的过程。从概念上来讲,一个编译程序的整个工作过程是划分成阶段进行的,每个阶段将源程序的一种表示形式转换成另一种表示形式,各个阶段进行的操作在逻辑上是紧密连接在一起的。一般一个编译过程划分成词法分析、语法分析、语义分析、中间代码生成,代码优化和目标代码生成六个阶段,这是一种典型的划分方法。事实上,某些阶段可能组合在一起,这些阶段间的源程序的中间表示形式就没必要构造出来了。我们将分别介绍各阶段的任务。另外两个重要的工作:表格管理和出错处理与上述六个阶段都有联系。编译过程中源程序的各种信息被保留在种种不同的表格里,编译各阶段的工作都涉及到构造、查找或更新有关的表格,因此需要有表格管理的工作;如果编译过程中发现源程序有错误,编译程序应报告错误的性质和错误发生的地点,并且将错误所造成的影响限制在尽可能小的范围内,使得源程序的其余部分能继续被编译下去,有些编译程序还能自动校正错误,这些工作称之为出错处理。图1.3表示了编译的各个阶段。
图1.3 编译的各个阶段 |
|
1.3 高级语言解释系统
为了实现在一个计算机上运行高级语言的程序,主要有两个途径:第一个途径是把该程序翻译为这个计算机的指令代码序列,这就是我们已经描述的编译过程。第二个途径是编写一个程序,它解释所遇到的高级语言程序中的语句并且完成这些语句的动作,这样的程序就叫解释程序。从功能上说,一个解释程序能让计算机执行高级语言。它与编译程序的主要不同是它不生成目标代码,它每遇到一个语句,就要对这个语句进行分析以决定语句的含义,执行相应的动作。右面的图示意了它的工作机理
第二章:PL/0编译程序
问答第1题 PL/0语言允许过程嵌套定义和递归调用,试问它的编译程序如何解决运行时的存储管理。
答: PL/0语言允许过程嵌套定义和递归调用,它的编译程序在运行时采用了栈式动态存储管理。(数组CODE存放的只读目标程序,它在运行时不改变。)运行时的数据区S是由解释程序定义的一维整型数组,解释执行时对数据空间S的管理遵循后进先出规则,当每个过程(包括主程序)被调用时,才分配数据空间,退出过程时,则所分配的数据空间被释放。应用动态链和静态链的方式分别解决递归调用和非局部变量的引用问题。
问答第2题 若PL/0编译程序运行时的存储分配策略采用栈式动态分配,并用动态链和静态链的方式分别解决递归调用和非局部变量的引用问题,试写出下列程序执行到赋值语句b∶=10时运行栈的布局示意图。
var x,y; procedure p; var a; procedure q; var b; begin (q) b∶=10; end (q); procedure s; var c,d; procedure r; var e,f; begin (r) call q; end (r); begin (s) call r; end (s); begin (p) call s; end (p); begin (main) call p; end (main).
答 : 程序执行到赋值语句b∶=10时运行栈的布局示意图为:
|
问答第3题
写出题2中当程序编译到r的过程体时的名字表table的内容。
name |
kind |
level/val |
adr |
size |
|
|
|
|
|
答 题2中当程序编译到r的过程体时的名字表table的内容为:
name |
kind |
level/val |
adr |
size |
|
x |
variable |
0 |
dx |
|
|
y |
variable |
0 |
dx+1 |
|
|
p |
procedure |
0 |
过程p的入口(待填) |
5 |
|
a |
variable |
1 |
dx |
|
|
q |
procedure |
1 |
过程q的入口 |
4 |
|
s |
procedure |
1 |
过程s的入口(待填) |
5 |
|
c |
variable |
2 |
dx |
|
|
d |
variable |
2 |
dx |
|
|
r |
procedure |
2 |
过程r的入口 |
5 |
|
e |
variable |
3 |
dx |
|
|
f |
variable |
3 |
dx+1 |
|
注意:q和s是并列的过程,所以q定义的变量b被覆盖。
问答第4题
指出栈顶指针T,最新活动记录基地址指针B,动态链指针DL,静态链指针SL与返回地址RA的用途。
答 : 栈顶指针T,最新活动记录基地址指针B,动态链指针DL,静态链指针SL与返回地址RA的用途说明如下:
T: 栈顶寄存器T指出了当前栈中最新分配的单元(T也是数组S的下标)。
B:基址寄存器,指向每个过程被调用时,在数据区S中给它分配的数据段起始 地址,也称基地址。
SL: 静态链,指向定义该过程的直接外过程(或主程序)运行时最新数据段的基地址,用以引用非局部(包围它的过程)变量时,寻找该变量的地址。
DL: 动态链,指向调用该过程前正在运行过程的数据段基地址,用以过程执行结束释放数据空间时,恢复调用该过程前运行栈的状态。
RA: 返回地址,记录调用该过程时目标程序的断点,即调用过程指令的下一条指令的地址,用以过程执行结束后返回调用过程时的下一条指令继续执行。
在每个过程被调用时在栈顶分配3个联系单元,用以存放SL,DL, RA。
问答第5题
PL/0编译程序所产生的目标代码是一种假想栈式计算机的汇编语言,请说明该汇编语言中下列指令各自的功能和所完成的操作。
· INT 0 A
· OPR 0 0
· CAL L A
答: PL/0编译程序所产生的目标代码中有3条非常重要的特殊指令,这3条指令在code中的位置和功能以及所完成的操作说明如下:
INT 0 A
在过程目标程序的入口处,开辟A个单元的数据段。A为局部变量的个数+3。
OPR 0 0
在过程目标程序的出口处,释放数据段(退栈),恢复调用该过程前正在运行的过程的数据段基址寄存器B和栈顶寄存器T的值,并将返回地址送到指令地址寄存器P中,以使调用前的程序从断点开始继续执行。
CAL L A
调用过程,完成填写静态链、动态链、返回地址,给出被调用过程的基地址值,送入基址寄存器B中,目标程序的入口地址A的值送指令地址寄存器P中,使指令从A开始执行。
问答第6题
给出对PL/0语言作如下功能扩充时的语法图和EBNF的语法描述。
(1) 扩充条件语句的功能使其为:
if〈条件〉then〈语句〉[else〈语句〉]
(2) 扩充repeat语句为:
repeat〈语句〉{
;〈语句〉}until〈条件〉
答 : 对PL/0语言作如下功能扩充时的语法图和EBNF的语法描述如下:
(1) 扩充条件语句的语法图为:
EBNF的语法描述为: 〈条件语句〉::= if〈条件〉then〈语句〉[else〈语句〉]
(2) 扩充repeat语句的语法图为:
EBNF的语法描述为: 〈 重复语句〉::= repeat〈语句〉{
;〈语句〉}until〈条件〉
第三章:词法分析程序
问答第1题
构造正规式1(0|1)*101相应的DFA.
答:先构造NFA:
用子集法将NFA确定化
. |
0 |
1 |
X |
. |
A |
A |
A |
AB |
AB |
AC |
AB |
AC |
A |
ABY |
ABY |
AC |
AB |
除X,A外,重新命名其他状态,令AB为B、AC为C、ABY为D,因为D含有Y(NFA的终态),所以D为终态。
. |
0 |
1 |
X |
. |
A |
A |
A |
B |
B |
C |
B |
C |
A |
D |
D |
C |
B |
DFA的状态图::
问答第2题
将下图确定化:
解:
用子集法将NFA确定化:
. |
0 |
1 |
S |
VQ |
QU |
VQ |
VZ |
QU |
QU |
V |
QUZ |
VZ |
Z |
Z |
V |
Z |