[antlr-interest] Tree Evaluation with Logical Operators (AND and OR)
Jim Idle
jimi at temporal-wave.com
Thu Jan 22 09:16:11 PST 2009
>
> 2009/1/22 Priolo, Scott <spriolo at walkerinfo.com>:
>
>> Sorry about that, I'm trying to setup a walker that will know what to do
>> with "OR" and "AND" operators. I have the parser setup so that the AST is
>> nicely formed with expr such as (< a 3). I'm able to walk and evaluate
>> these simple expr too. But! when I walk (or (and (< a 3) (< b 3)) (< c 3))
>> I'm stumped.
>>
You need to make your rules within an expression return a type, the
formulation of which depends on what things you can evaluate. If your
primitive types are all integer, then each stage of your expression can
return integer and the AND and OR rules can return 0 and 1. Otherwise,
you will need to create a class with 'operators' such as plus(),
minus(), and() etc. It will also contain an integer, perhaps a boolean,
perhaps a String and so on. Before applying your operations, you will
need to coerce the types according to some orthogonal rules, as in if
you have ^(AND INT STRING), what does that mean to you - do you convert
the string to an integer, or does it mean "is not null" for instance,
and what do want to do with ^(AND INT STRING) vs ^(AND STRING INT)
(promotion rules, diamonds and so on).
If it is just Integer though, then:
if: ^(IF e=expr ....) { if ($e.intVal != 0) { ....
expr
returns [int intVal]
:
: i=INT { intVal = stringtoint($i.text); ... some conversion func
: ^(AND e1=expr e2=expr) { if (stringtoint($e1.text) !=0 &&
stringtoint($e2.text) != 0) { intVal = 1; }....
.... and so on.
Jim
More information about the antlr-interest
mailing list