版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上 SHANGHAI UNIVERSITY<編譯原理>實(shí)驗(yàn)報(bào)告學(xué) 院計(jì)算機(jī)工程與科學(xué)學(xué)院組 號(hào)實(shí)驗(yàn)三日期2016.4.23學(xué)號(hào)姓名主要工作貢獻(xiàn)因子一、實(shí)驗(yàn)?zāi)康呐c要求l 給出 PL/0 文法規(guī)范,要求編寫 PL/0 語(yǔ)言的語(yǔ)法分析程序。l 通過設(shè)計(jì)、編制、調(diào)試一個(gè)典型的語(yǔ)法分析程序,實(shí)現(xiàn)對(duì)詞法分析程序所提供的單詞序列進(jìn)行語(yǔ)法檢查和結(jié)構(gòu)分析,進(jìn)一步掌握常用的語(yǔ)法分析方法。l 選擇一種語(yǔ)法分析方法(遞歸子程序法、LL(1)分析法、算符優(yōu)先分析法、SLR(1)分析法) ;選擇常見程序語(yǔ)言都具備的語(yǔ)法結(jié)構(gòu),如賦值語(yǔ)句,特別是表達(dá)式,作為分析對(duì)象確定語(yǔ)法分析的方法。l
2、將實(shí)驗(yàn)二“詞法分析”的輸出結(jié)果,作為表達(dá)式語(yǔ)法分析器的輸入,進(jìn)行語(yǔ)法解析,對(duì)于語(yǔ)法正確的表達(dá)式,報(bào)告“語(yǔ)法正確” ;對(duì)于語(yǔ)法錯(cuò)誤的表達(dá)式,報(bào)告“語(yǔ)法錯(cuò)誤” , 指出錯(cuò)誤原因。l 源程序中字符不區(qū)分大小寫,即: “a1”和“A1”是同一個(gè)標(biāo)識(shí)符。l 準(zhǔn)備至少 10 組測(cè)試用例, 每組測(cè)試用例包括: 輸入文件和輸出結(jié)果。二、 實(shí)驗(yàn)環(huán)境C+IDE:Codeblocks三、實(shí)驗(yàn)內(nèi)容已給 PL/0 語(yǔ)言文法,構(gòu)造表達(dá)式部分的語(yǔ)法分析器。分析對(duì)象算術(shù)表達(dá)式的 BNF 定義如下:<表達(dá)式> := +|-<項(xiàng)><加法運(yùn)算符> <項(xiàng)><項(xiàng)> := &l
3、t;因子><乘法運(yùn)算符> <因子><因子> := <標(biāo)識(shí)符>|<無(wú)符號(hào)整數(shù)>| (<表達(dá)式>)<加法運(yùn)算符> := +|-<乘法運(yùn)算符> := *|/四、 實(shí)驗(yàn)內(nèi)容的設(shè)計(jì)與實(shí)現(xiàn)源程序: #include<string>#include<cstring>#include<fstream>#include<stack>using namespace std;const string basic_word="begin","
4、;call","const","do","end","if","odd","procedure","read","then","var","while","write"const string basic_word_name="beginsym","callsym","constsym","dosy
5、m","endsym","ifsym","oddsym","proceduresym","readsym","thensym","varsym","whilesym","writesym"const string operator_="+-*/<>=:"/榪愮畻絎?const string operator_="+","-","
6、;*","/","=","#","<","<=",">",">=",":="const string operator_name="plus","minus","times","slash","eql","neq","lss","leq",&qu
7、ot;gtr","geq","becomes"const string cut_str="(),;. "const string cut_str_name="lparen","rparen","comma","semicolon","period","space"class compile private: string line;/瀛樺偍姣忎竴琛屾暟鎹? string buffer;/緙撳啿鍖? /int
8、 flag;/鏍囪瘑浣? /-1:鏃犲畾涔? /0:a-z A-Z /1:0-9 /2:+-*/<>=: /3:() ,;. /4:space,tab,n /5:闈?-4綾誨埆瀛楃 public: compile(string inname,string outname) ifstream fin(inname.c_str(),ios:in); fstream fout(outname.c_str(),ios:out); int flag=-1; if (fin.good() while (getline(fin, line)/鑾峰緱涓琛? int length=line.leng
9、th(); string t2="" string temp2; buffer="" for(int i=0;i<length;i+) flag=isWhat(linei); / cout<<linei<<endl; /cout<<buffer<<endl; temp2 =linei; /cout<<flag<<endl; if(i=length-1&&(flag=0|flag=1) string temp; temp=buffer; /cout<<
10、temp<<endl; buffer="" if(whatBasicWord(temp)!="no") fout<<"("<<whatBasicWord(temp)<<","<<temp<<")n" fout<<flush; cout<<"("<<whatBasicWord(temp)<<","<<temp<<&q
11、uot;)"<<endl; else if(isNumber(temp)=true) fout<<"("<<"number"<<","<<temp<<")n" fout<<flush; cout<<"("<<"number"<<","<<temp<<")"<<endl; e
12、lse if(markCheck(temp) fout<<"("<<"ident"<<","<<temp<<")n" fout<<flush; cout<<"("<<"ident"<<","<<temp<<")"<<endl; else fout<<"("<
13、<"illegal"<<","<<temp<<")n" fout<<flush; cout<<"("<<"illegal"<<","<<temp<<")"<<endl; else if(flag=0|flag=1|flag=5) buffer+=linei; /cout<<buffer<<endl; else
14、 string temp; temp=buffer; /cout<<temp<<endl; buffer="" if(temp!="") if(t2!="") if(t2=":") cout<<"(illegal,:)"<<endl; fout<<"(illegal,:)n" else cout<<"("<<whatOperator(t2)<<",&q
15、uot;<<t2<<")"<<endl; fout<<"("<<whatOperator(t2)<<","<<t2<<")n" t2="" fout<<flush; if(whatBasicWord(temp)!="no") fout<<"("<<whatBasicWord(temp)<<","
16、<<temp<<")n" fout<<flush; cout<<"("<<whatBasicWord(temp)<<","<<temp<<")"<<endl; else if(isNumber(temp)=true) fout<<"("<<"number"<<","<<temp<<"
17、;)n" fout<<flush; cout<<"("<<"number"<<","<<temp<<")"<<endl; else if(markCheck(temp) fout<<"("<<"ident"<<","<<temp<<")n" fout<<flush; cou
18、t<<"("<<"ident"<<","<<temp<<")"<<endl; else fout<<"("<<"illegal"<<","<<temp<<")n" fout<<flush; cout<<"("<<"illegal"&
19、lt;<","<<temp<<")"<<endl; if(flag=2) if(t2!="") if(t2=":"&&temp2!="=") cout<<"(illegal,:)"<<endl; fout<<"(illegal,:)n" fout<<flush; if(whatOperator(temp2)!="no") cout&
20、lt;<"("<<whatOperator(temp2)<<","<<temp2<<")"<<endl; fout<<"("<<whatOperator(temp2)<<","<<temp2<<")n" fout<<flush; else t2=temp2; else if(t2=">"|t2="<
21、"|t2=":")&&temp2="=") cout<<"("<<whatOperator(t2+temp2)<<","<<t2+temp2<<")"<<endl; fout<<"("<<whatOperator(t2+temp2)<<","<<t2+temp2<<")n" fou
22、t<<flush; t2="" else fout<<"("<<whatOperator(t2)<<","<<t2<<")n" fout<<flush; cout<<"("<<whatOperator(t2)<<","<<t2<<")"<<endl; cout<<"("&
23、lt;<whatOperator(temp2)<<","<<temp2<<")"<<endl; fout<<"("<<whatOperator(temp2)<<","<<temp2<<")n" fout<<flush; t2="" else if(temp2="<"|temp2=">"|temp2=&
24、quot;:") t2=temp2; else cout<<"("<<whatOperator(temp2)<<","<<temp2<<")"<<endl; fout<<"("<<whatOperator(temp2)<<","<<temp2<<")n" fout<<flush; else if(flag=3) /cout&
25、lt;<"33"<<temp2<<endl; cout<<"("<<whatCutStr(temp2)<<","<<temp2<<")"<<endl; fout<<"("<<whatCutStr(temp2)<<","<<temp2<<")n" fout<<flush; else if
26、(flag=4) else cout<<"can't open file"<<endl; int isWhat(char temp)/榪斿洖鏈瓧絎殑綾誨瀷 if(temp=' '|temp='t'|temp='n') return 4; if(isLetter(temp) return 0; if(isDigital(temp) return 1; if(isOperator_(temp) return 2; if(isCutStr(temp) return 3; return 5; strin
27、g whatCutStr(string temp) int i=0; for(i;i<cut_str.length();i+) if(temp0=cut_stri) return cut_str_namei; return "no" string whatBasicWord(string temp)/鍒柇鏄惁涓烘爣璇嗙 int i=0; int length=sizeof(basic_word)/sizeof(string); for(i;i<length;i+) if(isEqual(temp,basic_wordi) return basic_word_na
28、mei; return "no" string whatOperator(string temp) int length=sizeof(operator_)/sizeof(string); for(int i=0;i<length;i+) if(temp=operator_i) return operator_namei; return "no" bool isNumber(string temp) for(int i=0;i<temp.length();i+) if(!isDigital(tempi) return false; retur
29、n true; bool isOperator_(char temp)/鏄惁涓鴻繍綆楃 int i=0,length=operator_.length(); for(i;i<length;i+) if(temp=operator_i) break; if(i=length) return false; else return true; bool isCutStr(char temp)/鏄惁涓哄垎闅旂 int i=0,length=cut_str.length(); for(i;i<length;i+) if(temp=cut_stri) break; if(i=length) r
30、eturn false; else return true; bool isLetter(char temp)/鍒柇鏄惁涓哄瓧姣? if(temp>='a'&&temp<='z')|(temp>='A'&&temp<='Z') return true; return false; bool isDigital(char temp)/鍒柇鏄惁涓烘暟瀛? if(temp>='0'&&temp<='9') return t
31、rue; return false; bool markCheck(string tt)/鏍囪瘑絎悎娉曟鏌? if(tt0!='_'&&!isLetter(tt0) return false; else /cout<<tt<<endl; for(int i=1;i<tt.length();i+) if(!isLetter(tti)&&!isDigital(tti)&&tti!='_')/鍑虹幇 return false; return true; bool isEqual(string
32、 a1,string a2)/鍒柇涓釜瀛楃鏄惁鐩哥瓑 if(a1.length()!=a2.length() return false; else int length=a1.length(); for(int i=0;i<length;i+) if(a1i!=a2i&&a1i+32!=a2i&&a1i-32!=a2i) return false; return true; /* void writeInto(string filename)/鍐欏叆鏂囦歡 ofstream fout; fout.open(filename.c_str(); for(int
33、 i=0;i<len_;i+) fout<<"("<<indent_namei<<","<<indent_timesi<<")n" fout<<flush; fout.close(); */;class Grammarprotected: char state; int pre_k; bool flag; int index; string cate; string cate_str;public: Grammar() state='A' f
34、lag=1; pre_k=0; index=0; void setCate(string c,string c_str) cate=c; cate_str=c_str; void displayStatus() /cout<<pre_k<<endl; if(pre_k!=0) if(pre_k>0) cout<<"error:lost ')' to match '('. "<<endl; else cout<<"error:to more ')' at
35、 last."<<endl; if(flag=1&&(state='C'|state='D') cout<<"grammar legal!"<<endl; else if (flag=1&&(state!='C'|state!='D') cout<<"error:lost some expression at last!"<<endl; cout<<"grammar i
36、llegal!"<<endl; else cout<<"grammar illegal!"<<endl; void start(string inname) fstream fin(inname.c_str(),ios:in); string line,word,categrey,str; if(fin.good() while(getline(fin,line) int i=1; index+; categrey=""str="" word=line1; while(word!=&quo
37、t;,") categrey+=word; word=line+i; word=line+i; while(i!=line.length()-1) str+=word; word=line+i; /cout<<cate_str<<endl; cate=categrey; cate_str=str; if(cate_str="") break; inputStr(); else cout<<"can't open file"<<endl; void inputStr() if(state=&
38、#39;A') if(cate_str!="+"&&cate_str!="-"&&cate!="ident"&&cate!="number"&&cate_str!="(") cout<<"error:The begin of a expression must be '+' or '-' or '(' or 'ident' or '
39、number'.please check:"<<cate_str<<" ("<<index<<")"<<endl; flag=0; else if(cate_str="+"|cate_str="-") state='B' else if(cate="ident"|cate="number") state='B' inputStr(); else if(cate_str="(") pre_k+; else if(state='B') if(cate_str="(") state='A' pre_k+; else if(cate="ident"|cate="number") state='C
溫馨提示
- 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 腹腔鏡微創(chuàng)手術(shù)治療異位妊娠的臨床效果及安全性研究
- 二零二五年度林業(yè)碳匯交易林地承包合同范本3篇
- 二零二五年度環(huán)保產(chǎn)業(yè)委托擔(dān)保合同模板3篇
- 通信行業(yè)安全設(shè)備檢修
- 二零二五年度個(gè)人租賃車輛保險(xiǎn)合同范本2篇
- 《二零二五版水電站施工合同爭(zhēng)議解決及仲裁條款》3篇
- 二零二五年度電子商務(wù)平臺(tái)銷售擔(dān)保合同范本
- 初中學(xué)年度第二學(xué)期八年級(jí)地理教案
- 關(guān)注民生-加強(qiáng)公共安全-構(gòu)建和諧社會(huì)
- 二零二五年度金融創(chuàng)新產(chǎn)品居間服務(wù)合同3篇
- 《亞太經(jīng)合組織》課件
- 《會(huì)展概述》課件
- 《郴州市總體規(guī)劃》課件
- 【高中物理競(jìng)賽大全】 競(jìng)賽3 電磁學(xué) 50題競(jìng)賽真題強(qiáng)化訓(xùn)練解析版-高考物理備考復(fù)習(xí)重點(diǎn)資料歸納
- 再見2024你好2025展望未來
- 2025屆山東省濟(jì)南市歷城二中高二上數(shù)學(xué)期末學(xué)業(yè)質(zhì)量監(jiān)測(cè)試題含解析
- 2024年全國(guó)各地中考試題分類匯編:文學(xué)常識(shí)
- 2022年版義務(wù)教育語(yǔ)文課程標(biāo)準(zhǔn)題庫(kù)(教師教資培訓(xùn)考試專用十三套)
- 高考模擬作文“文化自信:春節(jié)走向世界”導(dǎo)寫+范文3篇
- 湖南汽車工程職業(yè)學(xué)院?jiǎn)握新殬I(yè)技能測(cè)試參考試題庫(kù)(含答案)
- 焊接機(jī)器人在汽車制造中應(yīng)用案例分析報(bào)告
評(píng)論
0/150
提交評(píng)論