[BACK]Return to verbose.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / yacc

Annotation of src/usr.bin/yacc/verbose.c, Revision 1.2

1.2     ! deraadt     1: /*     $NetBSD: verbose.c,v 1.4 1996/03/19 03:21:50 jtc Exp $  */
        !             2:
        !             3: /*
        !             4:  * Copyright (c) 1989 The Regents of the University of California.
        !             5:  * All rights reserved.
        !             6:  *
        !             7:  * This code is derived from software contributed to Berkeley by
        !             8:  * Robert Paul Corbett.
        !             9:  *
        !            10:  * Redistribution and use in source and binary forms, with or without
        !            11:  * modification, are permitted provided that the following conditions
        !            12:  * are met:
        !            13:  * 1. Redistributions of source code must retain the above copyright
        !            14:  *    notice, this list of conditions and the following disclaimer.
        !            15:  * 2. Redistributions in binary form must reproduce the above copyright
        !            16:  *    notice, this list of conditions and the following disclaimer in the
        !            17:  *    documentation and/or other materials provided with the distribution.
        !            18:  * 3. All advertising materials mentioning features or use of this software
        !            19:  *    must display the following acknowledgement:
        !            20:  *     This product includes software developed by the University of
        !            21:  *     California, Berkeley and its contributors.
        !            22:  * 4. Neither the name of the University nor the names of its contributors
        !            23:  *    may be used to endorse or promote products derived from this software
        !            24:  *    without specific prior written permission.
        !            25:  *
        !            26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
        !            27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            36:  * SUCH DAMAGE.
        !            37:  */
        !            38:
1.1       deraadt    39: #ifndef lint
1.2     ! deraadt    40: #if 0
        !            41: static char sccsid[] = "@(#)verbose.c  5.3 (Berkeley) 1/20/91";
        !            42: #else
        !            43: static char rcsid[] = "$NetBSD: verbose.c,v 1.4 1996/03/19 03:21:50 jtc Exp $";
        !            44: #endif
1.1       deraadt    45: #endif /* not lint */
                     46:
                     47: #include "defs.h"
                     48:
                     49: static short *null_rules;
                     50:
                     51: verbose()
                     52: {
                     53:     register int i;
                     54:
                     55:     if (!vflag) return;
                     56:
                     57:     null_rules = (short *) MALLOC(nrules*sizeof(short));
                     58:     if (null_rules == 0) no_space();
                     59:     fprintf(verbose_file, "\f\n");
                     60:     for (i = 0; i < nstates; i++)
                     61:        print_state(i);
                     62:     FREE(null_rules);
                     63:
                     64:     if (nunused)
                     65:        log_unused();
                     66:     if (SRtotal || RRtotal)
                     67:        log_conflicts();
                     68:
                     69:     fprintf(verbose_file, "\n\n%d terminals, %d nonterminals\n", ntokens,
                     70:            nvars);
                     71:     fprintf(verbose_file, "%d grammar rules, %d states\n", nrules - 2, nstates);
                     72: }
                     73:
                     74:
                     75: log_unused()
                     76: {
                     77:     register int i;
                     78:     register short *p;
                     79:
                     80:     fprintf(verbose_file, "\n\nRules never reduced:\n");
                     81:     for (i = 3; i < nrules; ++i)
                     82:     {
                     83:        if (!rules_used[i])
                     84:        {
                     85:            fprintf(verbose_file, "\t%s :", symbol_name[rlhs[i]]);
                     86:            for (p = ritem + rrhs[i]; *p >= 0; ++p)
                     87:                fprintf(verbose_file, " %s", symbol_name[*p]);
                     88:            fprintf(verbose_file, "  (%d)\n", i - 2);
                     89:        }
                     90:     }
                     91: }
                     92:
                     93:
                     94: log_conflicts()
                     95: {
                     96:     register int i;
                     97:
                     98:     fprintf(verbose_file, "\n\n");
                     99:     for (i = 0; i < nstates; i++)
                    100:     {
                    101:        if (SRconflicts[i] || RRconflicts[i])
                    102:        {
                    103:            fprintf(verbose_file, "State %d contains ", i);
                    104:            if (SRconflicts[i] == 1)
                    105:                fprintf(verbose_file, "1 shift/reduce conflict");
                    106:            else if (SRconflicts[i] > 1)
                    107:                fprintf(verbose_file, "%d shift/reduce conflicts",
                    108:                        SRconflicts[i]);
                    109:            if (SRconflicts[i] && RRconflicts[i])
                    110:                fprintf(verbose_file, ", ");
                    111:            if (RRconflicts[i] == 1)
                    112:                fprintf(verbose_file, "1 reduce/reduce conflict");
                    113:            else if (RRconflicts[i] > 1)
                    114:                fprintf(verbose_file, "%d reduce/reduce conflicts",
                    115:                        RRconflicts[i]);
                    116:            fprintf(verbose_file, ".\n");
                    117:        }
                    118:     }
                    119: }
                    120:
                    121:
                    122: print_state(state)
                    123: int state;
                    124: {
                    125:     if (state)
                    126:        fprintf(verbose_file, "\n\n");
                    127:     if (SRconflicts[state] || RRconflicts[state])
                    128:        print_conflicts(state);
                    129:     fprintf(verbose_file, "state %d\n", state);
                    130:     print_core(state);
                    131:     print_nulls(state);
                    132:     print_actions(state);
                    133: }
                    134:
                    135:
                    136: print_conflicts(state)
                    137: int state;
                    138: {
                    139:     register int symbol, act, number;
                    140:     register action *p;
                    141:
                    142:     symbol = -1;
                    143:     for (p = parser[state]; p; p = p->next)
                    144:     {
                    145:        if (p->suppressed == 2)
                    146:            continue;
                    147:
                    148:        if (p->symbol != symbol)
                    149:        {
                    150:            symbol = p->symbol;
                    151:            number = p->number;
                    152:            if (p->action_code == SHIFT)
                    153:                act = SHIFT;
                    154:            else
                    155:                act = REDUCE;
                    156:        }
                    157:        else if (p->suppressed == 1)
                    158:        {
                    159:            if (state == final_state && symbol == 0)
                    160:            {
                    161:                fprintf(verbose_file, "%d: shift/reduce conflict \
                    162: (accept, reduce %d) on $end\n", state, p->number - 2);
                    163:            }
                    164:            else
                    165:            {
                    166:                if (act == SHIFT)
                    167:                {
                    168:                    fprintf(verbose_file, "%d: shift/reduce conflict \
                    169: (shift %d, reduce %d) on %s\n", state, number, p->number - 2,
                    170:                            symbol_name[symbol]);
                    171:                }
                    172:                else
                    173:                {
                    174:                    fprintf(verbose_file, "%d: reduce/reduce conflict \
                    175: (reduce %d, reduce %d) on %s\n", state, number - 2, p->number - 2,
                    176:                            symbol_name[symbol]);
                    177:                }
                    178:            }
                    179:        }
                    180:     }
                    181: }
                    182:
                    183:
                    184: print_core(state)
                    185: int state;
                    186: {
                    187:     register int i;
                    188:     register int k;
                    189:     register int rule;
                    190:     register core *statep;
                    191:     register short *sp;
                    192:     register short *sp1;
                    193:
                    194:     statep = state_table[state];
                    195:     k = statep->nitems;
                    196:
                    197:     for (i = 0; i < k; i++)
                    198:     {
                    199:        sp1 = sp = ritem + statep->items[i];
                    200:
                    201:        while (*sp >= 0) ++sp;
                    202:        rule = -(*sp);
                    203:        fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
                    204:
                    205:         for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
                    206:            fprintf(verbose_file, "%s ", symbol_name[*sp]);
                    207:
                    208:        putc('.', verbose_file);
                    209:
                    210:        while (*sp >= 0)
                    211:        {
                    212:            fprintf(verbose_file, " %s", symbol_name[*sp]);
                    213:            sp++;
                    214:        }
                    215:        fprintf(verbose_file, "  (%d)\n", -2 - *sp);
                    216:     }
                    217: }
                    218:
                    219:
                    220: print_nulls(state)
                    221: int state;
                    222: {
                    223:     register action *p;
                    224:     register int i, j, k, nnulls;
                    225:
                    226:     nnulls = 0;
                    227:     for (p = parser[state]; p; p = p->next)
                    228:     {
                    229:        if (p->action_code == REDUCE &&
                    230:                (p->suppressed == 0 || p->suppressed == 1))
                    231:        {
                    232:            i = p->number;
                    233:            if (rrhs[i] + 1 == rrhs[i+1])
                    234:            {
                    235:                for (j = 0; j < nnulls && i > null_rules[j]; ++j)
                    236:                    continue;
                    237:
                    238:                if (j == nnulls)
                    239:                {
                    240:                    ++nnulls;
                    241:                    null_rules[j] = i;
                    242:                }
                    243:                else if (i != null_rules[j])
                    244:                {
                    245:                    ++nnulls;
                    246:                    for (k = nnulls - 1; k > j; --k)
                    247:                        null_rules[k] = null_rules[k-1];
                    248:                    null_rules[j] = i;
                    249:                }
                    250:            }
                    251:        }
                    252:     }
                    253:
                    254:     for (i = 0; i < nnulls; ++i)
                    255:     {
                    256:        j = null_rules[i];
                    257:        fprintf(verbose_file, "\t%s : .  (%d)\n", symbol_name[rlhs[j]],
                    258:                j - 2);
                    259:     }
                    260:     fprintf(verbose_file, "\n");
                    261: }
                    262:
                    263:
                    264: print_actions(stateno)
                    265: int stateno;
                    266: {
                    267:     register action *p;
                    268:     register shifts *sp;
                    269:     register int as;
                    270:
                    271:     if (stateno == final_state)
                    272:        fprintf(verbose_file, "\t$end  accept\n");
                    273:
                    274:     p = parser[stateno];
                    275:     if (p)
                    276:     {
                    277:        print_shifts(p);
                    278:        print_reductions(p, defred[stateno]);
                    279:     }
                    280:
                    281:     sp = shift_table[stateno];
                    282:     if (sp && sp->nshifts > 0)
                    283:     {
                    284:        as = accessing_symbol[sp->shift[sp->nshifts - 1]];
                    285:        if (ISVAR(as))
                    286:            print_gotos(stateno);
                    287:     }
                    288: }
                    289:
                    290:
                    291: print_shifts(p)
                    292: register action *p;
                    293: {
                    294:     register int count;
                    295:     register action *q;
                    296:
                    297:     count = 0;
                    298:     for (q = p; q; q = q->next)
                    299:     {
                    300:        if (q->suppressed < 2 && q->action_code == SHIFT)
                    301:            ++count;
                    302:     }
                    303:
                    304:     if (count > 0)
                    305:     {
                    306:        for (; p; p = p->next)
                    307:        {
                    308:            if (p->action_code == SHIFT && p->suppressed == 0)
                    309:                fprintf(verbose_file, "\t%s  shift %d\n",
                    310:                            symbol_name[p->symbol], p->number);
                    311:        }
                    312:     }
                    313: }
                    314:
                    315:
                    316: print_reductions(p, defred)
                    317: register action *p;
                    318: register int defred;
                    319: {
                    320:     register int k, anyreds;
                    321:     register action *q;
                    322:
                    323:     anyreds = 0;
                    324:     for (q = p; q ; q = q->next)
                    325:     {
                    326:        if (q->action_code == REDUCE && q->suppressed < 2)
                    327:        {
                    328:            anyreds = 1;
                    329:            break;
                    330:        }
                    331:     }
                    332:
                    333:     if (anyreds == 0)
                    334:        fprintf(verbose_file, "\t.  error\n");
                    335:     else
                    336:     {
                    337:        for (; p; p = p->next)
                    338:        {
                    339:            if (p->action_code == REDUCE && p->number != defred)
                    340:            {
                    341:                k = p->number - 2;
                    342:                if (p->suppressed == 0)
                    343:                    fprintf(verbose_file, "\t%s  reduce %d\n",
                    344:                            symbol_name[p->symbol], k);
                    345:            }
                    346:        }
                    347:
                    348:         if (defred > 0)
                    349:            fprintf(verbose_file, "\t.  reduce %d\n", defred - 2);
                    350:     }
                    351: }
                    352:
                    353:
                    354: print_gotos(stateno)
                    355: int stateno;
                    356: {
                    357:     register int i, k;
                    358:     register int as;
                    359:     register short *to_state;
                    360:     register shifts *sp;
                    361:
                    362:     putc('\n', verbose_file);
                    363:     sp = shift_table[stateno];
                    364:     to_state = sp->shift;
                    365:     for (i = 0; i < sp->nshifts; ++i)
                    366:     {
                    367:        k = to_state[i];
                    368:        as = accessing_symbol[k];
                    369:        if (ISVAR(as))
                    370:            fprintf(verbose_file, "\t%s  goto %d\n", symbol_name[as], k);
                    371:     }
                    372: }