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

Popular Prag Prog topics Top

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
edruder
I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here. ...
New
herminiotorres
Hi @Margaret , On page VII the book tells us the example and snippets will be all using Elixir version 1.11 But on page 3 almost the en...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
leonW
I ran this command after installing the sample application: $ cards add do something --owner Brian And got a file not found error: Fil...
New
Charles
In general, the book isn’t yet updated for Phoenix version 1.6. On page 18 of the book, the authors indicate that an auto generated of ro...
New
rainforest
Hi, I’ve got a question about the implementation of PubSub when using a Phoenix.Socket.Transport behaviour rather than channels. Before ...
New
Henrai
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New

Latest in PragProg

View all threads ❯