天津理工大学编译原理实验2:语法分析

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1/13实验报告学院(系)名称:计算机与通信工程学院姓名学号专业班级实验项目实验二:语法分析课程名称编译原理课程代码实验时间2016年4月21日第1、2节2016年4月26日第3、4节2016年4月28日第1、2节实验地点计算机软件实验室7-220批改意见成绩教师签字:实验内容:可选择LL1分析法、算符优先分析法、LR分析法之一,实现如下表达式文法的语法分析器:(1)E→E+T|E-T|T(2)T→T*F|T/F|F(3)F→P^F|P(4)P→(E)|i实验目的:1.掌握语法分析的基本概念和基本方法;2.正确理解LL1分析法、算符优先分析法、LR分析法的设计与使用方法。实验要求:1.按要求设计实现能识别上述文法所表示语言的语法分析器,并要求输出全部分析过程;2.要求详细描述所选分析方法针对上述文法的分析表构造过程;3.完成对所设计语法分析器的功能测试,并给出测试数据和实验结果;4.为增加程序可读性,请在程序中进行适当注释说明;5.整理上机步骤,总结经验和体会;6.认真完成并按时提交实验报告。2/13【实验过程记录(源程序、测试用例、测试结果及心得体会等)】使用的是LL(1)分析法,其分析表的构造方法和构造过程如下:实验源代码:#includeiostream#includecstring#definesize1024usingnamespacestd;intgetLength(charstr[size]){inti=0;while(str[i]!='\0')i++;returni;}intgetstringLength(stringstr){inti=0;while(str[i]!='\0')i++;returni;}chargettop(charstack[size],inttop){if(stack[top]!='\0')returnstack[top];elsereturn'#';}voidpopstack(char*stack,int*pointer){intp=*pointer;cout\tPopupstack[p]out!;stack[p]='\0';(*pointer)--;}voidpushstack(char*stack,int*pointer,stringstr){inti=0;intlength=getstringLength(str);coutpushstrinstackreversedorder.;for(i=length-1;i=0;i--){(*pointer)++;3/13stack[(*pointer)]=str[i];}}intgetcol(chartop){switch(top){case'+':return0;case'-':return1;case'*':return2;case'/':return3;case'^':return4;case')':return5;case'#':return6;case'(':return7;case'i':return8;default:coutError!Thischaracterstringisnotthisgrammer`ssentence.endl;return-1;}}voidshow(charstr[size],intindex){intlength=getLength(str);if(index!=-1)cout\t;for(inti=index+1;ilength;i++)coutstr[i];}intmain(){charstr[size];//接受字符串的数组charstack[size];//进行比对的栈intpointer=-1;//指向栈顶的指针intlength=0;//记录字符串长度intindex=0;//记录输入字符串4/13chartop;inti,j;//i表示行,j表示列stringproduction;boolmatch=false;stringtable[7][9]={//+-*/^)#(i/*E*/error,error,error,error,error,error,error,TX,TX,/*X*/+TX,-TX,error,error,error,empty,empty,error,error,/*T*/error,error,error,error,error,error,error,FY,FY,/*Y*/empty,empty,*FY,/FY,error,empty,empty,error,error,/*F*/error,error,error,error,error,error,error,PZ,PZ,/*Z*/empty,empty,empty,empty,^F,empty,empty,error,error,/*P*/error,error,error,error,error,error,error,(E),i};coutPleaseinputcharacterstring:;cinstr;length=getLength(str);str[length]='#';str[length+1]='\0';//coutlengthendl;//couttable[0][0];//已验证可以实现cout符号栈\t当前符号\t输入串\t\t\t\t说明endl;pointer++;stack[pointer]='#';pointer++;stack[pointer]='E';//初始化栈,使栈底是#Ewhile(str[index]!='\0'){top=gettop(stack,pointer);switch(top){case'E':i=0;//第1行j=getcol(str[index]);production=table[i][j];if(production==error){coutError!Thischaracterstringisnotthisgrammer`ssentence.endl;return0;}elseif(production==empty){cout\n;show(stack,-1);cout\tstr[index]\t;show(str,index);popstack(stack,&pointer);5/13}else//说明可以进行分解非终结符{cout\n;show(stack,-1);cout\tstr[index]\t;show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production);}break;case'X':i=1;//第2行j=getcol(str[index]);production=table[i][j];if(production==error){coutError!Thischaracterstringisnotthisgrammer`ssentence.endl;return0;}elseif(production==empty){cout\n;show(stack,-1);cout\tstr[index]\t;show(str,index);popstack(stack,&pointer);}else//说明可以进行分解非终结符{cout\n;show(stack,-1);cout\tstr[index]\t;show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production);}break;case'T':i=2;//第3行j=getcol(str[index]);production=table[i][j];if(production==error){coutError!Thischaracterstringisnotthisgrammer`ssentence.endl;6/13return0;}elseif(production==empty){cout\n;show(stack,-1);cout\tstr[index]\t;show(str,index);popstack(stack,&pointer);}else//说明可以进行分解非终结符{cout\n;show(stack,-1);cout\tstr[index]\t;show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production);}break;case'Y':i=3;//第4行j=getcol(str[index]);production=table[i][j];if(production==error){coutError!Thischaracterstringisnotthisgrammer`ssentence.endl;return0;}elseif(production==empty){cout\n;show(stack,-1);cout\tstr[index]\t;show(str,index);popstack(stack,&pointer);}else//说明可以进行分解非终结符{cout\n;show(stack,-1);cout\tstr[index]\t;show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production);7/13}break;case'F':i=4;//第5行j=getcol(str[index]);production=table[i][j];if(production==error){coutError!Thischaracterstringisnotthisgrammer`ssentence.endl;return0;}elseif(production==empty){cout\n;show(stack,-1);cout\tstr[index]\t;show(str,index);popstack(stack,&pointer);}else//说明可以进行分解非终结符{cout\n;show(stack,-1);cout\tstr[index]\t;show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production);}break;case'Z':i=5;//第6行j=getcol(str[index]);production=table[i][j];if(production==error){coutError!Thischaracterstringisnotthisgrammer`ssentence.endl;return0;}elseif(production==empty){cout\n;show(stack,-1);cout\tstr[index]\t;show(str,index);popstack(stack,&pointer);8/13}else//说明可以进行分解非终结符{cout\n;show(stack,-1);cout\tstr[index]\t;show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production);}break;case'P':i

1 / 13
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功