1 5 6 7
(PLUS 5 6 7)
2 5 6 7
(MINUS 5 6 7)
grammar TreeConstruct; options { output=AST; } tokens { PLUS; MINUS; MUL; DIV; } prog @init { int opid=1; } : OP {opid=Integer.parseInt($OP.text);} (DIGIT)+ -> {opid==1}? ^(PLUS DIGIT+) -> {opid==2}? ^(MINUS DIGIT+) -> {opid==3}? ^(MUL DIGIT+) -> {opid==4}? ^(DIV DIGIT+) -> ; OP: '1' .. '4'; DIGIT: '0' .. '9' ; WS: (' '|'\r'|'\n') {$channel=HIDDEN;};