[antlr-interest] TreeAdaptor

Terence Parr parrt at cs.usfca.edu
Mon Aug 14 16:11:04 PDT 2006


On Aug 14, 2006, at 2:01 PM, Sean Walton wrote:

> Terence Parr wrote:
>>
>> On Aug 14, 2006, at 5:41 AM, Sean Walton wrote:
>>>> doh!  Changed to:
>>>>
>>>>     /** Get a child 0..n-1 node */
>>>>     public Object getChild(Object t, int i);
>>>>
>>>>     /** How many children?  If 0, then this is a leaf node */
>>>>     public int getChildCount(Object t);
>>>>
>>>> Change 2870.
>>>>
>>>> Ter
>>>>
>>> You know, this wasn't necessary.  The 'getChild()' and  
>>> 'getChildCount()' methods simply *used to* operate on the current  
>>> node, because the child can be a parent to a different child.   
>>> The new method looks more like a static call, because it is now  
>>> independent (i.e., 'static') from the current node, resolving  
>>> getChild() through 't' and not the current instance.  If that is  
>>> the case, it is a more confusing interface than the first.
>>
>> Hi Sean.  There is no such thing as "current node".  To what do  
>> you refer?
>>
>> Ter
> (Sorry I can't cc the list; I'm at home and the list does not  
> recognize the source ebox.)
> The current node is the one that you get from this code:
>            nesc_Program_return t = g.nesc_Program();
>            Object tree = t.getTree();
> 'tree' points to the "current node" or in other words the head of  
> the tree.  As the program walks over the tree recursively (or even  
> iteratively), 'tree' will always point to the current instance.   
> Getting the child or the child count is simple and IMO elegant as  
> it was.  Adding a reference to the head of the tree (Object t)  
> obviates the need of instance information (referring to OOAD),  
> therefore it is independent from the instance, and therefore, it is  
> more of a static behavior.
> Unless I am missing something very obvious, I prefer the original  
> form, because it is simpler than the new version (and even more  
> elegant than V2).

Hi Sean,

ANTLR  Does not know anything about the node types; they are just  
plain objects. you can in your actions of course always reference  
t.getChild()  but during tree parsing ANTLR cannot know how to  
navigate your trees.  It must ask your adapter how to get children.

Terence


More information about the antlr-interest mailing list