worc4021

worc4021

ANTLR4 - Are channel names still supported?

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… :confused:

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

First Post!

parrt

parrt

Author of The Definitive ANTLR 4 Reference

I’m not sure exactly how the python target works. You will have to look at the generator code I would imagine. Take a look and see what does Lexer members come out as in python and then see what the references become in the generator code as well.

Where Next?

Popular Pragmatic Bookshelf topics Top

rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
New
gilesdotcodes
In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions: In Gemfile, change gem 'rails' t...
New
adamwoolhether
When trying to generate the protobuf .go file, I receive this error: Unknown flag: --go_opt libprotoc 3.12.3 MacOS 11.3.1 Googling ...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
fynn
This is as much a suggestion as a question, as a note for others. Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
creminology
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New
mert
AWDWR 7, page 152, page 153: Hello everyone, I’m a little bit lost on the hotwire part. I didn’t fully understand it. On page 152 @rub...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File > New Rule: And select Deny, O...
New

Sub Categories: