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 "lib/yaccpar"
17 #define PARSERS "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
403 root = getenv("PLAN9");
405 root = "/usr/local/plan9";
406 s = malloc(strlen(root)+1+strlen(parser)+1);
410 finput = Bopen(s, OREAD);
412 error("cannot find parser %s", s);
414 warray("yyr1", levprd, nprod);
415 aryfil(temp1, nprod, 0);
417 temp1[i] = prdptr[i+1]-prdptr[i]-2;
418 warray("yyr2", temp1, nprod);
420 aryfil(temp1, nstate, -1000);
422 for(j=tstates[i]; j!=0; j=mstates[j])
425 for(j=ntstates[i]; j!=0; j=mstates[j])
427 warray("yychk", temp1, nstate);
428 warray("yydef", defact, nstate);
430 /* put out token translation tables */
431 /* table 1 has 0-256 */
432 aryfil(temp1, 256, 0);
436 if(j >= 0 && j < 256) {
438 print("yacc bug -- cant have 2 different Ts with same value\n");
439 print(" %s and %s\n", tokset[i].name, tokset[temp1[j]].name);
447 warray("yytok1", temp1, c+1);
449 /* table 2 has PRIVATE-PRIVATE+256 */
450 aryfil(temp1, 256, 0);
453 j = tokset[i].value - PRIVATE;
454 if(j >= 0 && j < 256) {
456 print("yacc bug -- cant have 2 different Ts with same value\n");
457 print(" %s and %s\n", tokset[i].name, tokset[temp1[j]].name);
465 warray("yytok2", temp1, c+1);
467 /* table 3 has everything else */
468 Bprint(ftable, "long yytok3[] =\n{\n");
472 if(j >= 0 && j < 256)
474 if(j >= PRIVATE && j < 256+PRIVATE)
477 Bprint(ftable, "%4d,%4d,", j, i);
480 Bprint(ftable, "\n");
482 Bprint(ftable, "%4d\n};\n", 0);
484 /* copy parser text */
485 while((c=Bgetrune(finput)) != Beof) {
487 if((c = Bgetrune(finput)) != 'A')
488 Bputrune(ftable, '$');
489 else { /* copy actions */
490 faction = Bopen(actname, OREAD);
492 error("cannot reopen action tempfile");
493 while((c=Bgetrune(faction)) != Beof)
497 c = Bgetrune(finput);
506 * copies string q into p, returning next free char ptr
509 chcopy(char* p, char* q)
524 * creates output string for item pointed to by pp
530 static char sarr[ISIZE];
536 q = chcopy(sarr, nontrst[*p-NTBASE].name);
548 q = chcopy(q, symnam(i));
549 if(q > &sarr[ISIZE-30])
550 error("item too big");
553 /* an item calling for a reduction */
557 sprint(q, "%d)", -i);
563 * return a pointer to the name of symbol i
570 cp = (i >= NTBASE)? nontrst[i-NTBASE].name: tokset[i].name;
577 * output the summary on y.output
584 Bprint(foutput, "\n%d/%d terminals, %d/%d nonterminals\n",
585 ntokens, NTERMS, nnonter, NNONTERM);
586 Bprint(foutput, "%d/%d grammar rules, %d/%d states\n",
587 nprod, NPROD, nstate, NSTATES);
588 Bprint(foutput, "%d shift/reduce, %d reduce/reduce conflicts reported\n",
590 Bprint(foutput, "%d/%d working sets used\n",
591 (int)(zzcwp-wsets), WSETSIZE);
592 Bprint(foutput, "memory: states,etc. %d/%d, parser %d/%d\n",
593 (int)(zzmemsz-mem0), MEMSIZE, (int)(memp-amem), ACTSIZE);
594 Bprint(foutput, "%d/%d distinct lookahead sets\n", nlset, LSETSIZE);
595 Bprint(foutput, "%d extra closures\n", zzclose - 2*nstate);
596 Bprint(foutput, "%d shift entries, %d exceptions\n", zzacent, zzexcp);
597 Bprint(foutput, "%d goto entries\n", zzgoent);
598 Bprint(foutput, "%d entries saved by goto default\n", zzgobest);
600 if(zzsrconf != 0 || zzrrconf != 0) {
601 print("\nconflicts: ");
603 print("%d shift/reduce", zzsrconf);
604 if(zzsrconf && zzrrconf)
607 print("%d reduce/reduce", zzrrconf);
621 * write out error comment -- NEEDS WORK
628 fprint(2, "\n fatal error:");
629 fprint(2, s, (&s)[1]);
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];
1214 error("illegal option: %c", ARGC());
1216 openup(stemc, dflag, vflag, ytab, ytabc);
1218 if((fd = mkstemp(ttempname)) >= 0){
1219 tempname = ttempname;
1220 ftemp = Bfdopen(fd, OWRITE);
1222 if((fd = mkstemp(tactname)) >= 0){
1224 faction = Bfdopen(fd, OWRITE);
1226 if(ftemp == 0 || faction == 0)
1227 error("cannot open temp file");
1229 error("no input file");
1231 if(infile[0] != '/' && getwd(dirbuf, sizeof dirbuf)!=nil){
1232 i = strlen(infile)+1+strlen(dirbuf)+1+10;
1235 snprint(s, i, "%s/%s", dirbuf, infile);
1240 finput = Bopen(infile, OREAD);
1242 error("cannot open '%s'", argv[0]);
1246 extval = PRIVATE; /* tokens start in unicode 'private use' */
1248 defin(1, "$accept");
1253 for(t = gettok(); t != MARK && t != ENDFILE;)
1260 if(gettok() != IDENTIFIER)
1261 error("bad %%start construction");
1262 start = chfind(1, tokname);
1267 if(gettok() != TYPENAME)
1268 error("bad syntax in %%type");
1274 if((t=chfind(1, tokname)) < NTBASE) {
1275 j = TYPE(toklev[t]);
1276 if(j != 0 && j != ty)
1277 error("type redeclaration of token %s",
1280 SETTYPE(toklev[t], ty);
1282 j = nontrst[t-NTBASE].value;
1283 if(j != 0 && j != ty)
1284 error("type redeclaration of nonterminal %s",
1285 nontrst[t-NTBASE].name );
1287 nontrst[t-NTBASE].value = ty;
1301 /* copy the union declaration to the output */
1312 /* nonzero means new prec. and assoc. */
1316 /* get identifiers so defined */
1319 /* there is a type defined */
1334 j = chfind(0, tokname);
1336 error("%s defined earlier as nonterminal", tokname);
1338 if(ASSOC(toklev[j]))
1339 error("redeclaration of precedence of %s", tokname);
1340 SETASC(toklev[j], lev);
1341 SETPLEV(toklev[j], i);
1345 error("redeclaration of type of %s", tokname);
1346 SETTYPE(toklev[j],ty);
1350 tokset[j].value = numbval;
1351 if(j < ndefout && j > 3)
1352 error("please define type number of %s earlier",
1369 error("syntax error");
1372 error("unexpected EOF before %%");
1375 Bprint(ftable, "extern int yyerrflag;\n");
1376 Bprint(ftable, "#ifndef YYMAXDEPTH\n");
1377 Bprint(ftable, "#define YYMAXDEPTH 150\n");
1378 Bprint(ftable, "#endif\n" );
1380 Bprint(ftable, "#ifndef YYSTYPE\n");
1381 Bprint(ftable, "#define YYSTYPE int\n");
1382 Bprint(ftable, "#endif\n");
1384 Bprint(ftable, "YYSTYPE yylval;\n");
1385 Bprint(ftable, "YYSTYPE yyval;\n");
1389 /* added production */
1392 /* if start is 0, we will overwrite with the lhs of the first rule */
1397 while((t=gettok()) == LCURLY)
1400 error("bad syntax on first rule");
1403 prdptr[0][1] = chfind(1, tokname);
1406 while(t != MARK && t != ENDFILE) {
1407 /* process a rule */
1408 rlines[nprod] = lineno;
1410 *mem++ = *prdptr[nprod-1];
1412 if(t == IDENTCOLON) {
1413 *mem = chfind(1, tokname);
1415 error("token illegal on LHS of grammar rule");
1418 error("illegal rule: missing semicolon or | ?");
1419 /* read rule body */
1423 while(t == IDENTIFIER) {
1424 *mem = chfind(1, tokname);
1426 levprd[nprod] = toklev[*mem];
1431 if(gettok() != IDENTIFIER)
1432 error("illegal %%prec syntax");
1433 j = chfind(2, tokname);
1435 error("nonterminal %s illegal after %%prec",
1436 nontrst[j-NTBASE].name);
1437 levprd[nprod] = toklev[j];
1441 levprd[nprod] |= ACTFLAG;
1442 Bprint(faction, "\ncase %d:", nprod);
1443 cpyact(mem-prdptr[nprod]-1);
1444 Bprint(faction, " break;");
1445 if((t=gettok()) == IDENTIFIER) {
1447 /* action within rule... */
1448 sprint(actnm, "$$%d", nprod);
1450 /* make it a nonterminal */
1451 j = chfind(1, actnm);
1454 * the current rule will become rule number nprod+1
1455 * move the contents down, and make room for the null
1457 for(p = mem; p >= prdptr[nprod]; --p)
1461 /* enter null production for action */
1466 /* update the production information */
1467 levprd[nprod+1] = levprd[nprod] & ~ACTFLAG;
1468 levprd[nprod] = ACTFLAG;
1469 if(++nprod >= NPROD)
1470 error("more than %d rules", NPROD);
1473 /* make the action appear in the original rule */
1476 /* get some more of the rule */
1485 /* check that default action is reasonable */
1486 if(ntypes && !(levprd[nprod]&ACTFLAG) && nontrst[*prdptr[nprod]-NTBASE].value) {
1488 /* no explicit action, LHS has value */
1491 tempty = prdptr[nprod][1];
1493 error("must return a value, since LHS has a type");
1495 if(tempty >= NTBASE)
1496 tempty = nontrst[tempty-NTBASE].value;
1498 tempty = TYPE(toklev[tempty]);
1499 if(tempty != nontrst[*prdptr[nprod]-NTBASE].value)
1500 error("default action causes potential type clash");
1504 error("more than %d rules", NPROD);
1505 prdptr[nprod] = mem;
1509 /* end of all rules */
1514 Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile);
1515 while((c=Bgetrune(finput)) != Beof)
1516 Bputrune(ftable, c);
1522 * finish action routine
1529 Bprint(ftable, "#define YYEOFCODE %d\n", 1);
1530 Bprint(ftable, "#define YYERRCODE %d\n", 2);
1534 * define s to be a terminal if t=0
1535 * or a nonterminal if t=1
1538 defin(int nt, char *s)
1546 if(nnonter >= NNONTERM)
1547 error("too many nonterminals, limit %d",NNONTERM);
1548 nontrst[nnonter].name = cstash(s);
1549 return NTBASE + nnonter;
1552 /* must be a token */
1554 if(ntokens >= NTERMS)
1555 error("too many terminals, limit %d", NTERMS);
1556 tokset[ntokens].name = cstash(s);
1558 /* establish value for token */
1559 /* single character literal */
1561 val = chartorune(&rune, &s[1]);
1568 /* escape sequence */
1569 if(s[0] == ' ' && s[1] == '\\') {
1571 /* single character escape sequence */
1573 case 'n': val = '\n'; break;
1574 case 'r': val = '\r'; break;
1575 case 'b': val = '\b'; break;
1576 case 't': val = '\t'; break;
1577 case 'f': val = '\f'; break;
1578 case '\'': val = '\''; break;
1579 case '"': val = '"'; break;
1580 case '\\': val = '\\'; break;
1581 default: error("invalid escape");
1587 if(s[2] >= '0' && s[2] <= '7') {
1593 error("illegal \\nnn construction");
1594 val = 64*s[2] + 8*s[3] + s[4] - 73*'0';
1596 error("'\\000' is illegal");
1599 error("unknown escape");
1604 tokset[ntokens].value = val;
1605 toklev[ntokens] = 0;
1610 * write out the defines (at the end of the declaration section)
1616 char sar[NAMESIZE+10];
1618 for(i=ndefout; i<=ntokens; i++) {
1620 c = tokset[i].name[0];
1621 if(c != ' ' && c != '$') {
1622 Bprint(ftable, "#define %s %d\n",
1623 tokset[i].name, tokset[i].value);
1625 Bprint(fdefine, "#define\t%s\t%d\n",
1626 tokset[i].name, tokset[i].value);
1629 ndefout = ntokens+1;
1630 if(last && fdebug) {
1631 Bprint(fdebug, "char* yytoknames[] =\n{\n");
1633 if(tokset[i].name) {
1634 chcopy(sar, tokset[i].name);
1635 Bprint(fdebug, "\t\"%s\",\n", sar);
1638 Bprint(fdebug, "\t0,\n");
1640 Bprint(fdebug, "};\n");
1651 if(cnamp >= &cnames[cnamsz])
1652 error("too many characters in id's and literals");
1664 int i, base, match, reserve;
1665 static int peekline;
1671 c = Bgetrune(finput);
1672 while(c == ' ' || c == '\n' || c == '\t' || c == '\f') {
1675 c = Bgetrune(finput);
1680 lineno += skipcom();
1692 /* get, and look up, a type name (union member name) */
1694 while((c=Bgetrune(finput)) != '>' && c >= 0 && c != '\n') {
1696 c = runetochar(&tokname[i], &rune);
1701 error("unterminated < ... > clause");
1703 for(i=1; i<=ntypes; i++)
1704 if(!strcmp(typeset[i], tokname)) {
1710 typeset[numbval] = cstash(tokname);
1719 c = Bgetrune(finput);
1720 if(c == '\n' || c <= 0)
1721 error("illegal or missing ' or \"" );
1726 c = Bgetrune(finput);
1731 c = runetochar(&tokname[i], &rune);
1739 switch(c = Bgetrune(finput)) {
1740 case '0': return TERM;
1741 case '<': return LEFT;
1742 case '2': return BINARY;
1743 case '>': return RIGHT;
1745 case '\\': return MARK;
1746 case '=': return PREC;
1747 case '{': return LCURLY;
1748 default: reserve = 1;
1755 base = (c=='0')? 8: 10;
1756 for(c = Bgetrune(finput); isdigit(c); c = Bgetrune(finput))
1757 numbval = numbval*base + (c-'0');
1761 if(islower(c) || isupper(c) || c=='_' || c=='.' || c=='$') {
1763 while(islower(c) || isupper(c) || isdigit(c) ||
1764 c == '-' || c=='_' || c=='.' || c=='$') {
1765 if(reserve && isupper(c))
1768 c = runetochar(&tokname[i], &rune);
1771 c = Bgetrune(finput);
1779 /* find a reserved word */
1781 for(c=0; resrv[c].name; c++)
1782 if(strcmp(tokname, resrv[c].name) == 0)
1783 return resrv[c].value;
1784 error("invalid escape, or illegal reserved word: %s", tokname);
1787 /* look ahead to distinguish IDENTIFIER from IDENTCOLON */
1788 c = Bgetrune(finput);
1789 while(c == ' ' || c == '\t'|| c == '\n' || c == '\f' || c == '/') {
1792 /* look for comments */
1794 peekline += skipcom();
1795 c = Bgetrune(finput);
1804 * determine the type of a symbol
1812 v = nontrst[t-NTBASE].value;
1814 v = TYPE(toklev[t]);
1816 error("must specify type for %s", (t>=NTBASE)?
1817 nontrst[t-NTBASE].name: tokset[t].name);
1822 chfind(int t, char *s)
1829 if(!strcmp(s, tokset[i].name))
1832 if(!strcmp(s, nontrst[i].name))
1835 /* cannot find name */
1837 error("%s should have been defined earlier", s);
1842 * copy the union declaration to the output, and the define file if present
1850 Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile);
1851 Bprint(ftable, "typedef union ");
1853 Bprint(fdefine, "\ntypedef union ");
1857 if((c=Bgetrune(finput)) == Beof)
1858 error("EOF encountered while processing %%union");
1859 Bputrune(ftable, c);
1861 Bputrune(fdefine, c);
1872 /* we are finished copying */
1874 Bprint(ftable, " YYSTYPE;\n");
1876 Bprint(fdefine, "\tYYSTYPE;\nextern\tYYSTYPE\tyylval;\n");
1884 * copies code between \{ and \}
1892 c = Bgetrune(finput);
1894 c = Bgetrune(finput);
1897 Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile);
1900 if((c=Bgetrune(finput)) == '}')
1902 Bputc(ftable, '\\');
1905 if((c=Bgetrune(finput)) == '}')
1909 Bputrune(ftable, c);
1912 c = Bgetrune(finput);
1914 error("eof before %%}");
1918 * skip over comments
1919 * skipcom is called after reading a '/'
1927 /* i is the number of lines skipped */
1929 if(Bgetrune(finput) != '*')
1930 error("illegal comment");
1931 c = Bgetrune(finput);
1934 if((c=Bgetrune(finput)) == '/')
1938 c = Bgetrune(finput);
1940 error("EOF inside comment");
1945 * copy C action to the next ; or closing }
1951 int brac, match, j, s, fnd, tok;
1953 Bprint(faction, "\n#line\t%d\t\"%s\"\n", lineno, infile);
1957 c = Bgetrune(finput);
1962 Bputrune(faction, c);
1974 c = Bgetrune(finput);
1976 /* type description */
1979 if(gettok() != TYPENAME)
1980 error("bad syntax on $<ident> clause");
1982 c = Bgetrune(finput);
1985 Bprint(faction, "yyval");
1987 /* put out the proper tag... */
1990 tok = fdtype(*prdptr[nprod]);
1991 Bprint(faction, ".%s", typeset[tok]);
1997 c = Bgetrune(finput);
2003 c = Bgetrune(finput);
2008 error("Illegal use of $%d", j+offset);
2011 Bprint(faction, "yypt[-%d].yyv", -j);
2013 /* put out the proper tag */
2015 if(j+offset <= 0 && tok < 0)
2016 error("must specify type of $%d", j+offset);
2018 tok = fdtype(prdptr[nprod][j+offset]);
2019 Bprint(faction, ".%s", typeset[tok]);
2023 if(isupper(c) || islower(c) || c == '_' || c == '.') {
2024 int tok; /* tok used oustide for type info */
2026 /* look for $name */
2028 if(gettok() != IDENTIFIER)
2029 error("$ must be followed by an identifier");
2030 tok = chfind(2, tokname);
2031 if((c = Bgetrune(finput)) != '#') {
2035 if(gettok() != NUMBER) {
2036 error("# must be followed by number");
2040 for(j=1; j<=offset; ++j)
2041 if(tok == prdptr[nprod][j]) {
2047 error("$name or $name#number not found");
2049 Bputc(faction, '$');
2051 Bputc(faction, '-');
2058 Bputrune(faction, c);
2062 /* look for comments */
2063 Bputrune(faction, c);
2064 c = Bgetrune(finput);
2068 /* it really is a comment */
2069 Bputrune(faction, c);
2070 c = Bgetrune(finput);
2073 Bputrune(faction, c);
2074 if((c=Bgetrune(finput)) == '/')
2077 Bputrune(faction, c);
2080 c = Bgetrune(finput);
2082 error("EOF inside comment");
2085 /* character constant */
2090 /* character string */
2094 Bputrune(faction, c);
2095 while(c = Bgetrune(finput)) {
2097 Bputrune(faction, c);
2098 c = Bgetrune(finput);
2105 error("newline in string or char. const.");
2106 Bputrune(faction, c);
2108 error("EOF in string or character constant");
2111 error("action does not terminate");
2119 Bputrune(faction, c);
2124 openup(char *stem, int dflag, int vflag, int ytab, char *ytabc)
2129 sprint(buf, "%s.%s", stem, FILEU);
2130 foutput = Bopen(buf, OWRITE);
2132 error("cannot open %s", buf);
2135 sprint(buf, "%s.%s", stem, FILEDEBUG);
2136 if((fdebug = Bopen(buf, OWRITE)) == 0)
2137 error("can't open %s", buf);
2140 sprint(buf, "%s.%s", stem, FILED);
2141 fdefine = Bopen(buf, OWRITE);
2143 error("can't create %s", buf);
2146 sprint(buf, "%s.%s", stem, OFILE);
2149 ftable = Bopen(buf, OWRITE);
2151 error("cannot open table file %s", buf);
2155 * print the output for the states
2163 Bprint(ftable, "short yyexca[] =\n{");
2165 Bprint(fdebug, "char* yystates[] =\n{\n");
2167 /* output the stuff for state i */
2169 nolook = tystate[i]!=MUSTLOOKAHEAD;
2172 /* output actions */
2174 aryfil(temp1, ntokens+nnonter+1, 0);
2177 if(c > 1 && c < NTBASE && temp1[c] == 0) {
2179 if(c == *(v->pitem))
2180 putitem(v->pitem+1, (Lkset*)0);
2181 temp1[c] = state(c);
2183 if(c > NTBASE && temp1[(c -= NTBASE) + ntokens] == 0)
2184 temp1[c+ntokens] = amem[indgo[i]+c];
2187 temp1[1] = ACCEPTCODE;
2189 /* now, we have the shifts; look at the reductions */
2198 if(BIT(u->ws.lset, k)) {
2202 if(temp1[k] < 0) { /* reduce/reduce conflict */
2205 "\n%d: reduce/reduce conflict"
2206 " (red'ns %d and %d ) on %s",
2207 i, -temp1[k], lastred,
2209 if(-temp1[k] > lastred)
2210 temp1[k] = -lastred;
2213 /* potential shift/reduce conflict */
2214 precftn( lastred, k, i );
2222 Bprint(fdebug, "};\n");
2223 Bprint(ftable, "};\n");
2224 Bprint(ftable, "#define YYNPROD %d\n", nprod);
2225 Bprint(ftable, "#define YYPRIVATE %d\n", PRIVATE);
2227 Bprint(ftable, "#define yydebug %s\n", yydebug);
2231 * pack state i from temp1 into amem
2234 apack(int *p, int n)
2236 int *pp, *qq, *rr, off, *q, *r;
2238 /* we don't need to worry about checking because
2239 * we will only look at entries known to be there...
2240 * eliminate leading and trailing 0's
2244 for(pp = p, off = 0; *pp == 0 && pp <= q; ++pp, --off)
2251 /* now, find a place for the elements from p to q, inclusive */
2252 r = &amem[ACTSIZE-1];
2253 for(rr = amem; rr <= r; rr++, off++) {
2254 for(qq = rr, pp = p; pp <= q; pp++, qq++)
2256 if(*pp != *qq && *qq != 0)
2259 /* we have found an acceptable k */
2260 if(pkdebug && foutput != 0)
2261 Bprint(foutput, "off = %d, k = %d\n", off, (int)(rr-amem));
2262 for(qq = rr, pp = p; pp <= q; pp++, qq++)
2265 error("action table overflow");
2270 if(pkdebug && foutput != 0)
2271 for(pp = amem; pp <= memp; pp += 10) {
2272 Bprint(foutput, "\t");
2273 for(qq = pp; qq <= pp+9; qq++)
2274 Bprint(foutput, "%d ", *qq);
2275 Bprint(foutput, "\n");
2280 error("no space in action table");
2285 * output the gotos for the nontermninals
2290 int i, j, k, best, count, cbest, times;
2292 /* mark begining of gotos */
2293 Bprint(ftemp, "$\n");
2294 for(i = 1; i <= nnonter; i++) {
2297 /* find the best one to make default */
2301 /* is j the most frequent */
2302 for(j = 0; j <= nstate; j++) {
2305 if(tystate[j] == best)
2308 /* is tystate[j] the most frequent */
2311 for(k = j; k <= nstate; k++)
2312 if(tystate[k] == cbest)
2320 /* best is now the default entry */
2321 zzgobest += times-1;
2322 for(j = 0; j <= nstate; j++)
2323 if(tystate[j] != 0 && tystate[j] != best) {
2324 Bprint(ftemp, "%d,%d,", j, tystate[j]);
2328 /* now, the default */
2332 Bprint(ftemp, "%d\n", best);
2337 * output the gotos for nonterminal c
2346 /* first, find nonterminals with gotos on c */
2347 aryfil(temp1, nnonter+1, 0);
2354 /* cc is a nonterminal */
2355 if((cc=prdptr[i][1]-NTBASE) >= 0)
2356 /* cc has a goto on c */
2357 if(temp1[cc] != 0) {
2359 /* thus, the left side of production i does too */
2360 cc = *prdptr[i]-NTBASE;
2361 if(temp1[cc] == 0) {
2368 /* now, we have temp1[c] = 1 if a goto on c in closure of cc */
2369 if(g2debug && foutput != 0) {
2370 Bprint(foutput, "%s: gotos on ", nontrst[c].name);
2373 Bprint(foutput, "%s ", nontrst[i].name);
2374 Bprint(foutput, "\n");
2377 /* now, go through and put gotos into tystate */
2378 aryfil(tystate, nstate, 0);
2381 if((cc = *p->pitem) >= NTBASE)
2382 /* goto on c is possible */
2383 if(temp1[cc-NTBASE]) {
2384 tystate[i] = amem[indgo[i]+c];
2390 * decide a shift/reduce conflict by precedence.
2391 * r is a rule number, t a token number
2392 * the conflict is in state s
2393 * temp1[t] is changed to reflect the action
2396 precftn(int r, int t, int s)
2402 if(PLEVEL(lt) == 0 || PLEVEL(lp) == 0) {
2407 "\n%d: shift/reduce conflict (shift %d(%d), red'n %d(%d)) on %s",
2408 s, temp1[t], PLEVEL(lt), r, PLEVEL(lp), symnam(t));
2412 if(PLEVEL(lt) == PLEVEL(lp))
2415 if(PLEVEL(lt) > PLEVEL(lp))
2416 action = RASC; /* shift */
2418 action = LASC; /* reduce */
2420 case BASC: /* error action */
2423 case LASC: /* reduce */
2431 * temp1 has the actions, lastred the default
2436 int p, p0, p1, ntimes, tred, count, j, flag;
2438 /* find the best choice for lastred */
2444 if(temp1[j]+lastred == 0)
2446 /* count the number of appearances of temp1[j] */
2449 levprd[tred] |= REDFLAG;
2451 if(temp1[p]+tred == 0)
2453 if(count > ntimes) {
2460 * for error recovery, arrange that, if there is a shift on the
2461 * error recovery token, `error', that the default be the error action
2466 /* clear out entries in temp1 which equal lastred */
2468 if(temp1[p]+lastred == 0)
2472 defact[i] = lastred;
2475 if((p1=temp1[p0]) != 0) {
2480 if(p1 == ACCEPTCODE) {
2488 Bprint(ftable, "-1, %d,\n", i);
2489 Bprint(ftable, "\t%d, %d,\n", p0, p1);
2493 Bprint(ftemp, "%d,%d,", p0, p1);
2498 Bprint(ftable, "\t-2, %d,\n", lastred);
2500 Bprint(ftemp, "\n");
2515 Bprint(fdebug, " 0, /*%d*/\n", i);
2517 Bprint(fdebug, " \"");
2519 Bprint(fdebug, "%s\\n", writem(pp->pitem));
2520 if(tystate[i] == MUSTLOOKAHEAD)
2521 WSLOOP(wsets + (pstate[i+1] - pstate[i]), u)
2523 Bprint(fdebug, "%s\\n", writem(u->pitem));
2524 Bprint(fdebug, "\", /*%d*/\n", i);
2529 Bprint(foutput, "\nstate %d\n", i);
2531 Bprint(foutput, "\t%s\n", writem(pp->pitem));
2532 if(tystate[i] == MUSTLOOKAHEAD)
2533 /* print out empty productions in closure */
2534 WSLOOP(wsets+(pstate[i+1]-pstate[i]), u)
2536 Bprint(foutput, "\t%s\n", writem(u->pitem));
2538 /* check for state equal to another */
2540 if((j1=temp1[j0]) != 0) {
2541 Bprint(foutput, "\n\t%s ", symnam(j0));
2542 /* shift, error, or accept */
2544 if(j1 == ACCEPTCODE)
2545 Bprint(foutput, "accept");
2548 Bprint(foutput, "error");
2550 Bprint(foutput, "shift %d", j1);
2552 Bprint(foutput, "reduce %d (src line %d)", -j1, rlines[-j1]);
2555 /* output the final production */
2557 Bprint(foutput, "\n\t. reduce %d (src line %d)\n\n",
2558 lastred, rlines[lastred]);
2560 Bprint(foutput, "\n\t. error\n\n");
2562 /* now, output nonterminal actions */
2564 for(j0 = 1; j0 <= nnonter; j0++) {
2567 Bprint(foutput, "\t%s goto %d\n", symnam(j0+NTBASE), temp1[j1]);
2572 warray(char *s, int *v, int n)
2576 Bprint(ftable, "short %s[] =\n{", s);
2579 Bprint(ftable, "\n");
2580 Bprint(ftable, "%4d", v[i]);
2583 Bprint(ftable, "\n};\n");
2586 Bprint(ftable, ",");
2591 * in order to free up the mem and amem arrays for the optimizer,
2592 * and still be able to output yyr1, etc., after the sizes of
2593 * the action array is known, we hide the nonterminals
2594 * derived by productions in levprd.
2605 if(!(levprd[i] & REDFLAG)) {
2608 Bprint(foutput, "Rule not reduced: %s\n", writem(prdptr[i]));
2610 levprd[i] = *prdptr[i] - NTBASE;
2613 print("%d rules never reduced\n", j);
2619 int i, *p, j, k, *q;
2621 /* read the arrays from tempfile and set parameters */
2622 finput = Bopen(tempname, OREAD);
2624 error("optimizer cannot open tempfile");
2635 temp1[nstate] = pmem - mem0;
2641 error("bad tempfile");
2647 temp1[nstate] = yypgo[0] = pmem - mem0;
2652 yypgo[nnonter] = pmem-mem0;
2658 error("bad tempfile");
2663 yypgo[nnonter--] = pmem - mem0;
2664 for(i = 0; i < nstate; i++) {
2667 q = mem0 + temp1[i+1];
2668 for(p = mem0 + temp1[i]; p < q ; p += 2) {
2674 /* nontrivial situation */
2676 /* j is now the range */
2677 /* j -= k; *//* call scj */
2681 tystate[i] = (temp1[i+1]-temp1[i]) + 2*j;
2686 /* initialize ggreed table */
2687 for(i = 1; i <= nnonter; i++) {
2691 /* minimum entry index is always 0 */
2692 q = mem0 + yypgo[i+1] - 1;
2693 for(p = mem0+yypgo[i]; p < q ; p += 2) {
2698 ggreed[i] = ggreed[i] + 2*j;
2703 /* now, prepare to put the shift actions into the amem array */
2704 for(i = 0; i < ACTSIZE; i++)
2707 for(i = 0; i < nstate; i++) {
2708 if(tystate[i] == 0 && adb > 1)
2709 Bprint(ftable, "State %d: null\n", i);
2712 while((i = nxti()) != NOMORE)
2718 /* print amem array */
2720 for(p = amem; p <= maxa; p += 10) {
2721 Bprint(ftable, "%4d ", (int)(p-amem));
2722 for(i = 0; i < 10; ++i)
2723 Bprint(ftable, "%4d ", p[i]);
2724 Bprint(ftable, "\n");
2727 /* write out the output appropriate to the language */
2736 int *p, *r, *s, *q1, *q2;
2738 /* enter gotos on nonterminal i into array amem */
2741 q2 = mem0+ yypgo[i+1] - 1;
2742 q1 = mem0 + yypgo[i];
2744 /* now, find amem place for it */
2745 for(p = amem; p < &amem[ACTSIZE]; p++) {
2748 for(r = q1; r < q2; r += 2) {
2753 if((maxa = s) > &amem[ACTSIZE])
2754 error("a array overflow");
2756 /* we have found amem spot */
2759 if((maxa = p) > &amem[ACTSIZE])
2760 error("a array overflow");
2761 for(r = q1; r < q2; r += 2) {
2767 Bprint(ftable, "Nonterminal %d, entry at %d\n", i, pgo[i]);
2772 error("cannot place goto %d\n", i);
2778 int *r, *s, n, flag, j, *q1, *q2;
2782 /* enter state i into the amem array */
2783 q2 = mem0+temp1[i+1];
2785 /* find an acceptable place */
2786 for(n = -maxoff; n < ACTSIZE; n++) {
2788 for(r = q1; r < q2; r += 2) {
2789 if((s = *r + n + amem) < amem)
2798 /* check that the position equals another only if the states are identical */
2799 for(j=0; j<nstate; j++) {
2802 /* we have some disagreement */
2805 if(temp1[j+1]+temp1[i] == temp1[j]+temp1[i+1]) {
2807 /* states are equal */
2811 "State %d: entry at %d equals state %d\n",
2816 /* we have some disagreement */
2821 for(r = q1; r < q2; r += 2) {
2822 if((s = *r+n+amem) >= &amem[ACTSIZE])
2823 error("out of space in optimizer a array");
2826 if(*s != 0 && *s != r[1])
2827 error("clobber of a array, pos'n %d, by %d", s-amem, r[1]);
2832 Bprint(ftable, "State %d: entry at %d\n", i, indgo[i]);
2836 error("Error; failure to place state %d\n", i);
2849 for(i = 1; i <= nnonter; i++)
2850 if(ggreed[i] >= max) {
2854 for(i = 0; i < nstate; ++i)
2855 if(tystate[i] >= max) {
2860 Bprint(ftable, "nxti = %d, max = %d\n", maxi, max);
2878 for(p = maxa; p >= amem; p--)
2882 Bprint(foutput, "Optimizer space used: input %d/%d, output %d/%d\n",
2883 (int)(pmem-mem0+1), MEMSIZE, (int)(maxa-amem+1), ACTSIZE);
2884 Bprint(foutput, "%d table entries, %d zero\n", (int)(maxa-amem+1), i);
2885 Bprint(foutput, "maximum spread: %d, maximum offset: %d\n", maxspr, maxoff);
2889 * this version is for C
2890 * write out the optimized parser
2895 Bprint(ftable, "#define\tYYLAST\t%d\n", (int)(maxa-amem+1));
2896 arout("yyact", amem, (maxa-amem)+1);
2897 arout("yypact", indgo, nstate);
2898 arout("yypgo", pgo, nnonter+1);
2902 arout(char *s, int *v, int n)
2906 Bprint(ftable, "short %s[] =\n{", s);
2907 for(i = 0; i < n;) {
2909 Bprint(ftable, "\n");
2910 Bprint(ftable, "%4d", v[i]);
2913 Bprint(ftable, "\n};\n");
2915 Bprint(ftable, ",");
2920 * read and convert an integer from the standard input
2921 * return the terminating character
2922 * blanks, tabs, and newlines are ignored
2931 while((c=Bgetrune(finput)) != Beof) {
2933 val = val*10 + c-'0';
2945 if(pmem >= &mem0[MEMSIZE])
2946 error("out of space");