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

📄 exprelexer.java.svn-base

📁 实现的一个简单的语言编译器,可以编译类JAVA 的小语言
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
    zzEndRead = zzStartRead;  /* invalidate buffer    */

    if (zzReader != null)
      zzReader.close();
  }


  /**
   * Resets the scanner to read from a new input stream.
   * Does not close the old reader.
   *
   * All internal variables are reset, the old input stream 
   * <b>cannot</b> be reused (internal buffer is discarded and lost).
   * Lexical state is set to <tt>ZZ_INITIAL</tt>.
   *
   * @param reader   the new input stream 
   */
  public final void yyreset(java.io.Reader reader) {
    zzReader = reader;
    zzAtBOL  = true;
    zzAtEOF  = false;
    zzEndRead = zzStartRead = 0;
    zzCurrentPos = zzMarkedPos = zzPushbackPos = 0;
    yyline = yychar = yycolumn = 0;
    zzLexicalState = YYINITIAL;
  }


  /**
   * Returns the current lexical state.
   */
  public final int yystate() {
    return zzLexicalState;
  }


  /**
   * Enters a new lexical state
   *
   * @param newState the new lexical state
   */
  public final void yybegin(int newState) {
    zzLexicalState = newState;
  }


  /**
   * Returns the text matched by the current regular expression.
   */
  public final String yytext() {
    return new String( zzBuffer, zzStartRead, zzMarkedPos-zzStartRead );
  }


  /**
   * Returns the character at position <tt>pos</tt> from the 
   * matched text. 
   * 
   * It is equivalent to yytext().charAt(pos), but faster
   *
   * @param pos the position of the character to fetch. 
   *            A value from 0 to yylength()-1.
   *
   * @return the character at position pos
   */
  public final char yycharat(int pos) {
    return zzBuffer[zzStartRead+pos];
  }


  /**
   * Returns the length of the matched text region.
   */
  public final int yylength() {
    return zzMarkedPos-zzStartRead;
  }


  /**
   * Reports an error that occured while scanning.
   *
   * In a wellformed scanner (no or only correct usage of 
   * yypushback(int) and a match-all fallback rule) this method 
   * will only be called with things that "Can't Possibly Happen".
   * If this method is called, something is seriously wrong
   * (e.g. a JFlex bug producing a faulty scanner etc.).
   *
   * Usual syntax/scanner level error handling should be done
   * in error fallback rules.
   *
   * @param   errorCode  the code of the errormessage to display
   */
  private void zzScanError(int errorCode) {
    String message;
    try {
      message = ZZ_ERROR_MSG[errorCode];
    }
    catch (ArrayIndexOutOfBoundsException e) {
      message = ZZ_ERROR_MSG[ZZ_UNKNOWN_ERROR];
    }

    throw new Error(message);
  } 


  /**
   * Pushes the specified amount of characters back into the input stream.
   *
   * They will be read again by then next call of the scanning method
   *
   * @param number  the number of characters to be read again.
   *                This number must not be greater than yylength()!
   */
  public void yypushback(int number)  {
    if ( number > yylength() )
      zzScanError(ZZ_PUSHBACK_2BIG);

    zzMarkedPos -= number;
  }


  /**
   * Contains user EOF-code, which will be executed exactly once,
   * when the end of file is reached
   */
  private void zzDoEOF() throws java.io.IOException {
    if (!zzEOFDone) {
      zzEOFDone = true;
      yyclose();
    }
  }


  /**
   * Resumes scanning until the next regular expression is matched,
   * the end of input is encountered or an I/O-Error occurs.
   *
   * @return      the next token
   * @exception   java.io.IOException  if any I/O-Error occurs
   */
  public java_cup.runtime.Symbol next_token() throws java.io.IOException {
    int zzInput;
    int zzAction;

    // cached fields:
    int zzCurrentPosL;
    int zzMarkedPosL;
    int zzEndReadL = zzEndRead;
    char [] zzBufferL = zzBuffer;
    char [] zzCMapL = ZZ_CMAP;

    int [] zzTransL = ZZ_TRANS;
    int [] zzRowMapL = ZZ_ROWMAP;
    int [] zzAttrL = ZZ_ATTRIBUTE;

    while (true) {
      zzMarkedPosL = zzMarkedPos;

      boolean zzR = false;
      for (zzCurrentPosL = zzStartRead; zzCurrentPosL < zzMarkedPosL;
                                                             zzCurrentPosL++) {
        switch (zzBufferL[zzCurrentPosL]) {
        case '\u000B':
        case '\u000C':
        case '\u0085':
        case '\u2028':
        case '\u2029':
          yyline++;
          yycolumn = 0;
          zzR = false;
          break;
        case '\r':
          yyline++;
          yycolumn = 0;
          zzR = true;
          break;
        case '\n':
          if (zzR)
            zzR = false;
          else {
            yyline++;
            yycolumn = 0;
          }
          break;
        default:
          zzR = false;
          yycolumn++;
        }
      }

      if (zzR) {
        // peek one character ahead if it is \n (if we have counted one line too much)
        boolean zzPeek;
        if (zzMarkedPosL < zzEndReadL)
          zzPeek = zzBufferL[zzMarkedPosL] == '\n';
        else if (zzAtEOF)
          zzPeek = false;
        else {
          boolean eof = zzRefill();
          zzEndReadL = zzEndRead;
          zzMarkedPosL = zzMarkedPos;
          zzBufferL = zzBuffer;
          if (eof) 
            zzPeek = false;
          else 
            zzPeek = zzBufferL[zzMarkedPosL] == '\n';
        }
        if (zzPeek) yyline--;
      }
      zzAction = -1;

      zzCurrentPosL = zzCurrentPos = zzStartRead = zzMarkedPosL;
  
      zzState = zzLexicalState;


      zzForAction: {
        while (true) {
    
          if (zzCurrentPosL < zzEndReadL)
            zzInput = zzBufferL[zzCurrentPosL++];
          else if (zzAtEOF) {
            zzInput = YYEOF;
            break zzForAction;
          }
          else {
            // store back cached positions
            zzCurrentPos  = zzCurrentPosL;
            zzMarkedPos   = zzMarkedPosL;
            boolean eof = zzRefill();
            // get translated positions and possibly new buffer
            zzCurrentPosL  = zzCurrentPos;
            zzMarkedPosL   = zzMarkedPos;
            zzBufferL      = zzBuffer;
            zzEndReadL     = zzEndRead;
            if (eof) {
              zzInput = YYEOF;
              break zzForAction;
            }
            else {
              zzInput = zzBufferL[zzCurrentPosL++];
            }
          }
          int zzNext = zzTransL[ zzRowMapL[zzState] + zzCMapL[zzInput] ];
          if (zzNext == -1) break zzForAction;
          zzState = zzNext;

          int zzAttributes = zzAttrL[zzState];
          if ( (zzAttributes & 1) == 1 ) {
            zzAction = zzState;
            zzMarkedPosL = zzCurrentPosL;
            if ( (zzAttributes & 8) == 8 ) break zzForAction;
          }

        }
      }

      // store back cached position
      zzMarkedPos = zzMarkedPosL;

      switch (zzAction < 0 ? zzAction : ZZ_ACTION[zzAction]) {
        case 8: 
          { return symbol(ExprESymbol.SEMICOLON);
          }
        case 13: break;
        case 12: 
          { return symbol(ExprESymbol.EQ);
          }
        case 14: break;
        case 5: 
          { /*
                                 if (preSymbol == ExprESymbol.INTEGER_LITERAL) {
                                    errMsg("Unexpected integer \""+yytext()+"\". Expect \";\" or nothing here.", yyline+1, yycolumn+1);
                                    debugMsg("Replace integer \""+yytext()+"\" with \";\".");
                                    return symbol(ExprESymbol.SEMICOLON, -1);
                                 } else if (preSymbol == ExprESymbol.IDENTIFIER) {
                                    errMsg("Unexpected integer \""+yytext()+"\". Expect \"=\" before it.", yyline+1, yycolumn+1);
                                    debugMsg("Ignore integer \""+yytext()+"\".");
                                    return symbol(ExprESymbol.NOP, ExprESymbol.INTEGER_LITERAL);
                                 } else if (preSymbol == ExprESymbol.RPAREN) {
                                    errMsg("Unexpected integer \""+yytext()+"\". Expect \";\" or nothing here.", yyline+1, yycolumn+1);
                                    debugMsg("Ignore integer \""+yytext()+"\".");
                                    return symbol(ExprESymbol.NOP, preSymbol);
                                 }
                                 */
                                 return symbol(ExprESymbol.INTEGER_LITERAL, new Integer(yytext()));
          }
        case 15: break;
        case 6: 
          { lparenStack.push(new LParen(yyline+1, yycolumn+1));
                                 return symbol(ExprESymbol.LPAREN);
          }
        case 16: break;
        case 2: 
          { /* ignore */
          }
        case 17: break;
        case 3: 
          { return symbol(ExprESymbol.DIV);
          }
        case 18: break;
        case 10: 
          { return symbol(ExprESymbol.MINUS);
          }
        case 19: break;
        case 7: 
          { /*
                                 if (lparenStack.empty()) {
                                    errMsg("Unexpected \")\". Expect \"(\" somewhere before.", yyline+1, yycolumn+1);
                                    debugMsg("Ignore \")\".");
                                    return symbol(ExprESymbol.NOP, preSymbol);
                                 } else {
                                    debugMsg("Pop a ( from lparenStack.");
                                    lparenStack.pop();
                                 }
                                 
                                 if (preSymbol == ExprESymbol.LPAREN) {
                                    errMsg("Unexpected \")\". Expect an expression within \"()\".", yyline+1, yycolumn+1);
                                 }
                                 */
                                 System.out.println(zzReader);
                                 
                                 return symbol(ExprESymbol.RPAREN);
          }
        case 20: break;
        case 1: 
          { throw new RuntimeException("Illegal character \""+yytext()+
                                                            "\" at line "+yyline+", column "+yycolumn);
          }
        case 21: break;
        case 11: 
          { return symbol(ExprESymbol.MULT);
          }
        case 22: break;
        case 4: 
          { /*
                                 if (preSymbol == ExprESymbol.INTEGER_LITERAL) {
                                    errMsg("Unexpected identifier \""+yytext()+"\". Expect \";\" before it.", yyline+1, yycolumn+1);
                                    debugMsg("Replace identifier \""+yytext()+"\" with \";\".");
                                    return symbol(ExprESymbol.SEMICOLON, -1);
                                 } else if (preSymbol == ExprESymbol.IDENTIFIER) {
                                    errMsg("Unexpected identifier \""+yytext()+"\". Expect \";\" or \"=\" before it.", yyline+1, yycolumn+1);
                                    debugMsg("Ignore identifier \""+yytext()+"\".");
                                    return symbol(ExprESymbol.NOP, ExprESymbol.IDENTIFIER);
                                 } else if (preSymbol == ExprESymbol.RPAREN) {
                                    errMsg("Unexpected identifier \""+yytext()+"\". Expect \";\" or nothing here.", yyline+1, yycolumn+1);
                                    debugMsg("Ignore identifier \""+yytext()+"\".");
                                    return symbol(ExprESymbol.NOP, preSymbol);
                                 }
                                 */
                                 return symbol(ExprESymbol.IDENTIFIER, yytext());
          }
        case 23: break;
        case 9: 
          { return symbol(ExprESymbol.PLUS);
          }
        case 24: break;
        default: 
          if (zzInput == YYEOF && zzStartRead == zzCurrentPos) {
            zzAtEOF = true;
            zzDoEOF();
              {
                return symbol(ExprESymbol.EOF);
              }
          } 
          else {
            zzScanError(ZZ_NO_MATCH);
          }
      }
    }
  }

  /**
   * Converts an int token code into the name of the
   * token by reflection on the cup symbol class/interface ExprESymbol
   *
   * This code was contributed by Karl Meissner <meissnersd@yahoo.com>
   */
  private String getTokenName(int token) {
    try {
      java.lang.reflect.Field [] classFields = ExprESymbol.class.getFields();
      for (int i = 0; i < classFields.length; i++) {
        if (classFields[i].getInt(null) == token) {
          return classFields[i].getName();
        }
      }
    } catch (Exception e) {
      e.printStackTrace(System.err);
    }

    return "UNKNOWN TOKEN";
  }

  /**
   * Same as next_token but also prints the token to standard out
   * for debugging.
   *
   * This code was contributed by Karl Meissner <meissnersd@yahoo.com>
   */
  public java_cup.runtime.Symbol debug_next_token() throws java.io.IOException {
    java_cup.runtime.Symbol s = next_token();
    System.out.println( "line:" + (yyline+1) + " col:" + (yycolumn+1) + " --"+ yytext() + "--" + getTokenName(s.sym) + "--");
    return s;
  }

  /**
   * Runs the scanner on input files.
   *
   * This main method is the debugging routine for the scanner.
   * It prints debugging information about each returned token to
   * System.out until the end of file is reached, or an error occured.
   *
   * @param argv   the command line, contains the filenames to run
   *               the scanner on.
   */
  public static void main(String argv[]) {
    if (argv.length == 0) {
      System.out.println("Usage : java ExprELexer <inputfile>");
    }
    else {
      for (int i = 0; i < argv.length; i++) {
        ExprELexer scanner = null;
        try {
          scanner = new ExprELexer( new java.io.FileReader(argv[i]) );
          while ( !scanner.zzAtEOF ) scanner.debug_next_token();
        }
        catch (java.io.FileNotFoundException e) {
          System.out.println("File not found : \""+argv[i]+"\"");
        }
        catch (java.io.IOException e) {
          System.out.println("IO error scanning file \""+argv[i]+"\"");
          System.out.println(e);
        }
        catch (Exception e) {
          System.out.println("Unexpected exception:");
          e.printStackTrace();
        }
      }
    }
  }


}

⌨️ 快捷键说明

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