Hi @parrt!
I’ve been working my way through the antlr4 reference book and having a great time at it. I’ve done all my exercises with the antlr-tools
python module, so some of the examples in the book had to be adapted a bit. So far I’ve been able to get them all working correctly though. Until I reached Chapter 12 Section 1 “Broadcasting Tokens on Different Channels”.
We’d want to give the channels some form of reference that is inteligible to a human. In java the method is to declare static integer members to the lexer class and that would then be used whenever we want to reference the comments/whitespace channel. When I try to replicate that in python I initially tried to do the same. However, I expected that the routing would have to become
@lexer::members {
COMMENT = 1
WHITESPACE = 2
}
WS : [ \t\r\n]+ -> channel(CymbolLexer.WHITESPACE) ;
However, the translation tool did not have any of that:
PS C:\repos\antlr\Learning\lexmagic\Cymbol> ..\..\..\.venv\Scripts\antlr4.exe -Dlanguage=Python3 .\Cymbol.g4
error(50): Cymbol.g4:58:42: syntax error: mismatched input '.' expecting RPAREN while matching a lexer rule
error(50): Cymbol.g4:58:50: syntax error: mismatched input ')' expecting COLON while matching a lexer rule
error(50): Cymbol.g4:62:42: syntax error: mismatched input '.' expecting RPAREN while matching a lexer rule
error(50): Cymbol.g4:62:50: syntax error: mismatched input ')' expecting COLON while matching a lexer rule
error(50): Cymbol.g4:69:39: syntax error: mismatched input '.' expecting RPAREN while matching a lexer rule
error(50): Cymbol.g4:69:50: syntax error: mismatched input ')' expecting COLON while matching a lexer rule
It seemed to not like the ‘.’ character in the channel(...)
construct. I tried the same with globals that wouldn’t need a dot character. But then it complained about the “undefined” references - during the translation of the grammar, i.e. I don’t get a lexer.
PS C:\repos\antlr\Learning\lexmagic\Cymbol> ..\..\..\.venv\Scripts\antlr4.exe -Dlanguage=Python3 .\Cymbol.g4
error(177): Cymbol.g4:58:31: COMMENT is not a recognized channel name
error(177): Cymbol.g4:62:31: COMMENT is not a recognized channel name
error(177): Cymbol.g4:69:28: WHITESPACE is not a recognized channel name
I then tried the code from the books repository in case the python tools don’t have this implemented but I see the same error, and hence don’t get a lexer implementation.
PS C:\repos\antlr\Book\lexmagic> antlr4 .\Cymbol.g4
error(177): Cymbol.g4:59:30: WHITESPACE is not a recognized channel name
error(177): Cymbol.g4:62:33: COMMENTS is not a recognized channel name
In the appendix for the channel command I couldn’t find anything particularly useful on this. It seems to be expected to work…
Are there examples of working versions of the named channels? I can obviously make it work by using literal integers in the channel assignment and then also define lexer members that match these numbers but then the named channel is sort of broken since I might have fat-fingered the wrong number into the channel assignment…
Thank you,
Manuel