I'm reading the ANTLR4 defenitive guide and now I'm at the section about lexer rule resolution. Here is what's written in this section:
grammar KeywordTest;
enumDef : 'enum' '{' ... '}';
...
FOR: 'for'
...
ID:[a-zA-Z]; // does not match 'enum' or 'for'
Rule ID could also match kewords such as
enum
orfor
, which means there's more than one rule that could match the same string. [...] Literals such as'enum'
become lexical rules and go immediately after the parser rules but before the explicit lexical rules.
What does it mean and how does it help us to resolve the potential ambiguities? I would say that a declaration like
ENUM_KEYWORD: 'enum'
which ATNLR4
might use internally would be decalred right after the rule enumDef: 'enum' '{' ... '}
and will look as follows:
enumDef: ENUM_KEYWORD '{' ... '}
ENUM_KEYWORD: 'enum'
Is that exactly how ANTLR4 does things?