Annotation of researchv10dc/cmd/bison/output.c, revision 1.1.1.1

1.1       root        1: /* Output the generated parsing program for bison,
                      2:    Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
                      3: 
                      4: BISON is distributed in the hope that it will be useful, but WITHOUT ANY
                      5: WARRANTY.  No author or distributor accepts responsibility to anyone
                      6: for the consequences of using it or for whether it serves any
                      7: particular purpose or works at all, unless he says so in writing.
                      8: Refer to the BISON General Public License for full details.
                      9: 
                     10: Everyone is granted permission to copy, modify and redistribute BISON,
                     11: but only under the conditions described in the BISON General Public
                     12: License.  A copy of this license is supposed to have been given to you
                     13: along with BISON so you can know your rights and responsibilities.  It
                     14: should be in a file named COPYING.  Among other things, the copyright
                     15: notice and this notice must be preserved on all copies.
                     16: 
                     17:  In other words, you are welcome to use, share and improve this program.
                     18:  You are forbidden to forbid anyone else to use, share and improve
                     19:  what you give them.   Help stamp out software-hoarding!  */
                     20: 
                     21: /* functions to output parsing data to various files.  Entries are:
                     22: 
                     23:   output_headers ()
                     24: 
                     25: Output constant strings to the beginning of certain files.
                     26: 
                     27:   output_trailers()
                     28: 
                     29: Output constant strings to the ends of certain files.
                     30: 
                     31:   output ()
                     32: 
                     33: Output the parsing tables and the parser code to ftable.
                     34: 
                     35: The parser tables consist of:  (starred ones needed only for the semantic parser)
                     36: 
                     37: yytranslate = vector mapping yylex's token numbers into bison's token numbers.
                     38: 
                     39: yytname = vector of string-names indexed by bison token number
                     40: 
                     41: yyrline = vector of line-numbers of all rules.  For yydebug printouts.
                     42: 
                     43: * yyrhs = vector of items of all rules.
                     44:         This is exactly what ritems contains.
                     45: 
                     46: * yyprhs[r] = index in yyrhs of first item for rule r.
                     47: 
                     48: yyr1[r] = symbol number of symbol that rule r derives.
                     49: 
                     50: yyr2[r] = number of symbols composing right hand side of rule r.
                     51: 
                     52: * yystos[s] = the symbol number of the symbol that leads to state s.
                     53: 
                     54: yydefact[s] = default rule to reduce with in state s,
                     55:              when yytable doesn't specify something else to do.
                     56:              Zero means the default is an error.
                     57: 
                     58: yydefgoto[i] = default state to go to after a reduction of a rule that
                     59:               generates variable ntokens + i, except when yytable
                     60:               specifies something else to do.
                     61: 
                     62: yypact[s] = index in yytable of the portion describing state s.
                     63:             The lookahed token's type is used to index that portion
                     64:             to find out what to do.
                     65: 
                     66:            If the value in yytable is positive,
                     67:            we shift the token and go to that state.
                     68: 
                     69:            If the value is negative, it is minus a rule number to reduce by.
                     70: 
                     71:            If the value is zero, the default action from yydefact[s] is used.
                     72: 
                     73: yypgoto[i] = the index in yytable of the portion describing 
                     74:              what to do after reducing a rule that derives variable i + ntokens.
                     75:              This portion is indexed by the parser state number
                     76:             as of before the text for this nonterminal was read.
                     77:             The value from yytable is the state to go to.
                     78: 
                     79: yytable = a vector filled with portions for different uses,
                     80:           found via yypact and yypgoto.
                     81: 
                     82: yycheck = a vector indexed in parallel with yytable.
                     83:          It indicates, in a roundabout way, the bounds of the
                     84:          portion you are trying to examine.
                     85: 
                     86:          Suppose that the portion of yytable starts at index p
                     87:          and the index to be examined within the portion is i.
                     88:          Then if yycheck[p+i] != i, i is outside the bounds
                     89:          of what is actually allocated, and the default
                     90:          (from yydefact or yydefgoto) should be used.
                     91:          Otherwise, yytable[p+i] should be used.
                     92: 
                     93: YYFINAL = the state number of the termination state.
                     94: YYFLAG = most negative short int.  Used to flag ??
                     95: YYNTBASE = ntokens.
                     96: 
                     97: */
                     98: 
                     99: #include <stdio.h>
                    100: #include "machine.h"
                    101: #include "new.h"
                    102: #include "files.h"
                    103: #include "gram.h"
                    104: #include "state.h"
                    105: 
                    106: #define        MAXTABLE 32767
                    107: 
                    108: 
                    109: extern char **tags;
                    110: extern int tokensetsize;
                    111: extern int final_state;
                    112: extern core **state_table;
                    113: extern shifts **shift_table;
                    114: extern errs **err_table;
                    115: extern reductions **reduction_table;
                    116: extern short *accessing_symbol;
                    117: extern unsigned *LA;
                    118: extern short *LAruleno;
                    119: extern short *lookaheads;
                    120: extern char *consistent;
                    121: extern short *goto_map;
                    122: extern short *from_state;
                    123: extern short *to_state;
                    124: 
                    125: 
                    126: static int nvectors;
                    127: static int nentries;
                    128: static short **froms;
                    129: static short **tos;
                    130: static short *tally;
                    131: static short *width;
                    132: static short *actrow;
                    133: static short *state_count;
                    134: static short *order;
                    135: static short *base;
                    136: static short *pos;
                    137: static short *table;
                    138: static short *check;
                    139: static int lowzero;
                    140: static int high;
                    141: 
                    142: 
                    143: 
                    144: #define        GUARDSTR        "\n#include \"%s\"\nextern int yyerror;\n\
                    145: extern int yycost;\nextern char * yymsg;\nextern YYSTYPE yyval;\n\n\
                    146: yyguard(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
                    147: register YYLTYPE *yylsp;\n\
                    148: {\n  yyerror = 0;\nyycost = 0;\n  yymsg = 0;\nswitch (n)\n    {"
                    149: 
                    150: #define        ACTSTR          "\n#include \"%s\"\nextern YYSTYPE yyval;\
                    151: \nextern int yychar;\
                    152: yyaction(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
                    153: register YYLTYPE *yylsp;\n{\n  switch (n)\n{"
                    154: 
                    155: #define        ACTSTR_SIMPLE   "\n  switch (yyn) {\n"
                    156: 
                    157: 
                    158: 
                    159: output_headers()
                    160: {
                    161:   if (semantic_parser)
                    162:     fprintf(fguard, GUARDSTR, attrsfile);
                    163:   fprintf(faction, (semantic_parser ? ACTSTR : ACTSTR_SIMPLE), attrsfile);
                    164: /*  if (semantic_parser)       JF moved this below
                    165:     fprintf(ftable, "#include \"%s\"\n", attrsfile);
                    166:   fprintf(ftable, "#include <stdio.h>\n\n"); */
                    167: }
                    168: 
                    169: output_trailers()
                    170: {
                    171:   if (semantic_parser)
                    172:     {
                    173:       fprintf(fguard, "\n    }\n}\n");
                    174:       fprintf(faction, "\n    }\n}\n");
                    175:     }
                    176:   else
                    177:     fprintf(faction, "\n}\n");
                    178: }
                    179: 
                    180: 
                    181: output()
                    182: {
                    183:   int c;
                    184: 
                    185:   /* output_token_defines(ftable);     /* JF put out token defines FIRST */
                    186:   if (!semantic_parser)                /* JF Put out other stuff */
                    187:     {
                    188:       rewind(fattrs);
                    189:       while ((c=getc(fattrs))!=EOF)
                    190:         putc(c,ftable);
                    191:     }
                    192: 
                    193:   if (semantic_parser)
                    194:     fprintf(ftable, "#include \"%s\"\n", attrsfile);
                    195:   fprintf(ftable, "#include <stdio.h>\n\n");
                    196: 
                    197:   /* Make "const" do nothing if not in ANSI C.  */
                    198:   fprintf (ftable, "#ifndef __STDC__\n#define const\n#endif\n\n");
                    199: 
                    200:   free_itemsets();
                    201:   output_defines();
                    202:   output_token_translations();
                    203:   if (semantic_parser)
                    204:     output_gram();
                    205:   FREE(ritem);
                    206:   if (semantic_parser)
                    207:     output_stos();
                    208:   output_rule_data();
                    209:   output_actions();
                    210:   output_parser();
                    211:   output_program();
                    212: }
                    213: 
                    214: output_token_translations()
                    215: {
                    216:   register int i, j;
                    217: /*   register short *sp; JF unused */
                    218: 
                    219:   if (translations)
                    220:     {
                    221:       fprintf(ftable,
                    222:              "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
                    223:              max_user_token_number, nsyms);
                    224:     
                    225:       if (ntokens < 127)  /* play it very safe; check maximum element value.  */
                    226:         fprintf(ftable, "\nstatic const char yytranslate[] = {     0");
                    227:       else
                    228:        fprintf(ftable, "\nstatic const short yytranslate[] = {     0");
                    229:     
                    230:       j = 10;
                    231:       for (i = 1; i <= max_user_token_number; i++)
                    232:        {
                    233:          putc(',', ftable);
                    234:     
                    235:          if (j >= 10)
                    236:            {
                    237:              putc('\n', ftable);
                    238:              j = 1;
                    239:            }
                    240:          else
                    241:            {
                    242:              j++;
                    243:            }
                    244:     
                    245:          fprintf(ftable, "%6d", token_translations[i]);
                    246:        }
                    247:     
                    248:       fprintf(ftable, "\n};\n");
                    249:     }
                    250:   else
                    251:     {
                    252:       fprintf(ftable, "\n#define YYTRANSLATE(x) (x)\n");
                    253:     } 
                    254: }
                    255: 
                    256: 
                    257: 
                    258: output_gram()
                    259: {
                    260:   register int i;
                    261:   register int j;
                    262:   register short *sp;
                    263: 
                    264:   fprintf(ftable, "\nstatic const short yyprhs[] = {     0");
                    265: 
                    266:   j = 10;
                    267:   for (i = 1; i <= nrules; i++)
                    268:     {
                    269:       putc(',', ftable);
                    270: 
                    271:       if (j >= 10)
                    272:        {
                    273:          putc('\n', ftable);
                    274:          j = 1;
                    275:        }
                    276:       else
                    277:        {
                    278:          j++;
                    279:        }
                    280: 
                    281:       fprintf(ftable, "%6d", rrhs[i]);
                    282:     }
                    283: 
                    284:   fprintf(ftable, "\n};\n\nstatic const short yyrhs[] = {%6d", ritem[0]);
                    285: 
                    286:   j = 10;
                    287:   for (sp = ritem + 1; *sp; sp++)
                    288:     {
                    289:       putc(',', ftable);
                    290: 
                    291:       if (j >= 10)
                    292:        {
                    293:          putc('\n', ftable);
                    294:          j = 1;
                    295:        }
                    296:       else
                    297:        {
                    298:          j++;
                    299:        }
                    300: 
                    301:       if (*sp > 0)
                    302:        fprintf(ftable, "%6d", *sp);
                    303:       else
                    304:        fprintf(ftable, "     0");
                    305:     }
                    306: 
                    307:   fprintf(ftable, "\n};\n");
                    308: }
                    309: 
                    310: 
                    311: 
                    312: output_stos()
                    313: {
                    314:   register int i;
                    315:   register int j;
                    316: 
                    317:   fprintf(ftable, "\nstatic const short yystos[] = {     0");
                    318: 
                    319:   j = 10;
                    320:   for (i = 1; i < nstates; i++)
                    321:     {
                    322:       putc(',', ftable);
                    323: 
                    324:       if (j >= 10)
                    325:        {
                    326:          putc('\n', ftable);
                    327:          j = 1;
                    328:        }
                    329:       else
                    330:        {
                    331:          j++;
                    332:        }
                    333: 
                    334:       fprintf(ftable, "%6d", accessing_symbol[i]);
                    335:     }
                    336: 
                    337:   fprintf(ftable, "\n};\n");
                    338: }
                    339: 
                    340: 
                    341: 
                    342: output_rule_data()
                    343: {
                    344:   register int i;
                    345:   register int j;
                    346: 
                    347:   fprintf(ftable, "\nstatic const short yyrline[] = {     0");
                    348: 
                    349:   j = 10;
                    350:   for (i = 1; i <= nrules; i++)
                    351:     {
                    352:       putc(',', ftable);
                    353: 
                    354:       if (j >= 10)
                    355:        {
                    356:          putc('\n', ftable);
                    357:          j = 1;
                    358:        }
                    359:       else
                    360:        {
                    361:          j++;
                    362:        }
                    363: 
                    364:       fprintf(ftable, "%6d", rline[i]);
                    365:     }
                    366: 
                    367:   /* Output the table of token names.  */
                    368: 
                    369:   fprintf(ftable, "\n};\n\nstatic const char * const yytname[] = {     0");
                    370: 
                    371:   j = 10;
                    372:   for (i = 1; i <= ntokens; i++)
                    373:     {
                    374:       register char *p;
                    375:       putc(',', ftable);
                    376: 
                    377:       if (j >= 10)
                    378:        {
                    379:          putc('\n', ftable);
                    380:          j = 1;
                    381:        }
                    382:       else
                    383:        {
                    384:          j++;
                    385:        }
                    386: 
                    387:       putc ('\"', ftable);
                    388: 
                    389:       for (p = tags[i]; *p; p++)
                    390:        if (*p == '"' || *p == '\\')
                    391:          fprintf(ftable, "\\%c", *p);
                    392:        else if (*p == '\n')
                    393:          fprintf(ftable, "\\n");
                    394:        else if (*p == '\t')
                    395:          fprintf(ftable, "\\t");
                    396:        else if (*p == '\b')
                    397:          fprintf(ftable, "\\b");
                    398:        else if (*p < 040 || *p >= 0177)
                    399:          fprintf(ftable, "\\%03o", *p);
                    400:        else
                    401:          putc(*p, ftable);
                    402: 
                    403:       putc ('\"', ftable);
                    404:     }
                    405: 
                    406:   fprintf(ftable, "\n};\n\nstatic const short yyr1[] = {     0");
                    407: 
                    408:   j = 10;
                    409:   for (i = 1; i <= nrules; i++)
                    410:     {
                    411:       putc(',', ftable);
                    412: 
                    413:       if (j >= 10)
                    414:        {
                    415:          putc('\n', ftable);
                    416:          j = 1;
                    417:        }
                    418:       else
                    419:        {
                    420:          j++;
                    421:        }
                    422: 
                    423:       fprintf(ftable, "%6d", rlhs[i]);
                    424:     }
                    425: 
                    426:   FREE(rlhs + 1);
                    427: 
                    428:   fprintf(ftable, "\n};\n\nstatic const short yyr2[] = {     0");
                    429: 
                    430:   j = 10;
                    431:   for (i = 1; i < nrules; i++)
                    432:     {
                    433:       putc(',', ftable);
                    434: 
                    435:       if (j >= 10)
                    436:        {
                    437:          putc('\n', ftable);
                    438:          j = 1;
                    439:        }
                    440:       else
                    441:        {
                    442:          j++;
                    443:        }
                    444: 
                    445:       fprintf(ftable, "%6d", rrhs[i + 1] - rrhs[i] - 1);
                    446:     }
                    447: 
                    448:   putc(',', ftable);
                    449:   if (j >= 10)
                    450:     putc('\n', ftable);
                    451: 
                    452:   fprintf(ftable, "%6d\n};\n", nitems - rrhs[nrules] - 1);
                    453:   FREE(rrhs + 1);
                    454: }
                    455: 
                    456: 
                    457: 
                    458: output_defines()
                    459: {
                    460:   fprintf(ftable, "\n\n#define\tYYFINAL\t\t%d\n", final_state);
                    461:   fprintf(ftable, "#define\tYYFLAG\t\t%d\n", MINSHORT);
                    462:   fprintf(ftable, "#define\tYYNTBASE\t%d\n", ntokens);
                    463: }
                    464: 
                    465: 
                    466: 
                    467: /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck.  */
                    468: 
                    469: output_actions()
                    470: {
                    471:   nvectors = nstates + nvars;
                    472: 
                    473:   froms = NEW2(nvectors, short *);
                    474:   tos = NEW2(nvectors, short *);
                    475:   tally = NEW2(nvectors, short);
                    476:   width = NEW2(nvectors, short);
                    477: 
                    478:   token_actions();
                    479:   free_shifts();
                    480:   free_reductions();
                    481:   FREE(lookaheads);
                    482:   FREE(LA);
                    483:   FREE(LAruleno);
                    484:   FREE(accessing_symbol);
                    485: 
                    486:   goto_actions();
                    487:   FREE(goto_map + ntokens);
                    488:   FREE(from_state);
                    489:   FREE(to_state);
                    490: 
                    491:   sort_actions();
                    492:   pack_table();
                    493:   output_base();
                    494:   output_table();
                    495:   output_check();
                    496: }
                    497: 
                    498: 
                    499: 
                    500: /* figure out the actions for the specified state, indexed by lookahead token type.
                    501: 
                    502:    The yydefact table is output now.  The detailed info
                    503:    is saved for putting into yytable later.  */
                    504: 
                    505: token_actions()
                    506: {
                    507:   register int i;
                    508:   register int j;
                    509:   register int k;
                    510: 
                    511:   actrow = NEW2(ntokens, short);
                    512: 
                    513:   k = action_row(0);
                    514:   fprintf(ftable, "\nstatic const short yydefact[] = {%6d", k);
                    515:   save_row(0);
                    516: 
                    517:   j = 10;
                    518:   for (i = 1; i < nstates; i++)
                    519:     {
                    520:       putc(',', ftable);
                    521: 
                    522:       if (j >= 10)
                    523:        {
                    524:          putc('\n', ftable);
                    525:          j = 1;
                    526:        }
                    527:       else
                    528:        {
                    529:          j++;
                    530:        }
                    531: 
                    532:       k = action_row(i);
                    533:       fprintf(ftable, "%6d", k);
                    534:       save_row(i);
                    535:     }
                    536: 
                    537:   fprintf(ftable, "\n};\n");
                    538:   FREE(actrow);
                    539: }
                    540: 
                    541: 
                    542: 
                    543: /* Decide what to do for each type of token if seen as the lookahead token in specified state.
                    544:    The value returned is used as the default action (yydefact) for the state.
                    545:    In addition, actrow is filled with what to do for each kind of token,
                    546:    index by symbol number, with zero meaning do the default action.
                    547:    The value MINSHORT, a very negative number, means this situation
                    548:    is an error.  The parser recognizes this value specially.
                    549: 
                    550:    This is where conflicts are resolved.  The loop over lookahead rules
                    551:    considered lower-numbered rules last, and the last rule considered that likes
                    552:    a token gets to handle it.  */
                    553: 
                    554: int
                    555: action_row(state)
                    556: int state;
                    557: {
                    558:   register int i;
                    559:   register int j;
                    560:   register int k;
                    561:   register int m;
                    562:   register int n;
                    563:   register int count;
                    564:   register int default_rule;
                    565:   register int nreds;
                    566:   register int max;
                    567:   register int rule;
                    568:   register int shift_state;
                    569:   register int symbol;
                    570:   register unsigned mask;
                    571:   register unsigned *wordp;
                    572:   register reductions *redp;
                    573:   register shifts *shiftp;
                    574:   register errs *errp;
                    575:   int nodefault = 0;  /* set nonzero to inhibit having any default reduction */
                    576: 
                    577:   for (i = 0; i < ntokens; i++)
                    578:     actrow[i] = 0;
                    579: 
                    580:   default_rule = 0;
                    581:   nreds = 0;
                    582:   redp = reduction_table[state];
                    583: 
                    584:   if (redp)
                    585:     {
                    586:       nreds = redp->nreds;
                    587: 
                    588:       if (nreds >= 1)
                    589:        {
                    590:          /* loop over all the rules available here which require lookahead */
                    591:          m = lookaheads[state];
                    592:          n = lookaheads[state + 1];
                    593: 
                    594:          for (i = n - 1; i >= m; i--)
                    595:            {
                    596:              rule = - LAruleno[i];
                    597:              wordp = LA + i * tokensetsize;
                    598:              mask = 1;
                    599: 
                    600:              /* and find each token which the rule finds acceptable to come next */
                    601:              for (j = 0; j < ntokens; j++)
                    602:                {
                    603:                  /* and record this rule as the rule to use if that token follows.  */
                    604:                  if (mask & *wordp)
                    605:                    actrow[j] = rule;
                    606: 
                    607:                  mask <<= 1;
                    608:                  if (mask == 0)
                    609:                    {
                    610:                      mask = 1;
                    611:                      wordp++;
                    612:                    }
                    613:                }
                    614:            }
                    615:        }
                    616:     }
                    617: 
                    618:   shiftp = shift_table[state];
                    619: 
                    620:   /* now see which tokens are allowed for shifts in this state.
                    621:      For them, record the shift as the thing to do.  So shift is preferred to reduce.  */
                    622: 
                    623:   if (shiftp)
                    624:     {
                    625:       k = shiftp->nshifts;
                    626: 
                    627:       for (i = 0; i < k; i++)
                    628:        {
                    629:          shift_state = shiftp->shifts[i];
                    630:          if (! shift_state) continue;
                    631: 
                    632:          symbol = accessing_symbol[shift_state];
                    633: 
                    634:          if (ISVAR(symbol))
                    635:            break;
                    636: 
                    637:          actrow[symbol] = shift_state;
                    638: 
                    639:          /* do not use any default reduction if there is a shift for error */
                    640: 
                    641:          if (symbol == error_token_number) nodefault = 1;
                    642:        }
                    643:     }
                    644: 
                    645:   errp = err_table[state];
                    646: 
                    647:   /* See which tokens are an explicit error in this state
                    648:      (due to %nonassoc).  For them, record MINSHORT as the action.  */
                    649: 
                    650:   if (errp)
                    651:     {
                    652:       k = errp->nerrs;
                    653: 
                    654:       for (i = 0; i < k; i++)
                    655:        {
                    656:          symbol = errp->errs[i];
                    657:          actrow[symbol] = MINSHORT;
                    658:        }
                    659:     }
                    660: 
                    661:   /* now find the most common reduction and make it the default action for this state.  */
                    662: 
                    663:   if (nreds >= 1 && ! nodefault)
                    664:     {
                    665:       if (consistent[state])
                    666:        default_rule = redp->rules[0];
                    667:       else
                    668:        {
                    669:          max = 0;
                    670:          for (i = m; i < n; i++)
                    671:            {
                    672:              count = 0;
                    673:              rule = - LAruleno[i];
                    674:     
                    675:              for (j = 0; j < ntokens; j++)
                    676:                {
                    677:                  if (actrow[j] == rule)
                    678:                    count++;
                    679:                }
                    680:     
                    681:              if (count > max)
                    682:                {
                    683:                  max = count;
                    684:                  default_rule = rule;
                    685:                }
                    686:            }
                    687:     
                    688:          /* actions which match the default are replaced with zero,
                    689:             which means "use the default" */
                    690:     
                    691:          if (max > 0)
                    692:            {
                    693:              for (j = 0; j < ntokens; j++)
                    694:                {
                    695:                  if (actrow[j] == default_rule)
                    696:                    actrow[j] = 0;
                    697:                }
                    698:     
                    699:              default_rule = - default_rule;
                    700:            }
                    701:        }
                    702:     }
                    703: 
                    704:   /* If have no default rule, the default is an error.
                    705:      So replace any action which says "error" with "use default".  */
                    706: 
                    707:   if (default_rule == 0)
                    708:     for (j = 0; j < ntokens; j++)
                    709:       {
                    710:        if (actrow[j] == MINSHORT)
                    711:          actrow[j] = 0;
                    712:       }
                    713: 
                    714:   return (default_rule);
                    715: }
                    716: 
                    717: 
                    718: 
                    719: save_row(state)
                    720: int state;
                    721: {
                    722:   register int i;
                    723:   register int count;
                    724:   register short *sp;
                    725:   register short *sp1;
                    726:   register short *sp2;
                    727: 
                    728:   count = 0;
                    729:   for (i = 0; i < ntokens; i++)
                    730:     {
                    731:       if (actrow[i] != 0)
                    732:        count++;
                    733:     }
                    734: 
                    735:   if (count == 0)
                    736:     return;
                    737: 
                    738:   froms[state] = sp1 = sp = NEW2(count, short);
                    739:   tos[state] = sp2 = NEW2(count, short);
                    740: 
                    741:   for (i = 0; i < ntokens; i++)
                    742:     {
                    743:       if (actrow[i] != 0)
                    744:        {
                    745:          *sp1++ = i;
                    746:          *sp2++ = actrow[i];
                    747:        }
                    748:     }
                    749: 
                    750:   tally[state] = count;
                    751:   width[state] = sp1[-1] - sp[0] + 1;
                    752: }
                    753: 
                    754: 
                    755: 
                    756: /* figure out what to do after reducing with each rule,
                    757:    depending on the saved state from before the beginning
                    758:    of parsing the data that matched this rule.
                    759: 
                    760:    The yydefgoto table is output now.  The detailed info
                    761:    is saved for putting into yytable later.  */
                    762: 
                    763: goto_actions()
                    764: {
                    765:   register int i;
                    766:   register int j;
                    767:   register int k;
                    768: 
                    769:   state_count = NEW2(nstates, short);
                    770: 
                    771:   k = default_goto(ntokens);
                    772:   fprintf(ftable, "\nstatic const short yydefgoto[] = {%6d", k);
                    773:   save_column(ntokens, k);
                    774: 
                    775:   j = 10;
                    776:   for (i = ntokens + 1; i < nsyms; i++)
                    777:     {
                    778:       putc(',', ftable);
                    779: 
                    780:       if (j >= 10)
                    781:        {
                    782:          putc('\n', ftable);
                    783:          j = 1;
                    784:        }
                    785:       else
                    786:        {
                    787:          j++;
                    788:        }
                    789: 
                    790:       k = default_goto(i);
                    791:       fprintf(ftable, "%6d", k);
                    792:       save_column(i, k);
                    793:     }
                    794: 
                    795:   fprintf(ftable, "\n};\n");
                    796:   FREE(state_count);
                    797: }
                    798: 
                    799: 
                    800: 
                    801: int
                    802: default_goto(symbol)
                    803: int symbol;
                    804: {
                    805:   register int i;
                    806:   register int m;
                    807:   register int n;
                    808:   register int default_state;
                    809:   register int max;
                    810: 
                    811:   m = goto_map[symbol];
                    812:   n = goto_map[symbol + 1];
                    813: 
                    814:   if (m == n)
                    815:     return (-1);
                    816: 
                    817:   for (i = 0; i < nstates; i++)
                    818:     state_count[i] = 0;
                    819: 
                    820:   for (i = m; i < n; i++)
                    821:     state_count[to_state[i]]++;
                    822: 
                    823:   max = 0;
                    824:   default_state = -1;
                    825: 
                    826:   for (i = 0; i < nstates; i++)
                    827:     {
                    828:       if (state_count[i] > max)
                    829:        {
                    830:          max = state_count[i];
                    831:          default_state = i;
                    832:        }
                    833:     }
                    834: 
                    835:   return (default_state);
                    836: }
                    837: 
                    838: 
                    839: 
                    840: save_column(symbol, default_state)
                    841: int symbol;
                    842: int default_state;
                    843: {
                    844:   register int i;
                    845:   register int m;
                    846:   register int n;
                    847:   register short *sp;
                    848:   register short *sp1;
                    849:   register short *sp2;
                    850:   register int count;
                    851:   register int symno;
                    852: 
                    853:   m = goto_map[symbol];
                    854:   n = goto_map[symbol + 1];
                    855: 
                    856:   count = 0;
                    857:   for (i = m; i < n; i++)
                    858:     {
                    859:       if (to_state[i] != default_state)
                    860:        count++;
                    861:     }
                    862: 
                    863:   if (count == 0)
                    864:     return;
                    865: 
                    866:   symno = symbol - ntokens + nstates;
                    867: 
                    868:   froms[symno] = sp1 = sp = NEW2(count, short);
                    869:   tos[symno] = sp2 = NEW2(count, short);
                    870: 
                    871:   for (i = m; i < n; i++)
                    872:     {
                    873:       if (to_state[i] != default_state)
                    874:        {
                    875:          *sp1++ = from_state[i];
                    876:          *sp2++ = to_state[i];
                    877:        }
                    878:     }
                    879: 
                    880:   tally[symno] = count;
                    881:   width[symno] = sp1[-1] - sp[0] + 1;
                    882: }
                    883: 
                    884: 
                    885: 
                    886: /* the next few functions decide how to pack 
                    887:    the actions and gotos information into yytable. */
                    888: 
                    889: sort_actions()
                    890: {
                    891:   register int i;
                    892:   register int j;
                    893:   register int k;
                    894:   register int t;
                    895:   register int w;
                    896: 
                    897:   order = NEW2(nvectors, short);
                    898:   nentries = 0;
                    899: 
                    900:   for (i = 0; i < nvectors; i++)
                    901:     {
                    902:       if (tally[i] > 0)
                    903:        {
                    904:          t = tally[i];
                    905:          w = width[i];
                    906:          j = nentries - 1;
                    907: 
                    908:          while (j >= 0 && (width[order[j]] < w))
                    909:            j--;
                    910: 
                    911:          while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
                    912:            j--;
                    913: 
                    914:          for (k = nentries - 1; k > j; k--)
                    915:            order[k + 1] = order[k];
                    916: 
                    917:          order[j + 1] = i;
                    918:          nentries++;
                    919:        }
                    920:     }
                    921: }
                    922: 
                    923: 
                    924: 
                    925: pack_table()
                    926: {
                    927:   register int i;
                    928:   register int place;
                    929:   register int state;
                    930: 
                    931:   base = NEW2(nvectors, short);
                    932:   pos = NEW2(nentries, short);
                    933:   table = NEW2(MAXTABLE, short);
                    934:   check = NEW2(MAXTABLE, short);
                    935: 
                    936:   lowzero = 0;
                    937:   high = 0;
                    938: 
                    939:   for (i = 0; i < nvectors; i++)
                    940:     base[i] = MINSHORT;
                    941: 
                    942:   for (i = 0; i < MAXTABLE; i++)
                    943:     check[i] = -1;
                    944: 
                    945:   for (i = 0; i < nentries; i++)
                    946:     {
                    947:       state = matching_state(i);
                    948: 
                    949:       if (state < 0)
                    950:        place = pack_vector(i);
                    951:       else
                    952:        place = base[state];
                    953: 
                    954:       pos[i] = place;
                    955:       base[order[i]] = place;
                    956:     }
                    957: 
                    958:   for (i = 0; i < nvectors; i++)
                    959:     {
                    960:       FREE(froms[i]);
                    961:       FREE(tos[i]);
                    962:     }
                    963: 
                    964:   FREE(froms);
                    965:   FREE(tos);
                    966:   FREE(pos);
                    967: }
                    968: 
                    969: 
                    970: 
                    971: int
                    972: matching_state(vector)
                    973: int vector;
                    974: {
                    975:   register int i;
                    976:   register int j;
                    977:   register int k;
                    978:   register int t;
                    979:   register int w;
                    980:   register int match;
                    981:   register int prev;
                    982: 
                    983:   i = order[vector];
                    984:   if (i >= nstates)
                    985:     return (-1);
                    986: 
                    987:   t = tally[i];
                    988:   w = width[i];
                    989: 
                    990:   for (prev = vector - 1; prev >= 0; prev--)
                    991:     {
                    992:       j = order[prev];
                    993:       if (width[j] != w || tally[j] != t)
                    994:        return (-1);
                    995: 
                    996:       match = 1;
                    997:       for (k = 0; match && k < t; k++)
                    998:        {
                    999:          if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
                   1000:            match = 0;
                   1001:        }
                   1002: 
                   1003:       if (match)
                   1004:        return (j);
                   1005:     }
                   1006: 
                   1007:   return (-1);
                   1008: }
                   1009: 
                   1010: 
                   1011: 
                   1012: int
                   1013: pack_vector(vector)
                   1014: int vector;
                   1015: {
                   1016:   register int i;
                   1017:   register int j;
                   1018:   register int k;
                   1019:   register int t;
                   1020:   register int loc;
                   1021:   register int ok;
                   1022:   register short *from;
                   1023:   register short *to;
                   1024: 
                   1025:   i = order[vector];
                   1026:   t = tally[i];
                   1027: 
                   1028:   if (t == 0)
                   1029:     berror("pack_vector");
                   1030: 
                   1031:   from = froms[i];
                   1032:   to = tos[i];
                   1033: 
                   1034:   for (j = lowzero - from[0]; j < MAXTABLE; j++)
                   1035:     {
                   1036:       ok = 1;
                   1037: 
                   1038:       for (k = 0; ok && k < t; k++)
                   1039:        {
                   1040:          loc = j + from[k];
                   1041:          if (loc > MAXTABLE)
                   1042:            fatals("maximum table size (%d) exceeded",MAXTABLE);
                   1043: 
                   1044:          if (table[loc] != 0)
                   1045:            ok = 0;
                   1046:        }
                   1047: 
                   1048:       for (k = 0; ok && k < vector; k++)
                   1049:        {
                   1050:          if (pos[k] == j)
                   1051:            ok = 0;
                   1052:        }
                   1053: 
                   1054:       if (ok)
                   1055:        {
                   1056:          for (k = 0; k < t; k++)
                   1057:            {
                   1058:              loc = j + from[k];
                   1059:              table[loc] = to[k];
                   1060:              check[loc] = from[k];
                   1061:            }
                   1062: 
                   1063:          while (table[lowzero] != 0)
                   1064:            lowzero++;
                   1065: 
                   1066:          if (loc > high)
                   1067:            high = loc;
                   1068: 
                   1069:          return (j);
                   1070:        }
                   1071:     }
                   1072: 
                   1073:   berror("pack_vector");
                   1074:   return 0;    /* JF keep lint happy */
                   1075: }
                   1076: 
                   1077: 
                   1078: 
                   1079: /* the following functions output yytable, yycheck
                   1080:    and the vectors whose elements index the portion starts */
                   1081: 
                   1082: output_base()
                   1083: {
                   1084:   register int i;
                   1085:   register int j;
                   1086: 
                   1087:   fprintf(ftable, "\nstatic const short yypact[] = {%6d", base[0]);
                   1088: 
                   1089:   j = 10;
                   1090:   for (i = 1; i < nstates; i++)
                   1091:     {
                   1092:       putc(',', ftable);
                   1093: 
                   1094:       if (j >= 10)
                   1095:        {
                   1096:          putc('\n', ftable);
                   1097:          j = 1;
                   1098:        }
                   1099:       else
                   1100:        {
                   1101:          j++;
                   1102:        }
                   1103: 
                   1104:       fprintf(ftable, "%6d", base[i]);
                   1105:     }
                   1106: 
                   1107:   fprintf(ftable, "\n};\n\nstatic const short yypgoto[] = {%6d", base[nstates]);
                   1108: 
                   1109:   j = 10;
                   1110:   for (i = nstates + 1; i < nvectors; i++)
                   1111:     {
                   1112:       putc(',', ftable);
                   1113: 
                   1114:       if (j >= 10)
                   1115:        {
                   1116:          putc('\n', ftable);
                   1117:          j = 1;
                   1118:        }
                   1119:       else
                   1120:        {
                   1121:          j++;
                   1122:        }
                   1123: 
                   1124:       fprintf(ftable, "%6d", base[i]);
                   1125:     }
                   1126: 
                   1127:   fprintf(ftable, "\n};\n");
                   1128:   FREE(base);
                   1129: }
                   1130: 
                   1131: 
                   1132: 
                   1133: output_table()
                   1134: {
                   1135:   register int i;
                   1136:   register int j;
                   1137: 
                   1138:   fprintf(ftable, "\n\n#define\tYYLAST\t\t%d\n\n", high);
                   1139:   fprintf(ftable, "\nstatic const short yytable[] = {%6d", table[0]);
                   1140: 
                   1141:   j = 10;
                   1142:   for (i = 1; i <= high; i++)
                   1143:     {
                   1144:       putc(',', ftable);
                   1145: 
                   1146:       if (j >= 10)
                   1147:        {
                   1148:          putc('\n', ftable);
                   1149:          j = 1;
                   1150:        }
                   1151:       else
                   1152:        {
                   1153:          j++;
                   1154:        }
                   1155: 
                   1156:       fprintf(ftable, "%6d", table[i]);
                   1157:     }
                   1158: 
                   1159:   fprintf(ftable, "\n};\n");
                   1160:   FREE(table);
                   1161: }
                   1162: 
                   1163: 
                   1164: 
                   1165: output_check()
                   1166: {
                   1167:   register int i;
                   1168:   register int j;
                   1169: 
                   1170:   fprintf(ftable, "\nstatic const short yycheck[] = {%6d", check[0]);
                   1171: 
                   1172:   j = 10;
                   1173:   for (i = 1; i <= high; i++)
                   1174:     {
                   1175:       putc(',', ftable);
                   1176: 
                   1177:       if (j >= 10)
                   1178:        {
                   1179:          putc('\n', ftable);
                   1180:          j = 1;
                   1181:        }
                   1182:       else
                   1183:        {
                   1184:          j++;
                   1185:        }
                   1186: 
                   1187:       fprintf(ftable, "%6d", check[i]);
                   1188:     }
                   1189: 
                   1190:   fprintf(ftable, "\n};\n");
                   1191:   FREE(check);
                   1192: }
                   1193: 
                   1194: 
                   1195: 
                   1196: /* copy the parser code into the ftable file at the end.  */
                   1197: 
                   1198: output_parser()
                   1199: {
                   1200:   register int c;
                   1201: #ifdef DONTDEF
                   1202:   FILE *fpars;
                   1203: #else
                   1204: #define fpars fparser
                   1205: #endif
                   1206: 
                   1207:   if (pure_parser)
                   1208:     fprintf(ftable, "#define YYIMPURE 1\n\n");
                   1209:   else
                   1210:     fprintf(ftable, "#define YYPURE 1\n\n");
                   1211: 
                   1212: #ifdef DONTDEF /* JF no longer needed 'cuz open_extra_files changes the
                   1213:                   currently open parser from bison.simple to bison.hairy */
                   1214:   if (semantic_parser)
                   1215:     fpars = fparser;
                   1216:   else fpars = fparser1;
                   1217: #endif
                   1218: 
                   1219:   c = getc(fpars);
                   1220:   while (c != EOF)
                   1221:     {
                   1222:       if (c == '$') {
                   1223: #ifdef DONTDEF
                   1224:         fprintf(ftable, "#include \"%s\"\n", actfile);
                   1225: #else
                   1226:        /* JF don't #include the action file.  Stuff it right in. */
                   1227:        rewind(faction);
                   1228:        for(c=getc(faction);c!=EOF;c=getc(faction))
                   1229:                putc(c,ftable);
                   1230: #endif
                   1231:       } else
                   1232:        putc(c, ftable);
                   1233:       c = getc(fpars);
                   1234:     }
                   1235: }
                   1236: 
                   1237: 
                   1238: 
                   1239: output_program()
                   1240: {
                   1241:   register int c;
                   1242:   extern int lineno;
                   1243: 
                   1244:   fprintf(ftable, "#line %d \"%s\"\n", lineno, infile);
                   1245: 
                   1246:   c = getc(finput);
                   1247:   while (c != EOF)
                   1248:     {
                   1249:       putc(c, ftable);
                   1250:       c = getc(finput);
                   1251:     }
                   1252: }
                   1253: 
                   1254: 
                   1255: 
                   1256: free_itemsets()
                   1257: {
                   1258:   register core *cp,*cptmp;
                   1259: 
                   1260:   FREE(state_table);
                   1261: 
                   1262:   for (cp = first_state; cp; cp = cptmp) {
                   1263:     cptmp=cp->next;
                   1264:     FREE(cp);
                   1265:   }
                   1266: }
                   1267: 
                   1268: 
                   1269: 
                   1270: free_shifts()
                   1271: {
                   1272:   register shifts *sp,*sptmp;/* JF derefrenced freed ptr */
                   1273: 
                   1274:   FREE(shift_table);
                   1275: 
                   1276:   for (sp = first_shift; sp; sp = sptmp) {
                   1277:     sptmp=sp->next;
                   1278:     FREE(sp);
                   1279:   }
                   1280: }
                   1281: 
                   1282: 
                   1283: 
                   1284: free_reductions()
                   1285: {
                   1286:   register reductions *rp,*rptmp;/* JF fixed freed ptr */
                   1287: 
                   1288:   FREE(reduction_table);
                   1289: 
                   1290:   for (rp = first_reduction; rp; rp = rptmp) {
                   1291:     rptmp=rp->next;
                   1292:     FREE(rp);
                   1293:   }
                   1294: }

unix.superglobalmegacorp.com

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