[antlr-interest] reusing a parser for unit testing

Scott Amort jsamort at gmail.com
Sun Jul 9 16:55:34 PDT 2006


Hi All,

I'm writing a series of unit tests, and am wondering how to reuse my
ANTLR parser (C++ target) within these tests.  I have to setup and
teardown my parser each time I test a new input stream (currently simple
single line stringstream objects), but it would seem to be more
efficient to group related testing streams into a single test and only
setup/teardown once.  However, I can't seem to reset the parser - after
the first input stream, it gives me recognition errors, even though I
have populated the stringstream with a new string.

I came across some older mailing list archives about this, and here is
my approach (for this particular parser, start() expects a string
enclosed by square brackets):

istream_->str( "[testing string]" );
parser_->start();

// next round

istream_->str( "[new testing string]" );

// reset lexer, parser
lexer_->getInputState()->initialize( *istream_, "<testing stream>" );
parser_->getInputState()->reset();
parser_->setFilename( "<testing stream>" );

parser_->start();

But it doesn't seem to work, the first time works fine, but the next
round gives me this error:

<testing stream>: error: recognition error
<testing stream>:1:1: expecting [, found ''

Could anyone offer some assistance?  Thanks!

Best,
Scott


More information about the antlr-interest mailing list