1 of 5

Introduction to Antlr

Andreas Stefik

2 of 5

Antlr is a Language for Creating Languages

  • It was invented by Terence Parr
  • The different versions are dramatically different and we will be using 4 for this class
  • Antlr is used heavily in industry for all sorts of file processing. I've seen it in:
    • Industry strength development environments
    • Video games (e.g., Mass Effect)
    • In the processing of other languages
  • In many ways, Antlr is the state-of-the-art in languages for language design and is a significant improvement over prior techniques (e.g., Lexx/Yaxx, JCup, JavaC)

3 of 5

Antlr uses "Finite Automata" and "Context Free Grammars" Behind the Scenes

  • These are mathematical techniques commonly used for text processing, which is what you need when designing programming languages
  • However, Antlr is weird and does not match exactly to a typical textbook explanation for how these concepts work. The reasons include
    • Terence has invented a way to make the grammars easier to write, especially for things like expressions
    • While automata are powerful, some common operations in a programming language are a bit of a pain if you don't "cheat" a little (e.g., white space, comments)
    • Antlr does not use the "visitor" concept, often seen in language processors. Instead it uses listeners.

4 of 5

Let's Dissect an Antlr Grammar

grammar Interpreter;

start :

expression EOF

;

expression

:

| INT

| expression (PLUS | MINUS) expression

;

PLUS : '+';

MINUS : '-';

INT : '0'..'9'+;

Name

Parser Rules

Lexer Rules

5 of 5

Live Programming Session

Using and Interacting with Grammars