版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上實(shí)驗(yàn)報(bào)告學(xué)院(系)名稱(chēng):計(jì)算機(jī)與通信工程學(xué)院姓名學(xué)號(hào)專(zhuān)業(yè)班級(jí)實(shí)驗(yàn)項(xiàng)目實(shí)驗(yàn)三:語(yǔ)義分析與中間代碼生成課程名稱(chēng)編譯原理課程代碼實(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 | TTT*F | T/F | FFPF | PP(E) | i要求構(gòu)造出符合語(yǔ)義分析要求的屬性文法描述,并在完成實(shí)驗(yàn)二(語(yǔ)法分析)的基礎(chǔ)上,進(jìn)行語(yǔ)義分析程序設(shè)計(jì),最終輸出與測(cè)試用例等價(jià)的四元式中間代碼序列。實(shí)驗(yàn)?zāi)康模?
2、掌握語(yǔ)法制導(dǎo)翻譯的基本功能。2鞏固對(duì)語(yǔ)義分析的基本功能和原理的認(rèn)識(shí)。3能夠基于語(yǔ)法制導(dǎo)翻譯的知識(shí)進(jìn)行語(yǔ)義分析。4掌握類(lèi)高級(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>#inc
3、lude<cstring>#define size 1024using namespace std;int step=0;typedef struct variable_Tchar operate;/操作符 string var1;/變量 1 string var2;/變量 2 int num;/第幾個(gè)變量 variable_T;variable_T tsize;/記錄四元式變量的變量 int tsize=-1;/表示是第tsize+1個(gè)變量typedef struct char_stackchar content;/當(dāng)前字符 string endchar;/這個(gè)符號(hào)代表的中間變量
4、 可以是 i, 也可以是 t1, t2, 等等 int num;/和該字符相關(guān)的中間變量的序號(hào) char_stack;string table1913=/ + - * / ) # ( i E T F P /* 0 */ "err","err","err","err","err","err","err", "s5", "s6", "1", "2", "3",
5、"4",/* 1 */ "s7", "s8","err","err","err","err","acc","err","err","err","err","err","err",/* 2 */ "r3", "r3", "s9","s10",
6、"err", "r3", "r3","err","err","err","err","err","err",/* 3 */ "r6", "r6", "r6", "r6","err", "r6", "r6","err","err","
7、;err","err","err","err",/* 4 */ "r8", "r8", "r8", "r8","s11", "r8", "r8","err","err","err","err","err","err",/* 5 */ "err",&q
8、uot;err","err","err","err","err","err", "s5", "s6", "c", "2", "3", "4",/* 6 */ "r10","r10","r10","r10","r10","r10","r10&
9、quot;,"err","err","err","err","err","err",/* 7 */ "err","err","err","err","err","err","err", "s5", "s6","err", "d", "3",
10、 "4",/* 8 */ "err","err","err","err","err","err","err", "s5", "s6","err", "e", "3", "4",/* 9 */ "err","err","err","err",
11、"err","err","err", "s5", "s6","err","err", "f", "4",/* 10*/ "err","err","err","err","err","err","err", "s5", "s6","
12、err","err", "g", "4",/* 11*/ "err","err","err","err","err","err","err", "s5", "s6","err","err", "h", "4",/* 12*/ "s7", "
13、s8","err","err","err","s18","err","err","err","err","err","err","err",/* 13*/ "r1", "r1", "s9","s10","err", "r1", "r1&qu
14、ot;,"err","err","err","err","err","err",/* 14*/ "r2", "r2", "s9","s10","err", "r2", "r2","err","err","err","err","err",&
15、quot;err",/* 15*/ "r4", "r4", "r4", "r4","err", "r4", "r4","err","err","err","err","err","err",/* 16*/ "r5", "r5", "r5", "r5"
16、,"err", "r5", "r5","err","err","err","err","err","err",/* 17*/ "r7", "r7", "r7", "r7","err", "r7", "r7","err","err",&quo
17、t;err","err","err","err",/* 18*/ "r9", "r9", "r9", "r9", "r9", "r9", "r9","err","err","err","err","err","err"int getLength(char strsize
18、)int i=0;while(stri!='0')i+;return i; int getLengthc(char_stack strsize)int i=0;while(stri.content!='0')i+;return i; int getstringLength(string str)int i=0;while(stri!='0')i+;return i; char gettop(char stacksize,int top)if(stacktop!='0')return stacktop;elsereturn '
19、;#'void popstack(char *stack,int *pointer,int times)int p;for(int i=1;i<=times;i+)p=*pointer;stackp='0'(*pointer)-;void popstackc(char_stack *stack,int *pointer,int times)int p;for(int i=1;i<=times;i+)p=*pointer;stackp.content='0'(*pointer)-;void pushstack(char_stack *stack
20、,int *pointer,char *stack_state,int *pointer_state,char str,char sx,int x)int i=0;if(x=0)cout<<"ttt狀態(tài) "<<sx<<" 進(jìn)狀態(tài)棧 "else if(x=1)cout<<" 狀態(tài) "<<sx<<" 進(jìn)狀態(tài)棧 "if(str!='#')cout<<str<<" 進(jìn)字符棧"(*pointer
21、)+;stack(*pointer).content=str;(*pointer_state)+;stack_state(*pointer_state)=sx;int getcol(char top)switch(top)case '+':return 0;case '-':return 1;case '*':return 2;case '/':return 3;case '':return 4;case ')':return 5;case '#':return 6;case
22、9;(':return 7;case 'i':return 8;case 'E':return 9;case 'T':return 10;case 'F':return 11;case 'P':return 12;default:cout<<"Error! This character string is not this grammers sentence."<<endl;return -1;int getraw(char raw)switch(raw)case
23、 '0':return 0;case '1':return 1;case '2':return 2;case '3':return 3;case '4':return 4;case '5':return 5;case '6':return 6;case '7':return 7;case '8':return 8;case '9':return 9;case 'a':return 10;case 'b':
24、return 11;case 'c':return 12;case 'd':return 13;case 'e':return 14;case 'f':return 15;case 'g':return 16;case 'h':return 17;case 'i':return 18;default:cout<<"Error! This character string is not this grammers sentence."<<en
25、dl;return -1;char getraw_content(string str)if(str="1")return '1'else if(str="2")return '2'else if(str="3")return '3'else if(str="4")return '4'else if(str="c")return 'c'else if(str="d")return 'd
26、39;else if(str="e")return 'e'else if(str="f")return 'f'else if(str="g")return 'g'else if(str="h")return 'h'else if(str="i")return 'i'string get_tx(int num)switch(num)case 1:return "t1"case 2:return &q
27、uot;t2"case 3: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"ca
28、se 13:return "t13"case 14:return "t14"case 15:return "t15"case 16:return "t16"/.本程序暫時(shí)用到這么多,等有時(shí)間編寫(xiě)合適的可以將數(shù)字轉(zhuǎn)換為字符串的函數(shù)時(shí),即可更改本函數(shù) void show(char strsize,int index)int length=getLength(str);if(index!=-1)cout<<"t"for(int i=index+1;i<length;i+)cout&l
29、t;<stri;void showc(char_stack strsize,int index)int length=getLengthc(str);if(index!=-1)cout<<"t"for(int i=index+1;i<length;i+)cout<<stri.content;void switch_method(char_stack *stack,int *pointer,char *state_stack,int *pointer_state,string production,char *str,int *index)
30、step+;cout<<"n"<<step<<"t"/顯示步驟 show(state_stack,-1);/顯示狀態(tài)棧 cout<<"t"showc(stack,-1);/顯示符號(hào)站 cout<<"t"<<str(*index)<<"t"/顯示當(dāng)前字符 show(str,(*index);/顯示輸入串 char c=str(*index);if(production="err")cout<
31、;<"Error! This character string is not this grammers sentence."<<endl;return ;else if(production="s5")char sx='5'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);else if(production="s6") char sx='6'(*index)+;pushstack(stack,po
32、inter,state_stack,pointer_state,c,sx,0);else if(production="s7") char sx='7'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);else if(production="s8") char sx='8'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);else if(producti
33、on="s9") char sx='9'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);else if(production="s10") char sx='a'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);else if(production="s11") char sx='b'(*index)+;pushst
34、ack(stack,pointer,state_stack,pointer_state,c,sx,0);else if(production="s18") char sx='i'(*index)+;pushstack(stack,pointer,state_stack,pointer_state,c,sx,0);else if(production="r1") int po=(*pointer);/用P規(guī)約該表達(dá)式,有效變量在E的endchar中,需要找到E的位置,即下面的操作 string st=stackpo.endchar;/對(duì)應(yīng)
35、F po-=2;string se=stackpo.endchar;/在規(guī)約之前記錄下要規(guī)約的字符所代表的變量。對(duì)應(yīng) T tsize+;/新增臨時(shí)變量 ttsize.num=tsize+1;/下面四個(gè)表達(dá)式是按照上面的規(guī)約式進(jìn)行的賦值 ttsize.operate='+'ttsize.var1=se;ttsize.var2=st;cout<<"t("<<ttsize.operate<<","<<ttsize.var1<<","<<ttsize.va
36、r2<<",t"<<ttsize.num<<")"int p=(*pointer_state);p-=3;char second=state_stackp;int i=getraw(second);int j=getcol('E');char c_out=getraw_content(tableij);cout<<"tr1:用E->E+T規(guī)約且" popstack(state_stack,pointer_state,3);popstackc(stack,pointe
37、r,3);char c='E'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);string s=get_tx(ttsize.num);stack(*pointer).endchar=s;/把保存E+T規(guī)約的結(jié)果的變量保存至當(dāng)前字符的終結(jié)符 else if(production="r2")int po=(*pointer);/用P規(guī)約該表達(dá)式,有效變量在E的endchar中,需要找到E的位置,即下面的操作 string st=stackpo.endchar;/對(duì)
38、應(yīng) T po-=2;string se=stackpo.endchar;/在規(guī)約之前記錄下要規(guī)約的字符所代表的變量。對(duì)應(yīng) E tsize+;/新增臨時(shí)變量 ttsize.num=tsize+1;/下面四個(gè)表達(dá)式是按照上面的規(guī)約式進(jìn)行的賦值 ttsize.operate='-'ttsize.var1=se;ttsize.var2=st;cout<<"t("<<ttsize.operate<<","<<ttsize.var1<<","<<ttsize.
39、var2<<",t"<<ttsize.num<<")"int p=(*pointer_state);p-=3;char second=state_stackp;int i=getraw(second);int j=getcol('E');char c_out=getraw_content(tableij);cout<<"tr2:用E->E-T規(guī)約且" popstack(state_stack,pointer_state,3);popstackc(stack,poin
40、ter,3);char c='E'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);string s=get_tx(ttsize.num);stack(*pointer).endchar=s;/把保存E-T規(guī)約的結(jié)果的變量保存至當(dāng)前字符的終結(jié)符 else if(production="r3")string s=stack(*pointer).endchar;/在規(guī)約之前記錄下要規(guī)約的字符所代表的變量 int p=(*pointer_state);p-;char
41、second=state_stackp;int i=getraw(second);int j=getcol('E');char c_out=getraw_content(tableij);cout<<"tttr3:用E->T規(guī)約且" popstack(state_stack,pointer_state,1);popstackc(stack,pointer,1);char c='E'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1)
42、;stack(*pointer).endchar=s;/將記錄下的變量賦值給規(guī)約后的字符else if(production="r4") int po=(*pointer);/用P規(guī)約該表達(dá)式,有效變量在E的endchar中,需要找到E的位置,即下面的操作 string sf=stackpo.endchar;/對(duì)應(yīng) F po-=2;string st=stackpo.endchar;/在規(guī)約之前記錄下要規(guī)約的字符所代表的變量。對(duì)應(yīng) T tsize+;/新增臨時(shí)變量 ttsize.num=tsize+1;/下面四個(gè)表達(dá)式是按照上面的規(guī)約式進(jìn)行的賦值 ttsize.operat
43、e='*'ttsize.var1=st;ttsize.var2=sf;cout<<"t("<<ttsize.operate<<","<<ttsize.var1<<","<<ttsize.var2<<",t"<<ttsize.num<<")"int p=(*pointer_state);p-=3;char second=state_stackp;int i=getraw(se
44、cond);int j=getcol('T');char c_out=getraw_content(tableij);cout<<"tr4:用T->T*F規(guī)約且" popstack(state_stack,pointer_state,3);popstackc(stack,pointer,3);char c='T'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);string s=get_tx(ttsize.num);stack(
45、*pointer).endchar=s;/把保存T*F規(guī)約的結(jié)果的變量保存至當(dāng)前字符的終結(jié)符 else if(production="r5")int po=(*pointer);/用P規(guī)約該表達(dá)式,有效變量在E的endchar中,需要找到E的位置,即下面的操作 string sf=stackpo.endchar;/對(duì)應(yīng) F po-=2;string st=stackpo.endchar;/在規(guī)約之前記錄下要規(guī)約的字符所代表的變量。對(duì)應(yīng) T tsize+;/新增臨時(shí)變量 ttsize.num=tsize+1;/下面四個(gè)表達(dá)式是按照上面的規(guī)約式進(jìn)行的賦值 ttsize.oper
46、ate='/'ttsize.var1=st;ttsize.var2=sf;cout<<"t("<<ttsize.operate<<","<<ttsize.var1<<","<<ttsize.var2<<",t"<<ttsize.num<<")"int p=(*pointer_state);p-=3;char second=state_stackp;int i=getraw(
47、second);int j=getcol('T');char c_out=getraw_content(tableij);cout<<"tr5:用T->T/F規(guī)約且" popstack(state_stack,pointer_state,3);popstackc(stack,pointer,3);char c='T'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);string s=get_tx(ttsize.num);stac
48、k(*pointer).endchar=s;/把保存T/F規(guī)約的結(jié)果的變量保存至當(dāng)前字符的終結(jié)符 else if(production="r6")string s=stack(*pointer).endchar;/在規(guī)約之前記錄下要規(guī)約的字符所代表的變量 int p=(*pointer_state);p-;char second=state_stackp;int i=getraw(second);int j=getcol('T');char c_out=getraw_content(tableij);cout<<"tttr6:用T-&g
49、t;F規(guī)約且" popstack(state_stack,pointer_state,1);popstackc(stack,pointer,1);char c='T'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);stack(*pointer).endchar=s;/將記錄下的變量賦值給規(guī)約后的字符else if(production="r7")int po=(*pointer);/用P規(guī)約該表達(dá)式,有效變量在E的endchar中,需要找到E的位置,
50、即下面的操作 string sf=stackpo.endchar;/對(duì)應(yīng) F po-=2;string sp=stackpo.endchar;/在規(guī)約之前記錄下要規(guī)約的字符所代表的變量。對(duì)應(yīng) P tsize+;/新增臨時(shí)變量 ttsize.num=tsize+1;/下面四個(gè)表達(dá)式是按照上面的規(guī)約式進(jìn)行的賦值 ttsize.operate=''ttsize.var1=sp;ttsize.var2=sf;cout<<"t("<<ttsize.operate<<","<<ttsize.var1&l
51、t;<","<<ttsize.var2<<",t"<<ttsize.num<<")"int p=(*pointer_state);p-=3;char second=state_stackp;int i=getraw(second);int j=getcol('F');char c_out=getraw_content(tableij);cout<<"tr7:用F->PF規(guī)約且" popstack(state_stack,poin
52、ter_state,3);popstackc(stack,pointer,3);char c='F'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);string s=get_tx(ttsize.num);stack(*pointer).endchar=s;/把保存PF規(guī)約的結(jié)果的變量保存至當(dāng)前字符的終結(jié)符 else if(production="r8") int p=(*pointer_state);p-;char second=state_stackp;in
53、t i=getraw(second);int j=getcol('F');char c_out=getraw_content(tableij);cout<<"tttr8:用F->P規(guī)約且" string s=stack(*pointer).endchar;/在規(guī)約之前記錄下要規(guī)約的字符所代表的變量 popstack(state_stack,pointer_state,1);popstackc(stack,pointer,1);char c='F'/str(*index);pushstack(stack,pointer,state_stack,pointer_state,c,c_out,1);stack(*pointer).endchar=s;/將記錄下的變量賦值給規(guī)約后的字符 else if(production="r9")
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 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ì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度社保勞動(dòng)合同范本與社會(huì)保障3篇
- 二零二五年度勞動(dòng)合同轉(zhuǎn)讓及員工勞動(dòng)合同解除協(xié)議3篇
- 2025年度礦產(chǎn)資源開(kāi)發(fā)與購(gòu)買(mǎi)合同3篇
- 二零二五年度數(shù)據(jù)中心搬遷服務(wù)合同3篇
- 二零二五年度房地產(chǎn)買(mǎi)賣(mài)合同A3版(含產(chǎn)權(quán)登記服務(wù))3篇
- 二零二五年度文化產(chǎn)業(yè)園區(qū)財(cái)務(wù)擔(dān)保合同文化創(chuàng)意產(chǎn)業(yè)與會(huì)計(jì)操作3篇
- 二零二五年度教育行業(yè)助學(xué)貸款合同合規(guī)性分析3篇
- 二零二五年度二手珠寶首飾買(mǎi)賣(mài)簡(jiǎn)單協(xié)議書(shū)范本20253篇
- 海南政法職業(yè)學(xué)院《程序設(shè)計(jì)語(yǔ)言(C語(yǔ)言)》2023-2024學(xué)年第一學(xué)期期末試卷
- 2025年度建筑施工合同具體條款和工程標(biāo)的2篇
- 北京2025年首都醫(yī)科大學(xué)附屬北京友誼醫(yī)院招聘140人歷年參考題庫(kù)(頻考版)含答案解析
- 股權(quán)質(zhì)押權(quán)借款合同模板
- 《工商管理專(zhuān)業(yè)畢業(yè)實(shí)習(xí)》課程教學(xué)大綱
- 2025年中國(guó)社區(qū)團(tuán)購(gòu)行業(yè)發(fā)展環(huán)境、運(yùn)行態(tài)勢(shì)及投資前景分析報(bào)告(智研咨詢(xún)發(fā)布)
- 國(guó)開(kāi)電大本科《西方經(jīng)濟(jì)學(xué)(本)》網(wǎng)上形考(作業(yè)一至六)試題及答案
- 提高有風(fēng)險(xiǎn)患者預(yù)防跌倒墜床護(hù)理措施落實(shí)率品管圈PDCA案例匯報(bào)
- 建材行業(yè)綠色建筑材料配送方案
- 2024年行政執(zhí)法人員執(zhí)法資格知識(shí)考試題庫(kù)(附含答案)
- 西那卡塞治療甲旁亢
- 無(wú)人駕駛 物流行業(yè)市場(chǎng)調(diào)研分析報(bào)告
- 代理記賬有限公司簡(jiǎn)介(5個(gè)范本)
評(píng)論
0/150
提交評(píng)論