-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.h
More file actions
60 lines (48 loc) · 1.36 KB
/
Node.h
File metadata and controls
60 lines (48 loc) · 1.36 KB
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
#ifndef NODE_H
#define NODE_H
#include <list>
#include <iostream>
#include <fstream>
#include <vector>
#include <unistd.h>
#include <sys/wait.h>
using namespace std;
class Node {
public:
int id, lineno;
string type, value;
list<Node*> children;
Node(string t, string v, int l) : type(t), value(v), lineno(l){}
Node()
{
type = "uninitialised";
value = "uninitialised"; } // Bison needs this.
void print_tree(int depth=0) {
for(int i=0; i<depth; i++)
cout << " ";
cout << type << ":" << value << endl; //<< " @line: "<< lineno << endl;
for(auto i=children.begin(); i!=children.end(); i++)
(*i)->print_tree(depth+1);
}
void generate_tree() {
std::ofstream outStream;
char* filename = "tree.dot";
outStream.open(filename);
int count = 0;
outStream << "digraph {" << std::endl;
generate_tree_content(count, &outStream);
outStream << "}" << std::endl;
outStream.close();
printf("\nBuilt a parse-tree at %s. Use 'make tree' to generate the pdf version.\n", filename);
}
void generate_tree_content(int &count, ofstream *outStream) {
id = count++;
*outStream << "n" << id << " [label=\"" << type << ":" << value << "\"];" << endl;
for (auto i = children.begin(); i != children.end(); i++)
{
(*i)->generate_tree_content(count, outStream);
*outStream << "n" << id << " -> n" << (*i)->id << endl;
}
}
};
#endif