%{ import java.io.*; /* All of the below productions that do not have associated actions are using the DEFAULT action -- $$ = $1 */ %} %token PLUS TIMES INT CR RPAREN LPAREN %% lines : lines line | line ; line : expr CR {System.out.println($1.ival);} ; expr : expr PLUS term {$$ = new ParserVal($1.ival + $3.ival); } | term ; term : term TIMES factor {$$ = new ParserVal($1.ival * $3.ival); } | factor ; factor : LPAREN expr RPAREN {$$ = new ParserVal($2.ival);} | 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()); } /* 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(); }