-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
265 lines (213 loc) · 5.72 KB
/
parser.cpp
File metadata and controls
265 lines (213 loc) · 5.72 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <iostream>
#include <list>
#include "lexer.hpp"
#include "parser.hpp"
node_t::node_t() {
left = nullptr ;
right = nullptr ;
isbracket = false ;
data.value = -1;
}
syntax_tree_t::syntax_tree_t() {
state_ = 0;
root_ = nullptr;
}
syntax_tree_t::syntax_tree_t(node_t *new_root) {
state_ = 0;
root_ = new_root;
}
syntax_tree_t::syntax_tree_t(lex_array_t &lex_array) {
state_ = 0;
root_ = nullptr;
root_ = parse_expr(lex_array);
if (state_ != lex_array.size_) {
printf("strange input constructions!\n");
std::cout << lex_array.size_ << std::endl;
destroy_syntax_tree_t(root_);
root_ = nullptr;
return;
}
}
syntax_tree_t::syntax_tree_t(const syntax_tree_t &other) {
root_ = other.root_;
}
node_t *syntax_tree_t::parse_term(lex_array_t &lex_array) {
node_t *new_node = nullptr;
if (state_ >= lex_array.size_)
return nullptr;
switch (lex_array.lexems_[state_].kind) {
case BRAC:
if (lex_array.lexems_[state_].lex.b == LBRAC) {
++state_;
}
else {
std::cout << "Syntax error: unexpected bracket type - expected \'(\'\n" ;
state_ = lex_array.size_ + 1;
return nullptr;
}
new_node = parse_expr(lex_array);
if (new_node != nullptr)
new_node->isbracket = true;
if (lex_array.lexems_[state_].lex.b == RBRAC) {
++state_;
}
else {
std::cout << "Syntax error: unexpected bracket type - expected \')\'\n" ;
state_ = lex_array.size_ + 1;
return nullptr;
}
return new_node;
case VAR:
new_node = new node_t();
new_node->data.k = NODE_VAR;
new_node->data.u.var = lex_array.lexems_[state_++].lex.var;
return new_node;
case OP:
if (lex_array.lexems_[state_].lex.op == NOT) {
new_node = new node_t();
new_node->data.k = NODE_OP;
new_node->data.u.op = lex_array.lexems_[state_++].lex.op;
new_node->left = parse_term(lex_array);
return new_node;
}
printf("Operation does not exist!\n");
state_ = lex_array.size_ + 1;
return nullptr;
case T:
new_node = new node_t();
new_node->data.k = NODE_VAL;
new_node->data.u.val = 1;
new_node->data.value = 1;
++state_;
return new_node;
case F:
new_node = new node_t();
new_node->data.k = NODE_VAL;
new_node->data.u.val = 0;
new_node->data.value = 0;
++state_;
return new_node;
default:
std::cout << "Syntax error: unknow term type\n" ;
state_ = lex_array.size_ + 1;
return nullptr;
}
}
node_t *syntax_tree_t::parse_conjunct(lex_array_t &lex_array) {
node_t *new_node = nullptr;
new_node = parse_term(lex_array);
if (state_ >= lex_array.size_ || lex_array.lexems_[state_].lex.op != AND) {
return new_node;
}
node_t *temp = new node_t ();
temp->data.k = NODE_OP;
temp->data.u.op = AND;
temp->left = new_node;
temp->isbracket = false;
++state_;
temp->right = parse_conjunct(lex_array);
return temp;
}
node_t *syntax_tree_t::parse_disjunct(lex_array_t &lex_array) {
node_t *new_node;
new_node = parse_conjunct(lex_array);
if (state_ >= lex_array.size_ || lex_array.lexems_[state_].lex.op != OR) {
return new_node;
}
node_t *temp = new node_t();
temp->data.k = NODE_OP;
temp->data.u.op = OR;
temp->left = new_node;
temp->isbracket = false;
++state_;
temp->right = parse_disjunct(lex_array);
return temp;
}
node_t *syntax_tree_t::parse_expr(lex_array_t &lex_array) {
node_t *new_node;
new_node = parse_disjunct(lex_array);
if (state_ >= lex_array.size_ || lex_array.lexems_[state_].lex.op != IMPL) {
return new_node;
}
node_t *temp = new node_t();
temp->data.k = NODE_OP;
temp->data.u.op = IMPL;
temp->left = new_node;
temp->isbracket = false;
++state_;
temp->right = parse_expr(lex_array);
return temp;
}
void print_n(node_t *node) {
if (node == nullptr) {
printf("nullptr node\n");
}
switch (node->data.k) {
case NODE_OP:
switch (node->data.u.op) {
case AND: std::cout << "&" ; break;
case OR: std::cout << "|" ; break;
case IMPL: std::cout << "->" ; break;
case NOT: std::cout << "~" ; break;
case EQUAL: std::cout << "=" ; break;
default: std::cout << "parser error\n" ; return;
}
break;
case NODE_VAL: std::cout << node->data.u.val ; break;
case NODE_VAR: std::cout << node->data.u.var ; break;
default: printf("<%d - unknow node type>", node->data.k);
}
}
void syntax_tree_t::show() {
print_node(root_);
std::cout << std::endl;
}
void syntax_tree_t::destroy_syntax_tree_t(node_t *node) {
if (node == nullptr) return;
destroy_syntax_tree_t(node->left );
destroy_syntax_tree_t(node->right);
delete node;
return;
}
void syntax_tree_t::print_node(node_t *root) {
if (root == nullptr) return;
if (root->data.k == NODE_OP && root->data.u.op == NOT) {
std::cout << "~" ;
print_node(root->left);
return;
}
else if (root->isbracket) {
std::cout << "(" ;
print_node(root->left);
print_n(root);
print_node(root->right);
std::cout << ")" ;
} else {
print_node(root->left);
print_n(root);
print_node(root->right);
}
}
node_t *copy_node(node_t *copied_node) {
if (copied_node == nullptr)
return nullptr;
node_t *new_node = new node_t() ;
new_node->right = copy_node(copied_node->right);
new_node->left = copy_node(copied_node->left );
new_node->data.k = copied_node->data.k;
new_node->data.u = copied_node->data.u;
new_node->data.value = copied_node->data.value;
return new_node;
}
node_t *syntax_tree_t::copy_tree_root (node_t *root) {
return copy_node(root);
}
syntax_tree_t::~syntax_tree_t() {
destroy_syntax_tree_t(root_);
}
void destroy_subtree(node_t *current) {
if (current == nullptr) return;
destroy_subtree(current->left );
destroy_subtree(current->right);
delete current;
}