📄 compiler.java
字号:
import com.ms.wfc.app.*;
import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;
import com.ms.wfc.html.*;
import java.util.Vector;
import java.util.Stack;
import java.lang.System;
import java.lang.Character;
import java.io.*;
/**
* This class can take a variable number of parameters on the command
* line. Program execution begins with the main() method. The class
* constructor is not invoked unless an object of type 'compiler' is
* created in the main() method.
*/
public class compiler extends Form
{
LRAnalyse lrAnalyse;
boolean isWordFinished=false;
//symbolTable symTable=new symbolTable();
public compiler()
{
// Required for Visual J++ Form Designer support
initForm();
// TODO: Add any constructor code after initForm call
}
/**
* compiler overrides dispose so it can clean up the
* component list.
*/
public void dispose()
{
super.dispose();
components.dispose();
}
private void btnWord_click(Object source, Event e)
{
try{
if(rEditSource.getText().length()==0)
{
MessageBox.show("请输入源程序!","提示");
return;
}
lrAnalyse=new LRAnalyse();
rEditError.setText("");
rEditSym.setText(" 符号表");
rEditToken.setText(" token串");
lrAnalyse.symTable.recognize(rEditSource.getText());
for(int i=0;i<lrAnalyse.symTable.symbol.size();i++)
{
rEditSym.setText(rEditSym.getText()+"\n"+(String)lrAnalyse.symTable.symbol.elementAt(i)+"\t"+(String)lrAnalyse.symTable.code.elementAt(i));
}
for(int i=0;i<lrAnalyse.symTable.tokenSym.size();i++)
{
rEditToken.setText(rEditToken.getText()+"\n"+(String)lrAnalyse.symTable.tokenSym.elementAt(i)+"\t"+(String)lrAnalyse.symTable.tokenCode.elementAt(i));
}
if(lrAnalyse.symTable.isError)
{
rEditError.setText("error: line "+Integer.toString( lrAnalyse.symTable.errorLine)+",column "+Integer.toString(lrAnalyse.symTable.errorColumn));
isWordFinished=false;
}
else
{
isWordFinished=true;
rEditError.setText("词法分析成功!");
}
}
catch(NullPointerException ex)
{
MessageBox.show(ex.toString());
}
}
private void btnGra_click(Object source, Event e)
{
if(isWordFinished)
{
lrAnalyse.LrAnalyse();
lrAnalyse.analyse(false,false);
rEditFormula.setText(" 产生式");
rEditGroup.setText(" 四元组");
for(int i=0;i<lrAnalyse.formulaVector.size();i++)
{
rEditFormula.setText(rEditFormula.getText()+"\n"+(String)lrAnalyse.formulaVector.elementAt(i));
}
for(int i=0;i<lrAnalyse.groupVector.size();i++)
{
rEditGroup.setText(rEditGroup.getText()+"\n"+((fourGroup)lrAnalyse.groupVector.elementAt(i)).label+":"+"("+((fourGroup)lrAnalyse.groupVector.elementAt(i)).operate+","+((fourGroup)lrAnalyse.groupVector.elementAt(i)).opFirst+","+((fourGroup)lrAnalyse.groupVector.elementAt(i)).opSecond+","+((fourGroup)lrAnalyse.groupVector.elementAt(i)).result+")");
}
if(lrAnalyse.isError)
{
rEditError.setText("error: line "+Integer.toString( lrAnalyse.errorLine)+",column "+Integer.toString(lrAnalyse.errorColumn));
}
else
{
rEditError.setText("语法及语义分析成功!");
}
}
else
{
MessageBox.show("请先进行词法分析,并确保词法正确!");
}
}
private void btnExit_click(Object source, Event e)
{
System.exit(1);
}
private void btnSave_click(Object source, Event e)
{
try{
String fileName=new String();
String stringTemp=new String();
int dlgResult =saveFile.showDialog();
if (dlgResult == DialogResult.OK)
{
fileName= saveFile.getFileName();
FileOutputStream outputFile=new FileOutputStream(fileName);
stringTemp=rEditGroup.getText();
byte[] byteTemp=stringTemp.getBytes();
outputFile.write(byteTemp);
}
}
catch(IOException ex)
{
MessageBox.show(ex.toString(),"异常",MessageBox.OK);
}
}
private void btnOpen_click(Object source, Event e)
{
try{
String strTemp=new String();
String fileName=new String();
int dlgResult =openFile.showDialog();
if (dlgResult == DialogResult.OK)
{
fileName= openFile.getFileName();
FileInputStream inputFile;
File newFile=new File(fileName);
BufferedReader bufReader;
inputFile=new FileInputStream(fileName);
bufReader=new BufferedReader( new InputStreamReader(inputFile),(int)newFile.length());
String strData;
while((strData=bufReader.readLine())!=null)
{
strTemp=strTemp+strData+"\n";
}
rEditSource.setText(strTemp);
}
}
catch(IOException ex)
{
MessageBox.show(ex.toString(),"异常",MessageBox.OK);
}
}
private void btnStep_click(Object source, Event e)
{
if(isWordFinished)
{
lrAnalyse.LrAnalyse();
lrAnalyse.analyse(true,true);
rEditFormula.setText(" 产生式");
rEditGroup.setText(" 四元组");
for(int i=0;i<lrAnalyse.formulaVector.size();i++)
{
rEditFormula.setText(rEditFormula.getText()+"\n"+(String)lrAnalyse.formulaVector.elementAt(i));
}
for(int i=0;i<lrAnalyse.groupVector.size();i++)
{
rEditGroup.setText(rEditGroup.getText()+"\n"+((fourGroup)lrAnalyse.groupVector.elementAt(i)).label+":"+"("+((fourGroup)lrAnalyse.groupVector.elementAt(i)).operate+","+((fourGroup)lrAnalyse.groupVector.elementAt(i)).opFirst+","+((fourGroup)lrAnalyse.groupVector.elementAt(i)).opSecond+","+((fourGroup)lrAnalyse.groupVector.elementAt(i)).result+")");
}
if(lrAnalyse.isError)
{
rEditError.setText("error: line "+Integer.toString( lrAnalyse.errorLine)+",column "+Integer.toString(lrAnalyse.errorColumn));
}
else
{
rEditError.setText("语法及语义单步分析成功!");
}
}
else
{
MessageBox.show("请先进行词法分析,并确保词法正确!");
}
}
private void btnHelp_click(Object source, Event e)
{
String strHelp=new String();
strHelp="1.点击'打开源程序',打开文件,或直接在文本框中写入源程序。\n2.点击'词法分析',得到符号表和token串。\n3.点击'语法及语义分析'或'语法及语义单步分析',得到四元组,建议先进行'语法及语义分析',然后再进行'语法及语义单步分析',便于观察分析过程。\n4.点击'保存四元组',保存四元组。\n5.报错窗口中的列号不计空格。\n6.随程序附带个测试文件,test1为无错源程序,test2有词法错误,test3有语法错误,test4有词法及语法错误";
MessageBox.show(strHelp,"帮助");
}
private void btnSaveSource_click(Object source, Event e)
{
try{
String fileName=new String();
String stringTemp=new String();
int dlgResult =saveFile.showDialog();
if (dlgResult == DialogResult.OK)
{
fileName= saveFile.getFileName();
FileOutputStream outputFile=new FileOutputStream(fileName);
stringTemp=rEditSource.getText();
byte[] byteTemp=stringTemp.getBytes();
outputFile.write(byteTemp);
}
}
catch(IOException ex)
{
MessageBox.show(ex.toString(),"异常",MessageBox.OK);
}
}
/**
* NOTE: The following code is required by the Visual J++ form
* designer. It can be modified using the form editor. Do not
* modify it using the code editor.
*/
Container components = new Container();
RichEdit rEditError = new RichEdit();
Button btnWord = new Button();
RichEdit rEditSource = new RichEdit();
RichEdit rEditSym = new RichEdit();
Button btnSave = new Button();
Button btnGra = new Button();
StatusBar staBar = new StatusBar();
Button btnExit = new Button();
RichEdit rEditToken = new RichEdit();
Button btnOpen = new Button();
RichEdit rEditFormula = new RichEdit();
RichEdit rEditGroup = new RichEdit();
OpenFileDialog openFile = new OpenFileDialog();
SaveFileDialog saveFile = new SaveFileDialog();
Button btnStep = new Button();
Button btnHelp = new Button();
Button btnSaveSource = new Button();
private void initForm()
{
// NOTE: This form is storing resource information in an
// external file. Do not modify the string parameter to any
// resources.getObject() function call. For example, do not
// modify "foo1_location" in the following line of code
// even if the name of the Foo object changes:
// foo1.setLocation((Point)resources.getObject("foo1_location"));
IResourceManager resources = new ResourceManager(this, "compiler");
this.setText("类Pascal编译器 [制作人:张博 1010311012]");
this.setAutoScaleBaseSize(new Point(6, 12));
this.setClientSize(new Point(892, 646));
this.setIcon((Icon)resources.getObject("this_icon"));
this.setMaximizeBox(false);
this.setStartPosition(FormStartPosition.CENTER_SCREEN);
rEditError.setFont(Font.DEFAULT_GUI);
rEditError.setForeColor(Color.WINDOWTEXT);
rEditError.setLocation(new Point(8, 592));
rEditError.setSize(new Point(392, 40));
rEditError.setTabIndex(13);
rEditError.setText("");
rEditError.setReadOnly(true);
btnWord.setLocation(new Point(248, 16));
btnWord.setSize(new Point(75, 23));
btnWord.setTabIndex(3);
btnWord.setText("词法分析");
btnWord.addOnClick(new EventHandler(this.btnWord_click));
rEditSource.setFont(Font.DEFAULT_GUI);
rEditSource.setForeColor(Color.WINDOWTEXT);
rEditSource.setLocation(new Point(8, 56));
rEditSource.setSize(new Point(392, 528));
rEditSource.setTabIndex(0);
rEditSource.setText("");
rEditSym.setFont(Font.DEFAULT_GUI);
rEditSym.setForeColor(Color.WINDOWTEXT);
rEditSym.setLocation(new Point(408, 56));
rEditSym.setSize(new Point(88, 576));
rEditSym.setTabIndex(9);
rEditSym.setText(" 符号表");
rEditSym.setReadOnly(true);
rEditSym.setScrollBars(RichEditScrollBars.VERTICAL);
btnSave.setLocation(new Point(560, 16));
btnSave.setSize(new Point(75, 23));
btnSave.setTabIndex(6);
btnSave.setText("保存四元组");
btnSave.addOnClick(new EventHandler(this.btnSave_click));
btnGra.setLocation(new Point(320, 16));
btnGra.setSize(new Point(120, 23));
btnGra.setTabIndex(4);
btnGra.setText("语法及语义分析");
btnGra.addOnClick(new EventHandler(this.btnGra_click));
staBar.setBackColor(Color.CONTROL);
staBar.setLocation(new Point(0, 646));
staBar.setSize(new Point(892, 0));
staBar.setTabIndex(14);
staBar.setText("");
btnExit.setLocation(new Point(704, 16));
btnExit.setSize(new Point(75, 23));
btnExit.setTabIndex(8);
btnExit.setText("退出");
btnExit.addOnClick(new EventHandler(this.btnExit_click));
rEditToken.setFont(Font.DEFAULT_GUI);
rEditToken.setForeColor(Color.WINDOWTEXT);
rEditToken.setLocation(new Point(504, 56));
rEditToken.setSize(new Point(88, 576));
rEditToken.setTabIndex(10);
rEditToken.setText(" token串");
rEditToken.setReadOnly(true);
rEditToken.setScrollBars(RichEditScrollBars.VERTICAL);
btnOpen.setLocation(new Point(104, 16));
btnOpen.setSize(new Point(75, 23));
btnOpen.setTabIndex(1);
btnOpen.setText("打开源程序");
btnOpen.addOnClick(new EventHandler(this.btnOpen_click));
rEditFormula.setFont(Font.DEFAULT_GUI);
rEditFormula.setForeColor(Color.WINDOWTEXT);
rEditFormula.setLocation(new Point(600, 56));
rEditFormula.setSize(new Point(128, 576));
rEditFormula.setTabIndex(11);
rEditFormula.setText(" 产生式");
rEditFormula.setReadOnly(true);
rEditFormula.setScrollBars(RichEditScrollBars.VERTICAL);
rEditGroup.setFont(Font.DEFAULT_GUI);
rEditGroup.setForeColor(Color.WINDOWTEXT);
rEditGroup.setLocation(new Point(736, 56));
rEditGroup.setSize(new Point(152, 576));
rEditGroup.setTabIndex(12);
rEditGroup.setText(" 四元组");
rEditGroup.setReadOnly(true);
rEditGroup.setScrollBars(RichEditScrollBars.VERTICAL);
/* @designTimeOnly openFile.setLocation(new Point(96, 496)); */
/* @designTimeOnly saveFile.setLocation(new Point(16, 496)); */
btnStep.setLocation(new Point(440, 16));
btnStep.setSize(new Point(120, 23));
btnStep.setTabIndex(5);
btnStep.setText("语法及语义单步分析");
btnStep.addOnClick(new EventHandler(this.btnStep_click));
btnHelp.setCursor(Cursor.HELP);
btnHelp.setLocation(new Point(632, 16));
btnHelp.setSize(new Point(75, 23));
btnHelp.setTabIndex(7);
btnHelp.setText("帮助");
btnHelp.addOnClick(new EventHandler(this.btnHelp_click));
btnSaveSource.setLocation(new Point(176, 16));
btnSaveSource.setSize(new Point(75, 23));
btnSaveSource.setTabIndex(2);
btnSaveSource.setText("保存源程序");
btnSaveSource.addOnClick(new EventHandler(this.btnSaveSource_click));
this.setNewControls(new Control[] {
btnSaveSource,
btnHelp,
btnStep,
rEditGroup,
rEditFormula,
btnOpen,
btnSave,
rEditToken,
btnExit,
staBar,
btnGra,
rEditSym,
rEditSource,
btnWord,
rEditError});
}
/**
* The main entry point for the application.
*
* @param args Array of parameters passed to the application
* via the command line.
*/
public static void main(String args[])
{
Application.run(new compiler());
}
}
class fourGroup
{
boolean notFinished; //===出口是否填完
int label; //===标号
String operate; //===操作码
String opFirst; //===第一操作数
String opSecond; //===第二操作数
String result; //===结果
public fourGroup()
{
notFinished=true;
opFirst=" ";
opSecond=" ";
result="#";
}
}
class attribution
{
int trueList; //===真出口
int falseList; //===假出口
int nextList;
String place;
public attribution()
{
trueList=0;
falseList=0;
nextList=0;
}
}
class LRAnalyse
{
LRTable lrTable; //===分析表
genFormula genFormu; //===产生式
symbolTable symTable; //===符号表
Stack symStack; //===分析栈(非终结符)
Stack staStack; //===分析栈(状态号)
Stack attriStack; //===属性栈
Vector groupVector; //===四元组
Vector formulaVector; //===产生式
int newtempLabel; //===标识临时变量下标
int newLabel; //===标识四元组下标
int errorLine; //===出错行号
int errorColumn; //===出错列号
boolean isError; //===是否有错
boolean isStep; //===是否单步
public LRAnalyse()
{
isStep=false;
formulaVector=new Vector();
isError=false;
errorLine=0;
errorColumn=0;
lrTable=new LRTable();
genFormu=new genFormula();
symTable=new symbolTable();
symStack=new Stack();
staStack=new Stack();
staStack.push("0");
symStack.push("$");
newtempLabel=1;
newLabel=100;
attriStack=new Stack();
groupVector=new Vector();
}
//===初始化除词法外的信息
void LrAnalyse()
{
isStep=false;
formulaVector=new Vector();
isError=false;
errorLine=0;
errorColumn=0;
genFormu=new genFormula();
symStack=new Stack();
staStack=new Stack();
staStack.push("0");
symStack.push("$");
newtempLabel=1;
newLabel=100;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -