編譯原理語(yǔ)法制導(dǎo)的翻譯過(guò)程(L屬性的翻譯方案)實(shí)現(xiàn)帶括號(hào)的整數(shù)加減乘除四則運(yùn)算JAVA實(shí)現(xiàn)_第1頁(yè)
編譯原理語(yǔ)法制導(dǎo)的翻譯過(guò)程(L屬性的翻譯方案)實(shí)現(xiàn)帶括號(hào)的整數(shù)加減乘除四則運(yùn)算JAVA實(shí)現(xiàn)_第2頁(yè)
編譯原理語(yǔ)法制導(dǎo)的翻譯過(guò)程(L屬性的翻譯方案)實(shí)現(xiàn)帶括號(hào)的整數(shù)加減乘除四則運(yùn)算JAVA實(shí)現(xiàn)_第3頁(yè)
編譯原理語(yǔ)法制導(dǎo)的翻譯過(guò)程(L屬性的翻譯方案)實(shí)現(xiàn)帶括號(hào)的整數(shù)加減乘除四則運(yùn)算JAVA實(shí)現(xiàn)_第4頁(yè)
編譯原理語(yǔ)法制導(dǎo)的翻譯過(guò)程(L屬性的翻譯方案)實(shí)現(xiàn)帶括號(hào)的整數(shù)加減乘除四則運(yùn)算JAVA實(shí)現(xiàn)_第5頁(yè)
已閱讀5頁(yè),還剩8頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

編譯原理語(yǔ)法制導(dǎo)的翻譯過(guò)程(L屬性的翻譯?案)實(shí)現(xiàn)帶括

號(hào)的整數(shù)加減乘除四則運(yùn)算JAVA實(shí)現(xiàn)

編譯技術(shù)第6次上機(jī)內(nèi)容

算術(shù)表達(dá)式的擴(kuò)充

1.實(shí)驗(yàn)?的

充分理解語(yǔ)義分析的?法及相關(guān)語(yǔ)義計(jì)算的執(zhí)?時(shí)機(jī)。

掌握LR分析表的設(shè)計(jì)?法和語(yǔ)義加?程序的擴(kuò)充。

2.實(shí)驗(yàn)要求

參照算術(shù)表達(dá)式LR分析表的設(shè)計(jì)?法,設(shè)計(jì)擴(kuò)充后的算術(shù)表達(dá)式LR分析表,并對(duì)原語(yǔ)義加?程序進(jìn)?修改,加?新添加的內(nèi)容。寫(xiě)?段程

序,打印出計(jì)算結(jié)果E。

3.實(shí)驗(yàn)內(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)??下?上計(jì)算時(shí),打印出四則運(yùn)算的計(jì)算結(jié)果。

E、T、F這些?終結(jié)符需要綜合屬性。以L屬性的翻譯?案為基礎(chǔ),將下表的語(yǔ)義規(guī)則嵌套在語(yǔ)法分析的過(guò)程中,即實(shí)現(xiàn)語(yǔ)法制導(dǎo)的翻譯過(guò)

程。

產(chǎn)?式

L?En

E?E1+T

E?E1-T

E?T

T?T1*F

T?T1/F

語(yǔ)義規(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.以詞法分析和語(yǔ)法分析部分的上機(jī)結(jié)果為基礎(chǔ),添加語(yǔ)義分析部分。即以LR?法為基礎(chǔ)。當(dāng)進(jìn)?產(chǎn)?式歸約時(shí)執(zhí)?對(duì)應(yīng)的語(yǔ)義動(dò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.對(duì)浮點(diǎn)數(shù)和科學(xué)計(jì)數(shù)法的表?也能完成上述的操作。

2.增加減法和除法(也可繼續(xù)擴(kuò)展其他運(yùn)算)對(duì)應(yīng)的產(chǎn)?式,并能計(jì)算其語(yǔ)義結(jié)果。

3.在ftp中第五次上機(jī)的?件夾中有兩個(gè)壓縮包,?遞歸下降法實(shí)現(xiàn)的程序在壓縮包"recursion_calculator.rar"中,??遞歸的預(yù)測(cè)

分析?法實(shí)現(xiàn)的程序在壓縮包"predict_calculator.rar"中。可以任選其?作為基礎(chǔ)進(jìn)?改進(jìn),增加減法和除法的操作,寫(xiě)出改進(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;//是否出錯(cuò)

privatestaticintlookahead=0;//當(dāng)前字符

privatestaticint[]yylex=newint[20];

privatestaticintyyval=0;

privatestaticintflag=0;//計(jì)數(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>();//棧

//這是?段記號(hào)的定義

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("請(qǐng)輸?串:");

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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論