天津理工大學(xué)編譯原理試驗(yàn)3語(yǔ)義分析與中間代碼生成_第1頁(yè)
天津理工大學(xué)編譯原理試驗(yàn)3語(yǔ)義分析與中間代碼生成_第2頁(yè)
天津理工大學(xué)編譯原理試驗(yàn)3語(yǔ)義分析與中間代碼生成_第3頁(yè)
天津理工大學(xué)編譯原理試驗(yàn)3語(yǔ)義分析與中間代碼生成_第4頁(yè)
天津理工大學(xué)編譯原理試驗(yàn)3語(yǔ)義分析與中間代碼生成_第5頁(yè)
已閱讀5頁(yè),還剩15頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、人評(píng)球.大學(xué)實(shí)驗(yàn)報(bào)告學(xué)院(系)名稱:計(jì)算機(jī)與通信工程學(xué)院姓名學(xué)號(hào)專業(yè)班級(jí)實(shí)驗(yàn)項(xiàng)目實(shí)驗(yàn)三:語(yǔ)義分析與中間代碼生成課程名稱編譯原理課程代碼實(shí)驗(yàn)時(shí)間實(shí)驗(yàn)地點(diǎn)計(jì)算機(jī)軟件實(shí)驗(yàn)室7-220計(jì)算機(jī)軟件實(shí)驗(yàn)室7-215批改意見(jiàn)成績(jī)教師簽字:實(shí)驗(yàn)內(nèi)容:可選擇LL1分析法、算符優(yōu)先分析法、LR分析法之一,實(shí)現(xiàn)如下表達(dá)式文法的語(yǔ)法制導(dǎo)翻譯過(guò)程。文法GE如下所示:EE+T|E-T|TT-T*F|T/F|FF-PF|PP一(E)|i要求構(gòu)造出符合語(yǔ)義分析要求的屬性文法描述,并在完成實(shí)驗(yàn)二(語(yǔ)法分析)的基礎(chǔ)上,進(jìn)行語(yǔ)義分析程序設(shè)計(jì),最終輸出與測(cè)試用例等價(jià)的四元式中間代碼序列。實(shí)驗(yàn)?zāi)康模? .掌握語(yǔ)法制導(dǎo)翻譯的基本功能。2

2、 .鞏固對(duì)語(yǔ)義分析的基本功能和原理的認(rèn)識(shí)。3 .能夠基于語(yǔ)法制導(dǎo)翻譯的知識(shí)進(jìn)行語(yǔ)義分析。4 .掌握類高級(jí)語(yǔ)言中基本語(yǔ)句所對(duì)應(yīng)的語(yǔ)義動(dòng)作。5 .理解并處理語(yǔ)義分析中的異常和錯(cuò)誤。實(shí)驗(yàn)要求:1 .在實(shí)驗(yàn)二的基礎(chǔ)上,實(shí)現(xiàn)語(yǔ)法制導(dǎo)翻譯功能,輸出翻譯后所得四元式序列;2 .要求詳細(xì)描述所選分析方法進(jìn)行制導(dǎo)翻譯的設(shè)計(jì)過(guò)程;3 .完成對(duì)所設(shè)計(jì)分析器的功能測(cè)試,并給出測(cè)試數(shù)據(jù)和實(shí)驗(yàn)結(jié)果;4 .為增加程序可讀性,請(qǐng)?jiān)诔绦蛑羞M(jìn)行適當(dāng)注釋說(shuō)明;5 .整理上機(jī)步驟,總結(jié)經(jīng)驗(yàn)和體會(huì);6 .認(rèn)真完成并按時(shí)提交實(shí)驗(yàn)報(bào)告?!緦?shí)驗(yàn)過(guò)程記錄(源程序、測(cè)試用例、測(cè)試結(jié)果及心得體會(huì)等)】#include<iostream>

3、;#include<cstring>#definesize1024usingnamespacestd;intstep=0;typedefstructvariable_Tcharoperate;/操作符stringvar1;變量1stringvar2;變量2intnum;/第幾個(gè)變量variable_T;variable_Ttsize;/記錄四元式變量的變量inttsize=-1;表示是第tsize+1個(gè)變量typedefstructchar_stack可以是i,也可以是t1,t2,等等charcontent;/當(dāng)前字符stringendchar;這個(gè)符號(hào)代表的中間變量intnum;

4、/和該字符相關(guān)的中間變量的序號(hào)char_stack;stringtable1913=+-*/A)#(iETFP/*0*/"err","err","err","err","err","err","err","s5","s6","1","2","3","4",/*1*/"s7","s8","e

5、rr","err","err","err","acc","err","err","err","err","err","err",/*2*/"r3","r3","s9","s10","err","r3","r3","err",&qu

6、ot;err","err","err","err","err",/*3*/"r6","r6","r6","r6","err","r6","r6","err","err","err","err","err","err",/*4*/"r8&qu

7、ot;,"r8","r8","r8","s11","r8","r8","err","err","err","err","err","err",/*5*/"err","err","err","err","err","err","er

8、r","s5","s6","c","2","3","4",/*6*/"r10","r10","r10","r10","r10","r10","r10","err","err","err","err","err","err

9、",/*7*/"err","err","err","err","err","err","err","s5","s6","err","d","3","4",/*8*/"err","err","err","err","err",&qu

10、ot;err","err","s5","s6","err","e","3","4",/*9*/"err","err","err","err","err","err","err","s5","s6","err","err",&quo

11、t;f","4",/*10*/"err","err","err","err","err","err","err","s5","s6","err","err","g","4",/*11*/"err","err","err","err"

12、;,"err","err","err","s5","s6","err","err","h","4",/*12*/"s7","s8","err","err","err","s18","err","err","err","err&qu

13、ot;,"err","err","err",/*13*/"r1","r1","s9","s10","err","r1","r1","err","err","err","err","err","err",/*14*/"r2","r2",&qu

14、ot;s9","s10","err","r2","r2","err","err","err","err","err","err",/*15*/"r4","r4","r4","r4","err","r4","r4","err",&q

15、uot;err","err","err","err","err",/*16*/"r5","r5","r5","r5","err","r5","r5","err","err","err","err","err","err",/*17*/"r7

16、","r7","r7","r7","err","r7","r7","err","err","err","err","err","err",/*18*/"r9","r9","r9","r9","r9","r9","r9&q

17、uot;,"err","err","err","err","err","err"intgetLength(charstrsize)(inti=0;while(stri!='0')i+;returni;intgetLengthc(char_stackstrsize)(inti=0;while(stri.content!=''0')i+;returni;intgetstringLength(stringstr)(inti=0;while(s

18、tri!='0')i+;returni;chargettop(charstacksize,inttop)(if(stacktop!='0')returnstacktop;elsereturn'#'voidpopstack(char*stack,int*pointer,inttimes)(intp;for(inti=1;i<=times;i+)(p=*pointer;stackp='0'(*pointer)-;voidpopstackc(char_stack*stack,int*pointer,inttimes)(intp;f

19、or(inti=1;i<=times;i+)(p=*pointer;stackp.content='0'(*pointer)-;voidpushstack(char_stack*stack,int*pointer,char*stack_state,int*pointer_state,charstr,charsx,intx)(inti=0;if(x=0)cout<<"ttt狀態(tài)"<<sx<<"進(jìn)狀態(tài)棧"elseif(x=1)cout<<"狀態(tài)"<<sx&l

20、t;<"進(jìn)狀態(tài)棧"if(str!='#')(cout<<str<<"進(jìn)字符棧"(*pointer)+;stack(*pointer).content=str;(*pointer_state)+;stack_state(*pointer_state)=sx;intgetcol(chartop)(switch(top)(case'+':return0;casereturn1;case'*':return2;case'/':return3;case'A'

21、;:return4;case')':return5;case'#':return6;case'(':return7;case'i':return8;case'E':return9;case'T':return10;case'F':return11;case'P':return12;default:cout<<"Error!Thischaracterstringisnotthisgrammer'ssentence."<<

22、;endl;return-1;)intgetraw(charraw)(switch(raw)(case'0':return0;case'1':return1;case'2':return2;case'3':return3;case'4':return4;case'5':return5;case'6':return6;case'7':return7;case'8':return8;case'9':return9;case'a

23、9;:return10;case'b':return11;case'c':return12;case'd':return13;case'e':return14;case'f:return15;case'g':return16;case'h':return17;case'i':return18;default:cout<<"Error!Thischaracterstringisnotthisgrammer'ssentence."<&

24、lt;endl;return-1;)chargetraw_content(stringstr)(if(str="1")return'1'elseif(str="2")return'2'elseif(str="3")return'3'elseif(str="4")return'4'elseif(str="c")return'c'elseif(str="d")return'd'else

25、if(str="e")return'e'elseif(str="f")return'f'elseif(str="g")return'g'elseif(str="h")return'h'elseif(str="i")return'i')stringget_tx(intnum)(switch(num)(case 1:return"t1"case 2:return"t2"case 3

26、:return"t3"case 4:return"t4"case 5:return"t5"case 6:return"t6"case 7:return"t7"case 8:return"t8"case 9:return"t9"case 10:return"t10"case 11:return"t11"case 12:return"t12"case 13:return"t13"c

27、ase 14:return"t14"case 15:return"t15"case 16:return"t16"/.本程序暫時(shí)用到這么多,等有時(shí)間編寫(xiě)合適的可以將數(shù)字轉(zhuǎn)換為字符串的函數(shù)時(shí),即可更改本函數(shù))voidshow(charstrsize,intindex)(intlength=getLength(str);if(index!=-1)cout<<"t"for(inti=index+1;i<length;i+)cout<<stri;voidshowc(char_stackstrsi

28、ze,intindex)(intlength=getLengthc(str);if(index!=-1)cout<<"t"for(inti=index+1;i<length;i+)cout<<stri.content;voidswitch_method(char_stack*stack,int*pointer,char*state_stack,int*pointer_state,stringproduction,char*str,int*index)(step+;cout<<"n"<<step<

29、<"t"/顯示步驟show(state_stack,-1);/顯示狀態(tài)棧cout<<"t"showc(stack,-1);/顯示符號(hào)站cout<<"t"<<str(*index)<<"t"/顯示當(dāng)前字符show(str,(*index);顯示輸入串charc=str(*index);if(production="err")(cout<<"Error!Thischaracterstringisnotthisgrammer&

30、#39;ssentence."<<endl;return;elseif(production="s5")(charsx='5'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,O);elseif(production="s6")(charsx='6'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,O);)elseif(production=&

31、quot;s7")(charsx='7'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);)elseif(production="s8")(charsx='8'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);)elseif(production="s9")(charsx='9'(*index)+;pushstack(stack

32、,pointer,state_stack,pointer_state,c,sx,0);)elseif(production="s10")(charsx='a'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);)elseif(production="s11")(charsx='b'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);)elseif(produ

33、ction="s18")(charsx='i'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);)elseif(production="r1")(intpo=(*pointer);/用P規(guī)約該表達(dá)式,有效變量在E的endchar中,需要找到E的位置,即卜面的操作stringst=stackpo.endchar;/對(duì)應(yīng)Fpo-=2;stringse=stackpo.endchar;/在規(guī)約之前記錄下要規(guī)約的字符所代表的變量。對(duì)應(yīng)Ttsize+;新增臨時(shí)變量

34、ttsize.num=tsize+1;/下面四個(gè)表達(dá)式是按照上面的規(guī)約式進(jìn)行的賦值ttsize.operate='+'ttsize.var1=se;ttsize.var2=st;cout<<"t("<<ttsize.operate<<","<<ttsize.var1<<","<<ttsize.var2<<",t"<<ttsize.num<<")"intp=(*pointer

35、_state);p-=3;charsecond=state_stackp;inti=getraw(second);intj=getcol('E');charc_out=getraw_content(tableij);cout<<"tr1:用E->E+T規(guī)約且"popstack(state_stack,pointer_state,3);popstackc(stack,pointer,3);charc='E'/str(*index);pushstack(stack,pointer,state_stack,pointer_stat

36、e,c,c_out,1);strings=get_tx(ttsize.num);stack(*pointer).endchar=s;/把保存E+T規(guī)約的結(jié)果的變量保存至當(dāng)前字符的終結(jié)符elseif(production="r2")intpo=(*pointer);/用P規(guī)約該表達(dá)式,有效變量在E的endchar中,需要找到E的位置,即卜面的操作stringst=stackpo.endchar;/對(duì)應(yīng)Tpo-=2;stringse=stackpo.endchar;/在規(guī)約之前記錄下要規(guī)約的字符所代表的變量。對(duì)應(yīng)Etsize+;新增臨時(shí)變量ttsize.num=tsize+1;

37、/下面四個(gè)表達(dá)式是按照上面的規(guī)約式進(jìn)行的賦值ttsize.operate='-'ttsize.var1=se;ttsize.var2=st;cout<<"t("<<ttsize.operate<<","<<ttsize.var1<<","<<ttsize.var2<<",t"<<ttsize.num<<")"intp=(*pointer_state);p-=3;charse

38、cond=state_stackp;inti=getraw(second);intj=getcol('E');charc_out=getraw_content(tableij);cout<<"tr2:用E->E-T規(guī)約且"popstack(state_stack,pointer_state,3);popstackc(stack,pointer,3);charc='E'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);string

39、s=get_tx(ttsize.num);stack(*pointer).endchar=s;/把保存E-T規(guī)約的結(jié)果的變量保存至當(dāng)前字符的終結(jié)符elseif(production="r3")strings=stack(*pointer).endchar;/在規(guī)約之前記錄下要規(guī)約的字符所代表的變量intp=(*pointer_state);p-;charsecond=state_stackp;inti=getraw(second);intj=getcol('E');charc_out=getraw_content(tableij);cout<<&

40、quot;tttr3:用E->T規(guī)約且"popstack(state_stack,pointer_state,1);popstackc(stack,pointer,1);charc='E'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);stack(*pointer).endchar=s;/將記錄下的變量賦值給規(guī)約后的字符elseif(production="r4")intpo=(*pointer);/用P規(guī)約該表達(dá)式,有效變量在E的endcha

41、r中,需要找到E的位置,即卜面的操作stringsf=stackpo.endchar;/對(duì)應(yīng)Fpo-=2;stringst=stackpo.endchar;/在規(guī)約之前記錄下要規(guī)約的字符所代表的變量。對(duì)應(yīng)Ttsize+;新增臨時(shí)變量ttsize.num=tsize+1;/下面四個(gè)表達(dá)式是按照上面的規(guī)約式進(jìn)行的賦值ttsize.operate='*'ttsize.var1=st;ttsize.var2=sf;cout<<"t("<<ttsize.operate<<","<<ttsize.var

42、1<<","<<ttsize.var2<<",t"<<ttsize.num<<")"intp=(*pointer_state);p-=3;charsecond=state_stackp;inti=getraw(second);intj=getcol('T');charc_out=getraw_content(tableij);cout<<"tr4:用T->T*F規(guī)約且"popstack(state_stack,pointe

43、r_state,3);popstackc(stack,pointer,3);charc=T;/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);strings=get_tx(ttsize.num);stack(*pointer).endchar=s;/把保存T*F規(guī)約的結(jié)果的變量保存至當(dāng)前字符的終結(jié)符elseif(production="r5")intpo=(*pointer);/用P規(guī)約該表達(dá)式,有效變量在E的endchar中,需要找到E的位置,即卜面的操作stringsf=s

44、tackpo.endchar;/對(duì)應(yīng)Fpo-=2;stringst=stackpo.endchar;/在規(guī)約之前記錄下要規(guī)約的字符所代表的變量。對(duì)應(yīng)Ttsize+;新增臨時(shí)變量ttsize.num=tsize+1;/下面四個(gè)表達(dá)式是按照上面的規(guī)約式進(jìn)行的賦值ttsize.operate='/'ttsize.var1=st;ttsize.var2=sf;cout<<"t("<<ttsize.operate<<","<<ttsize.var1<<","<&l

45、t;ttsize.var2<<",t"<<ttsize.num<<")"intp=(*pointer_state);p-=3;charsecond=state_stackp;inti=getraw(second);intj=getcol('T');charc_out=getraw_content(tableij);cout<<"tr5:用T->T/F規(guī)約且"popstack(state_stack,pointer_state,3);popstackc(stack,p

46、ointer,3);charc=T;/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);strings=get_tx(ttsize.num);stack(*pointer).endchar=s;/把保存T/F規(guī)約的結(jié)果的變量保存至當(dāng)前字符的終結(jié)符elseif(production="r6")strings=stack(*pointer).endchar;/在規(guī)約之前記錄下要規(guī)約的字符所代表的變量intp=(*pointer_state);p-;charsecond=state_stackp;inti=getraw(second);intj=getcol('T');charc_out=getraw_content(tableij);cout<<"tttr6:用T->F規(guī)約且"popstack(state_stack,pointer_state,1);popstackc(stack,pointer,1);charc='T'/str(*index);pushs

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論