File: follow.c

package info (click to toggle)
glimpse 4.1-2
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 2,344 kB
  • ctags: 2,254
  • sloc: ansic: 32,194; makefile: 561; sh: 170; perl: 142
file content (259 lines) | stat: -rw-r--r-- 5,055 bytes parent folder | download | duplicates (12)
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
/* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal.  All Rights Reserved. */
/* the functions in this file take a syntax tree for a regular
   expression and produce a DFA using the McNaughton-Yamada
   construction.						*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "re.h"

#define TRUE	1

extern Pset pset_union(); 
extern int pos_cnt;
extern Re_node parse();

Re_lit_array lpos; 


/*  extend_re() extends the RE by adding a ".*(" at the front and a "("
	at the back.						   	*/

char *extend_re(s)
char *s;
{
	char *s1;

	s1 = malloc((unsigned) strlen(s)+4+1);
	return strcat(strcat(strcpy(s1, ".*("), s), ")");
}

void free_pos(fpos, pos_cnt)
Pset_array fpos;
int pos_cnt;
{
	Pset tpos, pos;
	int i;

	if ((fpos == NULL) || (*fpos == NULL)) return;
	for (i=0; i<=pos_cnt; i++) {
		pos = (*fpos)[i];
		while (pos != NULL) {
			tpos = pos;
			pos = pos->nextpos;
			free(tpos);
		}
	}
	free(fpos);
}

/* Function to clear out a Ch_Set */
void free_cset(cset)
Ch_Set cset;
{
	Ch_Set tset;

	while (cset != NULL) {
		tset = cset;
		cset = cset->rest;
		free(tset->elt);
		free(tset);
	}
}

/* Function to clear out the tree of re-nodes */
void free_re(e)
Re_node e;
{
	if (e == NULL) return;

/*
 * Was creating "reading freed memory", "freeing unallocated/freed memory"
 * errors. So abandoned it. Leaks are now up by 60B/call to 80B/call
 * -bg
 * Enabled on 26/Aug/1996 after changing pos routines to copy stuff rather than link up parents/children/etc.
 */
	{
	Pset tpos, pos;
	int tofree = 0;

	if ((Lastpos(e)) != (Firstpos(e))) tofree = 1;
	pos = Lastpos(e);
	while (pos != NULL) {
		tpos = pos;
		pos = pos->nextpos;
		free(tpos);
	}
	Lastpos(e) = NULL;

	if (tofree) {
		pos = Firstpos(e);
		while (pos != NULL) {
			tpos = pos;
			pos = pos->nextpos;
			free(tpos);
		}
		Firstpos(e) = NULL;
	}
	}
/*
 Enabled on 26/Aug/1996 after changing pos routines to copy stuff rather than link up parents/children/etc.
 */

	switch (Op(e)) {
	case EOS:
		if (lit_type(Lit(e)) == C_SET) free_cset(lit_cset(Lit(e)));
		free(Lit(e));
		break;
	case OPSTAR:
		free_re(Child(e));
		break;
	case OPCAT:
		free_re(Lchild(e));
		free_re(Rchild(e));
		break;
	case OPOPT:
		free_re(Child(e));
		break;
	case OPALT:
		free_re(Lchild(e));
		free_re(Rchild(e));
		break;
	case LITERAL:
		if (lit_type(Lit(e)) == C_SET) free_cset(lit_cset(Lit(e)));
		free(Lit(e));
		break;
	default: 
		fprintf(stderr, "free_re: unknown node type %d\n", Op(e));
	}
	free(e);
	return;
}


/* mk_followpos() takes a syntax tree for a regular expression and
   traverses it once, computing the followpos function at each node
   and returns a pointer to an array whose ith element is a pointer
   to a list of position nodes, representing the positions in
   followpos(i).							*/

void mk_followpos_1(e, fpos)
Re_node e;
Pset_array fpos;
{
	Pset pos;
	int i;

	switch (Op(e)) {
	case EOS: 
		break;
	case OPSTAR:
		pos = Lastpos(e);
		while (pos != NULL) {
			i = pos->posnum;
			(*fpos)[i] = pset_union(Firstpos(e), (*fpos)[i], 1);
			pos = pos->nextpos;
		}
		mk_followpos_1(Child(e), fpos);
		break;
	case OPCAT:
		pos = Lastpos(Lchild(e));
		while (pos != NULL) {
			i = pos->posnum;
			(*fpos)[i] = pset_union(Firstpos(Rchild(e)), (*fpos)[i], 1);
			pos = pos->nextpos;
		}
		mk_followpos_1(Lchild(e), fpos);
		mk_followpos_1(Rchild(e), fpos);
		break;
	case OPOPT:
		mk_followpos_1(Child(e), fpos);
		break;
	case OPALT:
		mk_followpos_1(Lchild(e), fpos);
		mk_followpos_1(Rchild(e), fpos);
		break;
	case LITERAL:
		break;
	default: 
		fprintf(stderr, "mk_followpos: unknown node type %d\n", Op(e));
	}
	return;
}

Pset_array mk_followpos(tree, npos)
Re_node tree;
int npos;
{
	int i;
	Pset_array fpos;

	if (tree == NULL || npos < 0) return NULL;
	fpos = (Pset_array) malloc((unsigned) (npos+1)*sizeof(Pset));
	if (fpos == NULL) return NULL;
	for (i = 0; i <= npos; i++) (*fpos)[i] = NULL;
	mk_followpos_1(tree, fpos);
	return fpos;
}

/* mk_poslist() sets a static array whose i_th element is a pointer to
   the RE-literal at position i.  It returns 1 if everything is OK,  0
   otherwise.								*/

/* init performs initialization actions; it returns -1 in case of error,
   0 if everything goes OK.						*/

int init(s, table)
char *s; 
int table[32][32];
{
	Pset_array fpos;
	Re_node e;
	Pset l;
	int i, j;
	char *s1;

	if ((s1 = extend_re(s)) == NULL) return -1;
	if ((e = parse(s1)) == NULL) {
		free(s1);
		return -1;
	}
	free(s1);
	if ((fpos = mk_followpos(e, pos_cnt)) == NULL) {
		free_re(e);
		return -1;
	}
	for (i = 0; i <= pos_cnt; i += 1) {
#ifdef Debug
		printf("followpos[%d] = ", i);
#endif
		l = (*fpos)[i];
		j = 0;
		for ( ; l != NULL; l = l->nextpos)  {
#ifdef Debug
			printf("%d ", l->posnum);
#endif
			table[i][j] = l->posnum;
			j++;
		} 
#ifdef Debug
		printf("\n");
#endif
	}
#ifdef Debug
	for (i=0; i <= pos_cnt; i += 1)  {
		j = 0;
		while (table[i][j] != 0) {
			printf(" %d ", table[i][j]);
			j++;
		}
		printf("\n");
	}
#endif
	free_pos(fpos, pos_cnt);
	free_re(e);
	return (pos_cnt);
}