-
Notifications
You must be signed in to change notification settings - Fork 7
/
mindfa.c
345 lines (311 loc) · 7.44 KB
/
mindfa.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <stdio.h>
#include <stdlib.h>
#include <lib.h>
#include <dfa.h>
#include <nfa.h>
#include <set.h>
struct set *groups[MAX_GROUPS];
int ngroups;
int sgroup; /* start group */
/* group auxiliary functio n*/
void add_group(int group, int dfa)
{
dfastates[dfa].group = group;
addset(groups[group], dfa);
}
void del_group(int dfa)
{
if (dfastates[dfa].group < 0)
errexit("del dfa not in some group");
delset(groups[dfastates[dfa].group], dfa);
dfastates[dfa].group = F;
}
int new_group(void)
{
if (ngroups >= MAX_GROUPS)
errexit("groups overflow!");
groups[ngroups] = newset();
return ngroups++;
}
void debug_group(void);
int init_groups(void)
{
int i, k, group;
struct dfa *dfa;
struct set *nonaccept;
for (i = 0; i < MAX_GROUPS; i++)
groups[i] = NULL;
/* init accept group and non-accept group */
sgroup = 0;
ngroups = 0;
new_group(); /* for non-accept group */
for (dfa = dfastates, i = 0; i < ndfas; dfa++, i++) {
group = 0;
if (dfa->accept) {
/* find the same accept(action) group */
for (k = 0; k < i; k++) {
if (dfastates[k].accept &&
dfastates[k].accept->action == dfa->accept->action) {
group = dfastates[k].group;
break;
}
}
/* not found, we create new accept group */
if (group == 0) /* group 0 is non-accept group */
group = new_group();
}
add_group(group, i);
}
/* fix: Is non-accept group? Remove it! */
if (emptyset(groups[0])) {
freeset(groups[0]);
groups[0] = NULL;
sgroup = 1;
}
#ifdef DEBUG
debug_group();
#endif
}
/* compute dfastates[dfatable[dfa][c]].group */
int transgroup(int (*dfatable)[128], int dfa, int c)
{
int nextdfa;
nextdfa = dfatable[dfa][c];
if (nextdfa == F)
return F;
/* dfastates[].group != F */
return dfastates[nextdfa].group;
}
void part_groups(int (*dfatable)[128], int g, int c)
{
struct set *group;
int firstgrp, newgrp;
int dfa, nextdfa;
/* get group set */
group = groups[g];
newgrp = -1;
/* get first dfa group */
startmember(group);
dfa = nextmember(group);
/* empty group */
if (dfa == -1)
return;
firstgrp = nextdfa = transgroup(dfatable, dfa, c);
/* loop every dfa in group */
while ((dfa = nextmember(group)) != -1) {
/* get dfa -- on c --> nextdfa::group */
if (transgroup(dfatable, dfa, c) != firstgrp) {
if (newgrp < 0)
newgrp = new_group();
del_group(dfa);
add_group(newgrp, dfa);
}
}
}
/*
* reset dfa accept string,
* make it associated its new state(group) number
* delete orignal accept set
*/
void reset_accept(struct set *accept, struct set **ap)
{
/* temp accept string stack */
struct {
int group;
struct accept *accept;
} accept_stack[MAX_GROUPS];
int i, top = 0;
struct dfa *dfa;
struct set *new;
/* new accept state set */
new = newset();
/* reset accept state */
for_each_member(i, accept) {
dfa = &dfastates[i];
if (!dfa->accept)
errexit("accept state dfa has no accept structure");
/* save accept string */
accept_stack[top].group = dfa->group;
accept_stack[top].accept = dfa->accept;
dfa->accept = NULL;
top++;
/* new accept set */
addset(new, dfa->group);
}
/* check */
for (i = 0; i < ndfas; i++)
if (dfastates[i].accept)
errexit("reset accept state error");
/* restore saved accept */
for (i = 0; i < top; i++) {
/* reduplicate accept dfa? */
if (dfastates[accept_stack[i].group].accept) {
freeaccept(accept_stack[i].accept);
} else {
dfastates[accept_stack[i].group].accept =
accept_stack[i].accept;
}
}
if (ap)
*ap = new;
else
freeset(new);
freeset(accept);
}
void minimize_dfa(int (*table)[128], struct set *accept, struct set **ap)
{
int c, group, n, start_group;
init_groups();
/* minimize dfatable */
do {
/* backup ngroups */
n = ngroups;
/* loop MAX_CHARS and loop ngroups can swich order */
for (c = 0; c < MAX_CHARS; c++) { /* loop MAX_CHARS */
for (group = sgroup; group < n; group++) {/* loop ngroups*/
part_groups(table, group, c);
}
}
} while (n < ngroups); /* add a new group(n < ngroups)? */
reset_accept(accept, ap);
}
int minimize_dfatable(int (*table)[128], int (**ret)[128])
{
struct set *group;
int (*t)[128]; /* new table */
int g, dfa, c;
#ifdef DEBUG_MIN_TABLE
int times = 0;
#endif
/* alloc new minimized table */
t = xmalloc(MAX_CHARS * (ngroups - sgroup) * sizeof(int));
group = newset();
/*
* loop hierarchy:
* DFAS --> Groups --> Chars
*/
for (dfa = 0; dfa < ndfas; dfa++) {
/*
* FIXED: Different dfa can make the same g!
* How to reduce the same g times?
* use group to detect whether group g has been handled!
*/
g = dfastates[dfa].group;
if (memberofset(g, group))
continue;
addset(group, g);
#ifdef DEBUG_MIN_TABLE
times++;
#endif
for (c = 0; c < MAX_CHARS; c++)
if (table[dfa][c] == F)
t[g - sgroup][c] = F;
else
t[g - sgroup][c] = dfastates[table[dfa][c]].group - sgroup;
}
/* set return table */
if (ret)
*ret = t;
#ifdef DEBUG_MIN_TABLE
dbg(" %d * 128(chars) times", times);
#endif
return (ngroups - sgroup);
}
int minimize_dfatable2(int (*table)[128], int (**ret)[128])
{
struct set *group;
int (*t)[128]; /* new table */
int g, dfa, c;
#ifdef DEBUG_MIN_TABLE
int times = 0;
#endif
/* alloc new minimized table */
t = xmalloc(MAX_CHARS * (ngroups - sgroup) * sizeof(int));
/*
* loop hierarchy:
* Groups --> DFAs --> Chars
*
* NOTE: new table row start from 0, not sgroup
*/
for (g = sgroup; g < ngroups; g++) {
startmember(groups[g]);
if ((dfa = nextmember(groups[g])) != -1) {
#ifdef DEBUG_MIN_TABLE
times++;
#endif
for (c = 0; c < MAX_CHARS; c++)
if (table[dfa][c] == F)
t[g - sgroup][c] = F;
else
t[g - sgroup][c] = dfastates[table[dfa][c]].group - sgroup;
/* only one table */
} else {
errexit("Group is empty!");
}
}
if (ret)
*ret = t;
#ifdef DEBUG_MIN_TABLE
dbg(" %d * 128(chars) times", times);
#endif
return (ngroups - sgroup);
}
void debug_group(void)
{
int i, g, n;
fprintf(stderr, "\n-------debug group-----------\n");
fprintf(stderr, "\n[dfas]\n");
for (i = 0; i < ndfas; i++) {
g = dfastates[i].group;
if (g == -1)
continue;
fprintf(stderr, "dfa %d group %d ", i, g);
if (dfastates[i].accept)
fprintf(stderr, "accept %s\n", dfastates[i].accept->action);
else
fprintf(stderr, "no accept\n");
}
fprintf(stderr, "[groups]\n");
for (g = sgroup; g < ngroups; g++) {
fprintf(stderr, "group:%d dfas:", g);
for_each_member(n, groups[g])
fprintf(stderr, "%d ", n);
fprintf(stderr, "\n");
}
fprintf(stderr, "-------end debug group-------\n\n");
}
#ifdef MIN_DFA_TEST
int main(int argc, char **argv)
{
struct nfa *nfa;
struct set *accept;
struct set *ap;
int (*table)[MAX_CHARS]; /* dfa table */
int (*mintable)[MAX_CHARS]; /* minimized dfa table */
int (*mintable2)[MAX_CHARS]; /* minimized dfa table2 */
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_nfas();
/* minimization */
minimize_dfa(table, accept, &ap);
/* debug */
debug_group();
dbg("------minimization 1 test--------");
minimize_dfatable(table, &mintable);
traverse_dfatable(mintable, ngroups - sgroup, ap);
dbg("------minimization 2 test--------");
minimize_dfatable2(table, &mintable2);
traverse_dfatable(mintable2, ngroups - sgroup, ap);
return 0;
}
#endif