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 theWork Withtextbox - Hit Enter and wait for the list to load (this will take a few moments)
- Expand the
Xtextnode and checkXtext 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
- Go to
- Install Faceted Project Framework
- Go to
Help > Install New Software... - Enter
http://download.eclipse.org/releases/lunain theWork Withtextbox - 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 FrameworkandEclipse Faceted Project Framework JDT Enablement - Click
Next, agree to the EULA, and click finish - Let the installer finish and restart Eclipse
- Go to
- 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
- Go to
- 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 Taband selectANTLR 4 Project(if you don't see this see step 4 of setup) - Click
Next, give the project a name and clickFinish - Once the project is complete right click the project and click
Properties - Go to
Project Facetsand clickConvert to faceted form...(if you don't see this see step 3 of setup) - Check the
Javaproject facet and clickApply(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 theSourcetab - Click
Add Folder...and checkProject > target > generated-sources > antlr4, clickOK - Click the
Librariestab Add External JARs..., find your copy ofantlr-4.x-complete.jar, clickOpen- Go to
ANTLR 4 > Tool, clickApplyif a pop-up appears - Check
Enable project specific settings - Click
Add, find your copy ofantlr-4.x-complete.jar, clickOpen - Check
4.x - Click
Apply, clickYesto rebuild, clickOKto 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 > Toolcheck 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 > Distributionsdisappears 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
来源:https://stackoverflow.com/questions/30128961/trouble-setting-up-antlr-4-ide-on-eclipse-luna-4-4