編譯原理語法分析_第1頁
編譯原理語法分析_第2頁
編譯原理語法分析_第3頁
編譯原理語法分析_第4頁
編譯原理語法分析_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、可編輯上機(jī)練習(xí)二:語法分析一、根據(jù)上機(jī)練習(xí)一給出的PL/0語言擴(kuò)充的巴克斯范式語法描述,利用遞歸下降的語法分析方法,編寫PL/0語言的語法分析程序。要求:1. 對給出的PL/0語言進(jìn)行分析,證明其可以進(jìn)行自上而下的語法分析;2. 對block、proc、statement、condition、expression、term、factor進(jìn)行分析,畫出語法分析圖,在此基礎(chǔ)上描述這些子程序的設(shè)計思想;3. 具有一定的語法錯誤處理能力;二、源代碼#include#include#include #include#includeusing namespace std;struct yufastring

2、 SYM; /單詞的類別string strToken; /用戶所定義的標(biāo)識符的值int l; /記錄換行符的個數(shù),即記錄源文件的行數(shù)yufa0, yufa1, yufa21000;/yufa1用于保存各個單詞,yufa1用于詞法分析過程;yufa0主要用于語法分析;char ch;int mm= 0; /結(jié)構(gòu)體數(shù)組下標(biāo)int line = 0;/出錯位置/char ch = ;string key15 = begin, end, if, then, else, while, write, read,do, call, const, var, procedure, program, odd ;

3、/預(yù)設(shè)保留字void prog();void block();void condecl();void _const();void vardecl();void proc();void body();void statement();void lexp();void exp();void term();void factor();void error0()cout program標(biāo)示符出錯 endl;exit(0); /出錯即退出 void error1()cout 第 line 行缺少分號 endl;void error2()cout 第 line 行標(biāo)識符(變量名)出錯 endl;void

4、error3()cout 第 line 常量未聲明(缺少const) endl;void error4()cout 第 line 常量未賦值 endl;void error5()cout 第 line 賦值號出錯或者缺少賦值號 endl;void error6()cout 第 line 變量未聲明(缺少var) endl; void error7()cout 第 line 行缺少右括號 endl;void error8()cout 第 line 行缺少左括號 endl;void error9()cout 第 line 行缺少參數(shù) endl;void error10()cout 第 line 行

5、procedure錯誤 endl;void error11()cout 第 line 行缺少end(begin與end不匹配) endl;void error12()cout 第 line 行缺少then(if與then不匹配) endl;void error13()cout 第 line 行缺少do(while與do不匹配) endl;void error14()cout 第 line 行調(diào)用過程缺少標(biāo)識符 endl;void error15()cout 第 line 行缺少分號 endl;void error16()cout 第 line 行條件語句出錯(缺少界符) endl;void p

6、rog()/程序的遞歸子程序?qū)崿F(xiàn)line+;if (yufa0.SYM = program)/ programmm+; yufa0.SYM = yufa2mm.SYM;if (yufa0.SYM = biaoshifu) / ,即標(biāo)識符 mm+; yufa0.SYM = yufa2mm.SYM;if (yufa0.SYM = ;)/;mm+; yufa0.SYM = yufa2mm.SYM;block();else error1();/缺少; else error2();/缺少標(biāo)識符或者標(biāo)識符錯誤 else error0();cout nn-語法分析結(jié)束-nn endl;void block(

7、)/程序體的遞歸子程序?qū)崿F(xiàn), if (yufa0.SYM = const)condecl();if (yufa0.SYM = var)vardecl();if (yufa0.SYM = procedure)proc();body();void condecl()/常量說明的遞歸子程序?qū)崿F(xiàn), const ,;line+;if (yufa0.SYM = const) / constmm+; yufa0.SYM = yufa2mm.SYM;_const();/while (yufa0.SYM = ,)/,逗號mm+; yufa0.SYM = yufa2mm.SYM;_const();/if (yuf

8、a0.SYM = ;)/;mm+; yufa0.SYM = yufa2mm.SYM;else error1();/缺少; else error3();/缺少常量定義const void _const()/常量, :=if (yufa0.SYM = biaoshifu) / mm+; yufa0.SYM = yufa2mm.SYM;if (yufa0.SYM = fuzhi)mm+; yufa0.SYM = yufa2mm.SYM;if (yufa0.SYM = didit)mm+; yufa0.SYM = yufa2mm.SYM;else error4();/常量未賦值 else error5

9、();/缺少賦值號,或者賦值號錯誤 else error2(); /缺少標(biāo)識符 void vardecl()/變量說明的遞歸子程序?qū)崿F(xiàn), var ,;line+;if (yufa0.SYM = var) mm+; yufa0.SYM = yufa2mm.SYM;if (yufa0.SYM = biaoshifu) /mm+; yufa0.SYM = yufa2mm.SYM;while (yufa0.SYM = ,)/,mm+; yufa0.SYM = yufa2mm.SYM;/下一單詞if (yufa0.SYM = biaoshifu) /mm+; yufa0.SYM = yufa2mm.SY

10、M;else error2();/缺少標(biāo)識符即缺少變量 if (yufa0.SYM = ;)mm+; yufa0.SYM = yufa2mm.SYM;/下一單詞else error2();else error6();/變量未聲明 void proc()/ procedure (,);line+;if (yufa0.SYM = procedure) /proceduremm+; yufa0.SYM = yufa2mm.SYM;if (yufa0.SYM = biaoshifu) / mm+; yufa0.SYM = yufa2mm.SYM;if (yufa0.SYM = () /(mm+; yu

11、fa0.SYM = yufa2mm.SYM;if (yufa0.SYM = biaoshifu)/ mm+; yufa0.SYM = yufa2mm.SYM;while (yufa0.SYM = ,)/ ,mm+; yufa0.SYM = yufa2mm.SYM;if (yufa0.SYM = biaoshifu)mm+; yufa0.SYM = yufa2mm.SYM;else error2();if (yufa0.SYM = )/ )mm+; yufa0.SYM = yufa2mm.SYM;if (yufa0.SYM = ;)/;mm+; yufa0.SYM = yufa2mm.SYM;/

12、下一單詞-block();while (yufa0.SYM = ;)mm+; yufa0.SYM = yufa2mm.SYM;proc();else error1();/ 缺少; else error7();/括號不匹配 (右括號少) else error8();/左括號缺少 else error9();/缺少參數(shù) else error10();/procedure錯誤 void body()/ begin ;endif (yufa0.SYM = begin)line+;mm+; yufa0.SYM = yufa2mm.SYM;statement();/while (yufa0.SYM = b

13、iaoshi) | (yufa0.SYM = if) | (yufa0.SYM = while) |/ (yufa0.SYM = call) | (yufa0.SYM = read) | (yufa0.SYM = write) | (yufa0.SYM = begin)/error1();/語句未到最后一條,缺少;while(yufa0.SYM = ;)mm+; yufa0.SYM = yufa2mm.SYM;statement();if (yufa0.SYM = end)line +;mm+; yufa0.SYM = yufa2mm.SYM;else error11();/缺少end與beg

14、in匹配 else error12();/缺少begin/ := /語句 / |if then else / |while do / |call (,)/ |/ |read (,)/ |write (,)void statement()if (yufa0.SYM = biaoshifu) / := line+;mm+; yufa0.SYM = yufa2mm.SYM;if (yufa0.SYM = fuzhi)mm+; yufa0.SYM = yufa2mm.SYM;exp();else error5();/賦值號錯誤 else if (yufa0.SYM = if) /if then els

15、e line+;mm+; yufa0.SYM = yufa2mm.SYM;lexp();if (yufa0.SYM = then) mm+; yufa0.SYM = yufa2mm.SYM;statement();if (yufa0.SYM = else)mm+; yufa0.SYM = yufa2mm.SYM;statement();else error12();/缺少then else if (yufa0.SYM = while)/while do line+;mm+; yufa0.SYM = yufa2mm.SYM;lexp();if (yufa0.SYM = do) mm+; yufa

16、0.SYM = yufa2mm.SYM;statement();else error13();/缺少do不匹配(while 與do 不匹配) else if (yufa0.SYM = call)/call (,)line+;mm+; yufa0.SYM = yufa2mm.SYM;if (yufa0.SYM = biaoshifu) /mm+; yufa0.SYM = yufa2mm.SYM;if (yufa0.SYM = () /mm+; yufa0.SYM = yufa2mm.SYM;while(yufa0.SYM = ,)mm+;yufa0.SYM = yufa2mm.SYM;exp()

17、; if (yufa0.SYM = )mm+; yufa0.SYM = yufa2mm.SYM; else error7();/缺少右括號/此處不會報錯,(,)因為表示可有可無的else error14();/調(diào)用過程缺少標(biāo)識符 else if (yufa0.SYM = read)/read (,)line+;mm+; yufa0.SYM = yufa2mm.SYM;if (yufa0.SYM = () mm+; yufa0.SYM = yufa2mm.SYM;if (yufa0.SYM = biaoshifu) mm+; yufa0.SYM = yufa2mm.SYM;while (yufa

18、0.SYM = ,)mm+; yufa0.SYM = yufa2mm.SYM;if (yufa0.SYM = biaoshifu) mm+; yufa0.SYM = yufa2mm.SYM;else error2();/缺少標(biāo)示符if (yufa0.SYM = ) mm+; yufa0.SYM = yufa2mm.SYM;else error7();/缺少右括號else error9();/read缺少參數(shù) else error8();/缺少左括號else if (yufa0.SYM = write)/write (,)line+;mm+; yufa0.SYM = yufa2mm.SYM; i

19、f (yufa0.SYM = () mm+; yufa0.SYM = yufa2mm.SYM; exp();while (yufa0.SYM = ,) mm+; yufa0.SYM = yufa2mm.SYM; exp();if (yufa0.SYM = ) mm+; yufa0.SYM = yufa2mm.SYM; else error7();/缺少右括號else error8();/缺少左括號else body();/ |odd / +|-/ /|()/ =|=/ +|-/ *|/ ll|d (注:l表示字母)/ ddvoid lexp()/ |odd if (yufa0.SYM = od

20、d)mm+;yufa0.SYM = yufa2mm.SYM;exp();else exp();if (yufa0.SYM = ge) | (yufa0.SYM = g) | (yufa0.SYM = l) | (yufa0.SYM = ne) | (yufa0.SYM = l) | (yufa0.SYM = =)mm+;yufa0.SYM = yufa2mm.SYM;exp();elseerror15();/條件語句出錯,缺少界符 /odd是否存在? void exp()/ +|-while(yufa0.SYM = +) | (yufa0.SYM = -)mm+;yufa0.SYM = yuf

21、a2mm.SYM;term();/ term();while (yufa0.SYM = +) | (yufa0.SYM = -)/ +|- mm+;yufa0.SYM = yufa2mm.SYM;term(); void term()/ factor();while (yufa0.SYM = *) | (yufa0.SYM = /)/ *|/ mm+;yufa0.SYM = yufa2mm.SYM;factor();/factor()void factor()/|()if (yufa0.SYM = biaoshi)mm+;yufa0.SYM = yufa2mm.SYM;else if (yuf

22、a0.SYM = digit)mm+;yufa0.SYM = yufa2mm.SYM;else if (yufa0.SYM = ()mm+;yufa0.SYM = yufa2mm.SYM;exp();if (yufa0.SYM = )mm+;yufa0.SYM = yufa2mm.SYM;else error7(); /缺少右括號else error8();/缺少左括號 string Concat(char ch,string strToken) strToken += ch; return strToken;int IsLetter(char c ) /判斷字母 if (c = a) | (

23、c = A) return 1;else return 0;int IsDigit(char c) /判斷數(shù)字 if (c = 0)&(c = 9) return 1;else return 0;int IsKey(string StrToken) /判斷保留字 int i;for (i = 0; i15; i+) if (pare(StrToken) = 0) return 1;return 0;void getsym(fstream &file)string StrToken = ;/if (ch = | ch = t | ch = n) /濾掉空白字符/ch = file

24、.get();if (IsLetter(ch)while (IsLetter(ch) | IsDigit(ch)StrToken = Concat(ch,StrToken);ch = file.get();if (IsKey(StrToken)int i;for (i = 0; i15; i+)if (pare(StrToken) = 0)yufa1.SYM = keyi;yufa1.strToken = StrToken;elseyufa1.SYM = biaoshifu;yufa1.strToken = StrToken; if (IsDigit(ch) StrToken

25、= ; while(IsDigit(ch) StrToken = Concat(ch,StrToken);ch = file.get(); if(IsLetter(ch) /數(shù)字后面跟字母,錯誤詞法 while(IsLetter(ch)|IsDigit(ch)StrToken = Concat(ch,StrToken);ch = file.get();cout 詞法錯誤!(數(shù)字后面跟字母) StrToken endl;elseyufa1.SYM = digit;yufa1.strToken = StrToken;else switch (ch)case+:yufa1.SYM = +;yufa1

26、.strToken = +;ch = file.get();break;case-:yufa1.SYM = -;yufa1.strToken = -;ch = file.get();break;case*:yufa1.SYM = *;yufa1.strToken = *;ch = file.get();break;case/:yufa1.SYM = /;yufa1.strToken = /;ch = file.get();break;case(:yufa1.SYM = (;yufa1.strToken = (;ch = file.get();break;case):yufa1.SYM = );

27、yufa1.strToken = );ch = file.get();break;case=:yufa1.SYM = =;yufa1.strToken = =;ch = file.get();break;case;:yufa1.SYM = ;yufa1.strToken = ;ch = file.get();break;case,:yufa1.SYM = ,;yufa1.strToken = ,;ch = file.get();break;case:ch = file.get();if (ch = =)yufa1.SYM = fuzhi;yufa1.strToken = :=;elsecout : 詞法錯誤(:后面沒有=) :ch = file.get();if (ch = =)yufa1.SYM = ge;yufa1.strToken = =;elseyufa1.SYM = g;yufa1.strToken = ;ch = file.get();break;case:ch = file.get();i

溫馨提示

  • 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論