[antlr-interest] Workarounding multiple alternatives

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


Hi,

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)
multiplication.

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

topdown
    : ^( 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?

Thanks,
Gereon


More information about the antlr-interest mailing list