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
Setup
Help > Install New Software...
http://download.eclipse.org/modeling/tmf/xtext/updates/composite/releases/
in the Work With
textboxXtext
node and check Xtext Complete SDK
(ensure the version is 2.7.3x)Next
, agree to the EULA, and click finishHelp > Install New Software...
http://download.eclipse.org/releases/luna
in the Work With
textboxFacet
Eclipse Faceted Project Framework
and Eclipse Faceted Project Framework JDT Enablement
Next
, agree to the EULA, and click finishHelp > Eclipse Marketplace...
antlr
ANTLR 4 IDE
(make sure it's ANTLR 4 IDE not ANTLR IDE)Creating an ANTRL 4 Project
I found most of this information here, the rest was reading errors and guessing
File > New Project > Project
General Tab
and select ANTLR 4 Project
(if you don't see this see step 4 of setup)Next
, give the project a name and click Finish
Properties
Project Facets
and click Convert to faceted form...
(if you don't see this see step 3 of setup)Java
project facet and click Apply
(if you don't see this see step 3 of setup)OK
, let the solution rebuild, open the properties againJava Build Path
, click the Source
tabAdd Folder...
and check Project > target > generated-sources > antlr4
, click OK
Libraries
tabAdd External JARs...
, find your copy of antlr-4.x-complete.jar
, click Open
ANTLR 4 > Tool
, click Apply
if a pop-up appearsEnable project specific settings
Add
, find your copy of antlr-4.x-complete.jar
, click Open
4.x
Apply
, click Yes
to rebuild, click OK
to exit the propertiesTest
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
ANTLR 4 > Tool
check your Xtext version, 2.8.0 causes an error in the tool windowIf you notice that the version of ANTLR you've added to ANTLR 4 > Tool > Distributions
disappears this seems to be ok
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