[antlr-interest] CommonTree & Tree grammar versus DIY

Andy Tripp antlr at jazillian.com
Thu Aug 21 13:20:12 PDT 2008


Terence Parr wrote:
 
> Yep.  I'm now in favor of manipulating the text instead and  
> regenerating the AST for the altered position. Tree manipulation is  
> fraught with danger

That seems to me like a very odd thing to say.
Manipulating a tree data structure is inherently no more dangerous than
manipulating any other kind of data structure.

The danger arises only when the tools you're using make the implicit
assumption that the tree is fairly static.
It's perfectly natural for a translator to manipulate a tree
(unless it's so trivial that it doesn't need to).

Imagine a program where every time a List of Person object is changed
(items added, deleted, changed), you have to specify the "shape" of the
List (how many items, each item's type, etc). That would be
silly - the program changes that List lots of times, in lots of ways.
Specifying the list "shape" at each step would be silly.

Now imagine the same program working on a tree rather than a list.
Same situation: the program changes the shape of the tree lots of
times in lots of ways, and
having to specify the "shape" of it after every change would be silly.
That's the situation when trying to use treewalkers in a translator.
It's just that the silliness isn't apparent until you get beyond 
just a couple of valid tree "shapes".

...in my opinion,
Andy



More information about the antlr-interest mailing list