[antlr-interest] Tree Evaluation with Logical Operators (AND and OR)

Priolo, Scott spriolo at walkerinfo.com
Thu Jan 22 05:23:22 PST 2009


I see the recursion now.  Thanks for point that out.

It just dawned on me that I'm trying to "set" the flag isLowScore on the
elements which cause the Boolean expression to be false.

a=1
b=3
c=1

So in the expression (a < 3) or (b < 3) or ((b = 3) AND (c < 3))

I need to mark b and c as the elements that caused the expression to be
false.  I can't figure out the action of the AND.

Thanks for your help.

-----Original Message-----
From: Oliver Zeigermann [mailto:oliver.zeigermann at gmail.com] 
Sent: Thursday, January 22, 2009 8:07 AM
To: Priolo, Scott
Cc: shmuel siegel; antlr-interest at antlr.org
Subject: Re: [antlr-interest] Tree Evaluation with Logical Operators
(AND and OR)

That looks good. What is the problem with it?

Note that both precedence and part/whole relation is composed into the
tree structure.

-Oliver

2009/1/22 Priolo, Scott <spriolo at walkerinfo.com>:
> This is what I have so far.  ID is converted to an INT. Variable are
> pulled out of a memory map that is preloaded prior to processing.
>
> I will try | ^(AND|OR expr expr) but it seems like there should be
more
> of a recursive nature to "and, or"  operators.
>
> expr returns [int value]
>    :   ^(GT a=expr b=expr)   {
>                               $value = (a>b)?1:0;
>                               le.isLowScore((a>b));
>                               System.out.println("(GT " + a + " " + b
+
> ") bool:" + (a>b));
>                               }
>    |   ^(GTEQ a=expr b=expr) {
>                                $value = (a>=b)?1:0;
>                                le.isLowScore((a>=b));
>                                System.out.println("(GTEQ " + a + " " +
> b + ") bool:" + (a>=b));
>                              }
>    |   ^(LT a=expr b=expr)   {
>                                $value = (a<b)?1:0;
>                                le.isLowScore((a<b));
>                                System.out.println("(LT " + a + " " + b
> + ") bool:" + (a<b));
>                              }
>    |   ^(LTEQ a=expr b=expr) {
>                                $value = (a<=b)?1:0;
>                                le.isLowScore((a<=b));
>                                System.out.println("(LTEQ " + a + " " +
> b + ") bool:" + (a<=b));
>                              }
>    |   ^(EQ a=expr b=expr)   {
>                                $value = (a==b)?1:0;
>                                le.isLowScore((a==b));
>                                System.out.println("(EQ " + a + " " + b
> + ") bool:" + (a==b));
>                              }
>    |   ^(NEQ a=expr b=expr)  {
>                                $value = (a!=b)?1:0;
>                                le.isLowScore((a!=b));
>                                System.out.println("(NEQ " + a + " " +
b
> + ") bool:" + (a!=b));
>                              }
>    |   ^(OR a=expr b=expr)   {
>                                // WHAT TO DO HERE
>                                //System.out.println("(OR " + a + " " +
> b + ") bool:" + (a|b));
>                              }
>    |   ^(AND a=expr b=expr)  {
>                                // WHAT TO DO HERE
>                                //System.out.println("(AND " + a + " "
+
> b + ") bool:" + (a&b));
>                              }
>    |   ID
>        {
>        try {
>                //pull value out of map if this is an id "Q22_B"
>                PgmQuestionsEntry q =
> (PgmQuestionsEntry)variableMap.get(($ID.text).toLowerCase());
>                if ( q!=null ) {
>                        int idvalue = (int) q.getResponseNumber();
>                        $value = idvalue;
>                        System.out.println("=> Parsing ID = " +
$ID.text
> + " value = " + idvalue);
>                } else System.err.println("undefined variable
> "+$ID.text);
>        } catch (Exception e)
>        {
>                e.printStackTrace();
>        }
>        }
>    |   INT
>        // when token matches an INT pattern convert it to int.
>        {$value = Integer.valueOf($INT.text);}
>    ;
>
> -----Original Message-----
> From: shmuel siegel [mailto:antlr at shmuelhome.mine.nu]
> Sent: Thursday, January 22, 2009 7:39 AM
> To: Oliver Zeigermann
> Cc: Priolo, Scott; antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Tree Evaluation with Logical Operators
> (AND and OR)
>
> Oliver Zeigermann wrote:
>> OK, so you need a tree grammar that can parse complex boolean
>> expressions, right?
>>
>> expression
>>   : ^((AND|OR) expression expression)
>>   | ^(('<'|'>'|'='|'!=') expression expression)
>>   | ^(('+'|'-'|'*'|'/') expression expression)
>>   | ID
>>   | literal
>>   ;
>>
>> Would that work?
>>
>> -Oliver
>>
>>
> This grammar is not syntactically restrictive since it allows ^(AND 3
> 4). This will cause problems if you want to evaluate the tree since
you
> won't know the type of the return value of expression. It is better to
> separate out arithmetic and logical expressions. Care must be taken to
> establish if ID is arithmetic or logical.
>
>


More information about the antlr-interest mailing list