java词法分析器.docx

上传人:b****6 文档编号:7581937 上传时间:2023-01-25 格式:DOCX 页数:30 大小:22.27KB
下载 相关 举报
java词法分析器.docx_第1页
第1页 / 共30页
java词法分析器.docx_第2页
第2页 / 共30页
java词法分析器.docx_第3页
第3页 / 共30页
java词法分析器.docx_第4页
第4页 / 共30页
java词法分析器.docx_第5页
第5页 / 共30页
点击查看更多>>
下载资源
资源描述

java词法分析器.docx

《java词法分析器.docx》由会员分享,可在线阅读,更多相关《java词法分析器.docx(30页珍藏版)》请在冰豆网上搜索。

java词法分析器.docx

java词法分析器

packageJAccidenceAnalyse;

importjava.io.*;

importjava.util.*;

importJAccidenceAnalyse.Buffer.*;

publicclassAccidenceAnalyser{

privatejava.io.FileSourceFile;

privatejava.io.FileReserveFile;

privatejava.io.FileClassFile;

privatejava.io.FileOutputFile;

publicPretreatmentpretreatment;

publicKeyWordTablekeyWordTable;

publicClassIdentityclassIdentity;

publicScanerscaner;

publicConcreteScanBufferFactorycsbFactory;

/**

*2)词法分析器主程序

*@roseuid3D9BB93303D0

*/

publicAccidenceAnalyser(){

System.out.println("[INFOR]已经建立词法分析器!

");

}

/**

*@roseuid3D9BAEF9029F

*/

publicvoidinitAA(){

//创建缓冲工厂

this.csbFactory=newConcreteScanBufferFactory();

//创建字符串扫描对象

scaner=newScaner(this);

//创建pre处理对象

pretreatment=newPretreatment(SourceFile,this);

//创建关键字表对象

keyWordTable=newKeyWordTable(ReserveFile);

//创建对象种别码表对象

classIdentity=newClassIdentity(ClassFile);

System.out.println("[INFOR]已经初始化词法分析器!

");

}

/**

*@roseuid3D9BAF12022D

*/

publicvoidsetFilesPath(StringreserveFileName,StringClassFileName,

StringsourceFileName,StringoutputFileName){

//创建文件对象

SourceFile=newjava.io.File(sourceFileName);

//创建文件对象

ReserveFile=newjava.io.File(reserveFileName);

//创建文件对象

ClassFile=newjava.io.File(ClassFileName);

//创建文件对象

OutputFile=newjava.io.File(outputFileName);

//如果文件已经存在,先删除,然后建立新文件

if(OutputFile.exists()){

OutputFile.delete();

}

try{

OutputFile.createNewFile();

}

catch(Exceptione){

e.printStackTrace(System.err);

}

try{

//创建文件随机读取对象

java.io.RandomAccessFileROutputFile=newjava.io.RandomAccessFile(this.

OutputFile,"rw");

//提示信息

ROutputFile.write("//////////////////////////////////////////////////\n".

getBytes());

ROutputFile.write(("//JAccidenceAnalyserversion"+getVersion()+

"designbyyellowicq//\n").getBytes());

ROutputFile.write("//java词法分析器//////////////\n".getBytes());

ROutputFile.write("//使用java语言开发////////////////////////////////////\n".

getBytes());

ROutputFile.write("//////////////////////////////////////////////////\n".

getBytes());

ROutputFile.write("词法分析结果如下:

\n".getBytes());

//关闭文件流

ROutputFile.close();

}

catch(Exceptione){

e.printStackTrace(System.err);

}

}

/**

*@roseuid3D9BAFAB0089

*/

publicvoidstartAA(){

//从预处理开始词法分析

this.pretreatment.startPretreatment();

}

/**

*@roseuid3D9BB0B40383

*/

publicvoidoutputAccidence(StringoutputString){

//把分析出来的单词写入文件

outputString="\n[第"+this.pretreatment.fileRow+"行]\n"+outputString;

try{

//创建文件随机读取对象

java.io.RandomAccessFileROutputFile=newjava.io.RandomAccessFile(this.

OutputFile,"rw");

//移动指针到文件末尾

ROutputFile.seek(ROutputFile.length());

//Startappending!

ROutputFile.write(outputString.getBytes());

//关闭文件流

ROutputFile.close();

}

catch(Exceptione){

e.printStackTrace(System.err);

}

//将分析的单词结果输出到终端

System.out.print(outputString);

}

/**

*@roseuid3D9BB0CE02C2

*/

publicvoidcontrolThread(){

//控制扫描器启动扫描

scaner.controlThread();

}

//获得版本号

publicStringgetVersion(){

return"1.0";

}

}

 

packageJAccidenceAnalyse;

importjava.util.*;

importjava.io.*;

publicclassClassIdentity{

privateHashtableClassHash;

privateFileClassFile;

privateFileReaderclassFileReader;//读文件对象

privateintTMP_BUFFER_SIZE=30;

/**

*6)类型种别码程序

*@roseuid3D9BB9390108

*/

publicClassIdentity(java.io.FileClassFile){

System.out.println("[INFOR]类型种别码表已创建!

");

this.ClassFile=ClassFile;

}

/**

*@roseuid3D9BB0B40383

*/

//查找类型种别码

publicintfindKey(StringclassWord){

intKEY;

for(Enumeratione=this.ClassHash.keys();e.hasMoreElements();){

KEY=Integer.parseInt((String)e.nextElement());

if(((String)this.ClassHash.get(Integer.toString(KEY))).

equalsIgnoreCase(classWord)){

returnKEY;

}

}

return-1;

}

/**

*@roseuid3D9BAE7303D3

*/

publicvoidinitClassIdentityTable(){

ClassHash=newHashtable();//创建hash表

intintLength;

char[]chrBuffer=newchar[TMP_BUFFER_SIZE];

StringclassWord;

intclassCounter=0;

try{

if(ClassFile.exists()){//文件存在

//创建读文件对象

classFileReader=newjava.io.FileReader(ClassFile);

//读文件内容到hash表

while((intLength=classFileReader.read(chrBuffer))!

=-1){

classCounter++;

//填写hash表

classWord=String.valueOf(chrBuffer).trim();

System.out.println("[INFOR]读取类型种别码:

[KEY:

"+classCounter+

"][VALUE:

"+classWord+"]");

this.ClassHash.put(Integer.toString(classCounter),classWord);

}

//关闭读文件对象

classFileReader.close();

}

else{//文件不存在

System.err.println("[ERROR]类型种别码文件不存在!

");

}

}

catch(Exceptione){

e.printStackTrace(System.err);

}

}

}

 

packageJAccidenceAnalyse;

importjava.util.*;

importjava.io.*;

publicclassKeyWordTable{

privateHashtableKWHash;

privateFileReserveFile;

privateFileReaderresFileReader;//读文件对象

privateintTMP_BUFFER_SIZE=30;

/**

*5)表留字表程序

*@roseuid3D9BB9390108

*/

publicKeyWordTable(java.io.FileReserveFile){

System.out.println("[INFOR]关键字表已创建!

");

this.ReserveFile=ReserveFile;

}

/**

*@paraminw

*@returnboolean

*@roseuid3D9BAE4702AD

*/

publicbooleanisKeyWord(Stringinw){

StringresWord;

//查找hash表

for(Enumeratione=this.KWHash.elements();e.hasMoreElements();){

resWord=(String)e.nextElement();

if(resWord.equalsIgnoreCase(inw)){

returntrue;

}

}

returnfalse;

}

/**

*@roseuid3D9BAE7303D3

*/

publicvoidinitKeyWordTable(){

KWHash=newHashtable();//创建hash表

intintLength;

char[]chrBuffer=newchar[TMP_BUFFER_SIZE];

StringresWord;

intresCounter=0;

try{

if(ReserveFile.exists()){//文件存在

//创建读文件对象

resFileReader=newjava.io.FileReader(ReserveFile);

//读文件内容到hash表

while((intLength=resFileReader.read(chrBuffer))!

=-1){

resCounter++;

//填写hash表

resWord=String.valueOf(chrBuffer).trim();

System.out.println("[INFOR]读取关键字:

[INDEX:

"+resCounter+

"][VALUE:

"+resWord+"]");

this.KWHash.put(Integer.toString(resCounter),resWord);

}

//关闭读文件对象

resFileReader.close();

}

else{//文件不存在

System.err.println("[ERROR]保留字文件不存在!

");

}

}

catch(Exceptione){

e.printStackTrace(System.err);

}

}

}

 

packageJAccidenceAnalyse;

importjavax.xml.parsers.*;

importorg.w3c.dom.*;

publicclassmain{

/**

*1)词法分析器引导文件

*@paramargs

*@returnvoid

*@roseuid3D9BAE4702AD

*/

publicstaticvoidmain(String[]args){

//读取配置文件,得到系统属性

StringcfgString[]=newString[4];

try{

cfgString=main.loadAACfg("d:

\\aaCfg.xml");

}

catch(Exceptione){

e.printStackTrace(System.err);

}

//设置待读文件名

////////////////////////////////////////////////////

//保留字表文件

StringreserveFileName=cfgString[0];

//类型种别码表文件

StringclassFileName=cfgString[1];

//需要分析的源文件

StringsourceFileName=cfgString[2];

//输出文件

StringoutputFileName=cfgString[3];

////////////////////////////////////////////////////

//创建词法分析器

AccidenceAnalyseraa=newAccidenceAnalyser();

aa.setFilesPath(reserveFileName,classFileName,sourceFileName,

outputFileName);//建立所需要的文件对象

//初始化词法分析器

aa.initAA();

//初始化关键字表

aa.keyWordTable.initKeyWordTable();

//初始化类型种别码表

aa.classIdentity.initClassIdentityTable();

//开始进行词法分析

aa.startAA();

//分析完毕

}

//读取配置文件

privatestaticString[]loadAACfg(Stringname)throwsException{

StringcfgString[]=newString[4];

/*解析xml配置文件*/

try{

/*创建文档工厂*/

DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();

/*创建文档解析器*/

DocumentBuilderbuilder=factory.newDocumentBuilder();

/*解析配置文件*/

Documentdoc=builder.parse(name);

/*规范化文档*/

doc.normalize();

/*查找接点表*/

NodeListnlists=doc.getElementsByTagName("FilePath");

for(inti=0;i

Elementitem=(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(Exceptione){

e.printStackTrace();

thrownewException("[ERROR]加载配置文件"+name+"错误!

");

}

//返回属性数组

returncfgString;

}

}

 

packageJAccidenceAnalyse;

importJAccidenceAnalyse.Buffer.*;

importjava.io.*;

publicclassPretreatment{

privateStringtmpString;

privateStringoutputString;

privateintBUFFER_SIZE=100;

privateAccidenceAnalyseraa;

publicInputBufferinputBuffer;//输入缓冲区--共享

privatejava.io.FileSourceFile;//文件对象

privatejava.io.RandomAccessFilerandomAFile;//随机文件对象

publicstaticintfileRow=0;

/**

*3)预处理子程序

*@roseuid3DAB7C530399

*/

publicPretreatment(FileSourceFile,AccidenceAnalyseraa){

try{

this.SourceFile=SourceFile;

this.randomAFile=newjava.io.RandomAccessFile(this.SourceFile,"r");

}

catch(FileNotFoundExceptione){

e.printStackTrace(System.err);

}

this.aa=aa;

inputBuffer=aa.csbFactory.createInputBuffer(BUFFER_SIZE);

System.out.println("[INFOR]预处理器已经创建!

");

}

/**

*@roseuid3D9BAFE20331

*/

publicvoidputSourceToINBuffer(StringtmpString){

this.inputBuffer.Data=tmpString.toCharArray();

}

/**

*@roseuid3D9BB0400169

*/

publicvoidputFinToSCBuffer(StringfiltratedString){

aa.scaner.scanBuffer.Data=filtratedString.toCharArray();

}

/**

*@roseuid3D9BB05E00A4

*/

publicvoidcontrolThread(){

intintLength;

intresCounter=0;

StringtmpString;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 生产经营管理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1