📄 java词法分析器_ - java - java_开发文档.htm
字号:
cfgString[0] =
item.getElementsByTagName("ReserveFileName").item(0).<BR>
getFirstChild().getNodeValue().trim();<BR>
/******************/<BR>
cfgString[1] =
item.getElementsByTagName("ClassFileName").item(0).<BR>
getFirstChild().getNodeValue().trim();<BR>
/******************/<BR>
cfgString[2] =
item.getElementsByTagName("SourceFileName").item(0).<BR>
getFirstChild().getNodeValue().trim();<BR>
/******************/<BR>
cfgString[3] =
item.getElementsByTagName("OutputFileName").item(0).<BR>
getFirstChild().getNodeValue().trim();<BR>
/******************/<BR>
}<BR> }<BR> catch (Exception e)
{<BR>
e.printStackTrace();<BR> throw new
Exception("[ERROR]加载配置文件 " + name + " 错误!");<BR>
}<BR> //返回属性数组<BR> return
cfgString;<BR> }</P>
<P>}<BR>2) 词法分析器主程序:AccidenceAnalyser.java<BR>//Source file:
d:\\JAccidenceAnalyse\\AccidenceAnalyser.java</P>
<P>package JAccidenceAnalyse;</P>
<P>import java.io.*;<BR>import java.util.*;<BR>import
JAccidenceAnalyse.Buffer.*;</P>
<P>public class AccidenceAnalyser {<BR> private java.io.File
SourceFile;<BR> private java.io.File ReserveFile;<BR> private
java.io.File ClassFile;<BR> private java.io.File
OutputFile;<BR> public Pretreatment pretreatment;<BR> public
KeyWordTable keyWordTable;<BR> public ClassIdentity
classIdentity;<BR> public Scaner scaner;<BR> public
ConcreteScanBufferFactory csbFactory;</P>
<P> /**<BR> * @roseuid 3D9BB93303D0<BR>
*/<BR> public AccidenceAnalyser() {<BR>
System.out.println("[INFOR]已经建立词法分析器!");<BR> }</P>
<P> /**<BR> * @roseuid 3D9BAEF9029F<BR>
*/<BR> public void initAA() {<BR>
//创建缓冲工厂<BR> this.csbFactory = new
ConcreteScanBufferFactory();<BR>
//创建字符串扫描对象<BR> scaner = new
Scaner(this);<BR> //创建pre处理对象<BR>
pretreatment = new Pretreatment(SourceFile, this);<BR>
//创建关键字表对象<BR> keyWordTable = new
KeyWordTable(ReserveFile);<BR>
//创建对象种别码表对象<BR> classIdentity = new
ClassIdentity(ClassFile);<BR>
System.out.println("[INFOR]已经初始化词法分析器!");<BR> }</P>
<P> /**<BR> * @roseuid 3D9BAF12022D<BR>
*/<BR> public void setFilesPath(String reserveFileName, String
ClassFileName,<BR>
String sourceFileName, String outputFileName) {<BR>
//创建文件对象<BR> SourceFile = new
java.io.File(sourceFileName);<BR>
//创建文件对象<BR> ReserveFile = new
java.io.File(reserveFileName);<BR>
//创建文件对象<BR> ClassFile = new
java.io.File(ClassFileName);<BR>
//创建文件对象<BR> OutputFile = new
java.io.File(outputFileName);</P>
<P> //如果文件已经存在,先删除,然后建立新文件<BR> if
(OutputFile.exists()) {<BR>
OutputFile.delete();<BR> }<BR> try
{<BR>
OutputFile.createNewFile();<BR> }<BR>
catch (Exception e) {<BR>
e.printStackTrace(System.err);<BR>
}<BR> try {<BR>
//创建文件随机读取对象<BR> java.io.RandomAccessFile
ROutputFile = new
java.io.RandomAccessFile(this.<BR>
OutputFile, "rw");<BR>
//提示信息<BR>
ROutputFile.write("//////////////////////////////////////////////////\n".<BR>
getBytes());<BR> ROutputFile.write(
("//JAccidenceAnalyser version " + getVersion()
+<BR>
" design by yellowicq//\n").getBytes());<BR>
ROutputFile.write("//java词法分析器//////////////\n".getBytes());<BR>
ROutputFile.write("//使用java语言开发////////////////////////////////////\n".<BR>
getBytes());<BR>
ROutputFile.write("//////////////////////////////////////////////////\n".<BR>
getBytes());<BR>
ROutputFile.write("词法分析结果如下:\n".getBytes());<BR>
//关闭文件流<BR>
ROutputFile.close();<BR> }<BR> catch
(Exception e) {<BR>
e.printStackTrace(System.err);<BR> }</P>
<P> }</P>
<P> /**<BR> * @roseuid 3D9BAFAB0089<BR>
*/<BR> public void startAA() {<BR>
//从预处理开始词法分析<BR>
this.pretreatment.startPretreatment();<BR> }</P>
<P> /**<BR> * @roseuid 3D9BB0B40383<BR>
*/<BR> public void outputAccidence(String outputString)
{<BR> //把分析出来的单词写入文件<BR> outputString
= "\n[第" + this.pretreatment.fileRow + "行]\n" +
outputString;<BR> try
{<BR>
//创建文件随机读取对象<BR> java.io.RandomAccessFile
ROutputFile = new
java.io.RandomAccessFile(this.<BR>
OutputFile, "rw");<BR>
//移动指针到文件末尾<BR>
ROutputFile.seek(ROutputFile.length());<BR>
//Start appending!<BR>
ROutputFile.write(outputString.getBytes());<BR>
//关闭文件流<BR> ROutputFile.close();</P>
<P> }<BR> catch (Exception e)
{<BR>
e.printStackTrace(System.err);<BR>
}<BR> //将分析的单词结果输出到终端<BR>
System.out.print(outputString);<BR> }</P>
<P> /**<BR> * @roseuid 3D9BB0CE02C2<BR>
*/<BR> public void controlThread() {<BR>
//控制扫描器启动扫描<BR> scaner.controlThread();<BR> }</P>
<P> //获得版本号<BR> public String getVersion()
{<BR> return "1.0";<BR>
}<BR>}<BR>3) 预处理子程序:Pretreatment.java<BR> //Source file:
d:\\JAccidenceAnalyse\\Pretreatment.java</P>
<P>package JAccidenceAnalyse;</P>
<P>import JAccidenceAnalyse.Buffer.*;<BR>import java.io.*;</P>
<P>public class Pretreatment {<BR> private String
tmpString;<BR> private String outputString;<BR> private int
BUFFER_SIZE = 100;<BR> private AccidenceAnalyser aa;<BR>
public InputBuffer inputBuffer; //输入缓冲区--共享<BR> private java.io.File
SourceFile; //文件对象<BR> private java.io.RandomAccessFile randomAFile;
//随机文件对象<BR> public static int fileRow = 0;<BR>
/**<BR> * @roseuid 3DAB7C530399<BR> */<BR>
public Pretreatment(File SourceFile, AccidenceAnalyser aa)
{<BR> try {<BR>
this.SourceFile = SourceFile;<BR>
this.randomAFile = new java.io.RandomAccessFile(this.SourceFile,
"r");<BR> }<BR> catch
(FileNotFoundException e) {<BR>
e.printStackTrace(System.err);<BR>
}<BR> this.aa = aa;<BR> inputBuffer =
aa.csbFactory.createInputBuffer(BUFFER_SIZE);<BR>
System.out.println("[INFOR]预处理器已经创建!");<BR> }</P>
<P> /**<BR> * @roseuid 3D9BAFE20331<BR>
*/<BR> public void putSourceToINBuffer(String tmpString)
{<BR> this.inputBuffer.Data =
tmpString.toCharArray();<BR> }</P>
<P> /**<BR> * @roseuid 3D9BB0400169<BR>
*/<BR> public void putFinToSCBuffer(String filtratedString)
{<BR> aa.scaner.scanBuffer.Data =
filtratedString.toCharArray();<BR> }</P>
<P> /**<BR> * @roseuid 3D9BB05E00A4<BR>
*/<BR> public void controlThread() {<BR> int
intLength;<BR> int resCounter = 0;<BR>
String tmpString;<BR> String
filtratedString;<BR>
System.out.println("[INFOR]开始单词分析////////////////////////////////////////");<BR>
try {<BR> if (SourceFile.exists()) {
//文件存在<BR>
//读文件内容到缓冲区<BR> while (
(tmpString = this.randomAFile.readLine()) != null)
{<BR>
++fileRow;<BR>
//分割符<BR>
System.out.println("...................begin row " + this.fileRow
+<BR>
".......................");<BR>
//开始这一行分析<BR>
System.out.println("[INFOR]正在处理行: " +
String.valueOf(fileRow));<BR>
//放入输入缓冲区<BR>
this.putSourceToINBuffer(tmpString);<BR>
//处理字符串<BR>
filtratedString =
this.filtrateSource(this.inputBuffer.Data);<BR>
System.out.println("[INFOR]已过滤句子: " +
filtratedString);<BR>
//放入扫描缓冲区<BR>
this.putFinToSCBuffer(filtratedString);<BR>
aa.controlThread();<BR>
}<BR>
System.out.println(<BR>
"[INFOR]分析完毕////////////////////////////////////////////");<BR>
}<BR> else {
//文件不存在<BR>
System.err.println("[ERROR]源文件不存在!");<BR>
}<BR> }<BR> catch (Exception e)
{<BR>
e.printStackTrace(System.err);<BR> }<BR> }</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -