在 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
Trouble Setting Up ANTLR 4 IDE on Eclipse Luna (4.4)
提问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
设置
- 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 With
textbox - Hit Enter and wait for the list to load (this will take a few moments)
- Expand the
Xtext
node 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/luna
in theWork 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
andEclipse 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
- 安装 Eclipse
- 安装 XText 2.7.3
- 去
Help > Install New Software...
http://download.eclipse.org/modeling/tmf/xtext/updates/composite/releases/
在Work With
文本框中输入- 按 Enter 并等待列表加载(这将需要一些时间)
- 展开
Xtext
节点并检查Xtext Complete SDK
(确保版本为2.7.3x) - 单击
Next
,同意 EULA,然后单击完成 - 让安装程序完成并重新启动 Eclipse
- 去
- 安装分面项目框架
- 去
Help > Install New Software...
http://download.eclipse.org/releases/luna
在Work With
文本框中输入- 按 Enter 并等待列表加载(这将需要一些时间)
- 在过滤器文本框中输入
Facet
- 选择
Eclipse Faceted Project Framework
和Eclipse Faceted Project Framework JDT Enablement
- 单击
Next
,同意 EULA,然后单击完成 - 让安装程序完成并重新启动 Eclipse
- 去
- 安装 ANTLR 4 IDE
- 去
Help > Eclipse Marketplace...
- 搜索
antlr
- 选择
ANTLR 4 IDE
(确保它是 ANTLR 4 IDE 而不是 ANTLR IDE) - 点击安装
- 如果提示并重新启动 Eclipse,让安装程序完成单击确定
- 去
- 获取antlr-4.x-complete.jar的副本
- 从这里下载文件
- 把它保存在你会记得的地方
Creating an ANTRL 4 Project
创建 ANTRL 4 项目
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 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 Facets
and clickConvert to faceted form...
(if you don't see this see step 3 of setup) - Check the
Java
project 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 theSource
tab - Click
Add Folder...
and checkProject > target > generated-sources > antlr4
, clickOK
- Click the
Libraries
tab Add External JARs...
, find your copy ofantlr-4.x-complete.jar
, clickOpen
- Go to
ANTLR 4 > Tool
, clickApply
if 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
, clickYes
to rebuild, clickOK
to exit the properties
- 去
File > New Project > Project
- 展开
General Tab
并选择ANTLR 4 Project
(如果您没有看到此内容,请参阅设置的第 4 步) - 单击
Next
,为项目命名并单击Finish
- 项目完成后,右键单击该项目,然后单击
Properties
- 转到
Project Facets
并单击Convert to faceted form...
(如果您没有看到此内容,请参阅设置的第 3 步) - 检查
Java
项目方面并单击Apply
(如果您没有看到此内容,请参阅设置的第 3 步) - 单击
OK
,让解决方案重建,再次打开属性 - 转到
Java Build Path
,单击Source
选项卡 - 单击
Add Folder...
并检查Project > target > generated-sources > antlr4
,单击OK
- 单击
Libraries
选项卡 Add External JARs...
,找到您的 副本antlr-4.x-complete.jar
,单击Open
- 转到
ANTLR 4 > Tool
,Apply
如果出现弹出窗口,请单击 - 查看
Enable project specific settings
- 单击
Add
,找到您的 副本antlr-4.x-complete.jar
,单击Open
- 查看
4.x
- 点击
Apply
,点击Yes
重建,点击OK
退出属性
Test
测试
Create a new class with the following code and try running. In the console write Hello there
and Ctrl+ zto send EOF to the input stream
使用以下代码创建一个新类并尝试运行。在控制台写Hello there
和Ctrl+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
笔记
- 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
- 如果您在尝试
ANTLR 4 > Tool
检查 Xtext 版本时看到错误,2.8.0 会导致工具窗口中出现错误 - 在第 8 步中,如果您更改了 ANTLR 生成的目录,请使用该目录
如果您注意到您添加的 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