[antlr-interest] Re: AST algorithm stuck in infinite loop?

Matt Benson gudnabrsam at yahoo.com
Tue May 6 07:20:25 PDT 2003


Everybody, if the original goal was to read a Java
source file and output the names of the classes in it,
and their arguments, wouldn't it be just as easy to
modify java.tree.g to store or print the desired
information as it walks the tree?  If this weren't for
educational purposes, of course, it would be much
easier to just run javadoc...

-Matt

--- jw9315 <jw9315 at bris.ac.uk> wrote:
> Apologies, the algorithm was not stuck in a loop, it
> was just taking 
> so long to complete that I did not notice! It still
> seems to go 
> through the same branch many times though, I was
> wondering if it was 
> meant to do this, or it was an error in the
> implementation of the 
> algorithm?
> Thanks,
> Jon
> 
> --- In antlr-interest at yahoogroups.com, "jw9315"
> <jw9315 at b...> wrote:
> > Hi,
> > I implemented the following algorithm to walk an
> AST as suggested 
> by 
> > a member of this group:
> > 
> > // Start
> > void visit( AST tree )
> >  {
> >          AST child = tree.getFirstChild();
> >          System.out.println("_" + child);
> >          // Test to see if node has a child
> >          if( child != null) 
> > 	 {
> > 		 System.out.println(child);
> >                  // Call method recursively
> > 		 visit( child ); 
> > 	 }
> >          // Else there were no children
> >          AST sibling = tree.getNextSibling();
> >          while( sibling != null)
> > 	 {
> > 	 	System.out.println(sibling);
> > 		visit( sibling );
> > 		sibling = sibling .getNextSibling();
> >  }
> > // End
> > 
> > However, I find that this enters an infinite loop
> and prints the 
> tree 
> > out over and over again. Have I implemented the
> algorithm 
> correctly, 
> > and if so, could someone tell me how to test for
> the fact that I 
> have 
> > walked the whole tree? Is there a special command
> for this?
> > Thanks in advance,
> > Jon
> 
> 
>  
> 
> Your use of Yahoo! Groups is subject to
> http://docs.yahoo.com/info/terms/ 
> 
> 


__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com

 

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




More information about the antlr-interest mailing list