%{ import java.io.*; %} %token PLUS TIMES INT CR RPAREN LPAREN %% lines : lines line | line ; line : expr CR ; expr : expr PLUS term | term ; term : term TIMES factor | factor ; factor: LPAREN expr RPAREN | INT ; %% /* Byacc/J expects a member method int yylex(). We need to provide one through this mechanism. See the jflex manual for more information. */ /* reference to the lexer object */ private scanner lexer; /* interface to the lexer */ private int yylex() { int retVal = -1; try { retVal = lexer.yylex(); } catch (IOException e) { System.err.println("IO Error:" + e); } return retVal; } /* error reporting */ public void yyerror (String error) { System.err.println("Error : " + error + " at line " + lexer.getLine()); System.err.println("String rejected"); } /* constructor taking in File Input */ public Parser (Reader r) { lexer = new scanner (r, this); } public static void main (String [] args) throws IOException { Parser yyparser = new Parser(new FileReader(args[0])); yyparser.yyparse(); }