《天津理工大学编译原理试验3:语义分析与中间代码生成.docx》由会员分享,可在线阅读,更多相关《天津理工大学编译原理试验3:语义分析与中间代码生成.docx(15页珍藏版)》请在优知文库上搜索。
1、大讲记大塔实验报告学院(系)名称:计算机与通信工程学院姓名学号专业班级实验项目实验三:语义分析与中间代码生成课程名称编译原理课程代码实验时间实验地点计算机软件实验室7-220计算机软件实验室7-215批改意见成绩教师签字:实验内容:可选择LLl分析法、算符优先分析法、LR分析法之一,实现如下表达式文法的语法制导翻译过程。文法GE如下所示:E-E+TIE-TITT-T*FIT/FIFF-PaFIPP(E)Ii要求构造出符合语义分析要求的属性文法描述,并在完成实验二(语法分析)的基础上,进行语义分析程序设计,最终输出与测试用例等价的四元式中间代码序列。实验目的:1 .掌握语法制导翻译的基本功能。2
2、 .巩固对语义分析的基本功能和原理的认识。3 .能够基于语法制导翻译的知识进行语义分析。4 .掌握类高级语言中基本语句所对应的语义动作。5 .理解并处理语义分析中的异常和错误。实验要求:1 .在实验二的基础上,实现语法制导翻译功能,输出翻译后所得四元式序列;2 .要求详细描述所选分析方法进行制导翻译的设计过程;3 .完成对所设计分析器的功能测试,并给出测试数据和实验结果;4 .为增加程序可读性,请在程序中进行适当注释说明;5 .整理上机步骤,总结经验和体会;6 .认真完成并按时提交实验报告。【实验过程记录(源程序、测试用例、测试结果及心得体会等)】#include#include#define
3、size1024usingnamespacestd;intstep=();typedefstructvariable_T(charOPerateJ操作符stringVar1;变量1stringvar2;变量2intnum;第几个变量variable_T;VariabIe_Ttsize;记录四元式变量的变量iniIsize=-I;/依示是第tsize+1个变量typedefstructchar_stack(charcontentJ/当前字符stringendchar:这个符号代表的中间变量可以是i,也可以是tl,t2,等等intnum;和该字符相关的中间变量的序号chajstack;string
4、table1913=/+-*a)#(iETFP*0*/eerr,eJeJe,err,e飞5”,飞6,1,2,3,4,/*1*,s7,飞8JenvelT”,“err”,eir”acc”,“enVerr7err“JenVenVerr,/*2*/,r3,r3,s9,sl,err,r3,yy,e,err,errerr,err,err,/*3*/,r6,r6,r6,r6,err,r6,rr,err,err,err,err,/*4*/r8,r8,r8,ur8,sl,r8,Y8rr,e,err,err,e,e,/*5*/,err*,eerr,err,err,errs5,s6,c,2,3,4u,/*6*/rl
5、0,10Jrl0,T10,T10,Tl(,T10,errJerr,err,errJerr,err,*7*/eJe,errJerreJeJe,s5,s6,err,d,“3“,“4,*8*/,err,errW,e,err,err,e,s5,s6,err,e,3,4,*9*/,errW,err,err,7,err,err,err,s5,s6,errerr,f,4,/10*/,err,e,err,e,e,e,e,s5,ts6,err,errg4,*11*/,err,err,e,err,err,7,err,err,s5,s6,e,err,h,4u,*12*/s7,err,err,slo,err,err,
6、errerr,err,err,err13*/rl,r,s9,slOeVTl,RJeVeVerr,5,eVe”14*/r2,r2,s9,sl,e,m2m,2W,err,7,e,7,e,err,err,15*/r4,r4,w,41l-zfHII-_Htw,zfIlMw,zr4,r4terr,r4r4,err,err,err,err,err,err16*/r5,r5,r,r3,r5,err,r3,r5,err,err,err,err,errerr*17*/,r7,r7,r7r7,errr7,Y7,e,err,err,err,err,*18*/,r9,r9,r9r9u,r9u,r9,r9,err,er
7、r,e,err,err,err,;inigetLength(charstrsize)(inti=0;while(stri!=,)i+;returni;)intgetLengthc(char_stackstrsize)(inti=O;vhile(stri.conlent!=,0)i+;returni;intgetstringLength(stringstr)inti=O;while(stri!=,O)i+;returni;)chargettop(charstacksize,inttop)(if(stacktop!=0)returnstacktop;elsereturnvoidpopstack(c
8、har*stack,int*pointer,inttimes)intp;fbr(inti=l;i=times;i+)(p=*pointer;stackp=0;(*pointer)-;1voidPoPStaCkC(ChaJStaCk*stack,int*poinler,inttimes)intp;fbr(inti=l;i=times;i+)p=*pointer;stackp.content=,0,;(*pointer);1voidPUShStaCk(Char_stack*stack,int*pointer,char*stack_state,int*poinler_state,charstr,ch
9、arsxjntx)inti=0;if(x=0)COUtttt状态sx”进状态栈elseif(x=l)CoUtVV状态VVsxVV”进状态栈”;if(str!=,)(coutstr进字符栈:(*poinler)+;stack(*pointer).Content=Str;(*pointer_state)+;stack_state(*pointer_state)=sx;intgetcol(chartop)(Switch(Iop)(casereturnO;case-:return1;case*:return2;caseV,:retum3;casea:retum4;case),:return5;case
10、return6;casereturn7;casei,:return8;case,E:return9;case,T:return10;caseF:return11;case,P:return12;default:coutError!Thischaracterstringisnotthisgrammer,ssentence,endl;return-1;intgetraw(charraw)(switch(raw)(case0:return0;case,:return1;case,2:return2;case,3:return3;case,4,:return4;case,5,:return5;case
11、,6:return6;case,7,:return7;case,8:return8;case,9:return9;case,a,:return10;case,b:return11;casec:return12;case,d:return13;case,e,:return14;casef:return15;caseg:return16;case,h,:return17;case,i,:return18;default:coutError!Thischaracterstringisnotthisgrammerssentence.endl;return-1;chargetraw_content(st
12、ringstr)(if(str=l)return;elseif(str=2)return2;elseif(str=3)return3;elseif(str=,4)return4;elseif(str=c)returnc;elseif(str=,d)returnd;elseif(str=e)returne;elseif(str=f)returnf;elseif(slr=g)returng,;elseif(str=h)returnh,;elseif(str=i)returni,;stringget_lx(intnum)switch(num)case 1:return,t,;case 2:return,t2;case 3:returnt3;case 4:return,t4;case 5:return,t5;cas