为多条线ANTLR重复相同的规则

时间:2018-05-25 12:09:49

标签: parsing antlr antlr4 lexer antlrworks

我想知道使用ANTLR为多行应用解析规则的正确方法是什么。我使用以下规则,适用于单行语句。我想在下一行重复这一点:

    grammar Condition;


/* Parser Rules */

condition : (expr+)? EOF;
expr
    : expr And expr         # andExpr
    | expr Or expr          # orExpr
    | LPar expr RPar        # parExpr
    | prop MIN Numerical expr       # eqExpr
    | prop some expr        # someExpr
    | prop only expr        # onlyExpr
    | prop value dataValue      # valueExpr
    | id                    # idExpr
    | not id                    # idExpr
    ;

id : Identifier;
prop:Identifier;
dataValue:Identifier;

/* Lexical Tokens */

And : 'AND';
Or : 'OR';
LPar : '(';
RPar : ')';
Equals : '=';
some : 'some';
only : 'only';
MIN : 'MIN';
value:'value';
not:'not';
NEWLINE: ('\n') { skip(); };

Numerical : [1-9] [0-9]*;
Data
    : [true]
    | [false]
    | [A]
    | [B]
    | [C]
    | [D]
    ;

// Using generic identifier tokens so that better warnings can be given in later passes.
Identifier : [a-zA-Z_] [a-zA-Z0-9_]*;

// Skip parsing of whitespace but save on hidden channel to enable retrieval of original string.
WhiteSpace : [ \t\r\n]+ -> channel(HIDDEN);

// Invalid character rule is used so that the lexer pass never fails.
InvalidChar : .;

上述语法在测试时给出了正确的结果,但是当我尝试使用访问者时它会消耗每个令牌,它会抛出以下错误:

  

第2行:0无关输入' SafetyGoal'期待{,' AND',' OR'}

有什么建议吗?

修改  下面是我用来读取输入文件并调用访问者代码的代码:

Stream<String> stream = Files.lines( Paths.get("C:\\test\\RulesTest.txt"), StandardCharsets.UTF_8);
stream.forEach(s -> contentBuilder.append(s).append("\n"));
String input=contentBuilder.toString();
InputStream inStream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
org.antlr.v4.runtime.ANTLRInputStream in=new org.antlr.v4.runtime.ANTLRInputStream(inStream);
System.out.println("These are the lines:"+contentBuilder);
ConditionLexer lexer=new ConditionLexer(in);
org.antlr.v4.runtime.CommonTokenStream tokens= new org.antlr.v4.runtime.CommonTokenStream(lexer);
ConditionParser parser=new ConditionParser(tokens);
ParseTree tree=parser.expr();
MyVisitor vis=new MyVisitor();
vis.visit(tree);

MyVisitor基本上包含与ANTLR生成的代码相同的代码,我在分析时存储结果。

2 个答案:

答案 0 :(得分:1)

您的Data规则有误:[true]匹配单个字符(true)。这样做:

Data
    : 'true'
    | 'false'
    | [A]
    | [B]
    | [C]
    | [D]
    ;

testResult value true与您的替代prop value dataValue不匹配,因为dataValue看起来像这样:

dataValue : Identifier;

它应该是这样的(我猜测):

dataValue : Identifier | Data;

当我按照上面的说明改变你的语法并解析输入时:

(FSR AND testedBy some (testResult value true)) 
SafetyGoal AND (fulfills some (not NR) OR fulfilledBy some NR)

我得到以下解析树:

enter image description here

答案 1 :(得分:1)

<div class="checkbox-container">
  <input type="checkbox" id="checkbox">
  <span className="checkmark"></span>
</div>
<label class="container" for="checkbox">
  I have read and do accept the <a href="#">terms and conditions</a>
</label>

您正在调用ParseTree tree=parser.expr(); 规则,该规则仅匹配单个表达式。您的expr规则是匹配多个表达式的规则,因此您应该调用该规则。