[antlr-interest] Evaluation boolean expressions

John B. Brodie jbb at acm.org
Tue Jul 26 19:07:12 PDT 2005


Hello :-

I am not sure that this will be of any help...

Xavier Benveniste asked:
>Thanks both of you for your answers.
>I tried the changes suggested by Tarun Khanna but I 've still errors when I
>run it with my IDE (eclipse) :
>(4 + (5*2) > 15) OR (4>1)
>
>Exception in thread "main" line 1:2: expecting LPAREN, found '4'
>
>
>
>So, please could you could find attached the grammar file and main java
>class I use.
>Maybe I did a mistake (again).
...attachment sniped...

Lets look at your ExprParser grammar:

logicalOrExpression must begin with a logicalAndExpression
logicalAndExpression must begin with a bexpr
bexpr must begin with an expr
expr must begin with a mexpr
mexpr must begin with an atom
atom must begin with a LPAREN
  which must then be followed by a logicalOrExpression and terminated with RPAREN
  but
    logicalOrExpression must begin with a logicalAndExpression
    logicalAndExpression must begin with a bexpr
    bexpr must begin with an expr
    expr must begin with a mexpr
    mexpr must begin with an atom
    atom must begin with a LPAREN
      which must then be followed by a logicalOrExpression terminated with RPAREN
      but
        logicalOrExpression must begin with a logicalAndExpression
        logicalAndExpression must begin with a bexpr
        bexpr must begin with an expr
        expr must begin with a mexpr
        mexpr must begin with an atom
        atom must begin with a LPAREN
          which must then be followed by a logicalOrExpression terminated with RPAREN

...and on to infinity!

your grammar never really recognizes anything because it insists on an infinite
series of leading LPARENs

LPAREN is required to start an atom; the only leaf of the grammar is atom

you might try replacing your parser rule

atom:   LPAREN logicalOrExpression RPAREN
    ;

with

atom:   INT
    | LPAREN! logicalOrExpression RPAREN!
    ;


however this will cause many problems which you may or may not find acceptable.

this new grammar will accept these expressions:

   1 OR 1

   1 > 1

   1 + 1

   1

which might not be what you wanted. certainly your TreeParser is not prepared
to deal with them...

Hope this helps.
   -jbb



More information about the antlr-interest mailing list