[antlr-interest] Re: Token Stream.

queengiraffe <cow_jumped_moon at hotmail.com> cow_jumped_moon at hotmail.com
Thu Jan 30 05:05:35 PST 2003


> Use a loop similar to:
> 
> for(;;)
> {
>     RefToken tok = lexer.nextToken();
> 
>     if ( tok->getType() == Token::EOF_TYPE )
>         break;
> 
>     cout << "Token: " << tok->toString() << endl;
> }
> 
> You should really wrap this in a try...catch. Also see the some of 
> the projects in the examples/cpp directory.
> 
> Micheal

Thank you very much for your help. I'm afraid I'm still a little 
stuck, the loop you supplied does give me the type for the tokens, 
however it seems to get stuck right afterwards and will not move onto 
the parser part.

I am trying to apdapt the calc program supplied in examples for cpp. 
I've looked at the other examples and I'm afraid I am still stuck.

Have I done the coding wrong, I'm really sorry, I am very new to this.

void main()
{
	ANTLR_USING_NAMESPACE(std)
	ANTLR_USING_NAMESPACE(antlr)
	try
	{
		CalcLexer lexer(cin);
		lexer.setFilename("Lexer");

		for(;;)
		{
			RefToken tok = lexer.nextToken();
                         if ( tok->getType() == Token::EOF_TYPE )
                          break;

			cout << "Token: " << tok->getText() << endl;

		}
		
		CalcParser parser(lexer);
		parser.setFilename("Parser");

		// Parse the input expression
		parser.expr();
		RefAST t = parser.getAST();
		if( t )
		{
		// Print the resulting tree out in LISP notation
			cout << t->toStringTree() << endl;
			CalcTreeWalker walker;

			// Traverse the tree created by the parser
			float r = walker.expr(t);
			cout << "value is " << r << endl;
		}
		else
			cout << "No tree produced" << endl;

		
	}
	catch(exception& e)
	{
		cerr << "exception: " << e.what() << endl;
	}
}


 

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



More information about the antlr-interest mailing list