Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

java - Trouble Setting Up ANTLR 4 IDE on Eclipse Luna (4.4)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

  1. Install Eclipse
    1. Download it from https://www.eclipse.org/downloads/
  2. Install XText 2.7.3
    1. Go to Help > Install New Software...
    2. Enter http://download.eclipse.org/modeling/tmf/xtext/updates/composite/releases/ in the Work With textbox
    3. Hit Enter and wait for the list to load (this will take a few moments)
    4. Expand the Xtext node and check Xtext Complete SDK (ensure the version is 2.7.3x)
    5. Click Next, agree to the EULA, and click finish
    6. Let the installer finish and restart Eclipse
  3. Install Faceted Project Framework
    1. Go to Help > Install New Software...
    2. Enter http://download.eclipse.org/releases/luna in the Work With textbox
    3. Hit Enter and wait for the list to load (this will take a few moments)
    4. In the filter text box enter Facet
    5. Select Eclipse Faceted Project Framework and Eclipse Faceted Project Framework JDT Enablement
    6. Click Next, agree to the EULA, and click finish
    7. Let the installer finish and restart Eclipse
  4. Install ANTLR 4 IDE
    1. Go to Help > Eclipse Marketplace...
    2. Search for antlr
    3. Choose ANTLR 4 IDE (make sure it's ANTLR 4 IDE not ANTLR IDE)
    4. Click Install
    5. Let the installer finish clicking ok if it prompts and restart Eclipse
  5. Obtain a copy of antlr-4.x-complete.jar
    1. Download the file from here
    2. 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

  1. Go to File > New Project > Project
  2. Expand the General Tab and select ANTLR 4 Project (if you don't see this see step 4 of setup)
  3. Click Next, give the project a name and click Finish
  4. Once the project is complete right click the project and click Properties
  5. Go to Project Facets and click Convert to faceted form... (if you don't see this see step 3 of setup)
  6. Check the Java project facet and click Apply (if you don't see this see step 3 of setup)
  7. Click OK, let the solution rebuild, open the properties again
  8. Go to Java Build Path, click the Source tab
  9. Click Add Folder... and check Project > target > generated-sources > antlr4, click OK
  10. Click the Libraries tab
  11. Add External JARs..., find your copy of antlr-4.x-complete.jar, click Open
  12. Go to ANTLR 4 > Tool, click Apply if a pop-up appears
  13. Check Enable project specific settings
  14. Click Add, find your copy of antlr-4.x-complete.jar, click Open
  15. Check 4.x
  16. 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

  1. 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
  2. In step 8, if you've changed the directory ANTLR generates it's sources use that directory
  3. 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:JavaLibantlr-4.5-complete.jar) Hello.g4 -o C:Usersusernameworkspaceprojectargetgenerated-sourcesantlr4 -listener -no-visitor -encoding UTF-8


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...