I already know the workaround for this problem, but I would like to really use this one approach, for at least one reason -- it should work.
This is rule taken from "The Definitive ANTLR Reference" by Terence Parr (the books is for ANTLR3):
expr : (INT -> INT) ('+' i=INT -> ^('+' $expr $i) )*;
If INT
is not followed by +
the result will be INT (single node), if it is -- subtree will be built with first INT
(referred as $expr
) as left branch.
I would like to build similar rule, yet with custom action:
mult_expr : (pow_expr -> pow_expr )
(op=MUL exr=pow_expr
-> { new BinExpr($op,$mult_expr.tree,$exr.tree) })*;
ANTLR accepts such rule, but when I run my parser with input (for example) "5 * 3" it gives me an error "line 1:1 missing EOF at '*'5".
QUESTION: how to use back reference with custom rewrite action?