1
/* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal. All Rights Reserved. */
1
2
/* the functions in this file take a syntax tree for a regular
2
3
expression and produce a DFA using the McNaughton-Yamada
8
extern char *strncpy(), *strcat(), *strcpy();
13
extern char *malloc();
14
14
extern Pset pset_union();
15
15
extern int pos_cnt;
16
16
extern Re_node parse();
21
21
/* extend_re() extends the RE by adding a ".*(" at the front and a "("
29
s1 = malloc((unsigned) strlen(s)+4+1);
30
return strcat(strcat(strcpy(s1, ".*("), s), ")");
29
s1 = malloc((unsigned) strlen(s)+4+1);
30
return strcat(strcat(strcpy(s1, ".*("), s), ")");
33
void free_pos(fpos, pos_cnt)
40
if ((fpos == NULL) || (*fpos == NULL)) return;
41
for (i=0; i<=pos_cnt; i++) {
52
/* Function to clear out a Ch_Set */
58
while (cset != NULL) {
66
/* Function to clear out the tree of re-nodes */
70
if (e == NULL) return;
73
* Was creating "reading freed memory", "freeing unallocated/freed memory"
74
* errors. So abandoned it. Leaks are now up by 60B/call to 80B/call
76
* Enabled on 26/Aug/1996 after changing pos routines to copy stuff rather than link up parents/children/etc.
82
if ((Lastpos(e)) != (Firstpos(e))) tofree = 1;
102
Enabled on 26/Aug/1996 after changing pos routines to copy stuff rather than link up parents/children/etc.
107
if (lit_type(Lit(e)) == C_SET) free_cset(lit_cset(Lit(e)));
125
if (lit_type(Lit(e)) == C_SET) free_cset(lit_cset(Lit(e)));
129
fprintf(stderr, "free_re: unknown node type %d\n", Op(e));
33
136
/* mk_followpos() takes a syntax tree for a regular expression and
34
137
traverses it once, computing the followpos function at each node
52
(*fpos)[i] = pset_union(Firstpos(e), (*fpos)[i]);
55
mk_followpos_1(Child(e), fpos);
154
while (pos != NULL) {
156
(*fpos)[i] = pset_union(Firstpos(e), (*fpos)[i], 1);
159
mk_followpos_1(Child(e), fpos);
58
pos = Lastpos(Lchild(e));
61
(*fpos)[i] = pset_union(Firstpos(Rchild(e)), (*fpos)[i]);
64
mk_followpos_1(Lchild(e), fpos);
65
mk_followpos_1(Rchild(e), fpos);
162
pos = Lastpos(Lchild(e));
163
while (pos != NULL) {
165
(*fpos)[i] = pset_union(Firstpos(Rchild(e)), (*fpos)[i], 1);
168
mk_followpos_1(Lchild(e), fpos);
169
mk_followpos_1(Rchild(e), fpos);
68
mk_followpos_1(Child(e), fpos);
172
mk_followpos_1(Child(e), fpos);
71
mk_followpos_1(Lchild(e), fpos);
72
mk_followpos_1(Rchild(e), fpos);
175
mk_followpos_1(Lchild(e), fpos);
176
mk_followpos_1(Rchild(e), fpos);
76
default: printf("mk_followpos: unknown node type %d\n", Op(e));
181
fprintf(stderr, "mk_followpos: unknown node type %d\n", Op(e));
81
186
Pset_array mk_followpos(tree, npos)
88
if (tree == NULL || npos < 0) return NULL;
89
fpos = (Pset_array) malloc((unsigned) (npos+1)*sizeof(Pset));
90
if (fpos == NULL) return NULL;
91
for (i = 0; i <= npos; i++) (*fpos)[i] = NULL;
92
mk_followpos_1(tree, fpos);
193
if (tree == NULL || npos < 0) return NULL;
194
fpos = (Pset_array) malloc((unsigned) (npos+1)*sizeof(Pset));
195
if (fpos == NULL) return NULL;
196
for (i = 0; i <= npos; i++) (*fpos)[i] = NULL;
197
mk_followpos_1(tree, fpos);
96
201
/* mk_poslist() sets a static array whose i_th element is a pointer to
101
206
0 if everything goes OK. */
103
208
int init(s, table)
104
char *s; int table[32][32];
111
if ((e = parse(extend_re(s))) == NULL) return -1;
112
if ((fpos = mk_followpos(e, pos_cnt)) == NULL) return -1;
113
for (i = 0; i <= pos_cnt; i += 1) {
115
printf("followpos[%d] = ", i);
119
for ( ; l != NULL; l = l->nextpos) {
121
printf("%d ", l->posnum);
123
table[i][j] = l->posnum;
131
for (i=0; i <= pos_cnt; i += 1) {
133
while (table[i][j] != 0) {
134
printf(" %d ", table[i][j]);
218
if ((s1 = extend_re(s)) == NULL) return -1;
219
if ((e = parse(s1)) == NULL) {
224
if ((fpos = mk_followpos(e, pos_cnt)) == NULL) {
228
for (i = 0; i <= pos_cnt; i += 1) {
230
printf("followpos[%d] = ", i);
234
for ( ; l != NULL; l = l->nextpos) {
236
printf("%d ", l->posnum);
238
table[i][j] = l->posnum;
246
for (i=0; i <= pos_cnt; i += 1) {
248
while (table[i][j] != 0) {
249
printf(" %d ", table[i][j]);
255
free_pos(fpos, pos_cnt);