Commit | Line | Data |
---|---|---|
e804469b C |
1 | # define CHAR 257 |
2 | # define DOT 258 | |
3 | # define CCL 259 | |
4 | # define NCCL 260 | |
5 | # define OR 261 | |
6 | # define CAT 262 | |
7 | # define STAR 263 | |
8 | # define PLUS 264 | |
9 | # define QUEST 265 | |
10 | ||
11 | # line 16 "egrep.y" | |
12 | static char *sccsid = "@(#)egrep.y 4.2 (Berkeley) 11/8/82"; | |
17377d12 BJ |
13 | #include <stdio.h> |
14 | ||
15 | #define MAXLIN 350 | |
16 | #define MAXPOS 4000 | |
17 | #define NCHARS 128 | |
18 | #define NSTATES 128 | |
19 | #define FINAL -1 | |
20 | char gotofn[NSTATES][NCHARS]; | |
21 | int state[NSTATES]; | |
22 | char out[NSTATES]; | |
23 | int line = 1; | |
24 | int name[MAXLIN]; | |
25 | int left[MAXLIN]; | |
26 | int right[MAXLIN]; | |
27 | int parent[MAXLIN]; | |
28 | int foll[MAXLIN]; | |
29 | int positions[MAXPOS]; | |
30 | char chars[MAXLIN]; | |
31 | int nxtpos; | |
32 | int nxtchar = 0; | |
33 | int tmpstat[MAXLIN]; | |
34 | int initstat[MAXLIN]; | |
35 | int xstate; | |
36 | int count; | |
37 | int icount; | |
38 | char *input; | |
fcd2fafb | 39 | FILE *exprfile; |
17377d12 BJ |
40 | |
41 | long lnum; | |
42 | int bflag; | |
43 | int cflag; | |
44 | int fflag; | |
45 | int lflag; | |
46 | int nflag; | |
47 | int hflag = 1; | |
48 | int sflag; | |
49 | int vflag; | |
50 | int nfile; | |
51 | int blkno; | |
52 | long tln; | |
53 | int nsucc; | |
54 | ||
55 | int f; | |
fcd2fafb | 56 | char *fname; |
e804469b C |
57 | #define yyclearin yychar = -1 |
58 | #define yyerrok yyerrflag = 0 | |
59 | extern int yychar; | |
60 | extern short yyerrflag; | |
61 | #ifndef YYMAXDEPTH | |
62 | #define YYMAXDEPTH 150 | |
63 | #endif | |
64 | #ifndef YYSTYPE | |
65 | #define YYSTYPE int | |
66 | #endif | |
67 | YYSTYPE yylval, yyval; | |
68 | # define YYERRCODE 256 | |
69 | ||
70 | # line 107 "egrep.y" | |
17377d12 | 71 | |
17377d12 BJ |
72 | yyerror(s) { |
73 | fprintf(stderr, "egrep: %s\n", s); | |
74 | exit(2); | |
75 | } | |
76 | ||
77 | yylex() { | |
78 | extern int yylval; | |
79 | int cclcnt, x; | |
80 | register char c, d; | |
81 | switch(c = nextch()) { | |
82 | case '$': | |
83 | case '^': c = '\n'; | |
84 | goto defchar; | |
85 | case '|': return (OR); | |
86 | case '*': return (STAR); | |
87 | case '+': return (PLUS); | |
88 | case '?': return (QUEST); | |
89 | case '(': return (c); | |
90 | case ')': return (c); | |
91 | case '.': return (DOT); | |
92 | case '\0': return (0); | |
93 | case '\n': return (OR); | |
94 | case '[': | |
95 | x = CCL; | |
96 | cclcnt = 0; | |
97 | count = nxtchar++; | |
98 | if ((c = nextch()) == '^') { | |
99 | x = NCCL; | |
100 | c = nextch(); | |
101 | } | |
102 | do { | |
103 | if (c == '\0') synerror(); | |
104 | if (c == '-' && cclcnt > 0 && chars[nxtchar-1] != 0) { | |
105 | if ((d = nextch()) != 0) { | |
106 | c = chars[nxtchar-1]; | |
107 | while (c < d) { | |
108 | if (nxtchar >= MAXLIN) overflo(); | |
109 | chars[nxtchar++] = ++c; | |
110 | cclcnt++; | |
111 | } | |
112 | continue; | |
113 | } | |
114 | } | |
115 | if (nxtchar >= MAXLIN) overflo(); | |
116 | chars[nxtchar++] = c; | |
117 | cclcnt++; | |
118 | } while ((c = nextch()) != ']'); | |
119 | chars[count] = cclcnt; | |
120 | return (x); | |
121 | case '\\': | |
122 | if ((c = nextch()) == '\0') synerror(); | |
123 | defchar: | |
124 | default: yylval = c; return (CHAR); | |
125 | } | |
126 | } | |
127 | nextch() { | |
128 | register char c; | |
129 | if (fflag) { | |
fcd2fafb RH |
130 | if ((c = getc(exprfile)) == EOF) { |
131 | fclose(exprfile); | |
132 | return(0); | |
133 | } | |
17377d12 BJ |
134 | } |
135 | else c = *input++; | |
136 | return(c); | |
137 | } | |
138 | ||
139 | synerror() { | |
140 | fprintf(stderr, "egrep: syntax error\n"); | |
141 | exit(2); | |
142 | } | |
143 | ||
144 | enter(x) int x; { | |
145 | if(line >= MAXLIN) overflo(); | |
146 | name[line] = x; | |
147 | left[line] = 0; | |
148 | right[line] = 0; | |
149 | return(line++); | |
150 | } | |
151 | ||
152 | cclenter(x) int x; { | |
153 | register linno; | |
154 | linno = enter(x); | |
155 | right[linno] = count; | |
156 | return (linno); | |
157 | } | |
158 | ||
159 | node(x, l, r) { | |
160 | if(line >= MAXLIN) overflo(); | |
161 | name[line] = x; | |
162 | left[line] = l; | |
163 | right[line] = r; | |
164 | parent[l] = line; | |
165 | parent[r] = line; | |
166 | return(line++); | |
167 | } | |
168 | ||
169 | unary(x, d) { | |
170 | if(line >= MAXLIN) overflo(); | |
171 | name[line] = x; | |
172 | left[line] = d; | |
173 | right[line] = 0; | |
174 | parent[d] = line; | |
175 | return(line++); | |
176 | } | |
177 | overflo() { | |
178 | fprintf(stderr, "egrep: regular expression too long\n"); | |
179 | exit(2); | |
180 | } | |
181 | ||
182 | cfoll(v) { | |
183 | register i; | |
184 | if (left[v] == 0) { | |
185 | count = 0; | |
186 | for (i=1; i<=line; i++) tmpstat[i] = 0; | |
187 | follow(v); | |
188 | add(foll, v); | |
189 | } | |
190 | else if (right[v] == 0) cfoll(left[v]); | |
191 | else { | |
192 | cfoll(left[v]); | |
193 | cfoll(right[v]); | |
194 | } | |
195 | } | |
196 | cgotofn() { | |
197 | register c, i, k; | |
198 | int n, s; | |
199 | char symbol[NCHARS]; | |
200 | int j, nc, pc, pos; | |
201 | int curpos, num; | |
202 | int number, newpos; | |
203 | count = 0; | |
204 | for (n=3; n<=line; n++) tmpstat[n] = 0; | |
205 | if (cstate(line-1)==0) { | |
206 | tmpstat[line] = 1; | |
207 | count++; | |
208 | out[0] = 1; | |
209 | } | |
210 | for (n=3; n<=line; n++) initstat[n] = tmpstat[n]; | |
211 | count--; /*leave out position 1 */ | |
212 | icount = count; | |
213 | tmpstat[1] = 0; | |
214 | add(state, 0); | |
215 | n = 0; | |
216 | for (s=0; s<=n; s++) { | |
217 | if (out[s] == 1) continue; | |
218 | for (i=0; i<NCHARS; i++) symbol[i] = 0; | |
219 | num = positions[state[s]]; | |
220 | count = icount; | |
221 | for (i=3; i<=line; i++) tmpstat[i] = initstat[i]; | |
222 | pos = state[s] + 1; | |
223 | for (i=0; i<num; i++) { | |
224 | curpos = positions[pos]; | |
225 | if ((c = name[curpos]) >= 0) { | |
226 | if (c < NCHARS) symbol[c] = 1; | |
227 | else if (c == DOT) { | |
228 | for (k=0; k<NCHARS; k++) | |
229 | if (k!='\n') symbol[k] = 1; | |
230 | } | |
231 | else if (c == CCL) { | |
232 | nc = chars[right[curpos]]; | |
233 | pc = right[curpos] + 1; | |
234 | for (k=0; k<nc; k++) symbol[chars[pc++]] = 1; | |
235 | } | |
236 | else if (c == NCCL) { | |
237 | nc = chars[right[curpos]]; | |
238 | for (j = 0; j < NCHARS; j++) { | |
239 | pc = right[curpos] + 1; | |
240 | for (k = 0; k < nc; k++) | |
241 | if (j==chars[pc++]) goto cont; | |
242 | if (j!='\n') symbol[j] = 1; | |
243 | cont:; | |
244 | } | |
245 | } | |
246 | else printf("something's funny\n"); | |
247 | } | |
248 | pos++; | |
249 | } | |
250 | for (c=0; c<NCHARS; c++) { | |
251 | if (symbol[c] == 1) { /* nextstate(s,c) */ | |
252 | count = icount; | |
253 | for (i=3; i <= line; i++) tmpstat[i] = initstat[i]; | |
254 | pos = state[s] + 1; | |
255 | for (i=0; i<num; i++) { | |
256 | curpos = positions[pos]; | |
257 | if ((k = name[curpos]) >= 0) | |
258 | if ( | |
259 | (k == c) | |
260 | | (k == DOT) | |
261 | | (k == CCL && member(c, right[curpos], 1)) | |
262 | | (k == NCCL && member(c, right[curpos], 0)) | |
263 | ) { | |
264 | number = positions[foll[curpos]]; | |
265 | newpos = foll[curpos] + 1; | |
266 | for (k=0; k<number; k++) { | |
267 | if (tmpstat[positions[newpos]] != 1) { | |
268 | tmpstat[positions[newpos]] = 1; | |
269 | count++; | |
270 | } | |
271 | newpos++; | |
272 | } | |
273 | } | |
274 | pos++; | |
275 | } /* end nextstate */ | |
276 | if (notin(n)) { | |
277 | if (n >= NSTATES) overflo(); | |
278 | add(state, ++n); | |
279 | if (tmpstat[line] == 1) out[n] = 1; | |
280 | gotofn[s][c] = n; | |
281 | } | |
282 | else { | |
283 | gotofn[s][c] = xstate; | |
284 | } | |
285 | } | |
286 | } | |
287 | } | |
288 | } | |
289 | ||
290 | cstate(v) { | |
291 | register b; | |
292 | if (left[v] == 0) { | |
293 | if (tmpstat[v] != 1) { | |
294 | tmpstat[v] = 1; | |
295 | count++; | |
296 | } | |
297 | return(1); | |
298 | } | |
299 | else if (right[v] == 0) { | |
300 | if (cstate(left[v]) == 0) return (0); | |
301 | else if (name[v] == PLUS) return (1); | |
302 | else return (0); | |
303 | } | |
304 | else if (name[v] == CAT) { | |
305 | if (cstate(left[v]) == 0 && cstate(right[v]) == 0) return (0); | |
306 | else return (1); | |
307 | } | |
308 | else { /* name[v] == OR */ | |
309 | b = cstate(right[v]); | |
310 | if (cstate(left[v]) == 0 || b == 0) return (0); | |
311 | else return (1); | |
312 | } | |
313 | } | |
314 | ||
315 | ||
316 | member(symb, set, torf) { | |
317 | register i, num, pos; | |
318 | num = chars[set]; | |
319 | pos = set + 1; | |
320 | for (i=0; i<num; i++) | |
321 | if (symb == chars[pos++]) return (torf); | |
322 | return (!torf); | |
323 | } | |
324 | ||
325 | notin(n) { | |
326 | register i, j, pos; | |
327 | for (i=0; i<=n; i++) { | |
328 | if (positions[state[i]] == count) { | |
329 | pos = state[i] + 1; | |
330 | for (j=0; j < count; j++) | |
331 | if (tmpstat[positions[pos++]] != 1) goto nxt; | |
332 | xstate = i; | |
333 | return (0); | |
334 | } | |
335 | nxt: ; | |
336 | } | |
337 | return (1); | |
338 | } | |
339 | ||
340 | add(array, n) int *array; { | |
341 | register i; | |
342 | if (nxtpos + count > MAXPOS) overflo(); | |
343 | array[n] = nxtpos; | |
344 | positions[nxtpos++] = count; | |
345 | for (i=3; i <= line; i++) { | |
346 | if (tmpstat[i] == 1) { | |
347 | positions[nxtpos++] = i; | |
348 | } | |
349 | } | |
350 | } | |
351 | ||
352 | follow(v) int v; { | |
353 | int p; | |
354 | if (v == line) return; | |
355 | p = parent[v]; | |
356 | switch(name[p]) { | |
357 | case STAR: | |
358 | case PLUS: cstate(v); | |
359 | follow(p); | |
360 | return; | |
361 | ||
362 | case OR: | |
363 | case QUEST: follow(p); | |
364 | return; | |
365 | ||
366 | case CAT: if (v == left[p]) { | |
367 | if (cstate(right[p]) == 0) { | |
368 | follow(p); | |
369 | return; | |
370 | } | |
371 | } | |
372 | else follow(p); | |
373 | return; | |
374 | case FINAL: if (tmpstat[line] != 1) { | |
375 | tmpstat[line] = 1; | |
376 | count++; | |
377 | } | |
378 | return; | |
379 | } | |
380 | } | |
381 | ||
382 | ||
383 | main(argc, argv) | |
384 | char **argv; | |
385 | { | |
386 | while (--argc > 0 && (++argv)[0][0]=='-') | |
387 | switch (argv[0][1]) { | |
388 | ||
389 | case 's': | |
390 | sflag++; | |
391 | continue; | |
392 | ||
393 | case 'h': | |
394 | hflag = 0; | |
395 | continue; | |
396 | ||
397 | case 'b': | |
398 | bflag++; | |
399 | continue; | |
400 | ||
401 | case 'c': | |
402 | cflag++; | |
403 | continue; | |
404 | ||
405 | case 'e': | |
406 | argc--; | |
407 | argv++; | |
408 | goto out; | |
409 | ||
410 | case 'f': | |
411 | fflag++; | |
412 | continue; | |
413 | ||
414 | case 'l': | |
415 | lflag++; | |
416 | continue; | |
417 | ||
418 | case 'n': | |
419 | nflag++; | |
420 | continue; | |
421 | ||
422 | case 'v': | |
423 | vflag++; | |
424 | continue; | |
425 | ||
426 | default: | |
427 | fprintf(stderr, "egrep: unknown flag\n"); | |
428 | continue; | |
429 | } | |
430 | out: | |
431 | if (argc<=0) | |
432 | exit(2); | |
433 | if (fflag) { | |
fcd2fafb RH |
434 | fname = *argv; |
435 | exprfile = fopen(fname, "r"); | |
436 | if (exprfile == (FILE *)NULL) { | |
17377d12 BJ |
437 | fprintf(stderr, "egrep: can't open %s\n", fname); |
438 | exit(2); | |
439 | } | |
440 | } | |
441 | else input = *argv; | |
442 | argc--; | |
443 | argv++; | |
444 | ||
445 | yyparse(); | |
446 | ||
447 | cfoll(line-1); | |
448 | cgotofn(); | |
449 | nfile = argc; | |
450 | if (argc<=0) { | |
451 | if (lflag) exit(1); | |
452 | execute(0); | |
453 | } | |
454 | else while (--argc >= 0) { | |
455 | execute(*argv); | |
456 | argv++; | |
457 | } | |
458 | exit(nsucc == 0); | |
459 | } | |
460 | ||
461 | execute(file) | |
462 | char *file; | |
463 | { | |
464 | register char *p; | |
465 | register cstat; | |
466 | register ccount; | |
467 | char buf[1024]; | |
468 | char *nlp; | |
469 | int istat; | |
470 | if (file) { | |
471 | if ((f = open(file, 0)) < 0) { | |
472 | fprintf(stderr, "egrep: can't open %s\n", file); | |
473 | exit(2); | |
474 | } | |
475 | } | |
476 | else f = 0; | |
477 | ccount = 0; | |
478 | lnum = 1; | |
479 | tln = 0; | |
480 | blkno = 0; | |
481 | p = buf; | |
482 | nlp = p; | |
483 | if ((ccount = read(f,p,512))<=0) goto done; | |
484 | istat = cstat = gotofn[0]['\n']; | |
485 | if (out[cstat]) goto found; | |
486 | for (;;) { | |
487 | cstat = gotofn[cstat][*p&0377]; /* all input chars made positive */ | |
488 | if (out[cstat]) { | |
489 | found: for(;;) { | |
490 | if (*p++ == '\n') { | |
491 | if (vflag == 0) { | |
492 | succeed: nsucc = 1; | |
493 | if (cflag) tln++; | |
494 | else if (sflag) | |
495 | ; /* ugh */ | |
496 | else if (lflag) { | |
497 | printf("%s\n", file); | |
498 | close(f); | |
499 | return; | |
500 | } | |
501 | else { | |
502 | if (nfile > 1 && hflag) printf("%s:", file); | |
503 | if (bflag) printf("%d:", blkno); | |
504 | if (nflag) printf("%ld:", lnum); | |
505 | if (p <= nlp) { | |
506 | while (nlp < &buf[1024]) putchar(*nlp++); | |
507 | nlp = buf; | |
508 | } | |
509 | while (nlp < p) putchar(*nlp++); | |
510 | } | |
511 | } | |
512 | lnum++; | |
513 | nlp = p; | |
514 | if ((out[(cstat=istat)]) == 0) goto brk2; | |
515 | } | |
516 | cfound: | |
517 | if (--ccount <= 0) { | |
518 | if (p <= &buf[512]) { | |
519 | if ((ccount = read(f, p, 512)) <= 0) goto done; | |
520 | } | |
521 | else if (p == &buf[1024]) { | |
522 | p = buf; | |
523 | if ((ccount = read(f, p, 512)) <= 0) goto done; | |
524 | } | |
525 | else { | |
526 | if ((ccount = read(f, p, &buf[1024]-p)) <= 0) goto done; | |
527 | } | |
528 | blkno++; | |
529 | } | |
530 | } | |
531 | } | |
532 | if (*p++ == '\n') { | |
533 | if (vflag) goto succeed; | |
534 | else { | |
535 | lnum++; | |
536 | nlp = p; | |
537 | if (out[(cstat=istat)]) goto cfound; | |
538 | } | |
539 | } | |
540 | brk2: | |
541 | if (--ccount <= 0) { | |
542 | if (p <= &buf[512]) { | |
543 | if ((ccount = read(f, p, 512)) <= 0) break; | |
544 | } | |
545 | else if (p == &buf[1024]) { | |
546 | p = buf; | |
547 | if ((ccount = read(f, p, 512)) <= 0) break; | |
548 | } | |
549 | else { | |
550 | if ((ccount = read(f, p, &buf[1024] - p)) <= 0) break; | |
551 | } | |
552 | blkno++; | |
553 | } | |
554 | } | |
555 | done: close(f); | |
556 | if (cflag) { | |
557 | if (nfile > 1) | |
558 | printf("%s:", file); | |
559 | printf("%ld\n", tln); | |
560 | } | |
561 | } | |
e804469b C |
562 | short yyexca[] ={ |
563 | -1, 1, | |
564 | 0, -1, | |
565 | -2, 0, | |
566 | }; | |
567 | # define YYNPROD 18 | |
568 | # define YYLAST 261 | |
569 | short yyact[]={ | |
570 | ||
571 | 10, 22, 4, 14, 11, 2, 1, 5, 0, 0, | |
572 | 10, 15, 16, 17, 18, 0, 19, 20, 3, 0, | |
573 | 10, 0, 0, 12, 0, 20, 0, 20, 0, 0, | |
574 | 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
575 | 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
576 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
577 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
578 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
579 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
580 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
581 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
582 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
583 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
584 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
585 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
586 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
587 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
588 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
589 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
590 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
591 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
592 | 0, 0, 0, 0, 0, 0, 11, 6, 7, 8, | |
593 | 9, 21, 0, 15, 16, 17, 11, 6, 7, 8, | |
594 | 9, 23, 0, 15, 16, 17, 11, 6, 7, 8, | |
595 | 9, 13, 0, 15, 16, 17, 11, 6, 7, 8, | |
596 | 9, 0, 0, 15, 16, 17, 11, 6, 7, 8, | |
597 | 9 }; | |
598 | short yypact[]={ | |
599 | ||
600 | -259,-1000,-1000, 0,-1000, -20,-1000,-1000,-1000,-1000, | |
601 | 0,-1000, 0, 0,-252,-1000,-1000,-1000, -40, -30, | |
602 | -10, 0,-1000, 0 }; | |
603 | short yypgo[]={ | |
604 | ||
605 | 0, 6, 5, 18, 3 }; | |
606 | short yyr1[]={ | |
607 | ||
608 | 0, 1, 2, 2, 2, 2, 3, 4, 4, 4, | |
609 | 4, 4, 4, 4, 4, 4, 4, 4 }; | |
610 | short yyr2[]={ | |
611 | ||
612 | 0, 1, 2, 4, 3, 3, 0, 1, 1, 1, | |
613 | 1, 3, 2, 2, 2, 2, 3, 1 }; | |
614 | short yychk[]={ | |
615 | ||
616 | -1000, -1, -2, -3, 261, -4, 257, 258, 259, 260, | |
617 | 40, 256, -3, 261, -4, 263, 264, 265, -4, -4, | |
618 | -4, 261, 41, 261 }; | |
619 | short yydef[]={ | |
620 | ||
621 | 6, -2, 1, 0, 6, 2, 7, 8, 9, 10, | |
622 | 0, 17, 0, 5, 12, 13, 14, 15, 0, 4, | |
623 | 11, 0, 16, 3 }; | |
624 | # | |
625 | # define YYFLAG -1000 | |
626 | # define YYERROR goto yyerrlab | |
627 | # define YYACCEPT return(0) | |
628 | # define YYABORT return(1) | |
629 | ||
630 | /* parser for yacc output */ | |
631 | ||
632 | #ifdef YYDEBUG | |
633 | int yydebug = 0; /* 1 for debugging */ | |
634 | #endif | |
635 | YYSTYPE yyv[YYMAXDEPTH]; /* where the values are stored */ | |
636 | int yychar = -1; /* current input token number */ | |
637 | int yynerrs = 0; /* number of errors */ | |
638 | short yyerrflag = 0; /* error recovery flag */ | |
639 | ||
640 | yyparse() { | |
641 | ||
642 | short yys[YYMAXDEPTH]; | |
643 | short yyj, yym; | |
644 | register YYSTYPE *yypvt; | |
645 | register short yystate, *yyps, yyn; | |
646 | register YYSTYPE *yypv; | |
647 | register short *yyxi; | |
648 | ||
649 | yystate = 0; | |
650 | yychar = -1; | |
651 | yynerrs = 0; | |
652 | yyerrflag = 0; | |
653 | yyps= &yys[-1]; | |
654 | yypv= &yyv[-1]; | |
655 | ||
656 | yystack: /* put a state and value onto the stack */ | |
657 | ||
658 | #ifdef YYDEBUG | |
659 | if( yydebug ) printf( "state %d, char 0%o\n", yystate, yychar ); | |
660 | #endif | |
661 | if( ++yyps> &yys[YYMAXDEPTH] ) { yyerror( "yacc stack overflow" ); return(1); } | |
662 | *yyps = yystate; | |
663 | ++yypv; | |
664 | *yypv = yyval; | |
665 | ||
666 | yynewstate: | |
667 | ||
668 | yyn = yypact[yystate]; | |
669 | ||
670 | if( yyn<= YYFLAG ) goto yydefault; /* simple state */ | |
671 | ||
672 | if( yychar<0 ) if( (yychar=yylex())<0 ) yychar=0; | |
673 | if( (yyn += yychar)<0 || yyn >= YYLAST ) goto yydefault; | |
674 | ||
675 | if( yychk[ yyn=yyact[ yyn ] ] == yychar ){ /* valid shift */ | |
676 | yychar = -1; | |
677 | yyval = yylval; | |
678 | yystate = yyn; | |
679 | if( yyerrflag > 0 ) --yyerrflag; | |
680 | goto yystack; | |
681 | } | |
682 | ||
683 | yydefault: | |
684 | /* default state action */ | |
685 | ||
686 | if( (yyn=yydef[yystate]) == -2 ) { | |
687 | if( yychar<0 ) if( (yychar=yylex())<0 ) yychar = 0; | |
688 | /* look through exception table */ | |
689 | ||
690 | for( yyxi=yyexca; (*yyxi!= (-1)) || (yyxi[1]!=yystate) ; yyxi += 2 ) ; /* VOID */ | |
691 | ||
692 | while( *(yyxi+=2) >= 0 ){ | |
693 | if( *yyxi == yychar ) break; | |
694 | } | |
695 | if( (yyn = yyxi[1]) < 0 ) return(0); /* accept */ | |
696 | } | |
697 | ||
698 | if( yyn == 0 ){ /* error */ | |
699 | /* error ... attempt to resume parsing */ | |
700 | ||
701 | switch( yyerrflag ){ | |
702 | ||
703 | case 0: /* brand new error */ | |
704 | ||
705 | yyerror( "syntax error" ); | |
706 | yyerrlab: | |
707 | ++yynerrs; | |
708 | ||
709 | case 1: | |
710 | case 2: /* incompletely recovered error ... try again */ | |
711 | ||
712 | yyerrflag = 3; | |
713 | ||
714 | /* find a state where "error" is a legal shift action */ | |
715 | ||
716 | while ( yyps >= yys ) { | |
717 | yyn = yypact[*yyps] + YYERRCODE; | |
718 | if( yyn>= 0 && yyn < YYLAST && yychk[yyact[yyn]] == YYERRCODE ){ | |
719 | yystate = yyact[yyn]; /* simulate a shift of "error" */ | |
720 | goto yystack; | |
721 | } | |
722 | yyn = yypact[*yyps]; | |
723 | ||
724 | /* the current yyps has no shift onn "error", pop stack */ | |
725 | ||
726 | #ifdef YYDEBUG | |
727 | if( yydebug ) printf( "error recovery pops state %d, uncovers %d\n", *yyps, yyps[-1] ); | |
728 | #endif | |
729 | --yyps; | |
730 | --yypv; | |
731 | } | |
732 | ||
733 | /* there is no state on the stack with an error shift ... abort */ | |
734 | ||
735 | yyabort: | |
736 | return(1); | |
737 | ||
738 | ||
739 | case 3: /* no shift yet; clobber input char */ | |
740 | ||
741 | #ifdef YYDEBUG | |
742 | if( yydebug ) printf( "error recovery discards char %d\n", yychar ); | |
743 | #endif | |
744 | ||
745 | if( yychar == 0 ) goto yyabort; /* don't discard EOF, quit */ | |
746 | yychar = -1; | |
747 | goto yynewstate; /* try again in the same state */ | |
748 | ||
749 | } | |
750 | ||
751 | } | |
752 | ||
753 | /* reduction by production yyn */ | |
754 | ||
755 | #ifdef YYDEBUG | |
756 | if( yydebug ) printf("reduce %d\n",yyn); | |
757 | #endif | |
758 | yyps -= yyr2[yyn]; | |
759 | yypvt = yypv; | |
760 | yypv -= yyr2[yyn]; | |
761 | yyval = yypv[1]; | |
762 | yym=yyn; | |
763 | /* consult goto table to find next state */ | |
764 | yyn = yyr1[yyn]; | |
765 | yyj = yypgo[yyn] + *yyps + 1; | |
766 | if( yyj>=YYLAST || yychk[ yystate = yyact[yyj] ] != -yyn ) yystate = yyact[yypgo[yyn]]; | |
767 | switch(yym){ | |
768 | ||
769 | case 1: | |
770 | # line 65 "egrep.y" | |
771 | { unary(FINAL, yypvt[-0]); | |
772 | line--; | |
773 | } break; | |
774 | case 2: | |
775 | # line 70 "egrep.y" | |
776 | { yyval = node(CAT, yypvt[-1], yypvt[-0]); } break; | |
777 | case 3: | |
778 | # line 72 "egrep.y" | |
779 | { yyval = node(CAT, yypvt[-2], yypvt[-1]); } break; | |
780 | case 4: | |
781 | # line 74 "egrep.y" | |
782 | { yyval = node(CAT, yypvt[-1], yypvt[-0]); } break; | |
783 | case 5: | |
784 | # line 76 "egrep.y" | |
785 | { yyval = node(CAT, yypvt[-2], yypvt[-1]); } break; | |
786 | case 6: | |
787 | # line 79 "egrep.y" | |
788 | { yyval = enter(DOT); | |
789 | yyval = unary(STAR, yyval); } break; | |
790 | case 7: | |
791 | # line 83 "egrep.y" | |
792 | { yyval = enter(yypvt[-0]); } break; | |
793 | case 8: | |
794 | # line 85 "egrep.y" | |
795 | { yyval = enter(DOT); } break; | |
796 | case 9: | |
797 | # line 87 "egrep.y" | |
798 | { yyval = cclenter(CCL); } break; | |
799 | case 10: | |
800 | # line 89 "egrep.y" | |
801 | { yyval = cclenter(NCCL); } break; | |
802 | case 11: | |
803 | # line 93 "egrep.y" | |
804 | { yyval = node(OR, yypvt[-2], yypvt[-0]); } break; | |
805 | case 12: | |
806 | # line 95 "egrep.y" | |
807 | { yyval = node(CAT, yypvt[-1], yypvt[-0]); } break; | |
808 | case 13: | |
809 | # line 97 "egrep.y" | |
810 | { yyval = unary(STAR, yypvt[-1]); } break; | |
811 | case 14: | |
812 | # line 99 "egrep.y" | |
813 | { yyval = unary(PLUS, yypvt[-1]); } break; | |
814 | case 15: | |
815 | # line 101 "egrep.y" | |
816 | { yyval = unary(QUEST, yypvt[-1]); } break; | |
817 | case 16: | |
818 | # line 103 "egrep.y" | |
819 | { yyval = yypvt[-1]; } break; | |
820 | } | |
821 | goto yystack; /* stack new state and value */ | |
822 | ||
823 | } |