ANTLR Cheat Sheet

Skip to end of metadata
Go to start of metadata

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.

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

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)

Labels: