Parser.getTokenStream() equivalent in C#

30 views
Skip to first unread message

Steve Nicholson

unread,
Jun 19, 2019, 3:00:59 PM6/19/19
to antlr-discussion
I'm working through the The Definitive ANTLR 4 Reference, 2e in C#. In the ExtractInterfaceListener class in chapter 4's "Building a Translator with a Listener" section, the line
 
TokenStream tokens = parser.getTokenStream();

doesn't compile (even if I change it to GetTokenStream() to fit C# convention). Also, according to Intellisense, there is no TokenStream type at all, only an ITokenStream interface.

I looked through the JavaParser and Parser classes and neither has a method that returns a TokenStream. In the decompiled source to Parser I saw a couple references to "(ITokenStream) this.InputStream" so I wound up with this:

ITokenStream tokens = (ITokenStream)_parser.InputStream;

The program now runs correctly (after I made a couple changes to Java.g4 to get it to work with C#).

My questions are:
  1. Is this an acceptable substitute for parser.getTokenStream()?
  2. Is it a bug that there isn't a GetTokenStream() method in the C# version of Parser?

easj...@ix.netcom.com

unread,
Jun 19, 2019, 3:51:22 PM6/19/19
to antlr-di...@googlegroups.com

Have you worked thru Sections 3.2 and 3.3 which show a Java main that hooks up a custom lexer, custom parser, etc. and passes the token stream to a custom listener?

 

HTH

Steve Nicholson

unread,
Jun 19, 2019, 4:00:13 PM6/19/19
to antlr-discussion
Yes, I worked through those with no problems. It was just the call to parser.getTokenStream() that hung me up in this section because there was no obvious C# counterpart.

easj...@ix.netcom.com

unread,
Jun 19, 2019, 4:26:48 PM6/19/19
to antlr-di...@googlegroups.com

Sorry that didn’t help.  I’ve only worked with Java and ANTLR and don’t know C++. My generated Java parser extends Parser from the ANTLR Java library which has the method you are missing.

 

This tutorial for ANTLR and C++ shows some *include statements but I have no idea what they do.

 

https://tomassetti.me/getting-started-antlr-cpp/

 

Best wishes.

--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/antlr-discussion/5988b8db-0e77-4642-8507-6975f8f1f560%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages