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

📄 scan.java~2~

📁 我用java写的词法分析器
💻 JAVA~2~
字号:
package 编译技术;

//import java.lang.Character.*;

public class Scan
{
   private int index = 0;
   private char[] sentenceChar = new char[1000];
     private int senLength = 0;
     private String TOKEN;
  private char CHAR;
    private String finalAccidence;
      private boolean ASTATE = true;
      ReservedWordTable rwt=new ReservedWordTable();
      String sourceString;

      public Scan(String sourceString)
      {
       this.sourceString= sourceString;
      }
        //private AccidenceAnalyser aa;
  public char GETBC(char[] sentenceChar)
  {
    try
    {
      while ( (sentenceChar[this.index]) == ' ')
      {
        this.index++;
      }
      this.index++;
    }
    catch (java.lang.ArrayIndexOutOfBoundsException e)
    {
      return ';'; //表示此行已经结束
    }
    return sentenceChar[index - 1];
  }

  public char GETCHAR(char[] sentenceChar)
  {
    //next();
    this.index++;
    return sentenceChar[this.index - 1];
  }

  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);
          CHAR = this.GETCHAR(sentenceChar);
          while (java.lang.Character.isLetter(CHAR) || java.lang.Character.isDigit(CHAR))
          {
            this.TOKEN = this.CONTACT(this.TOKEN, CHAR);
            CHAR = this.GETCHAR(sentenceChar);
          }
          this.RETRACT();

          //state2
          if (rwt.isKeyWord(TOKEN))
          {
            this.finalAccidence = this.finalAccidence + "[保留字] " +
                this.returnAWord(TOKEN) + "\n";
          }
          else
          {
            this.finalAccidence = this.finalAccidence + "[标识符] " +
                this.returnAWord(TOKEN) + "[种别码] " +
                String.valueOf(rwt.getKeyWordType(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(rwt.getKeyWordType(String.valueOf(CHAR))) +
                "\n";
          }
          //state9
          else
          {
            this.finalAccidence = this.finalAccidence + "[乘号] " +
                this.returnAWord(TOKEN) + "[种别码] " +
                String.valueOf(rwt.getKeyWordType(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;
  }

  public String CONTACT(String TOKEN, char CHAR)
  {
      String tmpS = TOKEN + String.valueOf(CHAR);
      TOKEN = tmpS;
      return TOKEN;
    }

    public void RETRACT() {
      this.index--;
    }

    public String returnAWord(String TOKEN) {
  return TOKEN;
}

    }

⌨️ 快捷键说明

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