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 char *PARSER = "#9/lib/yaccpar";
17 char *PARSERS = "#9/lib/yaccpars";
19 #define TEMPNAME "y.tmp.XXXXXX"
20 #define ACTNAME "y.acts.XXXXXX"
22 #define FILEU "output"
24 #define FILEDEBUG "debug"
29 * the following are adjustable
30 * according to memory size
47 PRIVATE = 0xE000, /* unicode private use */
49 /* relationships which must hold:
50 TBITSET ints must hold NTERMS+1 bits...
53 TEMPSIZE >= NTERMS + NNONTERM + 1
61 NOASC = 0, /* no assoc. */
62 LASC = 1, /* left assoc. */
63 RASC = 2, /* right assoc. */
64 BASC = 3, /* binary assoc. */
66 /* flags for state generation */
72 /* flags for a rule having an action, and being reduced */
77 /* output parser flags */
104 /* macros for getting associativity and precedence levels */
106 #define ASSOC(i) ((i)&03)
107 #define PLEVEL(i) (((i)>>4)&077)
108 #define TYPE(i) (((i)>>10)&077)
110 /* macros for setting associativity and precedence levels */
112 #define SETASC(i,j) i |= j
113 #define SETPLEV(i,j) i |= (j<<4)
114 #define SETTYPE(i,j) i |= (j<<10)
118 #define TLOOP(i) for(i=1; i<=ntokens; i++)
119 #define NTLOOP(i) for(i=0; i<=nnonter; i++)
120 #define PLOOP(s,i) for(i=s; i<nprod; i++)
121 #define SLOOP(i) for(i=0; i<nstate; i++)
122 #define WSBUMP(x) x++
123 #define WSLOOP(s,j) for(j=s; j<cwp; j++)
124 #define ITMLOOP(i,p,q) for(q=pstate[i+1], p=pstate[i]; p<q; p++)
125 #define SETLOOP(i) for(i=0; i<tbitset; i++)
127 /* command to clobber tempfiles after use */
129 #define ZAPFILE(x) if(x) remove(x)
131 /* I/O descriptors */
133 Biobuf* faction; /* file for saving actions */
134 Biobuf* fdefine; /* file for #defines */
135 Biobuf* fdebug; /* y.debug for strings for debugging */
136 Biobuf* ftable; /* y.tab.c file */
137 Biobuf* ftemp; /* tempfile to pass 2 */
138 Biobuf* finput; /* input file */
139 Biobuf* foutput; /* y.output file */
141 /* communication variables between various I/O routines */
143 char* infile; /* input file name */
144 int numbval; /* value of an input number */
145 char tokname[NAMESIZE+4]; /* input token name, slop for runes and 0 */
147 /* structure declarations */
177 /* storage of names */
179 char cnames[CNAMSZ]; /* place where token and nonterminal names are stored */
180 int cnamsz = CNAMSZ; /* size of cnames */
181 char* cnamp = cnames; /* place where next name is to be put in */
182 int ndefout = 4; /* number of defined symbols output */
185 char ttempname[] = TEMPNAME;
186 char tactname[] = ACTNAME;
192 /* storage of types */
193 int ntypes; /* number of types defined */
194 char* typeset[NTYPES]; /* pointers to type tags */
196 /* token information */
198 int ntokens = 0 ; /* number of tokens */
200 int toklev[NTERMS]; /* vector with the precedence of the terminals */
202 /* nonterminal information */
204 int nnonter = -1; /* the number of nonterminals */
205 Symb nontrst[NNONTERM];
206 int start; /* start symbol */
208 /* assigned token type values */
211 char* ytabc = OFILE; /* name of y.tab.c */
213 /* grammar rule information */
215 int mem0[MEMSIZE] ; /* production storage */
217 int nprod = 1; /* number of productions */
218 int* prdptr[NPROD]; /* pointers to descriptions of productions */
219 int levprd[NPROD]; /* precedence levels for the productions */
220 int rlines[NPROD]; /* line number for this rule */
222 /* state information */
224 int nstate = 0; /* number of states */
225 Item* pstate[NSTATES+2]; /* pointers to the descriptions of the states */
226 int tystate[NSTATES]; /* contains type information about the states */
227 int defact[NSTATES]; /* the default actions of states */
228 int tstates[NTERMS]; /* states generated by terminal gotos */
229 int ntstates[NNONTERM]; /* states generated by nonterminal gotos */
230 int mstates[NSTATES]; /* chain of overflows of term/nonterm generation lists */
231 int lastred; /* the number of the last reduction of a state */
233 /* lookahead set information */
235 Lkset lkst[LSETSIZE];
236 int nolook; /* flag to turn off lookahead computations */
237 int tbitset; /* size of lookahead sets */
238 int nlset = 0; /* next lookahead set index */
239 int nolook = 0; /* flag to suppress lookahead computations */
240 Lkset clset; /* temporary storage for lookahead computations */
242 /* working set information */
244 Wset wsets[WSETSIZE];
247 /* storage for action table */
249 int amem[ACTSIZE]; /* action table storage */
250 int* memp = amem; /* next free action table position */
251 int indgo[NSTATES]; /* index to the stored goto table */
253 /* temporary vector, indexable by states, terms, or ntokens */
255 int temp1[TEMPSIZE]; /* temporary storage, indexed by terms + ntokens or states */
256 int lineno = 1; /* current input line number */
257 int fatfl = 1; /* if on, error is fatal */
258 int nerrors = 0; /* number of errors */
260 /* statistics collection variables */
270 int* ggreed = lkst[0].lset;
271 int* pgo = wsets[0].ws.lset;
272 int* yypgo = &nontrst[0].value;
274 int maxspr = 0; /* maximum spread of any entry */
275 int maxoff = 0; /* maximum offset into a array */
282 /* storage for information about the nonterminals */
284 int** pres[NNONTERM+2]; /* vector of pointers to productions yielding each nonterminal */
285 Lkset* pfirst[NNONTERM+2]; /* vector of pointers to first sets for each nonterminal */
286 int pempty[NNONTERM+1]; /* vector of nonterminals nontrivially deriving e */
288 /* random stuff picked out from between functions */
300 int pidebug = 0; /* debugging flag for putitem */
302 int cldebug = 0; /* debugging flag for closure */
325 /* define functions */
327 void main(int, char**);
329 char* chcopy(char*, char*);
333 void error(char*, ...);
334 void aryfil(int*, int, int);
335 int setunion(int*, int*);
340 void putitem(int*, Lkset*);
344 Lkset* flset(Lkset*);
347 void setup(int, char**);
349 int defin(int, char*);
354 int chfind(int, char*);
359 void openup(char*, int, int, int, char*);
361 int apack(int*, int);
364 void precftn(int, int, int);
367 void warray(char*, int*, int);
375 void arout(char*, int*, int);
379 main(int argc, char *argv[])
381 PARSER = unsharp(PARSER);
382 PARSERS = unsharp(PARSERS);
385 setup(argc, argv); /* initialize and read productions */
386 tbitset = NWORDS(ntokens);
387 cpres(); /* make table of which productions yield a given nonterminal */
388 cempty(); /* make a table of which nonterminals can match the empty string */
389 cpfir(); /* make a table of firsts of nonterminals */
390 stagen(); /* generate the states */
391 output(); /* write the states and the tables */
401 * put out other arrays, copy the parsers
408 finput = Bopen(parser, OREAD);
410 error("cannot open parser %s: %r", parser);
411 warray("yyr1", levprd, nprod);
412 aryfil(temp1, nprod, 0);
414 temp1[i] = prdptr[i+1]-prdptr[i]-2;
415 warray("yyr2", temp1, nprod);
417 aryfil(temp1, nstate, -1000);
419 for(j=tstates[i]; j!=0; j=mstates[j])
422 for(j=ntstates[i]; j!=0; j=mstates[j])
424 warray("yychk", temp1, nstate);
425 warray("yydef", defact, nstate);
427 /* put out token translation tables */
428 /* table 1 has 0-256 */
429 aryfil(temp1, 256, 0);
433 if(j >= 0 && j < 256) {
435 print("yacc bug -- cant have 2 different Ts with same value\n");
436 print(" %s and %s\n", tokset[i].name, tokset[temp1[j]].name);
444 warray("yytok1", temp1, c+1);
446 /* table 2 has PRIVATE-PRIVATE+256 */
447 aryfil(temp1, 256, 0);
450 j = tokset[i].value - PRIVATE;
451 if(j >= 0 && j < 256) {
453 print("yacc bug -- cant have 2 different Ts with same value\n");
454 print(" %s and %s\n", tokset[i].name, tokset[temp1[j]].name);
462 warray("yytok2", temp1, c+1);
464 /* table 3 has everything else */
465 Bprint(ftable, "static\tconst\tlong yytok3[] =\n{\n");
469 if(j >= 0 && j < 256)
471 if(j >= PRIVATE && j < 256+PRIVATE)
474 Bprint(ftable, "%4d,%4d,", j, i);
477 Bprint(ftable, "\n");
479 Bprint(ftable, "%4d\n};\n", 0);
481 /* copy parser text */
482 while((c=Bgetrune(finput)) != Beof) {
484 if((c = Bgetrune(finput)) != 'A')
485 Bputrune(ftable, '$');
486 else { /* copy actions */
487 faction = Bopen(actname, OREAD);
489 error("cannot reopen action tempfile");
490 while((c=Bgetrune(faction)) != Beof)
494 c = Bgetrune(finput);
503 * copies string q into p, returning next free char ptr
506 chcopy(char* p, char* q)
521 * creates output string for item pointed to by pp
527 static char sarr[ISIZE];
533 q = chcopy(sarr, nontrst[*p-NTBASE].name);
545 q = chcopy(q, symnam(i));
546 if(q > &sarr[ISIZE-30])
547 error("item too big");
550 /* an item calling for a reduction */
554 sprint(q, "%d)", -i);
560 * return a pointer to the name of symbol i
567 cp = (i >= NTBASE)? nontrst[i-NTBASE].name: tokset[i].name;
574 * output the summary on y.output
581 Bprint(foutput, "\n%d/%d terminals, %d/%d nonterminals\n",
582 ntokens, NTERMS, nnonter, NNONTERM);
583 Bprint(foutput, "%d/%d grammar rules, %d/%d states\n",
584 nprod, NPROD, nstate, NSTATES);
585 Bprint(foutput, "%d shift/reduce, %d reduce/reduce conflicts reported\n",
587 Bprint(foutput, "%d/%d working sets used\n",
588 (int)(zzcwp-wsets), WSETSIZE);
589 Bprint(foutput, "memory: states,etc. %d/%d, parser %d/%d\n",
590 (int)(zzmemsz-mem0), MEMSIZE, (int)(memp-amem), ACTSIZE);
591 Bprint(foutput, "%d/%d distinct lookahead sets\n", nlset, LSETSIZE);
592 Bprint(foutput, "%d extra closures\n", zzclose - 2*nstate);
593 Bprint(foutput, "%d shift entries, %d exceptions\n", zzacent, zzexcp);
594 Bprint(foutput, "%d goto entries\n", zzgoent);
595 Bprint(foutput, "%d entries saved by goto default\n", zzgobest);
597 if(zzsrconf != 0 || zzrrconf != 0) {
598 print("\nconflicts: ");
600 print("%d shift/reduce", zzsrconf);
601 if(zzsrconf && zzrrconf)
604 print("%d reduce/reduce", zzrrconf);
618 * write out error comment -- NEEDS WORK
626 fprint(2, "\n fatal error:");
630 fprint(2, ", %s:%d\n", infile, lineno);
639 * set elements 0 through n-1 to c
642 aryfil(int *v, int n, int c)
651 * set a to the union of a and b
652 * return 1 if b is not a subset of a, 0 otherwise
655 setunion(int *a, int *b)
678 Bprint(foutput, "\tNULL");
680 Bprint(foutput, " { ");
683 Bprint(foutput, "%s ", symnam(j));
684 Bprint(foutput, "}");
689 * compute an array with the beginnings of productions yielding given nonterminals
690 * The array pres points to these lists
691 * the array pyield has the lists: the total size is only NPROD+1
697 static int *pyield[NPROD];
703 fatfl = 0; /* make undefined symbols nonfatal */
706 *pmem++ = prdptr[j]+1;
708 error("nonterminal %s not defined!", nontrst[i].name);
717 if(pmem != &pyield[nprod])
718 error("internal Yacc error: pyield %d", pmem-&pyield[nprod]);
722 * compute an array with the first of nonterminals
727 int *p, **s, i, **t, ch, changes;
729 zzcwp = &wsets[nnonter];
731 aryfil(wsets[i].ws.lset, tbitset, 0);
733 /* initially fill the sets */
734 for(s=pres[i]; s<t; ++s)
735 for(p = *s; (ch = *p) > 0; ++p) {
737 SETBIT(wsets[i].ws.lset, ch);
740 if(!pempty[ch-NTBASE])
745 /* now, reflect transitivity */
751 for(s = pres[i]; s < t; ++s)
752 for(p = *s; (ch = (*p-NTBASE)) >= 0; ++p) {
753 changes |= setunion(wsets[i].ws.lset, wsets[ch].ws.lset);
761 pfirst[i] = flset(&wsets[i].ws);
766 Bprint(foutput, "\n%s: ", nontrst[i].name);
768 Bprint(foutput, " %d\n", pempty[i]);
773 * sorts last state,and sees if it equals earlier ones. returns state number
778 Item *p1, *p2, *k, *l, *q1, *q2;
782 p2 = pstate[nstate+1];
784 return 0; /* null state */
786 for(k = p2-1; k > p1; k--) /* make k the biggest */
787 for(l = k-1; l >= p1; --l)
788 if(l->pitem > k->pitem) {
799 size1 = p2 - p1; /* size of state */
801 for(i = (c>=NTBASE)? ntstates[c-NTBASE]: tstates[c]; i != 0; i = mstates[i]) {
809 for(l = q1; l < q2; l++) {
810 if(l->pitem != k->pitem)
817 pstate[nstate+1] = pstate[nstate]; /* delete last state */
818 /* fix up lookaheads */
821 for(l = q1, k = p1; l < q2; ++l, ++k ) {
825 clset.lset[s] = l->look->lset[s];
826 if(setunion(clset.lset, k->look->lset)) {
828 /* register the new set */
829 l->look = flset( &clset );
836 error("yacc state/nolook error");
837 pstate[nstate+2] = p2;
838 if(nstate+1 >= NSTATES)
839 error("too many states");
841 mstates[nstate] = ntstates[c-NTBASE];
842 ntstates[c-NTBASE] = nstate;
844 mstates[nstate] = tstates[c];
847 tystate[nstate] = MUSTDO;
852 putitem(int *ptr, Lkset *lptr)
856 if(pidebug && foutput != 0)
857 Bprint(foutput, "putitem(%s), state %d\n", writem(ptr), nstate);
858 j = pstate[nstate+1];
861 j->look = flset(lptr);
862 pstate[nstate+1] = ++j;
863 if((int*)j > zzmemsz) {
865 if(zzmemsz >= &mem0[MEMSIZE])
866 error("out of state space");
871 * mark nonterminals which derive the empty string
872 * also, look for nonterminals which don't derive any token strings
880 /* first, use the array pempty to detect productions that can never be reduced */
881 /* set pempty to WHONOWS */
882 aryfil(pempty, nnonter+1, WHOKNOWS);
884 /* now, look at productions, marking nonterminals which derive something */
887 if(pempty[*prdptr[i] - NTBASE])
889 for(p = prdptr[i]+1; *p >= 0; ++p)
890 if(*p >= NTBASE && pempty[*p-NTBASE] == WHOKNOWS)
892 /* production can be derived */
894 pempty[*prdptr[i]-NTBASE] = OK;
899 /* now, look at the nonterminals, to see if they are all OK */
901 /* the added production rises or falls as the start symbol ... */
904 if(pempty[i] != OK) {
906 error("nonterminal %s never derives any token string", nontrst[i].name);
916 /* now, compute the pempty array, to see which nonterminals derive the empty string */
917 /* set pempty to WHOKNOWS */
918 aryfil( pempty, nnonter+1, WHOKNOWS);
920 /* loop as long as we keep finding empty nonterminals */
924 /* not known to be empty */
925 if(pempty[*prdptr[i]-NTBASE] == WHOKNOWS) {
926 for(p = prdptr[i]+1; *p >= NTBASE && pempty[*p-NTBASE] == EMPTY ; ++p)
928 /* we have a nontrivially empty nonterminal */
930 pempty[*prdptr[i]-NTBASE] = EMPTY;
931 /* got one ... try for another */
939 * generate the states
951 /* THIS IS FUNNY from the standpoint of portability
952 * it represents the magic moment when the mem0 array, which has
953 * been holding the productions, starts to hold item pointers, of a
955 * someday, alloc should be used to allocate all this stuff... for now, we
956 * accept that if pointers don't fit in integers, there is a problem...
959 pstate[0] = pstate[1] = (Item*)mem;
960 aryfil(clset.lset, tbitset, 0);
961 putitem(prdptr[0]+1, &clset);
964 pstate[2] = pstate[1];
966 aryfil(amem, ACTSIZE, 0);
968 /* now, the main state generation loop */
972 if(tystate[i] != MUSTDO)
975 aryfil(temp1, nnonter+1, 0);
976 /* take state i, close it, and do gotos */
978 /* generate goto's */
985 if(pstate[i+1]-pstate[i] <= p-wsets)
986 tystate[i] = MUSTLOOKAHEAD;
991 /* this item contributes to the goto */
992 if(c == *(q->pitem)) {
993 putitem(q->pitem+1, &q->ws);
997 state(c); /* register new state */
999 temp1[c-NTBASE] = state(c);
1001 if(gsdebug && foutput != 0) {
1002 Bprint(foutput, "%d: ", i);
1005 Bprint(foutput, "%s %d, ",
1006 nontrst[j].name, temp1[j]);
1007 Bprint(foutput, "\n");
1009 indgo[i] = apack(&temp1[1], nnonter-1) - 1;
1017 * generate the closure of state i
1025 int c, ch, work, k, *pi, **s, **t;
1029 /* first, copy kernel of state i to wsets */
1032 cwp->pitem = p->pitem;
1033 cwp->flag = 1; /* this item must get closed */
1035 cwp->ws.lset[k] = p->look->lset[k];
1039 /* now, go through the loop, closing each item */
1046 /* dot is before c */
1050 /* only interesting case is where . is before nonterminal */
1054 /* compute the lookahead */
1055 aryfil(clset.lset, tbitset, 0);
1057 /* find items involving c */
1059 if(v->flag == 1 && *(pi=v->pitem) == c) {
1063 while((ch = *++pi) > 0) {
1064 /* terminal symbol */
1066 SETBIT(clset.lset, ch);
1069 /* nonterminal symbol */
1070 setunion(clset.lset, pfirst[ch-NTBASE]->lset);
1071 if(!pempty[ch-NTBASE])
1075 setunion(clset.lset, v->ws.lset);
1079 * now loop over productions derived from c
1080 * c is now nonterminal number
1084 for(s = pres[c]; s < t; ++s) {
1086 * put these items into the closure
1090 /* yes, it is there */
1091 if(v->pitem == *s) {
1094 if(setunion(v->ws.lset, clset.lset))
1099 /* not there; make a new entry */
1100 if(cwp-wsets+1 >= WSETSIZE)
1101 error( "working set overflow");
1106 SETLOOP(k) cwp->ws.lset[k] = clset.lset[k];
1115 /* have computed closure; flags are reset; return */
1118 if(cldebug && foutput != 0) {
1119 Bprint(foutput, "\nState %d, nolook = %d\n", i, nolook);
1122 Bprint(foutput, "flag set!\n");
1124 Bprint(foutput, "\t%s", writem(u->pitem));
1126 Bprint(foutput, "\n");
1132 * decide if the lookahead set pointed to by p is known
1133 * return pointer to a perminent location for the set
1141 for(q = &lkst[nlset]; q-- > lkst;) {
1148 /* we have matched */
1154 if(nlset >= LSETSIZE)
1155 error("too many lookahead sets");
1157 q->lset[j] = p->lset[j];
1172 exits("interrupted");
1176 setup(int argc, char *argv[])
1179 int i, j, fd, lev, ty, ytab, *p;
1180 int vflag, dflag, stem;
1181 char actnm[8], *stemc, *s, dirbuf[128];
1221 error("illegal option: %c", ARGC());
1223 openup(stemc, dflag, vflag, ytab, ytabc);
1224 fout = dflag?fdefine:ftable;
1226 Bprint(ftable, "#define\tYYARG\t1\n\n");
1228 if((fd = mkstemp(ttempname)) >= 0){
1229 tempname = ttempname;
1230 ftemp = Bfdopen(fd, OWRITE);
1232 if((fd = mkstemp(tactname)) >= 0){
1234 faction = Bfdopen(fd, OWRITE);
1236 if(ftemp == 0 || faction == 0)
1237 error("cannot open temp file");
1239 error("no input file");
1241 if(infile[0] != '/' && getwd(dirbuf, sizeof dirbuf)!=nil){
1242 i = strlen(infile)+1+strlen(dirbuf)+1+10;
1245 snprint(s, i, "%s/%s", dirbuf, infile);
1250 finput = Bopen(infile, OREAD);
1252 error("cannot open '%s'", argv[0]);
1256 extval = PRIVATE; /* tokens start in unicode 'private use' */
1258 defin(1, "$accept");
1263 for(t = gettok(); t != MARK && t != ENDFILE;)
1270 if(gettok() != IDENTIFIER)
1271 error("bad %%start construction");
1272 start = chfind(1, tokname);
1277 if(gettok() != TYPENAME)
1278 error("bad syntax in %%type");
1284 if((t=chfind(1, tokname)) < NTBASE) {
1285 j = TYPE(toklev[t]);
1286 if(j != 0 && j != ty)
1287 error("type redeclaration of token %s",
1290 SETTYPE(toklev[t], ty);
1292 j = nontrst[t-NTBASE].value;
1293 if(j != 0 && j != ty)
1294 error("type redeclaration of nonterminal %s",
1295 nontrst[t-NTBASE].name );
1297 nontrst[t-NTBASE].value = ty;
1311 /* copy the union declaration to the output */
1322 /* nonzero means new prec. and assoc. */
1326 /* get identifiers so defined */
1329 /* there is a type defined */
1344 j = chfind(0, tokname);
1346 error("%s defined earlier as nonterminal", tokname);
1348 if(ASSOC(toklev[j]))
1349 error("redeclaration of precedence of %s", tokname);
1350 SETASC(toklev[j], lev);
1351 SETPLEV(toklev[j], i);
1355 error("redeclaration of type of %s", tokname);
1356 SETTYPE(toklev[j],ty);
1360 tokset[j].value = numbval;
1361 if(j < ndefout && j > 3)
1362 error("please define type number of %s earlier",
1379 error("syntax error");
1382 error("unexpected EOF before %%");
1386 Bprint(ftable, "extern int yyerrflag;\n");
1387 Bprint(ftable, "#ifndef YYMAXDEPTH\n");
1388 Bprint(ftable, "#define YYMAXDEPTH 150\n");
1389 Bprint(ftable, "#endif\n" );
1391 Bprint(ftable, "#ifndef YYSTYPE\n");
1392 Bprint(ftable, "#define YYSTYPE int\n");
1393 Bprint(ftable, "#endif\n");
1396 Bprint(ftable, "YYSTYPE yylval;\n");
1397 Bprint(ftable, "YYSTYPE yyval;\n");
1400 Bprint(ftable, "#include \"%s.%s\"\n\n", stemc, FILED);
1401 Bprint(fout, "struct Yyarg {\n");
1402 Bprint(fout, "\tint\tyynerrs;\n");
1403 Bprint(fout, "\tint\tyyerrflag;\n");
1404 Bprint(fout, "\tvoid*\targ;\n");
1405 Bprint(fout, "\tYYSTYPE\tyyval;\n");
1406 Bprint(fout, "\tYYSTYPE\tyylval;\n");
1407 Bprint(fout, "};\n\n");
1411 /* added production */
1414 /* if start is 0, we will overwrite with the lhs of the first rule */
1419 while((t=gettok()) == LCURLY)
1422 error("bad syntax on first rule");
1425 prdptr[0][1] = chfind(1, tokname);
1428 while(t != MARK && t != ENDFILE) {
1429 /* process a rule */
1430 rlines[nprod] = lineno;
1432 *mem++ = *prdptr[nprod-1];
1434 if(t == IDENTCOLON) {
1435 *mem = chfind(1, tokname);
1437 error("token illegal on LHS of grammar rule");
1440 error("illegal rule: missing semicolon or | ?");
1441 /* read rule body */
1445 while(t == IDENTIFIER) {
1446 *mem = chfind(1, tokname);
1448 levprd[nprod] = toklev[*mem];
1453 if(gettok() != IDENTIFIER)
1454 error("illegal %%prec syntax");
1455 j = chfind(2, tokname);
1457 error("nonterminal %s illegal after %%prec",
1458 nontrst[j-NTBASE].name);
1459 levprd[nprod] = toklev[j];
1463 levprd[nprod] |= ACTFLAG;
1464 Bprint(faction, "\ncase %d:", nprod);
1465 cpyact(mem-prdptr[nprod]-1);
1466 Bprint(faction, " break;");
1467 if((t=gettok()) == IDENTIFIER) {
1469 /* action within rule... */
1470 sprint(actnm, "$$%d", nprod);
1472 /* make it a nonterminal */
1473 j = chfind(1, actnm);
1476 * the current rule will become rule number nprod+1
1477 * move the contents down, and make room for the null
1479 for(p = mem; p >= prdptr[nprod]; --p)
1483 /* enter null production for action */
1488 /* update the production information */
1489 levprd[nprod+1] = levprd[nprod] & ~ACTFLAG;
1490 levprd[nprod] = ACTFLAG;
1491 if(++nprod >= NPROD)
1492 error("more than %d rules", NPROD);
1495 /* make the action appear in the original rule */
1498 /* get some more of the rule */
1507 /* check that default action is reasonable */
1508 if(ntypes && !(levprd[nprod]&ACTFLAG) && nontrst[*prdptr[nprod]-NTBASE].value) {
1510 /* no explicit action, LHS has value */
1513 tempty = prdptr[nprod][1];
1515 error("must return a value, since LHS has a type");
1517 if(tempty >= NTBASE)
1518 tempty = nontrst[tempty-NTBASE].value;
1520 tempty = TYPE(toklev[tempty]);
1521 if(tempty != nontrst[*prdptr[nprod]-NTBASE].value)
1522 error("default action causes potential type clash");
1526 error("more than %d rules", NPROD);
1527 prdptr[nprod] = mem;
1531 /* end of all rules */
1536 Bprint(ftable, "\n");
1538 Bprint(ftable, "#line\t%d\t\"%s\"\n", lineno, infile);
1539 while((c=Bgetrune(finput)) != Beof)
1540 Bputrune(ftable, c);
1546 * finish action routine
1553 Bprint(ftable, "#define YYEOFCODE %d\n", 1);
1554 Bprint(ftable, "#define YYERRCODE %d\n", 2);
1558 * define s to be a terminal if t=0
1559 * or a nonterminal if t=1
1562 defin(int nt, char *s)
1570 if(nnonter >= NNONTERM)
1571 error("too many nonterminals, limit %d",NNONTERM);
1572 nontrst[nnonter].name = cstash(s);
1573 return NTBASE + nnonter;
1576 /* must be a token */
1578 if(ntokens >= NTERMS)
1579 error("too many terminals, limit %d", NTERMS);
1580 tokset[ntokens].name = cstash(s);
1582 /* establish value for token */
1583 /* single character literal */
1585 val = chartorune(&rune, &s[1]);
1592 /* escape sequence */
1593 if(s[0] == ' ' && s[1] == '\\') {
1595 /* single character escape sequence */
1597 case 'n': val = '\n'; break;
1598 case 'r': val = '\r'; break;
1599 case 'b': val = '\b'; break;
1600 case 't': val = '\t'; break;
1601 case 'f': val = '\f'; break;
1602 case '\'': val = '\''; break;
1603 case '"': val = '"'; break;
1604 case '\\': val = '\\'; break;
1605 default: error("invalid escape");
1611 if(s[2] >= '0' && s[2] <= '7') {
1617 error("illegal \\nnn construction");
1618 val = 64*s[2] + 8*s[3] + s[4] - 73*'0';
1620 error("'\\000' is illegal");
1623 error("unknown escape");
1628 tokset[ntokens].value = val;
1629 toklev[ntokens] = 0;
1634 * write out the defines (at the end of the declaration section)
1640 char sar[NAMESIZE+10];
1642 for(i=ndefout; i<=ntokens; i++) {
1644 c = tokset[i].name[0];
1645 if(c != ' ' && c != '$') {
1646 Bprint(ftable, "#define %s %d\n",
1647 tokset[i].name, tokset[i].value);
1649 Bprint(fdefine, "#define\t%s\t%d\n",
1650 tokset[i].name, tokset[i].value);
1653 ndefout = ntokens+1;
1654 if(last && fdebug) {
1655 Bprint(fdebug, "static char* yytoknames[] =\n{\n");
1657 if(tokset[i].name) {
1658 chcopy(sar, tokset[i].name);
1659 Bprint(fdebug, "\t\"%s\",\n", sar);
1662 Bprint(fdebug, "\t0,\n");
1664 Bprint(fdebug, "};\n");
1675 if(cnamp >= &cnames[cnamsz])
1676 error("too many characters in id's and literals");
1688 int i, base, match, reserve;
1689 static int peekline;
1695 c = Bgetrune(finput);
1696 while(c == ' ' || c == '\n' || c == '\t' || c == '\f') {
1699 c = Bgetrune(finput);
1704 lineno += skipcom();
1716 /* get, and look up, a type name (union member name) */
1718 while((c=Bgetrune(finput)) != '>' && c >= 0 && c != '\n') {
1720 c = runetochar(&tokname[i], &rune);
1725 error("unterminated < ... > clause");
1727 for(i=1; i<=ntypes; i++)
1728 if(!strcmp(typeset[i], tokname)) {
1734 typeset[numbval] = cstash(tokname);
1743 c = Bgetrune(finput);
1744 if(c == '\n' || c <= 0)
1745 error("illegal or missing ' or \"" );
1750 c = Bgetrune(finput);
1755 c = runetochar(&tokname[i], &rune);
1763 switch(c = Bgetrune(finput)) {
1764 case '0': return TERM;
1765 case '<': return LEFT;
1766 case '2': return BINARY;
1767 case '>': return RIGHT;
1769 case '\\': return MARK;
1770 case '=': return PREC;
1771 case '{': return LCURLY;
1772 default: reserve = 1;
1779 base = (c=='0')? 8: 10;
1780 for(c = Bgetrune(finput); isdigit(c); c = Bgetrune(finput))
1781 numbval = numbval*base + (c-'0');
1785 if(islower(c) || isupper(c) || c=='_' || c=='.' || c=='$') {
1787 while(islower(c) || isupper(c) || isdigit(c) ||
1788 c == '-' || c=='_' || c=='.' || c=='$') {
1789 if(reserve && isupper(c))
1792 c = runetochar(&tokname[i], &rune);
1795 c = Bgetrune(finput);
1803 /* find a reserved word */
1805 for(c=0; resrv[c].name; c++)
1806 if(strcmp(tokname, resrv[c].name) == 0)
1807 return resrv[c].value;
1808 error("invalid escape, or illegal reserved word: %s", tokname);
1811 /* look ahead to distinguish IDENTIFIER from IDENTCOLON */
1812 c = Bgetrune(finput);
1813 while(c == ' ' || c == '\t'|| c == '\n' || c == '\f' || c == '/') {
1816 /* look for comments */
1818 peekline += skipcom();
1819 c = Bgetrune(finput);
1828 * determine the type of a symbol
1836 v = nontrst[t-NTBASE].value;
1838 v = TYPE(toklev[t]);
1840 error("must specify type for %s", (t>=NTBASE)?
1841 nontrst[t-NTBASE].name: tokset[t].name);
1846 chfind(int t, char *s)
1853 if(!strcmp(s, tokset[i].name))
1856 if(!strcmp(s, nontrst[i].name))
1859 /* cannot find name */
1861 error("%s should have been defined earlier", s);
1866 * copy the union declaration to the output, and the define file if present
1874 Bprint(ftable, "\n");
1876 Bprint(ftable, "#line\t%d\t\"%s\"\n", lineno, infile);
1877 Bprint(ftable, "typedef union ");
1879 Bprint(fdefine, "\ntypedef union ");
1883 if((c=Bgetrune(finput)) == Beof)
1884 error("EOF encountered while processing %%union");
1885 Bputrune(ftable, c);
1887 Bputrune(fdefine, c);
1898 /* we are finished copying */
1900 Bprint(ftable, " YYSTYPE;\n");
1902 Bprint(fdefine, "\tYYSTYPE;\n");
1904 Bprint(fdefine, "extern\tYYSTYPE\tyylval;\n");
1913 * copies code between \{ and \}
1920 c = Bgetrune(finput);
1922 c = Bgetrune(finput);
1925 Bprint(ftable, "\n");
1927 Bprint(ftable, "#line\t%d\t\"%s\"\n", lineno, infile);
1930 if((c=Bgetrune(finput)) == '}')
1932 Bputc(ftable, '\\');
1935 if((c=Bgetrune(finput)) == '}')
1939 Bputrune(ftable, c);
1942 c = Bgetrune(finput);
1944 error("eof before %%}");
1948 * skip over comments
1949 * skipcom is called after reading a '/'
1957 /* i is the number of lines skipped */
1959 if(Bgetrune(finput) != '*')
1960 error("illegal comment");
1961 c = Bgetrune(finput);
1964 if((c=Bgetrune(finput)) == '/')
1968 c = Bgetrune(finput);
1970 error("EOF inside comment");
1975 * copy C action to the next ; or closing }
1981 int brac, match, j, s, fnd, tok;
1983 Bprint(faction, "\n");
1985 Bprint(faction, "#line\t%d\t\"%s\"\n", lineno, infile);
1989 c = Bgetrune(finput);
1994 Bputrune(faction, c);
2006 c = Bgetrune(finput);
2008 /* type description */
2011 if(gettok() != TYPENAME)
2012 error("bad syntax on $<ident> clause");
2014 c = Bgetrune(finput);
2017 Bprint(faction, "yyval");
2019 /* put out the proper tag... */
2022 tok = fdtype(*prdptr[nprod]);
2023 Bprint(faction, ".%s", typeset[tok]);
2029 c = Bgetrune(finput);
2035 c = Bgetrune(finput);
2040 error("Illegal use of $%d", j+offset);
2043 Bprint(faction, "yypt[-%d].yyv", -j);
2045 /* put out the proper tag */
2047 if(j+offset <= 0 && tok < 0)
2048 error("must specify type of $%d", j+offset);
2050 tok = fdtype(prdptr[nprod][j+offset]);
2051 Bprint(faction, ".%s", typeset[tok]);
2055 if(isupper(c) || islower(c) || c == '_' || c == '.') {
2056 int tok; /* tok used oustide for type info */
2058 /* look for $name */
2060 if(gettok() != IDENTIFIER)
2061 error("$ must be followed by an identifier");
2062 tok = chfind(2, tokname);
2063 if((c = Bgetrune(finput)) != '#') {
2067 if(gettok() != NUMBER) {
2068 error("# must be followed by number");
2072 for(j=1; j<=offset; ++j)
2073 if(tok == prdptr[nprod][j]) {
2079 error("$name or $name#number not found");
2081 Bputc(faction, '$');
2083 Bputc(faction, '-');
2090 Bputrune(faction, c);
2094 /* look for comments */
2095 Bputrune(faction, c);
2096 c = Bgetrune(finput);
2100 /* it really is a comment */
2101 Bputrune(faction, c);
2102 c = Bgetrune(finput);
2105 Bputrune(faction, c);
2106 if((c=Bgetrune(finput)) == '/')
2109 Bputrune(faction, c);
2112 c = Bgetrune(finput);
2114 error("EOF inside comment");
2117 /* character constant */
2122 /* character string */
2126 Bputrune(faction, c);
2127 while(c = Bgetrune(finput)) {
2129 Bputrune(faction, c);
2130 c = Bgetrune(finput);
2137 error("newline in string or char. const.");
2138 Bputrune(faction, c);
2140 error("EOF in string or character constant");
2143 error("action does not terminate");
2151 Bputrune(faction, c);
2156 openup(char *stem, int dflag, int vflag, int ytab, char *ytabc)
2161 sprint(buf, "%s.%s", stem, FILEU);
2162 foutput = Bopen(buf, OWRITE);
2164 error("cannot open %s", buf);
2167 sprint(buf, "%s.%s", stem, FILEDEBUG);
2168 if((fdebug = Bopen(buf, OWRITE)) == 0)
2169 error("can't open %s", buf);
2172 sprint(buf, "%s.%s", stem, FILED);
2173 fdefine = Bopen(buf, OWRITE);
2175 error("can't create %s", buf);
2178 sprint(buf, "%s.%s", stem, OFILE);
2181 ftable = Bopen(buf, OWRITE);
2183 error("cannot open table file %s", buf);
2187 * print the output for the states
2195 Bprint(ftable, "static\tconst\tshort yyexca[] =\n{");
2197 Bprint(fdebug, "static\tconst\tchar* yystates[] =\n{\n");
2199 /* output the stuff for state i */
2201 nolook = tystate[i]!=MUSTLOOKAHEAD;
2204 /* output actions */
2206 aryfil(temp1, ntokens+nnonter+1, 0);
2209 if(c > 1 && c < NTBASE && temp1[c] == 0) {
2211 if(c == *(v->pitem))
2212 putitem(v->pitem+1, (Lkset*)0);
2213 temp1[c] = state(c);
2215 if(c > NTBASE && temp1[(c -= NTBASE) + ntokens] == 0)
2216 temp1[c+ntokens] = amem[indgo[i]+c];
2219 temp1[1] = ACCEPTCODE;
2221 /* now, we have the shifts; look at the reductions */
2230 if(BIT(u->ws.lset, k)) {
2234 if(temp1[k] < 0) { /* reduce/reduce conflict */
2237 "\n%d: reduce/reduce conflict"
2238 " (red'ns %d and %d ) on %s",
2239 i, -temp1[k], lastred,
2241 if(-temp1[k] > lastred)
2242 temp1[k] = -lastred;
2245 /* potential shift/reduce conflict */
2246 precftn( lastred, k, i );
2254 Bprint(fdebug, "};\n");
2255 Bprint(ftable, "};\n");
2256 Bprint(ftable, "#define YYNPROD %d\n", nprod);
2257 Bprint(ftable, "#define YYPRIVATE %d\n", PRIVATE);
2259 Bprint(ftable, "#define yydebug %s\n", yydebug);
2263 * pack state i from temp1 into amem
2266 apack(int *p, int n)
2268 int *pp, *qq, *rr, off, *q, *r;
2270 /* we don't need to worry about checking because
2271 * we will only look at entries known to be there...
2272 * eliminate leading and trailing 0's
2276 for(pp = p, off = 0; *pp == 0 && pp <= q; ++pp, --off)
2283 /* now, find a place for the elements from p to q, inclusive */
2284 r = &amem[ACTSIZE-1];
2285 for(rr = amem; rr <= r; rr++, off++) {
2286 for(qq = rr, pp = p; pp <= q; pp++, qq++)
2288 if(*pp != *qq && *qq != 0)
2291 /* we have found an acceptable k */
2292 if(pkdebug && foutput != 0)
2293 Bprint(foutput, "off = %d, k = %d\n", off, (int)(rr-amem));
2294 for(qq = rr, pp = p; pp <= q; pp++, qq++)
2297 error("action table overflow");
2302 if(pkdebug && foutput != 0)
2303 for(pp = amem; pp <= memp; pp += 10) {
2304 Bprint(foutput, "\t");
2305 for(qq = pp; qq <= pp+9; qq++)
2306 Bprint(foutput, "%d ", *qq);
2307 Bprint(foutput, "\n");
2312 error("no space in action table");
2317 * output the gotos for the nontermninals
2322 int i, j, k, best, count, cbest, times;
2324 /* mark begining of gotos */
2325 Bprint(ftemp, "$\n");
2326 for(i = 1; i <= nnonter; i++) {
2329 /* find the best one to make default */
2333 /* is j the most frequent */
2334 for(j = 0; j <= nstate; j++) {
2337 if(tystate[j] == best)
2340 /* is tystate[j] the most frequent */
2343 for(k = j; k <= nstate; k++)
2344 if(tystate[k] == cbest)
2352 /* best is now the default entry */
2353 zzgobest += times-1;
2354 for(j = 0; j <= nstate; j++)
2355 if(tystate[j] != 0 && tystate[j] != best) {
2356 Bprint(ftemp, "%d,%d,", j, tystate[j]);
2360 /* now, the default */
2364 Bprint(ftemp, "%d\n", best);
2369 * output the gotos for nonterminal c
2378 /* first, find nonterminals with gotos on c */
2379 aryfil(temp1, nnonter+1, 0);
2386 /* cc is a nonterminal */
2387 if((cc=prdptr[i][1]-NTBASE) >= 0)
2388 /* cc has a goto on c */
2389 if(temp1[cc] != 0) {
2391 /* thus, the left side of production i does too */
2392 cc = *prdptr[i]-NTBASE;
2393 if(temp1[cc] == 0) {
2400 /* now, we have temp1[c] = 1 if a goto on c in closure of cc */
2401 if(g2debug && foutput != 0) {
2402 Bprint(foutput, "%s: gotos on ", nontrst[c].name);
2405 Bprint(foutput, "%s ", nontrst[i].name);
2406 Bprint(foutput, "\n");
2409 /* now, go through and put gotos into tystate */
2410 aryfil(tystate, nstate, 0);
2413 if((cc = *p->pitem) >= NTBASE)
2414 /* goto on c is possible */
2415 if(temp1[cc-NTBASE]) {
2416 tystate[i] = amem[indgo[i]+c];
2422 * decide a shift/reduce conflict by precedence.
2423 * r is a rule number, t a token number
2424 * the conflict is in state s
2425 * temp1[t] is changed to reflect the action
2428 precftn(int r, int t, int s)
2434 if(PLEVEL(lt) == 0 || PLEVEL(lp) == 0) {
2439 "\n%d: shift/reduce conflict (shift %d(%d), red'n %d(%d)) on %s",
2440 s, temp1[t], PLEVEL(lt), r, PLEVEL(lp), symnam(t));
2444 if(PLEVEL(lt) == PLEVEL(lp))
2447 if(PLEVEL(lt) > PLEVEL(lp))
2448 action = RASC; /* shift */
2450 action = LASC; /* reduce */
2452 case BASC: /* error action */
2455 case LASC: /* reduce */
2463 * temp1 has the actions, lastred the default
2468 int p, p0, p1, ntimes, tred, count, j, flag;
2470 /* find the best choice for lastred */
2476 if(temp1[j]+lastred == 0)
2478 /* count the number of appearances of temp1[j] */
2481 levprd[tred] |= REDFLAG;
2483 if(temp1[p]+tred == 0)
2485 if(count > ntimes) {
2492 * for error recovery, arrange that, if there is a shift on the
2493 * error recovery token, `error', that the default be the error action
2498 /* clear out entries in temp1 which equal lastred */
2500 if(temp1[p]+lastred == 0)
2504 defact[i] = lastred;
2507 if((p1=temp1[p0]) != 0) {
2512 if(p1 == ACCEPTCODE) {
2520 Bprint(ftable, "-1, %d,\n", i);
2521 Bprint(ftable, "\t%d, %d,\n", p0, p1);
2525 Bprint(ftemp, "%d,%d,", p0, p1);
2530 Bprint(ftable, "\t-2, %d,\n", lastred);
2532 Bprint(ftemp, "\n");
2547 Bprint(fdebug, " 0, /*%d*/\n", i);
2549 Bprint(fdebug, " \"");
2551 Bprint(fdebug, "%s\\n", writem(pp->pitem));
2552 if(tystate[i] == MUSTLOOKAHEAD)
2553 WSLOOP(wsets + (pstate[i+1] - pstate[i]), u)
2555 Bprint(fdebug, "%s\\n", writem(u->pitem));
2556 Bprint(fdebug, "\", /*%d*/\n", i);
2561 Bprint(foutput, "\nstate %d\n", i);
2563 Bprint(foutput, "\t%s\n", writem(pp->pitem));
2564 if(tystate[i] == MUSTLOOKAHEAD)
2565 /* print out empty productions in closure */
2566 WSLOOP(wsets+(pstate[i+1]-pstate[i]), u)
2568 Bprint(foutput, "\t%s\n", writem(u->pitem));
2570 /* check for state equal to another */
2572 if((j1=temp1[j0]) != 0) {
2573 Bprint(foutput, "\n\t%s ", symnam(j0));
2574 /* shift, error, or accept */
2576 if(j1 == ACCEPTCODE)
2577 Bprint(foutput, "accept");
2580 Bprint(foutput, "error");
2582 Bprint(foutput, "shift %d", j1);
2584 Bprint(foutput, "reduce %d (src line %d)", -j1, rlines[-j1]);
2587 /* output the final production */
2589 Bprint(foutput, "\n\t. reduce %d (src line %d)\n\n",
2590 lastred, rlines[lastred]);
2592 Bprint(foutput, "\n\t. error\n\n");
2594 /* now, output nonterminal actions */
2596 for(j0 = 1; j0 <= nnonter; j0++) {
2599 Bprint(foutput, "\t%s goto %d\n", symnam(j0+NTBASE), temp1[j1]);
2604 warray(char *s, int *v, int n)
2608 Bprint(ftable, "static\tconst\tshort %s[] =\n{", s);
2611 Bprint(ftable, "\n");
2612 Bprint(ftable, "%4d", v[i]);
2615 Bprint(ftable, "\n};\n");
2618 Bprint(ftable, ",");
2623 * in order to free up the mem and amem arrays for the optimizer,
2624 * and still be able to output yyr1, etc., after the sizes of
2625 * the action array is known, we hide the nonterminals
2626 * derived by productions in levprd.
2637 if(!(levprd[i] & REDFLAG)) {
2640 Bprint(foutput, "Rule not reduced: %s\n", writem(prdptr[i]));
2642 levprd[i] = *prdptr[i] - NTBASE;
2645 print("%d rules never reduced\n", j);
2651 int i, *p, j, k, *q;
2653 /* read the arrays from tempfile and set parameters */
2654 finput = Bopen(tempname, OREAD);
2656 error("optimizer cannot open tempfile");
2667 temp1[nstate] = pmem - mem0;
2673 error("bad tempfile %s", tempname);
2679 temp1[nstate] = yypgo[0] = pmem - mem0;
2684 yypgo[nnonter] = pmem-mem0;
2690 error("bad tempfile");
2695 yypgo[nnonter--] = pmem - mem0;
2696 for(i = 0; i < nstate; i++) {
2699 q = mem0 + temp1[i+1];
2700 for(p = mem0 + temp1[i]; p < q ; p += 2) {
2706 /* nontrivial situation */
2708 /* j is now the range */
2709 /* j -= k; *//* call scj */
2713 tystate[i] = (temp1[i+1]-temp1[i]) + 2*j;
2718 /* initialize ggreed table */
2719 for(i = 1; i <= nnonter; i++) {
2723 /* minimum entry index is always 0 */
2724 q = mem0 + yypgo[i+1] - 1;
2725 for(p = mem0+yypgo[i]; p < q ; p += 2) {
2730 ggreed[i] = ggreed[i] + 2*j;
2735 /* now, prepare to put the shift actions into the amem array */
2736 for(i = 0; i < ACTSIZE; i++)
2739 for(i = 0; i < nstate; i++) {
2740 if(tystate[i] == 0 && adb > 1)
2741 Bprint(ftable, "State %d: null\n", i);
2744 while((i = nxti()) != NOMORE)
2750 /* print amem array */
2752 for(p = amem; p <= maxa; p += 10) {
2753 Bprint(ftable, "%4d ", (int)(p-amem));
2754 for(i = 0; i < 10; ++i)
2755 Bprint(ftable, "%4d ", p[i]);
2756 Bprint(ftable, "\n");
2759 /* write out the output appropriate to the language */
2768 int *p, *r, *s, *q1, *q2;
2770 /* enter gotos on nonterminal i into array amem */
2773 q2 = mem0+ yypgo[i+1] - 1;
2774 q1 = mem0 + yypgo[i];
2776 /* now, find amem place for it */
2777 for(p = amem; p < &amem[ACTSIZE]; p++) {
2780 for(r = q1; r < q2; r += 2) {
2785 if((maxa = s) > &amem[ACTSIZE])
2786 error("a array overflow");
2788 /* we have found amem spot */
2791 if((maxa = p) > &amem[ACTSIZE])
2792 error("a array overflow");
2793 for(r = q1; r < q2; r += 2) {
2799 Bprint(ftable, "Nonterminal %d, entry at %d\n", i, pgo[i]);
2804 error("cannot place goto %d\n", i);
2810 int *r, *s, n, flag, j, *q1, *q2;
2814 /* enter state i into the amem array */
2815 q2 = mem0+temp1[i+1];
2817 /* find an acceptable place */
2818 for(n = -maxoff; n < ACTSIZE; n++) {
2820 for(r = q1; r < q2; r += 2) {
2821 if((s = *r + n + amem) < amem)
2830 /* check that the position equals another only if the states are identical */
2831 for(j=0; j<nstate; j++) {
2834 /* we have some disagreement */
2837 if(temp1[j+1]+temp1[i] == temp1[j]+temp1[i+1]) {
2839 /* states are equal */
2843 "State %d: entry at %d equals state %d\n",
2848 /* we have some disagreement */
2853 for(r = q1; r < q2; r += 2) {
2854 if((s = *r+n+amem) >= &amem[ACTSIZE])
2855 error("out of space in optimizer a array");
2858 if(*s != 0 && *s != r[1])
2859 error("clobber of a array, pos'n %d, by %d", s-amem, r[1]);
2864 Bprint(ftable, "State %d: entry at %d\n", i, indgo[i]);
2868 error("Error; failure to place state %d\n", i);
2881 for(i = 1; i <= nnonter; i++)
2882 if(ggreed[i] >= max) {
2886 for(i = 0; i < nstate; ++i)
2887 if(tystate[i] >= max) {
2892 Bprint(ftable, "nxti = %d, max = %d\n", maxi, max);
2910 for(p = maxa; p >= amem; p--)
2914 Bprint(foutput, "Optimizer space used: input %d/%d, output %d/%d\n",
2915 (int)(pmem-mem0+1), MEMSIZE, (int)(maxa-amem+1), ACTSIZE);
2916 Bprint(foutput, "%d table entries, %d zero\n", (int)(maxa-amem+1), i);
2917 Bprint(foutput, "maximum spread: %d, maximum offset: %d\n", maxspr, maxoff);
2921 * this version is for C
2922 * write out the optimized parser
2927 Bprint(ftable, "#define\tYYLAST\t%d\n", (int)(maxa-amem+1));
2928 arout("yyact", amem, (maxa-amem)+1);
2929 arout("yypact", indgo, nstate);
2930 arout("yypgo", pgo, nnonter+1);
2934 arout(char *s, int *v, int n)
2938 Bprint(ftable, "static\tconst\tshort %s[] =\n{", s);
2939 for(i = 0; i < n;) {
2941 Bprint(ftable, "\n");
2942 Bprint(ftable, "%4d", v[i]);
2945 Bprint(ftable, "\n};\n");
2947 Bprint(ftable, ",");
2952 * read and convert an integer from the standard input
2953 * return the terminating character
2954 * blanks, tabs, and newlines are ignored
2963 while((c=Bgetrune(finput)) != Beof) {
2965 val = val*10 + c-'0';
2977 if(pmem >= &mem0[MEMSIZE])
2978 error("out of space");