class Walker extends TreeParser; options { buildAST=true; importVocab=P; // ASTLabelType = "MyAST"; } tokens { EXPR="expr"; } prog : stmts ; stmts : #(BLOCK {setASTNodeType("MyAST");} stmtList) ; stmtList : stmt stmtList | ; stmt : #(ASSIGN id expr) { // checking goes here } | #(IFTHEN expr stmts) | #(READ id ) | #(WRITE expr) ; expr : #(EQ expr expr) | #(NEQ expr expr) |! #(op:PLUS left:expr right:expr) {//System.out.println("Creating node for + expr"); #expr = #(#[EXPR,"expr"], op, left, right); MyAST _expr = (MyAST)#expr, _left = (MyAST)#left, _right = (MyAST)#right; _expr.setExprType(10 + _left.getExprType() + _right.getExprType()); } | #(MINUS expr expr) | ! #(t_op:TIMES t_left:expr t_right:expr) {//System.out.println("Creating node for + expr"); #expr = #(#[EXPR,"expr"], t_op, t_left, t_right); MyAST _expr = (MyAST)#expr, _left = (MyAST)#t_left, _right = (MyAST)#t_right; _expr.setExprType(20 + _left.getExprType() +_right.getExprType()); } | #(DIVIDE expr expr) // : MINUS^ (primary) | id | constant ; id : i:ID {MyAST t= new MyAST(); t.initialize(i); t.setExprType(2); #id = t; } ; constant : n:NUM {MyAST t= new MyAST(); t.initialize(n); t.setExprType(1); #constant = t; } ;