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

Eran Werner ewerner at idc.ac.il
Tue Mar 1 11:12:36 PST 2005


I managed to find some sort of solution by catching the semantic exception
thrown if the predicate is not true. So my code looks like this:

eval returns [bool result]
    :

      #(OR  result=eval 

         {result==false}? result=eval
	)

    | TRUE_  {result = 1;}
    | FALSE_ {result = 0;}
    exception 
    catch [ANTLR_USE_NAMESPACE(antlr)SemanticException ex] {}


but this is not more than stifling the exception. There must be a better way

Eran 



    ;-----Original Message-----
From: Bryan Ewbank [mailto:ewbank at gmail.com]
Sent: Tuesday, March 01, 2005 7:58 PM
To: ANTLR Interest
Subject: Re: [antlr-interest] Re: Adding parenthesis to the calc grammer

Eran pointed out that my code didn't work.  I'd missed part of the
discussion of semantic predicates.

To accept the tree, but not evaluate it, you must provide an alternate that
eats the tree but bypasses evaluation of that tree:

eval returns [bool result]
    :
        #(AND
            result = eval
            (
                {result==true}? result=eval
            |
                . // skip processing
            )
        )
    |
        #(OR
            result = eval
            (
                {result==false}? result=eval
            |
                . // skip processing
            )
        )
    | TRUE_  {result = 1;}
    | FALSE_ {result = 0;}
    ;



More information about the antlr-interest mailing list