📄 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 + -