《编译原理-C++编译器课程设计报告(共27页).doc》由会员分享,可在线阅读,更多相关《编译原理-C++编译器课程设计报告(共27页).doc(27页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上编译器的设计与分析学 号: 姓 名: 李 博 专 业: 计算机科学与技术 _课 程: 编译原理 指导教师: 闫 红 实验目的本实验设计的小型编译程序涉及到编译前端的三个阶段:词法分析、语法分析和语义分析生成中间代码(四元式),编译程序的重点放在中间代码生成阶段。编译程序的输出结果包括词法分析后的二元式序列、变量名表;语法分析后的状态栈分析过程显示;语义分析生成中间代码后的四元式程序。整个程序分为三个部分:(1)词法分析部分(2)语法分析、语义分析及四元式生成部分(3)输出显示部分实验要求:本程序仅考虑由下面产生式所定义的程序语句:S if B then S else
2、S | while B do S | begin L end | AL S;L | SA i:= EB BB|BB|B|(B)|I rop i|i 其中,各个非终结符的含义是:S-语句L语句串A赋值句B-布尔表达式E-算术表达式各个终结符的含义:i-整型变量或常数,布尔变量或常数;rop-为六种关系运算符的代表;-起语句分隔作用;:=-赋值符号-逻辑非运算符;-逻辑与运算符;-逻辑或运算符;规定程序是由一条语句或由begin和end嵌套起来的复合语句组成的,并且规定的语句末加上#表示程序结束。下面是符合规定的程序示例: begin A:=A+B*C; C:=A+2; while AB do i
3、f M=N THEN C:=D else while A=D do A:=Dend#实验内容:第一部分:词法分析一词法分析的功能:输入:所给文法的源程序字符串输出:1.二元组(单词种别,单词符号的属性值)构成的序列2.关键字: (相当于Pascal语言中的begin) , if ,else , while , (相当于Pascal语言中的end ) 所有的关键字都是小写字母. 3.运算符: + , - , * , / , = , , , = , , & ,| , ! 4.界 符: 逗号 ,分号 ,左圆括号 , 右圆括号 , # 5.常 数: 在这里只涉及到int型常量 6.其他单词是标识符(I
4、D)和整形常数(NUM),通过以下正规式定义: ID = letter(letter|digit)*NUM = digit digit * 7.空格由空白,制表符和换行符组成,空格一般用来分隔ID,NUM,运算符,界符和关键字,词法分析阶段通常会被过滤掉。二词法分析程序设计 单 词内部码编码号If Sy_if0thenSy_then1elseSy_else2whileSy_while3beginSy_begin4DoSy_do5endSy_end6andop_and39Orop_or40notop_not41+Plus34*Times36:becomes38;Semicolon 8(lpare
5、nt48)rparent49AccAcc-2=rop423.自动机转换图三程序实现数据结构:struct nTabint tc;int fc;nTab2200;int Label = 0;struct rWordschar sp10;int 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
6、,n,n1,E,sstack100,ibuf100,stack1000;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( );Lnum +;ch = *Pline;Pline +;void Scan ( ) while (ch != ) switch(ch) case :break;case a: case
7、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:Identifer( );break;case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:Number(); break;case ) bufCount.pos = 4;
8、else bufCount.pos = 1; Pline -; bufCount.sy1 = rop; Count +; break;case : Readch( ); if (ch =) bufCount.pos = 2; else bufCount.pos = 3; Pline -; bufCount.sy1 = rop; Count +; break;case(: bufCount.sy1 = lParent;Count +;break;case):bufCount.sy1 = rParent;Count +; break;case#:bufCount.sy1 = JingHao;Cou
9、nt +; break;case+:bufCount.sy1 = Plus;Count +; break;case*:bufCount.sy1 = Times;Count +; break;case: Readch();if (ch =) bufCount.sy1 = Becomes;Count +; break;case=:bufCount.sy1 = rop;bufCount.pos = 5;Count +; break;case;:bufCount.sy1 = Semicolon;Count +; break; Readch( );bufCount.sy1 = -1;nt Find(ch
10、ar spe1 )int ss1 = 0;int ii = 0;while (ss1 = 0 & ii = a & ch =0 & ch = 9); Pline -;Spelling k = 0; while (ss = 0 & i 10)if (!strcmp(Spelling,ResWordsi.sp) ss = 1;i +;if (ss = 1) bufCount.sy1 = ResWordsi - 1.sy;elsebufCount.sy1 = ident;j = Find(Spelling);if (j = -1)bufCount.pos = tt1;strcpy(nTab1tt1,
11、Spelling);tt1 +;nLength +;else bufCount.pos = j;Count +;for (k = 0; k = 0 & ch sy1 = 0)n.sy1 = pbuf - sy1;n.pos = pbuf - pos;pbuf+;int newTemp( )newt +;return newt;int Gen (char op1,struct aa arg11,struct aa arg22,int result1)strcpy(fexpnxq.op,op1);fexpnxq.arg1.sy1 = arg11.sy1;fexpnxq.arg1.pos = arg
12、11.pos;fexpnxq.arg2.sy1 = arg22.sy1;fexpnxq.arg2.pos = arg22.pos;fexpnxq.result = result1;nxq +;return nxq - 1;int Merg(int p1,int p2)int p;if ( p2 = 0 ) return p1;elsep = p2;while (fexpp.result != 0) p = fexpp.result;fexpp.result = p1;return p2;void BackPatch(int p,int t)int tempq;int q;q = p;while
13、 (q != 0)tempq = fexpq.result;fexpq.result = t;q = tempq;int Change1(int chan)switch(chan)case intConst:case ident: return 0;case Plus: return 1;case Times: return 2;case lParent: return 3;case rParent: return 4;case JingHao: return 5;case Tempsy:return 6;int Change2(int chan)switch(chan)case intCon
14、st:case ident:return 0;case rop:return 1;case lParent:return 2;case rParent: return 3;case op_not:return 4;case op_and:return 5;case op_or:return 6;case JingHao:return 7;case Tempsy:return 8;case EA:return 9;case EO:return 10;int Test(int value)switch(value)case intConst:case ident:case Plus:case Ti
15、mes:case Becomes:case lParent:case rParent:case rop:case op_and:case op_or:case op_not:return 1;default:return 0;int LrParse()int i1 = 0;int num = 0;if (Test(n.sy1)if (stacksp.sy1 = Sy_while)sign = 2;elseif (stacksp.sy1 = Sy_if) sign = 3;else sign = 1;doibufi1.sy1 = n.sy1;ibufi1.pos = n.pos;Readnu(
16、);i1 +;while (Test(n.sy1);ibufi1.sy1 = JingHao;pbuf -;sstack0.sy1 = JingHao;ssp = 0;if (sign = 1)sp1 = 0;stack1sp1 = 0;num = 2;n1.sy1 = ibufnum.sy1;n1.pos = ibufnum.pos;LrParse1(num);n.sy1 = a; if (sign = 2 | sign = 3)PointMark +;LabelMarkPointMark.nxq1 = nxq;sp1 = 0;stack1sp1 = 0;num = 0;n1.sy1 = i
17、bufnum.sy1;n1.pos = ibufnum.pos;LrParse2(num);LabelMarkPointMark.tc1 = nTab2Label - 1.tc;LabelMarkPointMark.fc1 = nTab2Label - 1.fc;BackPatch(LabelMarkPointMark.tc1,nxq);n.sy1 = e;Ir = actionstacksp.posn.sy1;cout sp t stacksp.postn.sy1tIrn;if (Ir = 0)sp +;stacksp.pos = Ir;stacksp.sy1 = n.sy1;Readnu(
18、 );LrParse( ); if (Ir = 100)switch (Ir)case 100:break;case 101:cout if e then S else S归约n;sp = sp - 6;n.sy1 = S;fexpLabelTempPointTemp.result = nxq;PointTemp -;if (stacksp.sy1 = Sy_then)Gen(j,oth1,oth1,0);BackPatch(LabelMarkPointMark.fc1,nxq);PointTemp +;LabelTempPointTemp = nxq - 1;PointMark -;if (
19、stacksp.sy1 = Sy_do)Gen(j,oth1,oth1,LabelMarkPointMark.nxq1);BackPatch(LabelMarkPointMark.fc1,nxq);break;case 102:cout while e do S归约n;sp = sp -4;n.sy1 = S;if (stacksp.sy1 = Sy_do)Gen(j,oth1,oth1,LabelMarkPointMark.nxq1);BackPatch(LabelMarkPointMark.fc1,nxq);if (stacksp.sy1 = Sy_then)Gen(j,oth1,oth1
20、,0);fexpLabelMarkPointMark.fc1.result = nxq;PointTemp +;LabelTempPointTemp = nxq - 1;break;case 103:cout begin L end 归约n;sp = sp -3;n.sy1 = S;if (stacksp.sy1 = Sy_then)Gen(j,oth1,oth1,0);BackPatch(LabelMarkPointMark.fc1,nxq);PointTemp +;LabelTempPointTemp = nxq - 1;if (stacksp.sy1 = Sy_do)Gen(j,oth1
21、,oth1,LabelMarkPointMark.nxq1);BackPatch(LabelMarkPointMark.fc1,nxq);break;case 104:cout a 归约n;sp = sp - 1;n.sy1 = S;if (stacksp.sy1 = Sy_then)Gen(j,oth1,oth1,0);BackPatch(LabelMarkPointMark.fc1,nxq);PointTemp +;LabelTempPointTemp = nxq - 1;if (stacksp.sy1 = Sy_do)Gen(j,oth1,oth1,LabelMarkPointMark.
22、nxq1);BackPatch(LabelMarkPointMark.fc1,nxq);break;case 105:cout 归约n;sp = sp - 1;n.sy1 = L;break;case 106:cout S;L 归约n;sp = sp - 3;n.sy1 = L;break;pbuf - ;LrParse( );if (Ir = ACC) return ACC;四心得体会通过这次语义分析的实验,我对高级语言的学习有了更深的认识,了解得更透彻。同时我了解了高级语言转化为目标代码或汇编指令的过程,对今后的学习将起很大的作用,对以后的编程有很大的帮助. 本次实验虽然只是完成了一个简单的程序,并且程序的主要框架课本上有给出,但在组织程序结构和深入了解上学到了很多,加深了对编译原理的理解,掌握乐编译程序的实现方法和技术。巩固了前面所学的知识。专心-专注-专业