Skip to content

What are Antlr4 channels used for? #4220

Answered by kaby76
matthew-dean asked this question in Q&A
Discussion options

You must be logged in to vote

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 rule WS: [ \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.

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
1 reply
@matthew-dean
Comment options

Comment options

You must be logged in to vote
4 replies
@matthew-dean
Comment options

@kaby76
Comment options

@matthew-dean
Comment options

@kaby76
Comment options

Answer selected by matthew-dean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants