語法分析器實驗報告_第1頁
語法分析器實驗報告_第2頁
語法分析器實驗報告_第3頁
語法分析器實驗報告_第4頁
語法分析器實驗報告_第5頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、詞法分析器實驗報告實驗名稱:語法分析器實驗內(nèi)容:利用LL (1)或LR (1)分析語句語法,判斷其是否符合可識別語法。學會根據(jù) 狀態(tài)變化、first、follow或歸約轉移思想構造狀態(tài)分析表,利用堆棧對當前內(nèi)容進行有效判 斷實驗設計:實現(xiàn)功能可對一段包含加減乘除括號的賦值語句進行語法分析,其必須以$為終結符,語句間以; 隔離,判斷其是否符合語法規(guī)則,依次輸出判斷過程中所用到的產(chǎn)生式,并輸出最終結 論,若有錯誤可以報錯并提示錯誤所在行數(shù)及原因實驗步驟設計文法消除左遞歸提取公共左因子求first,follow*構造分析表*設計程序編寫程序算法與數(shù)據(jù)結構LLtable:left記錄產(chǎn)生式左端字符;r

2、ight記錄產(chǎn)生式右端字符;ln記錄產(chǎn)生式右端字 符長度Status:記錄token分析情況Token: category,類型;value,具體內(nèi)容根據(jù)LL (1)算法,手工構造分析表,并將內(nèi)容用數(shù)組存儲,便于查找先將當前語句的各token按序存儲,當前處理語句最后一個token以#標記,作為輸 入流與產(chǎn)生式比較,堆棧中初始放入#,x,a為處理輸入流中當前讀頭內(nèi)容/ 若top=a= # 表示識別成功,退出分析程序/ 若top=a! = # 表示匹配,彈出棧頂符號,讀頭前進一個/ 若top為i或n,但top! =a,出錯,輸出當前語句所在行,出錯具體字符/ 若top不為i或n,查預測分析表,若

3、其中存放關于top產(chǎn)生式,則彈出top, 將產(chǎn)生式右部自右向左壓入棧內(nèi),輸出該產(chǎn)生式,若其中沒有產(chǎn)生式,出錯, 輸出當前語句所在行,出錯具體字符以;作為語句終結,每次遇到分號則處理之前語句并清空后預備下語句處理,當遇 至U$表示該段程序結束,停止繼續(xù)處理分析表構造過程x-i=ee-e+t|e-t|tt-t*f|t/f|ff-(e)|i|nnote: i表示變量,n表示數(shù)字,!表示空串提取左公因子x-i=ee-ea|ta-+t|-tt-tb|fb-*f|/ff-(e)|i|n消除左遞歸x-i=e e-tcc-ac|!a-+t|-tt-fdd-bd|!b-*e|/ff-(e)|i|n5.類d) F

4、IRST & FOLLOWFIRST(x)=iFOLLOW(x)=#FIRST(e)=(,i,nFOLLOW(e)=札)FIRST(t)=(,i,nFOLLOW(t)=札+,-, )FIRST(c)=+,-,!FOLLOW(c)=札)FIRST(a)=+,-FOLLOW(a)=札+,-, ) FIRST(f)= (,i,nFOLLOW(f)=札+,-,/,*FIRST(d)=*,/,!FOLLOW(d)=#,+,-,) FIRST(b)=*,/FOLLOW(b)=札+,-,/,*e)分析表012345678+-*/()in#0 xx-i=e1ee-tce-tce-tc2cc-acc-acc-!

5、c-!3aa-+ta-t4tt-fdt-fdt-fd5dd-!d-!d-bdd-bdd-!d-!6bb-*fb-/f7ff-(e)f-nclass parserpublic:LLtable table100100;void scanner();parser(istream& in);int getLine() const;/LL(1)表掃描輸入流中內(nèi)容并分析/初始化,得到輸入文件地址得到當前行數(shù)分析語法/分析堆棧/建立LL (1)表取字符所在表中行/取字符所在表中列輸入流插入當前tokenprivate:int match();stack proStack;void constructTabl

6、e();int getRow(char ch);int getCol(char ch);istream* pstream;void insertToken(token& t);status getToken(token& t);int getChar();int peekChar();void putBackChar(char ch);void skipChar();void initialization();int line;token tokens1000;int counter;6.主要代碼/找到 token得到當前字符下一個字符將字符放回跳過當前字符/初始化堆棧等當前行數(shù)字符表記錄當前

7、字符表使用范圍/建立LL (1)表void parser:constructTable()for (int i=0;i8;i+)for (int j=0;j9;j+)tableij.left=;for (int k=0;k3;k+)tableij.rightk=;table06.left=x;table06.ln=3;table06.right0=i;table06.right1=;table06.right2=e;table14.left=e;table14.ln=2;table14.right0=t;table14.right1=c;table16.left=e;table16.ln=2;

8、table16.right0=t;table16.right1=c;table17.left=e;table17.ln=2;table17.right0=t;table17.right1=c;table20.left=c;table20.ln=2;table20.right0=a;table20.right1=c;table21.left=c;table21.ln=2;table21.right0=a;table21.right1=c;table25.left=c;table25.ln=0;table25.right0=!;table28.left=c;table28.ln=0;table28

9、.right0=!;table30.left=a;table30.ln=2;table30.right0=+;table30.right1=t;table31.left=a;table31.ln=2;table31.right0=-;table31.right1=t;table44.left=t;table44.ln=2;table 44.right0=f;table44.right1=d;table46.left=t;table46.ln=2;table 46.right0=f;table46.right1=d;table47.left=t;table47.ln=2;table 47.rig

10、ht0=f;table47.right1=d;table50.left=d;table50.ln=0;table50.right0=!;table51.left=d;table51.ln=0;table51.right0=!;table52.left=d;table52.ln=2;table52.right0=b;table52.right1=d;table53.left=d;table53.ln=2;table53.right0=b;table53.right1=d;table55.left=d;table55.ln=0;table55.right0=!;table58.left=d;tab

11、le58.ln=0;table58.right0=!;table62.left=b;table62.ln=2;table62.right0=*;table62.right1=f;table63.left=b;table63.ln=2;table63.right0=/;table63.right1=f;table74.left=f;table74.ln=3;table74.right0=(; table74.right1=e;table74.right2=);table76.left=f;table76.ln=1;table76.right0=i;table77.left=f;table77.l

12、n=1;table77.right0=n;int parser:match()分析語法ofstream ofs(out.txt,ios:app);char a;int i=0;for (int p=0;pcounter;p+)couttokensp.value;ofstokensp.value;coutendl;ofsendlANALYSIS:endl;while(1)if(tokensi.category=n | tokensi.category=i)a=tokensi.category;elsea=(tokensi.value)0;if(a=proStack.top()if(a=#)cou

13、tThis is valid!endlendl;ofsThis is valid!endlendl;return 0;elseproStack.pop();i+;elseif(proStack.top() =n| proStack.top() =i)if(a!=#)coutERROR(LINE getLine() ): a cannot be matchedendl;ofsERROR(LINE getLine() ): a cannot be matchedendl;elsecoutERROR(LINE getLine() ): Unexpected endingendl;ofsERROR(L

14、INE getLine() ): Unexpected endingendl;coutThis is invalid!endlendl;ofsThis is invalid!endlendl;return 0;elseif(tablegetRow(proStack.top()getCol(a).left!=)char pst=proStack.top();int n=tablegetRow(pst)getCol(a).ln;int k=0;ofstablegetRow(pst)getCol(a).lefttablegetRow(pst)getCol(a).right0tablegetRow(p

15、st)g etCol(a).right1tablegetRow(pst)getCol(a).right20) /coutn tablegetRow(pst)getCol(a).rightn-1endl;proStack.push(tablegetRow(pst)getCol(a).rightn-1); n-;else if(a!=#)coutERROR(LINE getLine() ): a cannot be matchedendl; ofsERROR(LINE getLine(): a cannot be matchedendl; else coutERROR(LINE getLine()

16、: Unexpected endingendl; ofsERROR(LINE getLine() ): Unexpected endingendl;coutThis is invalid!endlendl;ofsThis is invalid!endli=e etc tfdf-n d-! cac a+tERROR(LINE 1 ): Unexpected ending This is invalid!v=*mANALYSIS:x-i=eERROR(LINE 2 ) : * cannot be matched This is invalid!ANALYSIS: x-i=e e-tc tfd f-

17、i d-! c_ac a-t tfd f-n d一!二out-記事本J 文件(B 編卓舊格式(Q)查看(Y) 幫助(H)d-! c-!This is valid!q二m+4/x ANALYSIS: xi=e e_tc t-fd f-i d-! c-ac a-+t tfd f-n dbd b/f f-i d-! c-!This is valid!x=x*(7+5) ANALYSIS: x-i=e e_tc tfd f-i dbd b*f f(e) etc |out-記事本文件(E)編輯舊格式(Q) (V) 幫助etctfdf-nd-!c_aca-+tt-fdf-nd-!c-!d-!c-!This

18、 is valid!x= (5+(4*m/7-3)*t ANALYSIS: xi=e etc tfd f-(e) etc tfd f-n d-!c_ac a+t t-fd f-(e) etc t-fd f-:n dbdI out-記事本文件(B (E)格式。(v)幫助fd catfetf&bfdbfd c & cd c&bf & cctdecd df df c t d d f n 1- a + f(tf nb*ib/n! a - f n ! ! ! ! b * i ! ! s iis valid!實驗總結:原本以為處理四則運算賦值將會很困難,但在使用LL (1)后發(fā)現(xiàn),思路還是挺清晰 簡單的,但在實驗過程中,由于LL(1)不能出現(xiàn)左遞歸

溫馨提示

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

評論

0/150

提交評論