[antlr-interest] Accessing input stream object with ANTLR and C++?

Ric Klaren klaren at cs.utwente.nl
Tue Aug 20 05:58:22 PDT 2002


Hi,

On Mon, Aug 19, 2002 at 05:30:27PM -0000, reidr wrote:
> I recently wrote a parser using Bison/Flex, but it seems kludgey in
> ways and kind of awkward to integrate with our other C++ code, so I
> thought I'd take a look at ANTLR.  What we're trying to do is parse a
> binary input stream, looking for specific marker bytes.  When a marker
> token is found, the flex action uses yyin to do some extra parsing of
> that particular section, like so:
>
>    short lsiz = readShort(*yyin); // Calls readShort utility function.

It seems to me that you need tokenstream multiplexing. Have a look at the
examples for it and see if that's what you need. In short: you create two or
more lexers for the parts of the input stream that need different
approaches. A multiplexer object (TokenStreamSelector) is used to switch at
appropriate times (when you see the marker) to the right lexer.

If you look for multiplexing tokenstream selectors tokenstream multiplexing
etc. in the archives you will turn up quite some interesting posts.

> [Since I posted this to the newsgroup, I discovered
> getInputBuffer().getChar(), which appears to get me the next char in
> the stream.  It'd still be useful to get an associated istream,
> though, because we have existing functions to do extra parsing on
> an istream.]

You most probably don't want to use getChar() since it bypasses all antlr's
mechanics. You really have to know what you are doing if you use it. Same
by accessing the stream directly. Because antlr can do backtracking to
solve things it could have read the way beyond where you think the stream
should be.

HTH,

Ric
--
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- klaren at cs.utwente.nl ----- +31 53 4893722  ----
-----+++++*****************************************************+++++++++-------
  Wo das Chaos auf die Ordnung trifft, gewinnt meist das Chaos, weil es
  besser organisiert ist. --- Friedrich Nietzsche


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list