1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
#include <iostream>
#include <fstream>
#include <antlr/CommonAST.hpp>
#include "JavaLexer.hpp"
#include "JavaRecognizer.hpp"
#include "JavaTreeParser.hpp"
ANTLR_USING_NAMESPACE(std)
ANTLR_USING_NAMESPACE(antlr)
size_t errors = 0;
static void doTreeAction( ASTFactory& factory, RefAST t)
{
if ( t == nullAST )
return;
JavaTreeParser tparse;
tparse.initializeASTFactory(factory);
tparse.setASTFactory(&factory);
try {
tparse.compilationUnit(t);
}
catch (ANTLRException& e) {
cerr << e.toString() << endl;
}
}
// 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;
errors++;
}
catch (exception& e) {
cerr << "exception: " << e.what() << endl;
errors++;
}
}
int main(int argc,char* argv[])
{
// Use a try/catch block for parser exceptions
try
{
// if we have at least one command-line argument
if ( argc > 1 )
{
cerr << "Parsing..." << endl;
// for each file specified on the command line
for(int i=1; i< argc;i++)
{
cerr << " " << argv[i] << endl;
parseFile(argv[i]);
}
}
else
cerr << "Usage: " << argv[0] << " <file name(s)>" << endl;
}
catch(exception& e) {
cerr << "exception: " << e.what() << endl;
return -1;
}
if( errors )
return -1;
return 0;
}
|