ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
During developing my fork YANTLR I've encountered, that ranges can be implemented not only for character, but for tokens too. It's extremely useful to use this feature when a grammar has a lot of tokens that could be identifiers, for instance, our SQL grammars.
Consider the current simplified PlSql grammar and its id rule:
With the range operator, it can be rewritten in the following way:
id
| REGULAR_ID
| ABSENT..ERROR_CODE
;
The range operator decreases the size of grammar and, more importantly, it makes grammar less error-prone because all tokens declared between ABSENT and ERROR_CODE are always considered in id and it decreases the chance that a user forget to add a newly added token to id rule.
I suppose the feature is not very complicated to implement, because the range operator is already supported for characters and it looks very natural to support ranges for tokens as well (as tokens are "characters" for parser). It's much easy than token value comparison operator (this feature could significantly reduce number of declared tokens in SQL grammars but in another way) that requires changing of runtimes.
@parrt if you give me green light, I can try to implement the feature for ANTLR 4.
During developing my fork YANTLR I've encountered, that ranges can be implemented not only for character, but for tokens too. It's extremely useful to use this feature when a grammar has a lot of tokens that could be identifiers, for instance, our SQL grammars.
Consider the current simplified PlSql grammar and its
id
rule:With the range operator, it can be rewritten in the following way:
The range operator decreases the size of grammar and, more importantly, it makes grammar less error-prone because all tokens declared between
ABSENT
andERROR_CODE
are always considered inid
and it decreases the chance that a user forget to add a newly added token toid
rule.I suppose the feature is not very complicated to implement, because the range operator is already supported for characters and it looks very natural to support ranges for tokens as well (as tokens are "characters" for parser). It's much easy than token value comparison operator (this feature could significantly reduce number of declared tokens in SQL grammars but in another way) that requires changing of runtimes.
@parrt if you give me green light, I can try to implement the feature for ANTLR 4.