在java中用我的词法分析器初始化一个类

时间:2016-09-22 18:08:17

标签: java lexical-analysis jflex

我正在尝试使用jflex和java文件创建一个词法分析器但是当我尝试编译MphpLex.java时我遇到了一个错误我正在使用我的一个类中的clite分析器中的材料。下面我将发布代码,我如何编译和错误消息。 汇编: jflex mphp_lex.jflex这个编译然后我使用: javac MphpLex.java(或下面的所有.java文件)

我收到错误:

MphpLex.java:14:错误:不兼容的类型:InputStream无法转换为Reader     MphpLexer lexer = new MphpLexer(System.in);

如果有人能够解释这个错误,那将非常感激。

mphp_lex.jflex:

%%
%{
  private void echo () { System . out . print (yytext ()); }

  public int position () { return yycolumn; }
%}


%class    MphpLexer
%function nextToken
%type     Token
%unicode
%line
%column
%eofval{
  { return new Token (TokenClass . EOF); }
%eofval}


WhiteSpace = [ \t\n]
%%

{WhiteSpace}    {  }

ErrorMessage.java:

public class ErrorMessage {

  public static void print (String message) {
    System . out . println ("***** Error: " + message + " *****");
    System . exit (0);
  }

  public static void print (int position, String message) {
    System . out . println ("");
    for (int i = 0; i < position; i++) 
      System . out . print (" ");
    System . out . println ("^");
    print (message);
  }

}

MphpLex.java:

import java.util.Scanner;

public class MphpLex {

  private static final int MAX_TOKENS = 100;

  public static void main (String args []) throws java.io.IOException {

    int i, n;
    Token [] token = new Token [MAX_TOKENS];
    MphpLexer lexer = new MphpLexer(System.in);

    System . out . println ("Source Program");
    System . out . println ("--------------");
    System . out . println ();

    n = -1;
    do {
      if (n < MAX_TOKENS)
        token [++n] = lexer . nextToken ();
      else
    ErrorMessage . print (0, "Maximum number of tokens exceeded");
    } while (token [n] . symbol () != TokenClass . EOF);

    System . out . println ();
    System . out . println ("List of Tokens");
    System . out . println ("--------------");
    System . out . println ();
    for (i = 0; i < n; i++)
      System . out . println (token [i]);
    System . out . println ();
  }

}

Token.java:

public class Token {

  private TokenClass symbol;    // current token
  private String lexeme;    // lexeme

  public Token () { }

  public Token (TokenClass symbol) {
    this (symbol, null);
  }

  public Token (TokenClass symbol, String lexeme) {
    this . symbol = symbol;
    this . lexeme  = lexeme;
  }

  public TokenClass symbol () { return symbol; }

  public String lexeme () { return lexeme; }

  public String toString () {
    switch (symbol) {
      case BOOL :      return "(keyword, bool) ";
      case CHAR :      return "(keyword, char) ";
      case ELSE :      return "(keyword, else) ";
      case FLOAT :     return "(keyword, float) ";
      case IF :        return "(keyword, if) ";
      case INT :       return "(keyword, int) ";
      case MAIN :      return "(keyword, main) ";
      case WHILE :     return "(keyword, while) ";
      case COMMA :     return "(punctuation, ,) ";
      case SEMICOLON : return "(punctuation, ;) ";
      case LBRACE :    return "(punctuation, {) ";
      case RBRACE :    return "(punctuation, }) ";
      case LPAREN :    return "(operator, () ";
      case RPAREN :    return "(operator, )) ";
      case LBRACK :    return "(operator, [) ";
      case RBRACK :    return "(operator, ]) ";
      case ASSIGN :    return "(operator, =) ";
      case OR :        return "(operator, ||) ";
      case AND :       return "(operator, &&) ";
      case PLUS :      return "(operator, +) ";
      case MINUS :     return "(operator, -) ";
      case TIMES :     return "(operator, *) ";
      case SLASH :     return "(operator, /) ";
      case MOD :       return "(operator, %) ";
      case EQ :        return "(operator, ==) ";
      case NE :        return "(operator, !=) ";
      case LT :        return "(operator, <) ";
      case LE :        return "(operator, <=) ";
      case GT :        return "(operator, >) ";
      case GE :        return "(operator, >=) ";
      case NOT :       return "(operator, !) ";
      case ID :        return "(identifier, " + lexeme + ") ";
      case INTEGER :   return "(integer, " + lexeme + ") ";
      case BOOLEAN :   return "(boolean, " + lexeme + ") ";
      case FLOATLIT :  return "(float, " + lexeme + ") ";
      case CHARLIT :   return "(char, " + lexeme + ") ";
      default : 
    ErrorMessage . print (0, "Unrecognized token");
        return null;
    }
  }

}

TokenClass.java:

public enum TokenClass {
  EOF, 
  // keywords
  BOOL, CHAR, ELSE, FLOAT, IF, INT, MAIN, WHILE,
  // punctuation
  COMMA, SEMICOLON, LBRACE, RBRACE,
  // operators
  LPAREN, RPAREN, LBRACK, RBRACK, ASSIGN, OR, AND, EQ, NE, LT, LE, GT, GE, 
  PLUS, MINUS, TIMES, SLASH, MOD, NOT,
  // ids and literals
  ID, INTEGER, BOOLEAN, FLOATLIT, CHARLIT
}

0 个答案:

没有答案