[antlr-interest] Tree Rewrite Caveats

Robin Hamilton-Pennell robin at pozytron.com
Fri May 23 20:21:06 PDT 2008


Hi all,

Been playing around with the tree rewriting stuff in 3.1b1. It's much
better than the previous test build was. I'm not through with my
experimentation and likely will find more things, but I thought I'd
make some notes on a few troubles I ran into, in case it helps anyone
else.

Firstly (this is probably true of tree parsers in general), if you use
a custom class as your AST node and the Java target, you will probably
need to add the method errorNode() in your TreeAdaptor class. This
will also entail either adding error node utility methods to your AST
node, or creating a custom subclass of CommonErrorNode. I could be
completely misguided on this; please say so.

Second, every (sub)rule that you are rewriting must match a tree node,
I think. This seems obvious in retrospect, but it took me a while to
figure out. That means that you can't do this:

stmt:
    expr   ->   expr
    ;

However, you can do either of the following (or change your AST
generation code):

stmt:
    expr  // omit rewrite rule
    ;

stmt:
    // (after changing your AST generation)
    ^(EXPR expr)   ->   ^(EXPR expr)
    ;

I should mention that both of these issues involves the rewrite=true
option. I haven't tested the effects of turning that off, actually.

Again, I could simply be completely misguided on this, in which case,
please enlighten me.

Thanks,
Robin


More information about the antlr-interest mailing list