1、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 KeyWo
2、rdTable keyWordTable;public ClassIdentity classIdentity;public Scaner scaner;public ConcreteScanBufferFactory csbFactory;/* 2) 词法分析器主程序* roseuid 3D9BB93303D0*/public AccidenceAnalyser() System.out.println(“INFOR已经建立词法分析器!“);/* roseuid 3D9BAEF9029F*/public void initAA() /创建缓冲工厂this.csbFactory = new C
3、oncreteScanBufferFactory();/创建字符串扫描对象scaner = new Scaner(this);/创建pre处理对象pretreatment = new Pretreatment(SourceFile, this);/创建关键字表对象keyWordTable = new KeyWordTable(ReserveFile);/创建对象种别码表对象classIdentity = new ClassIdentity(ClassFile);System.out.println(“INFOR已经初始化词法分析器 !“);/* roseuid 3D9BAF12022D*/pu
4、blic 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 jav
5、a.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.writ
6、e(“/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();/关闭文件流ROutput
7、File.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“
8、 + 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.
9、err);/将分析的单词结果输出到终端System.out.print(outputString);/* roseuid 3D9BB0CE02C2*/public void controlThread() /控制扫描器启动扫描scaner.controlThread();/获得版本号public String getVersion() return “1.0“;package JAccidenceAnalyse;import java.util.*;import java.io.*;public class ClassIdentity private Hashtable ClassHash;p
10、rivate File ClassFile;private FileReader classFileReader; /读文件对象private int TMP_BUFFER_SIZE = 30;/* 6) 类型种别码程序* roseuid 3D9BB9390108*/public ClassIdentity(java.io.File ClassFile) System.out.println(“INFOR类型种别码表已创建!“);this.ClassFile = ClassFile;/* roseuid 3D9BB0B40383*/查找类型种别码public int findKey(Strin
11、g classWord) int KEY;for (Enumeration e = this.ClassHash.keys(); e.hasMoreElements(); ) KEY = Integer.parseInt( (String) e.nextElement();if ( ( (String)this.ClassHash.get(Integer.toString(KEY).equalsIgnoreCase(classWord) return KEY;return -1;/* roseuid 3D9BAE7303D3*/public void initClassIdentityTabl
12、e() ClassHash = new Hashtable(); /创建hash表int intLength;char chrBuffer = new charTMP_BUFFER_SIZE;String classWord;int classCounter = 0;try if (ClassFile.exists() /文件存在/创建读文件对象classFileReader = new java.io.FileReader(ClassFile);/读文件内容到 hash表while ( (intLength = classFileReader.read(chrBuffer) != -1) c
13、lassCounter+;/填写 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
14、 (Exception e) e.printStackTrace(System.err);package JAccidenceAnalyse;import java.util.*;import java.io.*;public class KeyWordTable private Hashtable KWHash;private File ReserveFile;private FileReader resFileReader; /读文件对象private int TMP_BUFFER_SIZE = 30;/* 5) 表留字表程序* roseuid 3D9BB9390108*/public K
15、eyWordTable(java.io.File ReserveFile) System.out.println(“INFOR关键字表已创建!“);this.ReserveFile = ReserveFile;/* param inw* return boolean* roseuid 3D9BAE4702AD*/public boolean isKeyWord(String inw) String resWord;/查找hash表for (Enumeration e = this.KWHash.elements(); e.hasMoreElements(); ) resWord = (Stri
16、ng) e.nextElement();if (resWord.equalsIgnoreCase(inw) return true;return false;/* roseuid 3D9BAE7303D3*/public void initKeyWordTable() KWHash = new Hashtable(); /创建hash 表int intLength;char chrBuffer = new charTMP_BUFFER_SIZE;String resWord;int resCounter = 0;try if (ReserveFile.exists() /文件存在/创建读文件对
17、象resFileReader = new java.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
18、(resCounter), resWord);/关闭读文件对象resFileReader.close();else /文件不存在System.err.println(“ERROR保留字文件不存在!“);catch (Exception e) e.printStackTrace(System.err);package JAccidenceAnalyse;import javax.xml.parsers.*;import org.w3c.dom.*;public class main /* 1) 词法分析器引导文件* param args* return void* roseuid 3D9BAE4
19、702AD*/public static void main(String args) /读取配置文件, 得到系统属性String cfgString = new String4;try cfgString = main.loadAACfg(“d:aaCfg.xml“);catch (Exception e) e.printStackTrace(System.err);/设置待读文件名/保留字表文件String reserveFileName = cfgString0;/类型种别码表文件String classFileName = cfgString1;/需要分析的源文件String sour
20、ceFileName = cfgString2;/输出文件String outputFileName = cfgString3;/创建词法分析器AccidenceAnalyser aa = new AccidenceAnalyser();aa.setFilesPath(reserveFileName, classFileName, sourceFileName,outputFileName); /建立所需要的文件对象/初始化词法分析器aa.initAA();/初始化关键字表aa.keyWordTable.initKeyWordTable();/初始化类型种别码表aa.classIdentity
21、.initClassIdentityTable();/开始进行词法分析aa.startAA();/分析完毕/读取配置文件private static String loadAACfg(String name) throws Exception String cfgString = new String4;/*解析xml 配置文件 */try /*创建文档工厂*/DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();/*创建文档解析器*/DocumentBuilder builder = factory.new
22、DocumentBuilder();/*解析配置文件*/Document doc = builder.parse(name);/*规范化文档*/doc.normalize();/*查找接点表*/NodeList nlists = doc.getElementsByTagName(“FilePath“);for (int i = 0; i d:reserve.table d:class.table d:aClass.java d:output.aa Class.tableidentity digit = + * * , ( ) . Reserve.tablewhile do public class static new void int do for int System