编译语言-中间代码生成

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

实验四中间代码生成一、实验内容设计一个程序,该程序能够将形如x=yopz的简单赋值语句翻译为对应的四元式序列,其中op可为+、-、*、/等二元运算符。(可参考实验指导书P163至P164)。要求用JAVA语言编程。例如:若输入赋值语句a=b+c,则输出如下四元式序列。二、程序代码InToPost.JAVApackagezlf;importjava.io.IOException;publicclassInToPost{privateStacktheStack;privateStringinput;privateStringoutput=;publicInToPost(Stringin){input=in;intstackSize=input.length();theStack=newStack(stackSize);}publicStringdoTrans(){for(intj=0;jinput.length();j++){charch=input.charAt(j);switch(ch){case'+':case'-':gotOper(ch,1);break;case'*':case'/':gotOper(ch,2);break;case'(':theStack.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.pop();if(opTop=='('){theStack.push(opTop);break;}else{intprec2;if(opTop=='+'||opTop=='-')prec2=1;elseprec2=2;if(prec2prec1){theStack.push(opTop);break;}elseoutput=output+opTop;}}theStack.push(opThis);}publicvoidgotParen(charch){while(!theStack.isEmpty()){charchx=theStack.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--];}publiccharpeek(){returnstackArray[top];}publicbooleanisEmpty(){return(top==-1);}}}QuaternaryTypeOfAlgorithm.JAVApackagezlf;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.util.Stack;publicclassQuaternaryTypeOfAlgorithmextendsJFrame{privatestaticStackStringnumber=newStackString();privateintn=1;privateJButtonjbt=newJButton(生成);privateJTextAreajta=newJTextArea();privateJTextFieldjtf=newJTextField(A=B+C*D+E,20);privatebooleanhasError=false;publicQuaternaryTypeOfAlgorithm(){JPanelp1=newJPanel();p1.add(jtf);p1.add(jbt);add(p1,BorderLayout.NORTH);add(newJScrollPane(jta),BorderLayout.CENTER);jbt.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Stringequation=jtf.getText().trim();create(equation);number.clear();hasError=false;repaint();}});}publicvoidcreate(Stringinput){Stringoutput;InToPosttheTrans=newInToPost(input);output=theTrans.doTrans();try{for(inti=0;ioutput.length();i++){Stringt=output.substring(i,i+1);if(t.equals(+)){Stringn1=number.pop();Stringn2=number.pop();Stringn3=T+n;n++;number.push(n3);jta.append(n-1+:(+,+n2+,+n1+,+n3+)+'\n');}elseif(t.equals(-)){Stringn1=number.pop();Stringn3=T+n;n++;number.push(n3);jta.append(n-1+:(-,+n1+,++,+n3+)+'\n');}elseif(t.equals(*)){Stringn1=number.pop();Stringn2=number.pop();Stringn3=T+n;n++;number.push(n3);jta.append(n-1+:(*,+n2+,+n1+,+n3+)+'\n');}elseif(t.equals(/)){Stringn1=number.pop();Stringn2=number.pop();Stringn3=T+n;n++;number.push(n3);jta.append(n-1+:(/,+n2+,+n1+,+n3+)+'\n');}elseif(t.matches([a-zA-Z0-9=])){number.push(t);//System.out.println(number:+t);}}Stringlast2=number.pop();number.pop();Stringlast1=number.pop();n++;jta.append(n-1+:(=,+last2+,,+last1+)+'\n'+'\n'+'\n');}catch(Exceptione){hasError=true;}if(hasError||!number.empty()){JOptionPane.showMessageDialog(null,你输入的算式表达式有错!请重新输入);number.clear();}}publicstaticvoidmain(String[]args){QuaternaryTypeOfAlgorithmq=newQuaternaryTypeOfAlgorithm();q.setSize(400,200);q.setTitle(生成四元式序列);q.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);q.setVisible(true);q.setLocationRelativeTo(null);}}三、实验结果

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功