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

huangapple go评论87阅读模式
英文:

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

问题

I'm here to help with the translation. Here's the translated content:

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

找不到类型或命名空间名 'MySQLBaseLexer'(是否缺少using指令或程序集引用?)

我已经查看了教程、Stack Overflow和其他讨论在C#中使用ANTLR4的博客,但似乎没有什么能解决这些错误的方法。有人可以指导我正确的方向,让这个工作正常吗?

谢谢!

英文:

I am trying to play around with the ANTLR4 grammars for MySQL to do some basic statement parsing. This is my first time using ANTLR. I created a blank C# console project in VS2017, installed the ANTLR4, ANTLR4.CodeGenerator and Antlr4.Runtime nuget packages as well as the ANTLR Language Support extension. I added in the MySQLLexer.g4, MySQLParser.g4 and the predefined.tokens file. When I try to build the solution I get the following error:

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

I have been looking through the tutorials, SO, and other blogs that discuss using ANTLR4 on C# but nothing seems to resolve the errors. Could anyone point me in the right direction to get this working correctly?

Thanks!

答案1

得分: 2

I assume you are using the two grammar files 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 contains a few more details about this requirement.

Also, why don't you try using Visual Studio 2019, the official Java Antlr Tool 4.7.2 (which requires Java), and my Net Core template for generating an Antlr C# program? Everything is integrated into a build and run F5. Check out this and this. The C# tool is at least two years behind the official release of Antlr. Further, the *.tokens is a generated file.

I don't understand why people keep checking in Antlr-generated files into a repo. It should not be checked in and used, especially if you use my template. The .g4 files are your source. If you need an extension for editing Antlr grammars in VS, you might want to check out my AntlrVSIX extension.

英文:

I assume you are using the two grammar files 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 contains a few more details about this requirement.

Also, why don't you try using Visual Studio 2019, the official Java Antlr Tool 4.7.2 (which requires Java), and my Net Core template for generating an Antlr C# program? Everything is integrated into a build and run F5. Check out this and this. The C# tool is at least two years behind the official release of Antlr. Further, the *.tokens is a generated file.

I don't understand why people keep checking in Antlr-generated files into a repo. It should not be checked in and used, especially if you use my template. The .g4 files are your source. If you need an extension for editing Antlr grammars in VS, you might want to check out my AntlrVSIX extension.

答案2

得分: 0

I was interested in this topic once I saw this topic, but went on vacations recently and I could finish the sample.

Here is a sample:
https://github.com/hmadrigal/playground-dotnet/tree/master/MsDotNetCore.MySqlParser

I created where the grammar provided by workbench (https://github.com/mysql/mysql-workbench/tree/8.0/library/parsers/grammars) has been adapted to work on .NET Core (using .NET standard for portability)

I created this sample project, it is not meant for production, and I am not an ANTLR expert, but I love coding and parsers.

Take a look at README.md and https://github.com/hmadrigal/playground-dotnet/blob/master/MsDotNetCore.MySqlParser/MySqlParser.TerminalApp/MySqlParserTester.cs could be your bootstrap. If I ever get to write a blog post I'll share the notes in here.

英文:

I was interested on this topic once I saw this topic, but went on vacations recently and I could finish the sample.

Here is a sample:
https://github.com/hmadrigal/playground-dotnet/tree/master/MsDotNetCore.MySqlParser

I created where the grammar provided by workbench (https://github.com/mysql/mysql-workbench/tree/8.0/library/parsers/grammars) has been adapted to work on .NET Core (using .NET standard for portability)

I created this sample project, it is not meant for production, and I am not an ANTLR expert, but I love coding and parsers.

Take a look at README.md and https://github.com/hmadrigal/playground-dotnet/blob/master/MsDotNetCore.MySqlParser/MySqlParser.TerminalApp/MySqlParserTester.cs could be your bootstrap. If I ever get to write a blog post I'll share the notes in here.

huangapple
  • 本文由 发表于 2020年1月6日 23:39:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定