在 Eclipse Luna (4.4) 上设置 ANTLR 4 IDE 时遇到问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30128961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:37:01  来源:igfitidea点击:

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

javaeclipseantlrantlr4

提问by amura.cxg

I'm trying to install the ANTLR 4 IDEon 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.

我正在尝试在 Eclipse Luna (4.4) 上安装ANTLR 4 IDE。我已经从 Marketplace 安装了它,但我不知道如何创建一个包含 ANTLR 4 Lexer/Parser 的项目。

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.

当我去创建一个新项目时,我没有看到 ANTLR 4 的任何选项。我尝试创建一个 .g4 文件,它在编辑器中打开,但是当我保存它时,它什么也没做。

回答by amura.cxg

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)

我在互联网上环顾四周,找到了一些我拼凑起来的资源,并通过反复试验找到了解决方案。下面是我在我的几台机器上使用的指南,用于在 Eclipse 中设置 ANTLR 4 IDE。我想我应该在这里分享它并为其他人省去 Google 搜索数小时的麻烦(希望如此)

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)
  • Eclipse 4.4 Luna Xtext Complete SDK(需2.7.3版本)
  • Eclipse Faceted 项目框架(使用 3.4.0 测试) Eclipse Faceted
  • 项目框架 JDT Enablement(Tested with 3.4.0) ANTLR 4 SDK A
  • antlr-4.x-complete.jar 的副本(撰写本文时为 4.5)

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 Withtextbox
    3. Hit Enter and wait for the list to load (this will take a few moments)
    4. Expand the Xtextnode 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/lunain the Work Withtextbox
    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 Frameworkand 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
  1. 安装 Eclipse
    1. https://www.eclipse.org/downloads/下载
  2. 安装 XText 2.7.3
    1. Help > Install New Software...
    2. http://download.eclipse.org/modeling/tmf/xtext/updates/composite/releases/Work With文本框中输入
    3. 按 Enter 并等待列表加载(这将需要一些时间)
    4. 展开Xtext节点并检查Xtext Complete SDK(确保版本为2.7.3x)
    5. 单击Next,同意 EULA,然后单击完成
    6. 让安装程序完成并重新启动 Eclipse
  3. 安装分面项目框架
    1. Help > Install New Software...
    2. http://download.eclipse.org/releases/lunaWork With文本框中输入
    3. 按 Enter 并等待列表加载(这将需要一些时间)
    4. 在过滤器文本框中输入 Facet
    5. 选择Eclipse Faceted Project FrameworkEclipse Faceted Project Framework JDT Enablement
    6. 单击Next,同意 EULA,然后单击完成
    7. 让安装程序完成并重新启动 Eclipse
  4. 安装 ANTLR 4 IDE
    1. Help > Eclipse Marketplace...
    2. 搜索 antlr
    3. 选择ANTLR 4 IDE(确保它是 ANTLR 4 IDE 而不是 ANTLR IDE)
    4. 点击安装
    5. 如果提示并重新启动 Eclipse,让安装程序完成单击确定
  5. 获取antlr-4.x-complete.jar的副本
    1. 这里下载文件
    2. 把它保存在你会记得的地方

Creating an ANTRL 4 Project

创建 ANTRL 4 项目

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 Taband 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 Facetsand click Convert to faceted form...(if you don't see this see step 3 of setup)
  6. Check the Javaproject 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 Sourcetab
  9. Click Add Folder...and check Project > target > generated-sources > antlr4, click OK
  10. Click the Librariestab
  11. Add External JARs..., find your copy of antlr-4.x-complete.jar, click Open
  12. Go to ANTLR 4 > Tool, click Applyif 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 Yesto rebuild, click OKto exit the properties
  1. File > New Project > Project
  2. 展开General Tab并选择ANTLR 4 Project(如果您没有看到此内容,请参阅设置的第 4 步)
  3. 单击Next,为项目命名并单击Finish
  4. 项目完成后,右键单击该项目,然后单击 Properties
  5. 转到Project Facets并单击Convert to faceted form...(如果您没有看到此内容,请参阅设置的第 3 步)
  6. 检查Java项目方面并单击Apply(如果您没有看到此内容,请参阅设置的第 3 步)
  7. 单击OK,让解决方案重建,再次打开属性
  8. 转到Java Build Path,单击Source选项卡
  9. 单击Add Folder...并检查Project > target > generated-sources > antlr4,单击OK
  10. 单击Libraries选项卡
  11. Add External JARs...,找到您的 副本antlr-4.x-complete.jar,单击Open
  12. 转到ANTLR 4 > ToolApply如果出现弹出窗口,请单击
  13. 查看 Enable project specific settings
  14. 单击Add,找到您的 副本antlr-4.x-complete.jar,单击Open
  15. 查看 4.x
  16. 点击Apply,点击Yes重建,点击OK退出属性

Test

测试

Create a new class with the following code and try running. In the console write Hello thereand Ctrl+ zto send EOF to the input stream

使用以下代码创建一个新类并尝试运行。在控制台写Hello thereCtrl+z发送 EOF 到输入流

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 > Toolcheck 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 > 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

  1. 如果您在尝试ANTLR 4 > Tool检查 Xtext 版本时看到错误,2.8.0 会导致工具窗口中出现错误
  2. 在第 8 步中,如果您更改了 ANTLR 生成的目录,请使用该目录
  3. 如果您注意到您添加的 ANTLR 版本ANTLR 4 > Tool > Distributions消失了,这似乎没问题

    • 检查您的构建输出以查看它使用的是什么工具,即使它消失了,它仍应使用您添加的 JAR。这是我的样子:

    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