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 <[email protected]>: > 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:[email protected]] > Sent: Thursday, January 22, 2009 7:39 AM > To: Oliver Zeigermann > Cc: Priolo, Scott; [email protected] > 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. > > List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "il-antlr-interest" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en -~----------~----~----~----~------~----~------~--~---
