[antlr-interest] Multiple pass tree walking Q

Andy Tripp antlr at jazillian.com
Wed Oct 4 13:28:36 PDT 2006


Hill, Robert wrote:

>Yay - a Reply! I was thinking my mails weren't getting through for a
>minute there ;)
>
>Hmm, I considered this originally, but I think I only need 3 passes, so
>while it's a big hassle, using grammars has take out a huge wodge of
>time in getting it up and running , whereas I feel that walking the
>tree's manually would probably have taken a fair bit longer. 
>
Things can't get any simpler than walking the tree by hand:

void walk(AST ast) {
  // do stuff
  AST child = ast.getFirstChild();
  while (child != null) {
     walk(child);
     child = child.getNextSibling();
  }
}

I'm using an old version of ANTLR. By now, it's probably:
void walk(AST ast) {
// do stuff
for (AST child: ast.getChildren()) {
   walk(child);
}
}
 



More information about the antlr-interest mailing list