[antlr-interest] Re: java.tree.g: A way to get parent of primaryExpression?

micheal_jor open.zone at virgin.net
Sat May 17 06:43:10 PDT 2003


--- In antlr-interest at yahoogroups.com, "rustydstone" 
<rustydstone at y...> wrote:
> When calculating primary expression, I need somehow to determine the
> immediate parent of primary expression. In other words, primary
> expression will be calculated differently if the parent is, e.g.,
> unary minus or regular minus.

My immediate reaction is to ask "Why?". Why does the calculation of 
the sub-expression of a UNARY_MINUS have to be different from the 
calculation of the sub-expressions of a MINUS?

They shouldn't be different AFAICT, you should add action code to the 
sub-rules for MINUS and UNARY_MINUS to perform the relevant operation 
on the results of their subexpression(s).


However...

If you want to pass parameters to (and return values from) ANTLR's 
rules, you could do something like this:

expr [IParent parent] returns [IResult result]
{
   IResult expr1 = null;
   IResult expr2 = null;
}
[...]
  |    #(MINUS expr1=expr[parent] expr2=expr[parent]
       { result = expr1.binaryMinus(expr2);  }
[...]
  |    #(UNARY_MINUS expr1=expr[parent])
       { result = expr1.unaryMinus();  }
[...]
  |    result=primaryExpression[parent]
  ;

Cheers,

Micheal


> Any ideas? Thanks
> 
> --------------------------------------------------
> 
> expr:	#(QUESTION expr expr expr)
> [...]
>   |     #(MINUS expr expr)
> [...]
>   |     #(UNARY_MINUS expr)
>   |     #(UNARY_PLUS expr)
>   |     primaryExpression
> ;


 

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




More information about the antlr-interest mailing list