[antlr-interest] Evaluation boolean expressions

Christopher Schultz christopher.d.schultz at comcast.net
Fri Jul 29 16:03:39 PDT 2005


Xavier,

I have written an expression evaluator that supports the expression you
asked about, but in a slightly different form.

Yours: (4 + (5*2) > 15) OR (4>1)
Mine:  (4 + (5*2) > 15) || (4>1)

The evaluator I wrote supports all kinds of mathematical expressions, as
well as user-defined functions, etc.

It was developed as part of a home-brewed rules engine for my employer,
and I'm trying to wrestle it out of their hands and get it out to the
Open Source community. I'll post something on this mailing list when I
get that accomplished.

At any rate, this is what I get when I run that expression through my
evaluator (in debugging mode, so you can see the objects created by the
parser):

(4 + (5*2) > 15) || (4>1)      [entered via the keyboard]

Parsed: ( ( ( Constant <java.lang.Integer>4 + ( Constant
<java.lang.Integer>5 * Constant <java.lang.Integer>2 ) ) > Constant
<java.lang.Integer>15 ) || ( Constant <java.lang.Integer>4 > Constant
<java.lang.Integer>1 ) )

Result: true (java.lang.Boolean)

There's a lot of output up there, but that's because it's a pretty
complicated expression, even though it's only 25 characters long.

The parser was written using Antlr and an 8.5KB grammar file (including
comments and whitespace, etc.). The evaluator was written completely by
hand and does not use a Treeparser or anything related to Antlr. It took
quite a while to write... it was not a trivial task at all.

So, don't be discouraged by Antlr. Now that you've been tripped-up by a
few mistakes, you can continue to build your evaluator, little by
little. Or, you can wait for my announcement and download my code. ;)

-chris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 254 bytes
Desc: OpenPGP digital signature
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20050729/75e91aa9/signature.bin


More information about the antlr-interest mailing list