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

📄 scanner.java

📁 一个JAVA编写的简单编译器
💻 JAVA
字号:
package syntax;


public class Scanner {
  private java.io.BufferedReader source; 
  private int  ch;   
  private int  chn;  
  
  
  private StringBuffer yytext;  
  private int yylineno ;        
  
  public int getyylineno () {
    return yylineno;
  }

  public String getyytext () {
    return yytext.toString ();
  }

  private static final char CR    =  '\r';
  private static final char TAB   =  '\t';
  private static final char LF    =  '\n';
  private static final char Blank =  ' ';

  
  private void reset () {
    ch = chn = Blank;
    yylineno = 1;
    yytext = new StringBuffer (50);
  }
  
  public Scanner () {
    reset ();
  }

  //read the next character.
  private void input () {
    ch = chn;
    if (ch == '\n')
      yylineno++;
    try {
      chn = source.read ();
       if (chn == -1)
				chn = '$';
    } catch (java.io.IOException e) {
      System.err.println (e);
      System.exit (1);
    }
  }

  // scan the keywords, or indentifier,
  private int scanWords () {
    yytext.setLength (0);
    do                     //  a..zA..Z0..9_
    {
      yytext.append ((char)ch);
      input ();
    }
      while ((('a' <= ch) && (ch <= 'z')) ||
             (('A' <= ch) && (ch <= 'Z')) ||
             (('0' <= ch) && (ch <= '9')) ||
             ((ch == '_') || (ch == '.')));
    
    int i = CKeyWords.search (yytext.toString());
    if (i > 0)
      return i;
    return CKeyWords.SY_IDENTIFIER;
  }

 
  private int scanNumber ()   {
      yytext.append ((char)ch);
      input ();
      while ('0' <= ch && ch <= '9') {
        yytext.append ((char)ch);
        input ();
      }
      if (('l' == ch) || (ch == 'L')) {
        yytext.append ((char)ch);
        input ();
      }
      return CKeyWords.SY_NUMBERLITERAL;
  }


  private void skipWhiteSpace () {
    while (ch != -1)
      switch (ch) {
      case Blank : case CR: case LF: case TAB:
        input ();
        break;
      case '/':
        if (chn == '*') {
          input ();
          while (!((ch == '*') && (chn == '/')) || (ch == -1))
            input ();
          if (ch != -1) {
            input ();
            input ();
          }
          break;
        } else if (chn == '/') {
          do
            input ();
            while ((ch != '\n') && (ch != -1));
          input ();
          break;
        } else
          return;
      default : 
        return;
      }
  }

  
  //scan these symbols { ! } ~ ( ) , . : ; ? [ ]
  private int scan1 () {
    yytext.setLength (0);
    yytext.append ((char)ch);
    input ();    
    return CKeyWords.search (yytext.toString ());
  }

  // scan the doble symbols of the following:  | % & * + - / = ^
  
  private int scan2 () {
    yytext.setLength (0);
   
    yytext.append ((char)ch);
    yytext.append ((char)chn);
    int i = CKeyWords.search (yytext.toString ());
    if (i > 0) {
      
      input ();  
      input ();  
      return i;
    }
    
    return scan1 ();
  }

  
  private int scan3 () {
    
    yytext.setLength (0);
    yytext.append ((char)ch);
    input ();
    switch (ch) {
    case '<':
      //<<=
      yytext.append ((char)ch);
      input ();
      if (ch == '=') {
       
        yytext.append ((char)ch);
        input ();
        return CKeyWords.SY_LSHIFTASS;
      }
      //  <<
      return CKeyWords.SY_LSHIFT;
    case '=':
      //  <=
      yytext.append ((char)ch);
      input ();
      return CKeyWords.SY_LE;
    default :
      //  <
      return CKeyWords.SY_L;
    }
  }

  //scan symbols : > >= >> >>= >>> >>>=
  
  private int scan4 ()  {
    
    yytext.append ((char)ch);
    input ();
    switch (ch) {
    case '>':
      //   >>
      yytext.append ((char)ch);
      input ();
      if (ch == '=') {
        //  >>=
        yytext.append ((char)ch);
        input ();
        return CKeyWords.SY_RSHIFTASS;
      }
      if (ch == '>') {
        //  >>>
        yytext.append ((char)ch);
        input ();
        if (ch == '=') {
          //  >>>=
          yytext.append ((char)ch);
          input ();
          return CKeyWords.SY_RRSHIFTASS;
        }
        //  3 >>>
        return CKeyWords.SY_RRSHIFT;
      }
      //  >>
      return CKeyWords.SY_RSHIFT;
    case '=':
      //  >=
      yytext.append ((char)ch);
      input ();
      return CKeyWords.SY_GE;
    default :
      //  > 
      return CKeyWords.SY_G;
    }
  }

  // scan for the char, 
  private int scanChar () {
    yytext.setLength (0);
    input ();
    while ((ch != -1) && (ch != '\'')) {
      if (ch == '\\')
        input ();
      yytext.append ((char)ch);
      input ();
    }
    if (ch != -1)
      input ();
    return CKeyWords.SY_CHARLITERAL;
  }

  //scan for the string 
  private int scanString () {
    yytext.setLength (0);
    input ();
    while ((ch != -1) && (ch != '"')) {
      if (ch == '\\')
        input ();
      yytext.append ((char)ch);
      input ();
    }
    if (ch != -1)
      input ();
    return CKeyWords.SY_STRINGLITERAL;
  }

  public int yylex ()   {
    yytext.setLength (0);
    skipWhiteSpace ();

    switch (ch)   {
    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': case '_':
      return scanWords ();
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
      return scanNumber ();
    case '>':
      return scan4 ();
    case '<':
      return scan3 ();
    case '|': case '%': case '&': case '*': case '!':
    case '+': case '-': case '/': case '=': case '^':
      return scan2 ();
    case '{': case '}': case '[': case ']':
    case '(': case ')': case ',': case ':':
    case ';': case '?': case '~':
      return scan1 ();
    case '.':
      if (('0' <= chn) && (chn <= '9'))
        return scanNumber ();
      return scan1 ();
    case '\'':
      return scanChar ();
    case '"':
      return scanString ();
    case '$':
      return CKeyWords.EOF;
    default:
      if (ch != -1)
        util.Error.e1 ("Zeichen in Scanner");
      return -1;
    }
  }

  
  public void open (String name) {
    try {
      source = new java.io.BufferedReader (new java.io.FileReader (name));
    } catch (java.io.IOException e) {
      util.Error.e2 (e.toString ());
    }
  }


 
  public String getText () {
    return yytext.toString ();
  }


  public static void scanAll (String name) {
    int sy = 1;
    Scanner scan = new Scanner ();
    scan.open (name);
    sy = scan.yylex ();
    while (sy >= 0)  {
      sy = scan.yylex ();
      System.out.println (util.Options.nToS (sy, 4) + " " + scan.getText ());
    }
  }

}

⌨️ 快捷键说明

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