1

以下是我一直在研究的 ANTLR 语法的片段:

compoundEvaluation returns [boolean evalResult]
  : singleEvaluation (('AND'|'OR') singleEvaluation)*
;

//overall rule to evaluate a single expression
singleEvaluation returns [boolean evalResult]
  : simpleStringEvaluation {$evalResult = $simpleStringEvaluation.evalResult;} 
  | stringEvaluation {$evalResult = $stringEvaluation.evalResult;}
  | simpleDateEvaluation {$evalResult = $simpleDateEvaluation.evalResult;}
  | dateEvaluatorWithModifier1 {$evalResult = $dateEvaluatorWithModifier1.evalResult;}
  | dateEvaluatorWithoutModifier1 {$evalResult = $dateEvaluatorWithoutModifier1.evalResult;}
  | simpleIntegerEvaluator {$evalResult = $simpleIntegerEvaluator.evalResult;}
  | integerEvaluator {$evalResult = $integerEvaluator.evalResult;}
  | integerEvaluatorWithModifier {$evalResult = $integerEvaluatorWithModifier.evalResult;}
  ;

以下是其中一个评估规则的示例:

simpleStringEvaluation returns [boolean evalResult]
: op1=STR_FIELD_IDENTIFIER operator=(EQ|NE) '"'? op2=(SINGLE_VALUE|INTEGER) '"'?
{
  // I don't want these to be equal by default
  String op1Value = op1.getText();
  String op2Value = op2.getText();
  try {
    // get the values of the bean property specified by the value of op1 and op2
    op1Value = BeanUtils.getProperty(policy,op1.getText());
  } catch (NoSuchMethodException e) {
    e.printStackTrace();
  } catch (InvocationTargetException e) {
    e.printStackTrace();
  } catch (IllegalAccessException e) {
    e.printStackTrace();
  }

String strOperator = operator.getText();
if (strOperator.equals("=")) {
  evalResult = op1Value.equals(op2Value);
} 
if (strOperator.equals("<>")) {
  evalResult = !op1Value.equals(op2Value);
}
}
;

显然我是一个新手,因为我没有构建一棵树,但是代码有效,所以我对它相当满意。但是,下一步是对多个 singleEvaluation 语句执行逻辑评估。由于我将代码嵌入语法中,我希望有人能指出我正确的方向,以弄清楚如何评估 0 个或更多结果。

4

2 回答 2

1

无需将值存储在集合中。

为什么不简单地做这样的事情:

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

?

于 2012-11-13T20:25:24.333 回答
0

这就是我的做法。我创建了一个 Set 作为成员,然后在每个语句的 @init 中,我重新初始化了 Set。当语句被评估时,它填充了集合。由于该集合的唯一合法值是真或假,因此我最终得到了一个包含 0、1 或两个成员的集合。

OR 评估如下所示:

compoundOrEvaluation returns [boolean evalResult]
  @init {evaluationResults = new HashSet<Boolean>();}
  : a=singleEvaluation {evaluationResults.add($a.evalResult);} (('OR') b=singleEvaluation {evaluationResults.add($b.evalResult);})+
  {
    if (evaluationResults.size()==1) {
      evalResult = evaluationResults.contains(true);
    } else {
      evalResult = true;
    }
  }
  ;

AND 评估仅在 else 语句中有所不同,其中 evalResult 将设置为 false。到目前为止,这通过了我可以提交的单元测试。

最终我可能会使用一棵树和一个访问者类,但代码目前可以工作。

于 2012-11-12T15:15:05.483 回答