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

📄 java词法分析器.txt

📁 用java写的词法分析器,很不错的哦! 值得一看
💻 TXT
📖 第 1 页 / 共 3 页
字号:
                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':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, 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 '=':

          //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 '+':

          //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 '*':

          //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(TOKEN) + "[种别码] " +
                String.valueOf(aa.classIdentity.findKey(String.valueOf(CHAR))) +
                "\n";
          }

          //clear up token
          this.TOKEN = "";
          break;

        case ',':

          //state10
          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 '(':

          //state11
          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 ')':

          //state12
          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 '{':

          //state13
          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 '}':

          //state14
          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 '[':

          //state15
          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 ']':

          //state16
          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 '.':

          //state17
          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;

        default:

          //state18
          this.TOKEN = this.CONTACT(this.TOKEN, this.CHAR);

          //追加出错信息
          this.finalAccidence = this.finalAccidence + "[ERROR]" +
              this.WORD_ERROR_INF + "'" + this.TOKEN + "'" + "\n";
          this.ASTATE = false;

          //clear up token
          this.TOKEN = "";
          break;
      }
      if (this.ASTATE == false) {
        break;
      }

    }
    return this.finalAccidence;
  }

  /**
   * @roseuid 3D9BB2E70260
   */
  public void controlThread() {
    this.toDelString = this.readFromBuffer(this.scanBuffer.Data);
    this.aa.outputAccidence(this.scan(this.toDelString));
    //分割符
    System.out.println("...................end row " + aa.pretreatment.fileRow +
                       ".........................");
    //结束这一行分析
    //clear up the var
    this.index = 0;
    this.finalAccidence = "";
    this.ASTATE = true;
    this.toDelString = "";
    this.senLength = 0;
    this.TOKEN = "";
  }

  /**
   * @param tmpString
   * @return String
   * @roseuid 3D9BB3FB037F
   */
  public String returnAWord(String TOKEN) {
    return TOKEN;
  }

  /**
   * @roseuid 3D9BB55502B4
   */
  public void initBuffer() {
    this.scanBuffer = aa.csbFactory.createScanBuffer(BUFFER_SIZE);
  }

//以下为字符的处理方法
/////////////////////////////////////////////////////
//////////////字符方法开始///////////////////////////
/////////////////////////////////////////////////////
  /**
   * @roseuid 3D9BB0B40383
   */
  public char GETBC(char[] sentenceChar) {
    try {
      while ( (sentenceChar[this.index]) == ' ') {
        this.index++;
      }
      this.index++;
    }
    catch (java.lang.ArrayIndexOutOfBoundsException e) {
      return ';'; //表示此行已经结束
    }
    return sentenceChar[index - 1];
  }

  /**
   * @roseuid 3D9BB0B40383
   */
  public char GETCHAR(char[] sentenceChar) {
    next();
    return sentenceChar[this.index - 1];
  }

  /**
   * @roseuid 3D9BB0B40383
   */
  public void next() {
    this.index++;
  }

  /**
   * @roseuid 3D9BB0B40383
   */
  public boolean ISLETTER(char letter) {
    return java.lang.Character.isLetter(letter);
  }

  /**
   * @roseuid 3D9BB0B40383
   */
  public boolean ISDIGIT(char letter) {
    return java.lang.Character.isDigit(letter);
  }

  /**
   * @roseuid 3D9BB0B40383
   */
  public String CONTACT(String TOKEN, char CHAR) {
    String tmpS = TOKEN + String.valueOf(CHAR);
    TOKEN = tmpS;
    return TOKEN;
  }

  /**
   * @roseuid 3D9BB0B40383
   */
  public boolean ISRESERVE(String TOKEN) {
    return aa.keyWordTable.isKeyWord(TOKEN);
  }

  /**
   * @roseuid 3D9BB0B40383
   */
  public void RETRACT() {
    this.index--;
  }
/////////////////////////////////////////////////////
//////////////字符方法结束///////////////////////////
/////////////////////////////////////////////////////
}
5) 表留字表程序:KeyWordTable.java
//Source file: d:\\JAccidenceAnalyse\\KeyWordTable.java

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;

  /**
   * @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.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 char[TMP_BUFFER_SIZE];
    String resWord;
    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]读取关键字: [INDEX: " + resCounter +
                             "][VALUE: " + resWord + "]");
          this.KWHash.put(Integer.toString(resCounter), resWord);
        }

        //关闭读文件对象
        resFileReader.close();
      }
      else { //文件不存在
        System.err.println("[ERROR]保留字文件不存在!");
      }
    }
    catch (Exception e) {
      e.printStackTrace(System.err);
    }
  }
}
6) 类型种别码程序:ClassIdentity.java
//Source file: d:\\JAccidenceAnalyse\\KeyWordTable.java

package JAccidenceAnalyse;

import java.util.*;
import java.io.*;

public class ClassIdentity {
  private Hashtable ClassHash;
  private File ClassFile;
  private FileReader classFileReader; //读文件对象
  private int TMP_BUFFER_SIZE = 30;

  /**
   * @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 = 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(); //创建hash表
    int intLength;
    char[] chrBuffer = new char[TMP_BUFFER_SIZE];
    String classWord;
    int classCounter = 0;
    try {
      if (ClassFile.exists()) { //文件存在

⌨️ 快捷键说明

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