资源描述:
《编译原理-LR分析法(附源码)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、LR分析实验报告一、实验项目名称LR分析二、实验目的掌握用LR分析法对表达式文法进行自底向上语法分析的算法,加深对LR分析法的三、实验环境Windows10MicrosoftVisualStudio2015四、实验内容本次实验的SLR(1)文法为表达式拓广文法:(0)S’→E(1)E→E+T(2)E→T(3)T→T*F(4)T→F(5)F→(E)(6)F→i改进后的SLR(1)分析表如教材142页图7.8。编写识别表达式拓广文法的合法句子的SLR(1)分析程序,对输入的任意符号串,给出分析过程及分析结果。分析过
2、程要求输出步骤、状态栈、符号栈、输入串和语法动作。如果该符号串不是表达式文法的合法句子,要给出尽量详细的错误提示。五、实验步骤将改进后的SLR(1)分析表存到一个数组中,本次实验的文法是写在程序中的,不可改变,这种方法降低了实验代码的难度。六、源程序清单、测试数据、结果#define_CRT_SECURE_NO_WARNINGS#include#includechar*action[12][6]={/*ACTION*/"S5#",NULL,NULL,"S4#",NULL,N
3、ULL,NULL,"S6#",NULL,NULL,NULL,"acc",NULL,"r2#","S7#",NULL,"r2#","r2#",NULL,"r4#","r4#",NULL,"r4#","r4#","S5#",NULL,NULL,"S4#",NULL,NULL,NULL,"r6#","r6#",NULL,"r6#","r6#","S5#",NULL,NULL,"S4#",NULL,NULL,"S5#",NULL,NULL,"S4#",NULL,NULL,NULL,"S6#",NULL,NULL,"S1
4、1#",NULL,NULL,"r1#","S7#",NULL,"r1#","r1#",NULL,"r3#","r3#",NULL,"r3#","r3#",NULL,"r5#","r5#",NULL,"r5#","r5#",};intgoto1[12][3]={/*GOTO*/1,2,3,0,0,0,0,0,0,0,0,0,8,2,3,0,0,0,0,9,3,0,0,10,0,0,0,0,0,0,0,0,0,0,0,0};charvt[6]={'i','+','(',')','*','#'};/*存放终结符*/c
5、harvn[3]={'E','T','F',};/*存放非终结符*/char*LR[7]={"S->E#","E->E+T#","E->T#","T->T*F#","T->F#","F->(E)","F->i"};/*存放产生式*/inta[10];charb[10],c[10],c1;inttop1,top2,top3,top,m,n;voidlr(){intg,h,i,j,k,l,p,y,z,count;charx,copy[10],copy1[10];top1=0;top2=0;top3=0;top=0;
6、a[0]=0;y=a[0];b[0]='#';count=0;z=0;printf("----------------请输入表达式(以#结尾)--------------");do{scanf("%c",&c1);c[top3]=c1;top3=top3+1;}while(c1!='#');printf("步骤t状态栈tt符号栈tt输入串ttACTIONtGOTO");do{y=z;m=0;n=0;/*y,z指向状态栈栈顶*/g=top;j=0;k=0;x=c[top];count++;
7、printf("%dt",count);while(m<=top1){/*输出状态栈*/printf("%d",a[m]);m=m+1;}printf("tt");while(n<=top2){/*输出符号栈*/printf("%c",b[n]);n=n+1;}printf("tt");while(g<=top3){/*输出输入串*/printf("%c",c[g]);g=g+1;}printf("tt");while(x!=vt[j]&&j<=6)j++;if(j==6&&x!=vt[j]){p
8、rintf("error");return;}if(action[y][j]==NULL){printf("error");return;}elsestrcpy(copy,action[y][j]);if(copy[0]=='S'){/*处理移进*/z=copy[1]-'0';top1=top1+1;top2=top2+1;a[top1]=z;b[top2]=x;top=top