-
Notifications
You must be signed in to change notification settings - Fork 50
/
asplit.c
397 lines (360 loc) · 11.1 KB
/
asplit.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
/* Copyright (c) 1994 Burra Gopal, Udi Manber. All Rights Reserved. */
#include "agrep.h"
#include "putils.c"
extern int checksg();
extern int D;
extern FILE *debug;
/* All borrowed from agrep.c and are needed for searching the index */
extern ParseTree aterminals[MAXNUM_PAT];
extern int AComplexBoolean;
/* returns where it found the distinguishing token:
until that from prev value of begin is the current pattern
(not just the "words" in it) */
CHAR *
aparse_flat(begin, end, prev, next)
CHAR *begin;
CHAR *end;
int prev;
int *next;
{
if (begin > end) {
*next = prev;
return end;
}
if (prev & ENDSUB_EXP) prev &= ~ATTR_EXP;
if ((prev & ATTR_EXP) && !(prev & VAL_EXP)) prev |= VAL_EXP;
while (begin <= end) {
if (*begin == ',') {
prev |= OR_EXP;
prev |= VAL_EXP;
prev |= ENDSUB_EXP;
if (prev & AND_EXP) {
fprintf(stderr, "parse error at character '%c'\n", *begin);
return NULL;
}
*next = prev;
return begin;
}
else if (*begin == ';') {
prev |= AND_EXP;
prev |= VAL_EXP;
prev |= ENDSUB_EXP;
if (prev & OR_EXP) {
fprintf(stderr, "parse error at character '%c'\n", *begin);
return NULL;
}
*next = prev;
return begin;
}
else if (*begin == '\\') begin ++; /* skip two things */
begin++;
}
*next = prev;
return begin;
}
int
asplit_pattern_flat(APattern, AM, terminals, pnum_terminals, pAParse)
CHAR *APattern;
int AM;
ParseTree terminals[MAXNUM_PAT];
int *pnum_terminals;
int *pAParse;
{
CHAR *buffer;
CHAR *buffer_pat;
CHAR *buffer_end;
buffer = APattern;
buffer_end = buffer + AM;
*pAParse = 0;
/*
* buffer is the runnning pointer, buffer_pat is the place where
* the distinguishing delimiter was found, buffer_end is the end.
*/
while (buffer_pat = (aparse_flat(buffer, buffer_end, *pAParse, pAParse)) ) {
/* there is no pattern until after the distinguishing delimiter position: some agrep garbage */
if (buffer_pat <= buffer) {
buffer = buffer_pat+1;
if (buffer_pat >= buffer_end) break;
continue;
}
if (*pnum_terminals >= MAXNUM_PAT) {
fprintf(stderr, "boolean expression has too many terms\n");
return -1;
}
terminals[*pnum_terminals].op = 0;
terminals[*pnum_terminals].type = LEAF;
terminals[*pnum_terminals].terminalindex = *pnum_terminals;
terminals[*pnum_terminals].data.leaf.attribute = 0; /* default is no structure */
terminals[*pnum_terminals].data.leaf.value = (CHAR *)malloc(buffer_pat - buffer + 2);
memcpy(terminals[*pnum_terminals].data.leaf.value, buffer, buffer_pat - buffer); /* without distinguishing delimiter */
terminals[*pnum_terminals].data.leaf.value[buffer_pat - buffer] = '\0';
(*pnum_terminals)++;
if (buffer_pat >= buffer_end) break;
buffer = buffer_pat+1;
}
if (buffer_pat == NULL) return -1; /* got out of while loop because of NULL rather than break */
return(*pnum_terminals);
}
/*
* Recursive descent; C-style => AND + OR have equal priority => must bracketize expressions appropriately or will go left->right.
* Grammar:
* E = {E} | ~a | ~{E} | E ; E | E , E | a
* Parser:
* One look ahead at each literal will tell you what to do.
* ~ has highest priority, ; and , have equal priority (left to right associativity), ~~ is not allowed.
*/
ParseTree *
aparse_tree(buffer, len, bufptr, terminals, pnum_terminals)
CHAR *buffer;
int len;
int *bufptr;
ParseTree terminals[];
int *pnum_terminals;
{
int token, tokenlen;
CHAR tokenbuf[MAXNAME];
int oldtokenlen;
CHAR oldtokenbuf[MAXNAME];
ParseTree *t, *n, *leftn;
token = get_token_bool(buffer, len, bufptr, tokenbuf, &tokenlen);
switch(token)
{
case '{': /* (exp) */
if ((t = aparse_tree(buffer, len, bufptr, terminals, pnum_terminals)) == NULL) return NULL;
if ((token = get_token_bool(buffer, len, bufptr, tokenbuf, &tokenlen)) != '}') {
fprintf(stderr, "parse error at offset %d\n", *bufptr);
destroy_tree(t);
return (NULL);
}
if ((token = get_token_bool(buffer, len, bufptr, tokenbuf, &tokenlen)) == 'e') return t;
switch(token)
{
/* must find boolean infix operator */
case ',':
case ';':
leftn = t;
if ((t = aparse_tree(buffer, len, bufptr, terminals, pnum_terminals)) == NULL) return NULL;
n = (ParseTree *)malloc(sizeof(ParseTree));
n->op = (token == ';') ? ANDPAT : ORPAT ;
n->type = INTERNAL;
n->data.internal.left = leftn;
n->data.internal.right = t;
return n;
/* or end of parent sub expression */
case '}':
unget_token_bool(bufptr, tokenlen); /* part of someone else who called me */
return t;
default:
destroy_tree(t);
fprintf(stderr, "parse error at offset %d\n", *bufptr);
return NULL;
}
/* Go one level deeper */
case '~': /* not exp */
if ((token = get_token_bool(buffer, len, bufptr, tokenbuf, &tokenlen)) == 'e') return NULL;
switch(token)
{
case 'a':
if (*pnum_terminals >= MAXNUM_PAT) {
fprintf(stderr, "Pattern expression too large (> %d)\n", MAXNUM_PAT);
return NULL;
}
n = &terminals[*pnum_terminals];
n->op = 0;
n->type = LEAF;
n->terminalindex = (*pnum_terminals);
n->data.leaf.attribute = 0;
n->data.leaf.value = (unsigned char*)malloc(tokenlen + 2);
memcpy(n->data.leaf.value, tokenbuf, tokenlen);
n->data.leaf.value[tokenlen] = '\0';
(*pnum_terminals)++;
n->op |= NOTPAT;
t = n;
break;
case '{':
if ((t = aparse_tree(buffer, len, bufptr, terminals, pnum_terminals)) == NULL) return NULL;
if (t->op & NOTPAT) t->op &= ~NOTPAT;
else t->op |= NOTPAT;
if ((token = get_token_bool(buffer, len, bufptr, tokenbuf, &tokenlen)) != '}') {
fprintf(stderr, "parse error at offset %d\n", *bufptr);
destroy_tree(t);
return NULL;
}
break;
default:
fprintf(stderr, "parse error at offset %d\n", *bufptr);
return NULL;
}
/* The resulting tree is in t. Now do another lookahead at this level */
if ((token = get_token_bool(buffer, len, bufptr, tokenbuf, &tokenlen)) == 'e') return t;
switch(token)
{
/* must find boolean infix operator */
case ',':
case ';':
leftn = t;
if ((t = aparse_tree(buffer, len, bufptr, terminals, pnum_terminals)) == NULL) return NULL;
n = (ParseTree *)malloc(sizeof(ParseTree));
n->op = (token == ';') ? ANDPAT : ORPAT ;
n->type = INTERNAL;
n->data.internal.left = leftn;
n->data.internal.right = t;
return n;
case '}':
unget_token_bool(bufptr, tokenlen);
return t;
default:
destroy_tree(t);
fprintf(stderr, "parse error at offset %d\n", *bufptr);
return NULL;
}
case 'a': /* individual term (attr=val) */
if (tokenlen == 0) return NULL;
memcpy(oldtokenbuf, tokenbuf, tokenlen);
oldtokenlen = tokenlen;
oldtokenbuf[oldtokenlen + 1] = '\0';
token = get_token_bool(buffer, len, bufptr, tokenbuf, &tokenlen);
switch(token)
{
case '}': /* part of case '{' above: else syntax error not detected but semantics ok */
unget_token_bool(bufptr, tokenlen);
case 'e': /* endof input */
case ',':
case ';':
if (*pnum_terminals >= MAXNUM_PAT) {
fprintf(stderr, "Pattern expression too large (> %d)\n", MAXNUM_PAT);
return NULL;
}
n = &terminals[*pnum_terminals];
n->op = 0;
n->type = LEAF;
n->terminalindex = (*pnum_terminals);
n->data.leaf.attribute = 0;
n->data.leaf.value = (unsigned char*)malloc(oldtokenlen + 2);
strcpy(n->data.leaf.value, oldtokenbuf);
(*pnum_terminals)++;
if ((token == 'e') || (token == '}')) return n; /* nothing after terminal in expression */
leftn = n;
if ((t = aparse_tree(buffer, len, bufptr, terminals, pnum_terminals)) == NULL) return NULL;
n = (ParseTree *)malloc(sizeof(ParseTree));
n->op = (token == ';') ? ANDPAT : ORPAT ;
n->type = INTERNAL;
n->data.internal.left = leftn;
n->data.internal.right = t;
return n;
default:
fprintf(stderr, "parse error at offset %d\n", *bufptr);
return NULL;
}
case 'e': /* can't happen as I always do a lookahead above and return current tree if e */
default:
fprintf(stderr, "parse error at offset %d\n", *bufptr);
return NULL;
}
}
int
asplit_pattern(APattern, AM, terminals, pnum_terminals, pAParse)
CHAR *APattern;
int AM;
ParseTree terminals[];
int *pnum_terminals;
ParseTree **pAParse;
{
int bufptr = 0, ret, i, j;
if (is_complex_boolean(APattern, AM)) {
AComplexBoolean = 1;
*pnum_terminals = 0;
if ((*pAParse = aparse_tree(APattern, AM, &bufptr, terminals, pnum_terminals)) == NULL)
return -1;
/* print_tree(*pAParse, 0); */
return *pnum_terminals;
}
else {
for (i=0; i<AM; i++) {
if (APattern[i] == '\\') i++;
else if ((APattern[i] == '{') || (APattern[i] == '}')) {
/* eliminate it from pattern by shifting (including '\0') since agrep must essentially do a flat search */
for (j=i; j<AM; j++)
APattern[j] = APattern[j+1];
AM --;
i--; /* to counter the ++ on top */
}
}
AComplexBoolean = 0;
*pnum_terminals = 0;
if ((ret = asplit_pattern_flat(APattern, AM, terminals, pnum_terminals, (int *)pAParse)) == -1)
return -1;
return ret;
}
}
/*
int
dd(b, e)
char *b, *e;
{
int i=0;
extern int anum_terminals;
extern char amatched_terminals[];
for(;i<anum_terminals; i++) printf("%d ", amatched_terminals[i]);
putchar(':');
while (b != e) putchar(*b++);
putchar('\n');
return 1;
}
*/
/* fast interpreter for the tree using which terminals matched: array bound checks are not done: its recursive */
int
eval_tree(tree, matched_terminals)
ParseTree *tree;
char matched_terminals[];
{
int res;
if (tree == NULL) {
fprintf(stderr, "Eval on empty tree: returning true\n");
return 1; /* safety sake, but cannot happen! */
}
if (tree->type == LEAF) return ((tree->op & NOTPAT) ? (!matched_terminals[tree->terminalindex]) : (matched_terminals[tree->terminalindex]));
else if (tree->type == INTERNAL) {
if ((tree->op & OPMASK) == ANDPAT) { /* sequential evaluation */
if ((res = eval_tree(tree->data.internal.left, matched_terminals)) != 0) res = eval_tree(tree->data.internal.right, matched_terminals);
return (tree->op & NOTPAT) ? !res : res;
}
else { /* sequential evaluation */
if ((res = eval_tree(tree->data.internal.left, matched_terminals)) == 0) res = eval_tree(tree->data.internal.right, matched_terminals);
return (tree->op & NOTPAT) ? !res : res;
}
}
else {
fprintf(stderr, "Eval on bad tree: returning false\n");
return 0; /* safety sake, but cannot happen! */
}
}
/* [first, last) = C-style range for which we want the words in terminal-values' patterns: 0..num_terminals for !ComplexBoolean, term/term otherwise */
int
asplit_terminal(first, last, pat_buf, pat_ptr)
int first, last;
char *pat_buf;
int *pat_ptr;
{
int word_length;
int type;
int num_pat;
*pat_ptr = 0;
num_pat = 0;
for (; first<last; first++) {
word_length = strlen(aterminals[first].data.leaf.value);
if (word_length <= 0) continue;
if ((type = checksg(aterminals[first].data.leaf.value, D, 0)) == -1) return -1;
if (!type) return -1;
strcpy(&pat_buf[*pat_ptr], aterminals[first].data.leaf.value);
pat_buf[*pat_ptr + word_length] = '\n';
pat_buf[*pat_ptr + word_length + 1] = '\0';
*pat_ptr += (word_length + 1);
num_pat ++;
if(num_pat >= MAXNUM_PAT) {
fprintf(stderr, "Warning: too many words in pattern (> %d): ignoring...\n", MAXNUM_PAT);
break;
}
}
return num_pat;
}