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

Eran Werner ewerner at idc.ac.il
Mon Feb 21 07:51:27 PST 2005


 

HI 

I am trying to define a grammer for evaluation of Boolean Expressions.
Basically I used the calc example and replaced the addition and
multiplication operators

With AND, and OR operations. 

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. 

 

I define the .g grammer as 

 

:#(AND left = eval right = eval) {result = (left && right);}


|#(OR  left = eval right = eval) {result = (left || right);}


 

But the generated code is 

 

case AND:

            {

                  ANTLR_USE_NAMESPACE(antlr)RefAST __t40 = _t;

                  ANTLR_USE_NAMESPACE(antlr)RefAST tmp1_AST_in = _t;

                  match(_t,AND);

                  _t = _t->getFirstChild();

                  left=eval(_t);

                  _t = _retTree;

                  right=eval(_t);

                  _t = _retTree;

                  _t = __t40;

                  _t = _t->getNextSibling();

                  result = (left && right);

                  break;

            }

 

 

So I can not avoid the recursive evaluation of the rightChild, even in the
case that the leftChild returns false.

 

Any Suggestions…

 

Eran

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20050221/fbce4b79/attachment.html


More information about the antlr-interest mailing list