-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathre_to_nfa.cpp
103 lines (80 loc) · 1.98 KB
/
re_to_nfa.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
#include <iostream>
enum NodeType {
CHARACTER,
CONCATENATION,
UNION,
KLEENE
};
class ExpressionTree {
public:
NodeType type;
char value;
struct ExpressionTree * left;
struct ExpressionTree * right;
ExpressionTree(NodeType t) {
type = t;
left = NULL;
right = NULL;
}
};
struct Transition {
char symbol;
std::vector<struct NFA_State *> destinationStates;
}
struct NFA_State {
std::vector<struct Transition> transitions;
}
struct NFA {
struct NFA_State * startingState;
struct NFA_State * endingState;
}
struct NFA * eval(ExpressionTree *);
struct NFA * eval_char(ExpressionTree *);
struct NFA * eval_concat(ExpressionTree *);
struct NFA * eval_union(ExpressionTree *);
struct NFA * eval_kleene(ExpressionTree *);
struct NFA * eval(ExpressionTree * root) {
switch(root->type) {
case CHARACTER:
return eval_char(root);
break;
case CONCATENATION:
return eval_concat(root);
case UNION:
return eval_union(root);
case KLEENES:
return eval_kleens(root);
}
}
struct NFA * eval_char(ExpressionTree * root) {
struct NFA * nfa = new struct NFA;
nfa->startingState = new struct NFA_State;
nfa->endingState = new struct NFA_State;
struct Transition t;
t.symbol = root->value;
t.destinationStates.push_back(nfa->endingState);
nfa->startingState->transitions.push_back(t);
return nfa;
}
struct NFA * eval_concat(ExpressionTree * root) {
struct NFA * left_nfa = eval(root->left);
struct NFA * right_nfa = eval(root->right);
Transition t;
t.symbol
left_nfa->endingState->transitions.push_back(t);
}
struct ExpressionTree * createTree() {
ExpressionTree * a = new ExpressionTree(CHARACTER);
ExpressionTree * b = new ExpressionTree(CHARACTER);
ExpressionTree * plus = new ExpressionTree(UNION);
a->value = 'a';
b->value = 'b';
plus->left = a;
plus->right = b;
return plus;
}
int main() {
struct ExpressionTree* root = createTree();
cout << root << endl;
return 0;
}