⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java词法分析器.txt

📁 用java写的词法分析器,很不错的哦! 值得一看
💻 TXT
📖 第 1 页 / 共 3 页
字号:
作者:未知 来源:未知 加入时间:2004-8-10 开发文档 



Java词法分析器
            [使用java开发,并且用来分析java源文件]
2003年1月12日

1. 开发工具:rational rose2002 jedition,borland jbuilder6 professional
2. 开发步骤:
1) 基于状态转换图的编译器原理如下:

                     


2)在rose中建立分析器模型框架,根据分析器的状态转换图算法以及算法构造。词法分析器的框架结构如下图所示:


 (分析器软件包)
 
(词法分析器的控制器结构,包括预编译器,扫描程序,保留字表和单词的类型种别码表以及词法分析器的引导程序和控制程序)
 
(词法分析器的扫描缓冲区和输入缓冲区结构以及获得缓冲区的缓冲工厂)
3)使用rose正向工程产生java框架代码,在jbuilder中进行编辑实现功能代码,生成最终的代码,进行test和debug,最后形成最终的目标程序。具体的实现请参考源代码。编辑和测试如下图所示:

 
(开发环境)
 
(运行结果,详细结果附在后面)
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");
    }
    catch (FileNotFoundException e) {
      e.printStackTrace(System.err);
    }
    this.aa = aa;
    inputBuffer = aa.csbFactory.createInputBuffer(BUFFER_SIZE);
    System.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.toCharArray();
  }

  /**
   * @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) {
          ++fileRow;
          //分割符
          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("[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
   */
  public String filtrateSource(char[] Data) {
    String filtratedString = String.valueOf(Data).trim();
    return filtratedString;
  }

  /**
   * @roseuid 3D9BB9350315
   */
  public void startPretreatment() {
    this.controlThread();
  }
}
4) 扫描子程序:Scaner.java
//Source file: d:\\JAccidenceAnalyse\\Scaner.java

package JAccidenceAnalyse;

import JAccidenceAnalyse.Buffer.*;

public class Scaner {
  public ScanBuffer scanBuffer; //扫描缓冲区--共享
  private String finalAccidence;
  private AccidenceAnalyser aa;
  private int BUFFER_SIZE = 100;
  private String toDelString;
  private int senLength = 0;
  private char[] sentenceChar = new char[1000];
  private String TOKEN;
  private char CHAR;
  private int index = 0;
  private String IDENTITY = "identity";
  private String DIGIT = "digit";
  private String WORD_ERROR_INF = "在此行发现不能识别的单词,此行分析终止!";
  private boolean ASTATE = true;
  /**
   * @roseuid 3D9BB9370213
   */
  public Scaner(AccidenceAnalyser aa) {
    this.aa = aa;
    initBuffer();
    this.finalAccidence = "";
    System.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 = toDelString.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 letter
case 'a':case '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':case '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':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, CHAR);

          //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 + "[保留字] " +

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -