2013-12-23 12:44:16 +08:00
# ANTLR v4
2012-02-01 11:28:15 +08:00
2013-12-23 12:47:53 +08:00
**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.
2012-02-01 11:28:15 +08:00
2013-12-23 12:46:35 +08:00
* [Wikipedia ](https://en.wikipedia.org/wiki/ANTLR )
2013-12-31 03:47:47 +08:00
* [Official site ](http://www.antlr.org/ )
2013-12-23 12:46:35 +08:00
* [ANTLR v3 ](http://www.antlr3.org/ )
2014-02-19 04:23:42 +08:00
* [v3 to v4 Migration guide, differences ](https://theantlrguy.atlassian.net/wiki/pages/viewpage.action?pageId=1900596 )
2013-12-23 12:44:16 +08:00
## INTRODUCTION
2012-02-01 11:28:15 +08:00
2014-02-04 09:50:02 +08:00
Hi and welcome to the Honey Badger 4.2 release (February 3, 2014) of ANTLR!
2012-09-19 02:25:06 +08:00
2013-12-23 12:44:16 +08:00
## INSTALLATION
2012-09-19 02:25:06 +08:00
2013-12-23 12:44:16 +08:00
### UNIX
2012-09-19 02:25:06 +08:00
2013-12-23 12:44:16 +08:00
0. [Install Java ](http://www.oracle.com/technetwork/java/javase/downloads/index.html ) (version 1.6 or higher)
2012-09-19 02:25:06 +08:00
2013-01-02 05:43:15 +08:00
1. Download
2013-12-23 12:44:16 +08:00
```sh
2013-01-02 05:43:15 +08:00
$ cd /usr/local/lib
2014-02-04 09:50:02 +08:00
$ curl -O http://www.antlr4.org/download/antlr-4.2-complete.jar
2013-12-23 12:44:16 +08:00
```
2014-03-10 15:00:54 +08:00
Or just [download in browser ](http://www.antlr.org/download/antlr-4.2-complete.jar ) and put it somewhere rational like `/usr/local/lib` .
2012-09-19 02:25:06 +08:00
2014-02-04 09:50:02 +08:00
2. Add `antlr-4.2-complete.jar` to your `CLASSPATH` :
2013-12-23 12:44:16 +08:00
```sh
2014-02-04 09:50:02 +08:00
$ export CLASSPATH=".:/usr/local/lib/antlr-4.2-complete.jar:$CLASSPATH"
2013-12-23 12:44:16 +08:00
```
2013-01-02 05:43:15 +08:00
Is also a good idea to put this in your .bash_profile or whatever your
startup script is.
3. Create aliases for the ANTLR Tool, and TestRig.
2013-12-23 12:44:16 +08:00
```sh
2014-02-04 09:50:02 +08:00
$ alias antlr4='java -jar /usr/local/lib/antlr-4.2-complete.jar'
2013-01-26 03:20:29 +08:00
$ alias grun='java org.antlr.v4.runtime.misc.TestRig'
2013-12-23 12:44:16 +08:00
```
2013-01-02 05:43:15 +08:00
2013-12-23 12:44:16 +08:00
### Windows (Thanks to Graham Wideman)
2013-01-02 05:43:15 +08:00
2013-12-23 12:44:16 +08:00
0. [Install Java ](http://www.oracle.com/technetwork/java/javase/downloads/index.html ) (version 1.6 or higher)
2013-01-02 05:43:15 +08:00
2014-02-19 06:32:00 +08:00
1. Download [antlr-4.2-complete.jar ](http://www.antlr.org/download/antlr-4.2-complete.jar )
2013-12-23 12:44:16 +08:00
Save to your directory for 3rd party Java libraries, say `C:\Javalib`
2013-01-02 05:43:15 +08:00
2014-02-04 09:50:02 +08:00
2. Add `antlr-4.2-complete.jar` to `CLASSPATH` , either:
2013-01-02 05:43:15 +08:00
* Permanently: Using System Properties dialog > Environment variables >
2013-12-23 12:44:16 +08:00
Create or append to `CLASSPATH` variable
2013-01-02 05:43:15 +08:00
* Temporarily, at command line:
2014-02-04 09:50:02 +08:00
`SET CLASSPATH=C:\Javalib\antlr-4.2-complete.jar;%CLASSPATH%`
2013-01-02 05:43:15 +08:00
3. Create short convenient commands for the ANTLR Tool, and TestRig,
using batch files or doskey commands:
2013-12-23 12:44:16 +08:00
* Batch files (in directory in system `PATH` )
```
2013-01-02 05:43:15 +08:00
antlr4.bat: java org.antlr.v4.Tool %*
run.bat: java org.antlr.v4.runtime.misc.TestRig %*
2013-12-23 12:44:16 +08:00
```
2013-01-02 05:43:15 +08:00
* Or, use doskey commands:
2013-12-23 12:44:16 +08:00
```bat
2013-01-02 05:43:15 +08:00
doskey antlr4=java org.antlr.v4.Tool $*
doskey grun =java org.antlr.v4.runtime.misc.TestRig $*
2013-12-23 12:44:16 +08:00
```
2013-01-02 05:43:15 +08:00
2013-12-23 12:44:16 +08:00
## Testing installation
2012-09-19 02:25:06 +08:00
2013-12-23 12:44:16 +08:00
Either launch `org.antlr.v4.Tool` directly:
```sh
2012-09-19 02:25:06 +08:00
$ java org.antlr.v4.Tool
2014-02-04 09:50:02 +08:00
ANTLR Parser Generator Version 4.2
2012-09-19 02:25:06 +08:00
-o ___ specify output directory where all output is generated
-lib ___ specify location of .tokens files
...
2013-12-23 12:44:16 +08:00
```
or use `-jar` option on java:
```sh
2014-02-04 09:50:02 +08:00
$ java -jar /usr/local/lib/antlr-4.2-complete.jar
ANTLR Parser Generator Version 4.2
2012-09-19 02:25:06 +08:00
-o ___ specify output directory where all output is generated
-lib ___ specify location of .tokens files
...
2013-12-23 12:44:16 +08:00
```
2012-09-19 02:25:06 +08:00
2013-12-23 12:44:16 +08:00
## Example
2012-09-19 02:25:06 +08:00
2013-12-23 12:44:16 +08:00
In a temporary directory, put the following grammar inside file `Hello.g4` :
```g
2012-09-19 02:25:06 +08:00
// Define a grammar called Hello
// match keyword hello followed by an identifier
// match lower-case identifiers
grammar Hello;
r : 'hello' ID ;
ID : [a-z]+ ;
2014-02-19 09:33:20 +08:00
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
2013-12-23 12:44:16 +08:00
```
2012-09-19 02:25:06 +08:00
Then run ANTLR the tool on it:
2013-12-23 12:44:16 +08:00
```sh
2012-09-19 02:25:06 +08:00
$ cd /tmp
$ antlr4 Hello.g4
$ javac Hello*.java
2013-12-23 12:44:16 +08:00
```
2012-09-19 02:25:06 +08:00
Now test it:
2013-12-23 12:44:16 +08:00
```sh
2012-09-19 02:25:06 +08:00
$ grun Hello r -tree
hello parrt
^D
(r hello parrt)
2013-12-23 12:44:16 +08:00
```
(That `^D` means `EOF` on unix; it's `^Z` in Windows.) The `-tree` option prints the parse tree in LISP notation.
## The Definitive ANTLR 4 Reference
2014-01-07 20:24:38 +08:00
You can buy a book [The Definitive ANTLR 4 Reference ](http://pragprog.com/book/tpantlr2/the-definitive-antlr-4-reference )
2013-12-23 12:47:53 +08:00
2013-12-23 12:44:16 +08:00
Programmers run into parsing problems all the time. Whether it’ s a data format like JSON, a network protocol like SMTP, a server configuration file for Apache, a PostScript/PDF file, or a simple spreadsheet macro language—ANTLR v4 and this book will demystify the process. ANTLR v4 has been rewritten from scratch to make it easier than ever to build parsers and the language applications built on top. This completely rewritten new edition of the bestselling Definitive ANTLR Reference shows you how to take advantage of these new features.
[Book source code ](http://pragprog.com/titles/tpantlr2/source_code )
2012-09-19 02:25:06 +08:00
2014-01-07 20:24:38 +08:00
## Additional grammars
2013-12-23 12:44:16 +08:00
[This repository ](https://github.com/antlr/grammars-v4 ) is a collection of grammars without actions where the
root directory name is the all-lowercase name of the language parsed
by the grammar. For example, java, cpp, csharp, c, etc...
2012-09-19 02:25:06 +08:00
2013-12-31 03:47:47 +08:00
## Authors
2012-09-19 02:25:06 +08:00
2013-12-23 12:46:35 +08:00
[Terence Parr ](http://www.cs.usfca.edu/~parrt/ ), parrt@cs.usfca.edu
2013-12-23 12:44:16 +08:00
ANTLR project lead and supreme dictator for life< br / >
[University of San Francisco ](http://www.usfca.edu/ )
2013-12-31 03:47:47 +08:00
[Sam Harwell ](http://tunnelvisionlabs.com/ )