[antlr-interest] Workarounding multiple alternatives

nafur nafur42 at gmail.com
Thu May 10 07:53:11 PDT 2012


I'm trying to use tree grammars to implement several replacement rules.
My AST represents mathematical formulas and I want to do things like
x * x^-1  ->  I (replace "x" and "x inverse" by identity)

Obviously, I want to replace such a pattern within any (longer)

My current approach (this one for orthogonal matrices) looks as follows:

    : ^( MULT (before += .)* x=. ^( POW y=. TRANS ) (after += .)* )
        { $x.equals($y) && $x.has("ORTHOGONAL") }?
            -> ^( MULT $before? SYMBOL["I"] $after? )

Sure enough, antlr complains about the nondeterministic decision (before
and after can both take arbitrarily many nodes), but I have no idea left
how to fix this.
Can you give me some hint, how this could be implemented?


More information about the antlr-interest mailing list