C#ANTLR解析器不实现继承的抽象成员TokenNames.get

时间:2016-12-30 19:15:30

标签: c# parsing mono antlr4

我试图在Mono上的C#中使用ANTLR 4.5.3解析器(从macOS 10.11.3上的Xamarin Studio 6.1.2运行),但antlr4 Java命令生成的代码 - line工具有一个错误,阻止编译。

我的语法如下:

grammar Hello;

HELLO: 'hello';
WORD: [a-z]+;

greeting: HELLO WORD;

WS: [ \n\t\r]+ -> 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("\x3\x430\xD6D1\x8206\xAD2D\x4417\xAEF1\x8D80\xAADD\x3\x5");
        sb.Append("\b\x4\x2\t\x2\x3\x2\x3\x2\x3\x2\x3\x2\x2\x2\x3\x2\x2\x2\x6\x2");
        sb.Append("\x4\x3\x2\x2\x2\x4\x5\a\x3\x2\x2\x5\x6\a\x4\x2\x2\x6\x3\x3\x2");
        sb.Append("\x2\x2\x2");
        return sb.ToString();
    }

    public static readonly ATN _ATN =
        new ATNDeserializer().Deserialize(_serializedATN.ToCharArray());
}

评论是否需要任何其他生成的文件,以防错误无法与您自己的ANTLR重现。

1 个答案:

答案 0 :(得分:2)

NuGet包使用Sam Harwell's optimized C# target而不是标准包。您无法使用一个版本的工具以及另一个版本的运行时库,它们不兼容:查看release notes here

因此,如果您想继续使用NuGet软件包,可以使用里面的工具 - 软件包只是一个zip文件。

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