欢迎来到天天文库
浏览记录
ID:47466745
大小:71.50 KB
页数:5页
时间:2020-01-11
《编译语言-中间代码生成》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验四中间代码生成一、实验内容设计一个程序,该程序能够将形如x=yopz的简单赋值语句翻译为对应的四元式序列,其中op可为+、-、*、/等二元运算符。(可参考实验指导书P163至P164)。要求用JAVA语言编程。例如:若输入赋值语句a=b+c,则输出如下四元式序列。二、程序代码InToPost.JAVApackagezlf;importjava.io.IOException;publicclassInToPost{privateStacktheStack;privateStringinput;privateStringoutpu
2、t="";publicInToPost(Stringin){input=in;intstackSize=input.length();theStack=newStack(stackSize);}publicStringdoTrans(){for(intj=0;j3、eStack.push(ch);break;case')':gotParen(ch);break;default:output=output+ch;break;}}while(!theStack.isEmpty()){output=output+theStack.pop();}//System.out.println(output);returnoutput;}publicvoidgotOper(charopThis,intprec1){while(!theStack.isEmpty()){charopTop=theStack.p4、op();if(opTop=='('){theStack.push(opTop);break;}else{intprec2;if(opTop=='+'5、6、opTop=='-')prec2=1;elseprec2=2;if(prec27、ack.pop();if(chx=='(')break;elseoutput=output+chx;}}classStack{privateintmaxSize;privatechar[]stackArray;privateinttop;publicStack(intmax){maxSize=max;stackArray=newchar[maxSize];top=-1;}publicvoidpush(charj){stackArray[++top]=j;}publiccharpop(){returnstackArray[top--8、];}publiccharpeek(){returnstackArray[top];}publicbooleanisEmpty(){return(top==-1);}}}QuaternaryTypeOfAlgorithm.JAVApackagezlf;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.util.Stack;publicclassQuaternaryTypeOfAlgorithmextendsJFrame{privatesta9、ticStacknumber=newStack();privateintn=1;privateJButtonjbt=newJButton("生成");privateJTextAreajta=newJTextArea();privateJTextFieldjtf=newJTextField("A=B+C*D+E",20);privatebooleanhasError=false;publicQuaternaryTypeOfAlgorithm(){JPanelp1=newJPanel();p1.add(10、jtf);p1.add(jbt);add(p1,BorderLayout.NORTH);add(newJScrollPane(jta),BorderLayout.CENTER);jbt.addActionListener(newActionList
3、eStack.push(ch);break;case')':gotParen(ch);break;default:output=output+ch;break;}}while(!theStack.isEmpty()){output=output+theStack.pop();}//System.out.println(output);returnoutput;}publicvoidgotOper(charopThis,intprec1){while(!theStack.isEmpty()){charopTop=theStack.p
4、op();if(opTop=='('){theStack.push(opTop);break;}else{intprec2;if(opTop=='+'
5、
6、opTop=='-')prec2=1;elseprec2=2;if(prec27、ack.pop();if(chx=='(')break;elseoutput=output+chx;}}classStack{privateintmaxSize;privatechar[]stackArray;privateinttop;publicStack(intmax){maxSize=max;stackArray=newchar[maxSize];top=-1;}publicvoidpush(charj){stackArray[++top]=j;}publiccharpop(){returnstackArray[top--8、];}publiccharpeek(){returnstackArray[top];}publicbooleanisEmpty(){return(top==-1);}}}QuaternaryTypeOfAlgorithm.JAVApackagezlf;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.util.Stack;publicclassQuaternaryTypeOfAlgorithmextendsJFrame{privatesta9、ticStacknumber=newStack();privateintn=1;privateJButtonjbt=newJButton("生成");privateJTextAreajta=newJTextArea();privateJTextFieldjtf=newJTextField("A=B+C*D+E",20);privatebooleanhasError=false;publicQuaternaryTypeOfAlgorithm(){JPanelp1=newJPanel();p1.add(10、jtf);p1.add(jbt);add(p1,BorderLayout.NORTH);add(newJScrollPane(jta),BorderLayout.CENTER);jbt.addActionListener(newActionList
7、ack.pop();if(chx=='(')break;elseoutput=output+chx;}}classStack{privateintmaxSize;privatechar[]stackArray;privateinttop;publicStack(intmax){maxSize=max;stackArray=newchar[maxSize];top=-1;}publicvoidpush(charj){stackArray[++top]=j;}publiccharpop(){returnstackArray[top--
8、];}publiccharpeek(){returnstackArray[top];}publicbooleanisEmpty(){return(top==-1);}}}QuaternaryTypeOfAlgorithm.JAVApackagezlf;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.util.Stack;publicclassQuaternaryTypeOfAlgorithmextendsJFrame{privatesta
9、ticStacknumber=newStack();privateintn=1;privateJButtonjbt=newJButton("生成");privateJTextAreajta=newJTextArea();privateJTextFieldjtf=newJTextField("A=B+C*D+E",20);privatebooleanhasError=false;publicQuaternaryTypeOfAlgorithm(){JPanelp1=newJPanel();p1.add(
10、jtf);p1.add(jbt);add(p1,BorderLayout.NORTH);add(newJScrollPane(jta),BorderLayout.CENTER);jbt.addActionListener(newActionList
此文档下载收益归作者所有