ANTLR

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 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

Quick Start
See Getting Started with ANTLR v4

OS X
$ cd /usr/local/lib
$ sudo curl -O http://www.antlr.org/download/antlr-4.4-complete.jar
$ export CLASSPATH=".:/usr/local/lib/antlr-4.4-complete.jar:$CLASSPATH"
$ alias antlr4='java -jar /usr/local/lib/antlr-4.4-complete.jar'
$ alias grun='java org.antlr.v4.runtime.misc.TestRig'
LINUX
$ cd /usr/local/lib
$ wget http://antlr.org/download/antlr-4.4-complete.jar
$ export CLASSPATH=".:/usr/local/lib/antlr-4.4-complete.jar:$CLASSPATH"
$ alias antlr4='java -jar /usr/local/lib/antlr-4.4-complete.jar'
$ alias grun='java org.antlr.v4.runtime.misc.TestRig'
Windows
  1. Download http://antlr.org/download/antlr-4.4-complete.jar.
  2. 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.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
		
sample3
		
Linux
Mac

Latest News

Testimonials

Kudos. I'm actually really liking ANTLR! I have a pretty darn good velocity with a rapid prototyping project I am doing in my Google 20% time. For example, I just discovered a feature in rewrite rules that does exactly what I need (referencing previous rule ASTs, p. 174 in your book). It took me about 5 minutes to get this to work and remove an ugly wart from my grammar. Hats off! Guido van Rossum, Inventor of Python
ANTLR is an exceptionally powerful and flexible tool for parsing formal languages. At Twitter, we use it exclusively for query parsing in Twitter search. Our grammars are clean and concise, and the generated code is efficient and stable. The book is our go-to reference for ANTLR v4 -- engaging writing, clear descriptions and practical examples all in one place. Samuel Luckenbill, Senior Manager of Search Infrastructure, Twitter, inc.
Just wanted to take the opportunity to say thanks. ANTLR is a BIG improvement over yacc/lex, and your support for it most commendable. Managed to get my tired old brain around it in a day. Nice work! Brad Cox, Inventor of Objective-C
More...