How to configure antlr4 plugin for Intellij IDEA

前端 未结 2 1602
长发绾君心
长发绾君心 2021-01-31 04:59

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 manua

2条回答
  •  一向
    一向 (楼主)
    2021-01-31 05:04

    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

提交回复
热议问题