Annotation of coherent/b/bin/yacc/y5.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * write goto table and parsing table out to temp file
                      3:  * to be picked up later by the optimizer
                      4:  * at this point, the tables are fit to be processed by the C parser
                      5:  */
                      6: #include "yacc.h"
                      7: #include <action.h>
                      8: 
                      9: char   *actns[] = {
                     10:        "SHIFT",
                     11:        "REDUCE",
                     12:        "ACCEPT",
                     13:        "ERROR"
                     14: };
                     15: 
                     16: static int *uv;
                     17: 
                     18: go2out()
                     19: {
                     20:        register i;
                     21: 
                     22:        if( verbose )
                     23:                fprintf(listout, "\n\nGoto table:\n\n");
                     24:        uv = (int *)yalloc(nstates, sizeof *uv);
                     25:        rewopt();
                     26:        for(i=1; i<nnonterm; i++)
                     27:                outgo2(i);
                     28:        free(uv);
                     29: }
                     30: 
                     31: outgo2(n)
                     32: {
                     33:        extern yyredns, yygodef;
                     34:        int max, sno, j;
                     35:        struct sym *sp;
                     36:        struct go2n g2;
                     37: 
                     38: 
                     39:        sp = ntrmptr[n];
                     40:        for(j=0; j<sp->s_nstates; j++)
                     41:                uv[j] = 0;
                     42:        max = 0;
                     43:        for(j=0; j<sp->s_nstates; j++) {
                     44:                sno = findnt( &states[sp->s_states[j]], n+NTBASE)->ng_st;
                     45:                if( ++uv[sno] > uv[max] )
                     46:                        max = sno;
                     47:        }
                     48:        yygodef += uv[max];
                     49:        g2.from = (YYGOTO<<YYACTSH) | n;
                     50:        g2.to = sp->s_nstates - uv[max] + 1;
                     51:        yyredns += g2.to;
                     52:        fwrite(&g2, sizeof g2, 1, optout);
                     53:        if( verbose )
                     54:                fprintf(listout, "%s:\n\n", ntrmptr[n]->s_name);
                     55:        for(j=0; j<sp->s_nstates; j++) {
                     56:                sno = sp->s_states[j];
                     57:                if( (g2.to=findnt(&states[sno], n+NTBASE)->ng_st) == max )
                     58:                        continue;
                     59:                g2.from = sno;
                     60:                if( verbose )
                     61:                        fprintf(listout, "\t%d\t%d\n",  g2.from, g2.to);
                     62:                fwrite(&g2, sizeof g2, 1, optout);
                     63:        }
                     64:        g2.from = YYOTHERS;
                     65:        g2.to = max;
                     66:        fwrite(&g2, sizeof g2, 1, optout);
                     67:        if( verbose )
                     68:                fprintf(listout, "\t.\t%d\n\n", max);
                     69: }
                     70: 
                     71: paout()
                     72: {
                     73:        struct actn *rdact;
                     74:        register i;
                     75:        
                     76:        if (verbose)
                     77:                fprintf(listout, "\n\nParsing action table:\n\n");
                     78: 
                     79:        rdact = (struct actn *) yalloc (maxreds, sizeof(*rdact));
                     80:        for(i = 0; i < nstates; i++)
                     81:                outstate(i, rdact);
                     82:        free(rdact);
                     83: }
                     84: 
                     85: outstate(n, rdact)
                     86: struct actn rdact[];
                     87: {
                     88:        extern yydefact, yypact;
                     89:        register i, k;
                     90:        register struct state *stp;
                     91:        int size, max, errshift, pno, maxp, j, l;
                     92:        struct lset shls, rdls;
                     93:        struct actn act;
                     94: 
                     95:        stp = &states[n];
                     96:        zerolset(&shls);
                     97:        for(i=0; i<stp->s_tgo; i++) 
                     98:                setbit(&shls, stp->s_tgos[i].tg_trm);
                     99:        max = 0;
                    100:        maxp = -1;
                    101:        for(i=k=0; i<stp->s_nred; i++) {
                    102:                pno = stp->s_reds[i].rd_prod->p_prodno;
                    103:                copylset(&rdls, stp->s_reds[i].rd_lset);
                    104:                resolve(n, i, &shls, &rdls);
                    105:                size = 0;
                    106:                for(j=first(&rdls); j>=0; j=next(&rdls, j)) {
                    107:                        for(l=0; l<k; l++) 
                    108:                                if( rdact[l].a_chr==j ) {
                    109:                                        redred(n, pno, rdact[l].a_no&YYAMASK, j);
                    110:                                        goto nextj; /* C needs next <var> */
                    111:                                }
                    112:                        bounded(l, maxreds, "reductions");
                    113:                        rdact[k].a_chr = j;
                    114:                        rdact[k++].a_no = (YYREDACT<<YYACTSH) | pno;
                    115:                        size++;
                    116:                nextj:
                    117:                        ;
                    118:                }
                    119:                if( size>max ) {
                    120:                        max = size;
                    121:                        maxp = pno;
                    122:                }
                    123:        }
                    124:        if( bit(&shls, ERRNO) || maxp==0 ) /* shift on error or accept */
                    125:                maxp = -1;
                    126: 
                    127:        /* count total number of actions */
                    128:        size = k + lcount(&shls) + !bit(&shls,ERRNO); /* shifts+reds+default */
                    129:        if( maxp >= 0 ) {
                    130:                size -= max;
                    131:                yydefact += max;
                    132:        }
                    133:        yypact += size;
                    134:        act.a_chr = size;
                    135:        act.a_no = (YYPACTION<<YYACTSH) | n;
                    136:        if( verbose )
                    137:                fprintf(listout, "State %d (size %d):\n\n", n, size);
                    138:        fwrite(&act, sizeof act, 1, optout);
                    139: 
                    140:        /* now have shifts in shls redns in rdact */
                    141: 
                    142:        /* output shifts */
                    143:        for(j=0; j<stp->s_tgo; j++) {
                    144:                if( !bit(&shls, i = stp->s_tgos[j].tg_trm) )
                    145:                        continue;
                    146:                if( i==ERRNO ) { /* will be made default */
                    147:                        errshift = stp->s_tgos[j].tg_st;
                    148:                        continue;
                    149:                }
                    150:                act.a_chr = i;
                    151:                act.a_no = (YYSHIFTACT<<YYACTSH) | stp->s_tgos[j].tg_st;
                    152:                wract(&act);
                    153:        }
                    154: 
                    155:        /* output reductions */
                    156:        for(i=0; i<k; i++) {
                    157:                if( (pno = rdact[i].a_no&YYAMASK) == maxp )
                    158:                        continue;
                    159:                if( pno==0 ) /* $accept -> start $end . */
                    160:                        rdact[i].a_no = YYACCEPTACT<<YYACTSH;
                    161:                wract(&rdact[i]);
                    162:        }
                    163: 
                    164:        /* default action */
                    165:        act.a_chr = YYOTHERS;
                    166:        if( bit(&shls, ERRNO) ) {
                    167:                act.a_no = (YYSHIFTACT<<YYACTSH) | errshift;
                    168:        } else if( maxp>=0 )
                    169:                act.a_no = YYREDACT<<YYACTSH | maxp;
                    170:        else
                    171:                act.a_no = (YYERRACT<<YYACTSH);
                    172:        wract(&act);
                    173: }
                    174: 
                    175: wract(actp)
                    176: register struct actn *actp;
                    177: {
                    178:        register unsigned actn;
                    179:        actn = actp->a_no>>YYACTSH;
                    180:        if( verbose ) {
                    181:                fprintf(listout, "\t%s\t%s", actp->a_chr==YYOTHERS?".":
                    182:                    trmptr[actp->a_chr]->s_name, actns[actn]);
                    183:                if( actn<=YYREDACT )
                    184:                        fprintf(listout, "\t%d", actp->a_no&YYAMASK);
                    185:                fprintf(listout, "\n");
                    186:        }
                    187:        fwrite(actp, sizeof *actp, 1, optout);
                    188: }
                    189: 
                    190: lcount(lp)
                    191: struct lset *lp;
                    192: {
                    193:        register n, count;
                    194:        register unsigned char *ucp;
                    195:        extern unsigned char bcount [];
                    196: 
                    197:        count = 0;
                    198:        ucp = lp->l_bits;
                    199:        n = LSETSIZE;
                    200:        do {
                    201:                count += bcount [* ucp ++];
                    202:        } while (-- n);
                    203:        return count;
                    204: }
                    205: 
                    206: setdiff(ld, ls)
                    207: struct lset *ld, *ls;
                    208: {
                    209:        register n;
                    210:        register unsigned char *udcp, *uscp;
                    211: 
                    212:        n = LSETSIZE;
                    213:        udcp = ld->l_bits;
                    214:        uscp = ls->l_bits;
                    215:        do {
                    216:                * udcp ++ &= ~ * uscp ++;
                    217:        } while (-- n);
                    218: }
                    219: 
                    220: setunion(ld, ls)
                    221: struct lset *ld, *ls;
                    222: {
                    223:        register n;
                    224:        register unsigned char *udcp, *uscp;
                    225: 
                    226:        n = LSETSIZE;
                    227:        udcp = ld->l_bits;
                    228:        uscp = ls->l_bits;
                    229:        do {
                    230:                * udcp ++ |= * uscp ++;
                    231:        } while (-- n);
                    232: }
                    233: 
                    234: setint(ld, ls)
                    235: struct lset *ld, *ls;
                    236: {
                    237:        register n;
                    238:        register unsigned char *udcp, *uscp;
                    239: 
                    240:        n = LSETSIZE;
                    241:        udcp = ld->l_bits;
                    242:        uscp = ls->l_bits;
                    243:        do {
                    244:                * udcp ++ &= * uscp ++;
                    245:        } while (-- n);
                    246: }
                    247: 
                    248: copylset(ld, ls)
                    249: struct lset *ld, *ls;
                    250: {
                    251:        memcpy (ld->l_bits, ls->l_bits, sizeof (ld->l_bits));
                    252: }
                    253: 
                    254: zerolset(ld)
                    255: struct lset *ld;
                    256: {
                    257:        memset (ld->l_bits, 0, sizeof (ld->l_bits));
                    258: }
                    259: 
                    260: resolve(sno, p, sls, rls)
                    261: int p;
                    262: struct lset *sls, *rls;
                    263: {
                    264:        register i;
                    265:        struct lset cls;
                    266:        int todo;
                    267:        struct prod *pp;
                    268:        struct sym *tsp;
                    269: 
                    270:        copylset(&cls, sls);
                    271:        setint(&cls, rls);
                    272:        pp = states[sno].s_reds[p].rd_prod;
                    273:        for(i=first(&cls); i>=0; i=next(&cls,i) ) { /* conflict on term i */
                    274:                tsp = trmptr[i];
                    275:                if( tsp->s_ass<=UNASSOC || pp->p_ass<=UNASSOC ) {
                    276:                        shiftred(sno, pp->p_prodno, i);
                    277:                        clrbit(rls, i);
                    278:                        continue;
                    279:                        /* conflict resolved in favour of shift */
                    280:                }
                    281:                if( pp->p_prc > tsp->s_prc )
                    282:                        todo = LASSOC;
                    283:                else if( pp->p_prc < tsp->s_prc )
                    284:                        todo = RASSOC;
                    285:                else
                    286:                        todo = tsp->s_ass;
                    287:                switch( todo ) {
                    288:                case LASSOC: /* reduce */
                    289:                        clrbit(sls, i);
                    290:                        break;
                    291: 
                    292:                case RASSOC:
                    293:                        clrbit(rls, i);
                    294:                        break;
                    295: 
                    296:                case UNASSOC: /* non-self associating op */
                    297:                        clrbit(rls, i);
                    298:                        clrbit(sls, i);
                    299:                        break;
                    300:                }
                    301:        }
                    302: }
                    303: 
                    304: shiftred(sno, pn, tok)
                    305: {
                    306:        register i;
                    307:        struct state *stp;
                    308: 
                    309:        stp = &states[sno];
                    310:        ++nsrconf;
                    311:        if( !verbose )
                    312:                return;
                    313:        for(i=0; i<stp->s_tgo; i++)
                    314:                if( stp->s_tgos[i].tg_trm==tok )
                    315:                        break;
                    316:        fprintf(listout, "State %d: Shift/Reduce conflict ", sno);
                    317:        fprintf(listout, "(shift %d, red'n %d) on %s\n",
                    318:                stp->s_tgos[i].tg_st, pn, trmptr[tok]->s_name);
                    319: }
                    320: 
                    321: redred(sno, pn1, pn2, tok)
                    322: {
                    323:        struct state *stp;
                    324: 
                    325:        stp = &states[sno];
                    326:        ++nrrconf;
                    327:        if( !verbose )
                    328:                return;
                    329:        fprintf(listout, "State %d: Reduce/Reduce conflict ", sno);
                    330:        fprintf(listout, "(red'n %d, red'n %d) on %s\n",
                    331:                pn1, pn2, trmptr[tok]->s_name);
                    332: }
                    333: 
                    334: clrbit(lp, n)
                    335: struct lset *lp;
                    336: register unsigned n;
                    337: {
                    338:        lp->l_bits [n / CHAR_BIT] &= ~ (1 << (n & (CHAR_BIT - 1)) );
                    339: }
                    340: 
                    341: setbit(lp, n)
                    342: struct lset *lp;
                    343: register unsigned n;
                    344: {
                    345:        lp->l_bits [n / CHAR_BIT] |= 1 << (n & (CHAR_BIT - 1));
                    346: }
                    347: 
                    348: bit(lp, n)
                    349: struct lset *lp;
                    350: register unsigned n;
                    351: {
                    352:        return (lp->l_bits [n / CHAR_BIT] >> (n & (CHAR_BIT - 1))) & 1;
                    353: }
                    354: 
                    355: first(lp)
                    356: struct lset *lp;
                    357: {
                    358:        return next (lp,-1);
                    359: }
                    360: 
                    361: next(lp, n)
                    362: struct lset *lp;
                    363: register unsigned n;
                    364: {
                    365:        register w;
                    366:        register unsigned char *ucp;
                    367:        extern char ltab[];
                    368: 
                    369:        if (++ n >= LSETSIZE * CHAR_BIT)
                    370:                return -1;
                    371:        ucp = lp->l_bits;
                    372:        w = ucp [n / CHAR_BIT];
                    373:        w &= ~ ((1 << (n & (CHAR_BIT - 1))) - 1);
                    374:        n /= CHAR_BIT;
                    375:        while (w == 0) {
                    376:                if (++ n >= LSETSIZE)
                    377:                        return -1;
                    378:                w = ucp [n];
                    379:        }
                    380:        return n * CHAR_BIT + ltab [w];
                    381: }
                    382: 
                    383: struct ntgo *
                    384: findnt(stp, nt)
                    385: register struct state *stp;
                    386: {
                    387:        register i;
                    388: 
                    389:        for(i=0; i<stp->s_ntgo; i++ )
                    390:                if( stp->s_ntgos[i].ng_nt==nt )
                    391:                        return( &stp->s_ntgos[i] );
                    392:        yyerror(NLNO|FATAL, "oops in findnt");
                    393: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.