-
Notifications
You must be signed in to change notification settings - Fork 7
/
dfa.c
240 lines (212 loc) · 5.09 KB
/
dfa.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
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
#include <stdio.h>
#include <stdlib.h>
#include <set.h>
#include <nfa.h>
#include <dfa.h>
#include <lib.h>
/* dfa sets auxiliary function */
static int currentdfa;
int ndfas;
struct dfa *dfastates = NULL;
void free_dfa(struct dfa *dfa)
{
if (dfa->states) {
freeset(dfa->states);
dfa->states = NULL;
}
if (dfa->accept) {
freeaccept(dfa->accept);
dfa->accept = NULL;
}
}
static void free_dfas(void)
{
int i;
for (i = 0; i < ndfas; i++)
free_dfa(&dfastates[i]);
free(dfastates);
}
static void init_dfas(struct nfa *sstate, struct set *accept)
{
struct accept *acp;
struct set *first;
int i;
/* alloc dfa buffer */
dfastates = xmalloc(MAX_DFAS * sizeof(struct dfa));
/* init dfas */
for (i = 0; i < MAX_DFAS; i++) {
dfastates[i].group = -1;
dfastates[i].states = NULL;
dfastates[i].accept = NULL;
}
/* init first dfa state */
first = newset();
addset(first, nfastate(sstate));
epsilon_closure(first, &acp, 0);
/* NOTE: first dfa can be accepted, such as regexp: `.*` */
if (acp) {
dfastates[ndfas].accept = getaccept(acp);
addset(accept, 0);
}
dfastates[0].states = first;
/* some internal parmaters */
ndfas = 1;
currentdfa = 0;
}
static int add_dfa(struct set *nfastates, struct accept *accept)
{
if (ndfas >= MAX_DFAS)
errexit("dfas overflows");
if (!dfastates)
errexit("not init dfas");
if (nfastates) {
dfastates[ndfas].states = nfastates;
dfastates[ndfas].accept = getaccept(accept);
}
return ndfas++;
}
static int in_dfa(struct set *states)
{
int i;
/* no safe check */
for (i = 0; i < ndfas; i++) {
if (equset(states, dfastates[i].states))
return i;
}
return -1;
}
int state_dfa(struct dfa *dfa)
{
return dfa - dfastates;
}
static struct dfa *next_dfa(void)
{
if (currentdfa >= ndfas)
return NULL;
return &dfastates[currentdfa++];
}
void subsetconstruct(int (*dfatable)[128], struct set *acceptset)
{
struct dfa *dfa;
struct set *next;
int nextstate = 0;
int c, state;
struct accept *accept;
while (dfa = next_dfa()) {
for (c = 0; c < MAX_CHARS; c++) {
/* compute next dfa, to which dfa move on c */
next = move(dfa->states, c);
next = epsilon_closure(next, &accept, 0);
/* no transition */
if (!next)
state = F;
/* transition from current to next */
else if ((state = in_dfa(next)) >= 0)
freeset(next); /* next is alloced by move()*/
else
state = add_dfa(next, accept);
/* real assign the dfatable: [0->ndfas][0->MAX_CHARS] */
dfatable[state_dfa(dfa)][c] = state;
/* NOTE: using state, not ndfas - 1 */
if (accept)
addset(acceptset, state);
}
}
}
/*
* subset construction:
* convert NFA directed graph to DFA table
*
* RETURN:
* @table dfa state transtion table
* @acceptset dfa acceptable state set
* return value dfa state transtion table size
*/
int construct_dfa(struct nfa *sstate, int (**table)[], struct set **acceptset)
{
/* dfatable[STATES][CHARS] */
int (*dfatable)[MAX_CHARS];
struct set *accept;
int i;
/* init dfa table */
dfatable = xmalloc(MAX_TABLESIZE * sizeof(int));
/* alloc accept set */
accept = newset();
/*
* init internal dfa auxiliary method,
* which is used in subsetconstruct()
*/
init_dfas(sstate, accept);
/* subset construction */
subsetconstruct(dfatable, accept);
/* adjust dfatable real size */
dfatable = realloc(dfatable, ndfas * MAX_CHARS * sizeof(int));
/* return value */
if (table)
*table = dfatable;
else
free(dfatable);
if (acceptset)
*acceptset = accept;
else
freeset(accept);
return ndfas;
}
void traverse_dfatable(int (*dfatable)[128], int size, struct set *accept)
{
int i, c;
printf("\n");
fprintf(stderr, "\n\n[==== DFA Transition Table ====]\n");
for (i = 0; i < size; i++) {
for (c = 0; c < MAX_CHARS; c++)
if (dfatable[i][c] >= 0)
fprintf(stderr, " %d --> %d on %c\n",
i, dfatable[i][c], c);
if (dfatable[i][c] >= size)
errexit("dfa table corrupt");
}
/* check and output accept information */
for_each_member(i, accept) {
if (!dfastates[i].accept) {
fprintf(stderr, "dfastate[%d] has no accept", i);
exit(EXIT_FAILURE);
}
fprintf(stderr, " accept state:%d ", i);
if (dfastates[i].accept->anchor & AC_START)
fprintf(stderr, "^ ");
fprintf(stderr,"%s", dfastates[i].accept->action);
if (dfastates[i].accept->anchor & AC_END)
fprintf(stderr, " $");
fprintf(stderr, "\n");
}
fprintf(stderr, "[==== END DFA Transition Table ====]\n");
}
#ifdef DFA_TABLE_TEST
int main(int argc, char **argv)
{
char line[256];
struct nfa *nfa;
struct set *accept;
/*
* NOTE: int *table1[x] --> size = x*4
* int (*table2)[x] --> size = 4
* typeof(&table2) --> int (**table2p)[x]
*/
int (*table)[MAX_CHARS]; /* dfa table */
int size; /* dfa table size */
if (argc != 2)
errexit("ARGC != 2");
/* init token stream: interpreting regular expression */
text_open(argv[1]);
/* construct NFA from regular expression */
init_nfa_buffer();
nfa = machine();
traverse_nfa(nfa);
/* construct dfa table */
size = construct_dfa(nfa, &table, &accept);
traverse_dfatable(table, size, accept);
/* free dfa state sets */
free_dfas();
return 0;
}
#endif /* NFA_TABLE_TEST */