[antlr-interest] Re: Building trees with the correct associativity

lgcraymer lgc at mail1.jpl.nasa.gov
Wed Dec 1 18:34:50 PST 2004



Paul--

What you want is something on the order of

addExpr:
    :
    mulExpr (addOp mulExpr)*
    ;

or

addExpr
    :
    mulExpr (addOp addExpr)*
    ;

What happens with your grammar is that 2+3 is recognized in e2 as an
addExpr within a mulExpr.

--Loring


--- In antlr-interest at yahoogroups.com, "Paul J. Lucas"
<pauljlucas at m...> wrote:
> 	I'm surprised it took me this long to notice I had a problem
> 	with this, but...
> 
> 	If I have:
> 
> 		addExpr
> 		    : e1:mulExpr
> 		      (ao:addOp! e2:mulExpr!
> 		      	{
> 			    ## = #([ADD_EXPR,"ADD_EXPR"], ao, e1, e2);
> 			}
> 		      )*
> 		    ;
> 
> 	then my trees get built the wrong way, e.g.:
> 
> 		5 - 2 + 3
> 
> 	is as if it were:
> 
> 		5 - (2 + 3)
> 
> 	which is wrong.  Maybe I'm being a but slow, but it's not
> 	immediately clear how to build the tree the other way, i.e.:
> 
> 		(5 - 2) + 3
> 
> 	Help.
> 
> 	- Paul





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

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





More information about the antlr-interest mailing list