24

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

Morpheus
  • 1,672
  • 5
  • 25
  • 38

2 Answers2

26

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

Community
  • 1
  • 1
J. Simpson
  • 301
  • 3
  • 8
  • I get the error message that the generated code is version 4.5 but that the Antlr runtime is 4.4. I do not know where this version 4.4 comes from. I tried using antlr-4.5-complete.jar and the maven one. Where can I determine or check (or even change) the version of the runtime? – Pille Jul 05 '15 at 21:13
  • @J. Simpson: Do you know how to configure/disable the generated comment that gets inserted in the generated source files? – Mats Rydberg Feb 29 '16 at 08:03
3

Have you looked at the GitHub project page, IntelliJ Idea Plugin for ANTLR v4? You will find many screenshots with explanations.

Directly install from here: https://plugins.jetbrains.com/plugin/7358-antlr-v4-grammar-plugin/

You should also see tool windows such as the preview window.

Marcono1234
  • 2,746
  • 12
  • 29
Terence Parr
  • 5,722
  • 23
  • 32
  • I guess you are talking about [this] . Im(https://theantlrguy.atlassian.net/wiki/display/ANTLR4/Java+IDE+Integration) I'm sorry I don't see anything other than how to add the plugin to IDE. – Morpheus May 10 '14 at 02:07
  • And yes, I see the Parser Tree window. But how to run the the grammar ? g4 file is standing right there that I can't do anything. And btw, If it was simple and obvious as eclipse plugin I wont ask this question. – Morpheus May 10 '14 at 02:09
  • right click in editor or in navigator and "Test ANTLR rule"? Are you looking for a RUN button? Also feel free to read the doc – Terence Parr May 10 '14 at 22:08
  • I tried that. What it does is giving me bunch of null pointer exceptions in the plugin. But I guess, that should be a separate topic then. – Morpheus May 11 '14 at 14:18
  • please create issue for the bug :) Are you using 1.4 plugin? Plugin works with 12.1 and up in intellij. – Terence Parr May 12 '14 at 04:51