?? 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(" 產(chǎn)生式");
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("請先進(jìn)行詞法分析,并確保詞法正確!");
}
}
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(" 產(chǎn)生式");
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("請先進(jìn)行詞法分析,并確保詞法正確!");
}
}
private void btnHelp_click(Object source, Event e)
{
String strHelp=new String();
strHelp="1.點擊'打開源程序',打開文件,或直接在文本框中寫入源程序。\n2.點擊'詞法分析',得到符號表和token串。\n3.點擊'語法及語義分析'或'語法及語義單步分析',得到四元組,建議先進(jìn)行'語法及語義分析',然后再進(jìn)行'語法及語義單步分析',便于觀察分析過程。\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(" 產(chǎn)生式");
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; //===標(biāo)號
String operate; //===操作碼
String opFirst; //===第一操作數(shù)
String opSecond; //===第二操作數(shù)
String result; //===結(jié)果
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; //===產(chǎn)生式
symbolTable symTable; //===符號表
Stack symStack; //===分析棧(非終結(jié)符)
Stack staStack; //===分析棧(狀態(tài)號)
Stack attriStack; //===屬性棧
Vector groupVector; //===四元組
Vector formulaVector; //===產(chǎn)生式
int newtempLabel; //===標(biāo)識臨時變量下標(biāo)
int newLabel; //===標(biāo)識四元組下標(biāo)
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;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -