Grammar Lexicon

The lexicon of ANTLR is familiar to most programmers because it follows the syntax of C and its derivatives with some extensions for grammatical descriptions.

Comments

There are single-line, multiline, and Javadoc-style comments.

​ ​/** This grammar is an example illustrating the three kinds​
​ ​ * of comments.​
​ ​ */​
​ ​grammar​ T​;​
​ 
​ ​/* a multi-line​
​ ​ comment​
​ ​ */​
​ 
​ ​/** This rule matches a declarator for my language */​
​ decl ​:​ ID ​;​ ​// match a variable name​

The Javadoc comments are sent to the parser and are not ignored. These are allowed only at the start of the grammar and any rule.

Identifiers

Token names always start with ...

Get The Definitive ANTLR 4 Reference, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.