在 C# 中使用 MySQL ANTLR4 语法,得到 "The type or namespace name 'MySQLBaseLexer' could not be found"

Using MySQL ANTLR4 grammar in C#, getting "The type or namespace name 'MySQLBaseLexer' could not be found"

我正在尝试使用 MySQL 的 ANTLR4 语法来进行一些基本的语句解析。这是我第一次使用 ANTLR。我在 VS2017 中创建了一个空白的 C# 控制台项目,安装了 ANTLR4、ANTLR4.CodeGenerator 和 Antlr4.Runtime nuget 包以及 ANTLR 语言支持扩展。我在 MySQLLexer.g4、MySQLParser.g4 和 predefined.tokens 文件中添加了。当我尝试构建解决方案时出现以下错误:

The type or namespace name 'MySQLBaseLexer' could not be found (are you missing a using directive or an assembly reference?"

我一直在浏览教程、SO 和其他讨论在 C# 上使用 ANTLR4 的博客,但似乎没有任何方法可以解决错误。谁能指出我正确的方向以使其正常工作?

谢谢!

我假设您正在使用这两个语法文件located here. While those grammars may not contain C++ code, it requires the base class you mention which is C++ code, so you will need to port MySQLBaseLexer.cpp and auxiliary code to C#, which is found in the Git repo. The readme in the ANTLR4 grammar directory包含有关此要求的更多详细信息。

另外,你为什么不尝试使用 Visual Studio 2019,官方 Java Antlr Tool 4.7.2(需要 Java),以及我的 Net Core 模板来生成一个 Antlr C# 程序?一切都集成到构建和 运行 F5 中。查看 this and this。 C# 工具比 Antlr 的正式发布至少晚了两年。此外,*.tokens 是生成的文件。

我不明白为什么人们总是将 Antlr 生成的文件签入到存储库中。它不应该被签入和使用,特别是如果你使用我的模板。 .g4 文件是您的来源。如果您需要在 VS 中编辑 Antlr 语法的扩展,您可能需要查看我的 AntlrVSIX 扩展。

刚看到这个题目就对这个题目很感兴趣,不过最近去度假了,没来得及完成样例。

这是一个示例: https://github.com/hmadrigal/playground-dotnet/tree/master/MsDotNetCore.MySqlParser

我创建了 workbench (https://github.com/mysql/mysql-workbench/tree/8.0/library/parsers/grammars) 提供的语法,该语法已被改编为适用于 .NET Core(使用 .NET 标准实现可移植性)

我创建了这个示例项目,它不适合生产,我也不是 ANTLR 专家,但我喜欢编码和解析器。

看看 README.md,https://github.com/hmadrigal/playground-dotnet/blob/master/MsDotNetCore.MySqlParser/MySqlParser.TerminalApp/MySqlParserTester.cs 可能就是您的 bootstrap。如果我要写博客 post 我会在这里分享笔记。