The fragment is somewhat similar to the built-in function: it makes the grammar more understandable and easy to maintain.
A fragment will never be counted as a token; it serves only to simplify the grammar.
Consider:
NUMBER: DIGITS | OCTAL_DIGITS | HEX_DIGITS; fragment DIGITS: '1'..'9' '0'..'9'*; fragment OCTAL_DIGITS: '0' '0'..'7'+; fragment HEX_DIGITS: '0x' ('0'..'9' | 'a'..'f' | 'A'..'F')+;
In this example, a NUMBER match will always return NUMBER to the lexer, regardless of whether it matches "1234", "0xab12", or "0777".
See point 3
sirbrialliance Jun 27 2018-11-11T00: 00Z
source share