在ANTLR中分离多个规则时出错

时间:2012-11-27 12:14:18

标签: java antlr

我差点用ANTLR完成了我的第一次冒险,而且这是一次旅行。不幸的是,总是只有马蹄铁,手榴弹和核武器,对吗?

无论如何,我正在尝试解析看起来像这样的输入:

; IF AGE IS LESS THAN 21, STILL RETURN TRUE FOR OVERSEAS LOCATION \r\n
SHOW "AGE REQUIREMENTS FAILED" FOR \r\n
IF AGE < 21 THEN \r\n
LOCATION = "OVERSEAS" \r\n
ENDIF \r\n
\r\n
; NEED  SOMEONE WHO HAS WORKED FOR US FOR > 1 YEAR EXCEPT FOR CEO \r\n
SHOW "MINIMUM TIME REQUIREMENT NOT MET" FOR \r\n
IF STARTDATE > TODAY - 1 YEAR THEN \r\n
EMPLID=001 \r\n
ENDIF \r\n

通常,如果测试失败,则会显示消息。

无论如何,一个集合可以包含一个或多个SHOW规则。处理单个SHOW规则有效,但当输入流包含&gt;时,它不会“分裂”。 1 SHOW规则。

以下是语法中的相关规则:

showGroup returns [List<PolicyEvaluation> value]
  @init {List<PolicyEvaluation> peList = new ArrayList<PolicyEvaluation>();}
  : (expr1=show)* {peList.add($expr1.value);}
  {
    System.out.println("Entered policyGroup rule");
    $value = peList;
  }
  ;

// evaluate a single SHOW statement
show returns [PolicyEvaluation value]
  : ('SHOW' expr1=STRING 'FOR')? expr2=ifStatement EOL*
  {
    System.out.println("Entered show rule");
    Boolean expr2Value = (Boolean) $expr2.value;
    PolicyEvaluation pe = new PolicyEvaluation();
    if (expr1 == null) {
      pe.setValue(expr2Value);
      pe.setMessage(null);
    } else {
        if (expr2Value == false) {
          pe.setValue(false);
          pe.setMessage(expr1.getText());
        } else {
          pe.setValue(true);
          pe.setMessage(null);
        }   
    }
    $value = pe;
  }
  ;
// rules leading up to the show rule
// domain-specific grammar rules
STRING: '"' ID  (' ' ID)* '"'
  {
    System.out.println("Entered STRING lexer rule");
    // strip the quotes once we match this token
    setText(getText().substring(1, getText().length()-1));
  }
  ;
COMMENT: ';' (ID|' ')* EOL {$channel = HIDDEN;};
EOL: ('\r'|'\n'|'\r\n') {$channel = HIDDEN;};
SPACE: ' ' {$channel = HIDDEN;};

也许这很简单。任何帮助表示赞赏。

杰森

1 个答案:

答案 0 :(得分:3)

尝试更改此内容:(expr1=show)* {peList.add($expr1.value);}

(expr1=show {peList.add($expr1.value);})*

只有在完成所有show次匹配后才能触发该操作,让您在最后expr1上进行操作。