[antlr-interest] Evaluation boolean expressions

Xavier Benveniste xavier.benveniste at free.fr
Wed Jul 27 00:36:14 PDT 2005


Hi,

thanks anyway (your answer explains me why it doesn't work).

But I'm a little bit confused : does it worth using ANTL if such a simple
algorithm is so hard to develop?
Or can't ANTLR evaluates such simple expression as (4 + (5*2) > 15) OR (4>1)
?

----- Original Message ----- 
From: "John B. Brodie" <jbb at acm.org>
To: <xavier.benveniste at free.fr>
Cc: <antlr-interest at antlr.org>
Sent: Wednesday, July 27, 2005 4:07 AM
Subject: Re: [antlr-interest] Evaluation boolean expressions


> 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