[antlr-interest] Tree Evaluation with Logical Operators (AND and OR)
Oliver Zeigermann
oliver.zeigermann at gmail.com
Thu Jan 22 05:06:52 PST 2009
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