0

I'm trying to make a lexical analyzer with jflex and java files but I'm getting an error when I try to compile MphpLex.java I'm using material from a clite analyzer in one of my classes. Below I will post the code, how I'm compiling and the error message. compilation: jflex mphp_lex.jflex this compiles then I use: javac MphpLex.java (or all of the .java files below)

I get the error:

MphpLex.java:14: error: incompatible types: InputStream cannot be converted to Reader MphpLexer lexer = new MphpLexer(System.in);

If anyone could explain this error that would be much appreciated.

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
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Your main method is in `MphpLex`, not your generated `MphpLexer` class. You need to compile all your .java files and run `java MphpLex input1.txt`. (Of course, your program as shown will not actually read the input file, but that's neither here nor there.) – Michael Myers Sep 22 '16 at 18:15
  • Thank you! i had a feeling that was the problem, I was also trying to compile that file by itself and it the only one with an error i cant figure out. – John Alvarez Sep 22 '16 at 18:42
  • `System.in` is an `InputStream`, so you could wrap it in an `InputStreamReader` which you would then pass to the lexer. But you probably should be reading the text file that you are passed on the command line instead of reading the command line itself (which is what `System.in` is). – Michael Myers Sep 22 '16 at 23:06

0 Answers0