[antlr-interest] Parsing multiple messages using one parser

Ric Klaren ric.klaren at gmail.com
Wed Feb 8 13:37:35 PST 2006


On 2/8/06, Ric Klaren <ric.klaren at gmail.com> wrote:
> > Would you please be so kind to send some to me or post it on the mailing-list?
> > This would be a great help!
>
> I'll dig up the code at home and repost.

Here it is:

One time setup:

		// if we don't have a file we use stdin (cin)
		lexer = new MyLexer(std::cin);
		my_nodes_factory = new antlr::ASTFactory("MyAST", MyAST::factory );

		// Create a parser that reads from the scanner
		parser = new MyParser( *lexer );

		// Initialize it with factory and setup the factory and other trivia
		parser->setASTNodeFactory( my_nodes_factory );
		parser->initializeASTFactory( *my_nodes_factory );
		parser->setFilename("stdin");

Per file:
		// get our hands on the input state...
		antlr::LexerSharedInputState lex_input = lexer->getInputState();

		// and reinitialize it with the new file
		lex_input->initialize(file, filename);

		// and reset parser (antlr) internal state
		parser->getInputState()->reset();
		parser->setFilename(filename);

		// start parsing at the 'start' rule
		parser->start();

This code is from the 2.7.4 version. Might need some minor tweaking.

Cheers,

Ric


More information about the antlr-interest mailing list