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
|
#include <iostream>
#include <fstream>
#include "LangLexer.hpp"
#include "LangParser.hpp"
#include "LangWalker.hpp"
int main()
{
ANTLR_USING_NAMESPACE(std)
ANTLR_USING_NAMESPACE(antlr)
try {
LangLexer lexer(cin);
LangParser parser(lexer);
// set up the ast factory to use a custom AST type per default
// note that here the Ref prefix for the reference counter is
// strippped off.
ASTFactory ast_factory("MyAST", MyAST::factory);
// let the parser add it's stuff to the factory...
parser.initializeASTFactory(ast_factory);
parser.setASTFactory(&ast_factory);
parser.block();
cout << parser.getAST()->toStringList() << endl;
LangWalker walker;
// these two are not really necessary
// since we're not building an AST
walker.initializeASTFactory(ast_factory);
walker.setASTFactory(&ast_factory);
walker.block(RefMyAST(parser.getAST())); // walk tree
cout << "done walking" << endl;
#if 0
// disabled until configure/Makefile stuff stabilizes again
cout << "Writing AST" << endl;
ofstream xmlfile("out.xml");
if( xmlfile )
{
xmlfile << parser.getAST();
xmlfile.close();
}
cout << "Reading AST back" << endl;
ifstream infile("out.xml");
RefAST read_ast = ast_factory.LoadAST(infile);
if( ! parser.getAST()->equalsList(read_ast) )
cout << "AST's didn't match" << endl;
else
cout << "AST's matched!" << endl;
#endif
}
catch( ANTLRException& e )
{
cerr << "exception: " << e.getMessage() << endl;
return -1;
}
catch( exception& e )
{
cerr << "exception: " << e.what() << endl;
return -1;
}
return 0;
}
|