資源描述:
《編譯原理-LR分析法(附源碼)》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、LR分析實(shí)驗(yàn)報(bào)告一、實(shí)驗(yàn)項(xiàng)目名稱LR分析二、實(shí)驗(yàn)?zāi)康恼莆沼肔R分析法對表達(dá)式文法進(jìn)行自底向上語法分析的算法,加深對LR分析法的三、實(shí)驗(yàn)環(huán)境Windows10MicrosoftVisualStudio2015四、實(shí)驗(yàn)內(nèi)容本次實(shí)驗(yàn)的SLR(1)文法為表達(dá)式拓廣文法:(0)S’→E(1)E→E+T(2)E→T(3)T→T*F(4)T→F(5)F→(E)(6)F→i改進(jìn)后的SLR(1)分析表如教材142頁圖7.8。編寫識別表達(dá)式拓廣文法的合法句子的SLR(1)分析程序,對輸入的任意符號串,給出分析過程及分析結(jié)果。分析過
2、程要求輸出步驟、狀態(tài)棧、符號棧、輸入串和語法動(dòng)作。如果該符號串不是表達(dá)式文法的合法句子,要給出盡量詳細(xì)的錯(cuò)誤提示。五、實(shí)驗(yàn)步驟將改進(jìn)后的SLR(1)分析表存到一個(gè)數(shù)組中,本次實(shí)驗(yàn)的文法是寫在程序中的,不可改變,這種方法降低了實(shí)驗(yàn)代碼的難度。六、源程序清單、測試數(shù)據(jù)、結(jié)果#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','+','(',')','*','#'};/*存放終結(jié)符*/c
5、harvn[3]={'E','T','F',};/*存放非終結(jié)符*/char*LR[7]={"S->E#","E->E+T#","E->T#","T->T*F#","T->F#","F->(E)","F->i"};/*存放產(chǎn)生式*/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("----------------請輸入表達(dá)式(以#結(jié)尾)--------------");do{scanf("%c",&c1);c[top3]=c1;top3=top3+1;}while(c1!='#');printf("步驟t狀態(tài)棧tt符號棧tt輸入串ttACTIONtGOTO");do{y=z;m=0;n=0;/*y,z指向狀態(tài)棧棧頂*/g=top;j=0;k=0;x=c[top];count++;
7、printf("%dt",count);while(m<=top1){/*輸出狀態(tài)棧*/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'){/*處理移進(jìn)*/z=copy[1]-'0';top1=top1+1;top2=top2+1;a[top1]=z;b[top2]=x;top=top