Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] AST traversal

Yes I could do this in a general pattern. If the ASTNode.accept() is like the following, I can do both top-down and bottom-up traversal in the current framework:
 
currentNode.accept(){
    ASTVisitor.visit();
    for_each_children do
        childrenNode.accept();
    end for_each_do
    ASTVisitor.leave();
}
 
"leave()" is an abstract method in ASTVisitor, just as visit(). It is called AFTER all children (subtrees) are traversed, so implementing leave() gives me a bottom-up traversal, and implementing visit() gives me a top-down traversal. In the bottom-up traversal, I don't need to care how to get the leave nodes, the accept() can go all the way down to the leave nodes and "piggyback" information back up (by calling the leave() method).
 
I think this framework is very clean. But we have to do two important changes:
(1) For all subclasses of ASTNode, change their accept() method.
(2) For all subclasses of ASTVisitor, implement the leave() method.
 
Actually I haven't found any elegant way to implement a bottom-up traversal in the current framework. And I know the above method is a big change ...
 
Thanks,
 
Yuan
 


 
On 7/9/06, Doug Schaefer <DSchaefer@xxxxxxx> wrote:

As I mentioned the mechanisms are there to traverse the tree anyway you want to. You should be able to do what you need without changing current code. I assume the information you are passing up to the parent is specific to your application so I'm not sure you can do something like this using a general pattern such as the visitor pattern anyway.

 

Doug Schaefer, QNX Software Systems

Eclipse CDT Project Lead, Tools PMC member

http://cdtdoug.blogspot.com

 


From: cdt-dev-bounces@xxxxxxxxxxx [mailto: cdt-dev-bounces@xxxxxxxxxxx] On Behalf Of Yuan Zhang
Sent: Sunday, July 09, 2006 9:36 AM
To: CDT General developers list.
Subject: Re: [cdt-dev] AST traversal

 

The current ASTNode.accept() method first visit the current node, then visit its children. What I need is to visit Children first, collect some information from them, and feedback to their parent. This traversal is bottom-up because leaves nodes are visted before the root node. This can be achieved by calling, say  ASTVisitor.leave(), upon leaving the ASTNode.accept(). But I am afraid all subclasses of ASTVisitor have to be changed. So I am wondering whether I can implement this bottom-up traversal in some other way without changing the current code?

 

Thanks,

 

Yuan 

 

On 7/8/06, Doug Schaefer < DSchaefer@xxxxxxx> wrote:

Not sure what you mean by bottom up. Certainly you can navigate from a child node to its parent by following the getParent links. You can also tell what property you are in the parent by looking at getPropertyInParent. Not sure what else you would need.

 

Doug Schaefer, QNX Software Systems

Eclipse CDT Project Lead, Tools PMC member

http://cdtdoug.blogspot.com

 


From: cdt-dev-bounces@xxxxxxxxxxx [mailto: cdt-dev-bounces@xxxxxxxxxxx] On Behalf Of Yuan Zhang
Sent: Saturday, July 08, 2006 5:08 PM
To: cdt-dev@xxxxxxxxxxx
Subject: [cdt-dev] AST traversal

 

Hi,

 

I noticed that ASTNode.accept() and ASTVisitor.visit() together provide a top-down traversal of the AST. I am wondering whether there is a way to traverse the AST from bottom-up without modifying the current code? I have thought to associate each node a "property", and during the traversal change the parent node's property of the currently visited node. However, I cannot find any appropriate place to store this "property". Anybody can give me some hint?

 

Thanks,

 

Yuan 


_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/cdt-dev

 


_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/cdt-dev




Back to the top