Using Antlr with Eclipse on Windows
Alan Oursland
This short and simple tutorial is written for Windows but uses platform independent tools.
Simple adjust path names to adapt the tutorial to your platform.
Setup Environment
Install Eclipse
Unpack Antlr (assumed at "C:\antlr-2.7.2\")
Start Eclipse
Create a new Java project in Eclipse named "antl-examples"
File-New-Project
Select Java and Java Project.
Press Next.
Enter "antl-examples" as the project name
Press Next.
Make sure the src folder is "antlr-example/src"
Press Finish
Add the antlr.jar to the project classpath
Right click on the project name in the package explorer.
Select Properties
Select the Libraries tab.
Press the "Add External Jar" button.
Select "C:\antlr-2.7.2\antlr.jar" and press Open.
Press Ok to close the Properties dialog.
Set up Antlr as an External Tool
Run-External Tools-Configure
Press New
As the Name: "Antlr Compiler"
As the Tool Location: "C:\jdk1.4.0\bin\javaw.exe" (you may need to adjust the path)
As the Tool Arguments: "-classpath C:\antlr-2.7.2\antlr.jar antlr.Tool ${resource_name}"
These arguments:
- set the classpath
- specify antlr.Tool as the main class
- specify ${resource_name} as the file for antlr.Tool to process
"${resource_name}" will be the name of a selected file.
You can set this using the "Browse Variables" button.
As the Working Directory: "${container_loc}"
You can set this by selecting "Selected resource's container location" under the "Browse Options" button.
Verify that "Block until tool terminates" and "Show execution log on console" are both checked.
Press OK.
Create "t.g" under the src directory and copy from "Getting Started".
Select "t.g"
Run-External Tools-Antlr Compiler
Refresh "src" (select and press F5, or right-click and select Refresh)
Eclipse will find and compile the newly created files.
You need to refresh the directories each time antlr is run.
Create Main.java from "Getting Started"
Run the example
Open Main.java
Run-Run
Select "Java Application"
Press New
As Name: Antlr Example
As Project: antlr-examples (should default to this)
As Main class: Main (should default to this)
Press Apply
Press Run
Select the Console View tab at the bottom of the screen
Click in the console view.
Enter your name and press enter.
Congratulations!
You have just created your first Antlr program.
Back