-
Notifications
You must be signed in to change notification settings - Fork 50
/
parse.c
450 lines (409 loc) · 10.7 KB
/
parse.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal. All Rights Reserved. */
/* the functions in this file parse a string that represents
a regular expression, and return a pointer to a syntax
tree for that regular expression. */
#ifdef _WIN32
#include <memory.h>
#include <stdlib.h>
#endif
#include <stdio.h>
#include "re.h"
#define FALSE 0
#define TRUE 1
#define NextChar(s) *(*s)++
#define Unexpected(s, c) (**s == NUL || **s == c)
#define Invalid_range(x, y) (x == NUL || x == '-' || x == ']' || x < y)
extern Stack Push(Stack *s, Re_node v);
extern Re_node Pop(Stack *s);
extern Re_node Top(Stack s);
extern int Size(Stack s);
extern Pset pset_union(Pset s1, Pset s2, int dontreplicate);
extern Pset create_pos();
extern void free_re();
int final_pos, pos_cnt = 0;
/* retract_token() moves the string pointer back, effectively "unseeing"
the last character seen. It is used only to retract a right paren --
the idea is that the incarnation of parse_re() that saw the corresponding
left paren is supposed to take care of matching the right paren. This
is necessary to prevent recursive calls from mistakenly eating up someone
else's right parens. */
#define retract_token(s) --(*s)
/* mk_leaf() creates a leaf node that is (usually) a literal node. */
Re_node mk_leaf(opval, type, ch, cset)
short opval, type;
char ch;
Ch_Set cset;
{
Re_node node;
Re_Lit l;
new_node(Re_Lit, l, l);
new_node(Re_node, node, node);
if (l == NULL || node == NULL) {
if (l != NULL) free(l);
if (node != NULL) free(node);
return NULL;
}
lit_type(l) = type;
lit_pos(l) = pos_cnt++;
if (type == C_SET) lit_cset(l) = cset;
else lit_char(l) = ch; /* type == C_LIT */
Op(node) = opval;
Lit(node) = l;
Nullable(node) = FALSE;
Firstpos(node) = create_pos(lit_pos(l));
Lastpos(node) = Firstpos(node);
return node;
}
/* parse_cset() takes a pointer to a pointer to a string and parses
a prefix of it denoting a character set literal. It returns a pointer
to a Re_node node, NULL if there is an error. */
Re_node parse_cset(s)
char **s;
{
Ch_Set cs_ptr, curr_ptr, prev_ptr;
char ch;
Ch_Range range = NULL;
if (Unexpected(s, ']')) return NULL;
new_node(Ch_Set, curr_ptr, curr_ptr);
cs_ptr = curr_ptr;
while (!Unexpected(s, ']')) {
new_node(Ch_Range, range, range);
curr_ptr->elt = range;
ch = NextChar(s);
if (ch == '-') {
free(range);
free(curr_ptr);
return NULL; /* invalid range */
}
range->low_bd = ch;
if (**s == NUL) {
free(range);
free(curr_ptr);
return NULL;
}
else if (**s == '-') { /* character range */
(*s)++;
if (Invalid_range(**s, ch)) {
free(range);
free(curr_ptr);
return NULL;
}
else range->hi_bd = NextChar(s);
}
else range->hi_bd = ch;
prev_ptr = curr_ptr;
new_node(Ch_Set, curr_ptr, curr_ptr);
prev_ptr->rest = curr_ptr;
};
if (**s == ']') {
prev_ptr->rest = NULL;
return mk_leaf(LITERAL, C_SET, NUL, cs_ptr);
}
else {
if (range != NULL) free(range);
free(curr_ptr);
return NULL;
}
} /* parse_cset */
/* parse_wildcard() "parses" a wildcard -- a wildcard is treated as a
character range whose values span all ASCII values. parse_wildcard()
creates a node representing such a range. */
Re_node parse_wildcard()
{
Ch_Set s;
Ch_Range r;
new_node(Ch_Range, r, r);
r->low_bd = ASCII_MIN; /* smallest ASCII value */
r->hi_bd = ASCII_MAX; /* greatest ASCII value */
new_node(Ch_Set, s, s);
s->elt = r;
s->rest = NULL;
return mk_leaf(LITERAL, C_SET, NUL, s);
}
/* parse_chlit() parses a character literal. It is assumed that the
character in question does not have any special meaning. It returns
a pointer to a node for that literal. */
Re_node parse_chlit(ch)
char ch;
{
if (ch == NUL) return NULL;
else return mk_leaf(LITERAL, C_LIT, ch, NULL);
}
/* routine to free the malloced token */
void free_tok(next_token)
Tok_node next_token;
{
if (next_token == NULL) return;
switch (tok_type(next_token)) {
case LITERAL:
free_re(tok_val(next_token));
case EOS:
case RPAREN:
case LPAREN:
case OPSTAR:
case OPALT:
case OPOPT:
default:
free(next_token);
break;
}
}
/* get_token() returns the next token -- this may be a character
literal, a character set, an escaped character, a punctuation (i.e.
parenthesis), or an operator. It traverses the character string
representing the RE, given by a pointer s; leaves s positioned
immediately after the unit it parsed, and returns a pointer to
a token node for that unit. */
Tok_node get_token(s)
char **s;
{
Tok_node rn = NULL;
if (s == NULL || *s == NULL) return NULL; /* error */
new_node(Tok_node, rn, rn);
if (**s == NUL) tok_type(rn) = EOS; /* end of string */
else {
switch (**s) {
case '.': /* wildcard */
tok_type(rn) = LITERAL;
tok_val(rn) = parse_wildcard();
if (tok_val(rn) == NULL) {
free_tok(rn);
return NULL;
}
break;
case '[': /* character set literal */
(*s)++;
tok_type(rn) = LITERAL;
tok_val(rn) = parse_cset(s);
if (tok_val(rn) == NULL) {
free_tok(rn);
return NULL;
}
break;
case '(':
tok_type(rn) = LPAREN;
break;
case ')' :
tok_type(rn) = RPAREN;
break;
case '*' :
tok_type(rn) = OPSTAR;
break;
case '|' :
tok_type(rn) = OPALT;
break;
case '?' :
tok_type(rn) = OPOPT;
break;
case '\\': /* escaped character */
(*s)++;
default : /* must be ordinary character */
tok_type(rn) = LITERAL;
tok_val(rn) = parse_chlit(**s);
if (tok_val(rn) == NULL) {
free_tok(rn);
return NULL;
}
break;
} /* switch (**s) */
(*s)++;
} /* else */
return rn;
}
/* cat2() takes a stack of RE-nodes and, if the stack contains
more than one node, returns the stack obtained by condensing
the top two nodes of the stack into a single CAT-node. If there
is only one node on the stack, nothing is done. */
Stack cat2(stk)
Stack *stk;
{
Re_node r;
if (stk == NULL) return NULL;
if (*stk == NULL || (*stk)->next == NULL) return *stk;
new_node(Re_node, r, r);
if (r == NULL) return NULL; /* can't allocate memory */
Op(r) = OPCAT;
Rchild(r) = Pop(stk);
Lchild(r) = Pop(stk);
if (Push(stk, r) == NULL) {
free_re(Rchild(r));
free_re(Lchild(r));
free(r);
return NULL;
}
Nullable(r) = Nullable(Lchild(r)) && Nullable(Rchild(r));
if (Nullable(Lchild(r)))
Firstpos(r) = pset_union(Firstpos(Lchild(r)), Firstpos(Rchild(r)), 0);
else Firstpos(r) = Firstpos(Lchild(r));
if (Nullable(Rchild(r)))
Lastpos(r) = pset_union(Lastpos(Lchild(r)), Lastpos(Rchild(r)), 0);
else Lastpos(r) = Lastpos(Rchild(r));
return *stk;
}
/* wrap() takes a stack and an operator, takes the top element of the
stack and "wraps" that operator around it, then puts this back on the
stack and returns the resulting stack. */
Stack wrap(s, opv)
Stack *s;
short opv;
{
Re_node r;
if (s == NULL || *s == NULL) return NULL;
new_node(Re_node, r, r);
if (r == NULL) return NULL;
Op(r) = opv;
Child(r) = Pop(s);
if (Push(s, r) == NULL) {
free_re(Child(r));
free(r);
return NULL;
}
Nullable(r) = TRUE;
Firstpos(r) = Firstpos(Child(r));
Lastpos(r) = Lastpos(Child(r));
return *s;
}
/* mk_alt() takes a stack and a regular expression, creates an ALT-node
from the top of the stack and the given RE, and replaces the top-of-stack
by the resulting ALT-node. */
Stack mk_alt(s, r)
Stack *s;
Re_node r;
{
Re_node node;
if (s == NULL || *s == NULL || r == NULL) return NULL;
new_node(Re_node, node, node);
if (node == NULL) return NULL;
Op(node) = OPALT;
Lchild(node) = Pop(s);
Rchild(node) = r;
if (Push(s, node) == NULL) return NULL;
Nullable(node) = Nullable(Lchild(node)) || Nullable(Rchild(node));
Firstpos(node) = pset_union(Firstpos(Lchild(node)), Firstpos(Rchild(node)), 0);
Lastpos(node) = pset_union(Lastpos(Lchild(node)), Lastpos(Rchild(node)), 0);
return *s;
}
/* parse_re() takes a pointer to a string and traverses that string,
returning a pointer to a syntax tree for the regular expression
represented by that string, NULL if there is an error. */
Re_node parse_re(s, end)
char **s;
short end;
{
Stack stk = NULL, ret = NULL, top, temp;
Tok_node next_token, t1;
Re_node re = NULL, val;
if (s == NULL || *s == NULL) return NULL;
while (TRUE) {
ret = NULL;
if ((next_token = get_token(s)) == NULL) return NULL;
switch (tok_type(next_token)) {
case RPAREN:
retract_token(s);
case EOS:
if (end == tok_type(next_token)) {
free_tok(next_token);
top = cat2(&stk);
val = Top(top);
free(top);
return val;
}
else {
free_tok(next_token);
return NULL;
}
case LPAREN:
free_tok(next_token);
re = parse_re(s, RPAREN);
if ((ret = Push(&stk, re)) == NULL) return NULL;
if ((t1 = get_token(s)) == NULL) {
free(ret);
return NULL;
}
if ((tok_type(t1) != RPAREN) || (re == NULL)) {
free(ret);
free_tok(t1);
return NULL;
}
free_tok(t1);
if (Size(stk) > 2) {
temp = stk->next;
stk->next = cat2(&temp); /* condense CAT nodes */
if (stk->next == NULL) {
free(ret);
return NULL;
}
else stk->size = stk->next->size + 1;
}
break;
case OPSTAR:
if ((ret = wrap(&stk, OPSTAR)) == NULL) {
free_tok(next_token);
return NULL;
}
break;
case OPOPT:
if ((ret = wrap(&stk, OPOPT)) == NULL) {
free_tok(next_token);
return NULL;
}
break;
case OPALT:
if ((ret = cat2(&stk)) == NULL) {
free_tok(next_token);
return NULL;
}
re = parse_re(s, end);
if (re == NULL) {
free(ret);
free_tok(next_token);
return NULL;
}
if (mk_alt(&stk, re) == NULL) {
free(ret);
free_tok(next_token);
return NULL;
}
break;
case LITERAL:
if ((ret = Push(&stk, tok_val(next_token))) == NULL) {
free_tok(next_token);
return NULL;
}
free(next_token);
if (Size(stk) > 2) {
temp = stk->next;
stk->next = cat2(&temp); /* condense CAT nodes */
if (stk->next == NULL) {
free(ret);
return NULL;
}
else stk->size = stk->next->size + 1;
}
break;
default:
printf("parse_re: unknown token type %d\n", tok_type(next_token));
break;
}
/* free_tok(next_token); */
}
}
/* parse() essentially just calls parse_re(). Its purpose is to stick an
end-of-string token at the end of the syntax tree returned by parse_re().
It should really be done in parse_re, but the recursion there makes it
more desirable to have it here. */
Re_node parse(s)
char *s;
{
Re_node val, tree, temp;
Stack top, stk = NULL;
if ((tree = parse_re(&s, NUL)) == NULL) return NULL;
if (Push(&stk, tree) == NULL) return NULL;
temp = mk_leaf(EOS, C_LIT, NUL, NULL);
if (temp == NULL || Push(&stk, temp) == NULL) return NULL;
final_pos = --pos_cnt;
top = cat2(&stk);
val = Top(top);
free(top);
return val;
}