本文介绍了不匹配的输入时词法和语法分析的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与不匹配输入的ANTLR4问题,但解决不了。我发现了很多处理这一问题,并且通常在匹配令牌别的词法分析器旋转,但我没有看到它在我的情况。



我有了这个词法分析器的文法:

  FieldStart:'['定义'] ' - > pushMode(INFIELD); 
定义:元素;
模式INFIELD;
FieldEnd:[结束]' - > popMode;
ContentValue:〜[[] *;



,然后在下面的分析程序运行:

 字段:FieldStart ContentValue FieldEnd #Field_Found; 



我简化它放大的问题,但在这里就是我不能得到任何点。进一步



我运行在接下来的输入:

  [元]弗吉尼亚州禄* E [结束] 

和我得到这样的输出:

 键入:001 | FieldStart | [元] 
类型:004 | ContentValue | VA-禄* E
类型:003 | FieldEnd | [完]
类型:-001 | EOF | <&EOF GT;


([] [元]弗吉尼亚州禄* E [结束])

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



字符串标记=;
的foreach(的IToken CurrToken在TokenStream.GetTokens())
{
如果(CurrToken.Type == -1)
{
令牌+ =类型 + CurrToken.Type.ToString(000)+|+EOF+|+ CurrToken.Text +\\\
;
}
,否则
{
令牌+ =类型:+ CurrToken.Type.ToString(000)+|+ Lexer.RuleNames [CurrToken.Type - 1] +|+ CurrToken.Text +\\\

}
}
令牌+ =\\\
\\\
+ ParseTree.ToStringTree();



在经由



IParseTree是ParseTree = Parser.field();



我提出了这个错误:

 不匹配输入'弗吉尼亚州禄* E'期待ContentValue

我只是没有找到错误,你能帮助我吗?
我认为它有什么做的词法分析器模式,但据我读它看起来像解析器不关心(或不知道)有关模式的。



谢谢!


解决方案

我想现在我已经找到了如何解决我的问题,似乎有一个有分流词法分析器/分析器语法结构的工作,并在Visual Studio中使用词法模式时,需要配置与ANTRL4的NuGet版本(测试过2012年和2013年):



我必须包括



选项{tokenVocab = GRAMMAR_NAME_Lexer; }



在一开始我的解析器的语法。



否则,词法分析器并创建令牌和预期,但解析器将无法识别词法标记是另一种模式,但默认模式的模式。



我也遇到了popMode词法分析器命令时,有时会导致我的TokenStream抛出无效状态例外,我可以解决用模式(DEFAULT_MODE)而不是popMode。



我希望这可以帮助别人,但我还是想,如果有人谁了解ANTLR可以提供一些额外的说明,因为我只是直到它的工作四处玩弄解决了。


I'm having an ANTLR4 problem with mismatched input but can't solve it. I've found a lot of questions dealing with that, and the usually revolve around the lexer matching something else to the token, but I don't see it in my case.

I've got this lexer grammar:

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

Which then runs on the following parser:

field           :   FieldStart  ContentValue FieldEnd               #Field_Found;

I simplified it to zoom in to the problem, but here's the point where I can't get any further.

I'm running that on the following input:

[Element]Va-lu*e[end]

and I get this output:

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


([] [Element] Va-lu*e [end])

I generated the output with C#, doing the following (shortened):

            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();

Upon parsing this via

IParseTree ParseTree = Parser.field();

I am presented this error:

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

I just don't find the error, can you help me here?I assume it's got something to do with the lexer mode, but from as far as I read it looks like the parser doesn't care (or know) about the modes.

Thanks!

解决方案

I think I have now figured out how to solve my problem, there seems to be a required configuration when working with a split Lexer / Parser grammar structure AND using Lexer modes in Visual Studio (tested 2012 and 2013) with the ANTRL4 NuGet release:

I had to include

options {	tokenVocab = GRAMMAR_NAME_Lexer;	}

in my parser grammar at the beginning.

Otherwise, the lexer did create the tokens and the modes as expected but the parser will not recognize lexer tokens that are in another mode but the default mode.

I have also experienced that the "popMode" lexer command does sometimes cause my TokenStream to throw an invalid state exception, I could solve that with using "mode(DEFAULT_MODE)" instead of "popMode".

I hope this helps somebody, but I'd still like if someone who understands ANTLR could offer some additional clarification, since I just "solved" it by toying around until it worked.

这篇关于不匹配的输入时词法和语法分析的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 13:02