What is ANTLR?
ANTLR (ANother Tool for Language Recognition) is a
powerful parser generator for reading, processing, executing, or
translating structured text or binary files. It's widely used to build
languages, tools, and frameworks. From a grammar, ANTLR generates a
parser that can build and walk parse trees.
Looking for ANTLR v3?
Terence Parr is the maniac behind ANTLR and has been
working on language tools since 1989. He is a professor of computer
science at the
University of San Francisco.
Samples
OS X
$ cd /usr/local/lib
$ sudo curl -O http://www.antlr.org/download/antlr-4.2.2-complete.jar
$ export CLASSPATH=".:/usr/local/lib/antlr-4.2.2-complete.jar:$CLASSPATH"
$ alias antlr4='java -jar /usr/local/lib/antlr-4.2.2-complete.jar'
$ alias grun='java org.antlr.v4.runtime.misc.TestRig'
LINUX
$ cd /usr/local/lib
$ wget http://antlr.org/download/antlr-4.2.2-complete.jar
$ export CLASSPATH=".:/usr/local/lib/antlr-4.2.2-complete.jar:$CLASSPATH"
$ alias antlr4='java -jar /usr/local/lib/antlr-4.2.2-complete.jar'
$ alias grun='java org.antlr.v4.runtime.misc.TestRig'
Windows
- Download http://antlr.org/download/antlr-4.2.2-complete.jar.
- Add antlr4-complete.jar to CLASSPATH, either:
- Create batch commands for ANTLR Tool, TestRig in dir in PATH
antlr4.bat: java org.antlr.v4.Tool %*
grun.bat: java org.antlr.v4.runtime.misc.TestRig %*
grammar Expr;
prog: (expr NEWLINE)* ;
expr: expr ('*'|'/') expr
| expr ('+'|'-') expr
| INT
| '(' expr ')'
;
NEWLINE : [\r\n]+ ;
INT : [0-9]+ ;
$ antlr4 Expr.g4
$ javac Expr*.java
$ grun Expr prog -gui
100+2*34
^D
Linux
Mac