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

ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
jesse050717
Title: Web Development with Clojure, Third Edition, pg 116 Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
Mmm
Hi, build fails on: bracket-lib = “~0.8.1” when running on Mac Mini M1 Rust version 1.5.0: Compiling winit v0.22.2 error[E0308]: mi...
New
cro
I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New
swlaschin
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
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
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
New
roadbike
From page 13: On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
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
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
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
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New

Sub Categories: