Antlr (ANother Tool for Language Recognition)

About

ANTLR is lexer generator. It translates:

ANTLR is implemented in Java and generates lexer and parser in the following languages:

  • Java,
  • Ruby,
  • Python,
  • C,
  • C++,
  • C#
  • Javascript
  • and Objective C.

It grew from the old C/C++ PCCTS project. Java is then a “recent” addition in the ANTLR's lifetime.

The url string was not validated as an URL (http://www.antlr.org:license.html). Error: The url (http://www.antlr.org:license.html) is not validANTLR is licensed under BSD.

Getting started

Use cases

There are three use cases:

  • “validators.” to validate an input. Grammars for validators don't use actions or rewrite rules.
  • “processors.” to validate an input and process it. Grammars for processors use actions, but not rewrite rules.
  • “translators.” to validate and translate the input data structure into another data structure. Grammars for translators use actions (containing printlns) and/or rewrite rules.

LifeCyle / Workflow

  1. Write the grammar using one or more files with extension .g4
  2. Test your grammar with grun (note that you can also type the input at the console)
antlr4 # generate the parser
grun <grammar-name> <rule-to-test> <input-filename(s)> # CTRL+D/CTRL+Z to quit without file name
  1. Optionally write StringTemplate templates for producing output.
  2. Generate the lexer and parser classes from the grammar along with:
antlr4 <options> <grammar-file-g4>
# javascript target
antlr4 -Dlanguage=JavaScript myGrammar.g4
  1. Write an application that uses the the generated classes.
ANTLRInputStream input = new ANTLRInputStream(sqlStream);
MyLanguageLexer lexer = new MyLanguageLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
MyLanguageParser parser = new MyLanguageParser(tokens);
CollectingErrorListener errorListener = new CollectingErrorListener();
parser.removeErrorListeners();
parser.addErrorListener(errorListener);

Component

ANTLR is made up of two components:

  • the generator (Java based) called the tool that:
    • helps creating the grammar that you can test with the grun.
    • creates the lexer and parser in your target language
  • the runtime (target language based) that permits to run the lexer and parser (with visitor ,…)

Eclipse

Sample

Glossary

StringTemplate

StringTemplate is a library that supports using templates with placeholders for outputting text (ex. Java source code)

Documentation

Task Runner