资源描述:
《SLR分析器实验(编译原理).doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、SLR(1)分析器源代码如下:#include#includechar*action[6][4]={"S2#","r3#","r3#","r3#",/*ACTION表*/NULL,NULL,NULL,"acc","S2#","r3#","r3#","r3#",NULL,"S5#","S4#",NULL,NULL,"r1#","r1#","r1#",NULL,"r2#","r2#","r2#"};intgoto1[6][1]={1,0,3,0,0,0};/*GOTO表*/charvt[4]={'a','b','d','#
2、'};/*存放非终结符*/charvn[1]={'A'};/*存放终结符*/char*LR[4]={"S->A#","A->aAd#","A->aAb#","A->#"};/*存放产生式*/inta[10];charb[10],c[10],c1;inttop1,top2,top3,top,m,n;voidmain(){intg,h,i,j,k,l,p,y,z,count;charx,copy[10],copy1[10];top1=0;top2=0;top3=0;top=0;a[0]=0;y=a[0];b[0]='#';count=0;z=0;printf(
3、"请输入表达式:");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++;printf("%dt",count);while(m<=top1){/*输出状态栈*/printf("%d",a[m]);m=m+1;}printf("tt");while(n<=top2){/*
4、输出符号栈*/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<=2)j++;if(j==2&&x!=vt[j]){printf("error");return;}if(action[y][j]==NULL){printf("error");return;}elsestrcpy(copy,action[y][j]);if(copy[0]=='S'){/*处理
5、移进*/z=copy[1]-'0';top1=top1+1;top2=top2+1;a[top1]=z;b[top2]=x;top=top+1;i=0;while(copy[i]!='#'){printf("%c",copy[i]);i++;}printf("");}if(copy[0]=='r'){/*处理归约*/i=0;while(copy[i]!='#'){printf("%c",copy[i]);i++;}h=copy[1]-'0';strcpy(copy1,LR[h]);while(copy1[0]!=vn[k])k++;l=strlen(L
6、R[h])-4;top1=top1-l+1;top2=top2-l+1;y=a[top1-1];p=goto1[y][k];a[top1]=p;b[top2]=copy1[0];z=p;printf("t");printf("%d",p);}}while(action[y][j]!="acc");printf("acc");}