-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparsetree.h
166 lines (132 loc) · 4.27 KB
/
parsetree.h
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#pragma once
#include <list>
struct Node {
Node * parent;
string nonTerminal;
Record token;
list<Node*> children;
Node(const string & rule) : children(), parent(nullptr), nonTerminal(rule) {
}
Node(Record & token) : token(token), children(), parent(nullptr) {
}
Node(Record * token)
: token(*token),
children(),
parent(nullptr) {
}
Node(const string & rule, Record & token) : nonTerminal(rule), token(token), children(), parent(nullptr) {
}
Node * add(Node * child) {
child->parent = this;
children.push_back(child);
return child;
}
Node * addFirst(Node * child) {
child->parent = this;
children.push_front(child);
return child;
}
};
class ParseTree {
Node * root;
public:
ParseTree(): root(new Node("<Program>")) {
}
~ParseTree() {
deleteAllNodes();
}
Node * getRoot() {
return root;
}
virtual void printNodes(ostream & stream, bool showNonTerminal = false) {
stream << "-------Parse Tree Code-------------------" << endl;
printNodes(stream, root, showNonTerminal);
}
virtual void printRules(ostream & stream) {
stream << "-------Parse Tree Productions -----------" << endl;
return printRules(stream, root);
}
virtual void printTree(ostream & stream) {
stream << "-------Parse Tree -----------------------" << endl;
return printTree(stream, root);
}
void addToRoot(Node * child) {
add(root, child);
}
void add(Node * parent, Node * child) {
parent->add(child);
}
//Node * getLeftmostNode() {
// return getLeftmostNode(root);
//}
protected:
void deleteAllNodes() {
deleteAllNodes(root);
}
void deleteAllNodes(Node * node) {
for (Node * child: node->children) {
deleteAllNodes(child);
}
delete node;
}
virtual void printNodes(ostream & stream, Node * node, bool showNonTerminal) {
if (node->children.empty()) {
if (node->token.lexeme != ";")
stream << " ";
stream << node->token.lexeme;
if (node->token.lexeme == ";")
stream << endl;
} else {
if (showNonTerminal) {
stream << " " << node->nonTerminal << endl;
}
for (Node * child: node->children) {
printNodes(stream, child, showNonTerminal);
}
}
}
virtual void printRules(ostream & stream, Node * node, Record * currentRecord = nullptr) {
if (node->children.empty()) {
// this is a terminal node
if (node->nonTerminal.empty()) {
if (currentRecord != &node->token) {
stream << node->token << endl;
currentRecord = &node->token;
}
} else {
stream << " " << node->nonTerminal << endl;
}
} else {
Node * leftNode = getLeftmostNode(node);
if (&leftNode->token != currentRecord)
stream << leftNode->token << endl;
stream << " " << node->nonTerminal << endl;
currentRecord = &leftNode->token;
for (auto it = node->children.begin(); it != node->children.end(); ++it) {
printRules(stream, *it, currentRecord);
}
}
}
public:
Node * getLeftmostNode(Node * node) {
if (node->children.empty()) {
return node;
}
return getLeftmostNode(*node->children.begin());
}
protected:
virtual void printTree(ostream & stream, Node * node, int indent = 0) {
if (node->children.empty()) {
if (node->nonTerminal.empty()) {
stream << string(indent * 2, ' ') << node->token.lexeme << endl;
} else {
stream << string(indent * 2, ' ') << node->nonTerminal << endl;
}
} else {
stream << string(indent*2, ' ') << node->nonTerminal << endl;
for (auto it = node->children.begin(); it != node->children.end(); ++it) {
printTree(stream, *it, indent + 1);
}
}
}
};