[antlr-interest] loose tree walker?
Monty Zukowski
monty at codetransform.com
Mon May 2 10:57:41 PDT 2005
Bryan Ewbank wrote:
> 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?
>
Either will work. I prefer the former mysql because it reminds me of
the elif rules that can be removed in the next pass, and because I like
to think about things in a structured way, the latter I consider to be
more like search/replace. When you have more grammatical context
deciding your translations you will be forced to use the former style.
Monty
More information about the antlr-interest
mailing list