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?
I am persistent guy, and this idea of using my custom nodes in one step was bothering me… 😉
So, I did. The crucial points are:
putting
EOF!at the end of the “main” rule,when labeling the tokens, putting labels next to token, not to group, so
(op='*'|op='/'), notop=('*'|'/')I don’t know for sure if this approach of using grammar rules to create immediately custom nodes will be a good a idea, but since this solves the problem asked in question I am marking this as solution.
And for the record, the most interesting rule looks now like this: