版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
編譯原理語法制導(dǎo)的翻譯過程(L屬性的翻譯?案)實現(xiàn)帶括
號的整數(shù)加減乘除四則運算JAVA實現(xiàn)
編譯技術(shù)第6次上機(jī)內(nèi)容
算術(shù)表達(dá)式的擴(kuò)充
1.實驗?的
充分理解語義分析的?法及相關(guān)語義計算的執(zhí)?時機(jī)。
掌握LR分析表的設(shè)計?法和語義加?程序的擴(kuò)充。
2.實驗要求
參照算術(shù)表達(dá)式LR分析表的設(shè)計?法,設(shè)計擴(kuò)充后的算術(shù)表達(dá)式LR分析表,并對原語義加?程序進(jìn)?修改,加?新添加的內(nèi)容。寫?段程
序,打印出計算結(jié)果E。
3.實驗內(nèi)容
假設(shè)有以下?法:
L->En
E->E+T
E->E-T
E->T
T->T*F
T->T/F
T->F
F->(E)
F->id
設(shè)該?法進(jìn)??下?上計算時,打印出四則運算的計算結(jié)果。
E、T、F這些?終結(jié)符需要綜合屬性。以L屬性的翻譯?案為基礎(chǔ),將下表的語義規(guī)則嵌套在語法分析的過程中,即實現(xiàn)語法制導(dǎo)的翻譯過
程。
產(chǎn)?式
L?En
E?E1+T
E?E1-T
E?T
T?T1*F
T?T1/F
語義規(guī)則
{print(E.val)}
{E.val=E1.val+T.val}
{E.val=E1.val-T.val}
{E.val=T.val}
{T.val=T1.val*F.val}
{T.val=T1.val/F.val}
T?F
F?(E)
{T.val=F.val}
{F.val=E.val}
F?id
{F.val=id.lexval}
2.以詞法分析和語法分析部分的上機(jī)結(jié)果為基礎(chǔ),添加語義分析部分。即以LR?法為基礎(chǔ)。當(dāng)進(jìn)?產(chǎn)?式歸約時執(zhí)?對應(yīng)的語義動作。
3.輸?:
5+3+8*2
輸出:24
輸?10-6/2
輸出:7
4.若輸?有誤,如:3++2
則應(yīng)提?:重新輸?!
5.由于輸?串是具體的數(shù)值,因此應(yīng)調(diào)?相應(yīng)的詞法分析的功能。
擴(kuò)展:
1.對浮點數(shù)和科學(xué)計數(shù)法的表?也能完成上述的操作。
2.增加減法和除法(也可繼續(xù)擴(kuò)展其他運算)對應(yīng)的產(chǎn)?式,并能計算其語義結(jié)果。
3.在ftp中第五次上機(jī)的?件夾中有兩個壓縮包,?遞歸下降法實現(xiàn)的程序在壓縮包"recursion_calculator.rar"中,??遞歸的預(yù)測
分析?法實現(xiàn)的程序在壓縮包"predict_calculator.rar"中??梢匀芜x其?作為基礎(chǔ)進(jìn)?改進(jìn),增加減法和除法的操作,寫出改進(jìn)后
的?法,輸出表達(dá)式的結(jié)果。
importjava.util.Scanner;
importjava.util.Stack;
//E->E+T1
//E->E-T2
//E->T3
//T->T*F4
//T->T/F5
//T->F6
//F->(E)7
//F->id8
publicclassMain{
privatestaticbooleanguiyuefou=false;
privatestaticbooleanguiyuefou=false;
privatestaticStringStr=null;//輸?串
privatestaticStringSub=null;
privatestaticbooleanacc=false;//是否已處理完輸?串
privatestaticbooleanbResult=false;//是否出錯
privatestaticintlookahead=0;//當(dāng)前字符
privatestaticint[]yylex=newint[20];
privatestaticintyyval=0;
privatestaticintflag=0;//計數(shù)
privatestaticint[][]Goto=newint[][]{{1,3,2},{0,0,0},{0,0,0},{0,0,0},{7,3,9},
{0,0,0},{0,3,12},{0,0,0},{0,15,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0,0},
{0,14,0},{0,0,0},{0,0,0}};//Goto表
privatestaticStack<Integer>stack1=newStack<Integer>();
privatestaticStack<String>stack=newStack<String>();//棧
//這是?段記號的定義
publicstaticintADD=0;//+
publicstaticintMUL=1;//*
publicstaticintSUB=2;//-
publicstaticintDIV=3;//\
publicstaticintLBRACE=4;//(
publicstaticintRBRACE=5;//)
publicstaticintNUM=6;//number
publicstaticintEND=7;//#
publicstaticintOTHER=8;//other
publicstaticintnextToken(){
inti=0;
inty=0;
while(Sub.charAt(i)=='')
i++;
y=i;
while(Sub.charAt(y)>='0'&&Sub.charAt(y)<='9'){
y++;
}
if(y!=i){
yylex[flag++]=Integer.parseInt(Sub.substring(i,y));
Sub=Sub.substring(y,Sub.length());
returnNUM;
}else{
switch(Sub.charAt(i)){
case'+':
Sub=Sub.substring(1,Sub.length());
returnADD;
case'-':
Sub=Sub.substring(1,Sub.length());
returnSUB;
case'*':
Sub=Sub.substring(1,Sub.length());
returnMUL;
case'/':
Sub=Sub.substring(1,Sub.length());
returnDIV;
case'#':
Sub=Sub.substring(1,Sub.length());
returnEND;
case'(':
Sub=Sub.substring(1,Sub.length());
returnLBRACE;
case')':
Sub=Sub.substring(1,Sub.length());
returnRBRACE;
default:
Sub=Sub.substring(1,Sub.length());
returnOTHER;
}
}
}
}
publicstaticvoidGto(intstate,charfzjf){
inti=-1;
if(fzjf=='E')
i=0;
if(fzjf=='F')
i=1;
if(fzjf=='T')
i=2;
stack.push(fzjf+"");
stack.push(Goto[state][i]+"");
}
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
System.out.println("請輸?串:");
Scannerin=newScanner(System.in);
Str=in.nextLine();
Sub=Str;
in.close();
//0狀態(tài)先進(jìn)棧
stack.push(String.valueOf(0));
inti=0;
while(!bResult&&acc==false){
if(!guiyuefou){
lookahead=nextToken();
}
if(lookahead==NUM){
yyval=yylex[i++];
}
switch(stack.peek().charAt(0)){
case'0':
if(lookahead==NUM){
stack.push("id");
stack.push(""+5);
guiyuefou=false;
}elseif(lookahead==LBRACE){
stack.push("(");
stack.push(""+4);
guiyuefou=false;
}else{
System.out.println("error!");
bResult=true;
}
break;
case'1':
if("1".equals(stack.peek())){
if(lookahead==ADD){
stack.push("+");
stack.push(""+6);
guiyuefou=false;
}elseif(lookahead==SUB){
stack.push("-");
stack.push(""+16);
guiyuefou=false;
}elseif(lookahead==END){
acc=true;
acc=true;
System.out.println("接受!");
}else{
System.out.println("error!");
bResult=true;
}
break;
}elseif(stack.peek().charAt(1)=='0'){
if(lookahead==RBRACE){
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','F');
}elseif(lookahead==END){
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','F');
}elseif(lookahead==ADD){
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
stack.pop();
stack.pop();
stack.pop();
Gto((int)stack.peek().charAt(0)-(int)'0','F');
}elseif(lookahead==SUB){
stack.pop();
stack.pop();
guiyuefou=true;
stack.pop();
stack.pop();
stack.pop();
stack.pop();
Gto((int)stack.peek().charAt(0)-(int)'0','F');
}elseif(lookahead==DIV){
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','F');
}elseif(lookahead==MUL){
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','F');
}else{
System.out.println("error");
System.out.println("error");
bResult=true;
}
break;
}elseif(stack.peek().charAt(1)=='2'){
if(lookahead==RBRACE){
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','E');
inty=stack1.pop().intValue();
intx=stack1.pop().intValue();
intz=x+y;
stack1.push(z);
}elseif(lookahead==END){
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
stack.pop();
stack.pop();
stack.pop();
Gto((int)stack.peek().charAt(0)-(int)'0','E');
inty=stack1.pop().intValue();
intx=stack1.pop().intValue();
intz=x+y;
stack1.push(z);
}elseif(lookahead==ADD){
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
stack.pop();
stack.pop();
stack.pop();
Gto((int)stack.peek().charAt(0)-(int)'0','E');
inty=stack1.pop().intValue();
intx=stack1.pop().intValue();
intz=x+y;
stack1.push(z);
}elseif(lookahead==SUB){
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
stack.pop();
Gto((int)stack.peek().charAt(0)-(int)'0','E');
inty=stack1.pop().intValue();
intx=stack1.pop().intValue();
intz=x+y;
stack1.push(z);
}elseif(lookahead==DIV){
stack.push("/");
stack.push(13+"");
guiyuefou=false;
}elseif(lookahead==MUL){
stack.push("*");
stack.push(""+8);
guiyuefou=false;
}else{
System.out.println("error");
bResult=true;
}
break;
}elseif(stack.peek().charAt(1)=='3'){
if(lookahead==NUM){
stack.push("id");
stack.push(""+5);
guiyuefou=false;
}elseif(lookahead==LBRACE){
stack.push(")");
guiyuefou=false;
stack.push(""+4);
}else{
System.out.println("error");
bResult=true;
}
}elseif(stack.peek().charAt(1)=='4'){
if(lookahead==RBRACE){
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
stack.pop();
Gto((int)stack.peek().charAt(0)-(int)'0','T');
inty=stack1.pop().intValue();
intx=stack1.pop().intValue();
intz=x/y;
stack1.push(z);
}elseif(lookahead==END){
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','T');
inty=stack1.pop().intValue();
intx=stack1.pop().intValue();
intz=x/y;
stack1.push(z);
}elseif(lookahead==ADD){
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','T');
inty=stack1.pop().intValue();
intx=stack1.pop().intValue();
intz=x/y;
stack1.push(z);
}elseif(lookahead==SUB){
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','T');
inty=stack1.pop().intValue();
intx=stack1.pop().intValue();
intz=x/y;
stack1.push(z);
}elseif(lookahead==DIV){
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','T');
inty=stack1.pop().intValue();
intx=stack1.pop().intValue();
intz=x/y;
stack1.push(z);
}elseif(lookahead==MUL){
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
stack.pop();
Gto((int)stack.peek().charAt(0)-(int)'0','T');
inty=stack1.pop().intValue();
intx=stack1.pop().intValue();
intz=x/y;
stack1.push(z);
}else{
System.out.println("error");
bResult=true;
}
break;
}elseif(stack.peek().charAt(1)=='5'){
if(lookahead==RBRACE){
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','T');
inty=stack1.pop().intValue();
intx=stack1.pop().intValue();
intz=x*y;
stack1.push(z);
}elseif(lookahead==END){
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
stack.pop();
Gto((int)stack.peek().charAt(0)-(int)'0','T');
inty=stack1.pop().intValue();
intx=stack1.pop().intValue();
intz=x*y;
stack1.push(z);
}elseif(lookahead==ADD){
}elseif(lookahead==ADD){
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','T');
inty=stack1.pop().intValue();
intx=stack1.pop().intValue();
intz=x*y;
stack1.push(z);
}elseif(lookahead==SUB){
stack.pop();
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
stack.pop();
stack.pop();
Gto((int)stack.peek().charAt(0)-(int)'0','T');
inty=stack1.pop().intValue();
intx=stack1.pop().intValue();
intz=x*y;
stack1.push(z);
}elseif(lookahead==DIV){
stack.pop();
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
stack.pop();
stack.pop();
Gto((int)stack.peek().charAt(0)-(int)'0','T');
inty=stack1.pop().intValue();
intx=stack1.pop().intValue();
intz=x*y;
stack1.push(z);
}elseif(lookahead==MUL){
stack.pop();
stack.pop();
guiyuefou=true;
stack.pop();
stack.pop();
stack.pop();
stack.pop();
Gto((int)stack.peek().charAt(0)-(int)'0','T');
inty=stack1.pop().intValue();
intx=stack1.pop().intValue();
intz=x*y;
stack1.push(z);
}else{
System.out.println("error");
bResult=true;
}
break;
}
case'2':
if(lookahead==MUL){
stack.push("*");
stack.push(""+8);
guiyuefou=false;
}elseif(lookahead==RBRACE){
stack.pop();
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','E');
}elseif(lookahead==END){
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','E');
}elseif(lookahead==ADD){
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','E');
}elseif(lookahead==SUB){
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','E');
}elseif(lookahead==DIV){
stack.push("/");
stack.push(13+"");
guiyuefou=false;
}else{
System.out.println("error");
bResult=true;
}
break;
case'3':
if(lookahead==END){
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','T');
}elseif(lookahead==ADD){
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','T');
}elseif(lookahead==SUB){
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','T');
}elseif(lookahead==DIV){
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','T');
}elseif(lookahead==RBRACE){
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','T');
}elseif(lookahead==MUL){
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','T');
}else{
System.out.println("error");
bResult=true;
}
break;
case'4':
if(lookahead==NUM){
stack.push("id");
stack.push(""+5);
guiyuefou=false;
}elseif(lookahead==LBRACE){
stack.push("(");
stack.push(""+4);
guiyuefou=false;
}else{
System.out.println("error");
bResult=true;
}
break;
case'5':
if(lookahead==RBRACE){
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','F');
stack1.push(yyval);
}elseif(lookahead==END){
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','F');
stack1.push(yyval);
}elseif(lookahead==ADD){
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','F');
stack1.push(yyval);
}elseif(lookahead==SUB){
stack.pop();
guiyuefou=true;
stack.pop();
Gto((int)stack.peek().charAt(0)-(int)'0','F');
stack1.push(yyval);
}elseif(lookahead==DIV){
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','F');
stack1.push(yyval);
}elseif(lookahead==MUL){
stack.pop();
stack.pop();
guiyuefou=true;
Gto((int)stack.peek().charAt(0)-(int)'0','F');
stack1.push(yyval);
}else{
System.out.println("error");
bResult=true;
}
break;
case'6':
if(lookahead==NUM){
stack.push("id");
stack.push(""+5);
guiyuefou=false;
}elseif(lookahead==LBRACE){
stack.push("(");
stack.push(""+4);
guiyuefou=false;
}else{
System.out.println("error");
bResult=true;
}
break;
case'7':
if(lookahead==RBRACE){
stack.push(")"
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 廣東省農(nóng)村信用社聯(lián)合社2026校園招聘考試重點試題及答案解析
- 2025河南花花牛乳業(yè)集團(tuán)招聘15人考試重點試題及答案解析
- 2025年直播帶貨主播五年直播效果與數(shù)據(jù)優(yōu)化報告
- 2025年合肥工業(yè)大學(xué)招標(biāo)與采購管理中心專業(yè)技術(shù)人員招聘備考筆試試題及答案解析
- 2025年濟(jì)南事業(yè)單位公開招聘129人備考題庫及1套完整答案詳解
- 上海七十邁數(shù)字科技2026校園招聘備考題庫及一套參考答案詳解
- 2025年揚(yáng)州市江都婦幼保健院公開招聘編外合同制專業(yè)技術(shù)人員備考題庫及一套答案詳解
- 2025年貴陽產(chǎn)控安居投資運營有限公司社會招聘備考題庫有答案詳解
- 2025廣東廣州民間金融街管理委員會招聘輔助人員1人筆試重點試題及答案解析
- 北滘鎮(zhèn)第三幼兒園2025年招聘備考題庫參考答案詳解
- 地鐵保安考試題庫及答案
- 2025佛山農(nóng)商銀行社會招聘考試備考題庫及答案解析
- 中醫(yī)基礎(chǔ)學(xué)考試題(附答案)
- 六分鐘步行試驗臨床規(guī)范應(yīng)用中國專家共識解讀
- 鍋莊舞教學(xué)課件
- 混合性認(rèn)知障礙診治專家共識解讀課件
- 統(tǒng)編版語文二年級上冊 語文園地七教學(xué)課件
- 醫(yī)院保密教育培訓(xùn)課件
- 2026年高考語文復(fù)習(xí):文言文背誦篇目理解性默寫練習(xí)題匯編(含答案)
- 母嬰專科護(hù)士拓展匯報
- 2025年衛(wèi)健系統(tǒng)安全生產(chǎn)工作總結(jié)
評論
0/150
提交評論