antlr学习笔记

来源:互联网 发布:想要女生的身体 知乎 编辑:程序博客网 时间:2024/05/19 00:50

Requirements

  • Eclipse 4.3 (Kepler及以上版本)
  • XText 2.5.x/2.6

Installation

ANTLR 4 IDE必须通过旧的方式安装. 请遵循以下步骤:

  • Open Eclipse Kepler (4.3)
  • 点击: Help > Install New Software...
  • 需要先安装 XText 2.5.x/2.6
  • 复制并粘贴这个URL:http://download.eclipse.org/modeling/tmf/xtext/updates/composite/releases/ 到 Work with 区域
  • 点回车
  • 选择 XText. 注意: 不用选Xtend
  • 安装完后Eclipse会重启, 复制并粘贴这个URL: http://dl.bintray.com/jknack/antlr4ide 到Work with 区域
  • 选择: ANTLR 4 SDK IDE. 注意: 如果你看不到就打开分类
  • 一直点Next到Finish

Manual Installation

You can download the plugin and the save it in the dropins directory of your Eclipse installation.//上面安装成功了就没有使用手动安装

Usage

新的IDE使用非常简单 所有的以*.g4 为拓展明的文件都将用 ANTLR 4 Editor打开

Code Generation

如果语法是有效的代码是自动生成并保存的. 如果你想关闭这个特性 在: Window > Preferences > ANTLR 4 > Tool 下取消"Tool is activated" 选项. 你还可以设置一些别的选项

生成的代码保存在 target/generated-sources/antlr4 (和antlr4-maven-plugin是一个地址)

Manual Code Generation

You can fire a code generation action by selecting a *.g4 文件 from the Package Explorer, 只需要点击:Run As > ANTLR 4.

A default ANTLR 4 launch configuration will be created. You can modify the generated launch configuration by going to: Run > External Tools > External Tools Configurations... from there you will see the launch configuration and how to set or override code generation options

Syntax Diagrams

To open the Syntax Diagram view go to: Window > Show View > Other search and select: Syntax Diagram

Build with Maven 3.x

(懒得翻译,直接点第2步的网址http://maven.apache.org再点download,见英文后面)
  1. Fork and clone the repository from github
  2. Download and install Maven 3.x(安装过程在下面)
  3. Open a shell console and type: cd antlr4ide
  4. Build the project with: mvn clean package
  5. It takes a while to download and configure Eclipse dependencies, so be patient
  6. Wait for a: BUILD SUCCESS message

Windows下安装maven3.x

  1. 在下载maven.apach.org/download下下载 apache-maven-3.2.3-bin.zip 并解压你要安装的地方 比如我解压到了 (中间那个解压的apache-maven-3.2.3-bin文件不要了)
  2. 增加一个叫 M2_HOME 的环境变量,类型为用户变量值为 D:\apache-maven-3.2.3 . Be sure to omit any quotation marks around the path even if it contains spaces. 先不要选确定
  3. 在增加一个叫 M2 的环境变量值为 %M2_HOME%\bin.
  4. 可选项: 增加 MAVEN_OPTS 环境变量(用户变量)指定 JVM 属性, 例如,值为 -Xms256m -Xmx512m. 这个环境变量可以被用来给Maven提供更多选项.
  5. 在 Path 增加一个 %M2% to add Maven available in the command line.
  6. 确保 JAVA_HOME 在环境变量中不管是系统变量还是用户变量,并且值为 JDK的位置,确保 %JAVA_HOME%\bin 在 Path 环境变量中.
  7. 打开命令行输入 mvn --version 安装成功.

Eclipse setup

  1. Make sure you built the project first.
  2. Open Eclipse Kepler (4.3)
  3. Install Xtext 2.5.3
  4. Copy and paste this url:http://download.eclipse.org/modeling/tmf/xtext/updates/composite/releases/ in the Work with text field
  5. Hit Enter
  6. Choose XText 2.5.3. NOTE: DON'T confuse with Xtend, you must choose Xtext
  7. Restart Eclipse after installing Xtext
  8. Import the project into Eclipse
  9. Go to: File > Import... then General > Existing Projects into Workspace
  10. Choose project root antlr4ide
  11. Enabled: Search for nested projects
  12. Finish

You don't need any extra Eclipse plugin (like m2e or similar). Project metadata is on git so all you need to do is: mvn clean package and then import the projects into Eclipse.

0 0
原创粉丝点击