-
Notifications
You must be signed in to change notification settings - Fork 1
/
rgx.cpp
43 lines (36 loc) · 918 Bytes
/
rgx.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
#include "rgx.h"
#include "ast.h"
#include "astnode.h"
#include <stack>
using namespace rgx;
using namespace std;
shared_ptr<_pattern> rgx::compile(const string & re) {
_ast ast(re);
astTraversal(ast);
if (ast._build_type == _ast::build_to_dfa) {
return make_shared<_dfa_pattern>(ast);
} else {
return make_shared<_nfa_pattern>(ast);
}
}
void rgx::astTraversal(const _ast& ast) {
auto r = ast._root;
stack<decltype(r)> s;
if (r) {
s.push(r);
} else {
return;
}
while(!s.empty()) {
auto e = s.top();
s.pop();
cout << e->toString();
if (e->_right) {
s.push(e->_right);
}
if (e->_left) {
s.push(e->_left);
}
}
cout << "+++++++++++++++++++++++++++++++-----------astTraversalEnd-------------++++++++++++++++++++++++++++++++++++++++++\n" << endl;
}