[antlr-interest] translating a tree to another tree

Mark Wright markwright at internode.on.net
Mon Feb 11 20:35:28 PST 2008


On Sun, 10 Feb 2008 20:35:29 +0000
Sérgio Ferreira <sergio at moredata.pt> wrote:

> In the current version what will be the best strategy to change the 
> original AST to a changed AST that could then be used in a tree
> parser to generate code using string template ?

Hello Sérgio,

You can walk the tree manually as described in:

http://jazillian.com/articles/treewalkers.html

Since you are changing the AST, you may not have all the
information available as you visit each node to instantly
generate the new AST.  In which case, you will have to
collect the information first in some other data structures.

Then you can either walk the original AST again, looking up
information collected in the data structures from the first
walk, or walk the data structures, and hand code the
creation of the new AST.

It works great, as you can figure out which information the
string templates need, and calculate it all up front, then
output an AST in the simplest possible format that the
string templates can use.  Its really neat, the string templates
(and the tree parser if you are creating a new AST that is simpler
than the original AST) can be as dumb as possible, while all the smarts
are in the code that analyzes the original AST and data structures.

Regards, Mark

-- 


More information about the antlr-interest mailing list