不匹配时lexing和解析模式时的输入

我有输入不匹配的ANTLR4问题,但无法解决。 我发现了很多关于它的问题,并且通常围绕词法分析器匹配其他东西到令牌,但我不认为它在我的情况下。

我有这个lexer语法:

FieldStart : '[' Definition ']' -> pushMode(INFIELD) ; Definition : 'Element'; mode INFIELD; FieldEnd : '[end]' -> popMode ; ContentValue : ~[[]* ; 

然后在以下解析器上运行:

 field : FieldStart ContentValue FieldEnd #Field_Found; 

我简化了它以放大问题,但这里是我无法进一步发展的地方。

我正在运行以下输入:

 [Element]Va-lu*e[end] 

我得到这个输出:

 Type : 001 | FieldStart | [Element] Type : 004 | ContentValue | Va-lu*e Type : 003 | FieldEnd | [end] Type : -001 | EOF |  ([] [Element] Va-lu*e [end]) 

我使用C#生成输出,执行以下操作(缩短):

  string tokens = ""; foreach (IToken CurrToken in TokenStream.GetTokens()) { if (CurrToken.Type == -1) { tokens += "Type : " + CurrToken.Type.ToString("000") + " | " + "EOF" + " | " + CurrToken.Text + "\n"; } else { tokens += "Type : " + CurrToken.Type.ToString("000") + " | " + Lexer.RuleNames[CurrToken.Type - 1] + " | " + CurrToken.Text + "\n"; } } tokens += "\n\n" + ParseTree.ToStringTree(); 

在解析此通道时

 IParseTree ParseTree = Parser.field(); 

我出现了这个错误:

 "mismatched input 'Va-lu*e' expecting ContentValue" 

我只是没有找到错误,你能在这帮我吗? 我认为它与词法分析器模式有关,但据我所知,它似乎解析器不关心(或知道)模式。

谢谢!

模式不适用于组合语法。 拆分你的语法,它应该工作。

此外,请始终检查错误消息:

错误(120):../ Field.g4:14:5:词法模式只允许在词法分析器语法中

我想我现在已经想出了如何解决我的问题,在使用拆分的Lexer / Parser语法结构并使用Visual Studio(2012和2013测试)中的Lexer模式和ANTRL4 NuGet版本时,似乎需要配置:

我必须包括

 options { tokenVocab = GRAMMAR_NAME_Lexer; }