java词法分析器_第1页
java词法分析器_第2页
java词法分析器_第3页
java词法分析器_第4页
java词法分析器_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

2、KeyWordTable keyWordTable; public ClassIdentity classIdentity; public Scaner scaner; public ConcreteScanBufferFactory csbFactory; /* * 2) 词法分析器主程序 * roseuid 3D9BB93303D0 */ public AccidenceAnalyser() System.out.println("INFOR已经建立词法分析器!"); /* * roseuid 3D9BAEF9029F */ public void initAA() /

3、创建缓冲工厂 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("INF

4、OR已经初始化词法分析器!"); /* * 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 =

5、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 jav

6、a.io.RandomAccessFile(this. OutputFile, "rw"); /提示信息 ROutputFile.write("/n". getBytes(); ROutputFile.write( ("/JAccidenceAnalyser version " + getVersion() + " design by yellowicq/n").getBytes(); ROutputFile.write("/java词法分析器/n".getBytes(); ROutputFil

7、e.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.pretrea

8、tment.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.

9、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 */ publi

10、c void controlThread() /控制扫描器启动扫描 scaner.controlThread(); /获得版本号 public String getVersion() return "1.0" package JAccidenceAnalyse;import java.util.*;import java.io.*;public class ClassIdentity private Hashtable ClassHash; private File ClassFile; private FileReader classFileReader; /读文件对象

11、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(String classWord) int KEY; for (Enumeration e

12、 = 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 initClassIdentityTable() ClassHash = new Hashtable()

13、; /创建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) classCounter+; /填写hash表

14、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("ERR

15、OR类型种别码文件不存在!"); catch (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) 表留字表

16、程序 * roseuid 3D9BB9390108 */ public KeyWordTable(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.

17、KWHash.elements(); e.hasMoreElements(); ) resWord = (String) 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 re

18、sWord; int resCounter = 0; try if (ReserveFile.exists() /文件存在 /创建读文件对象 resFileReader = new java.io.FileReader(ReserveFile); /读文件内容到hash表 while ( (intLength = resFileReader.read(chrBuffer) != -1) resCounter+; /填写hash表 resWord = String.valueOf(chrBuffer).trim(); System.out.println("INFOR读取关键字: IN

19、DEX: " + resCounter + "VALUE: " + resWord + ""); this.KWHash.put(Integer.toString(resCounter), resWord); /关闭读文件对象 resFileReader.close(); else /文件不存在 System.err.println("ERROR保留字文件不存在!"); catch (Exception e) e.printStackTrace(System.err); package JAccidenceAnalyse;i

20、mport javax.xml.parsers.*;import org.w3c.dom.*;public class main /* * 1) 词法分析器引导文件 * param args * return void * roseuid 3D9BAE4702AD */ public static void main(String args) /读取配置文件,得到系统属性 String cfgString = new String4; try cfgString = main.loadAACfg("d:aaCfg.xml"); catch (Exception e) e.p

21、rintStackTrace(System.err); /设置待读文件名/ /保留字表文件 String reserveFileName = cfgString0; /类型种别码表文件 String classFileName = cfgString1; /需要分析的源文件 String sourceFileName = cfgString2; /输出文件 String outputFileName = cfgString3;/ /创建词法分析器 AccidenceAnalyser aa = new AccidenceAnalyser(); aa.setFilesPath(reserveFil

22、eName, classFileName, sourceFileName, outputFileName); /建立所需要的文件对象 /初始化词法分析器 aa.initAA(); /初始化关键字表 aa.keyWordTable.initKeyWordTable(); /初始化类型种别码表 aa.classIdentity.initClassIdentityTable(); /开始进行词法分析 aa.startAA(); /分析完毕 /读取配置文件 private static String loadAACfg(String name) throws Exception String cfgS

23、tring = new String4; /*解析xml配置文件*/ try /*创建文档工厂*/ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); /*创建文档解析器*/ DocumentBuilder builder = factory.newDocumentBuilder(); /*解析配置文件*/ Document doc = builder.parse(name); /*规范化文档*/ doc.normalize(); /*查找接点表*/ NodeList nlists = doc.getEl

24、ementsByTagName("FilePath"); for (int i = 0; i < nlists.getLength(); i+) Element item = (Element) nlists.item(i); /取得需要的配置属性 /*/ cfgString0 = item.getElementsByTagName("ReserveFileName").item(0). getFirstChild().getNodeValue().trim(); /*/ cfgString1 = item.getElementsByTagName

25、("ClassFileName").item(0). getFirstChild().getNodeValue().trim(); /*/ cfgString2 = item.getElementsByTagName("SourceFileName").item(0). getFirstChild().getNodeValue().trim(); /*/ cfgString3 = item.getElementsByTagName("OutputFileName").item(0). getFirstChild().getNodeVa

26、lue().trim(); /*/ catch (Exception e) e.printStackTrace(); throw new Exception("ERROR加载配置文件 " + name + " 错误!"); /返回属性数组 return cfgString; package JAccidenceAnalyse;import JAccidenceAnalyse.Buffer.*;import java.io.*;public class Pretreatment private String tmpString; private Strin

27、g 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; /* * 3) 预处理子程序 * roseuid 3DAB7C530399 */ public Pretreatm

28、ent(File SourceFile, AccidenceAnalyser aa) try this.SourceFile = SourceFile; this.randomAFile = new java.io.RandomAccessFile(this.SourceFile, "r"); catch (FileNotFoundException e) e.printStackTrace(System.err); this.aa = aa; inputBuffer = aa.csbFactory.createInputBuffer(BUFFER_SIZE); Syste

29、m.out.println("INFOR预处理器已经创建!"); /* * roseuid 3D9BAFE20331 */ public void putSourceToINBuffer(String tmpString) this.inputBuffer.Data = tmpString.toCharArray(); /* * roseuid 3D9BB0400169 */ public void putFinToSCBuffer(String filtratedString) aa.scaner.scanBuffer.Data = filtratedString.toC

30、harArray(); /* * roseuid 3D9BB05E00A4 */ public void controlThread() int intLength; int resCounter = 0; String tmpString; String filtratedString; System.out.println("INFOR开始单词分析/"); try if (SourceFile.exists() /文件存在 /读文件内容到缓冲区 while ( (tmpString = this.randomAFile.readLine() != null) +file

31、Row; /分割符 System.out.println(".begin row " + this.fileRow + "."); /开始这一行分析 System.out.println("INFOR正在处理行: " + String.valueOf(fileRow); /放入输入缓冲区 this.putSourceToINBuffer(tmpString); /处理字符串 filtratedString = this.filtrateSource(this.inputBuffer.Data); System.out.println(

32、"INFOR已过滤句子: " + filtratedString); /放入扫描缓冲区 this.putFinToSCBuffer(filtratedString); aa.controlThread(); System.out.println( "INFOR分析完毕/"); else /文件不存在 System.err.println("ERROR源文件不存在!"); catch (Exception e) e.printStackTrace(System.err); /* * roseuid 3D9BB07D0239 */ pub

33、lic String filtrateSource(char Data) String filtratedString = String.valueOf(Data).trim(); return filtratedString; /* * roseuid 3D9BB9350315 */ public void startPretreatment() this.controlThread(); package JAccidenceAnalyse;import JAccidenceAnalyse.Buffer.*;public class Scaner public ScanBuffer scan

34、Buffer; /扫描缓冲区-共享 private String finalAccidence; private AccidenceAnalyser aa; private int BUFFER_SIZE = 100; private String toDelString; private int senLength = 0; private char sentenceChar = new char1000; private String TOKEN; private char CHAR; private int index = 0; private String IDENTITY = &qu

35、ot;identity" private String DIGIT = "digit" private String WORD_ERROR_INF = "在此行发现不能识别的单词,此行分析终止!" private boolean ASTATE = true; /* * 4) 扫描子程序 * roseuid 3D9BB9370213 */ public Scaner(AccidenceAnalyser aa) this.aa = aa; initBuffer(); this.finalAccidence = "" System

36、.out.println("INFOR扫描处理器已经创建!"); /* * roseuid 3D9BB2860329 */ public String readFromBuffer(char Data) String toDelString = String.valueOf(Data); return toDelString; /* * param tmpString * return String * roseuid 3D9BB2D5008D */ public String scan(String toDelString) sentenceChar = toDelStr

37、ing.toCharArray(); this.senLength = sentenceChar.length; int i = 0; /分析单词 while (this.index <= this.senLength) /state0: this.TOKEN = "" this.CHAR = GETBC(sentenceChar); if (this.CHAR = '') break; /''表示这一行结束 /进入状态判断 switch (this.CHAR) /judge lettercase 'a':case &#

38、39;b':case 'c':case 'd':case 'e':case 'f':case 'g':case 'h':case 'i':case 'j':case 'k':case 'l':case 'm':case 'n':case 'o':case 'p':case 'q':case 'r':case 's':c

39、ase 't':case 'u':case 'v':case 'w':case 'x':case 'y':case 'z':case 'A':case 'B':case 'C':case 'D':case 'E':case 'F':case 'G':case 'H':case 'I':case 'J':case 'K&

40、#39;:case 'L':case 'M':case 'N':case 'O':case 'P':case 'Q':case 'R':case 'S':case 'T':case 'U':case 'V':case 'W':case 'X':case 'Y':case 'Z': /do this.TOKEN = this.CONTACT(TOKEN, CH

41、AR); /state1 CHAR = this.GETCHAR(sentenceChar); while (this.ISLETTER(CHAR) | this.ISDIGIT(CHAR) this.TOKEN = this.CONTACT(this.TOKEN, CHAR); CHAR = this.GETCHAR(sentenceChar); this.RETRACT(); /state2 if (aa.keyWordTable.isKeyWord(TOKEN) this.finalAccidence = this.finalAccidence + "保留字 " +

42、this.returnAWord(TOKEN) + "n" else this.finalAccidence = this.finalAccidence + "标识符 " + this.returnAWord(TOKEN) + "种别码 " + String.valueOf(aa.classIdentity.findKey(IDENTITY) + "n" /clear up token this.TOKEN = "" break; /judge ditital case '0':

43、case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9': /do this.TOKEN = this.CONTACT(TOKEN, CHAR); /state3 CHAR = this.GETCHAR(sentenceChar); while (this.ISDIGIT(CHAR) this.TOKEN = this.CONTACT(TOKEN,

44、 CHAR); CHAR = this.GETCHAR(sentenceChar); this.RETRACT(); /state4 this.finalAccidence = this.finalAccidence + "数字 " + this.returnAWord(TOKEN) + "种别码 " + String.valueOf(aa.classIdentity.findKey(DIGIT) + "n" /clear up token this.TOKEN = "" break; case '=

45、9;: /state5 this.TOKEN = this.CONTACT(TOKEN, CHAR); this.finalAccidence = this.finalAccidence + "等号 " + this.returnAWord(TOKEN) + "种别码 " + String.valueOf(aa.classIdentity.findKey(String.valueOf(CHAR) + "n" /clear up token this.TOKEN = "" break; case '+'

46、;: /state6 this.TOKEN = this.CONTACT(TOKEN, CHAR); this.finalAccidence = this.finalAccidence + "加号 " + this.returnAWord(TOKEN) + "种别码 " + String.valueOf(aa.classIdentity.findKey(String.valueOf(CHAR) + "n" /clear up token this.TOKEN = "" break; case '*'

47、: /state7 this.TOKEN = this.CONTACT(TOKEN, CHAR); CHAR = this.GETCHAR(sentenceChar); /state8 if (CHAR = '*') this.TOKEN = this.CONTACT(TOKEN, CHAR); this.finalAccidence = this.finalAccidence + "乘方 " + this.returnAWord(TOKEN) + "种别码 " + String.valueOf(aa.classIdentity.findKey(String.valueOf(CHAR) + "n" /state9 else this.finalAccidence = this.finalAccidence + "乘号 " + this.returnAWord(TO

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论