-
Notifications
You must be signed in to change notification settings - Fork 0
/
post2nfa.c
142 lines (133 loc) · 3.68 KB
/
post2nfa.c
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
#include "post2nfa.h"
#include <stdlib.h>
#include "nfa.h"
#include "state.h"
/// @brief Merges b into a, which connects the outs of b to a.
/// @param a The state to be replace. It's contents will be lost.
/// @param b The state to replace with.
/// @note State b is deleted after the merge.
static void merge_state(State* a, State* b) {
free(a->outs);
a->label = b->label;
a->outs = b->outs;
free(b);
}
/*
* Implements the McNaughton-Yamada-Thompson algorithm with extra supports on +
* (one or more) and ? (zero or one) operators.
*/
Nfa* post2nfa(const char* post) {
Nfa* stack[1000];
Nfa** top = stack;
#define IS_EMPTY() (top == stack)
#define PUSH(s) (*top++ = (s))
#define POP() IS_EMPTY() ? NULL : (*--top)
for (; *post; post++) {
switch (*post) {
case EXPLICIT_CONCAT: {
Nfa* n2 = POP();
Nfa* n1 = POP();
if (!n1 || !n2) {
if (n1) {
delete_nfa(n1);
}
if (n2) {
delete_nfa(n2);
}
return NULL;
}
merge_state(n1->accept, n2->start);
PUSH(create_nfa(n1->start, n2->accept));
free(n1);
free(n2);
} break;
case '|': {
Nfa* n2 = POP();
Nfa* n1 = POP();
if (!n1 || !n2) {
if (n1) {
delete_nfa(n1);
}
if (n2) {
delete_nfa(n2);
}
return NULL;
}
State* outs[2] = {n1->start, n2->start};
State* start = create_state(SPLIT, outs);
State* accept = create_state(ACCEPT, NULL);
n1->accept->label = EPSILON;
n1->accept->outs[0] = accept;
n2->accept->label = EPSILON;
n2->accept->outs[0] = accept;
PUSH(create_nfa(start, accept));
free(n1);
free(n2);
} break;
case '*': {
Nfa* n = POP();
if (!n) {
return NULL;
}
State* accept = create_state(ACCEPT, NULL);
State* outs[2] = {n->start, accept};
State* start = create_state(SPLIT, outs);
State* come_back = create_state(SPLIT, start->outs);
merge_state(n->accept, come_back);
PUSH(create_nfa(start, accept));
free(n);
} break;
case '?': {
Nfa* n = POP();
if (!n) {
return NULL;
}
State* accept = create_state(ACCEPT, NULL);
State* outs[2] = {n->start, accept};
State* start = create_state(SPLIT, outs);
n->accept->label = EPSILON;
n->accept->outs[0] = accept;
PUSH(create_nfa(start, accept));
free(n);
} break;
case '+': {
Nfa* n = POP();
if (!n) {
return NULL;
}
State* accept = create_state(ACCEPT, NULL);
State* outs[2] = {n->start, accept};
State* come_back = create_state(SPLIT, outs);
merge_state(n->accept, come_back);
// this extra epsilon transition is necessary so that the link doesn't
// break when merging the start state in concatenation
State* start = create_state(EPSILON, &n->start);
PUSH(create_nfa(start, accept));
free(n);
} break;
case '.': {
State* accept = create_state(ACCEPT, NULL);
State* start = create_state(ANY, &accept);
PUSH(create_nfa(start, accept));
} break;
default: {
State* accept = create_state(ACCEPT, NULL);
State* start = create_state(*post, &accept);
PUSH(create_nfa(start, accept));
} break;
}
}
Nfa* nfa = POP();
if (!IS_EMPTY()) {
delete_nfa(nfa);
while (!IS_EMPTY()) {
Nfa* n = POP();
delete_nfa(n);
}
return NULL;
}
return nfa;
#undef POP
#undef PUSH
#undef IS_EMPTY
}