课程设计报告.docx
《课程设计报告.docx》由会员分享,可在线阅读,更多相关《课程设计报告.docx(41页珍藏版)》请在冰豆网上搜索。
![课程设计报告.docx](https://file1.bdocx.com/fileroot1/2023-1/2/60d6b650-9dbd-48c9-a880-2f931612f915/60d6b650-9dbd-48c9-a880-2f931612f9151.gif)
课程设计报告
编译原理课程设计报告
学院:
装备制造学院
专业:
计算机12级
学号:
1233050168
姓名:
徐晓臣
教师:
闫红
时间:
2014.6.16—2014.6.23
成绩:
第一部分:
词法分析
一.词法分析的功能:
输入---源程序文件
原程序文件名:
pas.cpp
内容:
ifa=0thenb=1doa-a*b
#@
输出---
(1)相应Token序列
(‘f’,”NUL”)(‘i’,”a”)(‘=’,”NUL”)(‘x’,”0”)
(‘t’,”NUL”)(‘i’,”b”)(‘=’,”NUL”)(‘x’,”1”)(‘d’,”NUL”)(‘i’,”a”)(‘-’,”NUL”)(‘i’,”a”)(‘*’,”NUL”)(‘i’,”b”)
(2)关键字、界限符、常数等内部码
内部码
编码号
if
Sy_if
0
then
Sy_then
1
else
Sy_else
2
while
Sy_while
3
begin
Sy_begin
4
do
Sy_do
5
end
Sy_end
6
标识符
Ident
56
#
JingHao
10
;
Semicolon
8
+
Plus
34
*
Times
36
:
Becomes
38
(
lParent
48
)
rParent
49
二.词法分析程序设计
1.单词---------------Token序列
2.关键字等对照表
内部码
编码号
if
Sy_if
0
then
Sy_then
1
else
Sy_else
2
while
Sy_while
3
begin
Sy_begin
4
do
Sy_do
5
end
Sy_end
6
标识符
Ident
56
#
JingHao
10
;
Semicolon
8
+
Plus
34
*
Times
36
:
Becomes
38
(
lParent
48
)
rParent
49
单词
内部码
编码号
if
Sy_if
0
…
…
…
3.画出自动机转换图
三.程序实现
1.数据结构
1structrWordsResWords[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}};
定义单词数组。
{charsp[10];intsy;};是对单词数组的数范围定义
2.staticintaction[19][13]=
{{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,ACC,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1,6,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1,7,-1,-1,-1},
{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,9,8},
{-1,-1,104,-1,-1,-1,104,-1,104,-1,104,-1,-1},
{-1,10,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,11,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,12,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,105,-1,13,-1,-1,-1,-1},
{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,14,-1},
{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,15,-1},
{-1,-1,103,-1,-1,-1,103,-1,103,-1,103,-1,-1},
{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,9,16},
{-1,-1,17,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,102,-1,-1,-1,102,-1,102,-1,102,-1,-1},
{-1,-1,-1,-1,-1,-1,106,-1,-1,-1,-1,-1,-1},
{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,18,-1},
{-1,-1,101,-1,-1,-1,101,-1,101,-1,101,-1,-1}
};
staticintaction1[10][7]=
{{3,-1,-1,2,-1,-1,1},
{-1,4,5,-1,-1,ACC,-1},
{3,-1,-1,2,-1,-1,6},
{-1,104,104,-1,104,104,-1},
{3,-1,-1,2,-1,-1,7},
{3,-1,-1,2,-1,-1,8},
{-1,4,5,-1,9,-1,-1},
{-1,101,5,-1,101,101,-1},
{-1,102,102,-1,102,102,-1},
{-1,103,103,-1,103,103,-1}
};
staticintaction2[16][11]=
{{1,-1,4,-1,5,-1,-1,-1,13,7,8},
{-1,2,-1,101,-1,101,101,101,-1,-1,-1},
{3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,102,-1,102,102,102,-1,-1,-1},
{1,-1,4,-1,5,-1,-1,-1,11,7,8},
{1,-1,4,-1,5,-1,-1,-1,6,7,8},
{-1,-1,-1,104,-1,9,10,104,-1,-1,-1},
{1,-1,4,-1,5,-1,-1,-1,14,7,8},
{1,-1,4,-1,5,-1,-1,-1,15,7,8},
{105,-1,105,-1,105,-1,-1,-1,-1,-1,-1},
{107,-1,107,-1,107,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,12,-1,9,10,-1,-1,-1,-1},
{-1,-1,-1,103,-1,103,103,103,-1,-1,-1},
{-1,-1,-1,-1,-1,9,10,ACC,-1,-1,-1},
{-1,-1,-1,106,-1,9,10,106,-1,-1,-1},
{-1,-1,-1,108,-1,9,10,108,-1,-1,-1}
};
staticintaction[19][13],staticintaction1[10][7],staticintaction2[16][11]:
是对SLR
(1)的定义
3.while(ss1==0&&ii{if(!
strcmp(spe1,nTab1[ii]))ss1=1;
ii++;
}if(ss1==1)
returnii-1;
else
return-1;}:
是以循环集组成的。
4. structFourExp
{
charop[10];
structaaarg1;
structaaarg2;
intresult;
}fexp[200];
2.算法设计
1.voidScan是单词浏览的函数,扫描小写字母,将扫描的函数放入Identifer,还对数字进行扫描,放到number中。
若是‘=’放入Readch,查找是=等于。
2.EO是关系表达式的和取
3.eGn是一个四元式,nxp是四元式eGn的下标,将数据放入NXP中。
四.心得体会
通过本次实验,我了解到很多,开始不会设计,但是在我查阅资料和书籍之后,还有就是同学对我的帮助,我就成功的完成了程序的实现,知道了四元式是怎样实现的,而且还知道怎样通过四元式能够写出TIKEN的式子,知道四元式是怎么通过C++的只是实现的,这次试验我学到很多,以后会更加努力的。
第二部分语法分析
一.根据给定的文法画出算术表达式、布尔表达式、程序语句的LR(0)项目集规范族,验证上述的SLR分析表是否正确。
算术表达式的项目集规范族
I0:
S'→.E
E→.E+E
E→.E*E
E→.(E)
E→.I
I2:
E→(.E)
E→.E+E
E→.E*E
E→.(E)
E→.I
I4:
E→E+.E
E→.E+E
E→.E*E
E→.(E)
E→.i
I6:
E→(E.)
E→E.+E
E→E.*E
I8:
E→E*E.
E→E.+E
E→E.*E
I1:
S'→E.
E→E.+E
E→E.*E
E→.E+E
E→.E*E
E→.(E)
E→.i
I3:
E→i.
I5:
E→E*.E
E→.E+E
E→.E*E
E→.(E)
E→.i
I7:
E→E+E.
E→E.+E
E→E.*E
I9:
E→(E).
有错误,在6状态下当前输入符号为+时,应移进4状态,即s4.
布尔表达式的SLR
(1)分析表:
状态
ACTION
GOTO
i
rop
(
)
not
and
or
#
B
A
O
0
S1
S4
S3
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)程序语句的项目集规范族
I0:
S'→.S
S→.ifethenSelseS
S→.whileedoS
S→.beginLend
S→.a
I4:
S→begin.Lend
L→.S
L→.S;l
S→.ifethenSelseS
S→.whileedoS
S→.beginLend
S→.a
I8:
S→beginL.end
I12:
S→beginLend.
I16:
L→S;L.
I1:
S'→S.
I5:
S→a.
I9:
L→S.
L→S.;L
I13:
L→S;.L
L→.S
L→.S;L
S→.ifethenSelseS
S→.whileedoS
S→.beginLend
S→.a
I17:
S→ifethenSelse.S
S→.ifethenSelseS
S→.whileedoS
S→.beginLend
S→.a
I2:
S→if.ethenSelseS
I6:
S→ife.thenSelseS
I10:
S→ifethen.SelseS
S→.ifethenSelseS
S→.whileedoS
S→.beginLend
S→.a
I14:
S→ifethenS.elseS
I18:
S→ifethenSelseS.
I3:
S→while.edoS
I7:
S→whilee.doS
I11:
S→whileedo.S
S→.ifethenSelseS
S→.whileedoS
S→.beginLend
S→.a
I15:
S→whileedoS.
有错误,在0状态下当前输入符号为if时,应移进2状态,即s2
在0状态下当前输入符号为begin时,应移进4状态,即s4
二.语法分析程序的设计
1.分析框图,即程序之间的调用关系
2,四元式的结构形式。
100j>a0102
101j0
102=1b
一共五列,
第一列为地址,在输出程序Disp2()中用temp1表示;
第二列是算符OP,存储在程序中fexp[temp1].op中;
第三列是第一操作数,存储在程序中的nTab1[fexp[temp1].arg1.pos]中;
第四列是第二操作数,存储在程序中的nTab1[fexp[temp1].arg2.pos]中;
第五列是出口,程序中用tr和fr分别存储真出口和假出口
3.程序实现
#include
#include
#include
usingnamespacestd;
constintACC=-2;
constintSy_if=0;
constintSy_then=1;
constintSy_else=2;
constintSy_while=3;
constintSy_begin=4;
constintSy_do=5;
constintSy_end=6;
constinta=7;
constintSemicolon=8;
constinte=9;
constintJingHao=10;
constintS=11;
constintL=12;
constintTempsy=15;
constintEA=18;
constintEO=19;
constintPlus=34;
constintTimes=36;
constintBecomes=38;
constintop_and=39;
constintop_or=40;
constintop_not=41;
constintrop=42;
constintlParent=48;
constintrParent=49;
constintident=56;
constintintConst=57;
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];
structaaoth1;
structFourExp
{
charop[10];
structaaarg1;
structaaarg2;
intresult;
}fexp[200];
intssp=0;
structaa*pbuf=buf;
intnLength=0;
intLnum=0;
inttt1=0;
ifstreamcfile("pas.cpp");
ofstreammfile("pas.txt");
intnewt=0;
intnxq=100;
intIr;
intIr1;
intsp=0;
intstack1[100];
intsp1=0;
intnum=0;
structll
{
intnxq1;
inttc1;
intfc1;
}LabelMark[10];
intLabelTemp[10];
intPointMark=-1,PointTemp=-1;
intsign=0;
staticintaction[19][13]=
{{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,ACC,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1,6,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1,7,-1,-1,-1},
{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,9,8},
{-1,-1,104,-1,-1,-1,104,-1,104,-1,104,-1,-1},
{-1,10,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,11,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,12,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,105,-1,13,-1,-1,-1,-1},
{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,14,-1},
{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,15,-1},
{-1,-1,103,-1,-1,-1,103,-1,103,-1,103,-1,-1},
{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,9,16},
{-1,-1,17,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,102,-1,-1,-1,102,-1,102,-1,102,-1,-1},
{-1,-1,-1,-1,-1,-1,106,-1,-1,-1,-1,-1,-1},
{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,18,-1},
{-1,-1,101,-1,-1,-1,101,-1,101,-1,101,-1,-1}
};
staticintaction1[10][7]=
{{3,-1,-1,2,-1,-1,1},
{-1,4,5,-1,-1,ACC,-1},
{3,-1,-1,2,-1,-1,6},
{-1,104,104,-1,104,104,-1},
{3,-1,-1,2,-1,-1,7},
{3,-1,-1,2,-1,-1,8},
{-1,4,5,-1,9,-1,-1},
{-1,101,5,-1,101,101,-1},
{-1,102,102,-1,102,102,-1},
{-1,103,103,-1,103,103,-1}
};
staticintaction2[16][11]=
{{1,-1,4,-1,5,-1,-1,-1,13,7,8},
{-1,2,-1,101,-1,101,101,101,-1,-1,-1},
{3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,102,-1,102,102,102,-1,-1,-1},
{1,-1,4,-1,5,-1,-1,-1,11,7,8},
{1,-1,4,-1,5,-1,-1,-1,6,7,8},
{-1,-1,-1,104,-1,9,10,104,-1,-1,-1},
{1,-1,4,-1,5,-1,-1,-1,14,7,8},
{1,-1,4,-1,5,-1,-1,-1,15,7,8},
{105,-1,105,-1,105,-1,-1,-1,-1,-1,-1},
{107,-1,107,-1,107,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,12,-1,9,10,-1,-1,-1,-1},
{-1,-1,-1,103,-1,103,103,103,-1,-1,-1},
{-1,-1,-1,-1,-1,9,10,ACC,-1,-1,-1},
{-1,-1,-1,106,-1,9,10,106,-1,-1,-1},
{-1,-1,-1,108,-1,9,10,108,-1,-1,-1}
};
voidReadLine()
{
charch1;
Pline=Line;
ch1=cfile.get();
while(ch1!
='\n')
{
*Pline=ch1;
Pline++;
ch1=cfile.get();
}
*Pline='\0';
Pline=Line;
}
voidReadch()
{
if(ch=='\0')
{
ReadLine();
L