6 #define Bungetrune Bungetc /* ok for now. */
11 #define TBITSET ((32+NTERMS)/32) /* BOTCH?? +31 */
12 #define BIT(a,i) ((a)[(i)>>5] & (1<<((i)&037)))
13 #define SETBIT(a,i) ((a)[(i)>>5] |= (1<<((i)&037)))
14 #define NWORDS(n) (((n)+32)/32)
16 #define PARSER "/sys/lib/yaccpar"
17 #define PARSERS "/sys/lib/yaccpars"
18 #define TEMPNAME "y.tmp.XXXXXX"
19 #define ACTNAME "y.acts.XXXXXX"
21 #define FILEU "output"
23 #define FILEDEBUG "debug"
28 * the following are adjustable
29 * according to memory size
46 PRIVATE = 0xE000, /* unicode private use */
48 /* relationships which must hold:
49 TBITSET ints must hold NTERMS+1 bits...
52 TEMPSIZE >= NTERMS + NNONTERM + 1
60 NOASC = 0, /* no assoc. */
61 LASC = 1, /* left assoc. */
62 RASC = 2, /* right assoc. */
63 BASC = 3, /* binary assoc. */
65 /* flags for state generation */
71 /* flags for a rule having an action, and being reduced */
76 /* output parser flags */
103 /* macros for getting associativity and precedence levels */
105 #define ASSOC(i) ((i)&03)
106 #define PLEVEL(i) (((i)>>4)&077)
107 #define TYPE(i) (((i)>>10)&077)
109 /* macros for setting associativity and precedence levels */
111 #define SETASC(i,j) i |= j
112 #define SETPLEV(i,j) i |= (j<<4)
113 #define SETTYPE(i,j) i |= (j<<10)
117 #define TLOOP(i) for(i=1; i<=ntokens; i++)
118 #define NTLOOP(i) for(i=0; i<=nnonter; i++)
119 #define PLOOP(s,i) for(i=s; i<nprod; i++)
120 #define SLOOP(i) for(i=0; i<nstate; i++)
121 #define WSBUMP(x) x++
122 #define WSLOOP(s,j) for(j=s; j<cwp; j++)
123 #define ITMLOOP(i,p,q) for(q=pstate[i+1], p=pstate[i]; p<q; p++)
124 #define SETLOOP(i) for(i=0; i<tbitset; i++)
126 /* command to clobber tempfiles after use */
128 #define ZAPFILE(x) if(x) remove(x)
130 /* I/O descriptors */
132 Biobuf* faction; /* file for saving actions */
133 Biobuf* fdefine; /* file for #defines */
134 Biobuf* fdebug; /* y.debug for strings for debugging */
135 Biobuf* ftable; /* y.tab.c file */
136 Biobuf* ftemp; /* tempfile to pass 2 */
137 Biobuf* finput; /* input file */
138 Biobuf* foutput; /* y.output file */
140 /* communication variables between various I/O routines */
142 char* infile; /* input file name */
143 int numbval; /* value of an input number */
144 char tokname[NAMESIZE+4]; /* input token name, slop for runes and 0 */
146 /* structure declarations */
176 /* storage of names */
178 char cnames[CNAMSZ]; /* place where token and nonterminal names are stored */
179 int cnamsz = CNAMSZ; /* size of cnames */
180 char* cnamp = cnames; /* place where next name is to be put in */
181 int ndefout = 4; /* number of defined symbols output */
184 char ttempname[] = TEMPNAME;
185 char tactname[] = ACTNAME;
186 char* parser = PARSER;
189 /* storage of types */
190 int ntypes; /* number of types defined */
191 char* typeset[NTYPES]; /* pointers to type tags */
193 /* token information */
195 int ntokens = 0 ; /* number of tokens */
197 int toklev[NTERMS]; /* vector with the precedence of the terminals */
199 /* nonterminal information */
201 int nnonter = -1; /* the number of nonterminals */
202 Symb nontrst[NNONTERM];
203 int start; /* start symbol */
205 /* assigned token type values */
208 char* ytabc = OFILE; /* name of y.tab.c */
210 /* grammar rule information */
212 int mem0[MEMSIZE] ; /* production storage */
214 int nprod = 1; /* number of productions */
215 int* prdptr[NPROD]; /* pointers to descriptions of productions */
216 int levprd[NPROD]; /* precedence levels for the productions */
217 int rlines[NPROD]; /* line number for this rule */
219 /* state information */
221 int nstate = 0; /* number of states */
222 Item* pstate[NSTATES+2]; /* pointers to the descriptions of the states */
223 int tystate[NSTATES]; /* contains type information about the states */
224 int defact[NSTATES]; /* the default actions of states */
225 int tstates[NTERMS]; /* states generated by terminal gotos */
226 int ntstates[NNONTERM]; /* states generated by nonterminal gotos */
227 int mstates[NSTATES]; /* chain of overflows of term/nonterm generation lists */
228 int lastred; /* the number of the last reduction of a state */
230 /* lookahead set information */
232 Lkset lkst[LSETSIZE];
233 int nolook; /* flag to turn off lookahead computations */
234 int tbitset; /* size of lookahead sets */
235 int nlset = 0; /* next lookahead set index */
236 int nolook = 0; /* flag to suppress lookahead computations */
237 Lkset clset; /* temporary storage for lookahead computations */
239 /* working set information */
241 Wset wsets[WSETSIZE];
244 /* storage for action table */
246 int amem[ACTSIZE]; /* action table storage */
247 int* memp = amem; /* next free action table position */
248 int indgo[NSTATES]; /* index to the stored goto table */
250 /* temporary vector, indexable by states, terms, or ntokens */
252 int temp1[TEMPSIZE]; /* temporary storage, indexed by terms + ntokens or states */
253 int lineno = 1; /* current input line number */
254 int fatfl = 1; /* if on, error is fatal */
255 int nerrors = 0; /* number of errors */
257 /* statistics collection variables */
267 int* ggreed = lkst[0].lset;
268 int* pgo = wsets[0].ws.lset;
269 int* yypgo = &nontrst[0].value;
271 int maxspr = 0; /* maximum spread of any entry */
272 int maxoff = 0; /* maximum offset into a array */
279 /* storage for information about the nonterminals */
281 int** pres[NNONTERM+2]; /* vector of pointers to productions yielding each nonterminal */
282 Lkset* pfirst[NNONTERM+2]; /* vector of pointers to first sets for each nonterminal */
283 int pempty[NNONTERM+1]; /* vector of nonterminals nontrivially deriving e */
285 /* random stuff picked out from between functions */
297 int pidebug = 0; /* debugging flag for putitem */
299 int cldebug = 0; /* debugging flag for closure */
322 /* define functions */
324 void main(int, char**);
326 char* chcopy(char*, char*);
330 void error(char*, ...);
331 void aryfil(int*, int, int);
332 int setunion(int*, int*);
337 void putitem(int*, Lkset*);
341 Lkset* flset(Lkset*);
344 void setup(int, char**);
346 int defin(int, char*);
351 int chfind(int, char*);
356 void openup(char*, int, int, int, char*);
358 int apack(int*, int);
361 void precftn(int, int, int);
364 void warray(char*, int*, int);
372 void arout(char*, int*, int);
376 main(int argc, char *argv[])
379 setup(argc, argv); /* initialize and read productions */
380 tbitset = NWORDS(ntokens);
381 cpres(); /* make table of which productions yield a given nonterminal */
382 cempty(); /* make a table of which nonterminals can match the empty string */
383 cpfir(); /* make a table of firsts of nonterminals */
384 stagen(); /* generate the states */
385 output(); /* write the states and the tables */
395 * put out other arrays, copy the parsers
402 finput = Bopen(parser, OREAD);
404 error("cannot find parser %s", parser);
405 warray("yyr1", levprd, nprod);
406 aryfil(temp1, nprod, 0);
408 temp1[i] = prdptr[i+1]-prdptr[i]-2;
409 warray("yyr2", temp1, nprod);
411 aryfil(temp1, nstate, -1000);
413 for(j=tstates[i]; j!=0; j=mstates[j])
416 for(j=ntstates[i]; j!=0; j=mstates[j])
418 warray("yychk", temp1, nstate);
419 warray("yydef", defact, nstate);
421 /* put out token translation tables */
422 /* table 1 has 0-256 */
423 aryfil(temp1, 256, 0);
427 if(j >= 0 && j < 256) {
429 print("yacc bug -- cant have 2 different Ts with same value\n");
430 print(" %s and %s\n", tokset[i].name, tokset[temp1[j]].name);
438 warray("yytok1", temp1, c+1);
440 /* table 2 has PRIVATE-PRIVATE+256 */
441 aryfil(temp1, 256, 0);
444 j = tokset[i].value - PRIVATE;
445 if(j >= 0 && j < 256) {
447 print("yacc bug -- cant have 2 different Ts with same value\n");
448 print(" %s and %s\n", tokset[i].name, tokset[temp1[j]].name);
456 warray("yytok2", temp1, c+1);
458 /* table 3 has everything else */
459 Bprint(ftable, "long yytok3[] =\n{\n");
463 if(j >= 0 && j < 256)
465 if(j >= PRIVATE && j < 256+PRIVATE)
468 Bprint(ftable, "%4d,%4d,", j, i);
471 Bprint(ftable, "\n");
473 Bprint(ftable, "%4d\n};\n", 0);
475 /* copy parser text */
476 while((c=Bgetrune(finput)) != Beof) {
478 if((c = Bgetrune(finput)) != 'A')
479 Bputrune(ftable, '$');
480 else { /* copy actions */
481 faction = Bopen(actname, OREAD);
483 error("cannot reopen action tempfile");
484 while((c=Bgetrune(faction)) != Beof)
488 c = Bgetrune(finput);
497 * copies string q into p, returning next free char ptr
500 chcopy(char* p, char* q)
515 * creates output string for item pointed to by pp
521 static char sarr[ISIZE];
527 q = chcopy(sarr, nontrst[*p-NTBASE].name);
539 q = chcopy(q, symnam(i));
540 if(q > &sarr[ISIZE-30])
541 error("item too big");
544 /* an item calling for a reduction */
548 sprint(q, "%d)", -i);
554 * return a pointer to the name of symbol i
561 cp = (i >= NTBASE)? nontrst[i-NTBASE].name: tokset[i].name;
568 * output the summary on y.output
575 Bprint(foutput, "\n%d/%d terminals, %d/%d nonterminals\n",
576 ntokens, NTERMS, nnonter, NNONTERM);
577 Bprint(foutput, "%d/%d grammar rules, %d/%d states\n",
578 nprod, NPROD, nstate, NSTATES);
579 Bprint(foutput, "%d shift/reduce, %d reduce/reduce conflicts reported\n",
581 Bprint(foutput, "%d/%d working sets used\n",
582 (int)(zzcwp-wsets), WSETSIZE);
583 Bprint(foutput, "memory: states,etc. %d/%d, parser %d/%d\n",
584 (int)(zzmemsz-mem0), MEMSIZE, (int)(memp-amem), ACTSIZE);
585 Bprint(foutput, "%d/%d distinct lookahead sets\n", nlset, LSETSIZE);
586 Bprint(foutput, "%d extra closures\n", zzclose - 2*nstate);
587 Bprint(foutput, "%d shift entries, %d exceptions\n", zzacent, zzexcp);
588 Bprint(foutput, "%d goto entries\n", zzgoent);
589 Bprint(foutput, "%d entries saved by goto default\n", zzgobest);
591 if(zzsrconf != 0 || zzrrconf != 0) {
592 print("\nconflicts: ");
594 print("%d shift/reduce", zzsrconf);
595 if(zzsrconf && zzrrconf)
598 print("%d reduce/reduce", zzrrconf);
612 * write out error comment -- NEEDS WORK
619 fprint(2, "\n fatal error:");
620 fprint(2, s, (&s)[1]);
621 fprint(2, ", %s:%d\n", infile, lineno);
630 * set elements 0 through n-1 to c
633 aryfil(int *v, int n, int c)
642 * set a to the union of a and b
643 * return 1 if b is not a subset of a, 0 otherwise
646 setunion(int *a, int *b)
669 Bprint(foutput, "\tNULL");
671 Bprint(foutput, " { ");
674 Bprint(foutput, "%s ", symnam(j));
675 Bprint(foutput, "}");
680 * compute an array with the beginnings of productions yielding given nonterminals
681 * The array pres points to these lists
682 * the array pyield has the lists: the total size is only NPROD+1
688 static int *pyield[NPROD];
694 fatfl = 0; /* make undefined symbols nonfatal */
697 *pmem++ = prdptr[j]+1;
699 error("nonterminal %s not defined!", nontrst[i].name);
708 if(pmem != &pyield[nprod])
709 error("internal Yacc error: pyield %d", pmem-&pyield[nprod]);
713 * compute an array with the first of nonterminals
718 int *p, **s, i, **t, ch, changes;
720 zzcwp = &wsets[nnonter];
722 aryfil(wsets[i].ws.lset, tbitset, 0);
724 /* initially fill the sets */
725 for(s=pres[i]; s<t; ++s)
726 for(p = *s; (ch = *p) > 0; ++p) {
728 SETBIT(wsets[i].ws.lset, ch);
731 if(!pempty[ch-NTBASE])
736 /* now, reflect transitivity */
742 for(s = pres[i]; s < t; ++s)
743 for(p = *s; (ch = (*p-NTBASE)) >= 0; ++p) {
744 changes |= setunion(wsets[i].ws.lset, wsets[ch].ws.lset);
752 pfirst[i] = flset(&wsets[i].ws);
757 Bprint(foutput, "\n%s: ", nontrst[i].name);
759 Bprint(foutput, " %d\n", pempty[i]);
764 * sorts last state,and sees if it equals earlier ones. returns state number
769 Item *p1, *p2, *k, *l, *q1, *q2;
773 p2 = pstate[nstate+1];
775 return 0; /* null state */
777 for(k = p2-1; k > p1; k--) /* make k the biggest */
778 for(l = k-1; l >= p1; --l)
779 if(l->pitem > k->pitem) {
790 size1 = p2 - p1; /* size of state */
792 for(i = (c>=NTBASE)? ntstates[c-NTBASE]: tstates[c]; i != 0; i = mstates[i]) {
800 for(l = q1; l < q2; l++) {
801 if(l->pitem != k->pitem)
808 pstate[nstate+1] = pstate[nstate]; /* delete last state */
809 /* fix up lookaheads */
812 for(l = q1, k = p1; l < q2; ++l, ++k ) {
816 clset.lset[s] = l->look->lset[s];
817 if(setunion(clset.lset, k->look->lset)) {
819 /* register the new set */
820 l->look = flset( &clset );
827 error("yacc state/nolook error");
828 pstate[nstate+2] = p2;
829 if(nstate+1 >= NSTATES)
830 error("too many states");
832 mstates[nstate] = ntstates[c-NTBASE];
833 ntstates[c-NTBASE] = nstate;
835 mstates[nstate] = tstates[c];
838 tystate[nstate] = MUSTDO;
843 putitem(int *ptr, Lkset *lptr)
847 if(pidebug && foutput != 0)
848 Bprint(foutput, "putitem(%s), state %d\n", writem(ptr), nstate);
849 j = pstate[nstate+1];
852 j->look = flset(lptr);
853 pstate[nstate+1] = ++j;
854 if((int*)j > zzmemsz) {
856 if(zzmemsz >= &mem0[MEMSIZE])
857 error("out of state space");
862 * mark nonterminals which derive the empty string
863 * also, look for nonterminals which don't derive any token strings
871 /* first, use the array pempty to detect productions that can never be reduced */
872 /* set pempty to WHONOWS */
873 aryfil(pempty, nnonter+1, WHOKNOWS);
875 /* now, look at productions, marking nonterminals which derive something */
878 if(pempty[*prdptr[i] - NTBASE])
880 for(p = prdptr[i]+1; *p >= 0; ++p)
881 if(*p >= NTBASE && pempty[*p-NTBASE] == WHOKNOWS)
883 /* production can be derived */
885 pempty[*prdptr[i]-NTBASE] = OK;
890 /* now, look at the nonterminals, to see if they are all OK */
892 /* the added production rises or falls as the start symbol ... */
895 if(pempty[i] != OK) {
897 error("nonterminal %s never derives any token string", nontrst[i].name);
907 /* now, compute the pempty array, to see which nonterminals derive the empty string */
908 /* set pempty to WHOKNOWS */
909 aryfil( pempty, nnonter+1, WHOKNOWS);
911 /* loop as long as we keep finding empty nonterminals */
915 /* not known to be empty */
916 if(pempty[*prdptr[i]-NTBASE] == WHOKNOWS) {
917 for(p = prdptr[i]+1; *p >= NTBASE && pempty[*p-NTBASE] == EMPTY ; ++p)
919 /* we have a nontrivially empty nonterminal */
921 pempty[*prdptr[i]-NTBASE] = EMPTY;
922 /* got one ... try for another */
930 * generate the states
942 /* THIS IS FUNNY from the standpoint of portability
943 * it represents the magic moment when the mem0 array, which has
944 * been holding the productions, starts to hold item pointers, of a
946 * someday, alloc should be used to allocate all this stuff... for now, we
947 * accept that if pointers don't fit in integers, there is a problem...
950 pstate[0] = pstate[1] = (Item*)mem;
951 aryfil(clset.lset, tbitset, 0);
952 putitem(prdptr[0]+1, &clset);
955 pstate[2] = pstate[1];
957 aryfil(amem, ACTSIZE, 0);
959 /* now, the main state generation loop */
963 if(tystate[i] != MUSTDO)
966 aryfil(temp1, nnonter+1, 0);
967 /* take state i, close it, and do gotos */
969 /* generate goto's */
976 if(pstate[i+1]-pstate[i] <= p-wsets)
977 tystate[i] = MUSTLOOKAHEAD;
982 /* this item contributes to the goto */
983 if(c == *(q->pitem)) {
984 putitem(q->pitem+1, &q->ws);
988 state(c); /* register new state */
990 temp1[c-NTBASE] = state(c);
992 if(gsdebug && foutput != 0) {
993 Bprint(foutput, "%d: ", i);
996 Bprint(foutput, "%s %d, ",
997 nontrst[j].name, temp1[j]);
998 Bprint(foutput, "\n");
1000 indgo[i] = apack(&temp1[1], nnonter-1) - 1;
1008 * generate the closure of state i
1016 int c, ch, work, k, *pi, **s, **t;
1020 /* first, copy kernel of state i to wsets */
1023 cwp->pitem = p->pitem;
1024 cwp->flag = 1; /* this item must get closed */
1026 cwp->ws.lset[k] = p->look->lset[k];
1030 /* now, go through the loop, closing each item */
1037 /* dot is before c */
1041 /* only interesting case is where . is before nonterminal */
1045 /* compute the lookahead */
1046 aryfil(clset.lset, tbitset, 0);
1048 /* find items involving c */
1050 if(v->flag == 1 && *(pi=v->pitem) == c) {
1054 while((ch = *++pi) > 0) {
1055 /* terminal symbol */
1057 SETBIT(clset.lset, ch);
1060 /* nonterminal symbol */
1061 setunion(clset.lset, pfirst[ch-NTBASE]->lset);
1062 if(!pempty[ch-NTBASE])
1066 setunion(clset.lset, v->ws.lset);
1070 * now loop over productions derived from c
1071 * c is now nonterminal number
1075 for(s = pres[c]; s < t; ++s) {
1077 * put these items into the closure
1081 /* yes, it is there */
1082 if(v->pitem == *s) {
1085 if(setunion(v->ws.lset, clset.lset))
1090 /* not there; make a new entry */
1091 if(cwp-wsets+1 >= WSETSIZE)
1092 error( "working set overflow");
1097 SETLOOP(k) cwp->ws.lset[k] = clset.lset[k];
1106 /* have computed closure; flags are reset; return */
1109 if(cldebug && foutput != 0) {
1110 Bprint(foutput, "\nState %d, nolook = %d\n", i, nolook);
1113 Bprint(foutput, "flag set!\n");
1115 Bprint(foutput, "\t%s", writem(u->pitem));
1117 Bprint(foutput, "\n");
1123 * decide if the lookahead set pointed to by p is known
1124 * return pointer to a perminent location for the set
1132 for(q = &lkst[nlset]; q-- > lkst;) {
1139 /* we have matched */
1145 if(nlset >= LSETSIZE)
1146 error("too many lookahead sets");
1148 q->lset[j] = p->lset[j];
1163 exits("interrupted");
1169 fprint(2, "usage: yacc [-Dn] [-vdS] [-o outputfile] [-s stem] grammar\n");
1174 setup(int argc, char *argv[])
1177 int i, j, lev, ty, ytab, *p;
1178 int vflag, dflag, stem;
1179 char actnm[8], *stemc, *s, dirbuf[128];
1195 yydebug = EARGF(usage());
1202 ytabc = EARGF(usage());
1212 error("illegal option: %c", ARGC());
1214 openup(stemc, dflag, vflag, ytab, ytabc);
1216 ftemp = Bopen(tempname = mktemp(ttempname), OWRITE);
1217 faction = Bopen(actname = mktemp(tactname), OWRITE);
1218 if(ftemp == 0 || faction == 0)
1219 error("cannot open temp file");
1221 error("no input file");
1223 if(infile[0] != '/' && getwd(dirbuf, sizeof dirbuf)!=nil){
1224 i = strlen(infile)+1+strlen(dirbuf)+1+10;
1227 snprint(s, i, "%s/%s", dirbuf, infile);
1232 finput = Bopen(infile, OREAD);
1234 error("cannot open '%s'", argv[0]);
1238 extval = PRIVATE; /* tokens start in unicode 'private use' */
1240 defin(1, "$accept");
1245 for(t = gettok(); t != MARK && t != ENDFILE;)
1252 if(gettok() != IDENTIFIER)
1253 error("bad %%start construction");
1254 start = chfind(1, tokname);
1259 if(gettok() != TYPENAME)
1260 error("bad syntax in %%type");
1266 if((t=chfind(1, tokname)) < NTBASE) {
1267 j = TYPE(toklev[t]);
1268 if(j != 0 && j != ty)
1269 error("type redeclaration of token %s",
1272 SETTYPE(toklev[t], ty);
1274 j = nontrst[t-NTBASE].value;
1275 if(j != 0 && j != ty)
1276 error("type redeclaration of nonterminal %s",
1277 nontrst[t-NTBASE].name );
1279 nontrst[t-NTBASE].value = ty;
1293 /* copy the union declaration to the output */
1304 /* nonzero means new prec. and assoc. */
1308 /* get identifiers so defined */
1311 /* there is a type defined */
1326 j = chfind(0, tokname);
1328 error("%s defined earlier as nonterminal", tokname);
1330 if(ASSOC(toklev[j]))
1331 error("redeclaration of precedence of %s", tokname);
1332 SETASC(toklev[j], lev);
1333 SETPLEV(toklev[j], i);
1337 error("redeclaration of type of %s", tokname);
1338 SETTYPE(toklev[j],ty);
1342 tokset[j].value = numbval;
1343 if(j < ndefout && j > 3)
1344 error("please define type number of %s earlier",
1361 error("syntax error");
1364 error("unexpected EOF before %%");
1367 Bprint(ftable, "extern int yyerrflag;\n");
1368 Bprint(ftable, "#ifndef YYMAXDEPTH\n");
1369 Bprint(ftable, "#define YYMAXDEPTH 150\n");
1370 Bprint(ftable, "#endif\n" );
1372 Bprint(ftable, "#ifndef YYSTYPE\n");
1373 Bprint(ftable, "#define YYSTYPE int\n");
1374 Bprint(ftable, "#endif\n");
1376 Bprint(ftable, "YYSTYPE yylval;\n");
1377 Bprint(ftable, "YYSTYPE yyval;\n");
1381 /* added production */
1384 /* if start is 0, we will overwrite with the lhs of the first rule */
1389 while((t=gettok()) == LCURLY)
1392 error("bad syntax on first rule");
1395 prdptr[0][1] = chfind(1, tokname);
1398 while(t != MARK && t != ENDFILE) {
1399 /* process a rule */
1400 rlines[nprod] = lineno;
1402 *mem++ = *prdptr[nprod-1];
1404 if(t == IDENTCOLON) {
1405 *mem = chfind(1, tokname);
1407 error("token illegal on LHS of grammar rule");
1410 error("illegal rule: missing semicolon or | ?");
1411 /* read rule body */
1415 while(t == IDENTIFIER) {
1416 *mem = chfind(1, tokname);
1418 levprd[nprod] = toklev[*mem];
1423 if(gettok() != IDENTIFIER)
1424 error("illegal %%prec syntax");
1425 j = chfind(2, tokname);
1427 error("nonterminal %s illegal after %%prec",
1428 nontrst[j-NTBASE].name);
1429 levprd[nprod] = toklev[j];
1433 levprd[nprod] |= ACTFLAG;
1434 Bprint(faction, "\ncase %d:", nprod);
1435 cpyact(mem-prdptr[nprod]-1);
1436 Bprint(faction, " break;");
1437 if((t=gettok()) == IDENTIFIER) {
1439 /* action within rule... */
1440 sprint(actnm, "$$%d", nprod);
1442 /* make it a nonterminal */
1443 j = chfind(1, actnm);
1446 * the current rule will become rule number nprod+1
1447 * move the contents down, and make room for the null
1449 for(p = mem; p >= prdptr[nprod]; --p)
1453 /* enter null production for action */
1458 /* update the production information */
1459 levprd[nprod+1] = levprd[nprod] & ~ACTFLAG;
1460 levprd[nprod] = ACTFLAG;
1461 if(++nprod >= NPROD)
1462 error("more than %d rules", NPROD);
1465 /* make the action appear in the original rule */
1468 /* get some more of the rule */
1477 /* check that default action is reasonable */
1478 if(ntypes && !(levprd[nprod]&ACTFLAG) && nontrst[*prdptr[nprod]-NTBASE].value) {
1480 /* no explicit action, LHS has value */
1483 tempty = prdptr[nprod][1];
1485 error("must return a value, since LHS has a type");
1487 if(tempty >= NTBASE)
1488 tempty = nontrst[tempty-NTBASE].value;
1490 tempty = TYPE(toklev[tempty]);
1491 if(tempty != nontrst[*prdptr[nprod]-NTBASE].value)
1492 error("default action causes potential type clash");
1496 error("more than %d rules", NPROD);
1497 prdptr[nprod] = mem;
1501 /* end of all rules */
1506 Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile);
1507 while((c=Bgetrune(finput)) != Beof)
1508 Bputrune(ftable, c);
1514 * finish action routine
1521 Bprint(ftable, "#define YYEOFCODE %d\n", 1);
1522 Bprint(ftable, "#define YYERRCODE %d\n", 2);
1526 * define s to be a terminal if t=0
1527 * or a nonterminal if t=1
1530 defin(int nt, char *s)
1538 if(nnonter >= NNONTERM)
1539 error("too many nonterminals, limit %d",NNONTERM);
1540 nontrst[nnonter].name = cstash(s);
1541 return NTBASE + nnonter;
1544 /* must be a token */
1546 if(ntokens >= NTERMS)
1547 error("too many terminals, limit %d", NTERMS);
1548 tokset[ntokens].name = cstash(s);
1550 /* establish value for token */
1551 /* single character literal */
1553 val = chartorune(&rune, &s[1]);
1560 /* escape sequence */
1561 if(s[0] == ' ' && s[1] == '\\') {
1563 /* single character escape sequence */
1565 case 'n': val = '\n'; break;
1566 case 'r': val = '\r'; break;
1567 case 'b': val = '\b'; break;
1568 case 't': val = '\t'; break;
1569 case 'f': val = '\f'; break;
1570 case '\'': val = '\''; break;
1571 case '"': val = '"'; break;
1572 case '\\': val = '\\'; break;
1573 default: error("invalid escape");
1579 if(s[2] >= '0' && s[2] <= '7') {
1585 error("illegal \\nnn construction");
1586 val = 64*s[2] + 8*s[3] + s[4] - 73*'0';
1588 error("'\\000' is illegal");
1591 error("unknown escape");
1596 tokset[ntokens].value = val;
1597 toklev[ntokens] = 0;
1602 * write out the defines (at the end of the declaration section)
1608 char sar[NAMESIZE+10];
1610 for(i=ndefout; i<=ntokens; i++) {
1612 c = tokset[i].name[0];
1613 if(c != ' ' && c != '$') {
1614 Bprint(ftable, "#define %s %d\n",
1615 tokset[i].name, tokset[i].value);
1617 Bprint(fdefine, "#define\t%s\t%d\n",
1618 tokset[i].name, tokset[i].value);
1621 ndefout = ntokens+1;
1622 if(last && fdebug) {
1623 Bprint(fdebug, "char* yytoknames[] =\n{\n");
1625 if(tokset[i].name) {
1626 chcopy(sar, tokset[i].name);
1627 Bprint(fdebug, "\t\"%s\",\n", sar);
1630 Bprint(fdebug, "\t0,\n");
1632 Bprint(fdebug, "};\n");
1643 if(cnamp >= &cnames[cnamsz])
1644 error("too many characters in id's and literals");
1656 int i, base, match, reserve;
1657 static int peekline;
1663 c = Bgetrune(finput);
1664 while(c == ' ' || c == '\n' || c == '\t' || c == '\f') {
1667 c = Bgetrune(finput);
1672 lineno += skipcom();
1684 /* get, and look up, a type name (union member name) */
1686 while((c=Bgetrune(finput)) != '>' && c >= 0 && c != '\n') {
1688 c = runetochar(&tokname[i], &rune);
1693 error("unterminated < ... > clause");
1695 for(i=1; i<=ntypes; i++)
1696 if(!strcmp(typeset[i], tokname)) {
1702 typeset[numbval] = cstash(tokname);
1711 c = Bgetrune(finput);
1712 if(c == '\n' || c <= 0)
1713 error("illegal or missing ' or \"" );
1718 c = Bgetrune(finput);
1723 c = runetochar(&tokname[i], &rune);
1731 switch(c = Bgetrune(finput)) {
1732 case '0': return TERM;
1733 case '<': return LEFT;
1734 case '2': return BINARY;
1735 case '>': return RIGHT;
1737 case '\\': return MARK;
1738 case '=': return PREC;
1739 case '{': return LCURLY;
1740 default: reserve = 1;
1747 base = (c=='0')? 8: 10;
1748 for(c = Bgetrune(finput); isdigit(c); c = Bgetrune(finput))
1749 numbval = numbval*base + (c-'0');
1753 if(islower(c) || isupper(c) || c=='_' || c=='.' || c=='$') {
1755 while(islower(c) || isupper(c) || isdigit(c) ||
1756 c == '-' || c=='_' || c=='.' || c=='$') {
1757 if(reserve && isupper(c))
1760 c = runetochar(&tokname[i], &rune);
1763 c = Bgetrune(finput);
1771 /* find a reserved word */
1773 for(c=0; resrv[c].name; c++)
1774 if(strcmp(tokname, resrv[c].name) == 0)
1775 return resrv[c].value;
1776 error("invalid escape, or illegal reserved word: %s", tokname);
1779 /* look ahead to distinguish IDENTIFIER from IDENTCOLON */
1780 c = Bgetrune(finput);
1781 while(c == ' ' || c == '\t'|| c == '\n' || c == '\f' || c == '/') {
1784 /* look for comments */
1786 peekline += skipcom();
1787 c = Bgetrune(finput);
1796 * determine the type of a symbol
1804 v = nontrst[t-NTBASE].value;
1806 v = TYPE(toklev[t]);
1808 error("must specify type for %s", (t>=NTBASE)?
1809 nontrst[t-NTBASE].name: tokset[t].name);
1814 chfind(int t, char *s)
1821 if(!strcmp(s, tokset[i].name))
1824 if(!strcmp(s, nontrst[i].name))
1827 /* cannot find name */
1829 error("%s should have been defined earlier", s);
1834 * copy the union declaration to the output, and the define file if present
1842 Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile);
1843 Bprint(ftable, "typedef union ");
1845 Bprint(fdefine, "\ntypedef union ");
1849 if((c=Bgetrune(finput)) == Beof)
1850 error("EOF encountered while processing %%union");
1851 Bputrune(ftable, c);
1853 Bputrune(fdefine, c);
1864 /* we are finished copying */
1866 Bprint(ftable, " YYSTYPE;\n");
1868 Bprint(fdefine, "\tYYSTYPE;\nextern\tYYSTYPE\tyylval;\n");
1876 * copies code between \{ and \}
1884 c = Bgetrune(finput);
1886 c = Bgetrune(finput);
1889 Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile);
1892 if((c=Bgetrune(finput)) == '}')
1894 Bputc(ftable, '\\');
1897 if((c=Bgetrune(finput)) == '}')
1901 Bputrune(ftable, c);
1904 c = Bgetrune(finput);
1906 error("eof before %%}");
1910 * skip over comments
1911 * skipcom is called after reading a '/'
1919 /* i is the number of lines skipped */
1921 c = Bgetrune(finput);
1922 if(c == '/'){ /* C++ //: skip to end of line */
1923 while((c = Bgetrune(finput)) != Beof)
1926 }else if(c == '*'){ /* normal C comment */
1927 while((c = Bgetrune(finput)) != Beof) {
1929 if((c = Bgetrune(finput)) == '/')
1935 error("illegal comment");
1937 error("EOF inside comment");
1942 * copy C action to the next ; or closing }
1948 int brac, match, j, s, fnd, tok;
1950 Bprint(faction, "\n#line\t%d\t\"%s\"\n", lineno, infile);
1954 c = Bgetrune(finput);
1959 Bputrune(faction, c);
1971 c = Bgetrune(finput);
1973 /* type description */
1976 if(gettok() != TYPENAME)
1977 error("bad syntax on $<ident> clause");
1979 c = Bgetrune(finput);
1982 Bprint(faction, "yyval");
1984 /* put out the proper tag... */
1987 tok = fdtype(*prdptr[nprod]);
1988 Bprint(faction, ".%s", typeset[tok]);
1994 c = Bgetrune(finput);
2000 c = Bgetrune(finput);
2005 error("Illegal use of $%d", j+offset);
2008 Bprint(faction, "yypt[-%d].yyv", -j);
2010 /* put out the proper tag */
2012 if(j+offset <= 0 && tok < 0)
2013 error("must specify type of $%d", j+offset);
2015 tok = fdtype(prdptr[nprod][j+offset]);
2016 Bprint(faction, ".%s", typeset[tok]);
2020 if(isupper(c) || islower(c) || c == '_' || c == '.') {
2021 int tok; /* tok used oustide for type info */
2023 /* look for $name */
2025 if(gettok() != IDENTIFIER)
2026 error("$ must be followed by an identifier");
2027 tok = chfind(2, tokname);
2028 if((c = Bgetrune(finput)) != '#') {
2032 if(gettok() != NUMBER) {
2033 error("# must be followed by number");
2037 for(j=1; j<=offset; ++j)
2038 if(tok == prdptr[nprod][j]) {
2044 error("$name or $name#number not found");
2046 Bputc(faction, '$');
2048 Bputc(faction, '-');
2055 Bputrune(faction, c);
2059 /* look for comments */
2060 Bputrune(faction, c);
2061 c = Bgetrune(finput);
2065 /* it really is a comment */
2066 Bputrune(faction, c);
2067 c = Bgetrune(finput);
2070 Bputrune(faction, c);
2071 if((c=Bgetrune(finput)) == '/')
2074 Bputrune(faction, c);
2077 c = Bgetrune(finput);
2079 error("EOF inside comment");
2082 /* character constant */
2087 /* character string */
2091 Bputrune(faction, c);
2092 while(c = Bgetrune(finput)) {
2094 Bputrune(faction, c);
2095 c = Bgetrune(finput);
2102 error("newline in string or char. const.");
2103 Bputrune(faction, c);
2105 error("EOF in string or character constant");
2108 error("action does not terminate");
2116 Bputrune(faction, c);
2121 openup(char *stem, int dflag, int vflag, int ytab, char *ytabc)
2126 snprint(buf, sizeof buf, "%s.%s", stem, FILEU);
2127 foutput = Bopen(buf, OWRITE);
2129 error("cannot open %s", buf);
2132 snprint(buf, sizeof buf, "%s.%s", stem, FILEDEBUG);
2133 if((fdebug = Bopen(buf, OWRITE)) == 0)
2134 error("can't open %s", buf);
2137 snprint(buf, sizeof buf, "%s.%s", stem, FILED);
2138 fdefine = Bopen(buf, OWRITE);
2140 error("can't create %s", buf);
2143 snprint(buf, sizeof buf, "%s.%s", stem, OFILE);
2145 strecpy(buf, buf+sizeof buf, ytabc);
2146 ftable = Bopen(buf, OWRITE);
2148 error("cannot open table file %s", buf);
2152 * print the output for the states
2160 Bprint(ftable, "short yyexca[] =\n{");
2162 Bprint(fdebug, "char* yystates[] =\n{\n");
2164 /* output the stuff for state i */
2166 nolook = tystate[i]!=MUSTLOOKAHEAD;
2169 /* output actions */
2171 aryfil(temp1, ntokens+nnonter+1, 0);
2174 if(c > 1 && c < NTBASE && temp1[c] == 0) {
2176 if(c == *(v->pitem))
2177 putitem(v->pitem+1, (Lkset*)0);
2178 temp1[c] = state(c);
2180 if(c > NTBASE && temp1[(c -= NTBASE) + ntokens] == 0)
2181 temp1[c+ntokens] = amem[indgo[i]+c];
2184 temp1[1] = ACCEPTCODE;
2186 /* now, we have the shifts; look at the reductions */
2195 if(BIT(u->ws.lset, k)) {
2199 if(temp1[k] < 0) { /* reduce/reduce conflict */
2202 "\n%d: reduce/reduce conflict"
2203 " (red'ns %d and %d ) on %s",
2204 i, -temp1[k], lastred,
2206 if(-temp1[k] > lastred)
2207 temp1[k] = -lastred;
2210 /* potential shift/reduce conflict */
2211 precftn( lastred, k, i );
2219 Bprint(fdebug, "};\n");
2220 Bprint(ftable, "};\n");
2221 Bprint(ftable, "#define YYNPROD %d\n", nprod);
2222 Bprint(ftable, "#define YYPRIVATE %d\n", PRIVATE);
2224 Bprint(ftable, "#define yydebug %s\n", yydebug);
2228 * pack state i from temp1 into amem
2231 apack(int *p, int n)
2233 int *pp, *qq, *rr, off, *q, *r;
2235 /* we don't need to worry about checking because
2236 * we will only look at entries known to be there...
2237 * eliminate leading and trailing 0's
2241 for(pp = p, off = 0; *pp == 0 && pp <= q; ++pp, --off)
2248 /* now, find a place for the elements from p to q, inclusive */
2249 r = &amem[ACTSIZE-1];
2250 for(rr = amem; rr <= r; rr++, off++) {
2251 for(qq = rr, pp = p; pp <= q; pp++, qq++)
2253 if(*pp != *qq && *qq != 0)
2256 /* we have found an acceptable k */
2257 if(pkdebug && foutput != 0)
2258 Bprint(foutput, "off = %d, k = %d\n", off, (int)(rr-amem));
2259 for(qq = rr, pp = p; pp <= q; pp++, qq++)
2262 error("action table overflow");
2267 if(pkdebug && foutput != 0)
2268 for(pp = amem; pp <= memp; pp += 10) {
2269 Bprint(foutput, "\t");
2270 for(qq = pp; qq <= pp+9; qq++)
2271 Bprint(foutput, "%d ", *qq);
2272 Bprint(foutput, "\n");
2277 error("no space in action table");
2282 * output the gotos for the nontermninals
2287 int i, j, k, best, count, cbest, times;
2289 /* mark begining of gotos */
2290 Bprint(ftemp, "$\n");
2291 for(i = 1; i <= nnonter; i++) {
2294 /* find the best one to make default */
2298 /* is j the most frequent */
2299 for(j = 0; j <= nstate; j++) {
2302 if(tystate[j] == best)
2305 /* is tystate[j] the most frequent */
2308 for(k = j; k <= nstate; k++)
2309 if(tystate[k] == cbest)
2317 /* best is now the default entry */
2318 zzgobest += times-1;
2319 for(j = 0; j <= nstate; j++)
2320 if(tystate[j] != 0 && tystate[j] != best) {
2321 Bprint(ftemp, "%d,%d,", j, tystate[j]);
2325 /* now, the default */
2329 Bprint(ftemp, "%d\n", best);
2334 * output the gotos for nonterminal c
2343 /* first, find nonterminals with gotos on c */
2344 aryfil(temp1, nnonter+1, 0);
2351 /* cc is a nonterminal */
2352 if((cc=prdptr[i][1]-NTBASE) >= 0)
2353 /* cc has a goto on c */
2354 if(temp1[cc] != 0) {
2356 /* thus, the left side of production i does too */
2357 cc = *prdptr[i]-NTBASE;
2358 if(temp1[cc] == 0) {
2365 /* now, we have temp1[c] = 1 if a goto on c in closure of cc */
2366 if(g2debug && foutput != 0) {
2367 Bprint(foutput, "%s: gotos on ", nontrst[c].name);
2370 Bprint(foutput, "%s ", nontrst[i].name);
2371 Bprint(foutput, "\n");
2374 /* now, go through and put gotos into tystate */
2375 aryfil(tystate, nstate, 0);
2378 if((cc = *p->pitem) >= NTBASE)
2379 /* goto on c is possible */
2380 if(temp1[cc-NTBASE]) {
2381 tystate[i] = amem[indgo[i]+c];
2387 * decide a shift/reduce conflict by precedence.
2388 * r is a rule number, t a token number
2389 * the conflict is in state s
2390 * temp1[t] is changed to reflect the action
2393 precftn(int r, int t, int s)
2399 if(PLEVEL(lt) == 0 || PLEVEL(lp) == 0) {
2404 "\n%d: shift/reduce conflict (shift %d(%d), red'n %d(%d)) on %s",
2405 s, temp1[t], PLEVEL(lt), r, PLEVEL(lp), symnam(t));
2409 if(PLEVEL(lt) == PLEVEL(lp))
2412 if(PLEVEL(lt) > PLEVEL(lp))
2413 action = RASC; /* shift */
2415 action = LASC; /* reduce */
2417 case BASC: /* error action */
2420 case LASC: /* reduce */
2428 * temp1 has the actions, lastred the default
2433 int p, p0, p1, ntimes, tred, count, j, flag;
2435 /* find the best choice for lastred */
2441 if(temp1[j]+lastred == 0)
2443 /* count the number of appearances of temp1[j] */
2446 levprd[tred] |= REDFLAG;
2448 if(temp1[p]+tred == 0)
2450 if(count > ntimes) {
2457 * for error recovery, arrange that, if there is a shift on the
2458 * error recovery token, `error', that the default be the error action
2463 /* clear out entries in temp1 which equal lastred */
2465 if(temp1[p]+lastred == 0)
2469 defact[i] = lastred;
2472 if((p1=temp1[p0]) != 0) {
2477 if(p1 == ACCEPTCODE) {
2485 Bprint(ftable, "-1, %d,\n", i);
2486 Bprint(ftable, "\t%d, %d,\n", p0, p1);
2490 Bprint(ftemp, "%d,%d,", p0, p1);
2495 Bprint(ftable, "\t-2, %d,\n", lastred);
2497 Bprint(ftemp, "\n");
2512 Bprint(fdebug, " 0, /*%d*/\n", i);
2514 Bprint(fdebug, " \"");
2516 Bprint(fdebug, "%s\\n", writem(pp->pitem));
2517 if(tystate[i] == MUSTLOOKAHEAD)
2518 WSLOOP(wsets + (pstate[i+1] - pstate[i]), u)
2520 Bprint(fdebug, "%s\\n", writem(u->pitem));
2521 Bprint(fdebug, "\", /*%d*/\n", i);
2526 Bprint(foutput, "\nstate %d\n", i);
2528 Bprint(foutput, "\t%s\n", writem(pp->pitem));
2529 if(tystate[i] == MUSTLOOKAHEAD)
2530 /* print out empty productions in closure */
2531 WSLOOP(wsets+(pstate[i+1]-pstate[i]), u)
2533 Bprint(foutput, "\t%s\n", writem(u->pitem));
2535 /* check for state equal to another */
2537 if((j1=temp1[j0]) != 0) {
2538 Bprint(foutput, "\n\t%s ", symnam(j0));
2539 /* shift, error, or accept */
2541 if(j1 == ACCEPTCODE)
2542 Bprint(foutput, "accept");
2545 Bprint(foutput, "error");
2547 Bprint(foutput, "shift %d", j1);
2549 Bprint(foutput, "reduce %d (src line %d)", -j1, rlines[-j1]);
2552 /* output the final production */
2554 Bprint(foutput, "\n\t. reduce %d (src line %d)\n\n",
2555 lastred, rlines[lastred]);
2557 Bprint(foutput, "\n\t. error\n\n");
2559 /* now, output nonterminal actions */
2561 for(j0 = 1; j0 <= nnonter; j0++) {
2564 Bprint(foutput, "\t%s goto %d\n", symnam(j0+NTBASE), temp1[j1]);
2569 warray(char *s, int *v, int n)
2573 Bprint(ftable, "short %s[] =\n{", s);
2576 Bprint(ftable, "\n");
2577 Bprint(ftable, "%4d", v[i]);
2580 Bprint(ftable, "\n};\n");
2583 Bprint(ftable, ",");
2588 * in order to free up the mem and amem arrays for the optimizer,
2589 * and still be able to output yyr1, etc., after the sizes of
2590 * the action array is known, we hide the nonterminals
2591 * derived by productions in levprd.
2602 if(!(levprd[i] & REDFLAG)) {
2605 Bprint(foutput, "Rule not reduced: %s\n", writem(prdptr[i]));
2607 levprd[i] = *prdptr[i] - NTBASE;
2610 print("%d rules never reduced\n", j);
2616 int i, *p, j, k, *q;
2618 /* read the arrays from tempfile and set parameters */
2619 finput = Bopen(tempname, OREAD);
2621 error("optimizer cannot open tempfile");
2632 temp1[nstate] = pmem - mem0;
2638 error("bad tempfile");
2644 temp1[nstate] = yypgo[0] = pmem - mem0;
2649 yypgo[nnonter] = pmem-mem0;
2655 error("bad tempfile");
2660 yypgo[nnonter--] = pmem - mem0;
2661 for(i = 0; i < nstate; i++) {
2664 q = mem0 + temp1[i+1];
2665 for(p = mem0 + temp1[i]; p < q ; p += 2) {
2671 /* nontrivial situation */
2673 /* j is now the range */
2674 /* j -= k; /* call scj */
2678 tystate[i] = (temp1[i+1]-temp1[i]) + 2*j;
2683 /* initialize ggreed table */
2684 for(i = 1; i <= nnonter; i++) {
2688 /* minimum entry index is always 0 */
2689 q = mem0 + yypgo[i+1] - 1;
2690 for(p = mem0+yypgo[i]; p < q ; p += 2) {
2695 ggreed[i] = ggreed[i] + 2*j;
2700 /* now, prepare to put the shift actions into the amem array */
2701 for(i = 0; i < ACTSIZE; i++)
2704 for(i = 0; i < nstate; i++) {
2705 if(tystate[i] == 0 && adb > 1)
2706 Bprint(ftable, "State %d: null\n", i);
2709 while((i = nxti()) != NOMORE)
2715 /* print amem array */
2717 for(p = amem; p <= maxa; p += 10) {
2718 Bprint(ftable, "%4d ", (int)(p-amem));
2719 for(i = 0; i < 10; ++i)
2720 Bprint(ftable, "%4d ", p[i]);
2721 Bprint(ftable, "\n");
2724 /* write out the output appropriate to the language */
2733 int *p, *r, *s, *q1, *q2;
2735 /* enter gotos on nonterminal i into array amem */
2738 q2 = mem0+ yypgo[i+1] - 1;
2739 q1 = mem0 + yypgo[i];
2741 /* now, find amem place for it */
2742 for(p = amem; p < &amem[ACTSIZE]; p++) {
2745 for(r = q1; r < q2; r += 2) {
2750 if((maxa = s) > &amem[ACTSIZE])
2751 error("a array overflow");
2753 /* we have found amem spot */
2756 if((maxa = p) > &amem[ACTSIZE])
2757 error("a array overflow");
2758 for(r = q1; r < q2; r += 2) {
2764 Bprint(ftable, "Nonterminal %d, entry at %d\n", i, pgo[i]);
2769 error("cannot place goto %d\n", i);
2775 int *r, *s, n, flag, j, *q1, *q2;
2779 /* enter state i into the amem array */
2780 q2 = mem0+temp1[i+1];
2782 /* find an acceptable place */
2783 for(n = -maxoff; n < ACTSIZE; n++) {
2785 for(r = q1; r < q2; r += 2) {
2786 if((s = *r + n + amem) < amem)
2795 /* check that the position equals another only if the states are identical */
2796 for(j=0; j<nstate; j++) {
2799 /* we have some disagreement */
2802 if(temp1[j+1]+temp1[i] == temp1[j]+temp1[i+1]) {
2804 /* states are equal */
2808 "State %d: entry at %d equals state %d\n",
2813 /* we have some disagreement */
2818 for(r = q1; r < q2; r += 2) {
2819 if((s = *r+n+amem) >= &amem[ACTSIZE])
2820 error("out of space in optimizer a array");
2823 if(*s != 0 && *s != r[1])
2824 error("clobber of a array, pos'n %d, by %d", s-amem, r[1]);
2829 Bprint(ftable, "State %d: entry at %d\n", i, indgo[i]);
2833 error("Error; failure to place state %d\n", i);
2846 for(i = 1; i <= nnonter; i++)
2847 if(ggreed[i] >= max) {
2851 for(i = 0; i < nstate; ++i)
2852 if(tystate[i] >= max) {
2857 Bprint(ftable, "nxti = %d, max = %d\n", maxi, max);
2875 for(p = maxa; p >= amem; p--)
2879 Bprint(foutput, "Optimizer space used: input %d/%d, output %d/%d\n",
2880 (int)(pmem-mem0+1), MEMSIZE, (int)(maxa-amem+1), ACTSIZE);
2881 Bprint(foutput, "%d table entries, %d zero\n", (int)(maxa-amem+1), i);
2882 Bprint(foutput, "maximum spread: %d, maximum offset: %d\n", maxspr, maxoff);
2886 * this version is for C
2887 * write out the optimized parser
2892 Bprint(ftable, "#define\tYYLAST\t%d\n", (int)(maxa-amem+1));
2893 arout("yyact", amem, (maxa-amem)+1);
2894 arout("yypact", indgo, nstate);
2895 arout("yypgo", pgo, nnonter+1);
2899 arout(char *s, int *v, int n)
2903 Bprint(ftable, "short %s[] =\n{", s);
2904 for(i = 0; i < n;) {
2906 Bprint(ftable, "\n");
2907 Bprint(ftable, "%4d", v[i]);
2910 Bprint(ftable, "\n};\n");
2912 Bprint(ftable, ",");
2917 * read and convert an integer from the standard input
2918 * return the terminating character
2919 * blanks, tabs, and newlines are ignored
2928 while((c=Bgetrune(finput)) != Beof) {
2930 val = val*10 + c-'0';
2942 if(pmem >= &mem0[MEMSIZE])
2943 error("out of space");