%{ import java.io.*; import java.util.*; import java.text.*; %} %token BEGIN N S E W %% grid : seq {System.out.println("Done: " + $1.ival1 + " " + $1.ival2);} seq : seq instr {$$.ival1 = $1.ival1 + $2.ival1; $$.ival2 = $1.ival2 + $2.ival2;} | BEGIN ; instr : N | S | E | W ; %% public static final class Semantic { public int ival1 ; public int ival2 ; public Semantic(Semantic sem) { ival1 = sem.ival1; ival2 = sem.ival2; } public Semantic(int i1,int i2) { ival1 = i1; ival2 = i2; } public Semantic() { ival1=0;ival2=0;} } /* 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 */ /* 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 InputStreamReader(System.in)); yyparser.yyparse(); } public void yyerror(String error) { System.err.println("Error : " + error + " at line " + lexer.getLine() ); }