1

好的,对于我两天内的第三个 ANTLR 问题:

我的语法旨在解析布尔语句,如下所示:

AGE > 21 AND AGE < 35

由于这是一个相对简单的语法,我嵌入了代码而不是使用 AST。规则如下所示:

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

现在我需要使用括号实现操作顺序,以解析如下内容:

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

甚至更糟:

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

任何人都可以提出一种实现所需递归的方法吗?我宁愿在这个后期阶段不切换到 AST,而且我在这方面仍然是一个相对的菜鸟。

杰森

4

2 回答 2

1

我会这样做:

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;}
                                                ;
于 2012-11-15T14:58:42.680 回答
1

由于您的某些规则评估为布尔值,而其他规则评估为整数(或仅比较整数),您最好让您的规则返回一个通用对象,并相应地进行转换。

这是一个快速演示(包括在括号表达式的情况下进行递归调用):

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;};

解析输入"AGE >= 21 AND (DEPARTMENT=1000 OR (EMPID=1000 OR EMPID=1001))"将产生以下输出:

AGE >= 21 AND (DEPARTMENT=1000 OR (EMPID=1000 OR EMPID=1001)) -> true
于 2012-11-15T19:09:08.130 回答