Antlr4:不匹配的输入(Antlr4: Mismatched input)

2019-09-01 22:05发布

这里有一个简单的语法测试,我以为会很容易解析,但我得到“不匹配输入”马上蝙蝠,我想不出什么ANTLR的所期待的。

输入:

  # include "something" program TEST1 { BLAH BLAH }

我的语法:

  grammar ProgHeader;

  program: header* prog EOF ;
  header: '#' ( include | define ) ;
  include: 'include' string ;
  define: 'define' string string? ;
  string: '"' QTEXT '"' ;
  prog: 'program' QTEXT '{' BLOCK '}' ;
  QTEXT: ~[\r\n\"]+ ;
  BLOCK: ~[}]+ ; // don't care, example block
  WS: [ \t\r\n] -> skip ;

输出错误消息:

line 1:0 mismatched input '# include "something" program TEST1 { BLAH BLAH '
expecting {'program', '#'}

这确实让我困惑,因为它说,它在寻找一个“#”,并有在输入的开始一个权利。 我甩解析树了。 这似乎是在右上角卡,在“程序”规则:

(program # include "something" program TEST1 { BLAH BLAH  } )

HALP?

下面是完整的程序推动这一测试情况下,如果它的事项(我不认为它应该的问题,上面的信息就足够了,但在这里它是):

  package antlrtests;

  import antlrtests.grammars.*;
  import org.antlr.v4.runtime.*;
  import org.antlr.v4.runtime.tree.*;

  /**
   *
   * @author Brenden Towey
   */
  public class ProgHeaderTest {
     private String[] testVectors = {
        "# include \"something\" program TEST1 { BLAH BLAH } ",
     };
     public void runTests() {
        for( String test : testVectors )
           simpleTest( test );
     }
     private void simpleTest( String test ) {
        ANTLRInputStream ains = new ANTLRInputStream( test );
        ProgHeaderLexer wpl = new ProgHeaderLexer( ains );
        CommonTokenStream tokens = new CommonTokenStream( wpl );
        ProgHeaderParser wikiParser = new ProgHeaderParser( tokens );
        ParseTree parseTree = wikiParser.program();
        System.out.println( "'" + test + "': " + parseTree.toStringTree(
                wikiParser ) );
     }
  }

而完整的输出:

run:
line 1:0 mismatched input '# include "something" program TEST1 { BLAH BLAH ' expecting {'program', '#'}
'# include "something" program TEST1 { BLAH BLAH } ': (program # include "something" program TEST1 { BLAH BLAH  } )
BUILD SUCCESSFUL (total time: 0 seconds)

Answer 1:

这在最开始匹配最长令牌是QTEXT,它匹配的文本# include (文字,直到但不包括第一个"在这一点上字符),但有效的标记是‘程序’和‘#’,据报道。因此,更好地避免匹配几乎任何标记定义。



文章来源: Antlr4: Mismatched input
标签: antlr4