-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.cc
65 lines (43 loc) · 1.27 KB
/
compiler.cc
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
#include <compiler.hh>
#include <flex.hh>
using namespace std;
Graph::vertex_descriptor Compiler::add_node(Node n) {
return this->ast.add_node(n);
}
void Compiler::add_child(Graph::vertex_descriptor v1,
Graph::vertex_descriptor v2) {
this->ast.add_child(v1, v2);
}
bool Compiler::compile(const string &f) {
file = f;
this->location.initialize(&file);
FILE *fp = fopen(file.c_str(), "r");
if (!fp) {
cerr << "Unable to open file: " << file << endl;
return false;
}
yyin = fp;
yy::parser bparser = yy::parser(*this);
yyset_debug(this->trace_scanning);
bparser.set_debug_level(this->trace_parsing);
bool successful = bparser.parse() == 0;
if (!successful) {
fclose(fp);
return false;
}
successful = successful && this->ast.semantic_analysis();
cout << endl << endl;
cout << term(Color::YELLOW) << "Abstract Syntax Tree: (Graphviz notation)"
<< term(Color::RESET) << endl;
this->ast.print_tree();
cout << endl;
cout << term(Color::YELLOW) << "Symbol table (n=" << ast.symbol_table.size()
<< ")" << term(Color::RESET) << endl;
cout << "[" << endl;
for (auto &s : ast.symbol_table) {
cout << " " << s << endl;
}
cout << "]" << endl << endl;
fclose(fp);
return successful;
}