ANTLR


In computer-based language recognition, ANTLR, or ANother Tool for Language Recognition, is a parser generator that uses LL for parsing. ANTLR is the successor to the Purdue Compiler Construction Tool Set, first developed in 1989, and is under active development. Its maintainer is Professor Terence Parr of the University of San Francisco.

Usage

ANTLR takes as input a grammar that specifies a language and generates as output source code for a recognizer of that language. While Version 3 supported generating code in the programming languages Ada95, ActionScript, C, C#, Java, JavaScript, Objective-C, Perl, Python, Ruby, and Standard ML, the current release at present only targets Java, C#, C++, JavaScript, Python, Swift, and Go. A language is specified using a context-free grammar expressed using Extended Backus–Naur Form.
ANTLR can generate lexers, parsers, tree parsers, and combined lexer-parsers. Parsers can automatically generate parse trees or abstract syntax trees, which can be further processed with tree parsers. ANTLR provides a single consistent notation for specifying lexers, parsers, and tree parsers.
By default, ANTLR reads a grammar and generates a recognizer for the language defined by the grammar. If there are no syntax errors, the default action is to simply exit without printing any message. In order to do something useful with the language, actions can be attached to grammar elements in the grammar. These actions are written in the programming language in which the recognizer is being generated. When the recognizer is being generated, the actions are embedded in the source code of the recognizer at the appropriate points. Actions can be used to build and check symbol tables and to emit instructions in a target language, in the case of a compiler.
Other than lexers and parsers, ANTLR can be used to generate tree parsers. These are recognizers that process abstract syntax trees, which can be automatically generated by parsers. These tree parsers are unique to ANTLR and help processing abstract syntax trees.

Licensing

and ANTLR 4 are free software, published under a three-clause BSD License. Prior versions were released as public domain software. Documentation, derived from Parr's book The Definitive ANTLR 4 Reference, is included with the BSD-licensed ANTLR 4 source.
Various plugins have been developed for the Eclipse development environment to support the ANTLR grammar, including ANTLR Studio, a proprietary product, as well as the "ANTLR 2" and "ANTLR 3" plugins for Eclipse hosted on SourceForge.

ANTLR 4

ANTLR 4 deals with left recursion correctly and supports actions and attributes flexibly. That is, actions can be defined separately from the grammar, allowing for easier targeting of multiple languages.

Development

As reported on the tools page of the ANTLR project, plug-ins that enable features like syntax highlighting, syntax error checking and code completion are freely available for the most common IDEs.

Projects

Here is a non-comprehensive list of software built using ANTLR:
Over 200 grammars implemented in ANTLR 4 are available on Github. They range from grammars for a URL to grammars for entire languages like C, Java and Go.

Example

In the following example, a parser in ANTLR describes the sum of expressions can be seen in the form of "1 + 2 + 3":

// Common options, for example, the target language
options
// Followed by the parser
class SumParser extends Parser;
options
// Definition of an expression
statement: INTEGER *;
// Here is the Lexer
class SumLexer extends Lexer;
options
PLUS: '+';
DIGIT: ;
INTEGER: +;

The following listing demonstrates the call of the parser in a program:

TextReader reader;
// Fill TextReader with character
SumLexer lexer = new SumLexer;
SumParser parser = new SumParser;
parser.statement;