-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex_tree.cpp
237 lines (213 loc) · 5.47 KB
/
regex_tree.cpp
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
#include "regex_tree.h"
#include "utility.h"
RegexTree::RegexTree(const Regex& regex)
{
// Calculate the closing parens indices for each open paren
calc_close_index(regex.symbols());
// Set the root for the RegexTree
root = init<Node>(regex.symbols(), 0, regex.symbols().size());
}
std::vector<RegexTree::symbol_type> RegexTree::labels() const
{
std::unordered_set<symbol_type> result;
for (const auto& leaf : _leaves)
if (leaf->label().to_string() != "#")
result.insert(leaf->label());
return std::vector<symbol_type>(result.cbegin(), result.cend());
}
RegexTree::Node::Node(Type type,
std::unique_ptr<Node> left,
std::unique_ptr<Node> right)
: _type(type)
{
switch (type)
{
case Type::CONCAT:
case Type::UNION:
_children.emplace_back(std::move(left));
_children.emplace_back(std::move(right));
break;
case Type::STAR:
_children.emplace_back(std::move(left));
break;
default:
// TODO: throw another exception: can't make node of this type
throw std::exception();
}
}
RegexTree::Node::Node(Type type, symbol_type label, regex_id_type regex_id)
: _type(type), _label(label), _regex_id(regex_id)
{
if (type != Type::LEAF)
// TODO: throw exception: can't make node of this type
throw std::exception();
}
RegexTree::Node* RegexTree::Node::left() const
{
if (_type == Type::CONCAT || _type == Type::UNION)
{
return _children[0].get();
}
// TODO: throw exception: node has no left child
throw std::exception();
}
RegexTree::Node* RegexTree::Node::right() const
{
if (_type == Type::CONCAT || _type == Type::UNION)
{
return _children[1].get();
}
// TODO: throw exception: node has no right child
throw std::exception();
}
RegexTree::Node* RegexTree::Node::child() const
{
if (_type == Type::STAR)
{
return _children[0].get();
}
// TODO: throw exception: node has no children
throw std::exception();
}
RegexTree::symbol_type RegexTree::Node::label() const
{
if (_type == Type::LEAF)
{
return _label;
}
// TODO: throw exception: node is not a leaf
throw std::exception();
}
void RegexTree::calc_close_index(const std::vector<symbol_type>& symbols)
{
close_index.resize(symbols.size(), symbols.size());
std::vector<int> stack;
for (std::size_t i = 0; i < symbols.size(); ++i)
{
if (symbols[i].is_open_paren())
{
stack.emplace_back(i);
}
else if (symbols[i].is_close_paren())
{
close_index[stack.back()] = i;
stack.pop_back();
}
}
}
AugmentedRegexTree::leaf_pos_type AugmentedRegexTree::Node::leaf_pos() const
{
if (_type == Node::Type::LEAF)
{
return _leaf_pos;
}
// TODO: throw exception: node is not a leaf
throw std::exception();
}
AugmentedRegexTree::AugmentedRegexTree(const AugmentedRegex& regex)
{
// Calculate the closing parens indices for each open paren
calc_close_index(regex.symbols());
// Set the root for the RegexTree
root = init<Node>(regex.symbols(), 0, regex.symbols().size());
for (std::size_t i = 0; i < _leaves.size(); ++i)
{
static_cast<Node*>(_leaves[i])->_leaf_pos = i;
}
auto root_node = static_cast<Node*>(root.get());
// Calculate nullable
calc_nullable(root_node);
// Calculate firstpos and lastpos
calc_first_last_pos(root_node);
// Calculate followpos for the RegexTree
calc_followpos(root_node);
}
void AugmentedRegexTree::calc_nullable(AugmentedRegexTree::Node* node)
{
if (node->is_leaf())
node->nullable = false;
else if (node->is_star())
node->nullable = true;
else if (node->is_union() || node->is_concat())
{
calc_nullable(node->left());
calc_nullable(node->right());
if (node->is_union())
node->nullable = node->left()->nullable | node->right()->nullable;
else
node->nullable = node->left()->nullable & node->right()->nullable;
}
else
// TODO: throw exception: node is of undefined type
throw std::exception();
}
void AugmentedRegexTree::calc_first_last_pos(AugmentedRegexTree::Node* node)
{
if (node->is_leaf())
{
node->firstpos.insert(node->leaf_pos());
node->lastpos.insert(node->leaf_pos());
}
else if (node->is_union() || node->is_concat())
{
calc_first_last_pos(node->left());
calc_first_last_pos(node->right());
if (node->is_union() || node->left()->nullable)
{
node->firstpos = utility::union_sets(node->left()->firstpos,
node->right()->firstpos);
}
else
{
node->firstpos = node->left()->firstpos;
}
if (node->is_union() || node->right()->nullable)
{
node->lastpos = utility::union_sets(node->left()->lastpos,
node->right()->lastpos);
}
else
{
node->lastpos = node->right()->lastpos;
}
}
else if (node->is_star())
{
calc_first_last_pos(node->child());
node->firstpos = node->child()->firstpos;
node->lastpos = node->child()->lastpos;
}
else
// TODO: throw exception: node is of undefined type
throw std::exception();
}
void AugmentedRegexTree::calc_followpos(AugmentedRegexTree::Node* node)
{
if (node->is_union())
{
calc_followpos(node->left());
calc_followpos(node->right());
}
if (node->is_concat())
{
for (auto fpos : node->right()->firstpos)
{
for (auto lpos : node->left()->lastpos)
{
static_cast<Node*>(_leaves[lpos])->followpos.insert(fpos);
}
}
calc_followpos(node->left());
calc_followpos(node->right());
}
if (node->is_star())
{
for (auto lpos : node->child()->lastpos)
{
static_cast<Node*>(_leaves[lpos])->followpos.insert(
node->child()->firstpos.cbegin(),
node->child()->firstpos.cend());
}
calc_followpos(node->child());
}
}