ANTLR Lexer rules in v4 Simple rules


Example

Lexer rules define token types. Their name has to start with an uppercase letter to distinguish them from parser rules.

INTEGER: [0-9]+;
IDENTIFIER: [a-zA-Z_] [a-zA-Z_0-9]*;

OPEN_PAREN: '(';
CLOSE_PAREN: ')';

Basic syntax:

SyntaxMeaning
AMatch lexer rule or fragment named A
A BMatch A followed by B
(A|B)Match either A or B
'text'Match literal "text"
A?Match A zero or one time
A*Match A zero or more times
A+Match A one or more times
[A-Z0-9]Match one character in the defined ranges (in this example between A-Z or 0-9)
'a'..'z'Alternative syntax for a character range
~[A-Z]Negation of a range - match any single character not in the range
.Match any single character