idea使用antlr_如何为Intellij IDEA配置antlr4插件

本文介绍了在IntelliJ IDEA中配置ANTLR4插件的详细步骤,包括如何设置ANTLR文件,指定输出目录,以及如何生成ANTLR识别器。需要注意的是,ANTLR插件的输出目录默认为用户目录,需要设置为绝对路径或项目内的gen目录,并标记为生成源代码目录,否则可能导致编译问题。
摘要由CSDN通过智能技术生成

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 ?

Thank you

解决方案

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

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.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值