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+UTFmax+1]; /* 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 Blethal(finput, nil);
406 warray("yyr1", levprd, nprod);
407 aryfil(temp1, nprod, 0);
409 temp1[i] = prdptr[i+1]-prdptr[i]-2;
410 warray("yyr2", temp1, nprod);
412 aryfil(temp1, nstate, -1000);
414 for(j=tstates[i]; j!=0; j=mstates[j])
417 for(j=ntstates[i]; j!=0; j=mstates[j])
419 warray("yychk", temp1, nstate);
420 warray("yydef", defact, nstate);
422 /* put out token translation tables */
423 /* table 1 has 0-256 */
424 aryfil(temp1, 256, 0);
428 if(j >= 0 && j < 256) {
430 print("yacc bug -- cant have 2 different Ts with same value\n");
431 print(" %s and %s\n", tokset[i].name, tokset[temp1[j]].name);
439 warray("yytok1", temp1, c+1);
441 /* table 2 has PRIVATE-PRIVATE+256 */
442 aryfil(temp1, 256, 0);
445 j = tokset[i].value - PRIVATE;
446 if(j >= 0 && j < 256) {
448 print("yacc bug -- cant have 2 different Ts with same value\n");
449 print(" %s and %s\n", tokset[i].name, tokset[temp1[j]].name);
457 warray("yytok2", temp1, c+1);
459 /* table 3 has everything else */
460 Bprint(ftable, "long yytok3[] =\n{\n");
464 if(j >= 0 && j < 256)
466 if(j >= PRIVATE && j < 256+PRIVATE)
469 Bprint(ftable, "%4d,%4d,", j, i);
472 Bprint(ftable, "\n");
474 Bprint(ftable, "%4d\n};\n", 0);
476 /* copy parser text */
477 Bprint(ftable, "\n#line\t1\t\"%s\"\n", parser);
478 while((c=Bgetrune(finput)) != Beof) {
480 if((c = Bgetrune(finput)) != 'A')
481 Bputrune(ftable, '$');
482 else { /* copy actions */
483 faction = Bopen(actname, OREAD);
485 error("cannot reopen action tempfile");
486 Blethal(faction, nil);
487 while((c=Bgetrune(faction)) != Beof)
491 c = Bgetrune(finput);
500 * copies string q into p, returning next free char ptr
503 chcopy(char* p, char* q)
518 * creates output string for item pointed to by pp
524 static char sarr[ISIZE];
530 q = chcopy(sarr, nontrst[*p-NTBASE].name);
542 q = chcopy(q, symnam(i));
543 if(q > &sarr[ISIZE-30])
544 error("item too big");
547 /* an item calling for a reduction */
551 sprint(q, "%d)", -i);
557 * return a pointer to the name of symbol i
564 cp = (i >= NTBASE)? nontrst[i-NTBASE].name: tokset[i].name;
571 * output the summary on y.output
578 Bprint(foutput, "\n%d/%d terminals, %d/%d nonterminals\n",
579 ntokens, NTERMS, nnonter, NNONTERM);
580 Bprint(foutput, "%d/%d grammar rules, %d/%d states\n",
581 nprod, NPROD, nstate, NSTATES);
582 Bprint(foutput, "%d shift/reduce, %d reduce/reduce conflicts reported\n",
584 Bprint(foutput, "%d/%d working sets used\n",
585 (int)(zzcwp-wsets), WSETSIZE);
586 Bprint(foutput, "memory: states,etc. %d/%d, parser %d/%d\n",
587 (int)(zzmemsz-mem0), MEMSIZE, (int)(memp-amem), ACTSIZE);
588 Bprint(foutput, "%d/%d distinct lookahead sets\n", nlset, LSETSIZE);
589 Bprint(foutput, "%d extra closures\n", zzclose - 2*nstate);
590 Bprint(foutput, "%d shift entries, %d exceptions\n", zzacent, zzexcp);
591 Bprint(foutput, "%d goto entries\n", zzgoent);
592 Bprint(foutput, "%d entries saved by goto default\n", zzgobest);
594 if(zzsrconf != 0 || zzrrconf != 0) {
595 print("\nconflicts: ");
597 print("%d shift/reduce", zzsrconf);
598 if(zzsrconf && zzrrconf)
601 print("%d reduce/reduce", zzrrconf);
615 * write out error comment -- NEEDS WORK
622 fprint(2, "\n fatal error:");
623 fprint(2, s, (&s)[1]);
624 fprint(2, ", %s:%d\n", infile, lineno);
633 * set elements 0 through n-1 to c
636 aryfil(int *v, int n, int c)
645 * set a to the union of a and b
646 * return 1 if b is not a subset of a, 0 otherwise
649 setunion(int *a, int *b)
672 Bprint(foutput, "\tNULL");
674 Bprint(foutput, " { ");
677 Bprint(foutput, "%s ", symnam(j));
678 Bprint(foutput, "}");
683 * compute an array with the beginnings of productions yielding given nonterminals
684 * The array pres points to these lists
685 * the array pyield has the lists: the total size is only NPROD+1
691 static int *pyield[NPROD];
697 fatfl = 0; /* make undefined symbols nonfatal */
700 *pmem++ = prdptr[j]+1;
702 error("nonterminal %s not defined!", nontrst[i].name);
711 if(pmem != &pyield[nprod])
712 error("internal Yacc error: pyield %d", pmem-&pyield[nprod]);
716 * compute an array with the first of nonterminals
721 int *p, **s, i, **t, ch, changes;
723 zzcwp = &wsets[nnonter];
725 aryfil(wsets[i].ws.lset, tbitset, 0);
727 /* initially fill the sets */
728 for(s=pres[i]; s<t; ++s)
729 for(p = *s; (ch = *p) > 0; ++p) {
731 SETBIT(wsets[i].ws.lset, ch);
734 if(!pempty[ch-NTBASE])
739 /* now, reflect transitivity */
745 for(s = pres[i]; s < t; ++s)
746 for(p = *s; (ch = (*p-NTBASE)) >= 0; ++p) {
747 changes |= setunion(wsets[i].ws.lset, wsets[ch].ws.lset);
755 pfirst[i] = flset(&wsets[i].ws);
760 Bprint(foutput, "\n%s: ", nontrst[i].name);
762 Bprint(foutput, " %d\n", pempty[i]);
767 * sorts last state,and sees if it equals earlier ones. returns state number
772 Item *p1, *p2, *k, *l, *q1, *q2;
776 p2 = pstate[nstate+1];
778 return 0; /* null state */
780 for(k = p2-1; k > p1; k--) /* make k the biggest */
781 for(l = k-1; l >= p1; --l)
782 if(l->pitem > k->pitem) {
793 size1 = p2 - p1; /* size of state */
795 for(i = (c>=NTBASE)? ntstates[c-NTBASE]: tstates[c]; i != 0; i = mstates[i]) {
803 for(l = q1; l < q2; l++) {
804 if(l->pitem != k->pitem)
811 pstate[nstate+1] = pstate[nstate]; /* delete last state */
812 /* fix up lookaheads */
815 for(l = q1, k = p1; l < q2; ++l, ++k ) {
819 clset.lset[s] = l->look->lset[s];
820 if(setunion(clset.lset, k->look->lset)) {
822 /* register the new set */
823 l->look = flset( &clset );
830 error("yacc state/nolook error");
831 pstate[nstate+2] = p2;
832 if(nstate+1 >= NSTATES)
833 error("too many states");
835 mstates[nstate] = ntstates[c-NTBASE];
836 ntstates[c-NTBASE] = nstate;
838 mstates[nstate] = tstates[c];
841 tystate[nstate] = MUSTDO;
846 putitem(int *ptr, Lkset *lptr)
850 if(pidebug && foutput != 0)
851 Bprint(foutput, "putitem(%s), state %d\n", writem(ptr), nstate);
852 j = pstate[nstate+1];
855 j->look = flset(lptr);
856 pstate[nstate+1] = ++j;
857 if((int*)j > zzmemsz) {
859 if(zzmemsz >= &mem0[MEMSIZE])
860 error("out of state space");
865 * mark nonterminals which derive the empty string
866 * also, look for nonterminals which don't derive any token strings
874 /* first, use the array pempty to detect productions that can never be reduced */
875 /* set pempty to WHONOWS */
876 aryfil(pempty, nnonter+1, WHOKNOWS);
878 /* now, look at productions, marking nonterminals which derive something */
881 if(pempty[*prdptr[i] - NTBASE])
883 for(p = prdptr[i]+1; *p >= 0; ++p)
884 if(*p >= NTBASE && pempty[*p-NTBASE] == WHOKNOWS)
886 /* production can be derived */
888 pempty[*prdptr[i]-NTBASE] = OK;
893 /* now, look at the nonterminals, to see if they are all OK */
895 /* the added production rises or falls as the start symbol ... */
898 if(pempty[i] != OK) {
900 error("nonterminal %s never derives any token string", nontrst[i].name);
910 /* now, compute the pempty array, to see which nonterminals derive the empty string */
911 /* set pempty to WHOKNOWS */
912 aryfil( pempty, nnonter+1, WHOKNOWS);
914 /* loop as long as we keep finding empty nonterminals */
918 /* not known to be empty */
919 if(pempty[*prdptr[i]-NTBASE] == WHOKNOWS) {
920 for(p = prdptr[i]+1; *p >= NTBASE && pempty[*p-NTBASE] == EMPTY ; ++p)
922 /* we have a nontrivially empty nonterminal */
924 pempty[*prdptr[i]-NTBASE] = EMPTY;
925 /* got one ... try for another */
933 * generate the states
945 /* THIS IS FUNNY from the standpoint of portability
946 * it represents the magic moment when the mem0 array, which has
947 * been holding the productions, starts to hold item pointers, of a
949 * someday, alloc should be used to allocate all this stuff... for now, we
950 * accept that if pointers don't fit in integers, there is a problem...
953 pstate[0] = pstate[1] = (Item*)mem;
954 aryfil(clset.lset, tbitset, 0);
955 putitem(prdptr[0]+1, &clset);
958 pstate[2] = pstate[1];
960 aryfil(amem, ACTSIZE, 0);
962 /* now, the main state generation loop */
966 if(tystate[i] != MUSTDO)
969 aryfil(temp1, nnonter+1, 0);
970 /* take state i, close it, and do gotos */
972 /* generate goto's */
979 if(pstate[i+1]-pstate[i] <= p-wsets)
980 tystate[i] = MUSTLOOKAHEAD;
985 /* this item contributes to the goto */
986 if(c == *(q->pitem)) {
987 putitem(q->pitem+1, &q->ws);
991 state(c); /* register new state */
993 temp1[c-NTBASE] = state(c);
995 if(gsdebug && foutput != 0) {
996 Bprint(foutput, "%d: ", i);
999 Bprint(foutput, "%s %d, ",
1000 nontrst[j].name, temp1[j]);
1001 Bprint(foutput, "\n");
1003 indgo[i] = apack(&temp1[1], nnonter-1) - 1;
1011 * generate the closure of state i
1019 int c, ch, work, k, *pi, **s, **t;
1023 /* first, copy kernel of state i to wsets */
1026 cwp->pitem = p->pitem;
1027 cwp->flag = 1; /* this item must get closed */
1029 cwp->ws.lset[k] = p->look->lset[k];
1033 /* now, go through the loop, closing each item */
1040 /* dot is before c */
1044 /* only interesting case is where . is before nonterminal */
1048 /* compute the lookahead */
1049 aryfil(clset.lset, tbitset, 0);
1051 /* find items involving c */
1053 if(v->flag == 1 && *(pi=v->pitem) == c) {
1057 while((ch = *++pi) > 0) {
1058 /* terminal symbol */
1060 SETBIT(clset.lset, ch);
1063 /* nonterminal symbol */
1064 setunion(clset.lset, pfirst[ch-NTBASE]->lset);
1065 if(!pempty[ch-NTBASE])
1069 setunion(clset.lset, v->ws.lset);
1073 * now loop over productions derived from c
1074 * c is now nonterminal number
1078 for(s = pres[c]; s < t; ++s) {
1080 * put these items into the closure
1084 /* yes, it is there */
1085 if(v->pitem == *s) {
1088 if(setunion(v->ws.lset, clset.lset))
1093 /* not there; make a new entry */
1094 if(cwp-wsets+1 >= WSETSIZE)
1095 error( "working set overflow");
1100 SETLOOP(k) cwp->ws.lset[k] = clset.lset[k];
1109 /* have computed closure; flags are reset; return */
1112 if(cldebug && foutput != 0) {
1113 Bprint(foutput, "\nState %d, nolook = %d\n", i, nolook);
1116 Bprint(foutput, "flag set!\n");
1118 Bprint(foutput, "\t%s", writem(u->pitem));
1120 Bprint(foutput, "\n");
1126 * decide if the lookahead set pointed to by p is known
1127 * return pointer to a perminent location for the set
1135 for(q = &lkst[nlset]; q-- > lkst;) {
1142 /* we have matched */
1148 if(nlset >= LSETSIZE)
1149 error("too many lookahead sets");
1151 q->lset[j] = p->lset[j];
1166 exits("interrupted");
1172 fprint(2, "usage: yacc [-Dn] [-vdS] [-o outputfile] [-s stem] grammar\n");
1177 setup(int argc, char *argv[])
1180 int i, j, lev, ty, ytab, *p;
1181 int vflag, dflag, stem;
1182 char actnm[8], *stemc, *s, dirbuf[128];
1198 yydebug = EARGF(usage());
1205 ytabc = EARGF(usage());
1215 error("illegal option: %c", ARGC());
1217 openup(stemc, dflag, vflag, ytab, ytabc);
1219 ftemp = Bopen(tempname = mktemp(ttempname), OWRITE);
1220 faction = Bopen(actname = mktemp(tactname), OWRITE);
1221 if(ftemp == 0 || faction == 0)
1222 error("cannot open temp file");
1223 Blethal(ftemp, nil);
1224 Blethal(faction, nil);
1226 error("no input file");
1228 if(infile[0] != '/' && getwd(dirbuf, sizeof dirbuf)!=nil){
1229 i = strlen(infile)+1+strlen(dirbuf)+1+10;
1232 snprint(s, i, "%s/%s", dirbuf, infile);
1237 finput = Bopen(infile, OREAD);
1239 error("cannot open '%s'", argv[0]);
1240 Blethal(finput, nil);
1244 extval = PRIVATE; /* tokens start in unicode 'private use' */
1246 defin(1, "$accept");
1251 for(t = gettok(); t != MARK && t != ENDFILE;)
1258 if(gettok() != IDENTIFIER)
1259 error("bad %%start construction");
1260 start = chfind(1, tokname);
1265 if(gettok() != TYPENAME)
1266 error("bad syntax in %%type");
1272 if((t=chfind(1, tokname)) < NTBASE) {
1273 j = TYPE(toklev[t]);
1274 if(j != 0 && j != ty)
1275 error("type redeclaration of token %s",
1278 SETTYPE(toklev[t], ty);
1280 j = nontrst[t-NTBASE].value;
1281 if(j != 0 && j != ty)
1282 error("type redeclaration of nonterminal %s",
1283 nontrst[t-NTBASE].name );
1285 nontrst[t-NTBASE].value = ty;
1299 /* copy the union declaration to the output */
1310 /* nonzero means new prec. and assoc. */
1314 /* get identifiers so defined */
1317 /* there is a type defined */
1332 j = chfind(0, tokname);
1334 error("%s defined earlier as nonterminal", tokname);
1336 if(ASSOC(toklev[j]))
1337 error("redeclaration of precedence of %s", tokname);
1338 SETASC(toklev[j], lev);
1339 SETPLEV(toklev[j], i);
1343 error("redeclaration of type of %s", tokname);
1344 SETTYPE(toklev[j],ty);
1348 tokset[j].value = numbval;
1349 if(j < ndefout && j > 3)
1350 error("please define type number of %s earlier",
1367 error("syntax error");
1370 error("unexpected EOF before %%");
1373 Bprint(ftable, "extern int yyerrflag;\n");
1374 Bprint(ftable, "#ifndef YYMAXDEPTH\n");
1375 Bprint(ftable, "#define YYMAXDEPTH 150\n");
1376 Bprint(ftable, "#endif\n" );
1378 Bprint(ftable, "#ifndef YYSTYPE\n");
1379 Bprint(ftable, "#define YYSTYPE int\n");
1380 Bprint(ftable, "#endif\n");
1382 Bprint(ftable, "YYSTYPE yylval;\n");
1383 Bprint(ftable, "YYSTYPE yyval;\n");
1387 /* added production */
1390 /* if start is 0, we will overwrite with the lhs of the first rule */
1395 while((t=gettok()) == LCURLY)
1398 error("bad syntax on first rule");
1401 prdptr[0][1] = chfind(1, tokname);
1404 while(t != MARK && t != ENDFILE) {
1405 /* process a rule */
1406 rlines[nprod] = lineno;
1408 *mem++ = *prdptr[nprod-1];
1410 if(t == IDENTCOLON) {
1411 *mem = chfind(1, tokname);
1413 error("token illegal on LHS of grammar rule");
1416 error("illegal rule: missing semicolon or | ?");
1417 /* read rule body */
1421 while(t == IDENTIFIER) {
1422 *mem = chfind(1, tokname);
1424 levprd[nprod] = toklev[*mem];
1429 if(gettok() != IDENTIFIER)
1430 error("illegal %%prec syntax");
1431 j = chfind(2, tokname);
1433 error("nonterminal %s illegal after %%prec",
1434 nontrst[j-NTBASE].name);
1435 levprd[nprod] = toklev[j];
1439 levprd[nprod] |= ACTFLAG;
1440 Bprint(faction, "\ncase %d:", nprod);
1441 cpyact(mem-prdptr[nprod]-1);
1442 Bprint(faction, " break;");
1443 if((t=gettok()) == IDENTIFIER) {
1445 /* action within rule... */
1446 sprint(actnm, "$$%d", nprod);
1448 /* make it a nonterminal */
1449 j = chfind(1, actnm);
1452 * the current rule will become rule number nprod+1
1453 * move the contents down, and make room for the null
1455 for(p = mem; p >= prdptr[nprod]; --p)
1459 /* enter null production for action */
1464 /* update the production information */
1465 levprd[nprod+1] = levprd[nprod] & ~ACTFLAG;
1466 levprd[nprod] = ACTFLAG;
1467 if(++nprod >= NPROD)
1468 error("more than %d rules", NPROD);
1471 /* make the action appear in the original rule */
1474 /* get some more of the rule */
1483 /* check that default action is reasonable */
1484 if(ntypes && !(levprd[nprod]&ACTFLAG) && nontrst[*prdptr[nprod]-NTBASE].value) {
1486 /* no explicit action, LHS has value */
1489 tempty = prdptr[nprod][1];
1491 error("must return a value, since LHS has a type");
1493 if(tempty >= NTBASE)
1494 tempty = nontrst[tempty-NTBASE].value;
1496 tempty = TYPE(toklev[tempty]);
1497 if(tempty != nontrst[*prdptr[nprod]-NTBASE].value)
1498 error("default action causes potential type clash");
1502 error("more than %d rules", NPROD);
1503 prdptr[nprod] = mem;
1507 /* end of all rules */
1512 Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile);
1513 while((c=Bgetrune(finput)) != Beof)
1514 Bputrune(ftable, c);
1520 * finish action routine
1527 Bprint(ftable, "#define YYEOFCODE %d\n", 1);
1528 Bprint(ftable, "#define YYERRCODE %d\n", 2);
1532 * define s to be a terminal if t=0
1533 * or a nonterminal if t=1
1536 defin(int nt, char *s)
1544 if(nnonter >= NNONTERM)
1545 error("too many nonterminals, limit %d",NNONTERM);
1546 nontrst[nnonter].name = cstash(s);
1547 return NTBASE + nnonter;
1550 /* must be a token */
1552 if(ntokens >= NTERMS)
1553 error("too many terminals, limit %d", NTERMS);
1554 tokset[ntokens].name = cstash(s);
1556 /* establish value for token */
1557 /* single character literal */
1559 val = chartorune(&rune, &s[1]);
1566 /* escape sequence */
1567 if(s[0] == ' ' && s[1] == '\\') {
1569 /* single character escape sequence */
1571 case 'n': val = '\n'; break;
1572 case 'r': val = '\r'; break;
1573 case 'b': val = '\b'; break;
1574 case 't': val = '\t'; break;
1575 case 'f': val = '\f'; break;
1576 case '\'': val = '\''; break;
1577 case '"': val = '"'; break;
1578 case '\\': val = '\\'; break;
1579 default: error("invalid escape");
1585 if(s[2] >= '0' && s[2] <= '7') {
1591 error("illegal \\nnn construction");
1592 val = 64*s[2] + 8*s[3] + s[4] - 73*'0';
1594 error("'\\000' is illegal");
1597 error("unknown escape");
1602 tokset[ntokens].value = val;
1603 toklev[ntokens] = 0;
1608 * write out the defines (at the end of the declaration section)
1614 char sar[NAMESIZE+10];
1616 for(i=ndefout; i<=ntokens; i++) {
1618 c = tokset[i].name[0];
1619 if(c != ' ' && c != '$') {
1620 Bprint(ftable, "#define %s %d\n",
1621 tokset[i].name, tokset[i].value);
1623 Bprint(fdefine, "#define\t%s\t%d\n",
1624 tokset[i].name, tokset[i].value);
1627 ndefout = ntokens+1;
1628 if(last && fdebug) {
1629 Bprint(fdebug, "char* yytoknames[] =\n{\n");
1631 if(tokset[i].name) {
1632 chcopy(sar, tokset[i].name);
1633 Bprint(fdebug, "\t\"%s\",\n", sar);
1636 Bprint(fdebug, "\t0,\n");
1638 Bprint(fdebug, "};\n");
1649 if(cnamp >= &cnames[cnamsz])
1650 error("too many characters in id's and literals");
1662 int i, base, match, reserve;
1663 static int peekline;
1669 c = Bgetrune(finput);
1670 while(c == ' ' || c == '\n' || c == '\t' || c == '\f') {
1673 c = Bgetrune(finput);
1678 lineno += skipcom();
1690 /* get, and look up, a type name (union member name) */
1692 while((c=Bgetrune(finput)) != '>' && c >= 0 && c != '\n') {
1694 c = runetochar(&tokname[i], &rune);
1699 error("unterminated < ... > clause");
1701 for(i=1; i<=ntypes; i++)
1702 if(!strcmp(typeset[i], tokname)) {
1708 typeset[numbval] = cstash(tokname);
1717 c = Bgetrune(finput);
1718 if(c == '\n' || c <= 0)
1719 error("illegal or missing ' or \"" );
1724 c = Bgetrune(finput);
1729 c = runetochar(&tokname[i], &rune);
1737 switch(c = Bgetrune(finput)) {
1738 case '0': return TERM;
1739 case '<': return LEFT;
1740 case '2': return BINARY;
1741 case '>': return RIGHT;
1743 case '\\': return MARK;
1744 case '=': return PREC;
1745 case '{': return LCURLY;
1746 default: reserve = 1;
1753 base = (c=='0')? 8: 10;
1754 for(c = Bgetrune(finput); isdigit(c); c = Bgetrune(finput))
1755 numbval = numbval*base + (c-'0');
1759 if(islower(c) || isupper(c) || c=='_' || c=='.' || c=='$') {
1761 while(islower(c) || isupper(c) || isdigit(c) ||
1762 c == '-' || c=='_' || c=='.' || c=='$') {
1763 if(reserve && isupper(c))
1766 c = runetochar(&tokname[i], &rune);
1769 c = Bgetrune(finput);
1777 /* find a reserved word */
1779 for(c=0; resrv[c].name; c++)
1780 if(strcmp(tokname, resrv[c].name) == 0)
1781 return resrv[c].value;
1782 error("invalid escape, or illegal reserved word: %s", tokname);
1785 /* look ahead to distinguish IDENTIFIER from IDENTCOLON */
1786 c = Bgetrune(finput);
1787 while(c == ' ' || c == '\t'|| c == '\n' || c == '\f' || c == '/') {
1790 /* look for comments */
1792 peekline += skipcom();
1793 c = Bgetrune(finput);
1802 * determine the type of a symbol
1810 v = nontrst[t-NTBASE].value;
1812 v = TYPE(toklev[t]);
1814 error("must specify type for %s", (t>=NTBASE)?
1815 nontrst[t-NTBASE].name: tokset[t].name);
1820 chfind(int t, char *s)
1827 if(!strcmp(s, tokset[i].name))
1830 if(!strcmp(s, nontrst[i].name))
1833 /* cannot find name */
1835 error("%s should have been defined earlier", s);
1840 * copy the union declaration to the output, and the define file if present
1848 Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile);
1849 Bprint(ftable, "typedef union ");
1851 Bprint(fdefine, "\ntypedef union ");
1855 if((c=Bgetrune(finput)) == Beof)
1856 error("EOF encountered while processing %%union");
1857 Bputrune(ftable, c);
1859 Bputrune(fdefine, c);
1870 /* we are finished copying */
1872 Bprint(ftable, " YYSTYPE;\n");
1874 Bprint(fdefine, "\tYYSTYPE;\nextern\tYYSTYPE\tyylval;\n");
1882 * copies code between \{ and \}
1890 c = Bgetrune(finput);
1892 c = Bgetrune(finput);
1895 Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile);
1898 if((c=Bgetrune(finput)) == '}')
1900 Bputc(ftable, '\\');
1903 if((c=Bgetrune(finput)) == '}')
1907 Bputrune(ftable, c);
1910 c = Bgetrune(finput);
1912 error("eof before %%}");
1916 * skip over comments
1917 * skipcom is called after reading a '/'
1925 /* i is the number of lines skipped */
1927 c = Bgetrune(finput);
1928 if(c == '/'){ /* C++ //: skip to end of line */
1929 while((c = Bgetrune(finput)) != Beof)
1932 }else if(c == '*'){ /* normal C comment */
1933 while((c = Bgetrune(finput)) != Beof) {
1935 if((c = Bgetrune(finput)) == '/')
1941 error("illegal comment");
1943 error("EOF inside comment");
1948 * copy C action to the next ; or closing }
1954 int brac, match, j, s, fnd, tok;
1956 Bprint(faction, "\n#line\t%d\t\"%s\"\n", lineno, infile);
1960 c = Bgetrune(finput);
1965 Bputrune(faction, c);
1977 c = Bgetrune(finput);
1979 /* type description */
1982 if(gettok() != TYPENAME)
1983 error("bad syntax on $<ident> clause");
1985 c = Bgetrune(finput);
1988 Bprint(faction, "yyval");
1990 /* put out the proper tag... */
1993 tok = fdtype(*prdptr[nprod]);
1994 Bprint(faction, ".%s", typeset[tok]);
2000 c = Bgetrune(finput);
2006 c = Bgetrune(finput);
2011 error("Illegal use of $%d", j+offset);
2014 Bprint(faction, "yypt[-%d].yyv", -j);
2016 /* put out the proper tag */
2018 if(j+offset <= 0 && tok < 0)
2019 error("must specify type of $%d", j+offset);
2021 tok = fdtype(prdptr[nprod][j+offset]);
2022 Bprint(faction, ".%s", typeset[tok]);
2026 if(isupper(c) || islower(c) || c == '_' || c == '.') {
2027 int tok; /* tok used oustide for type info */
2029 /* look for $name */
2031 if(gettok() != IDENTIFIER)
2032 error("$ must be followed by an identifier");
2033 tok = chfind(2, tokname);
2034 if((c = Bgetrune(finput)) != '#') {
2038 if(gettok() != NUMBER) {
2039 error("# must be followed by number");
2043 for(j=1; j<=offset; ++j)
2044 if(tok == prdptr[nprod][j]) {
2050 error("$name or $name#number not found");
2052 Bputc(faction, '$');
2054 Bputc(faction, '-');
2061 Bputrune(faction, c);
2065 /* look for comments */
2066 Bputrune(faction, c);
2067 c = Bgetrune(finput);
2071 /* it really is a comment */
2072 Bputrune(faction, c);
2073 c = Bgetrune(finput);
2076 Bputrune(faction, c);
2077 if((c=Bgetrune(finput)) == '/')
2080 Bputrune(faction, c);
2083 c = Bgetrune(finput);
2085 error("EOF inside comment");
2088 /* character constant */
2093 /* character string */
2097 Bputrune(faction, c);
2098 while(c = Bgetrune(finput)) {
2100 Bputrune(faction, c);
2101 c = Bgetrune(finput);
2108 error("newline in string or char. const.");
2110 Bputrune(faction, c);
2112 error("EOF in string or character constant");
2115 error("action does not terminate");
2123 Bputrune(faction, c);
2128 openup(char *stem, int dflag, int vflag, int ytab, char *ytabc)
2133 snprint(buf, sizeof buf, "%s.%s", stem, FILEU);
2134 foutput = Bopen(buf, OWRITE);
2136 error("cannot open %s", buf);
2137 Blethal(foutput, nil);
2140 snprint(buf, sizeof buf, "%s.%s", stem, FILEDEBUG);
2141 if((fdebug = Bopen(buf, OWRITE)) == 0)
2142 error("can't open %s", buf);
2145 snprint(buf, sizeof buf, "%s.%s", stem, FILED);
2146 fdefine = Bopen(buf, OWRITE);
2148 error("can't create %s", buf);
2149 Blethal(fdefine, nil);
2152 snprint(buf, sizeof buf, "%s.%s", stem, OFILE);
2154 strecpy(buf, buf+sizeof buf, ytabc);
2155 ftable = Bopen(buf, OWRITE);
2157 error("cannot open table file %s", buf);
2158 Blethal(ftable, nil);
2162 * print the output for the states
2170 Bprint(ftable, "short yyexca[] =\n{");
2172 Bprint(fdebug, "char* yystates[] =\n{\n");
2174 /* output the stuff for state i */
2176 nolook = tystate[i]!=MUSTLOOKAHEAD;
2179 /* output actions */
2181 aryfil(temp1, ntokens+nnonter+1, 0);
2184 if(c > 1 && c < NTBASE && temp1[c] == 0) {
2186 if(c == *(v->pitem))
2187 putitem(v->pitem+1, (Lkset*)0);
2188 temp1[c] = state(c);
2190 if(c > NTBASE && temp1[(c -= NTBASE) + ntokens] == 0)
2191 temp1[c+ntokens] = amem[indgo[i]+c];
2194 temp1[1] = ACCEPTCODE;
2196 /* now, we have the shifts; look at the reductions */
2205 if(BIT(u->ws.lset, k)) {
2209 if(temp1[k] < 0) { /* reduce/reduce conflict */
2212 "\n%d: reduce/reduce conflict"
2213 " (red'ns %d and %d ) on %s",
2214 i, -temp1[k], lastred,
2216 if(-temp1[k] > lastred)
2217 temp1[k] = -lastred;
2220 /* potential shift/reduce conflict */
2221 precftn( lastred, k, i );
2229 Bprint(fdebug, "};\n");
2230 Bprint(ftable, "};\n");
2231 Bprint(ftable, "#define YYNPROD %d\n", nprod);
2232 Bprint(ftable, "#define YYPRIVATE %d\n", PRIVATE);
2234 Bprint(ftable, "#define yydebug %s\n", yydebug);
2238 * pack state i from temp1 into amem
2241 apack(int *p, int n)
2243 int *pp, *qq, *rr, off, *q, *r;
2245 /* we don't need to worry about checking because
2246 * we will only look at entries known to be there...
2247 * eliminate leading and trailing 0's
2251 for(pp = p, off = 0; *pp == 0 && pp <= q; ++pp, --off)
2258 /* now, find a place for the elements from p to q, inclusive */
2259 r = &amem[ACTSIZE-1];
2260 for(rr = amem; rr <= r; rr++, off++) {
2261 for(qq = rr, pp = p; pp <= q; pp++, qq++)
2263 if(*pp != *qq && *qq != 0)
2266 /* we have found an acceptable k */
2267 if(pkdebug && foutput != 0)
2268 Bprint(foutput, "off = %d, k = %d\n", off, (int)(rr-amem));
2269 for(qq = rr, pp = p; pp <= q; pp++, qq++)
2272 error("action table overflow");
2277 if(pkdebug && foutput != 0)
2278 for(pp = amem; pp <= memp; pp += 10) {
2279 Bprint(foutput, "\t");
2280 for(qq = pp; qq <= pp+9; qq++)
2281 Bprint(foutput, "%d ", *qq);
2282 Bprint(foutput, "\n");
2287 error("no space in action table");
2292 * output the gotos for the nontermninals
2297 int i, j, k, best, count, cbest, times;
2299 /* mark begining of gotos */
2300 Bprint(ftemp, "$\n");
2301 for(i = 1; i <= nnonter; i++) {
2304 /* find the best one to make default */
2308 /* is j the most frequent */
2309 for(j = 0; j <= nstate; j++) {
2312 if(tystate[j] == best)
2315 /* is tystate[j] the most frequent */
2318 for(k = j; k <= nstate; k++)
2319 if(tystate[k] == cbest)
2327 /* best is now the default entry */
2328 zzgobest += times-1;
2329 for(j = 0; j <= nstate; j++)
2330 if(tystate[j] != 0 && tystate[j] != best) {
2331 Bprint(ftemp, "%d,%d,", j, tystate[j]);
2335 /* now, the default */
2339 Bprint(ftemp, "%d\n", best);
2344 * output the gotos for nonterminal c
2353 /* first, find nonterminals with gotos on c */
2354 aryfil(temp1, nnonter+1, 0);
2361 /* cc is a nonterminal */
2362 if((cc=prdptr[i][1]-NTBASE) >= 0)
2363 /* cc has a goto on c */
2364 if(temp1[cc] != 0) {
2366 /* thus, the left side of production i does too */
2367 cc = *prdptr[i]-NTBASE;
2368 if(temp1[cc] == 0) {
2375 /* now, we have temp1[c] = 1 if a goto on c in closure of cc */
2376 if(g2debug && foutput != 0) {
2377 Bprint(foutput, "%s: gotos on ", nontrst[c].name);
2380 Bprint(foutput, "%s ", nontrst[i].name);
2381 Bprint(foutput, "\n");
2384 /* now, go through and put gotos into tystate */
2385 aryfil(tystate, nstate, 0);
2388 if((cc = *p->pitem) >= NTBASE)
2389 /* goto on c is possible */
2390 if(temp1[cc-NTBASE]) {
2391 tystate[i] = amem[indgo[i]+c];
2397 * decide a shift/reduce conflict by precedence.
2398 * r is a rule number, t a token number
2399 * the conflict is in state s
2400 * temp1[t] is changed to reflect the action
2403 precftn(int r, int t, int s)
2409 if(PLEVEL(lt) == 0 || PLEVEL(lp) == 0) {
2414 "\n%d: shift/reduce conflict (shift %d(%d), red'n %d(%d)) on %s",
2415 s, temp1[t], PLEVEL(lt), r, PLEVEL(lp), symnam(t));
2419 if(PLEVEL(lt) == PLEVEL(lp))
2422 if(PLEVEL(lt) > PLEVEL(lp))
2423 action = RASC; /* shift */
2425 action = LASC; /* reduce */
2427 case BASC: /* error action */
2430 case LASC: /* reduce */
2438 * temp1 has the actions, lastred the default
2443 int p, p0, p1, ntimes, tred, count, j, flag;
2445 /* find the best choice for lastred */
2451 if(temp1[j]+lastred == 0)
2453 /* count the number of appearances of temp1[j] */
2456 levprd[tred] |= REDFLAG;
2458 if(temp1[p]+tred == 0)
2460 if(count > ntimes) {
2467 * for error recovery, arrange that, if there is a shift on the
2468 * error recovery token, `error', that the default be the error action
2473 /* clear out entries in temp1 which equal lastred */
2475 if(temp1[p]+lastred == 0)
2479 defact[i] = lastred;
2482 if((p1=temp1[p0]) != 0) {
2487 if(p1 == ACCEPTCODE) {
2495 Bprint(ftable, "-1, %d,\n", i);
2496 Bprint(ftable, "\t%d, %d,\n", p0, p1);
2500 Bprint(ftemp, "%d,%d,", p0, p1);
2505 Bprint(ftable, "\t-2, %d,\n", lastred);
2507 Bprint(ftemp, "\n");
2522 Bprint(fdebug, " 0, /*%d*/\n", i);
2524 Bprint(fdebug, " \"");
2526 Bprint(fdebug, "%s\\n", writem(pp->pitem));
2527 if(tystate[i] == MUSTLOOKAHEAD)
2528 WSLOOP(wsets + (pstate[i+1] - pstate[i]), u)
2530 Bprint(fdebug, "%s\\n", writem(u->pitem));
2531 Bprint(fdebug, "\", /*%d*/\n", i);
2536 Bprint(foutput, "\nstate %d\n", i);
2538 Bprint(foutput, "\t%s\n", writem(pp->pitem));
2539 if(tystate[i] == MUSTLOOKAHEAD)
2540 /* print out empty productions in closure */
2541 WSLOOP(wsets+(pstate[i+1]-pstate[i]), u)
2543 Bprint(foutput, "\t%s\n", writem(u->pitem));
2545 /* check for state equal to another */
2547 if((j1=temp1[j0]) != 0) {
2548 Bprint(foutput, "\n\t%s ", symnam(j0));
2549 /* shift, error, or accept */
2551 if(j1 == ACCEPTCODE)
2552 Bprint(foutput, "accept");
2555 Bprint(foutput, "error");
2557 Bprint(foutput, "shift %d", j1);
2559 Bprint(foutput, "reduce %d (src line %d)", -j1, rlines[-j1]);
2562 /* output the final production */
2564 Bprint(foutput, "\n\t. reduce %d (src line %d)\n\n",
2565 lastred, rlines[lastred]);
2567 Bprint(foutput, "\n\t. error\n\n");
2569 /* now, output nonterminal actions */
2571 for(j0 = 1; j0 <= nnonter; j0++) {
2574 Bprint(foutput, "\t%s goto %d\n", symnam(j0+NTBASE), temp1[j1]);
2579 warray(char *s, int *v, int n)
2583 Bprint(ftable, "short %s[] =\n{", s);
2586 Bprint(ftable, "\n");
2587 Bprint(ftable, "%4d", v[i]);
2590 Bprint(ftable, "\n};\n");
2593 Bprint(ftable, ",");
2598 * in order to free up the mem and amem arrays for the optimizer,
2599 * and still be able to output yyr1, etc., after the sizes of
2600 * the action array is known, we hide the nonterminals
2601 * derived by productions in levprd.
2612 if(!(levprd[i] & REDFLAG)) {
2615 Bprint(foutput, "Rule not reduced: %s\n", writem(prdptr[i]));
2617 levprd[i] = *prdptr[i] - NTBASE;
2620 print("%d rules never reduced\n", j);
2626 int i, *p, j, k, *q;
2628 /* read the arrays from tempfile and set parameters */
2629 finput = Bopen(tempname, OREAD);
2631 error("optimizer cannot open tempfile");
2632 Blethal(finput, nil);
2643 temp1[nstate] = pmem - mem0;
2649 error("bad tempfile");
2655 temp1[nstate] = yypgo[0] = pmem - mem0;
2660 yypgo[nnonter] = pmem-mem0;
2666 error("bad tempfile");
2671 yypgo[nnonter--] = pmem - mem0;
2672 for(i = 0; i < nstate; i++) {
2675 q = mem0 + temp1[i+1];
2676 for(p = mem0 + temp1[i]; p < q ; p += 2) {
2682 /* nontrivial situation */
2684 /* j is now the range */
2685 /* j -= k; /* call scj */
2689 tystate[i] = (temp1[i+1]-temp1[i]) + 2*j;
2694 /* initialize ggreed table */
2695 for(i = 1; i <= nnonter; i++) {
2699 /* minimum entry index is always 0 */
2700 q = mem0 + yypgo[i+1] - 1;
2701 for(p = mem0+yypgo[i]; p < q ; p += 2) {
2706 ggreed[i] = ggreed[i] + 2*j;
2711 /* now, prepare to put the shift actions into the amem array */
2712 for(i = 0; i < ACTSIZE; i++)
2715 for(i = 0; i < nstate; i++) {
2716 if(tystate[i] == 0 && adb > 1)
2717 Bprint(ftable, "State %d: null\n", i);
2720 while((i = nxti()) != NOMORE)
2726 /* print amem array */
2728 for(p = amem; p <= maxa; p += 10) {
2729 Bprint(ftable, "%4d ", (int)(p-amem));
2730 for(i = 0; i < 10; ++i)
2731 Bprint(ftable, "%4d ", p[i]);
2732 Bprint(ftable, "\n");
2735 /* write out the output appropriate to the language */
2744 int *p, *r, *s, *q1, *q2;
2746 /* enter gotos on nonterminal i into array amem */
2749 q2 = mem0+ yypgo[i+1] - 1;
2750 q1 = mem0 + yypgo[i];
2752 /* now, find amem place for it */
2753 for(p = amem; p < &amem[ACTSIZE]; p++) {
2756 for(r = q1; r < q2; r += 2) {
2761 if((maxa = s) > &amem[ACTSIZE])
2762 error("a array overflow");
2764 /* we have found amem spot */
2767 if((maxa = p) > &amem[ACTSIZE])
2768 error("a array overflow");
2769 for(r = q1; r < q2; r += 2) {
2775 Bprint(ftable, "Nonterminal %d, entry at %d\n", i, pgo[i]);
2780 error("cannot place goto %d\n", i);
2786 int *r, *s, n, flag, j, *q1, *q2;
2790 /* enter state i into the amem array */
2791 q2 = mem0+temp1[i+1];
2793 /* find an acceptable place */
2794 for(n = -maxoff; n < ACTSIZE; n++) {
2796 for(r = q1; r < q2; r += 2) {
2797 if((s = *r + n + amem) < amem)
2806 /* check that the position equals another only if the states are identical */
2807 for(j=0; j<nstate; j++) {
2810 /* we have some disagreement */
2813 if(temp1[j+1]+temp1[i] == temp1[j]+temp1[i+1]) {
2815 /* states are equal */
2819 "State %d: entry at %d equals state %d\n",
2824 /* we have some disagreement */
2829 for(r = q1; r < q2; r += 2) {
2830 if((s = *r+n+amem) >= &amem[ACTSIZE])
2831 error("out of space in optimizer a array");
2834 if(*s != 0 && *s != r[1])
2835 error("clobber of a array, pos'n %d, by %d", s-amem, r[1]);
2840 Bprint(ftable, "State %d: entry at %d\n", i, indgo[i]);
2844 error("Error; failure to place state %d\n", i);
2857 for(i = 1; i <= nnonter; i++)
2858 if(ggreed[i] >= max) {
2862 for(i = 0; i < nstate; ++i)
2863 if(tystate[i] >= max) {
2868 Bprint(ftable, "nxti = %d, max = %d\n", maxi, max);
2886 for(p = maxa; p >= amem; p--)
2890 Bprint(foutput, "Optimizer space used: input %d/%d, output %d/%d\n",
2891 (int)(pmem-mem0+1), MEMSIZE, (int)(maxa-amem+1), ACTSIZE);
2892 Bprint(foutput, "%d table entries, %d zero\n", (int)(maxa-amem+1), i);
2893 Bprint(foutput, "maximum spread: %d, maximum offset: %d\n", maxspr, maxoff);
2897 * this version is for C
2898 * write out the optimized parser
2903 Bprint(ftable, "#define\tYYLAST\t%d\n", (int)(maxa-amem+1));
2904 arout("yyact", amem, (maxa-amem)+1);
2905 arout("yypact", indgo, nstate);
2906 arout("yypgo", pgo, nnonter+1);
2910 arout(char *s, int *v, int n)
2914 Bprint(ftable, "short %s[] =\n{", s);
2915 for(i = 0; i < n;) {
2917 Bprint(ftable, "\n");
2918 Bprint(ftable, "%4d", v[i]);
2921 Bprint(ftable, "\n};\n");
2923 Bprint(ftable, ",");
2928 * read and convert an integer from the standard input
2929 * return the terminating character
2930 * blanks, tabs, and newlines are ignored
2939 while((c=Bgetrune(finput)) != Beof) {
2941 val = val*10 + c-'0';
2953 if(pmem >= &mem0[MEMSIZE])
2954 error("out of space");