Go to file
ericvergnaud 00eb4f89ca Merge branch 'Improved-tests-granularity' into Preliminary-enhancements-for-Python-target 2014-06-07 00:01:13 +08:00
antlr4-maven-plugin [maven-release-plugin] prepare for next development iteration 2014-04-06 21:19:40 -05:00
runtime new definition of context sensitivity required change in test code. Updated profiler per Sam's comments. 2014-06-03 22:04:02 -07:00
tool Merge branch 'Improved-tests-granularity' into Preliminary-enhancements-for-Python-target 2014-06-07 00:01:13 +08:00
.gitignore Git hygiene 2014-05-21 00:19:35 +08:00
CHANGES.txt Update CHANGES.txt 2014-03-25 09:20:41 -07:00
LICENSE.txt update README, date on license. 2013-01-01 13:43:15 -08:00
README.md add link to the releases 2014-03-25 09:48:43 -07:00
build.xml [maven-release-plugin] prepare for next development iteration 2014-04-06 21:19:40 -05:00
contributors.txt Add aphyr (Kyle Kingsbury) to contributors list 2014-03-18 17:01:43 -07:00
pom.xml [maven-release-plugin] prepare for next development iteration 2014-04-06 21:19:40 -05:00

README.md

ANTLR v4

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 parse trees and also generates a listener interface (or visitor) that makes it easy to respond to the recognition of phrases of interest.

Release notes

INTRODUCTION

Hi and welcome to the Honey Badger 4.2.1 release (March 25, 2014) of ANTLR!

INSTALLATION

UNIX

  1. Install Java (version 1.6 or higher)

  2. Download

    $ cd /usr/local/lib
    $ curl -O http://www.antlr4.org/download/antlr-4.2.1-complete.jar
    

    Or just download in browser and put it somewhere rational like /usr/local/lib.

  3. Add antlr-4.2.1-complete.jar to your CLASSPATH:

    $ export CLASSPATH=".:/usr/local/lib/antlr-4.2.1-complete.jar:$CLASSPATH"
    

    Is also a good idea to put this in your .bash_profile or whatever your startup script is.

  4. Create aliases for the ANTLR Tool, and TestRig.

    $ alias antlr4='java -jar /usr/local/lib/antlr-4.2.1-complete.jar'
    $ alias grun='java org.antlr.v4.runtime.misc.TestRig'
    

Windows (Thanks to Graham Wideman)

  1. Install Java (version 1.6 or higher)

  2. Download antlr-4.2.1-complete.jar Save to your directory for 3rd party Java libraries, say C:\Javalib

  3. Add antlr-4.2.1-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\antlr-4.2.1-complete.jar;%CLASSPATH%

  1. 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:

$ java org.antlr.v4.Tool
ANTLR Parser Generator Version 4.2.1
    -o ___              specify output directory where all output is generated
    -lib ___            specify location of .tokens files
...

or use -jar option on java:

$ java -jar /usr/local/lib/antlr-4.2-complete.jar.1
ANTLR Parser Generator Version 4.2.1
    -o ___              specify output directory where all output is generated
    -lib ___            specify location of .tokens files
...

Example

In a temporary directory, put the following grammar inside file Hello.g4:

// 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\r\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.

The Definitive ANTLR 4 Reference

You can buy a book The Definitive ANTLR 4 Reference

Programmers run into parsing problems all the time. Whether its 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

Additional grammars

This repository 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...

Authors

Terence Parr, parrt@cs.usfca.edu
ANTLR project lead and supreme dictator for life University of San Francisco

Sam Harwell