[antlr-interest] rewrite rule for optional root insertion?

Krešimir Tonković kresimir.tonkovic at multimodus.hr
Thu Apr 5 05:59:14 PDT 2012


Hi!

I have many rules like this in my grammar:

plus_minus_exp :
    mult_div_exp ('+'^ | '-'^ mult_div_exp)?
;

which produces desired trees:
1+2 -> ^(+ 1 2)
1+2*3 ->^(+ 1 ^(* 2 3))
2*3 -> ^(* 2 3)
for correct operator precedence and no superfluous nodes.

However, I have some cases where tree operators are not enough. I need more
control that rewrite rules provide. I have tried with syntactic predicates,
but I find they hurt the performance of my parser and I would like to avoid
that.

How would I rewrite above rule with rewrite rules and no syntactic
predicates?

Regards,
-- 
Krešimir Tonković
Multimodus d.o.o.


More information about the antlr-interest mailing list