如何为Intellij IDEA配置antlr4插件 [英] How to configure antlr4 plugin for Intellij IDEA

查看:2252
本文介绍了如何为Intellij IDEA配置antlr4插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到处寻找如何配置IntelliJ IDEA的antlr4插件。但我找不到任何东西。我只能安装插件。如果我为一个空项目手动添加.g4文件,我会在右键菜单中看到生成ANTLR识别器选项。就这些。我认为这是非常有前途的插件。任何人都可以告诉/指导我如何继续使用该插件?

I looked all over the place for how to configure the antlr4 plugin for IntelliJ IDEA. But I can't find anything. I was only able to install the plugin. If I add .g4 files manually for a empty project I get the "Generate ANTLR Recognizer" option in right click menu. That is all. I thought It was very promising plugin. Can anyone please tell/direct me how to proceed with the plugin ?

谢谢

推荐答案

我在IntelliJ 14上安装了ANTLR插件,并且能够让它运行起来。一些小事情比预期更难。

I installed the ANTLR plugin on IntelliJ 14 and was able to get it working. A couple little things made it harder than expected.


  • 插件设置是每个ANTLR文件(.g4)所以突出显示.g4文件你想要生成,然后右键单击以配置ANTLR。否则,ANTLR相关的上下文菜单显示为灰色。

  • 输出目录设置相对于您的主目录,因此如果您设置输出目录但看不到任何内容,请尝试查找您的主目录,或设置绝对路径。如果您根本没有设置输出目录,它似乎会生成相对于项目目录的Java工件。这就是我能够让它工作的方式。

  • 右键单击ANTLR语法文件并从上下文菜单中选择Generate ANTLR Recognizer。主Build菜单下的Compile'file.g4'选项对我没有任何作用。

  • 接下来将输出目录(默认情况下,项目根目录中的gen目录)标记为生成的源。右键单击gen并在上下文菜单底部附近应为Mark Directory As,并将Generated Sources Root作为子菜单。如果你没有将ANTLR输出目录标记为生成的源,IntelliJ将无法找到词法分析器和解析器的Java类文件,因此它不会编译它们,并且它不会清除你的语法错误您引用ANTLR词法分析器/解析器的其他程序文件。

  • 看来该插件使用的是其包含的ANTLR版本,而不是您在项目中使用的ANTLR版本作为库,所以使用插件可以生成无法在项目中编译的Java源代码。

  • the plugin settings are per ANTLR file (.g4) so highlight the .g4 file you want to generate, then right click to configure ANTLR. Otherwise, the ANTLR related context menus are greyed out.
  • the output directory setting is relative to your home directory, so if you set the output directory and don't see anything, try looking in your home directory, or set an absolute path. If you don't set the output directory at all, it appears to generate the Java artifacts relative to the project directory. That's how I was able to get it working.
  • right click on the ANTLR grammar file and select Generate ANTLR Recognizer from the context menu. The Compile 'file.g4' option under the main Build menu does nothing for me.
  • next mark the output directory (by default, the gen directory in the project root) as generated source. Right click on gen and near the bottom of the context menu should be 'Mark Directory As', with 'Generated Sources Root' as a sub menu. If you don't mark the ANTLR output directory as generated sources, IntelliJ will not be able to find the Java class files for the lexer and parser, so it won't compile them, and it won't clear the syntax errors in your other program files where you reference the ANTLR lexer/parser.
  • it appears that the plugin uses the version of ANTLR it contains rather than the version of ANTLR you are using in your project as a library, so using the plugin it is possible to generate Java source code that will not compile within your project.

你可以看到一个简单的例子这里有一个Java ANTLR项目: https://stackoverflow.com/a/21552478/4239384

You can see a simple example of a Java ANTLR project here: https://stackoverflow.com/a/21552478/4239384

这篇关于如何为Intellij IDEA配置antlr4插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆