What are Antlr4 channels used for? #4220
-
The documentation doesn't really say. It shows channels marked with |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
May I suggest that you read the book ? |
Beta Was this translation helpful? Give feedback.
-
During lexical analysis, input is broken into tokens and placed on the token stream, which is just a list of tokens. A channel is an attribute of a token that can be used to hide the token from parsing. Without a channel, the parser grammar would need to be changed to explicitly handle the token wherever it could occur. E.g. adjusting the grammar for everywhere whitespace could occur would make a grammar extremely messy. When you have a lexer rule like |
Beta Was this translation helpful? Give feedback.
During lexical analysis, input is broken into tokens and placed on the token stream, which is just a list of tokens. A channel is an attribute of a token that can be used to hide the token from parsing. Without a channel, the parser grammar would need to be changed to explicitly handle the token wherever it could occur. E.g. adjusting the grammar for everywhere whitespace could occur would make a grammar extremely messy. When you have a lexer rule like
WS: [ \n\r]+ -> channel(HIDDEN);
the token channel is "hidden", which is implemented as just an integer. A ruleWS: [ \n\r]+ -> skip;
means to not even create a token for the string, and not add a token for that string on the token stream.