2013-01-21 08:51:11 +08:00
|
|
|
ANTLR v4
|
2012-02-01 11:28:15 +08:00
|
|
|
|
2013-06-30 03:15:27 +08:00
|
|
|
Terence Parr, parrt@cs.usfca.edu
|
2012-02-01 11:28:15 +08:00
|
|
|
ANTLR project lead and supreme dictator for life
|
|
|
|
University of San Francisco
|
|
|
|
|
|
|
|
INTRODUCTION
|
|
|
|
|
2013-06-30 03:15:27 +08:00
|
|
|
Hi and welcome to the Honey Badger 4.1 release of ANTLR!
|
2012-09-19 02:25:06 +08:00
|
|
|
|
|
|
|
INSTALLATION
|
|
|
|
|
2013-01-02 05:43:15 +08:00
|
|
|
UNIX
|
2012-09-19 02:25:06 +08:00
|
|
|
|
2013-01-02 05:43:15 +08:00
|
|
|
0. Install Java (version 1.6 or higher)
|
2012-09-19 02:25:06 +08:00
|
|
|
|
2013-01-02 05:43:15 +08:00
|
|
|
1. Download
|
2012-09-19 02:25:06 +08:00
|
|
|
|
2013-01-02 05:43:15 +08:00
|
|
|
$ cd /usr/local/lib
|
2013-06-30 03:15:27 +08:00
|
|
|
$ curl -O http://www.antlr4.org/download/antlr-4.1-complete.jar
|
2012-09-19 02:25:06 +08:00
|
|
|
|
2013-01-02 05:43:15 +08:00
|
|
|
Or just download in browser using URL:
|
|
|
|
|
2013-06-30 03:15:27 +08:00
|
|
|
http://www.antlr4.org/download/antlr-4.1-complete.jar
|
2013-01-02 05:43:15 +08:00
|
|
|
|
|
|
|
and put it somewhere rational like /usr/local/lib.
|
|
|
|
|
2013-06-30 03:15:27 +08:00
|
|
|
2. Add antlr-4.1-complete.jar to your CLASSPATH:
|
2013-01-02 05:43:15 +08:00
|
|
|
|
2013-06-30 03:15:27 +08:00
|
|
|
$ export CLASSPATH=".:/usr/local/lib/antlr-4.1-complete.jar:$CLASSPATH"
|
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-06-30 03:15:27 +08:00
|
|
|
$ alias antlr4='java -jar /usr/local/lib/antlr-4.1-complete.jar'
|
2013-01-26 03:20:29 +08:00
|
|
|
$ alias grun='java org.antlr.v4.runtime.misc.TestRig'
|
2013-01-02 05:43:15 +08:00
|
|
|
|
|
|
|
WINDOWS (Thanks to Graham Wideman)
|
|
|
|
|
|
|
|
0. Install Java (version 1.6 or higher)
|
|
|
|
|
2013-06-30 03:15:27 +08:00
|
|
|
1. Download http://antlr.org/download/antlr-4.1-complete.jar
|
2013-01-02 05:43:15 +08:00
|
|
|
Save to your directory for 3rd party Java libraries, say C:\Javalib
|
|
|
|
|
2013-06-30 03:15:27 +08:00
|
|
|
2. Add antlr-4.1-complete.jar to CLASSPATH, either:
|
2013-01-02 05:43:15 +08:00
|
|
|
|
|
|
|
* Permanently: Using System Properties dialog > Environment variables >
|
|
|
|
Create or append to CLASSPATH variable
|
|
|
|
|
|
|
|
* Temporarily, at command line:
|
2013-06-30 03:15:27 +08:00
|
|
|
SET CLASSPATH=C:\Javalib\antlr-4.1-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:
|
|
|
|
|
|
|
|
* Batch files (in directory in system PATH)
|
|
|
|
|
|
|
|
antlr4.bat: java org.antlr.v4.Tool %*
|
|
|
|
run.bat: java org.antlr.v4.runtime.misc.TestRig %*
|
|
|
|
|
|
|
|
* Or, use doskey commands:
|
|
|
|
|
|
|
|
doskey antlr4=java org.antlr.v4.Tool $*
|
|
|
|
doskey grun =java org.antlr.v4.runtime.misc.TestRig $*
|
|
|
|
|
|
|
|
TESTING INSTALLATION
|
|
|
|
|
|
|
|
Either launch org.antlr.v4.Tool directly:
|
2012-09-19 02:25:06 +08:00
|
|
|
|
|
|
|
$ java org.antlr.v4.Tool
|
2013-06-30 03:15:27 +08:00
|
|
|
ANTLR Parser Generator Version 4.1
|
2012-09-19 02:25:06 +08:00
|
|
|
-o ___ specify output directory where all output is generated
|
|
|
|
-lib ___ specify location of .tokens files
|
|
|
|
...
|
|
|
|
|
|
|
|
or use -jar option on java:
|
|
|
|
|
2013-06-30 03:15:27 +08:00
|
|
|
$ java -jar /usr/local/lib/antlr-4.1-complete.jar
|
|
|
|
ANTLR Parser Generator Version 4.1
|
2012-09-19 02:25:06 +08:00
|
|
|
-o ___ specify output directory where all output is generated
|
|
|
|
-lib ___ specify location of .tokens files
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
EXAMPLE
|
|
|
|
|
2013-01-02 05:43:15 +08:00
|
|
|
In a temporary directory, put the following grammar inside file Hello.g4:
|
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]+ ;
|
|
|
|
WS : [ \t\n]+ -> skip ; // skip spaces, tabs, newlines
|
|
|
|
|
|
|
|
Then run ANTLR the tool on it:
|
|
|
|
|
|
|
|
$ cd /tmp
|
|
|
|
$ antlr4 Hello.g4
|
|
|
|
$ javac Hello*.java
|
|
|
|
|
|
|
|
Now test it:
|
|
|
|
|
|
|
|
$ grun Hello r -tree
|
|
|
|
hello parrt
|
|
|
|
^D
|
|
|
|
(r hello parrt)
|
|
|
|
|
|
|
|
(That ^D means EOF on unix; it's ^Z in Windows.) The -tree option prints
|
|
|
|
the parse tree in LISP notation.
|
|
|
|
|
|
|
|
BOOK SOURCE CODE
|
|
|
|
|
2012-09-30 03:53:44 +08:00
|
|
|
http://pragprog.com/titles/tpantlr2/source_code
|
2013-06-30 03:15:27 +08:00
|
|
|
|
|
|
|
GRAMMARS
|
|
|
|
|
|
|
|
https://github.com/antlr/grammars-v4
|