问题描述
我有一个带有 Antlr3 语法文件的 C# 解决方案,我正在尝试升级到 Anltr4.事实证明,语法是最简单的部分(它变得更好,而且大小只有三分之一!).生成解析器原来是棘手的部分.
I have a C# solution with an Antlr3 grammar file, and I'm trying to upgrade to Anltr4. It turns out the grammar was the easy part (it became better, and one third the size!). Generating the parser turned out to be the tricky part.
在旧解决方案中,我只是在语法文件更改时运行 AntlrWorks 来更新词法分析器和解析器 .cs 文件.词法分析器和解析器与语法直接包含在同一个项目中,因此解析器周围的框架可以直接使用它们.
In the old solution I merely ran AntlrWorks to update the lexer and parser .cs files when the grammar file changed. The lexer and parser were included directly in the same project as the grammar so the framework around the parser could make use of them directly.
对于 C# 的 Antlr4 目标,我注意到(至少在默认情况下)生成的 Parser C# 类的输出路径位于中间目录中,例如对象调试.这意味着我不能直接在同一个项目中使用解析器.如果我希望生成的源在我的 sln 中可用,我应该更改输出路径吗?我不想在我的 sln 中使用它吗?
With the Antlr4 targets for C# I noticed that (at least by default) the output path of the generated Parser C# classes is in the intermediate directory, e.g. objDebug. This means I can't use the parser directly in the same project. Am I supposed to change the output path if I want the generated source usable in my sln? Don't I want it in my sln?
我尝试为解析器生成制作一个单独的项目,即一个包含仅语法文件的项目.当使用 Antlr4 目标构建此项目时,可以从需要解析器类的项目中引用生成的程序集.但是,如果该项目包含在解决方案中,我仍然没有在 Visual Studio 中获得任何智能感知,因为它的智能感知基于已加载项目的源文件,因此它仍然不太可用.我可以让它正常工作的唯一方法是构建然后卸载解析器项目,并让其他项目将其作为程序集文件而不是项目引用.
I tried making a separate project for the parser generation, that is, a project containing only the grammar file. When this project is built using the Antlr4 targets, the resulting assembly can be referenced from projects needing the parser classes. However, if this project is included in the solution I still don't get any intellisense in visual studio since it bases its intellisense on source files for loaded projects, so it is still not quite usable. The only way I could get it working properly was to build then unload the parser project, and have other projects reference it as an assembly file rather than a project.
在查看 antlr-interest 列表后,我发现这个线程表明智能感知的问题可能是 Resharpers 故障.如果是这种情况,那么我的问题是如何在我的解决方案中继续使用 Antlr4 和 Resharper?https://groups.google.com/forum/#!topic/antlr-discussion/QSuJXphaBDg
After looking on the antlr-interest list I found this thread indicating that the problem with intellisense may be Resharpers fault. If this is the case, then my question is rather how to keep using both Antlr4 and Resharper in my solution? https://groups.google.com/forum/#!topic/antlr-discussion/QSuJXphaBDg
推荐答案
您遇到了 ReSharper 的一个已知错误.ANTLR 3 和 ANTLR 4 C# 构建集成是可靠的,并且使用 Microsoft 用于其他语言和代码生成任务的长期建立的模式.您的选择是:
You are encountering a known bug with ReSharper. The ANTLR 3 and ANTLR 4 C# build integration is reliable, and uses long-established patterns that Microsoft uses with other languages and code generation tasks. Your options are:
- 停止使用 ReSharper
- 让 ReSharper 作者解决对此功能缺乏支持的问题,或者
- 修改 ANTLR 4 目标以解决 ReSharper 中的限制,这将在构建中引入其他问题,这些问题可能会或可能不会被您接受.
选项 1 无疑是最便宜的,并且最有可能为您的项目提供可靠的长期支持.
Option 1 is certainly the least expensive, and most likely to provide reliable long-term support for your projects.
这篇关于Antlr4 C# 目标和生成文件的输出路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!