C#ANTLR Parser不会实现继承的抽象成员Tokennames.get



我正在尝试使用c#中的c#中的antlr 4.5.3解析器(从Xamarin Studio 6.1.2上运行MacOS 10.11.3),但是antlr4 Java生成的代码命令行工具具有一个防止汇编的错误。

我的语法如下:

grammar Hello;
HELLO: 'hello';
WORD: [a-z]+;
greeting: HELLO WORD;
WS: [ ntr]+ -> skip;

当语法通过antlr4时,没有任何错误或警告,但是在Xamarin Studio中,HelloParser类的汇编中会失败:

错误CS0534:HelloParser未实现继承的抽象成员Antlr4.Runtime.Recognizer<Antlr4.Runtime.IToken,Antlr4.Runtime.Atn.ParserATNSimulator>.TokenNames.get(CS0534)

我有相应的antlr 4.5.3 nuget软件包,没有其他错误。结果解析器类似乎还可以。(生成GreetingContext类和HelloParser.greeting方法。)

为什么antlr不生成此方法,我该如何解决此错误?

如果需要,这是HelloParser.cs文件的整个:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     ANTLR Version: 4.5.3
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
// Generated from Hello.g4 by ANTLR 4.5.3
// Unreachable code detected
#pragma warning disable 0162
// The variable '...' is assigned but its value is never used
#pragma warning disable 0219
// Missing XML comment for publicly visible type or member '...'
#pragma warning disable 1591
// Ambiguous reference in cref attribute
#pragma warning disable 419
using System;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;
using Antlr4.Runtime;
using Antlr4.Runtime.Atn;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Tree;
using DFA = Antlr4.Runtime.Dfa.DFA;
[System.CodeDom.Compiler.GeneratedCode("ANTLR", "4.5.3")]
[System.CLSCompliant(false)]
public partial class HelloParser : Parser {
    public const int
        HELLO=1, WORD=2, WS=3;
    public const int
        RULE_greeting = 0;
    public static readonly string[] ruleNames = {
        "greeting"
    };
    private static readonly string[] _LiteralNames = {
        null, "'hello'"
    };
    private static readonly string[] _SymbolicNames = {
        null, "HELLO", "WORD", "WS"
    };
    public static readonly IVocabulary DefaultVocabulary = new Vocabulary(_LiteralNames, _SymbolicNames);
    [NotNull]
    public override IVocabulary Vocabulary
    {
        get
        {
            return DefaultVocabulary;
        }
    }
    public override string GrammarFileName { get { return "Hello.g4"; } }
    public override string[] RuleNames { get { return ruleNames; } }
    public override string SerializedAtn { get { return _serializedATN; } }
    public HelloParser(ITokenStream input)
        : base(input)
    {
        Interpreter = new ParserATNSimulator(this,_ATN);
    }
    public partial class GreetingContext : ParserRuleContext {
        public ITerminalNode HELLO() { return GetToken(HelloParser.HELLO, 0); }
        public ITerminalNode WORD() { return GetToken(HelloParser.WORD, 0); }
        public GreetingContext(ParserRuleContext parent, int invokingState)
            : base(parent, invokingState)
        {
        }
        public override int RuleIndex { get { return RULE_greeting; } }
        public override void EnterRule(IParseTreeListener listener) {
            IHelloListener typedListener = listener as IHelloListener;
            if (typedListener != null) typedListener.EnterGreeting(this);
        }
        public override void ExitRule(IParseTreeListener listener) {
            IHelloListener typedListener = listener as IHelloListener;
            if (typedListener != null) typedListener.ExitGreeting(this);
        }
    }
    [RuleVersion(0)]
    public GreetingContext greeting() {
        GreetingContext _localctx = new GreetingContext(Context, State);
        EnterRule(_localctx, 0, RULE_greeting);
        try {
            EnterOuterAlt(_localctx, 1);
            {
            State = 2; Match(HELLO);
            State = 3; Match(WORD);
            }
        }
        catch (RecognitionException re) {
            _localctx.exception = re;
            ErrorHandler.ReportError(this, re);
            ErrorHandler.Recover(this, re);
        }
        finally {
            ExitRule();
        }
        return _localctx;
    }
    private static string _serializedATN = _serializeATN();
    private static string _serializeATN()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("x3x430xD6D1x8206xAD2Dx4417xAEF1x8D80xAADDx3x5");
        sb.Append("bx4x2tx2x3x2x3x2x3x2x3x2x2x2x3x2x2x2x6x2");
        sb.Append("x4x3x2x2x2x4x5ax3x2x2x5x6ax4x2x2x6x3x3x2");
        sb.Append("x2x2x2");
        return sb.ToString();
    }
    public static readonly ATN _ATN =
        new ATNDeserializer().Deserialize(_serializedATN.ToCharArray());
}

注释如果您需要其他任何生成的文件,以防您自己的antlr可重现该错误。

Nuget软件包使用SAM HARWER的优化C#目标而不是标准的nuget软件包。您不能将一个版本中的工具与另一个版本的工具一起使用,它们不兼容:在此处查看发行说明。

因此,如果您想继续使用Nuget软件包为方便起见,则可以使用内部工具 - 软件包只是一个zip文件。

我不知道您是否可以使用Xamarin Studio简化此工作流程,但是使用Visual Studio,您可以完全跳过手动解析器生成步骤,因为该软件包与MSBuild集成以在编译时生成解析器代码。

相关内容

  • 没有找到相关文章

最新更新