Jason
Jason

Reputation: 4144

Recursively Processing Rules in ANTLR

Okay, for my third ANTLR question in two days:

My Grammar is meant to parse boolean statements, something like this:

AGE > 21 AND AGE < 35

Since this is a relatively simple grammar, I embedded the code rather than using an AST. The rule looks like this:

: a=singleEvaluation { $evalResult = $a.evalResult;} 
(('AND') b=singleEvaluation {$evalResult = $evalResult && $b.evalResult;})+
{
// code
}
;

Now I need to implement order of operations using parenthesis, to parse something like this:

AGE >= 21 AND (DEPARTMENT=1000 OR DEPARTMENT=1001)

or even worse:

AGE >= 21 AND (DEPARTMENT=1000 OR (EMPID=1000 OR EMPID=1001))

Can anyone suggest a way to implement the recursion needed? I'd rather not switch to an AST at this late stage, and I'm still a relative noob at this.

Jason

Upvotes: 1

Views: 250

Answers (2)

Bart Kiers
Bart Kiers

Reputation: 170227

Since some of your rules evaluate to a boolean, and others to an integer (or only compare integers), you'd best let your rules return a generic Object, and cast accordingly.

Here's a quick demo (including making a recursive call in case of parenthesized expressions):

grammar T;

@parser::members {
  private java.util.Map<String, Integer> memory = new java.util.HashMap<String, Integer>();
}

parse
@init{
  // initialize some test values
  memory.put("AGE", 42);
  memory.put("DEPARTMENT", 999);
  memory.put("EMPID", 1001);
}
 : expression EOF {System.out.println($text + " -> " + $expression.value);}
 ;

expression returns [Object value]
 : logical {$value = $logical.value;}
 ;

logical returns [Object value]
 : e1=equality {$value = $e1.value;} ( 'AND' e2=equality {$value = (Boolean)$value && (Boolean)$e2.value;}
                                     | 'OR' e2=equality  {$value = (Boolean)$value || (Boolean)$e2.value;}
                                     )*
 ;

equality  returns [Object value]
 : r1=relational {$value = $r1.value;} ( '=' r2=relational  {$value = $value.equals($r2.value);}
                                       | '!=' r2=relational {$value = !$value.equals($r2.value);}
                                       )*
 ;

relational returns [Object value]
 : a1=atom {$value = $a1.value;} ( '>=' a2=atom {$value = (Integer)$a1.value >= (Integer)$a2.value;}
                                 | '>'  a2=atom {$value = (Integer)$a1.value >  (Integer)$a2.value;}
                                 | '<=' a2=atom {$value = (Integer)$a1.value <= (Integer)$a2.value;}
                                 | '<'  a2=atom {$value = (Integer)$a1.value <  (Integer)$a2.value;}
                                 )?
 ;

atom returns [Object value]
 : INTEGER            {$value = Integer.valueOf($INTEGER.text);}
 | ID                 {$value = memory.get($ID.text);}
 | '(' expression ')' {$value = $expression.value;}
 ;

INTEGER : '0'..'9'+;
ID      : ('a'..'z' | 'A'..'Z')+;
SPACE   : ' ' {$channel=HIDDEN;};

Parsing the input "AGE >= 21 AND (DEPARTMENT=1000 OR (EMPID=1000 OR EMPID=1001))" would result in the following output:

AGE >= 21 AND (DEPARTMENT=1000 OR (EMPID=1000 OR EMPID=1001)) -> true

Upvotes: 1

Andy
Andy

Reputation: 644

I would do it like this:

program : a=logicalExpression {System.out.println($a.evalResult);}
        ;

logicalExpression returns [boolean evalResult]  :   a=andExpression { $evalResult = $a.evalResult;} (('OR') b=andExpression {$evalResult = $evalResult || $b.evalResult;})*
                                                ;

andExpression returns [boolean evalResult]      :   a=atomicExpression { $evalResult = $a.evalResult;} (('AND') b=atomicExpression {$evalResult = $evalResult && $b.evalResult;})*
                                                ;

atomicExpression returns [boolean evalResult]   :   a=singleEvaluation {$evalResult = $a.evalResult;}
                                                |   '(' b=logicalExpression ')' {$evalResult = $b.evalResult;}
                                                ;

singleEvaluation returns [boolean evalResult ]  :   'TRUE'  {$evalResult = true;}
                                                |   'FALSE' {$evalResult = false;}
                                                ;

Upvotes: 1

Related Questions