Blob


1 %token CHAR CCL NCCL STR DELIM SCON ITER NEWE NULLS
2 %left SCON '/' NEWE
3 %left '|'
4 %left '$' '^'
5 %left CHAR CCL NCCL '(' '.' STR NULLS
6 %left ITER
7 %left CAT
8 %left '*' '+' '?'
10 %{
11 # include "ldefs.h"
12 #define YYSTYPE union _yystype_
13 union _yystype_
14 {
15 int i;
16 uchar *cp;
17 };
18 %}
19 %%
20 %{
21 int i;
22 int j,k;
23 int g;
24 uchar *p;
25 %}
26 acc : lexinput
27 ={
28 # ifdef DEBUG
29 if(debug) sect2dump();
30 # endif
31 }
32 ;
33 lexinput: defns delim prods end
34 | defns delim end
35 ={
36 if(!funcflag)phead2();
37 funcflag = TRUE;
38 }
39 | error
40 ={
41 # ifdef DEBUG
42 if(debug) {
43 sect1dump();
44 sect2dump();
45 }
46 # endif
47 }
48 ;
49 end: delim | ;
50 defns: defns STR STR
51 ={ strcpy((char*)dp,(char*)$2.cp);
52 def[dptr] = dp;
53 dp += strlen((char*)$2.cp) + 1;
54 strcpy((char*)dp,(char*)$3.cp);
55 subs[dptr++] = dp;
56 if(dptr >= DEFSIZE)
57 error("Too many definitions");
58 dp += strlen((char*)$3.cp) + 1;
59 if(dp >= dchar+DEFCHAR)
60 error("Definitions too long");
61 subs[dptr]=def[dptr]=0; /* for lookup - require ending null */
62 }
63 |
64 ;
65 delim: DELIM
66 ={
67 # ifdef DEBUG
68 if(sect == DEFSECTION && debug) sect1dump();
69 # endif
70 sect++;
71 }
72 ;
73 prods: prods pr
74 ={ $$.i = mn2(RNEWE,$1.i,$2.i);
75 }
76 | pr
77 ={ $$.i = $1.i;}
78 ;
79 pr: r NEWE
80 ={
81 if(divflg == TRUE)
82 i = mn1(S1FINAL,casecount);
83 else i = mn1(FINAL,casecount);
84 $$.i = mn2(RCAT,$1.i,i);
85 divflg = FALSE;
86 casecount++;
87 }
88 | error NEWE
89 ={
90 # ifdef DEBUG
91 if(debug) sect2dump();
92 # endif
93 }
94 r: CHAR
95 ={ $$.i = mn0($1.i); }
96 | STR
97 ={
98 p = $1.cp;
99 i = mn0(*p++);
100 while(*p)
101 i = mn2(RSTR,i,*p++);
102 $$.i = i;
104 | '.'
105 ={ symbol['\n'] = 0;
106 if(psave == FALSE){
107 p = ccptr;
108 psave = ccptr;
109 for(i=1;i<'\n';i++){
110 symbol[i] = 1;
111 *ccptr++ = i;
113 for(i='\n'+1;i<NCH;i++){
114 symbol[i] = 1;
115 *ccptr++ = i;
117 *ccptr++ = 0;
118 if(ccptr > ccl+CCLSIZE)
119 error("Too many large character classes");
121 else
122 p = psave;
123 $$.i = mn1(RCCL,(int)p);
124 cclinter(1);
126 | CCL
127 ={ $$.i = mn1(RCCL,$1.i); }
128 | NCCL
129 ={ $$.i = mn1(RNCCL,$1.i); }
130 | r '*'
131 ={ $$.i = mn1(STAR,$1.i); }
132 | r '+'
133 ={ $$.i = mn1(PLUS,$1.i); }
134 | r '?'
135 ={ $$.i = mn1(QUEST,$1.i); }
136 | r '|' r
137 ={ $$.i = mn2(BAR,$1.i,$3.i); }
138 | r r %prec CAT
139 ={ $$.i = mn2(RCAT,$1.i,$2.i); }
140 | r '/' r
141 ={ if(!divflg){
142 j = mn1(S2FINAL,-casecount);
143 i = mn2(RCAT,$1.i,j);
144 $$.i = mn2(DIV,i,$3.i);
146 else {
147 $$.i = mn2(RCAT,$1.i,$3.i);
148 warning("Extra slash removed");
150 divflg = TRUE;
152 | r ITER ',' ITER '}'
153 ={ if($2.i > $4.i){
154 i = $2.i;
155 $2.i = $4.i;
156 $4.i = i;
158 if($4.i <= 0)
159 warning("Iteration range must be positive");
160 else {
161 j = $1.i;
162 for(k = 2; k<=$2.i;k++)
163 j = mn2(RCAT,j,dupl($1.i));
164 for(i = $2.i+1; i<=$4.i; i++){
165 g = dupl($1.i);
166 for(k=2;k<=i;k++)
167 g = mn2(RCAT,g,dupl($1.i));
168 j = mn2(BAR,j,g);
170 $$.i = j;
173 | r ITER '}'
174 ={
175 if($2.i < 0)warning("Can't have negative iteration");
176 else if($2.i == 0) $$.i = mn0(RNULLS);
177 else {
178 j = $1.i;
179 for(k=2;k<=$2.i;k++)
180 j = mn2(RCAT,j,dupl($1.i));
181 $$.i = j;
184 | r ITER ',' '}'
185 ={
186 /* from n to infinity */
187 if($2.i < 0)warning("Can't have negative iteration");
188 else if($2.i == 0) $$.i = mn1(STAR,$1.i);
189 else if($2.i == 1)$$.i = mn1(PLUS,$1.i);
190 else { /* >= 2 iterations minimum */
191 j = $1.i;
192 for(k=2;k<$2.i;k++)
193 j = mn2(RCAT,j,dupl($1.i));
194 k = mn1(PLUS,dupl($1.i));
195 $$.i = mn2(RCAT,j,k);
198 | SCON r
199 ={ $$.i = mn2(RSCON,$2.i,$1.i); }
200 | '^' r
201 ={ $$.i = mn1(CARAT,$2.i); }
202 | r '$'
203 ={ i = mn0('\n');
204 if(!divflg){
205 j = mn1(S2FINAL,-casecount);
206 k = mn2(RCAT,$1.i,j);
207 $$.i = mn2(DIV,k,i);
209 else $$.i = mn2(RCAT,$1.i,i);
210 divflg = TRUE;
212 | '(' r ')'
213 ={ $$.i = $2.i; }
214 | NULLS
215 ={ $$.i = mn0(RNULLS); }
217 %%
218 int
219 yylex(void)
221 uchar *p;
222 int c, i;
223 uchar *t, *xp;
224 int n, j, k, x;
225 static int sectbegin;
226 static uchar token[TOKENSIZE];
227 static int iter;
229 # ifdef DEBUG
230 yylval.i = 0;
231 # endif
233 if(sect == DEFSECTION) { /* definitions section */
234 while(!eof) {
235 if(prev == '\n'){ /* next char is at beginning of line */
236 getl(p=buf);
237 switch(*p){
238 case '%':
239 switch(*(p+1)){
240 case '%':
241 lgate();
242 Bprint(&fout,"#define YYNEWLINE %d\n",'\n');
243 Bprint(&fout,"int\nyylex(void){\nint nstr; extern int yyprevious;\nif(yyprevious){}\n");
244 sectbegin = TRUE;
245 i = treesize*(sizeof(*name)+sizeof(*left)+
246 sizeof(*right)+sizeof(*nullstr)+sizeof(*parent))+ALITTLEEXTRA;
247 p = myalloc(i,1);
248 if(p == 0)
249 error("Too little core for parse tree");
250 free(p);
251 name = myalloc(treesize,sizeof(*name));
252 left = myalloc(treesize,sizeof(*left));
253 right = myalloc(treesize,sizeof(*right));
254 nullstr = myalloc(treesize,sizeof(*nullstr));
255 parent = myalloc(treesize,sizeof(*parent));
256 if(name == 0 || left == 0 || right == 0 || parent == 0 || nullstr == 0)
257 error("Too little core for parse tree");
258 return(freturn(DELIM));
259 case 'p': case 'P': /* has overridden number of positions */
260 while(*p && !isdigit(*p))p++;
261 maxpos = atol((char*)p);
262 # ifdef DEBUG
263 if (debug) print("positions (%%p) now %d\n",maxpos);
264 # endif
265 if(report == 2)report = 1;
266 continue;
267 case 'n': case 'N': /* has overridden number of states */
268 while(*p && !isdigit(*p))p++;
269 nstates = atol((char*)p);
270 # ifdef DEBUG
271 if(debug)print( " no. states (%%n) now %d\n",nstates);
272 # endif
273 if(report == 2)report = 1;
274 continue;
275 case 'e': case 'E': /* has overridden number of tree nodes */
276 while(*p && !isdigit(*p))p++;
277 treesize = atol((char*)p);
278 # ifdef DEBUG
279 if (debug) print("treesize (%%e) now %d\n",treesize);
280 # endif
281 if(report == 2)report = 1;
282 continue;
283 case 'o': case 'O':
284 while (*p && !isdigit(*p))p++;
285 outsize = atol((char*)p);
286 if (report ==2) report=1;
287 continue;
288 case 'a': case 'A': /* has overridden number of transitions */
289 while(*p && !isdigit(*p))p++;
290 if(report == 2)report = 1;
291 ntrans = atol((char*)p);
292 # ifdef DEBUG
293 if (debug)print("N. trans (%%a) now %d\n",ntrans);
294 # endif
295 continue;
296 case 'k': case 'K': /* overriden packed char classes */
297 while (*p && !isdigit(*p))p++;
298 if (report==2) report=1;
299 free(pchar);
300 pchlen = atol((char*)p);
301 # ifdef DEBUG
302 if (debug) print( "Size classes (%%k) now %d\n",pchlen);
303 # endif
304 pchar=pcptr=myalloc(pchlen, sizeof(*pchar));
305 continue;
306 case '{':
307 lgate();
308 while(getl(p) && strcmp((char*)p,"%}") != 0)
309 Bprint(&fout, "%s\n",(char*)p);
310 if(p[0] == '%') continue;
311 error("Premature eof");
312 case 's': case 'S': /* start conditions */
313 lgate();
314 while(*p && strchr(" \t,", *p) == 0) p++;
315 n = TRUE;
316 while(n){
317 while(*p && strchr(" \t,", *p)) p++;
318 t = p;
319 while(*p && strchr(" \t,", *p) == 0)p++;
320 if(!*p) n = FALSE;
321 *p++ = 0;
322 if (*t == 0) continue;
323 i = sptr*2;
324 Bprint(&fout,"#define %s %d\n",(char*)t,i);
325 strcpy((char*)sp, (char*)t);
326 sname[sptr++] = sp;
327 sname[sptr] = 0; /* required by lookup */
328 if(sptr >= STARTSIZE)
329 error("Too many start conditions");
330 sp += strlen((char*)sp) + 1;
331 if(sp >= stchar+STARTCHAR)
332 error("Start conditions too long");
334 continue;
335 default:
336 warning("Invalid request %s",p);
337 continue;
338 } /* end of switch after seeing '%' */
339 case ' ': case '\t': /* must be code */
340 lgate();
341 Bprint(&fout, "%s\n",(char*)p);
342 continue;
343 default: /* definition */
344 while(*p && !isspace(*p)) p++;
345 if(*p == 0)
346 continue;
347 prev = *p;
348 *p = 0;
349 bptr = p+1;
350 yylval.cp = buf;
351 if(isdigit(buf[0]))
352 warning("Substitution strings may not begin with digits");
353 return(freturn(STR));
356 /* still sect 1, but prev != '\n' */
357 else {
358 p = bptr;
359 while(*p && isspace(*p)) p++;
360 if(*p == 0)
361 warning("No translation given - null string assumed");
362 strcpy((char*)token, (char*)p);
363 yylval.cp = token;
364 prev = '\n';
365 return(freturn(STR));
368 /* end of section one processing */
369 } else if(sect == RULESECTION){ /* rules and actions */
370 while(!eof){
371 switch(c=gch()){
372 case '\0':
373 return(freturn(0));
374 case '\n':
375 if(prev == '\n') continue;
376 x = NEWE;
377 break;
378 case ' ':
379 case '\t':
380 if(sectbegin == TRUE){
381 cpyact();
382 while((c=gch()) && c != '\n');
383 continue;
385 if(!funcflag)phead2();
386 funcflag = TRUE;
387 Bprint(&fout,"case %d:\n",casecount);
388 if(cpyact())
389 Bprint(&fout,"break;\n");
390 while((c=gch()) && c != '\n');
391 if(peek == ' ' || peek == '\t' || sectbegin == TRUE){
392 warning("Executable statements should occur right after %%");
393 continue;
395 x = NEWE;
396 break;
397 case '%':
398 if(prev != '\n') goto character;
399 if(peek == '{'){ /* included code */
400 getl(buf);
401 while(!eof && getl(buf) && strcmp("%}",(char*)buf) != 0)
402 Bprint(&fout,"%s\n",(char*)buf);
403 continue;
405 if(peek == '%'){
406 gch();
407 gch();
408 x = DELIM;
409 break;
411 goto character;
412 case '|':
413 if(peek == ' ' || peek == '\t' || peek == '\n'){
414 Bprint(&fout,"%d\n",30000+casecount++);
415 continue;
417 x = '|';
418 break;
419 case '$':
420 if(peek == '\n' || peek == ' ' || peek == '\t' || peek == '|' || peek == '/'){
421 x = c;
422 break;
424 goto character;
425 case '^':
426 if(prev != '\n' && scon != TRUE) goto character; /* valid only at line begin */
427 x = c;
428 break;
429 case '?':
430 case '+':
431 case '.':
432 case '*':
433 case '(':
434 case ')':
435 case ',':
436 case '/':
437 x = c;
438 break;
439 case '}':
440 iter = FALSE;
441 x = c;
442 break;
443 case '{': /* either iteration or definition */
444 if(isdigit(c=gch())){ /* iteration */
445 iter = TRUE;
446 ieval:
447 i = 0;
448 while(isdigit(c)){
449 token[i++] = c;
450 c = gch();
452 token[i] = 0;
453 yylval.i = atol((char*)token);
454 munputc(c);
455 x = ITER;
456 break;
457 } else { /* definition */
458 i = 0;
459 while(c && c!='}'){
460 token[i++] = c;
461 c = gch();
463 token[i] = 0;
464 i = lookup(token,def);
465 if(i < 0)
466 warning("Definition %s not found",token);
467 else
468 munputs(subs[i]);
469 continue;
471 case '<': /* start condition ? */
472 if(prev != '\n') /* not at line begin, not start */
473 goto character;
474 t = slptr;
475 do {
476 i = 0;
477 c = gch();
478 while(c != ',' && c && c != '>'){
479 token[i++] = c;
480 c = gch();
482 token[i] = 0;
483 if(i == 0)
484 goto character;
485 i = lookup(token,sname);
486 if(i < 0) {
487 warning("Undefined start condition %s",token);
488 continue;
490 *slptr++ = i+1;
491 } while(c && c != '>');
492 *slptr++ = 0;
493 /* check if previous value re-usable */
494 for (xp=slist; xp<t; ){
495 if (strcmp((char*)xp, (char*)t)==0)
496 break;
497 while (*xp++);
499 if (xp<t){
500 /* re-use previous pointer to string */
501 slptr=t;
502 t=xp;
504 if(slptr > slist+STARTSIZE) /* note not packed ! */
505 error("Too many start conditions used");
506 yylval.cp = t;
507 x = SCON;
508 break;
509 case '"':
510 i = 0;
511 while((c=gch()) && c != '"' && c != '\n'){
512 if(c == '\\') c = usescape(gch());
513 token[i++] = c;
514 if(i > TOKENSIZE){
515 warning("String too long");
516 i = TOKENSIZE-1;
517 break;
520 if(c == '\n') {
521 yyline--;
522 warning("Non-terminated string");
523 yyline++;
525 token[i] = 0;
526 if(i == 0)x = NULLS;
527 else if(i == 1){
528 yylval.i = token[0];
529 x = CHAR;
530 } else {
531 yylval.cp = token;
532 x = STR;
534 break;
535 case '[':
536 for(i=1;i<NCH;i++) symbol[i] = 0;
537 x = CCL;
538 if((c = gch()) == '^'){
539 x = NCCL;
540 c = gch();
542 while(c != ']' && c){
543 if(c == '\\') c = usescape(gch());
544 symbol[c] = 1;
545 j = c;
546 if((c=gch()) == '-' && peek != ']'){ /* range specified */
547 c = gch();
548 if(c == '\\') c = usescape(gch());
549 k = c;
550 if(j > k) {
551 n = j;
552 j = k;
553 k = n;
555 if(!(('A' <= j && k <= 'Z') ||
556 ('a' <= j && k <= 'z') ||
557 ('0' <= j && k <= '9')))
558 warning("Non-portable Character Class");
559 for(n=j+1;n<=k;n++)
560 symbol[n] = 1; /* implementation dependent */
561 c = gch();
564 /* try to pack ccl's */
565 i = 0;
566 for(j=0;j<NCH;j++)
567 if(symbol[j])token[i++] = j;
568 token[i] = 0;
569 p = ccl;
570 while(p <ccptr && strcmp((char*)token,(char*)p) != 0)p++;
571 if(p < ccptr) /* found it */
572 yylval.cp = p;
573 else {
574 yylval.cp = ccptr;
575 strcpy((char*)ccptr,(char*)token);
576 ccptr += strlen((char*)token) + 1;
577 if(ccptr >= ccl+CCLSIZE)
578 error("Too many large character classes");
580 cclinter(x==CCL);
581 break;
582 case '\\':
583 c = usescape(gch());
584 default:
585 character:
586 if(iter){ /* second part of an iteration */
587 iter = FALSE;
588 if('0' <= c && c <= '9')
589 goto ieval;
591 if(isalpha(peek)){
592 i = 0;
593 yylval.cp = token;
594 token[i++] = c;
595 while(isalpha(peek))
596 token[i++] = gch();
597 if(peek == '?' || peek == '*' || peek == '+')
598 munputc(token[--i]);
599 token[i] = 0;
600 if(i == 1){
601 yylval.i = token[0];
602 x = CHAR;
604 else x = STR;
605 } else {
606 yylval.i = c;
607 x = CHAR;
610 scon = FALSE;
611 if(x == SCON)scon = TRUE;
612 sectbegin = FALSE;
613 return(freturn(x));
616 /* section three */
617 ptail();
618 # ifdef DEBUG
619 if(debug)
620 Bprint(&fout,"\n/*this comes from section three - debug */\n");
621 # endif
622 while(getl(buf) && !eof)
623 Bprint(&fout,"%s\n",(char*)buf);
624 return(freturn(0));
626 /* end of yylex */
627 # ifdef DEBUG
628 int
629 freturn(int i)
631 if(yydebug) {
632 print("now return ");
633 if(i < NCH) allprint(i);
634 else print("%d",i);
635 printf(" yylval = ");
636 switch(i){
637 case STR: case CCL: case NCCL:
638 strpt(yylval.cp);
639 break;
640 case CHAR:
641 allprint(yylval.i);
642 break;
643 default:
644 print("%d",yylval.i);
645 break;
647 print("\n");
649 return(i);
651 # endif