编译原理课程设计

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

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

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

资源描述

福建农林大学金山学院计算机类课程设计报告课程名称:编译原理课程设计题目:语法分析器姓名:何陈文系:计算机专业:计算机科学与技术年级:2009学号:092231011指导教师:李小林职称:副教授2012年6月20日福建农林大学金山学院计算机类课程设计结果评定评语:成绩:指导教师签字:任务下达日期:评定日期:目录1正则表达式·································································································41.1正则表达式·························································································41.2确定化(化简)后的状态转换图··········································错误!未定义书签。1.3分析程序代码······························································错误!未定义书签。1.4程序运行截图······························································错误!未定义书签。1.5小结···········································································错误!未定义书签。2LL(1)分析···································································································62.1LL(1)文法···························································································62.2LL(1)预测分析表··················································································62.3分析程序代码······························································错误!未定义书签。2.4程序运行截图······························································错误!未定义书签。2.5小结··········································································错误!未定义书签。3算符优先分析······································································错误!未定义书签。3.1算符优先文法······························································错误!未定义书签。3.2算符优先关系表···························································错误!未定义书签。3.3分析程序代码······························································错误!未定义书签。3.4程序运行截图······························································错误!未定义书签。3.5小结··········································································错误!未定义书签。4LR分析·····································································································144.1LR文法·····································································错误!未定义书签。4.2LR分析表·························································································144.3分析程序代码·····················································································154.4程序运行截图······························································错误!未定义书签。4.5小结··········································································错误!未定义书签。总结·······················································································错误!未定义书签。参考文献:··············································································错误!未定义书签。1正则表达式1.1正则表达(a|b)*(aa|bb)(a|b)*1.2转化成二维表存储123213331.3参考代码:#includestdio.hvoidmain(){intA[4][2]={1,2,3,2,1,3,3,3};inti=0,j=0;chars[100];while(1){printf(请输入字符串:);gets(s);while(s[i]){if(s[i]-'a'1||s[i]'a')break;j=A[j][s[i++]-'a'];}if(s[i]==0&&j==3)printf(right\n);elseprintf(wrong\n);i=0;j=0;}1.4输出结果:符合文法的输出:不符合文法的输出;小结:实验中利用数组做维另一个数组的下标,通过这样的二维数组转化实现状态的转化。2LL(1)分析2.1LL(1)文法E→TE'E'→+TE'|εT→FT'T'→*FT'|εF→(E)|i2.2LL(1)预测分析表i+*()#EE→TE'E→TE'E'E'→+TE'E'→εE'→εTT→FT'T→FT'T'T'→εT'→*FT'T'→εT'→εFF→iF→(E)2.3代码#includeiostream.h#includemalloc.h#includeiomanip.h#includestring.h#definemaxsize64typedefstruct{chardata[maxsize];inttop;}seqstack;typedefstruct{chardata[5];}thing;thingm[6][7]={\0\0\0,i\0\0,+\0\0\0,*\0\0\0,(\0\0\0,)\0\0\0,$\0\0,E\0\0\0,TA\0\0,\0\0\0,\0\0\0,TA\0\0,\0\0\0,\0\0\0,A\0\0\0,\0\0\0,+TA\0,\0\0\0,\0\0\0,e\0\0\0,e\0\0\0,T\0\0\0,FB\0\0,\0\0\0,\0\0\0,FB\0\0,\0\0\0,\0\0\0,B\0\0\0,\0\0\0,e\0\0\0,*FB\0,\0\0\0,e\0\0\0,e\0\0\0,F\0\0\0,i\0\0,\0\0\0,\0\0\0,(E)\0,\0\0\0,\0\0\0};//istandsforid;AstandsforE';BstandsforT'thingAPPLY(charone,charanother){inti=0,j=0;for(i=1;i6;i++){if(m[i][0].data[0]==one)break;}for(j=1;j7;j++){if(m[0][j].data[0]==another)break;}returnm[i][j];}voidSHOW(seqstack*one,seqstack*another){inti=0;for(i=0;i=one-top;i++)coutone-data[i];cout;for(i=another-top;i-1;i--)coutanother-data[i];coutendl;}voidJUSTIFY(){intl=0,j=0;charc;thinga;seqstack*nend=newseqstack();seqstack*formula=newseqstack();seqstack*temp=newseqstack();temp-top=-1;nend-top=-1;formula-top=-1;nend-data[++nend-top]='$';nend-data[++nend-top]='E';formula-data[++formula-top]='$';cout请输入表达式(endwith'@'):endl;;cinc;while(c!='@'){temp-data[++temp-top]=c;cinc;}while(temp-top-1){formula-data[++formula-top]=temp-data[temp-top--];}intstop=0;while(stop!=1){if(nend-data[nend-top]=='$'){stop=1;//SHOW(nend,formula);break;}elseif(nend-data[nend-top]==formula-data[formula-top]){nend-top--;formula-top--;//SHOW(nend,formula);}elseif(nend-data[nend-top]=='e'){nend-top--;SHOW(nend,formula);}else{a=APPLY(nend-data[nend-top],formula-data[formula-top]);l=strlen(a.data);if(l==0){couterror!!!!endl;break;}elseif(l0){for(j=0;jl;j++){temp-data[++temp-top]=a.data[j];}SHOW(nend,formula);nend-top--;while(temp-top-1){nend-data[++nend-top]=temp-data[temp-top--];}//SHOW(nend,formula);}}}coutendl;if(stop==1)coutyesitis!!!!!!!endl;elsecouterror!!!!!endl;coutendl;}voidmain(){JUSTIFY();}2.4输出结果正确的表达式:i+i错误的表达式:(i+*i)2.5小结该文法利用一个二维表,表中填各个产生式和空,踩空就会出错。利用三个堆栈来控制表达式,一个

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

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

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

×
保存成功