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

shmuel siegel antlr at shmuelhome.mine.nu
Thu Jan 22 04:38:34 PST 2009


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