?? accidenceanalyser.txt
字號:
Java詞法分析器
[使用java開發,并且用來分析java源文件]
1. 開發工具:rational rose2002 jedition,borland jbuilder6 professional
2. 開發步驟:
3. 源代碼:
//lisence head
/*Java Accidence Analyser
**Author yellowicq
**All copyright reserved
**Version 1.0
*/
//lisence
1) 詞法分析器引導文件:main.java
package JAccidenceAnalyse;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class main {
/**
* @param args
* @return void
* @roseuid 3D9BAE4702AD
*/
public static void main(String[] args) {
//讀取配置文件,得到系統屬性
String cfgString[] = new String[4];
try {
cfgString = main.loadAACfg("d:\\aaCfg.xml");
}
catch (Exception e) {
e.printStackTrace(System.err);
}
//設置待讀文件名
////////////////////////////////////////////////////
//保留字表文件
String reserveFileName = cfgString[0];
//類型種別碼表文件
String classFileName = cfgString[1];
//需要分析的源文件
String sourceFileName = cfgString[2];
//輸出文件
String outputFileName = cfgString[3];
////////////////////////////////////////////////////
//創建詞法分析器
AccidenceAnalyser aa = new AccidenceAnalyser();
aa.setFilesPath(reserveFileName, classFileName, sourceFileName,
outputFileName); //建立所需要的文件對象
//初始化詞法分析器
aa.initAA();
//初始化關鍵字表
aa.keyWordTable.initKeyWordTable();
//初始化類型種別碼表
aa.classIdentity.initClassIdentityTable();
//開始進行詞法分析
aa.startAA();
//分析完畢
}
//讀取配置文件
private static String[] loadAACfg(String name) throws Exception {
String cfgString[] = new String[4];
/*解析xml配置文件*/
try {
/*創建文檔工廠*/
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
/*創建文檔解析器*/
DocumentBuilder builder = factory.newDocumentBuilder();
/*解析配置文件*/
Document doc = builder.parse(name);
/*規范化文檔*/
doc.normalize();
/*查找接點表*/
NodeList nlists = doc.getElementsByTagName("FilePath");
for (int i = 0; i < nlists.getLength(); i++) {
Element item = (Element) nlists.item(i);
//取得需要的配置屬性
/******************/
cfgString[0] = item.getElementsByTagName("ReserveFileName").item(0).
getFirstChild().getNodeValue().trim();
/******************/
cfgString[1] = item.getElementsByTagName("ClassFileName").item(0).
getFirstChild().getNodeValue().trim();
/******************/
cfgString[2] = item.getElementsByTagName("SourceFileName").item(0).
getFirstChild().getNodeValue().trim();
/******************/
cfgString[3] = item.getElementsByTagName("OutputFileName").item(0).
getFirstChild().getNodeValue().trim();
/******************/
}
}
catch (Exception e) {
e.printStackTrace();
throw new Exception("[ERROR]加載配置文件 " + name + " 錯誤!");
}
//返回屬性數組
return cfgString;
}
}
2) 詞法分析器主程序:AccidenceAnalyser.java
//Source file: d:\\JAccidenceAnalyse\\AccidenceAnalyser.java
package JAccidenceAnalyse;
import java.io.*;
import java.util.*;
import JAccidenceAnalyse.Buffer.*;
public class AccidenceAnalyser {
private java.io.File SourceFile;
private java.io.File ReserveFile;
private java.io.File ClassFile;
private java.io.File OutputFile;
public Pretreatment pretreatment;
public KeyWordTable keyWordTable;
public ClassIdentity classIdentity;
public Scaner scaner;
public ConcreteScanBufferFactory csbFactory;
/**
* @roseuid 3D9BB93303D0
*/
public AccidenceAnalyser() {
System.out.println("[INFOR]已經建立詞法分析器!");
}
/**
* @roseuid 3D9BAEF9029F
*/
public void initAA() {
//創建緩沖工廠
this.csbFactory = new ConcreteScanBufferFactory();
//創建字符串掃描對象
scaner = new Scaner(this);
//創建pre處理對象
pretreatment = new Pretreatment(SourceFile, this);
//創建關鍵字表對象
keyWordTable = new KeyWordTable(ReserveFile);
//創建對象種別碼表對象
classIdentity = new ClassIdentity(ClassFile);
System.out.println("[INFOR]已經初始化詞法分析器!");
}
/**
* @roseuid 3D9BAF12022D
*/
public void setFilesPath(String reserveFileName, String ClassFileName,
String sourceFileName, String outputFileName) {
//創建文件對象
SourceFile = new java.io.File(sourceFileName);
//創建文件對象
ReserveFile = new java.io.File(reserveFileName);
//創建文件對象
ClassFile = new java.io.File(ClassFileName);
//創建文件對象
OutputFile = new java.io.File(outputFileName);
//如果文件已經存在,先刪除,然后建立新文件
if (OutputFile.exists()) {
OutputFile.delete();
}
try {
OutputFile.createNewFile();
}
catch (Exception e) {
e.printStackTrace(System.err);
}
try {
//創建文件隨機讀取對象
java.io.RandomAccessFile ROutputFile = new java.io.RandomAccessFile(this.
OutputFile, "rw");
//提示信息
ROutputFile.write("//////////////////////////////////////////////////\n".
getBytes());
ROutputFile.write( ("//JAccidenceAnalyser version " + getVersion() +
" design by yellowicq//\n").getBytes());
ROutputFile.write("//java詞法分析器//////////////\n".getBytes());
ROutputFile.write("//使用java語言開發////////////////////////////////////\n".
getBytes());
ROutputFile.write("//////////////////////////////////////////////////\n".
getBytes());
ROutputFile.write("詞法分析結果如下:\n".getBytes());
//關閉文件流
ROutputFile.close();
}
catch (Exception e) {
e.printStackTrace(System.err);
}
}
/**
* @roseuid 3D9BAFAB0089
*/
public void startAA() {
//從預處理開始詞法分析
this.pretreatment.startPretreatment();
}
/**
* @roseuid 3D9BB0B40383
*/
public void outputAccidence(String outputString) {
//把分析出來的單詞寫入文件
outputString = "\n[第" + this.pretreatment.fileRow + "行]\n" + outputString;
try {
//創建文件隨機讀取對象
java.io.RandomAccessFile ROutputFile = new java.io.RandomAccessFile(this.
OutputFile, "rw");
//移動指針到文件末尾
ROutputFile.seek(ROutputFile.length());
//Start appending!
ROutputFile.write(outputString.getBytes());
//關閉文件流
ROutputFile.close();
}
catch (Exception e) {
e.printStackTrace(System.err);
}
//將分析的單詞結果輸出到終端
System.out.print(outputString);
}
/**
* @roseuid 3D9BB0CE02C2
*/
public void controlThread() {
//控制掃描器啟動掃描
scaner.controlThread();
}
//獲得版本號
public String getVersion() {
return "1.0";
}
}
3) 預處理子程序:Pretreatment.java
//Source file: d:\\JAccidenceAnalyse\\Pretreatment.java
package JAccidenceAnalyse;
import JAccidenceAnalyse.Buffer.*;
import java.io.*;
public class Pretreatment {
private String tmpString;
private String outputString;
private int BUFFER_SIZE = 100;
private AccidenceAnalyser aa;
public InputBuffer inputBuffer; //輸入緩沖區--共享
private java.io.File SourceFile; //文件對象
private java.io.RandomAccessFile randomAFile; //隨機文件對象
public static int fileRow = 0;
/**
* @roseuid 3DAB7C530399
*/
public Pretreatment(File SourceFile, AccidenceAnalyser aa) {
try {
this.SourceFile = SourceFile;
this.randomAFile = new java.io.RandomAccessFile(this.SourceFile, "r");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -