天津理工大學(xué)編譯原理實驗2:語法分析_第1頁
天津理工大學(xué)編譯原理實驗2:語法分析_第2頁
天津理工大學(xué)編譯原理實驗2:語法分析_第3頁
天津理工大學(xué)編譯原理實驗2:語法分析_第4頁
天津理工大學(xué)編譯原理實驗2:語法分析_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上實驗報告學(xué)院(系)名稱:計算機與通信工程學(xué)院姓名學(xué)號專業(yè)班級實驗項目實驗二:語法分析課程名稱編譯原理課程代碼實驗時間2016年4月21日 第1、2節(jié)2016年4月26日 第3、4節(jié)2016年4月28日 第1、2節(jié)實驗地點計算機軟件實驗室7-220批改意見成績教師簽字: 實驗內(nèi)容: 可選擇LL1分析法、算符優(yōu)先分析法、LR分析法之一,實現(xiàn)如下表達式文法的語法分析器:(1)EE+T | E-T | T(2)TT*F | T/F | F(3)FPF | P(4)P(E) | i實驗?zāi)康模?掌握語法分析的基本概念和基本方法;2正確理解LL1分析法、算符優(yōu)先分析法、LR分析法的

2、設(shè)計與使用方法。實驗要求:1按要求設(shè)計實現(xiàn)能識別上述文法所表示語言的語法分析器,并要求輸出全部分析過程;2要求詳細(xì)描述所選分析方法針對上述文法的分析表構(gòu)造過程;3完成對所設(shè)計語法分析器的功能測試,并給出測試數(shù)據(jù)和實驗結(jié)果;4為增加程序可讀性,請在程序中進行適當(dāng)注釋說明;5整理上機步驟,總結(jié)經(jīng)驗和體會;6認(rèn)真完成并按時提交實驗報告?!緦嶒炦^程記錄(源程序、測試用例、測試結(jié)果及心得體會等)】使用的是LL(1)分析法,其分析表的構(gòu)造方法和構(gòu)造過程如下:實驗源代碼:#include<iostream>#include<cstring>#define size 1024using

3、 namespace std;int getLength(char strsize)int i=0;while(stri!='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 '#'void popstack(char *stack,int *pointer)

4、int p=*pointer;cout<<"tPopup "<<stackp<<" out!"stackp='0'(*pointer)-;void pushstack(char *stack,int *pointer,string str)int i=0;int length=getstringLength(str);cout<<" push "<<str<<" in stack reversed order."for(i=le

5、ngth-1;i>=0;i-)(*pointer)+;stack(*pointer)=stri;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 '(':return 7;case 'i

6、':return 8;default:cout<<"Error! This character string is not this grammers sentence."<<endl;return -1;void show(char strsize,int index)int length=getLength(str);if(index!=-1)cout<<"t"for(int i=index+1;i<length;i+)cout<<stri;int main()char strsize;/

7、接受字符串的數(shù)組 char stacksize;/進行比對的棧 int pointer=-1;/指向棧頂?shù)闹羔?int length=0;/記錄字符串長度 int index=0;/記錄輸入字符串 char top;int i,j;/i表示行,j表示列 string production;bool match=false;string table79=/ + - * / ) # ( i/* E */"error", "error", "error", "error", "error", &quo

8、t;error", "error", "TX", "TX",/* X */"+TX", "-TX", "error", "error", "error", "empty", "empty", "error", "error",/* T */"error","error","error",&

9、quot;error","error","error","error","FY","FY",/* Y */"empty","empty","*FY","/FY","error","empty","empty","error","error",/* F */"error","err

10、or","error","error","error","error","error","PZ","PZ",/* Z */"empty","empty","empty","empty","F","empty","empty","error","error",/* P *

11、/"error","error","error","error","error","error","error","(E)","i"cout<<"Please input character string: "cin>>str;length=getLength(str);strlength='#'strlength+1='0'/cout<

12、;<length<<endl;/cout<<table00; /已驗證可以實現(xiàn) cout<<"符號棧t當(dāng)前符號t輸入串tttt說明"<<endl; pointer+;stackpointer='#'pointer+;stackpointer='E'/初始化棧,使棧底是 #E while(strindex!='0')top=gettop(stack,pointer);switch(top)case 'E':i=0;/第1行 j=getcol(strindex)

13、;production=tableij;if(production="error")cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show

14、(str,index);popstack(stack,&pointer);else/說明可以進行分解非終結(jié)符 cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production); break;case 'X':i=1;/第2行 j=getcol(strindex);p

15、roduction=tableij;if(production="error")cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(s

16、tr,index);popstack(stack,&pointer);else/說明可以進行分解非終結(jié)符 cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production); break;case 'T':i=2;/第3行 j=getcol(strindex);pro

17、duction=tableij;if(production="error")cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str

18、,index);popstack(stack,&pointer);else/說明可以進行分解非終結(jié)符 cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production); break;case 'Y':i=3;/第4行 j=getcol(strindex);produ

19、ction=tableij;if(production="error")cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,i

20、ndex);popstack(stack,&pointer);else/說明可以進行分解非終結(jié)符 cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production); break;case 'F':i=4;/第5行 j=getcol(strindex);product

21、ion=tableij;if(production="error")cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,ind

22、ex);popstack(stack,&pointer);else/說明可以進行分解非終結(jié)符 cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production); break;case 'Z':i=5;/第6行 j=getcol(strindex);productio

23、n=tableij;if(production="error")cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index

24、);popstack(stack,&pointer);else/說明可以進行分解非終結(jié)符 cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production); break;case 'P':i=6;/第7行 j=getcol(strindex);production=

25、tableij;if(production="error")cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;else if(production="empty")cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);

26、popstack(stack,&pointer);else/說明可以進行分解非終結(jié)符 cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production); break;case '+':switch(strindex)case '+':cout<

27、<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);cout<<"tMatch + ! "/" Popup + and next char in."<<endl;popstack(stack,&pointer);match=true;break;default:cout<<"Error! This character string is

28、 not this grammers sentence."<<endl;return 0;break;case '-':switch(strindex)case '-':cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);cout<<"tMatch - ! "/Popup - and next char in."<

29、;<endl;popstack(stack,&pointer);match=true;break;default:cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;break;case '*':switch(strindex)case '*':cout<<"n"show(stack,-1);cout<<"t"<<st

30、rindex<<"t"show(str,index);cout<<"tMatch * ! "/Popup * and next char in."<<endl;popstack(stack,&pointer);match=true;break;default:cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;break;case '/&

31、#39;:switch(strindex)case '/':cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);cout<<"tMatch / ! "/Popup / and next char in."<<endl;popstack(stack,&pointer);match=true;break;default:cout<&l

32、t;"Error! This character string is not this grammers sentence."<<endl;return 0;break;case '(':switch(strindex)case '(':cout<<"n"show(stack,-1);cout<<"t"<<strindex<<"t"show(str,index);cout<<"tMatch ( ! &q

33、uot;/Popup ( and next char in."<<endl;popstack(stack,&pointer);match=true;break;default:cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;break;case ')':switch(strindex)case ')':cout<<"n"show(stack,

34、-1);cout<<"t"<<strindex<<"t"show(str,index);cout<<"tMatch ) ! "/Popup ) and next char in."<<endl;popstack(stack,&pointer);match=true;break;default:cout<<"Error! This character string is not this grammers sentence."<<endl;return 0;break;case '':switch(strindex)case '':cout<<"n"show(stack,-1);cout<<"t"<<strindex<

溫馨提示

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

評論

0/150

提交評論