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.

Popular Pragmatic topics Top

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
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
belgoros
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84: bundle exec sequel...
New
adamwoolhether
I’m not quite sure what’s going on here, but I’m unable to have to containers successfully complete the Readiness/Liveness checks. I’m im...
New
New
taguniversalmachine
Hi, I am getting an error I cannot figure out on my test. I have what I think is the exact code from the book, other than I changed “us...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
dtonhofer
@parrt In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4....
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New

Other popular topics Top

PragmaticBookshelf
A PragProg Hero’s Journey with Brian P. Hogan @bphogan Have you ever worried that your only legacy will be in the form of legacy...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
foxtrottwist
Here’s our thread for the Keyboardio Atreus. It is a mechanical keyboard based on and a slight update of the original Atreus (Keyboardio ...
New
PragmaticBookshelf
“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin Even when you’re ultimately right about what the future ho...
New
PragmaticBookshelf
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
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
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1127 25299 748
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
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
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New

Latest in PragProg

View all threads ❯