




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、上海電力學(xué)院編譯原理課程實驗報告 實驗名稱: 實驗三 自下而上語法分析及語義分析 院系: 計算機科學(xué)與技術(shù)學(xué)院 專業(yè)年級: 學(xué)生姓名: 學(xué)號: 指導(dǎo)老師: 實驗日期: 實驗三自上而下的語法分析 一、實驗?zāi)康模?通過本實驗掌握LR分析器的構(gòu)造過程,并根據(jù)語法制導(dǎo)翻譯,掌握屬性文法的自下而上計算的過程。 二、實驗學(xué)時: 4學(xué)時。 三、實驗內(nèi)容 根據(jù)給出的簡單表達式的語法構(gòu)成規(guī)則(見五),編制LR分析程序,要求能對用給定的語法規(guī)則書寫的源程序進行語法分析和語義分析。對于正確的表達式,給出表達式的值。對于錯誤的表達式,給出出錯位置。四、實驗方法 采用LR分析法。首先給出S-屬性文法的定義(為簡便起見,
2、每個文法符號只設(shè)置一個綜合屬性,即該文法符號所代表的表達式的值。屬性文法的定義可參照書137頁表6.1),并將其改造成用LR分析實現(xiàn)時的語義分析動作(可參照書145頁表6.5)。接下來給出LR分析表。然后程序的具體實現(xiàn): l LR分析表可用二維數(shù)組(或其他)實現(xiàn)。l 添加一個val棧作為語義分析實現(xiàn)的工具。l 編寫總控程序,實現(xiàn)語法分析和語義分析的過程。注:對于整數(shù)的識別可以借助實驗1。五、文法定義 簡單的表達式文法如下: (1)E->E+T(2)E->E-T(3)E->T(4)T->T*F(5)T->T/F(6)T->F(7)F->(E)(8)F-&
3、gt;i狀態(tài)ACTION(動作)GOTO(轉(zhuǎn)換)i+-*/()#ETF0S5S41231S6S12acc2R3R3S7S13R3R33R6R6R6R6R6R64S5S48235R8R8R8R8R8R86S5S4937S5S4108S6R12S119R1R1S7S13R1R110R4R4R4R4R4R411R7R7R7R7R7R712S5S414313S5S41514R2R2S7S13R2R215R5R5R5R5R5R55、 處理程序例和處理結(jié)果例 示例1:20133191*(20133191+3191)+ 3191#六、源代碼【cifa.h】/cifa.h#include<string&
4、gt;using namespace std;/單詞結(jié)構(gòu)定義struct WordTypeint code;string pro;/函數(shù)聲明WordType get_w();void getch();void getBC();bool isLetter();bool isDigit();void retract();int Reserve(string str);string concat(string str);【Table.action.h】/table_action.hclass Table_actionint row_num,line_num;int lineName8;string
5、tableData168;public:Table_action()row_num=16;line_num=8;lineName0=30;lineName1=7;lineName2=13;lineName3=8;lineName4=14;lineName5=1;lineName6=2;lineName7=15;lineName8=0;for(int m=0;m<row_num;m+)for(int n=0;n<line_num;n+)tableDatamn=""tableData00="S5"tableData05="S4"
6、;tableData11="S6"tableData12="S12"tableData17="acc"tableData21="R3"tableData22="R3"tableData23="S7"tableData24="S13"tableData26="R3"tableData27="R3"tableData31="R6"tableData32="R6"tableData3
7、3="R6"tableData34="R6"tableData36="R6"tableData37="R6"tableData40="S5"tableData45="S4"tableData51="R8"tableData52="R8"tableData53="R8"tableData54="R8"tableData56="R8"tableData57="R8&quo
8、t;tableData60="S5"tableData65="S4"tableData70="S5"tableData75="S4"tableData81="S6"tableData82="S12"tableData86="S11"tableData91="R1"tableData92="R1"tableData93="S7"tableData94="S13"tableData
9、96="R1"tableData97="R1"tableData101="R4"tableData102="R4"tableData103="R4"tableData104="R4"tableData106="R4"tableData107="R4"tableData111="R7"tableData112="R7"tableData113="R7"tableData114=&
10、quot;R7"tableData116="R7"tableData117="R7"tableData120="S5"tableData125="S4"tableData130="S5"tableData135="S4"tableData141="R2"tableData142="R2"tableData143="S7"tableData144="S13"tableData146=&qu
11、ot;R2"tableData147="R2"tableData151="R5"tableData152="R5"tableData153="R5"tableData154="R5"tableData155="R5"tableData156="R5"tableData157="R5"string getCell(int rowN,int lineN)int row=rowN;int line=getLineNumber(lin
12、eN);if(row>=0&&row<row_num&&line>=0&&line<=line_num)return tableDatarowline;else return""int getLineNumber(int lineN)for(int i=0;i<line_num;i+)if(lineNamei=lineN)return i;return -1;【Table_go.h】/table_go.hclass Table_goint row_num,line_num;/行數(shù)、列數(shù)string
13、 lineName3;int tableData163;public:Table_go()row_num=16;line_num=3;lineName0="E"lineName1="T"lineName2="F"for(int m=0;m<row_num;m+)for(int n=0;n<line_num;n+)tableDatamn=0;tableData00=1;tableData01=2;tableData02=3;tableData40=8;tableData41=2;tableData42=3;tableData
14、61=9;tableData62=3;tableData72=10;tableData121=14;tableData122=3;tableData132=15;int getCell(int rowN,string lineNa)int row=rowN;int line=getLineNumber(lineNa);if(row>=0&&row<row_num&&line<=line_num)return tableDatarowline;elsereturn -1;int getLineNumber(string lineNa)for(in
15、t i=0;i<line_num;i+)if(lineNamei=lineNa)return i;return -1;【Stack_num.h】class Stack_numint i; /棧頂標記int *data; /棧結(jié)構(gòu)public:Stack_num() /構(gòu)造函數(shù) data=new int100;i=-1;int push(int m) /進棧操作 i+;datai=m;return i;int pop() /出棧操作i-;return datai+1;int getTop() /返回棧頂return datai;Stack_num() /析構(gòu)函數(shù)delete data;in
16、t topNumber()return i;void outStack()for(int m=0;m<=i;m+)cout<<datam;【Stack_str.h】class Stack_strint i; /棧頂標記string *data; /棧結(jié)構(gòu)public:Stack_str() /構(gòu)造函數(shù) data=new string50;i=-1;int push(string m) /進棧操作 i+;datai=m;return i;int pop() /出棧操作datai=""i-;return i;string getTop() /返回棧頂retur
17、n datai;Stack_str() /析構(gòu)函數(shù)delete data;int topNumber()return i;void outStack()for(int m=0;m<=i;m+)cout<<datam;【cifa.cpp】/cifa.cpp#include<iostream>#include<string>#include"cifa.h"using namespace std;/關(guān)鍵字表和對應(yīng)的編碼string codestring10="main","int","if
18、","then","else","return","void","cout","endl"int codebook10=26,21,22,23,24,25,27,28,29;/全局變量char ch;int flag=0;/*/主函數(shù)int main()WordType word;cout<<"請輸入源程序序列:"word=get_w(); while(!="#")/#為自己設(shè)置的結(jié)束標志cout&
19、lt;<"("<<word.code<<","<<"“"<<<<"”"<<")"<<endl;word=get_w();return 0;*/WordType get_w()string str=""int code;WordType wordtmp;getch();/讀一個字符getBC();/去掉空白符if(isLetter() /以字母開頭while(isLette
20、r()|isDigit()str=concat(str);getch();retract();code=Reserve(str);if(code=-1)wordtmp.code=0;=str;/不是關(guān)鍵字elsewordtmp.code=code;=str;/是關(guān)鍵字else if(isDigit() /以數(shù)字開頭while(isDigit()str=concat(str);getch();retract();wordtmp.code=30;=str;else if(ch='(') wordtmp.code=1;
21、="("else if(ch=')') wordtmp.code=2;=")"else if(ch='') wordtmp.code=3;=""else if(ch='') wordtmp.code=4;=""else if(ch='') wordtmp.code=5;=""else if(ch='=') w
22、ordtmp.code=6;="="else if(ch='+') wordtmp.code=7;="+"else if(ch='*') wordtmp.code=8;="*"else if(ch='>') wordtmp.code=9;=">"else if(ch='<') wordtmp.code=10;="&l
23、t;"else if(ch=',') wordtmp.code=11;=","else if(ch=''') wordtmp.code=12;="'"else if(ch='-') wordtmp.code=13;="-" else if(ch='/') wordtmp.code=14;="/"else if(ch='#')
24、 wordtmp.code=15;="#"else if(ch='|') wordtmp.code=16;="|"else wordtmp.code=100;=ch;return wordtmp;void getch()if(flag=0) /沒有回退的字符ch=getchar();else /有回退字符,用回退字符,并設(shè)置標志flag=0;void getBC()while(ch=' '|ch='t'|ch='n')ch=g
25、etchar();bool isLetter()if(ch>='a'&&ch<='z'|ch>='A'&&ch<='Z')return true;else return false;bool isDigit()if(ch>='0'&&ch<='9')return true;else return false;string concat(string str)return str+ch;void retract()fl
26、ag=1;int Reserve(string str)int i;for(i=0;i<=8;i+)if(codestringi=str) /是某個關(guān)鍵字,返回對應(yīng)的編碼return codebooki;if(i=9) /不是關(guān)鍵字return -1;【LR.cpp】#include<iostream>#include<string>#include<cstdlib>#include"cifa.h"#include"stack_num.h"#include"stack_str.h"#inclu
27、de"table_action.h"#include"table_go.h"using namespace std;void process()int stepNum=1;int topStat;Stack_num statusSTK; /狀態(tài)棧 Stack_str symbolSTK; /符號棧Stack_num valueSTK; /值棧 WordType word;Table_action actionTAB; /行為表 Table_go goTAB; /轉(zhuǎn)向表cout<<"請輸入源程序,以#結(jié)束:"word=get
28、_w();/總控程序初始化操作 symbolSTK.push("#");statusSTK.push(0);valueSTK.push(0);cout<<"步驟t狀態(tài)棧t符號棧t值棧t當(dāng)前詞t動作t轉(zhuǎn)向"<<endl;/分析while(1)topStat=statusSTK.getTop(); /當(dāng)前狀態(tài)棧頂string act=actionTAB.getCell(topStat,word.code);/根據(jù)狀態(tài)棧頂和當(dāng)前單詞查到的動作/輸出cout<<stepNum+<<"t"stat
29、usSTK.outStack(); cout<<"t"symbolSTK.outStack(); cout<<"t"valueSTK.outStack(); cout<<"t"cout<<<<"t"/行為為“acc”,且當(dāng)前處理的單詞為#,且狀態(tài)棧里就兩個狀態(tài)/說明正常分析結(jié)束if(act="acc"&&="#"&&statusSTK.topNumber
30、()=1)cout<<act<<endl;cout<<"分析成功!"<<endl;cout<<"結(jié)果為:"<<valueSTK.getTop()<<endl;return;/讀到act表里標記為錯誤的單元格else if(act="")cout<<endl<<"不是文法的句子!"<<endl;cout<<"錯誤的位置為單詞"<<<&
31、lt;"附近。"return;/移進動作else if(act0='S')int newstat=atoi(act.substr(1).c_str();statusSTK.push(newstat);symbolSTK.push();if(word.code=30)/整數(shù),壓入其值valueSTK.push(atoi(.c_str();else /其他單詞,壓入0占位valueSTK.push(0);/輸出word=get_w();cout<<act<<endl;/規(guī)約動作else if(act0=
32、39;R')cout<<act<<'t'string sn=act.substr(1);/產(chǎn)生式編號/根據(jù)規(guī)約使用的產(chǎn)生式更新各個棧if(sn="1")/E->E+TstatusSTK.pop();statusSTK.pop();statusSTK.pop();symbolSTK.pop();symbolSTK.pop();symbolSTK.pop();symbolSTK.push("E");int right_digit=valueSTK.pop();valueSTK.pop();int left
33、_digit=valueSTK.pop();int new_value=left_digit+right_digit;valueSTK.push(new_value);else if(sn="2")/E->E-TstatusSTK.pop();statusSTK.pop();statusSTK.pop();symbolSTK.pop();symbolSTK.pop();symbolSTK.pop();symbolSTK.push("E");int right_digit=valueSTK.pop();valueSTK.pop();int left_
34、digit=valueSTK.pop();int new_value=left_digit-right_digit;valueSTK.push(new_value);else if(sn="4")/T->T*FstatusSTK.pop();statusSTK.pop();statusSTK.pop();symbolSTK.pop();symbolSTK.pop();symbolSTK.pop();symbolSTK.push("T");int right_digit=valueSTK.pop();valueSTK.pop();int left_digit=valueSTK.pop();int new_value=left_digit*right_digit;valueSTK.push(new_value);else if(sn="5")/T->T/FstatusSTK.pop();statusSTK.pop();statusSTK.pop();symbolSTK.pop();symbolS
溫馨提示
- 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)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 商業(yè)會議中的在線教育與技術(shù)支持研究
- 2022-2027年中國二手3C電商行業(yè)市場運行現(xiàn)狀及未來發(fā)展預(yù)測報告
- 成人教育招生管理制度及落實范文
- 數(shù)據(jù)平臺自我優(yōu)待行為的反壟斷規(guī)制研究
- 家園共育視角下農(nóng)村幼兒早期閱讀習(xí)慣培養(yǎng)的實踐研究
- 歷史題材影視作品在高中歷史教學(xué)中的應(yīng)用研究
- 信息溝通策略視角下公眾對再生水接受意愿的影響因素研究
- 蝦皮鰹魚骨粉高鈣薄脆餅干的工藝及品質(zhì)研究
- 基于Ni基陽極固體氧化物燃料電池抗積碳性能改進的研究
- GB/T 33084-2016大型合金結(jié)構(gòu)鋼鍛件技術(shù)條件
- GB/T 17587.3-1998滾珠絲杠副第3部分:驗收條件和驗收檢驗
- 半條被子(紅軍長征時期故事) PPT
- 安徽省A10聯(lián)盟2023年高一物理第二學(xué)期期末學(xué)業(yè)質(zhì)量監(jiān)測模擬試題(含答案解析)
- JP柜出廠檢驗記錄
- 《語言學(xué)綱要》學(xué)習(xí)指導(dǎo)書習(xí)題答案
- 硫酸分裝經(jīng)營企業(yè)風(fēng)險分級管控及隱患治理資料
- icao考試圖片題飛行員ICAO描述模板
- 鹽城市區(qū)第Ⅲ防洪區(qū)水環(huán)境綜合治理PPP項目Ⅱ標段“6·4”一般基坑坍塌事故調(diào)查報告
- 撥叉綜合課程設(shè)計
- 學(xué)校物業(yè)服務(wù)監(jiān)督及處罰辦法
評論
0/150
提交評論