1 | #include <iostream> |
---|
2 | #include <fstream> |
---|
3 | |
---|
4 | #include "LangLexer.hpp" |
---|
5 | #include "LangParser.hpp" |
---|
6 | #include "LangWalker.hpp" |
---|
7 | |
---|
8 | int main() |
---|
9 | { |
---|
10 | ANTLR_USING_NAMESPACE(std) |
---|
11 | ANTLR_USING_NAMESPACE(antlr) |
---|
12 | try { |
---|
13 | LangLexer lexer(cin); |
---|
14 | LangParser parser(lexer); |
---|
15 | // set up the ast factory to use a custom AST type per default |
---|
16 | // note that here the Ref prefix for the reference counter is |
---|
17 | // strippped off. |
---|
18 | ASTFactory ast_factory("MyAST", MyAST::factory); |
---|
19 | |
---|
20 | // let the parser add it's stuff to the factory... |
---|
21 | parser.initializeASTFactory(ast_factory); |
---|
22 | parser.setASTFactory(&ast_factory); |
---|
23 | |
---|
24 | parser.block(); |
---|
25 | |
---|
26 | cout << parser.getAST()->toStringList() << endl; |
---|
27 | |
---|
28 | LangWalker walker; |
---|
29 | // these two are not really necessary |
---|
30 | // since we're not building an AST |
---|
31 | walker.initializeASTFactory(ast_factory); |
---|
32 | walker.setASTFactory(&ast_factory); |
---|
33 | |
---|
34 | walker.block(RefMyAST(parser.getAST())); // walk tree |
---|
35 | cout << "done walking" << endl; |
---|
36 | |
---|
37 | #if 0 |
---|
38 | // disabled until configure/Makefile stuff stabilizes again |
---|
39 | cout << "Writing AST" << endl; |
---|
40 | ofstream xmlfile("out.xml"); |
---|
41 | if( xmlfile ) |
---|
42 | { |
---|
43 | xmlfile << parser.getAST(); |
---|
44 | xmlfile.close(); |
---|
45 | } |
---|
46 | cout << "Reading AST back" << endl; |
---|
47 | ifstream infile("out.xml"); |
---|
48 | |
---|
49 | RefAST read_ast = ast_factory.LoadAST(infile); |
---|
50 | if( ! parser.getAST()->equalsList(read_ast) ) |
---|
51 | cout << "AST's didn't match" << endl; |
---|
52 | else |
---|
53 | cout << "AST's matched!" << endl; |
---|
54 | #endif |
---|
55 | } |
---|
56 | catch( ANTLRException& e ) |
---|
57 | { |
---|
58 | cerr << "exception: " << e.getMessage() << endl; |
---|
59 | return -1; |
---|
60 | } |
---|
61 | catch( exception& e ) |
---|
62 | { |
---|
63 | cerr << "exception: " << e.what() << endl; |
---|
64 | return -1; |
---|
65 | } |
---|
66 | return 0; |
---|
67 | } |
---|