[antlr-interest] Re: Algorithm for walking an AST tree??

jw9315 jw9315 at bris.ac.uk
Sun May 4 16:21:47 PDT 2003


Hi,
Is the algorithm mentioned below, 

Preorder Traversal or
Postorder Traversal or
Inorder Traversal?

Thanks,
Jon
> Off the top of my head:
> 
> void visit( AST tree )
> {
>     AST child = tree.getFirstChild();
>     if( child )
>         visit( child )
> 
>     AST sibling = tree.getNextSibling();
>     while( sibling ){
>         visit( sibling );
>         sibling = sibling .getNextSibling();
>     }
> }
> 
> > I feel I should be iterating through the children of each node?
> 
> The above will do a depth-first traversal - depending on what you 
want to
> do, depth-first might not be what you want. The recursiveness of 
visit()
> handles iterating through children, the explicit while-loop 
iterates through
> siblings.
> 
> Ross.


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list