Antlr - Lexer Grammar

Card Puncher Data Processing


The grammar file of the lexer is composed of lexer rule, optionally broken into multiple lexical modes

The most common approach is to combine combined in in one grammar file, the lexer and parser grammar.


Only lexer grammars can contain:

  • mode specifications.
  • custom channels specifications


lexer grammar Name;

In the parser grammar, you define the lexer with the tokenVocab option.

parser grammar MarkupParser;

options { tokenVocab=MarkupLexer; }

Discover More
Card Puncher Data Processing
Antlr - (Grammar|Lexicon) (g4)

Grammar in the context of Antlr. The grammar definition of Antlr is called a antlr/antlr4/blob/master/doc/lexicon.mdLexicon because the grammar is used by the lexer (hence the lexer grammar) See: ...
Card Puncher Data Processing
Antlr - Channel

When you don't want to check for non-semantic rule such as comments / whitespace, you can throw them away with -> skip but if you want to preserve them, you use channels. Only lexer grammars can...
Card Puncher Data Processing
Antlr - Lexer Rule (Token names|Lexical Rule)

in Antlr. They are rules that defines tokens. They are written generally in the grammar but may be written in a lexer grammar file Each lexer rule is either matched or not so every lexer rule expression...
Card Puncher Data Processing
Antlr - Lexical mode

Lexical mode in Antlr Lexical modes allow to split a single lexer grammar file into multiple sublexers. The lexer can then only return tokens matched by rules from the current mode. Lexers start out...
Card Puncher Data Processing
Antlr - Parser Grammar

The grammar file of the parser contains the parser rule. grammar file If you have two differents file and your lexer grammar is defined in another file, you need to define it with the tokenVocab...

Share this page:
Follow us:
Task Runner