Annotation of coherent/d/bin/yacc/y5.c, revision 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:        register i;
        !            74: 
        !            75:        if( verbose )
        !            76:                fprintf(listout, "\n\nParsing action table:\n\n");
        !            77:        for(i=0; i<nstates; i++)
        !            78:                outstate(i);
        !            79: }
        !            80: 
        !            81: outstate(n)
        !            82: {
        !            83:        extern yydefact, yypact;
        !            84:        register i, k;
        !            85:        register struct state *stp;
        !            86:        int size, max, errshift, pno, maxp, j, l;
        !            87:        struct lset shls, rdls;
        !            88:        struct actn rdact[MAXREDS], act;
        !            89: 
        !            90:        stp = &states[n];
        !            91:        zerolset(&shls);
        !            92:        for(i=0; i<stp->s_tgo; i++) 
        !            93:                setbit(&shls, stp->s_tgos[i].tg_trm);
        !            94:        max = 0;
        !            95:        maxp = -1;
        !            96:        for(i=k=0; i<stp->s_nred; i++) {
        !            97:                pno = stp->s_reds[i].rd_prod->p_prodno;
        !            98:                copylset(&rdls, stp->s_reds[i].rd_lset);
        !            99:                resolve(n, i, &shls, &rdls);
        !           100:                size = 0;
        !           101:                for(j=first(&rdls); j>=0; j=next(&rdls, j)) {
        !           102:                        for(l=0; l<k; l++) 
        !           103:                                if( rdact[l].a_chr==j ) {
        !           104:                                        redred(n, pno, rdact[l].a_no&YYAMASK, j);
        !           105:                                        goto nextj; /* C needs next <var> */
        !           106:                                }
        !           107:                        bounded(l, MAXREDS, "reductions");
        !           108:                        rdact[k].a_chr = j;
        !           109:                        rdact[k++].a_no = (YYREDACT<<YYACTSH) | pno;
        !           110:                        size++;
        !           111:                nextj:
        !           112:                        ;
        !           113:                }
        !           114:                if( size>max ) {
        !           115:                        max = size;
        !           116:                        maxp = pno;
        !           117:                }
        !           118:        }
        !           119:        if( bit(&shls, ERRNO) || maxp==0 ) /* shift on error or accept */
        !           120:                maxp = -1;
        !           121: 
        !           122:        /* count total number of actions */
        !           123:        size = k + lcount(&shls) + !bit(&shls,ERRNO); /* shifts+reds+default */
        !           124:        if( maxp >= 0 ) {
        !           125:                size -= max;
        !           126:                yydefact += max;
        !           127:        }
        !           128:        yypact += size;
        !           129:        act.a_chr = size;
        !           130:        act.a_no = (YYPACTION<<YYACTSH) | n;
        !           131:        if( verbose )
        !           132:                fprintf(listout, "State %d (size %d):\n\n", n, size);
        !           133:        fwrite(&act, sizeof act, 1, optout);
        !           134: 
        !           135:        /* now have shifts in shls redns in rdact */
        !           136: 
        !           137:        /* output shifts */
        !           138:        for(j=0; j<stp->s_tgo; j++) {
        !           139:                if( !bit(&shls, i = stp->s_tgos[j].tg_trm) )
        !           140:                        continue;
        !           141:                if( i==ERRNO ) { /* will be made default */
        !           142:                        errshift = stp->s_tgos[j].tg_st;
        !           143:                        continue;
        !           144:                }
        !           145:                act.a_chr = i;
        !           146:                act.a_no = (YYSHIFTACT<<YYACTSH) | stp->s_tgos[j].tg_st;
        !           147:                wract(&act);
        !           148:        }
        !           149: 
        !           150:        /* output reductions */
        !           151:        for(i=0; i<k; i++) {
        !           152:                if( (pno = rdact[i].a_no&YYAMASK) == maxp )
        !           153:                        continue;
        !           154:                if( pno==0 ) /* $accept -> start $end . */
        !           155:                        rdact[i].a_no = YYACCEPTACT<<YYACTSH;
        !           156:                wract(&rdact[i]);
        !           157:        }
        !           158: 
        !           159:        /* default action */
        !           160:        act.a_chr = YYOTHERS;
        !           161:        if( bit(&shls, ERRNO) ) {
        !           162:                act.a_no = (YYSHIFTACT<<YYACTSH) | errshift;
        !           163:        } else if( maxp>=0 )
        !           164:                act.a_no = YYREDACT<<YYACTSH | maxp;
        !           165:        else
        !           166:                act.a_no = (YYERRACT<<YYACTSH);
        !           167:        wract(&act);
        !           168: }
        !           169: 
        !           170: wract(actp)
        !           171: register struct actn *actp;
        !           172: {
        !           173:        register unsigned actn;
        !           174:        actn = actp->a_no>>YYACTSH;
        !           175:        if( verbose ) {
        !           176:                fprintf(listout, "\t%s\t%s", actp->a_chr==YYOTHERS?".":
        !           177:                    trmptr[actp->a_chr]->s_name, actns[actn]);
        !           178:                if( actn<=YYREDACT )
        !           179:                        fprintf(listout, "\t%d", actp->a_no&YYAMASK);
        !           180:                fprintf(listout, "\n");
        !           181:        }
        !           182:        fwrite(actp, sizeof *actp, 1, optout);
        !           183: }
        !           184: 
        !           185: lcount(lp)
        !           186: struct lset *lp;
        !           187: {
        !           188:        register n, count;
        !           189:        register unsigned char *ucp;
        !           190:        extern unsigned char bcount[];
        !           191: 
        !           192:        count = 0;
        !           193:        ucp = lp->l_bits;
        !           194:        n = LSETSIZE;
        !           195:        do {
        !           196:                count += bcount[*ucp++];
        !           197:        } while (--n);
        !           198:        return(count);
        !           199: }
        !           200: 
        !           201: setdiff(ld, ls)
        !           202: struct lset *ld, *ls;
        !           203: {
        !           204:        register n;
        !           205:        register unsigned char *udcp, *uscp;
        !           206: 
        !           207:        n = LSETSIZE;
        !           208:        udcp = ld->l_bits;
        !           209:        uscp = ls->l_bits;
        !           210:        do {
        !           211:                *udcp++ &= ~ *uscp++;
        !           212:        } while (--n);
        !           213: }
        !           214: 
        !           215: setunion(ld, ls)
        !           216: struct lset *ld, *ls;
        !           217: {
        !           218:        register n;
        !           219:        register unsigned char *udcp, *uscp;
        !           220: 
        !           221:        n = LSETSIZE;
        !           222:        udcp = ld->l_bits;
        !           223:        uscp = ls->l_bits;
        !           224:        do {
        !           225:                *udcp++ |= *uscp++;
        !           226:        } while (--n);
        !           227: }
        !           228: 
        !           229: setint(ld, ls)
        !           230: struct lset *ld, *ls;
        !           231: {
        !           232:        register n;
        !           233:        register unsigned char *udcp, *uscp;
        !           234: 
        !           235:        n = LSETSIZE;
        !           236:        udcp = ld->l_bits;
        !           237:        uscp = ls->l_bits;
        !           238:        do {
        !           239:                *udcp++ &= *uscp++;
        !           240:        } while (--n);
        !           241: }
        !           242: 
        !           243: copylset(ld, ls)
        !           244: struct lset *ld, *ls;
        !           245: {
        !           246:        register n;
        !           247:        register unsigned char *udcp, *uscp;
        !           248: 
        !           249:        n = LSETSIZE;
        !           250:        udcp = ld->l_bits;
        !           251:        uscp = ls->l_bits;
        !           252:        do {
        !           253:                *udcp++ = *uscp++;
        !           254:        } while (--n);
        !           255: }
        !           256: 
        !           257: zerolset(ld)
        !           258: struct lset *ld;
        !           259: {
        !           260:        register n;
        !           261:        register unsigned char *udcp;
        !           262: 
        !           263:        n = LSETSIZE;
        !           264:        udcp = ld->l_bits;
        !           265:        do {
        !           266:                *udcp++ = 0;
        !           267:        } while (--n);
        !           268: }
        !           269: 
        !           270: resolve(sno, p, sls, rls)
        !           271: int p;
        !           272: struct lset *sls, *rls;
        !           273: {
        !           274:        register i;
        !           275:        struct lset cls;
        !           276:        int todo;
        !           277:        struct prod *pp;
        !           278:        struct sym *tsp;
        !           279: 
        !           280:        copylset(&cls, sls);
        !           281:        setint(&cls, rls);
        !           282:        pp = states[sno].s_reds[p].rd_prod;
        !           283:        for(i=first(&cls); i>=0; i=next(&cls,i) ) { /* conflict on term i */
        !           284:                tsp = trmptr[i];
        !           285:                if( tsp->s_ass<=UNASSOC || pp->p_ass<=UNASSOC ) {
        !           286:                        shiftred(sno, pp->p_prodno, i);
        !           287:                        clrbit(rls, i);
        !           288:                        continue;
        !           289:                        /* conflict resolved in favour of shift */
        !           290:                }
        !           291:                if( pp->p_prc > tsp->s_prc )
        !           292:                        todo = LASSOC;
        !           293:                else if( pp->p_prc < tsp->s_prc )
        !           294:                        todo = RASSOC;
        !           295:                else
        !           296:                        todo = tsp->s_ass;
        !           297:                switch( todo ) {
        !           298:                case LASSOC: /* reduce */
        !           299:                        clrbit(sls, i);
        !           300:                        break;
        !           301: 
        !           302:                case RASSOC:
        !           303:                        clrbit(rls, i);
        !           304:                        break;
        !           305: 
        !           306:                case UNASSOC: /* non-self associating op */
        !           307:                        clrbit(rls, i);
        !           308:                        clrbit(sls, i);
        !           309:                        break;
        !           310:                }
        !           311:        }
        !           312: }
        !           313: 
        !           314: shiftred(sno, pn, tok)
        !           315: {
        !           316:        register i;
        !           317:        struct state *stp;
        !           318: 
        !           319:        stp = &states[sno];
        !           320:        ++nsrconf;
        !           321:        if( !verbose )
        !           322:                return;
        !           323:        for(i=0; i<stp->s_tgo; i++)
        !           324:                if( stp->s_tgos[i].tg_trm==tok )
        !           325:                        break;
        !           326:        fprintf(listout, "State %d: Shift/Reduce conflict ", sno);
        !           327:        fprintf(listout, "(shift %d, red'n %d) on %s\n",
        !           328:                stp->s_tgos[i].tg_st, pn, trmptr[tok]->s_name);
        !           329: }
        !           330: 
        !           331: redred(sno, pn1, pn2, tok)
        !           332: {
        !           333:        struct state *stp;
        !           334: 
        !           335:        stp = &states[sno];
        !           336:        ++nrrconf;
        !           337:        if( !verbose )
        !           338:                return;
        !           339:        fprintf(listout, "State %d: Reduce/Reduce conflict ", sno);
        !           340:        fprintf(listout, "(red'n %d, red'n %d) on %s\n",
        !           341:                pn1, pn2, trmptr[tok]->s_name);
        !           342: }
        !           343: 
        !           344: clrbit(lp, n)
        !           345: struct lset *lp;
        !           346: register n;
        !           347: {
        !           348:        register unsigned char *ucp;
        !           349: 
        !           350:        ucp = lp->l_bits;
        !           351:        ucp[n>>LOGCHAR] &= ~( 01 << (n & (NBCHAR-1)) );
        !           352: }
        !           353: 
        !           354: setbit(lp, n)
        !           355: struct lset *lp;
        !           356: register n;
        !           357: {
        !           358:        register unsigned char *ucp;
        !           359: 
        !           360:        ucp = lp->l_bits;
        !           361:        ucp[n>>LOGCHAR] |= 01 << (n & (NBCHAR-1));
        !           362: }
        !           363: 
        !           364: bit(lp, n)
        !           365: struct lset *lp;
        !           366: register n;
        !           367: {
        !           368:        register unsigned char *ucp;
        !           369: 
        !           370:        ucp = lp->l_bits;
        !           371:        return( (ucp[n>>LOGCHAR] >> (n&(NBCHAR-1))) & 01 );
        !           372: }
        !           373: 
        !           374: first(lp)
        !           375: struct lset *lp;
        !           376: {
        !           377:        return(next(lp,-1));
        !           378: }
        !           379: 
        !           380: next(lp, n)
        !           381: struct lset *lp;
        !           382: register n;
        !           383: {
        !           384:        register w;
        !           385:        register unsigned char *ucp;
        !           386:        extern char ltab[];
        !           387: 
        !           388:        if( ++n >= LSETSIZE*NBCHAR )
        !           389:                return(-1);
        !           390:        ucp = lp->l_bits;
        !           391:        w = ucp[n >> LOGCHAR];
        !           392:        w &= ~ ( (01 << (n&(NBCHAR-1))) - 1);
        !           393:        n >>= LOGCHAR;
        !           394:        while (w == 0) {
        !           395:                if ( ++n >= LSETSIZE )
        !           396:                        return(-1);
        !           397:                w = ucp[n];
        !           398:        }
        !           399:        return( (n<<LOGCHAR) + ltab[w] );
        !           400: }
        !           401: 
        !           402: struct ntgo *
        !           403: findnt(stp, nt)
        !           404: register struct state *stp;
        !           405: {
        !           406:        register i;
        !           407: 
        !           408:        for(i=0; i<stp->s_ntgo; i++ )
        !           409:                if( stp->s_ntgos[i].ng_nt==nt )
        !           410:                        return( &stp->s_ntgos[i] );
        !           411:        yyerror(NLNO|FATAL, "oops in findnt");
        !           412: }

unix.superglobalmegacorp.com

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