[antlr-interest] Re: Manually creating tree - infinite loop

lgcraymer lgc at mail1.jpl.nasa.gov
Thu Nov 6 15:33:29 PST 2003


Adding to Monty's comment:  What you encountered is a "feature" of 
ANTLR 2.x.  ! on a node says "don't incorporate into a tree"; without 
that, ANTLR goes ahead and inserts the node into the tree being built. 
 When you then use it for construction in an action, the child and 
sibling pointers have been set and that can cause problems as in your 
example--ANTLR chases pointers until it can find a place to insert 
the node, and you can get the infinite loop behavior that you saw.

This may get fixed in ANTLR 3.

--Loring


--- In antlr-interest at yahoogroups.com, mzukowski at y... wrote:
> I can't really follow what rule was getting you into trouble, but 
infinite
> loops are usually caused because people forget that trees and nodes 
are the
> same object.  A node contains the down and right pointers.  So if 
you were
> to do something like #(a1, a2, a1) you would create an infinite 
loop.  Often
> people forget to copy a node if they want something like the above.
> 
> Monty
> 
> -----Original Message-----
> From: Edward Povazan [mailto:epovazan at t...] 
> Sent: Thursday, November 06, 2003 2:17 PM
> To: antlr-interest
> Subject: [antlr-interest] Manually creating tree - infinite loop
> 
> Hello,
> 
> I am not too familiar with manipulating trees, and I had an infinite 
loop. I
> 'solved' it using ! to exclude the nodes from automatic tree 
construction,
> but I still and not sure of things.
> 
> One rule in my grammar is:
> rel_op:
> add_op (GT^ add_op)*
> ;
> 
> I allow things like:
> 1 > 2 > 3
> and I would like to convert the above to
> 1 > 2 and 2 > 3
> so that it is easier to use in my tree parser for code generation.
> 
> The first case creates a tree like
> #(> #(> 1 2) 3)
> and I want
> #(AND #(> 1 2) #(> 2 3))
> 
> I broke things down like this so I could build my own tree:
> // add_op (GT add_op (GT add_op)*)?
> 
> a1:add_op
> (
> gt1:GT! a2:add_op!
> { ## = #(#gt1, #a1, #a2); }
> (
> gt2:GT! a3:add_op!
> { ## = #([AND, "and"], ##, #(#gt2, a2, a3)); #a2 = #a3; }
> )*
> )?
> ;
> 
> If I don't include the ! above, things loop forever in line 340 of
> ASTFactory.java:
> // Chase tail to last sibling
> while (tail.getNextSibling() != null) {
> tail = tail.getNextSibling();
> }
> Is this correct behaviour? I know not including ! adds redundant 
tree nodes
> which are discarded in my own tree building, but just wondering if 
this
> should cause infinite loops.
> 
> Also, I end up with a tree like
> #(#(AND #(> 1 2 3) #(> 2 3))
> and I don't see why. When is 3 being added to ## before I add it as 
a child
> to AND.
> 
> And lastly, is there a better way to do this? Are there docs to tree 
stuff
> like this - I found info on ## by searching the lists/internet, but 
not in
> the official docs. Did I miss them?
> 
> Thanks for any info,
> -Ed
> 
> 
>  
> 
> Your use of Yahoo! Groups is subject to 
http://docs.yahoo.com/info/terms/


 

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




More information about the antlr-interest mailing list