1、课程设计报告编译原理课程设计报告学 院: 装备制造学院 专 业: 计算机12级 学 号: 1233050168 姓 名: 徐 晓 臣 教 师: 闫 红 时 间:2014.6.162014.6.23成 绩: 第一部分:词法分析一词法分析的功能: 输入-源程序文件原程序文件名:pas.cpp内容: if a =0 then b = 1 do a-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
2、,”b”) (2)关键字、界限符、常数等内部码内部码编码号ifSy_if0thenSy_then1elseSy_else2whileSy_while3beginSy_begin4doSy_do5endSy_end6标识符Ident56#JingHao10;Semicolon8+Plus34*Times36:Becomes38(lParent48)rParent49二词法分析程序设计1单词- -Token序列2关键字等对照表内部码编码号ifSy_if0thenSy_then1elseSy_else2whileSy_while3beginSy_begin4doSy_do5endSy_end6标识符
3、Ident56#JingHao10;Semicolon8+Plus34*Times36:Becomes38(lParent48)rParent49单 词内部码编码号if Sy_if03.画出自动机转换图三程序实现1数据结构1 struct rWords ResWords10 = 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;定义单词数组。char sp10;int sy; ;是对单词数组的数范
4、围定义2. static int action1913 = 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
5、, -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
6、,-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;static int action1107 = 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,
7、-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; static int action21611 = 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,
8、-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
9、,-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; static int action1913,static int action1107 ,static int action21611:是对SLR(1)的定义3. while (ss1 = 0 & ii a 0 102 101 j 0 102 = 1 b一共五列,第一列为地址,在输出程序Disp2( )中用temp1表示;第二列是算符OP,存储在程序中fexptemp1.op中;第三列是第一操作数,存储在程序中的nTab1fexpte
10、mp1.arg1.pos中;第四列是第二操作数,存储在程序中的nTab1fexptemp1.arg2.pos中;第五列是出口,程序中用tr和fr分别存储真出口和假出口3程序实现#include #include #include using namespace std;const int ACC = -2;const int Sy_if = 0;const int Sy_then = 1;const int Sy_else = 2;const int Sy_while = 3;const int Sy_begin = 4;const int Sy_do = 5;const int Sy_end
11、 = 6;const int a = 7;const int Semicolon = 8;const int e = 9;const int JingHao = 10;const int S = 11;const int L = 12;const int Tempsy = 15;const int EA = 18;const int EO = 19;const int Plus = 34;const int Times = 36;const int Becomes = 38;const int op_and = 39;const int op_or = 40;const int op_not
12、= 41;const int rop = 42;const int lParent = 48;const int rParent = 49;const int ident = 56;const int intConst = 57;char ch = 0;int Count = 0;static char Spelling10 = ;static char Line81 = ;char *Pline;static char nTab110010;struct nTab int tc; int fc;nTab2200;int Label = 0;struct rWords char sp10; i
13、nt sy;struct rWords ResWords10 = 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;struct aa int sy1; int pos;buf1000,n,n1,E,sstack100,ibuf100,stack1000;struct aa oth1;struct FourExp char op10; struct aa arg1; struct aa arg2;
14、 int result;fexp200;int ssp = 0;struct aa *pbuf = buf;int nLength = 0;int Lnum = 0;int tt1 = 0;ifstream cfile(pas.cpp);ofstream mfile(pas.txt);int newt = 0;int nxq = 100;int Ir;int Ir1;int sp = 0;int stack1100;int sp1 = 0;int num = 0;struct ll int nxq1; int tc1; int fc1;LabelMark10;int LabelTemp10;i
15、nt PointMark = -1,PointTemp = -1;int sign = 0;static int action1913 = 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
16、, -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,-
17、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;static int action1107 = 3,-1,-1,2,-1,-1,1, -1,4,5,-1,-1,ACC,-1, 3,-1,-1,2,-1,-1,6,
18、 -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; static int action21611 = 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,
19、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,1
20、03,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;void ReadLine( ) char ch1; Pline = Line; ch1 = cfile.get(); while( ch1 != n) *Pline = ch1; Pline +; ch1 = cfile.get(); *Pline = 0; Pline = Line;void Readch( ) if (ch = 0) ReadLine( ); L
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1