ANTLR Installing for Command Line Use


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.