[antlr-interest] Re: Adding parenthesis to the calc grammer

Bryan Ewbank ewbank at gmail.com
Mon Feb 21 23:43:45 PST 2005


To get short-circuit evaluation, you need to use semantic predicates. 
In ANTLR, a semantic predicate uses "{" and "}?", and must evaluate to
a boolean value (which is handy for you :-)...

eval returns [bool result]
    : #(AND result=eval ( {result==true}?  result=eval )? )
    | #(OR  result=eval ( {result==false}? result=eval )? )
    ;

The semantic predicates prevent descending into the RHS unless
evaluation is required.

On Mon, 21 Feb 2005 17:51:27 +0200, Eran Werner <ewerner at idc.ac.il> wrote:
> My problem is that I wish to optimize the evaluation so that if the left
> hand side of an AND statement is false, ANTLR will not evaluate the right
> hand side, and similarly for OR operations.


More information about the antlr-interest mailing list