编译技术课程设计.docx
《编译技术课程设计.docx》由会员分享,可在线阅读,更多相关《编译技术课程设计.docx(45页珍藏版)》请在冰点文库上搜索。
编译技术课程设计
课程设计报告
课程设计名称:
编译技术
系:
三系
学生姓名:
孙**
班级:
学号:
成绩:
指导教师:
石鲁生
开课时间:
2011-2012学年2学期
一.设计题目
小型程序设计语言编译器的设计与实现
二.主要内容
自定义一种简单的小型高机程序设计语言,采用任意一种编程语言和编程工具设计并实现其编译器,展示编译系统中词法分析和语法分析的全过程。
三.具体要求
1、对单词的构词规则有明确的定义;
2、编写的词法分析程序能够正确识别源程序中的单词符号;
3、识别出的单词以<种别码,值>的二元式形式保存在符号表中并输出;
4、构造出程序设计语言各语法单位的SLR分析表;(其他分析表亦可)
5、能够对输入的源程序做出正确的语法分析并输出清晰的结果;
6、高级语言的规模不易过大,注重编译器内核的设计,对于界面无要求;
7、课程设计报告中应对所自定义高级语言进行简单描述,对编译器的设计进行简单的需求分析,给出设计说明和程序结构框架,阐明设计思路、用到的原理和方法。
8、不可更改课程设计报告中已有的字体和格式,课程设计报告中新加入内容,汉字采用宋体五号字,英文字符和数字采用TimesNewRoman五号字。
9、课程设计报告中的运行结果应以程序运行截图形式出现。
10、程序源代码以邮件名称:
“班级学号姓名”发送至指定邮箱:
sqckcsj08@
四.进度安排
序号
内容
时间(天)
1
布置题目、查找资料
1
2
概要设计
1
3
详细设计
3
4
调试分析
1
5
测试结果
0.5
6
总结和完成设计报告
1
合计
7.5
五.成绩评定
1.考核方法:
通过现场考勤、检查源程序和批改课程设计报告相结合的方式考察学生的动手能力,独立分析解决问题的能力和创新精神,并根据学生的学习态度综合考评。
2.成绩评定:
成绩分优、良、中、及格和不及格五等。
六.设计思路
1.词法分析
(1)单词符号及种别表
符号
种别编码
说明
sy_if
0
保留字if
sy_then
1
保留字then
sy_else
2
保留字else
sy_while
3
保留字whlie
sy_begin
4
保留字begin
sy_do
5
保留字do
sy_end
6
保留字end
a
7
赋值语句
semicolon
8
“;”
e
9
布尔表达式
jinghao
10
“#”
S
11
语句
L
12
复合语句
tempsy
15
临时变量
EA
18
Band
EO
19
Bor
plus
34
“+”
times
36
“*”
becomes
38
“:
=”
op_and
39
“and”
op_or
40
“or”
op_not
41
“not”
rop
42
关系运算符
lparent
48
“(”
rparent
49
“)”
ident
56
变量
intconst
57
整常量
2.SLR的分析表:
1).算术表达式的SLR
(1)分析表:
算数表达式文法G[E]如下:
E->E+E|E*E|(E)|i
将文法G[E]拓广为文法G’[E]:
(0)S’→E
(1)E→E+E
(2)E→E*E
(3)E→(E)
(4)E→i
由此得到算数表达式的SLR
(1)分析表如下:
状态
ACTION
GOTO
i
+
*
(
)
#
E
0
s3
s2
1
1
s4
s5
acc
2
s3
s2
6
3
r4
r4
r4
r4
4
s3
s2
7
5
s3
s2
8
6
s4
s5
s9
7
r1
s5
r1
r1
8
r2
r2
r2
r2
9
r3
r3
r3
r3
2).布尔表达式的SLR分析表:
布尔表达式的文法如下:
B->B∧B|B∨B|¬B|Iropi|i
为了便于语法分析时加工处理,我们将上述文法改为文法G[S]:
B→BAB|B0B|¬B|(B)|Iropi|i
BA→B∧
B0→B∨
将文法G[S]拓广为文法G[S’]:
(0)S’→B
(1)B→i
(2)B→Iropi
(3)B→(B)
(4)B→NOTB
(5)A→BAND
(6)B→AB
(7)O→BOR
(8)B→OB
由此得到布尔表达式的SLR
(1)分析表如下:
状态
ACTION
GOTO
i
rop
(
)
NOT
AND
OR
#
B
A
O
0
s1
s4
s5
13
7
8
1
s2
r1
r1
r1
r1
2
s3
3
r2
r2
r2
r2
4
s1
s4
s5
11
7
8
5
s1
s4
s5
6
7
8
6
r4
s9
s10
r4
7
s1
s4
s5
14
7
8
8
s1
s4
s5
15
7
8
9
r5
r5
r5
10
r7
r7
r7
11
s12
s9
s10
12
r3
r3
r3
r3
13
s9
s10
acc
14
r6
s9
s10
r6
15
r8
s9
s10
r8
3).程序语句的SLR分析表:
程序语句的文法G[S]如下
S→ifethenSelseS|whileedoS|beginLend|a
L→S;L|S
由于在编译程序设计与视线中,我们是将赋值语句与算数表达式归为一类处理的,故在此将赋值语句仅看作是程序语句文法中一个终结符a,将布尔表达式B也看作为终结符e。
将文法G[S]拓广为文法G[S’]:
(0)S’→S
(1)S→ifethenSelseS
(2)S→whileedoS
(3)S→beginLend
(4)S→a
(5)L→S
(6)L→S;L
由此得到程序语句的SLR
(1)分析表如下:
状态
ACTION
GOTO
if
then
else
while
begin
do
end
a
;
e
#
S
L
0
s2
s3
s4
s5
1
1
acc
2
s6
3
s7
4
s2
s3
s4
s5
9
8
5
r4
r4
r4
r4
6
s10
7
s11
8
s12
9
r5
s13
10
s2
s3
s4
s5
14
11
s2
s3
s4
s5
15
12
r3
r3
r3
r3
13
s2
s3
s4
s5
9
16
14
s17
15
r2
r2
r2
r2
16
r6
17
s2
s3
s4
s5
18
18
r1
r1
r1
r1
七.源程序及运行结果
1)源代码:
#include
#include
#include
#defineACC-2
/***************************************************************/
#definesy_if0
#definesy_then1
#definesy_else2
#definesy_while3
#definesy_begin4
#definesy_do5
#definesy_end6
#definea7
#definesemicolon8
#definee9
#definejinghao10
#defineS11
#defineL12
#definetempsy15
#defineEA18
#defineEO19
#defineplus34
#definetimes36
#definebecomes38
#defineop_and39
#defineop_or40
#defineop_not41
#definerop42
#definelparent48
#definerparent49
#defineident56
#defineintconst57
/***************************************************************/
charch='\0';
intcount=0;
staticcharspelling[10]={""};
staticcharline[81]={""};
char*pline;
staticcharntab1[100][10];
structntab{
inttc;
intfc;
}ntab2[200];
intlabel=0;
structrwords{
charsp[10];
intsy;
};
structrwordsreswords[10]={
{"if",sy_if},
{"do",sy_do},
{"else",sy_else},
{"while",sy_while},
{"then",sy_then},
{"begin",sy_begin},
{"end",sy_end},
{"and",op_and},
{"or",op_or},
{"not",op_not}
};
structaa{
intsy1;
intpos;
}buf[1000],
n,
n1,
E,
sstack[100],
ibuf[100],
stack[1000];
structaaoth;
structfourexp{
charop[10];
structaaarg1;
structaaarg2;
intresult;
}fexp[200];
intssp=0;
structaa*pbuf=buf;
intnlength=0;
intlnum=0;
inttt1=0;
FILE*cfile;
/***************************************************************/
intnewt=0;
intnxq=100;
intlr;
intlr1;
intsp=0;
intstack1[100];
intsp1=0;
intnum=0;
structll{
intnxq1;
inttc1;
intfc1;
}labelmark[10];
intlabeltemp[10];
intpointmark=-1,
pointtemp=-1;
intsign=0;
/*********************程序语句的LR分析表**********************/
staticintaction[19][13]={
/*0*/{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,1,-1},
/*1*/{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,ACC,-1,-1},
/*2*/{-1,-1,-1,-1,-1,-1,-1,-1,-1,6,-1,-1,-1},
/*3*/{-1,-1,-1,-1,-1,-1,-1,-1,-1,7,-1,-1,-1,},
/*4*/{2,-1,-1,3,5,-1,-1,5,-1,-1,-1,9,8},
/*5*/{-1,-1,104,-1,-1,-1,104,-1,104,-1,104,-1,-1},
/*6*/{-1,10,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
/*7*/{-1,-1,-1,-1,-1,11,-1,-1,-1,-1,-1,-1,-1},
/*8*/{-1,-1,-1,-1,-1,-1,12,-1,-1,-1,-1,-1,-1},
/*9*/{-1,-1,-1,-1,-1,-1,105,-1,13,-1,-1,-1,-1},
/*10*/{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,14,-1},
/*11*/{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,14,-1},
/*12*/{-1,-1,103,-1,-1,-1,103,-1,103,-1,103,-1,-1},
/*13*/{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,9,16},
/*14*/{-1,-1,17,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
/*15*/{-1,-1,102,-1,-1,-1,102,-1,102,-1,102,-1,-1},
/*16*/{-1,-1,-1,-1,-1,-1,106,-1,-1,-1,-1,-1,-1},
/*17*/{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,18,-1},
/*18*/{-1,-1,101,-1,-1,-1,101,-1,101,-1,101,-1,-1}
};
/*****************算术表达式的LR分析表***********************/
staticintaction1[10][7]={
/*0*/{3,-1,-1,2,-1,-1,1},
/*1*/{-1,4,5,-1,-1,ACC,-1},
/*2*/{3,-1,-1,2,-1,-1,6},
/*3*/{-1,104,104,-1,104,104,-1},
/*4*/{3,-1,-1,2,-1,-1,7},
/*5*/{3,-1,-1,2,-1,-1,8},
/*6*/{-1,4,5,-1,9,-1,-1},
/*7*/{-1,101,5,-1,101,101,-1},
/*8*/{-1,102,102,-1,102,102,-1},
/*9*/{-1,103,103,-1,103,103,-1}
};
/***************布尔表达式LR分析表**************************/
staticintaction2[16][11]={
/*0*/{1,-1,4,-1,5,-1,-1,-1,13,7,8},
/*1*/{-1,2,-1,101,101,101,-1,-1,-1,},
/*2*/{3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
/*3*/{-1,-1,-1,102,-1,102,102,102,-1,-1,-1},
/*4*/{1,-1,4,-1,5,-1,-1,-1,11,7,8},
/*5*/{1,-1,4,-1,5,-1,-1,-1,6,7,8},
/*6*/{-1,-1,-1,104,-1,9,10,104,-1,-1,-1},
/*7*/{1,-1,4,-1,5,-1,-1,-1,14,7,8},
/*8*/{1,-1,4,-1,5,-1,-1,-1,15,7,8},
/*9*/{105,-1,105,-1,105,-1,-1,-1,-1,-1,-1},
/*10*/{107,-1,107,-1,107,-1,-1,-1,-1,-1,-1},
/*11*/{-1,-1,-1,12,-1,9,10,-1,-1,-1,-1},
/*12*/{-1,-1,-1,103,-1,103,103,103,-1,-1,-1},
/*13*/{-1,-1,-1,-1,-1,9,10,ACC,-1,-1,-1},
/*14*/{-1,-1,-1,106,-1,9,10,106,-1,-1,-1},
/*15*/{-1,-1,-1,108,-1,9,10,108,-1,-1,-1}
};
/*************从文件读取一行到缓冲区*************************/
voidreadline()
{
charch1;
pline=line;
ch1=getc(cfile);
while(ch1!
='\n'){
*pline=ch1;
pline++;
ch1=getc(cfile);
}
*pline='\0';
pline=line;
}
/*****************从缓冲区读取一个字符***********************/
voidreadch()
{
if(ch=='\0'){
readline();
lnum++;
}
ch=*pline;
pline++;
}
/****************标识符和关键字的识别*************************/
intfind(charspel[])
{
intss1=0;
intii=0;
while((ss1==0)&&(iiif(!
strcmp(spel,ntab1[ii]))ss1=1;
ii++;
}
if(ss1==1)returnii-1;
elsereturn-1;
}
voididentifier()
{
intiii=0,j,k;
intss=0;
k=0;
do{
spelling[k]=ch;
k++;
readch();
}while(((ch>='a')&&(ch<='z'))||((ch>='0')&&(ch<='9')));
pline--;
spelling[k]='\0';
while((ss==0)&&(iii<10)){
if(!
strcmp(spelling,reswords[iii].sp))ss=1;
iii++;
}
/*关键字匹配*/
if(ss==1){
buf[count].sy1=reswords[iii-1].sy;
}
else{
buf[count].sy1=ident;
j=find(spelling);
if(j==-1){
buf[count].pos=tt1;
strcpy(ntab1[tt1],spelling);
tt1++;
nlength++;
}
elsebuf[count].pos=j;
}
count++;
}
/********************数字识别*********************************/
voidnumber()
{
intivalue=0;
intdigit;
do{
digit=ch-'0';
ivalue=ivalue*10+digit;
readch();
}while((ch>='0')&&(ch<='9'));
buf[count].sy1=intconst;
buf[count].pos=ivalue;
count++;
pline--;
}
/******************扫描主函数********************************/
voidscan()
{
//inti;
while(ch!
='~'){
switch(ch){
case'':
break;
case'a':
case'b':
case'c':
case'd':
case'e':
case'f':
case'g':
case'h':
case'i':
case'j':
case'k':
case'l':
case'm':
case'n':
case'o':
case'p':
case'q':
case'r':
case's':
case't':
case'u':
case'v':
case'w':
case'x':
case'y':
case'z':
identifier();
break;
case'0':
case'1':
case'2':
case'3':
case