-
Notifications
You must be signed in to change notification settings - Fork 7
/
interpret.c
304 lines (264 loc) · 6.04 KB
/
interpret.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
#include <stdio.h>
#include <string.h>
#include <lib.h>
#include <reg.h>
#include <set.h>
#include <nfa.h>
#define MAXSTACKNFAS 128
//#define RECURSION_EPSILON_CLOSURE
#ifdef RECURSION_EPSILON_CLOSURE
/* real recursion computation function */
void e_closure(struct nfa *nfa, struct set *stateset, struct accept **acp)
{
int state;
if (!nfa)
return;
state = nfastate(nfa);
/* greedy algorithm: accept as much states as possible */
if (!nfa->next[0]) {
if (!nfa->accept)
errexit("no accept action string");
if (acp)
*acp = nfa->accept;
}
if (nfa->edge == EG_EPSILON) {
/* next 0 */
if (nfa->next[0] &&
!test_add_set(stateset, nfastate(nfa->next[0])))
e_closure(nfa->next[0], stateset, acp);
/* next 1 */
if (nfa->next[1] &&
!test_add_set(stateset, nfastate(nfa->next[1])))
e_closure(nfa->next[1], stateset, acp);
}
}
/*
* recursion implemented computation for epsilon closure set from input
* @dup if 1, output set realloced
* otherwise, epsilon closure set is added into input
* and return input
* @accept accept state
* @input start state for computing epsilon closure
*
* input only contains sstate
*/
struct set *epsilon_closure(struct set *input, struct accept **acp, int dup)
{
int start_state[MAXSTACKNFAS];
int state;
struct set *output;
/*
* We cannot using e_closure(nfa, output),
* because sstate is already in output.
*/
if (acp)
*acp = NULL;
if (!input)
return NULL;
/* set return value(epsilon closure set) */
output = dup ? dupset(input) : input;
/* buffering start state int input */
for (startmember(input), state = 0; state < MAXSTACKNFAS; state++) {
start_state[state] = nextmember(input);
if (start_state[state] == -1) {
state--;
break;
}
}
if (state >= MAXSTACKNFAS)
errexit("state stack overflows");
/* computing epsilon closure of every start state in input set */
for (; state >= 0; state--)
e_closure(statenfa(start_state[state]), output, acp);
return output;
}
#else /* RECURSION_EPSILON_CLOSURE */
/*
* stack implemented computation for epsilon closure set from input
*/
struct set *epsilon_closure(struct set *input, struct accept **acp, int dup)
{
struct nfa *nfastack[MAXSTACKNFAS];
struct nfa *nfa;
int top = -1, state;
struct set *output;
int i;
/* init accpet */
if (acp)
*acp = NULL;
if (!input)
return NULL;
/* set return value (output set)*/
output = dup ? dupset(input) : input;
/* push all states in the input set onto nfastack */
for_each_member(state, input) {
nfastack[++top] = statenfa(state);
if (top >= MAXSTACKNFAS)
errexit("nfa stack overflows");
}
/* real handling */
while (top >= 0) {
nfa = nfastack[top--];
/* NOTE: terminal nfa is epsilon but its next is NULL */
/* epsilon transition from nfa to nfa->next[0|1] */
if (nfa->edge == EG_EPSILON) {
if (nfa->next[0] &&
!test_add_set(output, nfastate(nfa->next[0]))) {
nfastack[++top] = nfa->next[0];
if (top >= MAXSTACKNFAS)
errexit("nfa stack overflows");
}
if (nfa->next[1] &&
!test_add_set(output, nfastate(nfa->next[1]))) {
nfastack[++top] = nfa->next[1];
if (top >= MAXSTACKNFAS)
errexit("nfa stack overflows");
}
}
if (!nfa->next[0]) {
if (acp)
*acp = nfa->accept;
}
}
return output;
}
#endif /* !RECURSION_EPSILON_CLOSURE */
/*
* return a set that contains all states that can be reached by
* making transitions on @state from NFA set @input
*
* @output is moved from @input on @state
*/
struct set *move(struct set *input, int state)
{
struct set *output = NULL;
struct nfa *nfa;
int i;
for_each_member(i, input) {
nfa = statenfa(i);
if ((nfa->edge == EG_CCL && memberofset(state, nfa->set)) ||
(nfa->edge == state)) {
if (!output)
output = newset();
/* assert nfa->next[1] == NULL */
addset(output, nfastate(nfa->next[0]));
}
}
return output;
}
void printstateset(struct set *set)
{
int state;
for_each_member(state, set)
printf("%d ", state);
printf("\n");
}
#ifdef INTERPRET
void usage(void)
{
fprintf(stderr, "USAGE: egrep_like regexpfile file\n");
exit(EXIT_FAILURE);
}
static int lineno;
int grep(char *line, struct nfa *nfa, char **endstr)
{
struct set *start, *end;
struct accept *accept, *prevaccept;
int c, buflen;
char *buf, *bufpos;
/* init accept state */
accept = NULL;
prevaccept = NULL;
/* init start state set */
start = newset();
addset(start, nfastate(nfa));
start = epsilon_closure(start, &accept, 0);
/* get input string */
buf = line;
buflen = strlen(buf);
bufpos = buf;
/* matching: greedy algorithm */
while (c = *bufpos) {
end = move(start, c);
if (!end)
break;
end = epsilon_closure(end, &accept, 0);
if (accept) {
prevaccept = accept;
accept = NULL;
}
/* for next loop */
freeset(start);
start = end;
end = NULL;
bufpos++;
}
if (start)
freeset(start);
if (end)
freeset(end);
/* output matched string */
if (prevaccept) {
if (endstr)
*endstr = bufpos;
return 1;
}
/* not matched */
return 0;
}
extern FILE *fout;
/* egrep-like test program */
int main(int argc, char **argv)
{
char line[256];
char *lp, *ep;
FILE *f;
struct nfa *nfa;
/* handle arguments */
if (argc != 3)
usage();
/* init token stream: interpreting regular expression */
open_script(argv[1]);
fout = fopen("/dev/null", "w");
if (!fout)
errexit("fopen");
parse_cheader();
parse_macro();
/* construct NFA from regular expression */
parse_prepare_regexp();
init_nfa_buffer();
nfa = machine();
/* init file stream */
f = fopen(argv[2], "r");
if (!f)
errexit("fopen");
/* grep */
lineno = 0;
while (fgets(line + 1, 255, f)) {
line[0] = '\n'; /* prev line tail for start anchor `^` */
lineno++;
lp = line;
ep = NULL;
while (*lp) {
if (grep(lp, nfa, &ep))
break;
lp++;
}
/* output matched pattern */
if (ep) {
/* elimite prev line tail */
if (*lp == '\n') {
lp++;
}
/* red matched pattern */
printf(green(%d)cambrigeblue(:)"%.*s"red(%.*s)"%s",
lineno,
lp - line - 1, line + 1,
ep - lp, lp,
ep);
}
}
fclose(f);
return 0;
}
#endif /* INTERPRET */