[antlr-interest] Evaluation boolean expressions
Martin Probst
mail at martin-probst.com
Tue Jul 26 08:45:45 PDT 2005
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
More information about the antlr-interest
mailing list