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

Annotation of src/usr.bin/yacc/output.c, Revision 1.4

1.4     ! deraadt     1: /*     $OpenBSD: output.c,v 1.3 1996/06/26 05:44:39 deraadt Exp $      */
1.2       deraadt     2: /*     $NetBSD: output.c,v 1.4 1996/03/19 03:21:41 jtc Exp $   */
                      3:
                      4: /*
                      5:  * Copyright (c) 1989 The Regents of the University of California.
                      6:  * All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Robert Paul Corbett.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *     This product includes software developed by the University of
                     22:  *     California, Berkeley and its contributors.
                     23:  * 4. Neither the name of the University nor the names of its contributors
                     24:  *    may be used to endorse or promote products derived from this software
                     25:  *    without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     28:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     29:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     30:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     31:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     32:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     33:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     34:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     35:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     36:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     37:  * SUCH DAMAGE.
                     38:  */
                     39:
1.1       deraadt    40: #ifndef lint
1.2       deraadt    41: #if 0
                     42: static char sccsid[] = "@(#)output.c   5.7 (Berkeley) 5/24/93";
                     43: #else
1.4     ! deraadt    44: static char rcsid[] = "$OpenBSD: output.c,v 1.3 1996/06/26 05:44:39 deraadt Exp $";
1.2       deraadt    45: #endif
1.1       deraadt    46: #endif /* not lint */
                     47:
                     48: #include "defs.h"
                     49:
                     50: static int nvectors;
                     51: static int nentries;
                     52: static short **froms;
                     53: static short **tos;
                     54: static short *tally;
                     55: static short *width;
                     56: static short *state_count;
                     57: static short *order;
                     58: static short *base;
                     59: static short *pos;
                     60: static int maxtable;
                     61: static short *table;
                     62: static short *check;
                     63: static int lowzero;
                     64: static int high;
                     65:
                     66:
                     67: output()
                     68: {
                     69:     free_itemsets();
                     70:     free_shifts();
                     71:     free_reductions();
                     72:     output_prefix();
                     73:     output_stored_text();
                     74:     output_defines();
                     75:     output_rule_data();
                     76:     output_yydefred();
                     77:     output_actions();
                     78:     free_parser();
                     79:     output_debug();
                     80:     output_stype();
                     81:     if (rflag) write_section(tables);
                     82:     write_section(header);
                     83:     output_trailing_text();
                     84:     write_section(body);
                     85:     output_semantic_actions();
                     86:     write_section(trailer);
                     87: }
                     88:
                     89:
                     90: output_prefix()
                     91: {
                     92:     if (symbol_prefix == NULL)
                     93:        symbol_prefix = "yy";
                     94:     else
                     95:     {
                     96:        ++outline;
                     97:        fprintf(code_file, "#define yyparse %sparse\n", symbol_prefix);
                     98:        ++outline;
                     99:        fprintf(code_file, "#define yylex %slex\n", symbol_prefix);
                    100:        ++outline;
                    101:        fprintf(code_file, "#define yyerror %serror\n", symbol_prefix);
                    102:        ++outline;
                    103:        fprintf(code_file, "#define yychar %schar\n", symbol_prefix);
                    104:        ++outline;
                    105:        fprintf(code_file, "#define yyval %sval\n", symbol_prefix);
                    106:        ++outline;
                    107:        fprintf(code_file, "#define yylval %slval\n", symbol_prefix);
                    108:        ++outline;
                    109:        fprintf(code_file, "#define yydebug %sdebug\n", symbol_prefix);
                    110:        ++outline;
                    111:        fprintf(code_file, "#define yynerrs %snerrs\n", symbol_prefix);
                    112:        ++outline;
                    113:        fprintf(code_file, "#define yyerrflag %serrflag\n", symbol_prefix);
                    114:        ++outline;
                    115:        fprintf(code_file, "#define yyss %sss\n", symbol_prefix);
                    116:        ++outline;
1.4     ! deraadt   117:        fprintf(code_file, "#define yysslim %ssslim\n", symbol_prefix);
        !           118:        ++outline;
1.1       deraadt   119:        fprintf(code_file, "#define yyssp %sssp\n", symbol_prefix);
                    120:        ++outline;
                    121:        fprintf(code_file, "#define yyvs %svs\n", symbol_prefix);
                    122:        ++outline;
                    123:        fprintf(code_file, "#define yyvsp %svsp\n", symbol_prefix);
1.4     ! deraadt   124:        ++outline;
        !           125:        fprintf(code_file, "#define yystacksize %sstacksize\n", symbol_prefix);
1.1       deraadt   126:        ++outline;
                    127:        fprintf(code_file, "#define yylhs %slhs\n", symbol_prefix);
                    128:        ++outline;
                    129:        fprintf(code_file, "#define yylen %slen\n", symbol_prefix);
                    130:        ++outline;
                    131:        fprintf(code_file, "#define yydefred %sdefred\n", symbol_prefix);
                    132:        ++outline;
                    133:        fprintf(code_file, "#define yydgoto %sdgoto\n", symbol_prefix);
                    134:        ++outline;
                    135:        fprintf(code_file, "#define yysindex %ssindex\n", symbol_prefix);
                    136:        ++outline;
                    137:        fprintf(code_file, "#define yyrindex %srindex\n", symbol_prefix);
                    138:        ++outline;
                    139:        fprintf(code_file, "#define yygindex %sgindex\n", symbol_prefix);
                    140:        ++outline;
                    141:        fprintf(code_file, "#define yytable %stable\n", symbol_prefix);
                    142:        ++outline;
                    143:        fprintf(code_file, "#define yycheck %scheck\n", symbol_prefix);
                    144:        ++outline;
                    145:        fprintf(code_file, "#define yyname %sname\n", symbol_prefix);
                    146:        ++outline;
                    147:        fprintf(code_file, "#define yyrule %srule\n", symbol_prefix);
                    148:     }
                    149:     ++outline;
                    150:     fprintf(code_file, "#define YYPREFIX \"%s\"\n", symbol_prefix);
                    151: }
                    152:
                    153:
                    154: output_rule_data()
                    155: {
                    156:     register int i;
                    157:     register int j;
                    158:
                    159:
                    160:     fprintf(output_file, "short %slhs[] = {%42d,", symbol_prefix,
                    161:            symbol_value[start_symbol]);
                    162:
                    163:     j = 10;
                    164:     for (i = 3; i < nrules; i++)
                    165:     {
                    166:        if (j >= 10)
                    167:        {
                    168:            if (!rflag) ++outline;
                    169:            putc('\n', output_file);
                    170:            j = 1;
                    171:        }
                    172:         else
                    173:            ++j;
                    174:
                    175:         fprintf(output_file, "%5d,", symbol_value[rlhs[i]]);
                    176:     }
                    177:     if (!rflag) outline += 2;
                    178:     fprintf(output_file, "\n};\n");
                    179:
                    180:     fprintf(output_file, "short %slen[] = {%42d,", symbol_prefix, 2);
                    181:
                    182:     j = 10;
                    183:     for (i = 3; i < nrules; i++)
                    184:     {
                    185:        if (j >= 10)
                    186:        {
                    187:            if (!rflag) ++outline;
                    188:            putc('\n', output_file);
                    189:            j = 1;
                    190:        }
                    191:        else
                    192:          j++;
                    193:
                    194:         fprintf(output_file, "%5d,", rrhs[i + 1] - rrhs[i] - 1);
                    195:     }
                    196:     if (!rflag) outline += 2;
                    197:     fprintf(output_file, "\n};\n");
                    198: }
                    199:
                    200:
                    201: output_yydefred()
                    202: {
                    203:     register int i, j;
                    204:
                    205:     fprintf(output_file, "short %sdefred[] = {%39d,", symbol_prefix,
                    206:            (defred[0] ? defred[0] - 2 : 0));
                    207:
                    208:     j = 10;
                    209:     for (i = 1; i < nstates; i++)
                    210:     {
                    211:        if (j < 10)
                    212:            ++j;
                    213:        else
                    214:        {
                    215:            if (!rflag) ++outline;
                    216:            putc('\n', output_file);
                    217:            j = 1;
                    218:        }
                    219:
                    220:        fprintf(output_file, "%5d,", (defred[i] ? defred[i] - 2 : 0));
                    221:     }
                    222:
                    223:     if (!rflag) outline += 2;
                    224:     fprintf(output_file, "\n};\n");
                    225: }
                    226:
                    227:
                    228: output_actions()
                    229: {
                    230:     nvectors = 2*nstates + nvars;
                    231:
                    232:     froms = NEW2(nvectors, short *);
                    233:     tos = NEW2(nvectors, short *);
                    234:     tally = NEW2(nvectors, short);
                    235:     width = NEW2(nvectors, short);
                    236:
                    237:     token_actions();
                    238:     FREE(lookaheads);
                    239:     FREE(LA);
                    240:     FREE(LAruleno);
                    241:     FREE(accessing_symbol);
                    242:
                    243:     goto_actions();
                    244:     FREE(goto_map + ntokens);
                    245:     FREE(from_state);
                    246:     FREE(to_state);
                    247:
                    248:     sort_actions();
                    249:     pack_table();
                    250:     output_base();
                    251:     output_table();
                    252:     output_check();
                    253: }
                    254:
                    255:
                    256: token_actions()
                    257: {
                    258:     register int i, j;
                    259:     register int shiftcount, reducecount;
                    260:     register int max, min;
                    261:     register short *actionrow, *r, *s;
                    262:     register action *p;
                    263:
                    264:     actionrow = NEW2(2*ntokens, short);
                    265:     for (i = 0; i < nstates; ++i)
                    266:     {
                    267:        if (parser[i])
                    268:        {
                    269:            for (j = 0; j < 2*ntokens; ++j)
                    270:            actionrow[j] = 0;
                    271:
                    272:            shiftcount = 0;
                    273:            reducecount = 0;
                    274:            for (p = parser[i]; p; p = p->next)
                    275:            {
                    276:                if (p->suppressed == 0)
                    277:                {
                    278:                    if (p->action_code == SHIFT)
                    279:                    {
                    280:                        ++shiftcount;
                    281:                        actionrow[p->symbol] = p->number;
                    282:                    }
                    283:                    else if (p->action_code == REDUCE && p->number != defred[i])
                    284:                    {
                    285:                        ++reducecount;
                    286:                        actionrow[p->symbol + ntokens] = p->number;
                    287:                    }
                    288:                }
                    289:            }
                    290:
                    291:            tally[i] = shiftcount;
                    292:            tally[nstates+i] = reducecount;
                    293:            width[i] = 0;
                    294:            width[nstates+i] = 0;
                    295:            if (shiftcount > 0)
                    296:            {
                    297:                froms[i] = r = NEW2(shiftcount, short);
                    298:                tos[i] = s = NEW2(shiftcount, short);
                    299:                min = MAXSHORT;
                    300:                max = 0;
                    301:                for (j = 0; j < ntokens; ++j)
                    302:                {
                    303:                    if (actionrow[j])
                    304:                    {
                    305:                        if (min > symbol_value[j])
                    306:                            min = symbol_value[j];
                    307:                        if (max < symbol_value[j])
                    308:                            max = symbol_value[j];
                    309:                        *r++ = symbol_value[j];
                    310:                        *s++ = actionrow[j];
                    311:                    }
                    312:                }
                    313:                width[i] = max - min + 1;
                    314:            }
                    315:            if (reducecount > 0)
                    316:            {
                    317:                froms[nstates+i] = r = NEW2(reducecount, short);
                    318:                tos[nstates+i] = s = NEW2(reducecount, short);
                    319:                min = MAXSHORT;
                    320:                max = 0;
                    321:                for (j = 0; j < ntokens; ++j)
                    322:                {
                    323:                    if (actionrow[ntokens+j])
                    324:                    {
                    325:                        if (min > symbol_value[j])
                    326:                            min = symbol_value[j];
                    327:                        if (max < symbol_value[j])
                    328:                            max = symbol_value[j];
                    329:                        *r++ = symbol_value[j];
                    330:                        *s++ = actionrow[ntokens+j] - 2;
                    331:                    }
                    332:                }
                    333:                width[nstates+i] = max - min + 1;
                    334:            }
                    335:        }
                    336:     }
                    337:     FREE(actionrow);
                    338: }
                    339:
                    340: goto_actions()
                    341: {
                    342:     register int i, j, k;
                    343:
                    344:     state_count = NEW2(nstates, short);
                    345:
                    346:     k = default_goto(start_symbol + 1);
                    347:     fprintf(output_file, "short %sdgoto[] = {%40d,", symbol_prefix, k);
                    348:     save_column(start_symbol + 1, k);
                    349:
                    350:     j = 10;
                    351:     for (i = start_symbol + 2; i < nsyms; i++)
                    352:     {
                    353:        if (j >= 10)
                    354:        {
                    355:            if (!rflag) ++outline;
                    356:            putc('\n', output_file);
                    357:            j = 1;
                    358:        }
                    359:        else
                    360:            ++j;
                    361:
                    362:        k = default_goto(i);
                    363:        fprintf(output_file, "%5d,", k);
                    364:        save_column(i, k);
                    365:     }
                    366:
                    367:     if (!rflag) outline += 2;
                    368:     fprintf(output_file, "\n};\n");
                    369:     FREE(state_count);
                    370: }
                    371:
                    372: int
                    373: default_goto(symbol)
                    374: int symbol;
                    375: {
                    376:     register int i;
                    377:     register int m;
                    378:     register int n;
                    379:     register int default_state;
                    380:     register int max;
                    381:
                    382:     m = goto_map[symbol];
                    383:     n = goto_map[symbol + 1];
                    384:
                    385:     if (m == n) return (0);
                    386:
                    387:     for (i = 0; i < nstates; i++)
                    388:        state_count[i] = 0;
                    389:
                    390:     for (i = m; i < n; i++)
                    391:        state_count[to_state[i]]++;
                    392:
                    393:     max = 0;
                    394:     default_state = 0;
                    395:     for (i = 0; i < nstates; i++)
                    396:     {
                    397:        if (state_count[i] > max)
                    398:        {
                    399:            max = state_count[i];
                    400:            default_state = i;
                    401:        }
                    402:     }
                    403:
                    404:     return (default_state);
                    405: }
                    406:
                    407:
                    408:
                    409: save_column(symbol, default_state)
                    410: int symbol;
                    411: int default_state;
                    412: {
                    413:     register int i;
                    414:     register int m;
                    415:     register int n;
                    416:     register short *sp;
                    417:     register short *sp1;
                    418:     register short *sp2;
                    419:     register int count;
                    420:     register int symno;
                    421:
                    422:     m = goto_map[symbol];
                    423:     n = goto_map[symbol + 1];
                    424:
                    425:     count = 0;
                    426:     for (i = m; i < n; i++)
                    427:     {
                    428:        if (to_state[i] != default_state)
                    429:            ++count;
                    430:     }
                    431:     if (count == 0) return;
                    432:
                    433:     symno = symbol_value[symbol] + 2*nstates;
                    434:
                    435:     froms[symno] = sp1 = sp = NEW2(count, short);
                    436:     tos[symno] = sp2 = NEW2(count, short);
                    437:
                    438:     for (i = m; i < n; i++)
                    439:     {
                    440:        if (to_state[i] != default_state)
                    441:        {
                    442:            *sp1++ = from_state[i];
                    443:            *sp2++ = to_state[i];
                    444:        }
                    445:     }
                    446:
                    447:     tally[symno] = count;
                    448:     width[symno] = sp1[-1] - sp[0] + 1;
                    449: }
                    450:
                    451: sort_actions()
                    452: {
                    453:   register int i;
                    454:   register int j;
                    455:   register int k;
                    456:   register int t;
                    457:   register int w;
                    458:
                    459:   order = NEW2(nvectors, short);
                    460:   nentries = 0;
                    461:
                    462:   for (i = 0; i < nvectors; i++)
                    463:     {
                    464:       if (tally[i] > 0)
                    465:        {
                    466:          t = tally[i];
                    467:          w = width[i];
                    468:          j = nentries - 1;
                    469:
                    470:          while (j >= 0 && (width[order[j]] < w))
                    471:            j--;
                    472:
                    473:          while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
                    474:            j--;
                    475:
                    476:          for (k = nentries - 1; k > j; k--)
                    477:            order[k + 1] = order[k];
                    478:
                    479:          order[j + 1] = i;
                    480:          nentries++;
                    481:        }
                    482:     }
                    483: }
                    484:
                    485:
                    486: pack_table()
                    487: {
                    488:     register int i;
                    489:     register int place;
                    490:     register int state;
                    491:
                    492:     base = NEW2(nvectors, short);
                    493:     pos = NEW2(nentries, short);
                    494:
                    495:     maxtable = 1000;
                    496:     table = NEW2(maxtable, short);
                    497:     check = NEW2(maxtable, short);
                    498:
                    499:     lowzero = 0;
                    500:     high = 0;
                    501:
                    502:     for (i = 0; i < maxtable; i++)
                    503:        check[i] = -1;
                    504:
                    505:     for (i = 0; i < nentries; i++)
                    506:     {
                    507:        state = matching_vector(i);
                    508:
                    509:        if (state < 0)
                    510:            place = pack_vector(i);
                    511:        else
                    512:            place = base[state];
                    513:
                    514:        pos[i] = place;
                    515:        base[order[i]] = place;
                    516:     }
                    517:
                    518:     for (i = 0; i < nvectors; i++)
                    519:     {
                    520:        if (froms[i])
                    521:            FREE(froms[i]);
                    522:        if (tos[i])
                    523:            FREE(tos[i]);
                    524:     }
                    525:
                    526:     FREE(froms);
                    527:     FREE(tos);
                    528:     FREE(pos);
                    529: }
                    530:
                    531:
                    532: /*  The function matching_vector determines if the vector specified by */
                    533: /*  the input parameter matches a previously considered        vector.  The    */
                    534: /*  test at the start of the function checks if the vector represents  */
                    535: /*  a row of shifts over terminal symbols or a row of reductions, or a */
                    536: /*  column of shifts over a nonterminal symbol.  Berkeley Yacc does not        */
                    537: /*  check if a column of shifts over a nonterminal symbols matches a   */
                    538: /*  previously considered vector.  Because of the nature of LR parsing */
                    539: /*  tables, no two columns can match.  Therefore, the only possible    */
                    540: /*  match would be between a row and a column.  Such matches are       */
                    541: /*  unlikely.  Therefore, to save time, no attempt is made to see if a */
                    542: /*  column matches a previously considered vector.                     */
                    543: /*                                                                     */
                    544: /*  Matching_vector is poorly designed.  The test could easily be made */
                    545: /*  faster.  Also, it depends on the vectors being in a specific       */
                    546: /*  order.                                                             */
                    547:
                    548: int
                    549: matching_vector(vector)
                    550: int vector;
                    551: {
                    552:     register int i;
                    553:     register int j;
                    554:     register int k;
                    555:     register int t;
                    556:     register int w;
                    557:     register int match;
                    558:     register int prev;
                    559:
                    560:     i = order[vector];
                    561:     if (i >= 2*nstates)
                    562:        return (-1);
                    563:
                    564:     t = tally[i];
                    565:     w = width[i];
                    566:
                    567:     for (prev = vector - 1; prev >= 0; prev--)
                    568:     {
                    569:        j = order[prev];
                    570:        if (width[j] != w || tally[j] != t)
                    571:            return (-1);
                    572:
                    573:        match = 1;
                    574:        for (k = 0; match && k < t; k++)
                    575:        {
                    576:            if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
                    577:                match = 0;
                    578:        }
                    579:
                    580:        if (match)
                    581:            return (j);
                    582:     }
                    583:
                    584:     return (-1);
                    585: }
                    586:
                    587:
                    588:
                    589: int
                    590: pack_vector(vector)
                    591: int vector;
                    592: {
                    593:     register int i, j, k, l;
                    594:     register int t;
                    595:     register int loc;
                    596:     register int ok;
                    597:     register short *from;
                    598:     register short *to;
                    599:     int newmax;
                    600:
                    601:     i = order[vector];
                    602:     t = tally[i];
                    603:     assert(t);
                    604:
                    605:     from = froms[i];
                    606:     to = tos[i];
                    607:
                    608:     j = lowzero - from[0];
                    609:     for (k = 1; k < t; ++k)
                    610:        if (lowzero - from[k] > j)
                    611:            j = lowzero - from[k];
                    612:     for (;; ++j)
                    613:     {
                    614:        if (j == 0)
                    615:            continue;
                    616:        ok = 1;
                    617:        for (k = 0; ok && k < t; k++)
                    618:        {
                    619:            loc = j + from[k];
                    620:            if (loc >= maxtable)
                    621:            {
                    622:                if (loc >= MAXTABLE)
                    623:                    fatal("maximum table size exceeded");
                    624:
                    625:                newmax = maxtable;
                    626:                do { newmax += 200; } while (newmax <= loc);
                    627:                table = (short *) REALLOC(table, newmax*sizeof(short));
                    628:                if (table == 0) no_space();
                    629:                check = (short *) REALLOC(check, newmax*sizeof(short));
                    630:                if (check == 0) no_space();
                    631:                for (l  = maxtable; l < newmax; ++l)
                    632:                {
                    633:                    table[l] = 0;
                    634:                    check[l] = -1;
                    635:                }
                    636:                maxtable = newmax;
                    637:            }
                    638:
                    639:            if (check[loc] != -1)
                    640:                ok = 0;
                    641:        }
                    642:        for (k = 0; ok && k < vector; k++)
                    643:        {
                    644:            if (pos[k] == j)
                    645:                ok = 0;
                    646:        }
                    647:        if (ok)
                    648:        {
                    649:            for (k = 0; k < t; k++)
                    650:            {
                    651:                loc = j + from[k];
                    652:                table[loc] = to[k];
                    653:                check[loc] = from[k];
                    654:                if (loc > high) high = loc;
                    655:            }
                    656:
                    657:            while (check[lowzero] != -1)
                    658:                ++lowzero;
                    659:
                    660:            return (j);
                    661:        }
                    662:     }
                    663: }
                    664:
                    665:
                    666:
                    667: output_base()
                    668: {
                    669:     register int i, j;
                    670:
                    671:     fprintf(output_file, "short %ssindex[] = {%39d,", symbol_prefix, base[0]);
                    672:
                    673:     j = 10;
                    674:     for (i = 1; i < nstates; i++)
                    675:     {
                    676:        if (j >= 10)
                    677:        {
                    678:            if (!rflag) ++outline;
                    679:            putc('\n', output_file);
                    680:            j = 1;
                    681:        }
                    682:        else
                    683:            ++j;
                    684:
                    685:        fprintf(output_file, "%5d,", base[i]);
                    686:     }
                    687:
                    688:     if (!rflag) outline += 2;
                    689:     fprintf(output_file, "\n};\nshort %srindex[] = {%39d,", symbol_prefix,
                    690:            base[nstates]);
                    691:
                    692:     j = 10;
                    693:     for (i = nstates + 1; i < 2*nstates; i++)
                    694:     {
                    695:        if (j >= 10)
                    696:        {
                    697:            if (!rflag) ++outline;
                    698:            putc('\n', output_file);
                    699:            j = 1;
                    700:        }
                    701:        else
                    702:            ++j;
                    703:
                    704:        fprintf(output_file, "%5d,", base[i]);
                    705:     }
                    706:
                    707:     if (!rflag) outline += 2;
                    708:     fprintf(output_file, "\n};\nshort %sgindex[] = {%39d,", symbol_prefix,
                    709:            base[2*nstates]);
                    710:
                    711:     j = 10;
                    712:     for (i = 2*nstates + 1; i < nvectors - 1; i++)
                    713:     {
                    714:        if (j >= 10)
                    715:        {
                    716:            if (!rflag) ++outline;
                    717:            putc('\n', output_file);
                    718:            j = 1;
                    719:        }
                    720:        else
                    721:            ++j;
                    722:
                    723:        fprintf(output_file, "%5d,", base[i]);
                    724:     }
                    725:
                    726:     if (!rflag) outline += 2;
                    727:     fprintf(output_file, "\n};\n");
                    728:     FREE(base);
                    729: }
                    730:
                    731:
                    732:
                    733: output_table()
                    734: {
                    735:     register int i;
                    736:     register int j;
                    737:
                    738:     ++outline;
                    739:     fprintf(code_file, "#define YYTABLESIZE %d\n", high);
                    740:     fprintf(output_file, "short %stable[] = {%40d,", symbol_prefix,
                    741:            table[0]);
                    742:
                    743:     j = 10;
                    744:     for (i = 1; i <= high; i++)
                    745:     {
                    746:        if (j >= 10)
                    747:        {
                    748:            if (!rflag) ++outline;
                    749:            putc('\n', output_file);
                    750:            j = 1;
                    751:        }
                    752:        else
                    753:            ++j;
                    754:
                    755:        fprintf(output_file, "%5d,", table[i]);
                    756:     }
                    757:
                    758:     if (!rflag) outline += 2;
                    759:     fprintf(output_file, "\n};\n");
                    760:     FREE(table);
                    761: }
                    762:
                    763:
                    764:
                    765: output_check()
                    766: {
                    767:     register int i;
                    768:     register int j;
                    769:
                    770:     fprintf(output_file, "short %scheck[] = {%40d,", symbol_prefix,
                    771:            check[0]);
                    772:
                    773:     j = 10;
                    774:     for (i = 1; i <= high; i++)
                    775:     {
                    776:        if (j >= 10)
                    777:        {
                    778:            if (!rflag) ++outline;
                    779:            putc('\n', output_file);
                    780:            j = 1;
                    781:        }
                    782:        else
                    783:            ++j;
                    784:
                    785:        fprintf(output_file, "%5d,", check[i]);
                    786:     }
                    787:
                    788:     if (!rflag) outline += 2;
                    789:     fprintf(output_file, "\n};\n");
                    790:     FREE(check);
                    791: }
                    792:
                    793:
                    794: int
                    795: is_C_identifier(name)
                    796: char *name;
                    797: {
                    798:     register char *s;
                    799:     register int c;
                    800:
                    801:     s = name;
                    802:     c = *s;
                    803:     if (c == '"')
                    804:     {
                    805:        c = *++s;
                    806:        if (!isalpha(c) && c != '_' && c != '$')
                    807:            return (0);
                    808:        while ((c = *++s) != '"')
                    809:        {
                    810:            if (!isalnum(c) && c != '_' && c != '$')
                    811:                return (0);
                    812:        }
                    813:        return (1);
                    814:     }
                    815:
                    816:     if (!isalpha(c) && c != '_' && c != '$')
                    817:        return (0);
                    818:     while (c = *++s)
                    819:     {
                    820:        if (!isalnum(c) && c != '_' && c != '$')
                    821:            return (0);
                    822:     }
                    823:     return (1);
                    824: }
                    825:
                    826:
                    827: output_defines()
                    828: {
                    829:     register int c, i;
                    830:     register char *s;
                    831:
                    832:     for (i = 2; i < ntokens; ++i)
                    833:     {
                    834:        s = symbol_name[i];
                    835:        if (is_C_identifier(s))
                    836:        {
                    837:            fprintf(code_file, "#define ");
                    838:            if (dflag) fprintf(defines_file, "#define ");
                    839:            c = *s;
                    840:            if (c == '"')
                    841:            {
                    842:                while ((c = *++s) != '"')
                    843:                {
                    844:                    putc(c, code_file);
                    845:                    if (dflag) putc(c, defines_file);
                    846:                }
                    847:            }
                    848:            else
                    849:            {
                    850:                do
                    851:                {
                    852:                    putc(c, code_file);
                    853:                    if (dflag) putc(c, defines_file);
                    854:                }
                    855:                while (c = *++s);
                    856:            }
                    857:            ++outline;
                    858:            fprintf(code_file, " %d\n", symbol_value[i]);
                    859:            if (dflag) fprintf(defines_file, " %d\n", symbol_value[i]);
                    860:        }
                    861:     }
                    862:
                    863:     ++outline;
                    864:     fprintf(code_file, "#define YYERRCODE %d\n", symbol_value[1]);
                    865:
                    866:     if (dflag && unionized)
                    867:     {
                    868:        fclose(union_file);
                    869:        union_file = fopen(union_file_name, "r");
                    870:        if (union_file == NULL) open_error(union_file_name);
                    871:        while ((c = getc(union_file)) != EOF)
                    872:            putc(c, defines_file);
                    873:        fprintf(defines_file, " YYSTYPE;\nextern YYSTYPE %slval;\n",
                    874:                symbol_prefix);
                    875:     }
                    876: }
                    877:
                    878:
                    879: output_stored_text()
                    880: {
                    881:     register int c;
                    882:     register FILE *in, *out;
                    883:
                    884:     fclose(text_file);
                    885:     text_file = fopen(text_file_name, "r");
                    886:     if (text_file == NULL)
                    887:        open_error(text_file_name);
                    888:     in = text_file;
                    889:     if ((c = getc(in)) == EOF)
                    890:        return;
                    891:     out = code_file;
                    892:     if (c ==  '\n')
                    893:        ++outline;
                    894:     putc(c, out);
                    895:     while ((c = getc(in)) != EOF)
                    896:     {
                    897:        if (c == '\n')
                    898:            ++outline;
                    899:        putc(c, out);
                    900:     }
                    901:     if (!lflag)
                    902:        fprintf(out, line_format, ++outline + 1, code_file_name);
                    903: }
                    904:
                    905:
                    906: output_debug()
                    907: {
                    908:     register int i, j, k, max;
                    909:     char **symnam, *s;
                    910:
                    911:     ++outline;
                    912:     fprintf(code_file, "#define YYFINAL %d\n", final_state);
                    913:     outline += 3;
                    914:     fprintf(code_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
                    915:            tflag);
                    916:     if (rflag)
                    917:        fprintf(output_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
                    918:                tflag);
                    919:
                    920:     max = 0;
                    921:     for (i = 2; i < ntokens; ++i)
                    922:        if (symbol_value[i] > max)
                    923:            max = symbol_value[i];
                    924:     ++outline;
                    925:     fprintf(code_file, "#define YYMAXTOKEN %d\n", max);
                    926:
                    927:     symnam = (char **) MALLOC((max+1)*sizeof(char *));
                    928:     if (symnam == 0) no_space();
                    929:
                    930:     /* Note that it is  not necessary to initialize the element                */
                    931:     /* symnam[max].                                                    */
                    932:     for (i = 0; i < max; ++i)
                    933:        symnam[i] = 0;
                    934:     for (i = ntokens - 1; i >= 2; --i)
                    935:        symnam[symbol_value[i]] = symbol_name[i];
                    936:     symnam[0] = "end-of-file";
                    937:
                    938:     if (!rflag) ++outline;
                    939:     fprintf(output_file, "#if YYDEBUG\nchar *%sname[] = {", symbol_prefix);
                    940:     j = 80;
                    941:     for (i = 0; i <= max; ++i)
                    942:     {
                    943:        if (s = symnam[i])
                    944:        {
                    945:            if (s[0] == '"')
                    946:            {
                    947:                k = 7;
                    948:                while (*++s != '"')
                    949:                {
                    950:                    ++k;
                    951:                    if (*s == '\\')
                    952:                    {
                    953:                        k += 2;
                    954:                        if (*++s == '\\')
                    955:                            ++k;
                    956:                    }
                    957:                }
                    958:                j += k;
                    959:                if (j > 80)
                    960:                {
                    961:                    if (!rflag) ++outline;
                    962:                    putc('\n', output_file);
                    963:                    j = k;
                    964:                }
                    965:                fprintf(output_file, "\"\\\"");
                    966:                s = symnam[i];
                    967:                while (*++s != '"')
                    968:                {
                    969:                    if (*s == '\\')
                    970:                    {
                    971:                        fprintf(output_file, "\\\\");
                    972:                        if (*++s == '\\')
                    973:                            fprintf(output_file, "\\\\");
                    974:                        else
                    975:                            putc(*s, output_file);
                    976:                    }
                    977:                    else
                    978:                        putc(*s, output_file);
                    979:                }
                    980:                fprintf(output_file, "\\\"\",");
                    981:            }
                    982:            else if (s[0] == '\'')
                    983:            {
                    984:                if (s[1] == '"')
                    985:                {
                    986:                    j += 7;
                    987:                    if (j > 80)
                    988:                    {
                    989:                        if (!rflag) ++outline;
                    990:                        putc('\n', output_file);
                    991:                        j = 7;
                    992:                    }
                    993:                    fprintf(output_file, "\"'\\\"'\",");
                    994:                }
                    995:                else
                    996:                {
                    997:                    k = 5;
                    998:                    while (*++s != '\'')
                    999:                    {
                   1000:                        ++k;
                   1001:                        if (*s == '\\')
                   1002:                        {
                   1003:                            k += 2;
                   1004:                            if (*++s == '\\')
                   1005:                                ++k;
                   1006:                        }
                   1007:                    }
                   1008:                    j += k;
                   1009:                    if (j > 80)
                   1010:                    {
                   1011:                        if (!rflag) ++outline;
                   1012:                        putc('\n', output_file);
                   1013:                        j = k;
                   1014:                    }
                   1015:                    fprintf(output_file, "\"'");
                   1016:                    s = symnam[i];
                   1017:                    while (*++s != '\'')
                   1018:                    {
                   1019:                        if (*s == '\\')
                   1020:                        {
                   1021:                            fprintf(output_file, "\\\\");
                   1022:                            if (*++s == '\\')
                   1023:                                fprintf(output_file, "\\\\");
                   1024:                            else
                   1025:                                putc(*s, output_file);
                   1026:                        }
                   1027:                        else
                   1028:                            putc(*s, output_file);
                   1029:                    }
                   1030:                    fprintf(output_file, "'\",");
                   1031:                }
                   1032:            }
                   1033:            else
                   1034:            {
                   1035:                k = strlen(s) + 3;
                   1036:                j += k;
                   1037:                if (j > 80)
                   1038:                {
                   1039:                    if (!rflag) ++outline;
                   1040:                    putc('\n', output_file);
                   1041:                    j = k;
                   1042:                }
                   1043:                putc('"', output_file);
                   1044:                do { putc(*s, output_file); } while (*++s);
                   1045:                fprintf(output_file, "\",");
                   1046:            }
                   1047:        }
                   1048:        else
                   1049:        {
                   1050:            j += 2;
                   1051:            if (j > 80)
                   1052:            {
                   1053:                if (!rflag) ++outline;
                   1054:                putc('\n', output_file);
                   1055:                j = 2;
                   1056:            }
                   1057:            fprintf(output_file, "0,");
                   1058:        }
                   1059:     }
                   1060:     if (!rflag) outline += 2;
                   1061:     fprintf(output_file, "\n};\n");
                   1062:     FREE(symnam);
                   1063:
                   1064:     if (!rflag) ++outline;
                   1065:     fprintf(output_file, "char *%srule[] = {\n", symbol_prefix);
                   1066:     for (i = 2; i < nrules; ++i)
                   1067:     {
                   1068:        fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
                   1069:        for (j = rrhs[i]; ritem[j] > 0; ++j)
                   1070:        {
                   1071:            s = symbol_name[ritem[j]];
                   1072:            if (s[0] == '"')
                   1073:            {
                   1074:                fprintf(output_file, " \\\"");
                   1075:                while (*++s != '"')
                   1076:                {
                   1077:                    if (*s == '\\')
                   1078:                    {
                   1079:                        if (s[1] == '\\')
                   1080:                            fprintf(output_file, "\\\\\\\\");
                   1081:                        else
                   1082:                            fprintf(output_file, "\\\\%c", s[1]);
                   1083:                        ++s;
                   1084:                    }
                   1085:                    else
                   1086:                        putc(*s, output_file);
                   1087:                }
                   1088:                fprintf(output_file, "\\\"");
                   1089:            }
                   1090:            else if (s[0] == '\'')
                   1091:            {
                   1092:                if (s[1] == '"')
                   1093:                    fprintf(output_file, " '\\\"'");
                   1094:                else if (s[1] == '\\')
                   1095:                {
                   1096:                    if (s[2] == '\\')
                   1097:                        fprintf(output_file, " '\\\\\\\\");
                   1098:                    else
                   1099:                        fprintf(output_file, " '\\\\%c", s[2]);
                   1100:                    s += 2;
                   1101:                    while (*++s != '\'')
                   1102:                        putc(*s, output_file);
                   1103:                    putc('\'', output_file);
                   1104:                }
                   1105:                else
                   1106:                    fprintf(output_file, " '%c'", s[1]);
                   1107:            }
                   1108:            else
                   1109:                fprintf(output_file, " %s", s);
                   1110:        }
                   1111:        if (!rflag) ++outline;
                   1112:        fprintf(output_file, "\",\n");
                   1113:     }
                   1114:
                   1115:     if (!rflag) outline += 2;
                   1116:     fprintf(output_file, "};\n#endif\n");
                   1117: }
                   1118:
                   1119:
                   1120: output_stype()
                   1121: {
                   1122:     if (!unionized && ntags == 0)
                   1123:     {
                   1124:        outline += 3;
                   1125:        fprintf(code_file, "#ifndef YYSTYPE\ntypedef int YYSTYPE;\n#endif\n");
                   1126:     }
                   1127: }
                   1128:
                   1129:
                   1130: output_trailing_text()
                   1131: {
                   1132:     register int c, last;
                   1133:     register FILE *in, *out;
                   1134:
                   1135:     if (line == 0)
                   1136:        return;
                   1137:
                   1138:     in = input_file;
                   1139:     out = code_file;
                   1140:     c = *cptr;
                   1141:     if (c == '\n')
                   1142:     {
                   1143:        ++lineno;
                   1144:        if ((c = getc(in)) == EOF)
                   1145:            return;
                   1146:        if (!lflag)
                   1147:        {
                   1148:            ++outline;
                   1149:            fprintf(out, line_format, lineno, input_file_name);
                   1150:        }
                   1151:        if (c == '\n')
                   1152:            ++outline;
                   1153:        putc(c, out);
                   1154:        last = c;
                   1155:     }
                   1156:     else
                   1157:     {
                   1158:        if (!lflag)
                   1159:        {
                   1160:            ++outline;
                   1161:            fprintf(out, line_format, lineno, input_file_name);
                   1162:        }
                   1163:        do { putc(c, out); } while ((c = *++cptr) != '\n');
                   1164:        ++outline;
                   1165:        putc('\n', out);
                   1166:        last = '\n';
                   1167:     }
                   1168:
                   1169:     while ((c = getc(in)) != EOF)
                   1170:     {
                   1171:        if (c == '\n')
                   1172:            ++outline;
                   1173:        putc(c, out);
                   1174:        last = c;
                   1175:     }
                   1176:
                   1177:     if (last != '\n')
                   1178:     {
                   1179:        ++outline;
                   1180:        putc('\n', out);
                   1181:     }
                   1182:     if (!lflag)
                   1183:        fprintf(out, line_format, ++outline + 1, code_file_name);
                   1184: }
                   1185:
                   1186:
                   1187: output_semantic_actions()
                   1188: {
                   1189:     register int c, last;
                   1190:     register FILE *out;
                   1191:
                   1192:     fclose(action_file);
                   1193:     action_file = fopen(action_file_name, "r");
                   1194:     if (action_file == NULL)
                   1195:        open_error(action_file_name);
                   1196:
                   1197:     if ((c = getc(action_file)) == EOF)
                   1198:        return;
                   1199:
                   1200:     out = code_file;
                   1201:     last = c;
                   1202:     if (c == '\n')
                   1203:        ++outline;
                   1204:     putc(c, out);
                   1205:     while ((c = getc(action_file)) != EOF)
                   1206:     {
                   1207:        if (c == '\n')
                   1208:            ++outline;
                   1209:        putc(c, out);
                   1210:        last = c;
                   1211:     }
                   1212:
                   1213:     if (last != '\n')
                   1214:     {
                   1215:        ++outline;
                   1216:        putc('\n', out);
                   1217:     }
                   1218:
                   1219:     if (!lflag)
                   1220:        fprintf(out, line_format, ++outline + 1, code_file_name);
                   1221: }
                   1222:
                   1223:
                   1224: free_itemsets()
                   1225: {
                   1226:     register core *cp, *next;
                   1227:
                   1228:     FREE(state_table);
                   1229:     for (cp = first_state; cp; cp = next)
                   1230:     {
                   1231:        next = cp->next;
                   1232:        FREE(cp);
                   1233:     }
                   1234: }
                   1235:
                   1236:
                   1237: free_shifts()
                   1238: {
                   1239:     register shifts *sp, *next;
                   1240:
                   1241:     FREE(shift_table);
                   1242:     for (sp = first_shift; sp; sp = next)
                   1243:     {
                   1244:        next = sp->next;
                   1245:        FREE(sp);
                   1246:     }
                   1247: }
                   1248:
                   1249:
                   1250:
                   1251: free_reductions()
                   1252: {
                   1253:     register reductions *rp, *next;
                   1254:
                   1255:     FREE(reduction_table);
                   1256:     for (rp = first_reduction; rp; rp = next)
                   1257:     {
                   1258:        next = rp->next;
                   1259:        FREE(rp);
                   1260:     }
                   1261: }