ANTLR Cheat Sheet

Example

grammar T;
def : modifier+ 'int' ID '=' INT ';'
    | modifier+ 'int' ID ';'
    ;
modifier : 'public' | 'static' ;
INT : '0'..'9'+ ;
ID  : 'a'..'z'+ ;
WS  : (' '|'\r'|'\n')+ {$channel = HIDDEN;} ;

Matches input such as public static int i = 3;.

Here's a main program to invoke the parser on the file argument from command-line.

import java.io.*;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
public class Test {
    public static void main(String args[]) throws Exception {
        TLexer lex = new TLexer(new ANTLRFileStream(args[0]));
        CommonTokenStream tokens = new CommonTokenStream(lex);

        TParser parser = new TParser(tokens);
        TParser.startRule_return r = parser.startRule(); // launch parsing
        // print tree if building trees
        if ( r!=null ) System.out.println(((CommonTree)r.tree).toStringTree());
    }
}

Here is a test rig for building an AST with a parser and walking it with a tree parser:

import java.io.*;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
public class Test {
    public static void main(String args[]) throws Exception {
        TLexer lex = new TLexer(new ANTLRFileStream(args[0]));
        CommonTokenStream tokens = new CommonTokenStream(lex);

        TParser parser = new TParser(tokens); // created from T.g
        TParser.startRule_return r = parser.startRule(); // launch parsing
        if ( r!=null ) System.out.println(((CommonTree)r.tree).toStringTree());

        CommonTreeNodeStream nodes = new CommonTreeNodeStream((Tree)r.tree);
        nodes.setTokenStream(tokens);
        TP walker = new TP(nodes); // created from TP.g
        TP.startRule_return r2 = walker.startRule();
        CommonTree rt = ((CommonTree)r2.tree);
        // if tree parser constructs trees
        if ( rt!=null ) System.out.println(((CommonTree)r2.tree).toStringTree());               
    }
}

ANTLR Symbols

See also Grammars and Special symbols in actions.

Symbol Description
$ Attribute
@ Action
:: action or dynamically-scoped attribute scope specifier
: rule definition
; end rule
| alternative
's' char or string literal
. wildcard
= label assignment
+= list label assignment
[..] argument or return value spec
{...} action
... forced action; execute even while backtracking
(...) subrule
+ 1 or more
* 0 or more
? optional or semantic predicate
~ match not
! don't include in AST
^ make AST root node
=> always execute predicate
-> rewrite rule
<token options> token option spec like ID<node=VarNode>
^(...) tree grammar or rewrite element
// ... single-line comment
/* ... */ multi-line comment
Keyword Description
scope Dynamically-scoped attribute
fragment lexer rule is a helper rule, not real token for parser
lexer grammar type
tree grammar type
parser grammar type
grammar grammar header
returns rule return value(s)
throws rule throws exception(s)
catch catch rule exceptions
finally do this no matter what
options grammar or rule options
tokens can add tokens with this; usually imaginary tokens
import import grammar(s)
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.