ANTLR v4 is a powerful tool used for building new programming languages and processing/translating structured text or binary files. ANTLR uses a grammar you create to generate a parser which can build and traverse a parse tree (or abstract syntax tree, AST). The parser consists of output files in a target language that you specify. ANTLR v4 supports several targets including: Java, C#, JavaScript, Python2, and Python3. Support for C++ is being worked on. For working in GUI IDEs, there are plug-ins for Visual Studio, Intellij, NetBeans, and Eclipse.
For general information, visit the ANTLR website. To get serious about ANTLR, check out the highly recommended book written by Terrence Parr (the guy who created ANTLR) The Definitive ANTLR 4 Reference.
Significant Version Info
ANTLR is distributed as a Java Jar file It can be downloaded here. As ANTLR is compiled as a jar file it subsequently requires the Java runtime environment to operate, if you do not have It can be downloaded here.
Once the ANTLR JAR file has been downloaded you can run ANTLR from the command line in the same way as any other JAR file:
Java -jar antlr-4.5.3-complete.jar
(Assuming you are operating in the same directory as the antlr-4.5.3-complete.jar file).
This should output something similar to this :
ANTLR Parser Generator Version 4.5.3
-o ___ specify output directory where all output is generated
-lib ___ specify location of grammars, tokens files
-atn generate rule augmented transition network diagrams
-encoding ___ specify grammar file encoding; e.g., euc-jp
-message-format ___ specify output style for messages in antlr, gnu, vs2005
-long-messages show exception details when available for errors and warnings
-listener generate parse tree listener (default)
-no-listener don't generate parse tree listener
-visitor generate parse tree visitor
-no-visitor don't generate parse tree visitor (default)
-package ___ specify a package/namespace for the generated code
-depend generate file dependencies
-D<option>=value set/override a grammar-level option
-Werror treat warnings as errors
-XdbgST launch StringTemplate visualizer on generated code
-XdbgSTWait wait for STViz to close before continuing
-Xforce-atn use the ATN simulator for all predictions
-Xlog dump lots of logging info to antlr-timestamp.log
other recommended actions for setup include:
1. Add antlr4-complete.jar to CLASSPATH, either: Permanently:
Using System Properties dialog > Environment variables > Create or append to CLASSPATH variable Temporarily, at command line: SET CLASSPATH=.;C:\Javalib\antlr4-complete.jar;%CLASSPATH%
3.Create batch commands for ANTLR Tool, TestRig in dir in PATH
antlr4.bat: java org.antlr.v4.Tool %*
grun.bat: java org.antlr.v4.gui.TestRig %*
After setup you can build an application using your .g4 grammar file :
Java -jar antlr-4.5.3-complete.jar yourGrammar.g4
You can also build an application in other languages with the -Dlanguage parameter. For example to generate C# files you would do something like this:
java -jar antlr-4.5.3-complete.jar yourGrammar.g4 -Dlanguage=CSharp
See here for full list of pre-made grammar's for common programming languages.
Download the latest version of ANTLR and extract it to a folder.
You can use also Maven, Gradle, or other build tool to depend on its runtime (the classes the generated grammars use): org.antlr:antlr4-runtime
.
In order to automatically -as part of the build process- generate the parser in a maven project, use the Maven plugin: org.antlr:antlr4
.
(Tested with ANTLR 4.5.3, Eclipse Neon, ANTLR 4 IDE 0.3.5, and Java 1.8)
Install the ANTLR IDE in Eclipse.
Work around for the “Failed to create injector…” error.
Tell Eclipse/Java where ANTLR is.
(Optional) Configure the ANTLR IDE generated sources directory.
Create an ANTLR 4 project.