版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
上海電力學(xué)院編譯原理課程實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)名稱(chēng):實(shí)驗(yàn)三自下而上語(yǔ)法分析及語(yǔ)義分析院系:計(jì)算機(jī)科學(xué)與技術(shù)學(xué)院專(zhuān)業(yè)年級(jí):學(xué)生姓名:學(xué)號(hào):指導(dǎo)老師:實(shí)驗(yàn)日期:實(shí)驗(yàn)三自上而下的語(yǔ)法分析一、實(shí)驗(yàn)?zāi)康模航?jīng)過(guò)本實(shí)驗(yàn)掌握LR分析器的結(jié)構(gòu)過(guò)程,并依據(jù)語(yǔ)法制導(dǎo)翻譯,掌握屬性文法的自下而上計(jì)算的過(guò)程。二、實(shí)驗(yàn)學(xué)時(shí):4學(xué)時(shí)。三、實(shí)驗(yàn)內(nèi)容依據(jù)給出的簡(jiǎn)單表達(dá)式的語(yǔ)法組成規(guī)則(見(jiàn)五),編制LR分析程序,要求能對(duì)用給定的語(yǔ)法例則書(shū)寫(xiě)的源程序進(jìn)行語(yǔ)法分析和語(yǔ)義分析。關(guān)于正確的表達(dá)式,給出表達(dá)式的值。關(guān)于錯(cuò)誤的表達(dá)式,給出犯錯(cuò)地點(diǎn)。四、實(shí)驗(yàn)方法采納LR分析法。第一給出S-屬性文法的定義(為簡(jiǎn)單起見(jiàn),每個(gè)文法符號(hào)只設(shè)置一個(gè)綜合屬性,即該文法符號(hào)所代表的表達(dá)式的值。屬性文法的定義可參照書(shū)137頁(yè)表6.1),并將其改造成用LR分析實(shí)現(xiàn)時(shí)的語(yǔ)義分析動(dòng)作(可參照書(shū)145頁(yè)表6.5)。接下來(lái)給出LR分析表。此后程序的詳細(xì)實(shí)現(xiàn):LR分析表可用二維數(shù)組(或其余)實(shí)現(xiàn)。增添一個(gè)val棧作為語(yǔ)義分析實(shí)現(xiàn)的工具。編寫(xiě)總控程序,實(shí)現(xiàn)語(yǔ)法分析和語(yǔ)義分析的過(guò)程。注:關(guān)于整數(shù)的鑒識(shí)能夠借助實(shí)驗(yàn)1。五、文法定義簡(jiǎn)單的表達(dá)式文法以下:(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->i狀態(tài)ACTION(動(dòng)作)GOTO(變換)i+-*/()#ETF0S5S41231S6S12acc2R3R3S7S13R3R33R6R6R6R6R6R64S5S48235R8R8R8R8R8R86S5S4937S5S4108S6R12S119R1R1S7S13R1R110R4R4R4R4R4R411R7R7R7R7R7R712S5S414313S5S41514R2R2S7S13R2R215R5R5R5R5R5R5五、辦理程序例和辦理結(jié)果例示例1:2*(+3191)+3191#六、源代碼cifa.h】//cifa.h#include<string>usingnamespacestd;//單詞結(jié)構(gòu)定義structWordType{intcode;stringpro;};//函數(shù)申明WordTypeget_w();voidgetch();voidgetBC();boolisLetter();boolisDigit();voidretract();intReserve(stringstr);stringconcat(stringstr);Table.action.h】//table_action.hclassTable_action{introw_num,line_num;intlineName[8];stringtableData[16][8];public:Table_action(){row_num=16;line_num=8;lineName[0]=30;lineName[1]=7;lineName[2]=13;lineName[3]=8;lineName[4]=14;lineName[5]=1;lineName[6]=2;lineName[7]=15;lineName[8]=0;for(intm=0;m<row_num;m++)for(intn=0;n<line_num;n++)tableData[m][n]="";tableData[0][0]="S5";tableData[0][5]="S4";tableData[1][1]="S6";tableData[1][2]="S12";tableData[1][7]="acc";tableData[2][1]="R3";tableData[2][2]="R3";tableData[2][3]="S7";tableData[2][4]="S13";tableData[2][6]="R3";tableData[2][7]="R3";tableData[3][1]="R6";tableData[3][2]="R6";tableData[3][3]="R6";tableData[3][4]="R6";tableData[3][6]="R6";tableData[3][7]="R6";tableData[4][0]="S5";tableData[4][5]="S4";tableData[5][1]="R8";tableData[5][2]="R8";tableData[5][3]="R8";tableData[5][4]="R8";tableData[5][6]="R8";tableData[5][7]="R8";tableData[6][0]="S5";tableData[6][5]="S4";tableData[7][0]="S5";tableData[7][5]="S4";tableData[8][1]="S6";tableData[8][2]="S12";tableData[8][6]="S11";tableData[9][1]="R1";tableData[9][2]="R1";tableData[9][3]="S7";tableData[9][4]="S13";tableData[9][6]="R1";tableData[9][7]="R1";tableData[10][1]="R4";tableData[10][2]="R4";tableData[10][3]="R4";tableData[10][4]="R4";tableData[10][6]="R4";tableData[10][7]="R4";tableData[11][1]="R7";tableData[11][2]="R7";tableData[11][3]="R7";tableData[11][4]="R7";tableData[11][6]="R7";tableData[11][7]="R7";tableData[12][0]="S5";tableData[12][5]="S4";tableData[13][0]="S5";tableData[13][5]="S4";tableData[14][1]="R2";tableData[14][2]="R2";tableData[14][3]="S7";tableData[14][4]="S13";tableData[14][6]="R2";tableData[14][7]="R2";tableData[15][1]="R5";tableData[15][2]="R5";tableData[15][3]="R5";tableData[15][4]="R5";tableData[15][5]="R5";tableData[15][6]="R5";tableData[15][7]="R5";}stringgetCell(introwN,intlineN){introw=rowN;intline=getLineNumber(lineN);if(row>=0&&row<row_num&&line>=0&&line<=line_num)returntableData[row][line];elsereturn"";}intgetLineNumber(intlineN){for(inti=0;i<line_num;i++)if(lineName[i]==lineN)returni;return-1;}};Table_go.h】//table_go.hclassTable_go{introw_num,line_num;//行數(shù)、列數(shù)stringlineName[3];inttableData[16][3];public:Table_go(){row_num=16;line_num=3;lineName[0]="E";lineName[1]="T";lineName[2]="F";for(intm=0;m<row_num;m++)for(intn=0;n<line_num;n++)tableData[m][n]=0;tableData[0][0]=1;tableData[0][1]=2;tableData[0][2]=3;tableData[4][0]=8;tableData[4][1]=2;tableData[4][2]=3;tableData[6][1]=9;tableData[6][2]=3;tableData[7][2]=10;tableData[12][1]=14;tableData[12][2]=3;tableData[13][2]=15;}intgetCell(introwN,stringlineNa){introw=rowN;intline=getLineNumber(lineNa);if(row>=0&&row<row_num&&line<=line_num)returntableData[row][line];elsereturn-1;}intgetLineNumber(stringlineNa){for(inti=0;i<line_num;i++)if(lineName[i]==lineNa)returni;return-1;}};Stack_num.h】classStack_num{inti;
//棧頂標(biāo)記int*data;
//棧結(jié)構(gòu)public:Stack_num()
//結(jié)構(gòu)函數(shù){data=newint[100];i=-1;}intpush(intm)//進(jìn)棧操作{i++;data[i]=m;returni;}intpop()//出棧操作{i--;returndata[i+1];}intgetTop()//返回棧頂{returndata[i];}~Stack_num()//析構(gòu)函數(shù){delete[]data;}inttopNumber(){returni;}voidoutStack(){for(intm=0;m<=i;m++)cout<<data[m];}};【Stack_str.h】classStack_str{inti;string*data;
//棧頂標(biāo)記//棧結(jié)構(gòu)public:Stack_str()
//結(jié)構(gòu)函數(shù){data=newstring[50];i=-1;}intpush(stringm)//進(jìn)棧操作{i++;data[i]=m;returni;}intpop()//出棧操作{data[i]="";i--;returni;}stringgetTop()//返回棧頂{returndata[i];}~Stack_str()//析構(gòu)函數(shù){delete[]data;}inttopNumber(){returni;}voidoutStack(){for(intm=0;m<=i;m++)cout<<data[m];}};cifa.cpp】//cifa.cpp#include<iostream>#include<string>#include"cifa.h"usingnamespacestd;//重點(diǎn)字表和對(duì)應(yīng)的編碼stringcodestring[10]={"main","int","if","then","else","return","void","cout","endl"};intcodebook[10]={26,21,22,23,24,25,27,28,29};//全局變量charch;intflag=0;/*//主函數(shù)intmain(){WordTypeword;cout<<"請(qǐng)輸入源程序序列:";word=get_w();while(!="#")//#為自己設(shè)置的結(jié)束標(biāo)記{cout<<"("<<word.code<<","<<"“"<<<<"”"<<")"<<endl;word=get_w();};return0;}*/WordTypeget_w(){stringstr="";intcode;WordTypewordtmp;getch();//讀一個(gè)字符getBC();//去掉空白符if(isLetter()){//以字母開(kāi)頭while(isLetter()||isDigit()){str=concat(str);getch();}retract();code=Reserve(str);if(code==-1){wordtmp.code=0;=str;}//不是重點(diǎn)字else{wordtmp.code=code;=str;}//是重點(diǎn)字}elseif(isDigit()){//以數(shù)字開(kāi)頭while(isDigit()){str=concat(str);getch();}retract();wordtmp.code=30;=str;}elseif(ch=='('){wordtmp.code=1;="(";}elseif(ch==')'){wordtmp.code=2;=")";}elseif(ch=='{'){wordtmp.code=3;="{";}elseif(ch=='}'){wordtmp.code=4;="}";}elseif(ch==';'){wordtmp.code=5;=";";}elseif(ch=='='){wordtmp.code=6;="=";}elseif(ch=='+'){wordtmp.code=7;="+";}elseif(ch=='*'){wordtmp.code=8;="*";}elseif(ch=='>'){wordtmp.code=9;=">";}elseif(ch=='<'){wordtmp.code=10;="<";}elseif(ch==','){wordtmp.code=11;=",";}elseif(ch=='\''){wordtmp.code=12;="\'";}elseif(ch=='-'){wordtmp.code=13;="-";}elseif(ch=='/'){wordtmp.code=14;="/";}elseif(ch=='#'){wordtmp.code=15;="#";}elseif(ch=='|'){wordtmp.code=16;="|";}else{wordtmp.code=100;=ch;}returnwordtmp;}voidgetch(){if(flag==0)//沒(méi)有回退的字符ch=getchar();else//有回退字符,用回退字符,并設(shè)置標(biāo)記flag=0;}voidgetBC(){while(ch==''||ch=='\t'||ch=='\n')ch=getchar();}boolisLetter(){if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')returntrue;elsereturnfalse;}boolisDigit(){if(ch>='0'&&ch<='9')returntrue;elsereturnfalse;}stringconcat(stringstr){returnstr+ch;}voidretract(){flag=1;}intReserve(stringstr){inti;for(i=0;i<=8;i++){if(codestring[i]==str)//是某個(gè)重點(diǎn)字,返回對(duì)應(yīng)的編碼returncodebook[i];}if(i==9)//不是重點(diǎn)字return-1;}LR.cpp】#include<iostream>#include<string>#include<cstdlib>#include"cifa.h"#include"stack_num.h"#include"stack_str.h"#include"table_action.h"#include"table_go.h"usingnamespacestd;voidprocess(){intstepNum=1;inttopStat;Stack_numstatusSTK;Stack_strsymbolSTK;
//狀態(tài)棧//符號(hào)棧Stack_numvalueSTK;
//值棧WordTypeword;Table_actionactionTAB;Table_gogoTAB;
//行為表//轉(zhuǎn)向表cout<<"請(qǐng)輸入源程序,以#結(jié)束:";word=get_w();//總控程序初始化操作symbolSTK.push("#");statusSTK.push(0);valueSTK.push(0);cout<<"步驟\t狀態(tài)棧\t符號(hào)棧\t值棧\t目前詞\t動(dòng)作\t轉(zhuǎn)向"<<endl;//分析while(1){topStat=statusSTK.getTop();//目前狀態(tài)棧頂stringact=actionTAB.getCell(topStat,word.code);//依據(jù)狀態(tài)棧頂和目前單詞查到的動(dòng)作//輸出cout<<stepNum++<<"\t";statusSTK.outStack();cout<<"\t";symbolSTK.outStack();cout<<"\t";valueSTK.outStack();cout<<"\t";cout<<<<"\t";//行為為“acc”,且目前辦理的單詞為#,且狀態(tài)棧里就兩個(gè)狀態(tài)//說(shuō)明正常分析結(jié)束if(act=="acc"&&=="#"&&statusSTK.topNumber()==1){cout<<act<<endl;cout<<"分析成功!"<<endl;cout<<"結(jié)果為:"<<valueSTK.getTop()<<endl;return;}//讀到act表里標(biāo)記為錯(cuò)誤的單元格elseif(act==""){cout<<endl<<"不是文法的句子!"<<endl;cout<<"錯(cuò)誤的地點(diǎn)為單詞"<<<<"周邊。";return;}//移進(jìn)動(dòng)作elseif(act[0]=='S'){intnewstat=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ī)約動(dòng)作elseif(act[0]=='R'){cout<<act<<'\t';stringsn=act.substr(1);//產(chǎn)生式編號(hào)//依據(jù)規(guī)約使用的產(chǎn)生式更新各個(gè)棧if(sn=="1")//E->E+T{statusSTK.pop();statusSTK.pop();statusSTK.pop();symbolSTK.pop();symbolSTK.pop();symbolSTK.pop();symbolSTK.push("E");intright_digit=valueSTK.pop();valueSTK.pop();intleft_digit=valueSTK.pop();intnew_value=left_digit+right_digit;valueSTK.push(new_value);}elseif(sn=="2")//E->E-T{statusSTK.pop();statusSTK.pop();statusSTK.pop();symbolSTK.pop();symbolSTK.pop();symbolSTK.pop();symbolSTK.push("E");intright_digit=valueSTK.pop();valueSTK.pop();intleft_digit=valueSTK.pop();intnew_value=left_digit-right_digit;valueSTK.push(new_value);}elseif(sn=="4")//T->T*F{statusSTK.pop();statusSTK.pop();statusSTK.pop();symbolSTK.pop();symbolSTK.pop();symbolSTK.pop();symbolSTK.push("T");intright_digit=valueSTK.pop();valueSTK.pop();intleft_digit=valueSTK.pop();intnew_value=left_digit*right_digit;valueSTK.push(new_value);}elseif(sn=="5")//T->T/F{statusSTK.pop();statusSTK.pop();statusSTK.pop();symbolSTK.pop();symbolSTK.pop();symbo
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 品管經(jīng)理述職報(bào)告
- 肺部感染護(hù)理查房指南
- 《GBT 34341-2017 組織水足跡評(píng)價(jià)和報(bào)告指南》專(zhuān)題研究報(bào)告
- 2026年青海建筑職業(yè)技術(shù)學(xué)院?jiǎn)握新殬I(yè)適應(yīng)性測(cè)試題庫(kù)及參考答案詳解1套
- 新能源汽車(chē)充電樁信息運(yùn)維服務(wù)合同
- 智能手環(huán)維修技師(高級(jí))考試試卷及答案
- 珠寶設(shè)計(jì)行業(yè)珠寶設(shè)計(jì)項(xiàng)目經(jīng)理崗位招聘考試試卷及答案
- 物業(yè)公司年度個(gè)人工作總結(jié)報(bào)告2025(3篇)
- 2025年公共衛(wèi)生的試題及答案
- 2025年化學(xué)單質(zhì):碳項(xiàng)目發(fā)展計(jì)劃
- 2025中國(guó)融通資產(chǎn)管理集團(tuán)有限公司招聘筆試備考試題(230人)附答案解析
- 人工智能行業(yè)-“人工智能+”行動(dòng)深度解讀與產(chǎn)業(yè)發(fā)展機(jī)遇
- 2025棗莊市生態(tài)環(huán)境修復(fù)礦區(qū)復(fù)墾政策實(shí)施效果與國(guó)土空間規(guī)劃
- (一診)達(dá)州市2026屆高三第一次診斷性測(cè)試思想政治試題(含標(biāo)準(zhǔn)答案)
- 購(gòu)車(chē)意向金合同范本
- 2025廣東廣電網(wǎng)絡(luò)校園招聘筆試歷年參考題庫(kù)附帶答案詳解
- 江蘇大學(xué)《無(wú)機(jī)與分析化學(xué)實(shí)驗(yàn)B》2025-2026學(xué)年第一學(xué)期期末試卷
- 2025GINA全球哮喘處理和預(yù)防策略(更新版)解讀課件
- 2025年中國(guó)職場(chǎng)人心理健康調(diào)查研究報(bào)告
- 2025四川成都東方廣益投資有限公司下屬企業(yè)招聘9人備考題庫(kù)及完整答案詳解1套
- 2025~2026學(xué)年山東省德州市高二上學(xué)期九校聯(lián)考英語(yǔ)試卷
評(píng)論
0/150
提交評(píng)論