1 %token CHAR CCL NCCL STR DELIM SCON ITER NEWE NULLS
5 %left CHAR CCL NCCL '(' '.' STR NULLS
12 #define YYSTYPE union _yystype_
29 if(debug) sect2dump();
33 lexinput: defns delim prods end
36 if(!funcflag)phead2();
51 ={ strcpy((char*)dp,(char*)$2.cp);
53 dp += strlen((char*)$2.cp) + 1;
54 strcpy((char*)dp,(char*)$3.cp);
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 */
68 if(sect == DEFSECTION && debug) sect1dump();
74 ={ $$.i = mn2(RNEWE,$1.i,$2.i);
82 i = mn1(S1FINAL,casecount);
83 else i = mn1(FINAL,casecount);
84 $$.i = mn2(RCAT,$1.i,i);
91 if(debug) sect2dump();
95 ={ $$.i = mn0($1.i); }
101 i = mn2(RSTR,i,*p++);
113 for(i='\n'+1;i<NCH;i++){
118 if(ccptr > ccl+CCLSIZE)
119 error("Too many large character classes");
123 $$.i = mn1(RCCL,(int)p);
127 ={ $$.i = mn1(RCCL,$1.i); }
129 ={ $$.i = mn1(RNCCL,$1.i); }
131 ={ $$.i = mn1(STAR,$1.i); }
133 ={ $$.i = mn1(PLUS,$1.i); }
135 ={ $$.i = mn1(QUEST,$1.i); }
137 ={ $$.i = mn2(BAR,$1.i,$3.i); }
139 ={ $$.i = mn2(RCAT,$1.i,$2.i); }
142 j = mn1(S2FINAL,-casecount);
143 i = mn2(RCAT,$1.i,j);
144 $$.i = mn2(DIV,i,$3.i);
147 $$.i = mn2(RCAT,$1.i,$3.i);
148 warning("Extra slash removed");
152 | r ITER ',' ITER '}'
159 warning("Iteration range must be positive");
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++){
167 g = mn2(RCAT,g,dupl($1.i));
175 if($2.i < 0)warning("Can't have negative iteration");
176 else if($2.i == 0) $$.i = mn0(RNULLS);
180 j = mn2(RCAT,j,dupl($1.i));
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 */
193 j = mn2(RCAT,j,dupl($1.i));
194 k = mn1(PLUS,dupl($1.i));
195 $$.i = mn2(RCAT,j,k);
199 ={ $$.i = mn2(RSCON,$2.i,$1.i); }
201 ={ $$.i = mn1(CARAT,$2.i); }
205 j = mn1(S2FINAL,-casecount);
206 k = mn2(RCAT,$1.i,j);
209 else $$.i = mn2(RCAT,$1.i,i);
215 ={ $$.i = mn0(RNULLS); }
225 static int sectbegin;
226 static uchar token[TOKENSIZE];
233 if(sect == DEFSECTION) { /* definitions section */
235 if(prev == '\n'){ /* next char is at beginning of line */
242 Bprint(&fout,"#define YYNEWLINE %d\n",'\n');
243 Bprint(&fout,"int\nyylex(void){\nint nstr; extern int yyprevious;\nif(yyprevious){}\n");
245 i = treesize*(sizeof(*name)+sizeof(*left)+
246 sizeof(*right)+sizeof(*nullstr)+sizeof(*parent))+ALITTLEEXTRA;
249 error("Too little core for parse tree");
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);
263 if (debug) print("positions (%%p) now %d\n",maxpos);
265 if(report == 2)report = 1;
267 case 'n': case 'N': /* has overridden number of states */
268 while(*p && !isdigit(*p))p++;
269 nstates = atol((char*)p);
271 if(debug)print( " no. states (%%n) now %d\n",nstates);
273 if(report == 2)report = 1;
275 case 'e': case 'E': /* has overridden number of tree nodes */
276 while(*p && !isdigit(*p))p++;
277 treesize = atol((char*)p);
279 if (debug) print("treesize (%%e) now %d\n",treesize);
281 if(report == 2)report = 1;
284 while (*p && !isdigit(*p))p++;
285 outsize = atol((char*)p);
286 if (report ==2) report=1;
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);
293 if (debug)print("N. trans (%%a) now %d\n",ntrans);
296 case 'k': case 'K': /* overriden packed char classes */
297 while (*p && !isdigit(*p))p++;
298 if (report==2) report=1;
300 pchlen = atol((char*)p);
302 if (debug) print( "Size classes (%%k) now %d\n",pchlen);
304 pchar=pcptr=myalloc(pchlen, sizeof(*pchar));
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 */
314 while(*p && strchr(" \t,", *p) == 0) p++;
317 while(*p && strchr(" \t,", *p)) p++;
319 while(*p && strchr(" \t,", *p) == 0)p++;
322 if (*t == 0) continue;
324 Bprint(&fout,"#define %s %d\n",(char*)t,i);
325 strcpy((char*)sp, (char*)t);
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");
336 warning("Invalid request %s",p);
338 } /* end of switch after seeing '%' */
339 case ' ': case '\t': /* must be code */
341 Bprint(&fout, "%s\n",(char*)p);
343 default: /* definition */
344 while(*p && !isspace(*p)) p++;
352 warning("Substitution strings may not begin with digits");
353 return(freturn(STR));
356 /* still sect 1, but prev != '\n' */
359 while(*p && isspace(*p)) p++;
361 warning("No translation given - null string assumed");
362 strcpy((char*)token, (char*)p);
365 return(freturn(STR));
368 /* end of section one processing */
369 } else if(sect == RULESECTION){ /* rules and actions */
375 if(prev == '\n') continue;
380 if(sectbegin == TRUE){
382 while((c=gch()) && c != '\n');
385 if(!funcflag)phead2();
387 Bprint(&fout,"case %d:\n",casecount);
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 %%");
398 if(prev != '\n') goto character;
399 if(peek == '{'){ /* included code */
401 while(!eof && getl(buf) && strcmp("%}",(char*)buf) != 0)
402 Bprint(&fout,"%s\n",(char*)buf);
413 if(peek == ' ' || peek == '\t' || peek == '\n'){
414 Bprint(&fout,"%d\n",30000+casecount++);
420 if(peek == '\n' || peek == ' ' || peek == '\t' || peek == '|' || peek == '/'){
426 if(prev != '\n' && scon != TRUE) goto character; /* valid only at line begin */
443 case '{': /* either iteration or definition */
444 if(isdigit(c=gch())){ /* iteration */
453 yylval.i = atol((char*)token);
457 } else { /* definition */
464 i = lookup(token,def);
466 warning("Definition %s not found",token);
471 case '<': /* start condition ? */
472 if(prev != '\n') /* not at line begin, not start */
478 while(c != ',' && c && c != '>'){
485 i = lookup(token,sname);
487 warning("Undefined start condition %s",token);
491 } while(c && c != '>');
493 /* check if previous value re-usable */
494 for (xp=slist; xp<t; ){
495 if (strcmp((char*)xp, (char*)t)==0)
500 /* re-use previous pointer to string */
504 if(slptr > slist+STARTSIZE) /* note not packed ! */
505 error("Too many start conditions used");
511 while((c=gch()) && c != '"' && c != '\n'){
512 if(c == '\\') c = usescape(gch());
515 warning("String too long");
522 warning("Non-terminated string");
536 for(i=1;i<NCH;i++) symbol[i] = 0;
538 if((c = gch()) == '^'){
542 while(c != ']' && c){
543 if(c == '\\') c = usescape(gch());
546 if((c=gch()) == '-' && peek != ']'){ /* range specified */
548 if(c == '\\') c = usescape(gch());
555 if(!(('A' <= j && k <= 'Z') ||
556 ('a' <= j && k <= 'z') ||
557 ('0' <= j && k <= '9')))
558 warning("Non-portable Character Class");
560 symbol[n] = 1; /* implementation dependent */
564 /* try to pack ccl's */
567 if(symbol[j])token[i++] = j;
570 while(p <ccptr && strcmp((char*)token,(char*)p) != 0)p++;
571 if(p < ccptr) /* found it */
575 strcpy((char*)ccptr,(char*)token);
576 ccptr += strlen((char*)token) + 1;
577 if(ccptr >= ccl+CCLSIZE)
578 error("Too many large character classes");
586 if(iter){ /* second part of an iteration */
588 if('0' <= c && c <= '9')
597 if(peek == '?' || peek == '*' || peek == '+')
611 if(x == SCON)scon = TRUE;
620 Bprint(&fout,"\n/*this comes from section three - debug */\n");
622 while(getl(buf) && !eof)
623 Bprint(&fout,"%s\n",(char*)buf);
632 print("now return ");
633 if(i < NCH) allprint(i);
635 printf(" yylval = ");
637 case STR: case CCL: case NCCL:
644 print("%d",yylval.i);