在 Eclipse Luna (4.4) 上设置 ANTLR 4 IDE 时遇到问题 [英] Trouble Setting Up ANTLR 4 IDE on Eclipse Luna (4.4)
问题描述
我正在尝试在 Eclipse Luna (4.4) 上安装 ANTLR 4 IDE.我已经从 Marketplace 安装了它,但我不知道如何创建一个包含 ANTLR 4 Lexer/Parser 的项目.
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.
当我去创建一个新项目时,我没有看到 ANTLR 4 的任何选项.我尝试创建一个 .g4 文件,它在编辑器中打开,但当我保存它时,它什么也没做.
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.
推荐答案
我环顾了整个互联网,找到了一些我拼凑起来的资源,并通过反复试验找到了解决方案.下面是我在我的几台机器上使用的指南,用于在 Eclipse 中设置 ANTLR 4 IDE.我想我应该在这里分享它并为其他人省去 Google 搜索数小时的麻烦(希望如此)
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 4.4 Luna Xtext Complete SDK(需要 2.7.3 版)
- Eclipse Faceted 项目框架(使用 3.4.0 测试)Eclipse Faceted
- 项目框架 JDT 启用(使用 3.4.0 测试)ANTLR 4 SDK A
- antlr-4.x-complete.jar 的副本(撰写本文时为 4.5)
设置
- 安装 Eclipse
- Install Eclipse
- 转到
帮助>安装新软件...
- 在
Work With
文本框中输入http://download.eclipse.org/modeling/tmf/xtext/updates/composite/releases/
- 按 Enter 并等待列表加载(这将需要一些时间)
- 展开
Xtext
节点,勾选Xtext Complete SDK
(确保版本为2.7.3x) - 点击
Next
,同意EULA,然后点击完成 - 让安装程序完成并重新启动 Eclipse
- 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
- 转到
帮助>安装新软件...
- 在
Work With
文本框中输入http://download.eclipse.org/releases/luna
- 按 Enter 并等待列表加载(这将需要一些时间)
- 在过滤器文本框中输入
Facet
- 选择
Eclipse Faceted Project Framework
和Eclipse Faceted Project Framework JDT Enablement
- 点击
Next
,同意EULA,然后点击完成 - 让安装程序完成并重新启动 Eclipse
- 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
- 转到
帮助>Eclipse 市场...
- 搜索
antlr
- 选择
ANTLR 4 IDE
(确保它是ANTLR 4 IDE而不是ANTLR IDE) - 点击安装
- 让安装程序在提示时完成单击确定并重新启动 Eclipse
- 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
- 从这里 下载文件
- 把它保存在你会记得的地方
创建 ANTRL 4 项目
我在这里找到了大部分信息,剩下的就是阅读错误和猜测
I found most of this information here, the rest was reading errors and guessing
- 转到<代码>文件>新项目项目
- 展开
General 选项卡
并选择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 >目标 >生成源 >antlr4
,点击OK
- 点击
库
标签 Add External JARs...
,找到antlr-4.x-complete.jar
的副本,点击Open
- 转到<代码>ANTLR 4 >工具,出现弹窗点击
Apply
- 勾选
启用项目特定设置
- 点击
Add
,找到antlr-4.x-complete.jar
的副本,点击Open
- 检查
4.x
- 点击
Apply
,点击Yes
重建,点击OK
退出属性
- 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
测试
使用以下代码创建一个新类并尝试运行.在控制台中写入 Hello there
和 Ctrl + z 将 EOF 发送到输入流
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
}
}
注意事项
- 如果您在尝试进入
ANTLR 4 > 时看到错误工具
检查你的Xtext版本,2.8.0导致工具窗口出错 - 在第 8 步中,如果您更改了 ANTLR 生成的目录,请使用该目录
如果您注意到您添加到
ANTLR 4 > 的 ANTLR 版本工具 >Distributions
消失了,这似乎没问题
- 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
- 检查您的构建输出以查看它使用的是什么工具,即使它消失了,它仍应使用您添加的 JAR.这是我的样子:
ANTLR 工具 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
这篇关于在 Eclipse Luna (4.4) 上设置 ANTLR 4 IDE 时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!