[antlr-interest] loose tree walker?

Bryan Ewbank ewbank at gmail.com
Mon May 2 13:49:13 PDT 2005


I'm seeking guidance and information. Let's say I'm doing a tree
transformation to replace "elif" with "if" everywhere it occurs.

I can either be very precise and walk the entire tree as it is
supposed to be formed now:
    prog : ( stmt )* ;
    stmt : ( stmt_if | stmt_for | ... ) ;
    stmt_if : #(IF expr stmt ( stmt_elif | stmt )? );
    stmt_elif #(ELIF expr stmt ( stmt )? ) { ... convert elif to if ... } ;
    // omitting expr and the rest of the grammar

Or I can do a loose tree walker, like this:

    traverseKids : ( traverseTree )* ;
    traverseTree : ( (ELIF) => stmt_elif | #( . traverseKids ) );
    stmt_elif : #(ELIF traverseKids ) { ... convert elif to if ... } ;

The second has some appeal to me simply because it removes the rest of
the tree and the dependencies on how that tree is shaped.

what do others do?


More information about the antlr-interest mailing list