[antlr-interest] CommonTree & Tree grammar versus DIY

Andy Tripp antlr at jazillian.com
Thu Aug 21 15:33:16 PDT 2008


Terence Parr wrote:
> 
> On Aug 21, 2008, at 1:20 PM, Andy Tripp wrote:
> 
>> 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.
> 
> Apparently you've never made a cycle in a tree before ;)  
> Recursive/self-similar data structures *ARE* more difficult to alter 
> properly than a List; by hand anyway.
Hmm. I guess I just don't see it.
A List of Person objects doesn't get much harder when the Person class
has a "Person mom" field. Sure, you can accidentally create a cycle, but
you can also accidentally make the same mistake with a List (e.g.
having a "next" pointer in a linked list point somewhere invalid, like
to itself).
  


> For lang X to X, morphing trees 
> is ok but altering language makes each new pass have to match a new kind 
> of tree. 

No, it doesn't. There is no "new kind of tree" - it stays a tree data structure
always. It's the ANTLR treewalker implementation that makes it seem like there's
"something new" going on. The "shape" of the tree is changing, just as any
other data structure's "shape" changes over time. It's the ANTLR treewalker
forcing you to provide a "snapshot" of that evolving shape that's making
it seem like the tree is somehow now of "a new kind of tree".

> lang X to Y is not so good as a result.
> 
>> 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".
> 
> That's why I said I am liking the text rewrite approach; the approach 
> you are using, no?

Yes.

> 
> Ter
> 



More information about the antlr-interest mailing list