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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
|
# include "ldefs.h"
uchar *
getl(uchar *p) /* return next line of input, throw away trailing '\n' */
/* returns 0 if eof is had immediately */
{
int c;
uchar *s, *t;
t = s = p;
while(((c = gch()) != 0) && c != '\n')
*t++ = c;
*t = 0;
if(c == 0 && s == t) return((uchar *)0);
prev = '\n';
pres = '\n';
return(s);
}
void
printerr(char *type, char *fmt, va_list argl)
{
char buf[1024];
if(!eof)fprint(errorf,"%d: ",yyline);
fprint(errorf,"(%s) ", type);
vseprint(buf, buf+sizeof(buf), fmt, argl);
fprint(errorf, "%s\n", buf);
}
void
error(char *s,...)
{
va_list argl;
va_start(argl, s);
printerr("Error", s, argl);
va_end(argl);
# ifdef DEBUG
if(debug && sect != ENDSECTION) {
sect1dump();
sect2dump();
}
# endif
if(
# ifdef DEBUG
debug ||
# endif
report == 1) statistics();
exits("error"); /* error return code */
}
void
warning(char *s,...)
{
va_list argl;
va_start(argl, s);
printerr("Warning", s, argl);
va_end(argl);
Bflush(&fout);
}
void
lgate(void)
{
int fd;
if (lgatflg) return;
lgatflg=1;
if(foutopen == 0){
fd = create("lex.yy.c", OWRITE, 0666);
if(fd < 0)
error("Can't open lex.yy.c");
Binit(&fout, fd, OWRITE);
foutopen = 1;
}
phead1();
}
void
cclinter(int sw)
{
/* sw = 1 ==> ccl */
int i, j, k;
int m;
if(!sw){ /* is NCCL */
for(i=1;i<NCH;i++)
symbol[i] ^= 1; /* reverse value */
}
for(i=1;i<NCH;i++)
if(symbol[i]) break;
if(i >= NCH) return;
i = cindex[i];
/* see if ccl is already in our table */
j = 0;
if(i){
for(j=1;j<NCH;j++){
if((symbol[j] && cindex[j] != i) ||
(!symbol[j] && cindex[j] == i)) break;
}
}
if(j >= NCH) return; /* already in */
m = 0;
k = 0;
for(i=1;i<NCH;i++)
if(symbol[i]){
if(!cindex[i]){
cindex[i] = ccount;
symbol[i] = 0;
m = 1;
} else k = 1;
}
/* m == 1 implies last value of ccount has been used */
if(m)ccount++;
if(k == 0) return; /* is now in as ccount wholly */
/* intersection must be computed */
for(i=1;i<NCH;i++){
if(symbol[i]){
m = 0;
j = cindex[i]; /* will be non-zero */
for(k=1;k<NCH;k++){
if(cindex[k] == j){
if(symbol[k]) symbol[k] = 0;
else {
cindex[k] = ccount;
m = 1;
}
}
}
if(m)ccount++;
}
}
}
int
usescape(int c)
{
int d;
switch(c){
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case 'b': c = '\b'; break;
case 'f': c = 014; break; /* form feed for ascii */
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
c -= '0';
while('0' <= (d=gch()) && d <= '7'){
c = c * 8 + (d-'0');
if(!('0' <= peek && peek <= '7')) break;
}
break;
}
return(c);
}
int
lookup(uchar *s, uchar **t)
{
int i;
i = 0;
while(*t){
if(strcmp((char *)s, *(char **)t) == 0)
return(i);
i++;
t++;
}
return(-1);
}
int
cpyact(void)
{ /* copy C action to the next ; or closing } */
int brac, c, mth;
int savline, sw;
brac = 0;
sw = TRUE;
savline = 0;
while(!eof){
c = gch();
swt:
switch( c ){
case '|': if(brac == 0 && sw == TRUE){
if(peek == '|')gch(); /* eat up an extra '|' */
return(0);
}
break;
case ';':
if( brac == 0 ){
Bputc(&fout, c);
Bputc(&fout, '\n');
return(1);
}
break;
case '{':
brac++;
savline=yyline;
break;
case '}':
brac--;
if( brac == 0 ){
Bputc(&fout, c);
Bputc(&fout, '\n');
return(1);
}
break;
case '/': /* look for comments */
Bputc(&fout, c);
c = gch();
if( c != '*' ) goto swt;
/* it really is a comment */
Bputc(&fout, c);
savline=yyline;
while( c=gch() ){
if( c=='*' ){
Bputc(&fout, c);
if( (c=gch()) == '/' ) goto loop;
}
Bputc(&fout, c);
}
yyline=savline;
error( "EOF inside comment" );
case '\'': /* character constant */
mth = '\'';
goto string;
case '"': /* character string */
mth = '"';
string:
Bputc(&fout, c);
while( c=gch() ){
if( c=='\\' ){
Bputc(&fout, c);
c=gch();
}
else if( c==mth ) goto loop;
Bputc(&fout, c);
if (c == '\n') {
yyline--;
error( "Non-terminated string or character constant");
}
}
error( "EOF in string or character constant" );
case '\0':
yyline = savline;
error("Action does not terminate");
default:
break; /* usual character */
}
loop:
if(c != ' ' && c != '\t' && c != '\n') sw = FALSE;
Bputc(&fout, c);
}
error("Premature EOF");
return(0);
}
int
gch(void){
int c;
prev = pres;
c = pres = peek;
peek = pushptr > pushc ? *--pushptr : Bgetc(fin);
if(peek == Beof && sargc > 1){
Bterm(fin);
fin = Bopen(sargv[fptr++],OREAD);
if(fin == 0)
error("Cannot open file %s",sargv[fptr-1]);
peek = Bgetc(fin);
sargc--;
sargv++;
}
if(c == Beof) {
eof = TRUE;
Bterm(fin);
fin = 0;
return(0);
}
if(c == '\n')yyline++;
return(c);
}
int
mn2(int a, int d, uintptr c)
{
name[tptr] = a;
left[tptr] = d;
right[tptr] = c;
parent[tptr] = 0;
nullstr[tptr] = 0;
switch(a){
case RSTR:
parent[d] = tptr;
break;
case BAR:
case RNEWE:
if(nullstr[d] || nullstr[c]) nullstr[tptr] = TRUE;
parent[d] = parent[c] = tptr;
break;
case RCAT:
case DIV:
if(nullstr[d] && nullstr[c])nullstr[tptr] = TRUE;
parent[d] = parent[c] = tptr;
break;
case RSCON:
parent[d] = tptr;
nullstr[tptr] = nullstr[d];
break;
# ifdef DEBUG
default:
warning("bad switch mn2 %d %d",a,d);
break;
# endif
}
if(tptr > treesize)
error("Parse tree too big %s",(treesize == TREESIZE?"\nTry using %e num":""));
return(tptr++);
}
int
mnp(int a, void *p)
{
name[tptr] = a;
left[tptr] = 0;
parent[tptr] = 0;
nullstr[tptr] = 0;
ptr[tptr] = p;
switch(a){
case RCCL:
case RNCCL:
if(strlen(p) == 0) nullstr[tptr] = TRUE;
break;
default:
error("bad switch mnp %d %P", a, p);
break;
}
if(tptr > treesize)
error("Parse tree too big %s",(treesize == TREESIZE?"\nTry using %e num":""));
return(tptr++);
}
int
mn1(int a, int d)
{
name[tptr] = a;
left[tptr] = d;
parent[tptr] = 0;
nullstr[tptr] = 0;
switch(a){
case STAR:
case QUEST:
nullstr[tptr] = TRUE;
parent[d] = tptr;
break;
case PLUS:
case CARAT:
nullstr[tptr] = nullstr[d];
parent[d] = tptr;
break;
case S2FINAL:
nullstr[tptr] = TRUE;
break;
# ifdef DEBUG
case FINAL:
case S1FINAL:
break;
default:
warning("bad switch mn1 %d %d",a,d);
break;
# endif
}
if(tptr > treesize)
error("Parse tree too big %s",(treesize == TREESIZE?"\nTry using %e num":""));
return(tptr++);
}
int
mn0(int a)
{
name[tptr] = a;
parent[tptr] = 0;
nullstr[tptr] = 0;
if(a >= NCH) switch(a){
case RNULLS: nullstr[tptr] = TRUE; break;
# ifdef DEBUG
default:
warning("bad switch mn0 %d",a);
break;
# endif
}
if(tptr > treesize)
error("Parse tree too big %s",(treesize == TREESIZE?"\nTry using %e num":""));
return(tptr++);
}
void
munputc(int p)
{
*pushptr++ = peek; /* watch out for this */
peek = p;
if(pushptr >= pushc+TOKENSIZE)
error("Too many characters pushed");
}
void
munputs(uchar *p)
{
int i,j;
*pushptr++ = peek;
peek = p[0];
i = strlen((char*)p);
for(j = i-1; j>=1; j--)
*pushptr++ = p[j];
if(pushptr >= pushc+TOKENSIZE)
error("Too many characters pushed");
}
int
dupl(int n)
{
/* duplicate the subtree whose root is n, return ptr to it */
int i;
i = name[n];
if(i < NCH) return(mn0(i));
switch(i){
case RNULLS:
return(mn0(i));
case RCCL: case RNCCL:
return(mnp(i,ptr[n]));
case FINAL: case S1FINAL: case S2FINAL:
return(mn1(i,left[n]));
case STAR: case QUEST: case PLUS: case CARAT:
return(mn1(i,dupl(left[n])));
case RSTR: case RSCON:
return(mn2(i,dupl(left[n]),right[n]));
case BAR: case RNEWE: case RCAT: case DIV:
return(mn2(i,dupl(left[n]),dupl(right[n])));
# ifdef DEBUG
default:
warning("bad switch dupl %d",n);
# endif
}
return(0);
}
# ifdef DEBUG
void
allprint(int c)
{
if(c < 0)
c += 256; /* signed char */
switch(c){
case 014:
print("\\f");
charc++;
break;
case '\n':
print("\\n");
charc++;
break;
case '\t':
print("\\t");
charc++;
break;
case '\b':
print("\\b");
charc++;
break;
case ' ':
print("\\\bb");
break;
default:
if(!isprint(c)){
print("\\%-3o",c);
charc += 3;
} else
print("%c", c);
break;
}
charc++;
}
void
strpt(uchar *s)
{
charc = 0;
while(*s){
allprint(*s++);
if(charc > LINESIZE){
charc = 0;
print("\n\t");
}
}
}
void
sect1dump(void)
{
int i;
print("Sect 1:\n");
if(def[0]){
print("str trans\n");
i = -1;
while(def[++i])
print("%s\t%s\n",def[i],subs[i]);
}
if(sname[0]){
print("start names\n");
i = -1;
while(sname[++i])
print("%s\n",sname[i]);
}
}
void
sect2dump(void)
{
print("Sect 2:\n");
treedump();
}
void
treedump(void)
{
int t;
uchar *p;
print("treedump %d nodes:\n",tptr);
for(t=0;t<tptr;t++){
print("%4d ",t);
parent[t] ? print("p=%4d",parent[t]) : print(" ");
print(" ");
if(name[t] < NCH)
allprint(name[t]);
else switch(name[t]){
case RSTR:
print("%d ",left[t]);
allprint(right[t]);
break;
case RCCL:
print("ccl ");
allprint(ptr[t]);
break;
case RNCCL:
print("nccl ");
allprint(ptr[t]);
break;
case DIV:
print("/ %d %d",left[t],right[t]);
break;
case BAR:
print("| %d %d",left[t],right[t]);
break;
case RCAT:
print("cat %d %d",left[t],right[t]);
break;
case PLUS:
print("+ %d",left[t]);
break;
case STAR:
print("* %d",left[t]);
break;
case CARAT:
print("^ %d",left[t]);
break;
case QUEST:
print("? %d",left[t]);
break;
case RNULLS:
print("nullstring");
break;
case FINAL:
print("final %d",left[t]);
break;
case S1FINAL:
print("s1final %d",left[t]);
break;
case S2FINAL:
print("s2final %d",left[t]);
break;
case RNEWE:
print("new %d %d",left[t],right[t]);
break;
case RSCON:
p = (uchar *)right[t];
print("start %s",sname[*p++-1]);
while(*p)
print(", %s",sname[*p++-1]);
print(" %d",left[t]);
break;
default:
print("unknown %d %d %d",name[t],left[t],right[t]);
break;
}
if(nullstr[t])print("\t(null poss.)");
print("\n");
}
}
# endif
|