grammar TreeConstruct; options { output=AST; } tokens { PLUS; MINUS; } prog : first=DIGIT other+=DIGIT* -> ^( { "1".equals($first.text)? new CommonTree(new CommonToken(PLUS, "plus")) : new CommonTree(new CommonToken(MINUS, "minus")) } $other*) ; DIGIT: '0' .. '9' ; WS: (' '|'\r'|'\n') {$channel=HIDDEN;};
1 2 3
(plus 2 3)
2 2 3
(minus 2 3)