[antlr-interest] Evaluation boolean expressions
Xavier Benveniste
xavier.benveniste at free.fr
Tue Jul 26 09:35:44 PDT 2005
Hi,
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).
Concerning your notice (Martin Probst) <<If you want to achieve
composability (OR (> 2 5) (OR (= 3 3) (< 1 4))) you might want to add
"lexpr" as an option to bexpr.>>,
can you more explicit ? I'm sorry, but I've started ANTLR since a few days.
----- Original Message -----
From: "Martin Probst" <mail at martin-probst.com>
To: "Tarun Khanna" <tarunkhanna at gmail.com>
Cc: "Xavier Benveniste" <xavier.benveniste at free.fr>;
<antlr-interest at antlr.org>
Sent: Tuesday, July 26, 2005 5:45 PM
Subject: Re: [antlr-interest] Evaluation boolean expressions
> If you want to achieve composability (OR (> 2 5) (OR (= 3 3) (< 1 4)))
> you might want to add "lexpr" as an option to bexpr.
>
> On Tue, 2005-07-26 at 11:35 -0400, Tarun Khanna wrote:
> > Hi,
> >
> > Here is the changed grammar. Seems to work fine. I hope you'll notice
> > the changes...
> >
> > The first one is in atom, atom: LPAREN logicalOrExpression RPAREN
> >
> > Then in the TreeParser, I have added a new rule lexpr.
> >
> > Parse the tree using logicalOrExpression as the starting point. Then
> > use the lexpr to walk the tree.
> >
> > -----------------------------
> > class ExprParser extends Parser;
> >
> > options {
> >
> > buildAST = true;
> >
> > defaultErrorHandler = false;
> >
> > }
> >
> > logicalOrExpression: logicalAndExpression (OR^ logicalAndExpression)*
> >
> > ;
> >
> > logicalAndExpression: bexpr (AND^ bexpr)*
> >
> > ;
> >
> > bexpr: expr ((EQUALS^|NOT_EQUALS^|GT^|GTE^|LT^|LTE^) expr)*
> >
> > ;
> >
> > expr: mexpr ((PLUS^|MINUS^) mexpr)*
> >
> > ;
> >
> > mexpr
> >
> > : atom ((MULT^|DIV^) atom)*
> >
> > ;
> >
> > atom: INT
> >
> > | LPAREN! logicalOrExpression RPAREN!
> >
> > // | LPAREN! logicalOrExpression RPAREN!
> >
> > ;
> >
> > class ExprLexer extends Lexer;
> >
> > options {
> >
> > k = 2; // two characters of lookahead
> >
> > }
> >
> > /* Integers */
> >
> > INT : ('0'..'9')+ ;
> >
> > /* Ignored characters */
> >
> > WS : ( ' '
> >
> > | '\r' '\n'
> >
> > | '\n'
> >
> > | '\t'
> >
> > )
> >
> > {$setType(Token.SKIP);}
> >
> > ;
> >
> > /* Logical operators */
> >
> > OR : "OR";
> >
> > AND : "AND";
> >
> > /* Comparison operators */
> >
> > EQUALS : "==";
> >
> > NOT_EQUALS : "<>";
> >
> > GT : '>';
> >
> > GTE : ">=";
> >
> > LT : '<';
> >
> > LTE : "<=";
> >
> > /* Arithmetic operators */
> >
> > PLUS : '+';
> >
> > MINUS : '-';
> >
> > MULT : '*';
> >
> > DIV : '/';
> >
> > /* Parentheses */
> >
> > LPAREN : '(';
> >
> > RPAREN : ')';
> >
> > class ExprTreeParser extends TreeParser;
> >
> > options {
> >
> > importVocab=ExprParser;
> >
> > }
> >
> > expr returns [int r=0] { int a,b;}
> >
> > : #(PLUS a=expr b=expr) { r = a + b;}
> >
> > | #(MINUS a=expr b=expr) { r = a - b; }
> >
> > | #(MULT a=expr b=expr) { r = a * b; }
> >
> > | #(DIV a=expr b=expr) { r = a / b; }
> >
> >
> > | i:INT {r = (int)Integer.parseInt(i.getText());}
> >
> > ;
> >
> > bexpr returns [boolean r=false] { int a,b; }
> >
> > : #(EQUALS a=expr b=expr) {r = (a == b);}
> >
> > | #(NOT_EQUALS a=expr b=expr) {r = (a != b);}
> >
> > | #(GT a=expr b=expr) {r = (a > b);}
> >
> > | #(GTE a=expr b=expr) {r = (a >= b);}
> >
> > | #(LT a=expr b=expr) {r = (a < b);}
> >
> > | #(LTE a=expr b=expr) {r = (a <= b);}
> >
> > ;
> >
> > lexpr returns [boolean r= false] {boolean a, b;}
> > : #(OR a=bexpr b=bexpr) { r = a || b; }
> >
> > | #(AND a=bexpr b=bexpr) { r = a && b; }
> >
> > ;
> >
> >
> >
> >
> > On 7/26/05, Xavier Benveniste <xavier.benveniste at free.fr> wrote:
> > Hi,
> >
> > I (hardly) succeeded in building a very simple int calculator.
> > Then, I succeeded in building an int 'equality' evaluator :
> > for instance, if I enter :
> > 5+(4*3) > 10 + 9
> > I have the following result :
> >
> > ( > ( + 5 ( * 4 3 ) ) ( + 10 9 ) )
> >
> > false
> >
> > So far, so good.
> >
> > Now, what I'm trying to do is to evaluate expression like :
> > (4 + (5*2) > 15) OR (4>1).
> > But, here I'm stuck.
> >
> > You 'll find below the grammar I wrote.
> > I'm sure it's pretty straightforward but I can't manage
> > to achieve it.
> > If any ANTLR's guru could help me.
> > Thanks.
> > class
> >
> > ExprParser extends Parser;
> >
> > options
> >
> > {
> >
> > buildAST =
> >
> > true;
> >
> > defaultErrorHandler =
> >
> > false;
> >
> > }
> >
> > //logicalOrExpression: logicalAndExpression (OR^
> > logicalAndExpression)*
> >
> > // ;
> >
> > //logicalAndExpression: bexpr (AND^ bexpr)*
> >
> > // ;
> >
> > bexpr: expr ((EQUALS^|NOT_EQUALS^|GT^|GTE^|LT^|LTE^) expr)*
> >
> > ;
> >
> > expr: mexpr ((PLUS^|MINUS^) mexpr)*
> >
> > ;
> >
> > mexpr
> >
> > : atom ((MULT^|DIV^) atom)*
> >
> > ;
> >
> > atom: INT
> >
> > | LPAREN! expr RPAREN!
> >
> > // | LPAREN! logicalOrExpression RPAREN!
> >
> > ;
> >
> > class
> >
> > ExprLexer extends Lexer;
> >
> > options
> >
> > {
> >
> > k = 2;
> >
> > // two characters of lookahead
> >
> > }
> >
> > /* Integers */
> >
> > INT : (
> >
> > '0'..'9')+ ;
> >
> > /* Ignored characters */
> >
> > WS : (
> >
> > ' '
> >
> > |
> >
> > '\r' '\n'
> >
> > |
> >
> > '\n'
> >
> > |
> >
> > '\t'
> >
> > )
> >
> > {$setType(Token.SKIP);}
> >
> > ;
> >
> > /* Logical operators */
> >
> > OR :
> >
> > "OR";
> >
> > AND :
> >
> > "AND";
> >
> > /* Comparison operators */
> >
> > EQUALS :
> >
> > "==";
> >
> > NOT_EQUALS :
> >
> > "<>";
> >
> > GT :
> >
> > '>';
> >
> > GTE :
> >
> > ">=";
> >
> > LT :
> >
> > '<';
> >
> > LTE :
> >
> > "<=";
> >
> > /* Arithmetic operators */
> >
> > PLUS :
> >
> > '+';
> >
> > MINUS :
> >
> > '-';
> >
> > MULT :
> >
> > '*';
> >
> > DIV :
> >
> > '/';
> >
> > /* Parentheses */
> >
> > LPAREN :
> >
> > '(';
> >
> > RPAREN :
> >
> > ')';
> >
> > class
> >
> > ExprTreeParser extends TreeParser;
> >
> > options
> >
> > {
> >
> > importVocab=ExprParser;
> >
> > }
> >
> > expr
> >
> > returns [int r=0] { int a,b;}
> >
> > : #(PLUS a=expr b=expr) { r = a + b;}
> >
> > | #(MINUS a=expr b=expr) { r = a - b; }
> >
> > | #(MULT a=expr b=expr) { r = a * b; }
> >
> > | #(DIV a=expr b=expr) { r = a / b; }
> >
> > | i:INT {r = (
> >
> > int)Integer.parseInt(i.getText());}
> >
> > ;
> >
> > bexpr
> >
> > returns [boolean r=false] { int a,b; }
> >
> > : #(EQUALS a=expr b=expr) {r = (a == b);}
> >
> > | #(NOT_EQUALS a=expr b=expr) {r = (a != b);}
> >
> > | #(GT a=expr b=expr) {r = (a > b);}
> >
> > | #(GTE a=expr b=expr) {r = (a >= b);}
> >
> > | #(LT a=expr b=expr) {r = (a < b);}
> >
> > | #(LTE a=expr b=expr) {r = (a <= b);}
> >
> > ;
> >
> > //logicalAndExpression returns [boolean r=false] { boolean
> > a,b; }
> >
> > // : #(AND a=bexpr b=bexpr) {r = (a && b); }
> >
> > // ;
> >
> > //logicalOrExpression returns [boolean r=false] {boolean
> > a,b; }
> >
> > // : #(OR a=logicalAndExpression b=logicalAndExpression) {r =
> > (a || b);}
> >
> > // ;
> >
> >
> >
> >
> >
> > --
> > Tarun Khanna
>
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Main.java
Type: application/octet-stream
Size: 437 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20050726/40f33671/Main.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: expr.g
Type: application/octet-stream
Size: 1783 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20050726/40f33671/expr.obj
More information about the antlr-interest
mailing list