




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
天津理工大學編譯原理實驗2語法分析實驗報告學院(系)名稱:計算機與通信工程學院姓名學號專業(yè)班級實驗項目實驗二:語法分析課程名稱編譯原理課程代碼2016年4月21日第1、2節(jié)實驗時間2016年4月26日第3、4節(jié)實驗地點計算機軟件實驗室7-2202016年4月28日第1、2節(jié)批改意見成績教師簽字:實驗內容:可選擇LL1分析法、算符優(yōu)先分析法、LR分析法之一,實現(xiàn)如下表達式文法的語法分析器:(1)E?E+T|E-T|T(2)T?T*F|T/F|F(3)F?P^F|P(4)P?(E)|i實驗目的:1(掌握語法分析的基本概念和基本方法;2(正確理解LL1分析法、算符優(yōu)先分析法、LR分析法的設計與使用方法。實驗要求:1(按要求設計實現(xiàn)能識別上述文法所表示語言的語法分析器,并要求輸出全部分析過程;2(要求詳細描述所選分析方法針對上述文法的分析表構造過程;3(完成對所設計語法分析器的功能測試,并給出測試數(shù)據(jù)和實驗結果;4(為增加程序可讀性,請在程序中進行適當注釋說明;5(整理上機步驟,總結經(jīng)驗和體會;6(認真完成并按時提交實驗報告。第1頁共13頁【實驗過程記錄(源程序、測試用例、測試結果及心得體會等)】使用的是LL(1)分析法,其分析表的構造方法和構造過程如下:實驗源代碼:#include<iostream>#include<cstring>#definesize1024usingnamespacestd;intgetLength(charstr[size]){inti=0;while(str[i]!='\0')i++;returni;}intgetstringLength(stringstr){inti=0;while(str[i]!='\0')i++;returni;}chargettop(charstack[size],inttop){if(stack[top]!='\0')returnstack[top];elsereturn'#';}voidpopstack(char*stack,int*pointer){intp=*pointer;cout<<"\tPopup"<<stack[p]<<"out!";stack[p]='\0';(*pointer)--;}voidpushstack(char*stack,int*pointer,stringstr){inti=0;intlength=getstringLength(str);cout<<"push"<<str<<"instackreversedorder.";for(i=length-1;i>=0;i--){(*pointer)++;第2頁共13頁stack[(*pointer)]=str[i];}}intgetcol(chartop){switch(top){case'+':return0;case'-':return1;case'*':return2;case'/':return3;case'^':return4;case')':return5;case'#':return6;case'(':return7;case'i':return8;default:cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence."<<endl;return-1;}}voidshow(charstr[size],intindex){intlength=getLength(str);if(index!=-1)cout<<"\t";for(inti=index+1;i<length;i++)cout<<str[i];}intmain(){charstr[size];//接受字符串的數(shù)組charstack[size];//進行比對的棧intpointer=-1;//指向棧頂?shù)闹羔榠ntlength=0;//記錄字符串長度intindex=0;//記錄輸入字符串第3頁共13頁chartop;inti,j;//i表示行,j表示列stringproduction;boolmatch=false;stringtable[7][9]={//+-*/^)#(i/*E*/"error","error","error","error","error","error","error","TX","TX",/*X*/"+TX","-TX","error","error","error","empty","empty","error","error",/*T*/"error","error","error","error","error","error","error","FY","FY",/*Y*/"empty","empty","*FY","/FY","error","empty","empty","error","error",/*F*/"error","error","error","error","error","error","error","PZ","PZ",/*Z*/"empty","empty","empty","empty","^F","empty","empty","error","error",/*P*/"error","error","error","error","error","error","error","(E)","i"};cout<<"Pleaseinputcharacterstring:";cin>>str;length=getLength(str);str[length]='#';str[length+1]='\0';//cout<<length<<endl;//cout<<table[0][0];//已驗證可以實現(xiàn)cout<<"符號棧\t當前符號\t輸入串\t\t\t\t說明"<<endl;pointer++;stack[pointer]='#';pointer++;stack[pointer]='E';//初始化棧,使棧底是#Ewhile(str[index]!='\0'){top=gettop(stack,pointer);switch(top){case'E':i=0;//第1行j=getcol(str[index]);production=table[i][j];if(production=="error"){cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence."<<endl;return0;}elseif(production=="empty"){cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);popstack(stack,&pointer);第4頁共13頁}else//說明可以進行分解非終結符{cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production);}break;case'X':i=1;//第2行j=getcol(str[index]);production=table[i][j];if(production=="error"){cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence."<<endl;return0;}elseif(production=="empty"){cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);popstack(stack,&pointer);}else//說明可以進行分解非終結符{cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production);}break;case'T':i=2;//第3行j=getcol(str[index]);production=table[i][j];if(production=="error"){cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence."<<endl;第5頁共13頁return0;}elseif(production=="empty"){cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);popstack(stack,&pointer);}else//說明可以進行分解非終結符{cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production);}break;case'Y':i=3;//第4行j=getcol(str[index]);production=table[i][j];if(production=="error"){cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence."<<endl;return0;}elseif(production=="empty"){cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);popstack(stack,&pointer);}else//說明可以進行分解非終結符{cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production);第6頁共13頁}break;case'F':i=4;//第5行j=getcol(str[index]);production=table[i][j];if(production=="error"){cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence."<<endl;return0;}elseif(production=="empty"){cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);popstack(stack,&pointer);}else//說明可以進行分解非終結符{cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production);}break;case'Z':i=5;//第6行j=getcol(str[index]);production=table[i][j];if(production=="error"){cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence."<<endl;return0;}elseif(production=="empty"){cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);popstack(stack,&pointer);第7頁共13頁}else//說明可以進行分解非終結符{cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production);}break;case'P':i=6;//第7行j=getcol(str[index]);production=table[i][j];if(production=="error"){cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence."<<endl;return0;}elseif(production=="empty"){cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);popstack(stack,&pointer);}else//說明可以進行分解非終結符{cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);popstack(stack,&pointer);pushstack(stack,&pointer,production);}break;case'+':switch(str[index]){case'+':cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";第8頁共13頁show(str,index);cout<<"\tMatch+!";//"Popup+andnextcharin."<<endl;popstack(stack,&pointer);match=true;break;default:cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence."<<endl;return0;}break;case'-':switch(str[index]){case'-':cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);cout<<"\tMatch-!";//Popup-andnextcharin."<<endl;popstack(stack,&pointer);match=true;break;default:cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence."<<endl;return0;}break;case'*':switch(str[index]){case'*':cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);cout<<"\tMatch*!";//Popup*andnextcharin."<<endl;popstack(stack,&pointer);match=true;break;default:cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence."<<endl;return0;}break;case'/':第9頁共13頁switch(str[index]){case'/':cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);cout<<"\tMatch/!";//Popup/andnextcharin."<<endl;popstack(stack,&pointer);match=true;break;default:cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence."<<endl;return0;}break;case'(':switch(str[index]){case'(':cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);cout<<"\tMatch(!";//Popup(andnextcharin."<<endl;popstack(stack,&pointer);match=true;break;default:cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence."<<endl;return0;}break;case')':switch(str[index]){case')':cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);cout<<"\tMatch)!";//Popup)andnextcharin."<<endl;popstack(stack,&pointer);match=true;break;第10頁共13頁default:cout<<"Error!Thischaracterstringisnotthisgrammer`ssentence."<<endl;return0;}break;case'^':switch(str[index]){case'^':cout<<"\n";show(stack,-1);cout<<"\t"<<str[index]<<"\t";show(str,index);cout<<"\
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 醫(yī)用織物管理規(guī)范
- 2025屆安徽省部分省示范中學高考化學考前最后一卷預測卷含解析
- 心內科危重病人護理常規(guī)
- 工廠環(huán)境安全主題
- 小學生生命生存生活教育
- 統(tǒng)編版(2024)語文一年級下冊第八單元綜合素質測評B卷(含答案)
- 第二單元評估檢測題(單元測試)無答案六年級下冊數(shù)學冀教版
- 2025年弱粘煤項目合作計劃書
- 彈力小車課件
- 宿舍美甲店創(chuàng)業(yè)計劃書
- 企業(yè)廉潔風險防控課件教學
- 中醫(yī)護理三基練習題庫+答案
- 2025年護士三基考核試題及答案
- 七年級下冊2025春季歷史 教學設計《明朝對外關系》 學習資料
- 《設備管理標準化實施手冊》
- 湖南省長沙市明達中學2024-2025學年九年級下學期入學考試英語試卷(含答案無聽力原文及音頻)
- 汽車站建設項目可行性研究報告
- 《中國古典園林之美》課件
- 2024年09月上海2024交通銀行交銀金融科技校園招考筆試歷年參考題庫附帶答案詳解
- 2025年人教五四新版八年級數(shù)學上冊階段測試試卷
- 2025年廣西中馬欽州產業(yè)園區(qū)管委會招商服務有限公司招聘筆試參考題庫附帶答案詳解
評論
0/150
提交評論