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

Ross Bencina rbencina at iprimus.com.au
Sun May 4 15:11:20 PDT 2003


Jon wrote:
> Hi,
> Could someone help me out with the algorithm for walking an AST tree?

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