I'm trying to install the ANTLR 4 IDE on Eclipse Luna (4.4). I've installed it from the Marketplace but I have no idea how to create a project that has an ANTLR 4 Lexer/Parser in it.
When I go to create a new project I don't see any options for ANTLR 4. I tried creating a .g4 file and it opens in the editor but when I save it doesn't do anything.
I looked around all over the internet and found a handful of resources that I cobbled together and found a solution by trial and error.
Below is a guide that I've used on a few of my machines to get ANTLR 4 IDE setup in Eclipse. I figured I should share it here and save others the trouble of Google searching for hours (hopefully)
Prerequisites
- Eclipse 4.4 Luna Xtext Complete SDK(Needs to be version 2.7.3)
- Eclipse Faceted Project Framework (Tested with 3.4.0) Eclipse Faceted
- Project Framework JDT Enablement(Tested with 3.4.0) ANTLR 4 SDK A
- A copy of the antlr-4.x-complete.jar (4.5 at the time of writing)
Setup
- Install Eclipse
- Download it from https://www.eclipse.org/downloads/
- Install XText 2.7.3
- Go to
Help > Install New Software...
- Enter
http://download.eclipse.org/modeling/tmf/xtext/updates/composite/releases/
in the Work With
textbox
- Hit Enter and wait for the list to load (this will take a few moments)
- Expand the
Xtext
node and check Xtext Complete SDK
(ensure the version is 2.7.3x)
- Click
Next
, agree to the EULA, and click finish
- Let the installer finish and restart Eclipse
- Install Faceted Project Framework
- Go to
Help > Install New Software...
- Enter
http://download.eclipse.org/releases/luna
in the Work With
textbox
- Hit Enter and wait for the list to load (this will take a few moments)
- In the filter text box enter
Facet
- Select
Eclipse Faceted Project Framework
and Eclipse Faceted Project Framework JDT Enablement
- Click
Next
, agree to the EULA, and click finish
- Let the installer finish and restart Eclipse
- Install ANTLR 4 IDE
- Go to
Help > Eclipse Marketplace...
- Search for
antlr
- Choose
ANTLR 4 IDE
(make sure it's ANTLR 4 IDE not ANTLR IDE)
- Click Install
- Let the installer finish clicking ok if it prompts and restart Eclipse
- Obtain a copy of antlr-4.x-complete.jar
- Download the file from here
- Save it somewhere you'll remember
Creating an ANTRL 4 Project
I found most of this information here, the rest was reading errors and guessing
- Go to
File > New Project > Project
- Expand the
General Tab
and select ANTLR 4 Project
(if you don't see this see step 4 of setup)
- Click
Next
, give the project a name and click Finish
- Once the project is complete right click the project and click
Properties
- Go to
Project Facets
and click Convert to faceted form...
(if you don't see this see step 3 of setup)
- Check the
Java
project facet and click Apply
(if you don't see this see step 3 of setup)
- Click
OK
, let the solution rebuild, open the properties again
- Go to
Java Build Path
, click the Source
tab
- Click
Add Folder...
and check Project > target > generated-sources > antlr4
, click OK
- Click the
Libraries
tab
Add External JARs...
, find your copy of antlr-4.x-complete.jar
, click Open
- Go to
ANTLR 4 > Tool
, click Apply
if a pop-up appears
- Check
Enable project specific settings
- Click
Add
, find your copy of antlr-4.x-complete.jar
, click Open
- Check
4.x
- Click
Apply
, click Yes
to rebuild, click OK
to exit the properties
Test
Create a new class with the following code and try running. In the console write Hello there
and Ctrl + z to send EOF to the input stream
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
public class HelloRunner
{
public static void main( String[] args) throws Exception
{
ANTLRInputStream input = new ANTLRInputStream( System.in);
HelloLexer lexer = new HelloLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
HelloParser parser = new HelloParser(tokens);
ParseTree tree = parser.r(); // begin parsing at rule 'r'
System.out.println(tree.toStringTree(parser)); // print LISP-style tree
}
}
Notes
- If you see an error when you try to go into
ANTLR 4 > Tool
check your Xtext version, 2.8.0 causes an error in the tool window
- In step 8, if you've changed the directory ANTLR generates it's sources use that directory
If you notice that the version of ANTLR you've added to ANTLR 4 > Tool > Distributions
disappears this seems to be ok
- Check your build output to see what tool it's using, it should still use the JAR you added even if it disappears. This is what mine looks like:
ANTLR Tool v4.5 (C:\JavaLib\antlr-4.5-complete.jar)
Hello.g4 -o C:\Users\username\workspace\project\target\generated-sources\antlr4 -listener -no-visitor -encoding UTF-8