// Here's where we do the real work... static void parseFile(const string& f) { try { ifstream s(f.c_str()); // Create a scanner that reads from the input stream JavaLexer lexer(s); lexer.setFilename(f); /* while (true) { RefToken t = lexer.nextToken(); if (t->getType() == Token::EOF_TYPE) break; cout << t->getText() << ":" << t->getType() << endl; } */ // Create a parser that reads from the scanner JavaRecognizer parser(lexer); parser.setFilename(f); // start parsing at the compilationUnit rule parser.compilationUnit(); // do something with the tree doTreeAction(f, parser.getAST()); } catch (exception& e) { cerr << "parser exception: " << e.what() << endl; // e.printStackTrace(); // so we can get stack trace } }
// Here's where we do the real work... static void parseFile(const string& f) { try { ifstream s(f.c_str()); // Create a scanner that reads from the input stream JavaLexer lexer(s); lexer.setFilename(f); /* while (true) { RefToken t = lexer.nextToken(); if (t->getType() == Token::EOF_TYPE) break; cout << t->getText() << ":" << t->getType() << endl; } */ // Create a parser that reads from the scanner JavaRecognizer parser(lexer); parser.setFilename(f); // make an ast factory ASTFactory ast_factory; // initialize and put it in the parser... parser.initializeASTFactory(ast_factory); parser.setASTFactory(&ast_factory); // start parsing at the compilationUnit rule parser.compilationUnit(); // do something with the tree doTreeAction( ast_factory, parser.getAST() ); } catch (ANTLRException& e) { cerr << "parser exception: " << e.toString() << endl; } catch (exception& e) { cerr << "exception: " << e.what() << endl; } }