Does Antlr4 support multiple token types in type()? If not, why not? #4219
Replies: 1 comment 4 replies
-
In Antlr, there is no notion of multiple types for a token. A token type is an integer, and it is generally immutable when set. In an Antlr grammar, the name of the lexer rule represents the token type, which you can use in a parser rule, e.g., The Chevrotain documentation does not contain a single discussion of the "categories" field of a token (under lexing), and there are only a few closed issues that make reference to the feature. Chevrotain/chevrotain#962 Chevrotain/chevrotain#1055 Chevrotain/chevrotain#564 (comment). So we can only guess as to how it is supposed to work from one of the eight example grammars. In the calculator example, a category is set for Mult and for Div. Where is the field actually used?
It isn't clear what value a category brings. |
Beta Was this translation helpful? Give feedback.
-
The documentation is surprisingly thin for Antlr4, so I'm not sure if this is possible or not, but I've, in the past, used Chevrotain, which allows you to map tokens to other categories. The idea is that for the parsing phase, you can reference a "category" as a token. It looks like Antlr's equivalent of this is
-> type( x )
but it looks like you can only map to one type. Is it possible to do more such that input can be mapped to multiple token types?Beta Was this translation helpful? Give feedback.
All reactions