home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / BYACC.ZIP / OUTPUT.C < prev    next >
C/C++ Source or Header  |  1992-03-16  |  21KB  |  1,117 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "byacc.h"
  4.  
  5. static int nvectors;
  6. static int nentries;
  7. static short **froms;
  8. static short **tos;
  9. static short *tally;
  10. static short *width;
  11. static short *state_count;
  12. static short *order;
  13. static short *base;
  14. static short *pos;
  15. static int maxtable;
  16. static short *table;
  17. static short *check;
  18. static int lowzero;
  19. static int high;
  20.  
  21. void
  22. output(void)
  23. {
  24.     free_itemsets();
  25.     free_shifts();
  26.     free_reductions();
  27.     output_rule_data();
  28.     output_yydefred();
  29.     output_actions();
  30.     free_parser();
  31.     output_defines();
  32.     output_stored_text();
  33.     output_debug();
  34.     output_stype();
  35.     write_section(header);
  36.     output_trailing_text();
  37.     write_section(body);
  38.     output_semantic_actions();
  39.     write_section(trailer);
  40. }
  41.  
  42. void
  43. output_rule_data(void)
  44. {
  45.     register int i;
  46.     register int j;
  47.  
  48.   
  49.     fprintf(output_file, "short yylhs[] = {%42d,",
  50.         symbol_value[start_symbol]);
  51.  
  52.     j = 10;
  53.     for (i = 3; i < nrules; i++)
  54.     {
  55.     if (j >= 10)
  56.     {
  57.         ++outline;
  58.         putc('\n', output_file);
  59.         j = 1;
  60.     }
  61.         else
  62.         ++j;
  63.  
  64.         fprintf(output_file, "%5d,", symbol_value[rlhs[i]]);
  65.     }
  66.     outline += 2;
  67.     fprintf(output_file, "\n};\n");
  68.  
  69.     fprintf(output_file, "short yylen[] = {%42d,", 2);
  70.  
  71.     j = 10;
  72.     for (i = 3; i < nrules; i++)
  73.     {
  74.     if (j >= 10)
  75.     {
  76.         ++outline;
  77.         putc('\n', output_file);
  78.         j = 1;
  79.     }
  80.     else
  81.       j++;
  82.  
  83.         fprintf(output_file, "%5d,", rrhs[i + 1] - rrhs[i] - 1);
  84.     }
  85.     outline += 2;
  86.     fprintf(output_file, "\n};\n");
  87. }
  88.  
  89. void
  90. output_yydefred(void)
  91. {
  92.     register int i, j;
  93.  
  94.     fprintf(output_file, "short yydefred[] = {%39d,",
  95.         (defred[0] ? defred[0] - 2 : 0));
  96.  
  97.     j = 10;
  98.     for (i = 1; i < nstates; i++)
  99.     {
  100.     if (j < 10)
  101.         ++j;
  102.     else
  103.     {
  104.         ++outline;
  105.         putc('\n', output_file);
  106.         j = 1;
  107.     }
  108.  
  109.     fprintf(output_file, "%5d,", (defred[i] ? defred[i] - 2 : 0));
  110.     }
  111.  
  112.     outline += 2;
  113.     fprintf(output_file, "\n};\n");
  114. }
  115.  
  116. void
  117. output_actions(void)
  118. {
  119.     nvectors = 2*nstates + nvars;
  120.  
  121.     froms = NEW2(nvectors, short *);
  122.     tos = NEW2(nvectors, short *);
  123.     tally = NEW2(nvectors, short);
  124.     width = NEW2(nvectors, short);
  125.  
  126.     token_actions();
  127.     FREE(lookaheads);
  128.     FREE(LA);
  129.     FREE(LAruleno);
  130.     FREE(accessing_symbol);
  131.  
  132.     goto_actions();
  133.     FREE(goto_map + ntokens);
  134.     FREE(from_state);
  135.     FREE(to_state);
  136.  
  137.     sort_actions();
  138.     pack_table();
  139.     output_base();
  140.     output_table();
  141.     output_check();
  142. }
  143.  
  144. void
  145. token_actions(void)
  146. {
  147.     register int i, j;
  148.     register int shiftcount, reducecount;
  149.     register int max, min;
  150.     register short *actionrow, *r, *s;
  151.     register action *p;
  152.  
  153.     actionrow = NEW2(2*ntokens, short);
  154.     for (i = 0; i < nstates; ++i)
  155.     {
  156.     if (parser[i])
  157.     {
  158.         for (j = 0; j < 2*ntokens; ++j)
  159.         actionrow[j] = 0;
  160.  
  161.         shiftcount = 0;
  162.         reducecount = 0;
  163.         for (p = parser[i]; p; p = p->next)
  164.         {
  165.         if (p->suppressed == 0)
  166.         {
  167.             if (p->action_code == SHIFT)
  168.             {
  169.             ++shiftcount;
  170.             actionrow[p->symbol] = p->number;
  171.             }
  172.             else if (p->action_code == REDUCE && p->number != defred[i])
  173.             {
  174.             ++reducecount;
  175.             actionrow[p->symbol + ntokens] = p->number;
  176.             }
  177.         }
  178.         }
  179.  
  180.         tally[i] = shiftcount;
  181.         tally[nstates+i] = reducecount;
  182.         width[i] = 0;
  183.         width[nstates+i] = 0;
  184.         if (shiftcount > 0)
  185.         {
  186.         froms[i] = r = NEW2(shiftcount, short);
  187.         tos[i] = s = NEW2(shiftcount, short);
  188.         min = MAXSHORT;
  189.         max = 0;
  190.         for (j = 0; j < ntokens; ++j)
  191.         {
  192.             if (actionrow[j])
  193.             {
  194.             if (min > symbol_value[j])
  195.                 min = symbol_value[j];
  196.             if (max < symbol_value[j])
  197.                 max = symbol_value[j];
  198.             *r++ = symbol_value[j];
  199.             *s++ = actionrow[j];
  200.             }
  201.         }
  202.         width[i] = max - min + 1;
  203.         }
  204.         if (reducecount > 0)
  205.         {
  206.         froms[nstates+i] = r = NEW2(reducecount, short);
  207.         tos[nstates+i] = s = NEW2(reducecount, short);
  208.         min = MAXSHORT;
  209.         max = 0;
  210.         for (j = 0; j < ntokens; ++j)
  211.         {
  212.             if (actionrow[ntokens+j])
  213.             {
  214.             if (min > symbol_value[j])
  215.                 min = symbol_value[j];
  216.             if (max < symbol_value[j])
  217.                 max = symbol_value[j];
  218.             *r++ = symbol_value[j];
  219.             *s++ = actionrow[ntokens+j] - 2;
  220.             }
  221.         }
  222.         width[nstates+i] = max - min + 1;
  223.         }
  224.     }
  225.     }
  226.     FREE(actionrow);
  227. }
  228.  
  229. void
  230. goto_actions(void)
  231. {
  232.     register int i, j, k;
  233.  
  234.     state_count = NEW2(nstates, short);
  235.  
  236.     k = default_goto(start_symbol + 1);
  237.     fprintf(output_file, "short yydgoto[] = {%40d,", k);
  238.     save_column(start_symbol + 1, k);
  239.  
  240.     j = 10;
  241.     for (i = start_symbol + 2; i < nsyms; i++)
  242.     {
  243.     if (j >= 10)
  244.     {
  245.         ++outline;
  246.         putc('\n', output_file);
  247.         j = 1;
  248.     }
  249.     else
  250.         ++j;
  251.  
  252.     k = default_goto(i);
  253.     fprintf(output_file, "%5d,", k);
  254.     save_column(i, k);
  255.     }
  256.  
  257.     outline += 2;
  258.     fprintf(output_file, "\n};\n");
  259.     FREE(state_count);
  260. }
  261.  
  262. int
  263. default_goto(int symbol)
  264. {
  265.     register int i;
  266.     register int m;
  267.     register int n;
  268.     register int default_state;
  269.     register int max;
  270.  
  271.     m = goto_map[symbol];
  272.     n = goto_map[symbol + 1];
  273.  
  274.     if (m == n) return (0);
  275.  
  276.     for (i = 0; i < nstates; i++)
  277.     state_count[i] = 0;
  278.  
  279.     for (i = m; i < n; i++)
  280.     state_count[to_state[i]]++;
  281.  
  282.     max = 0;
  283.     default_state = 0;
  284.     for (i = 0; i < nstates; i++)
  285.     {
  286.     if (state_count[i] > max)
  287.     {
  288.         max = state_count[i];
  289.         default_state = i;
  290.     }
  291.     }
  292.  
  293.     return (default_state);
  294. }
  295.  
  296.  
  297. void
  298. save_column(int symbol, int default_state)
  299. {
  300.     register int i;
  301.     register int m;
  302.     register int n;
  303.     register short *sp;
  304.     register short *sp1;
  305.     register short *sp2;
  306.     register int count;
  307.     register int symno;
  308.  
  309.     m = goto_map[symbol];
  310.     n = goto_map[symbol + 1];
  311.  
  312.     count = 0;
  313.     for (i = m; i < n; i++)
  314.     {
  315.     if (to_state[i] != default_state)
  316.         ++count;
  317.     }
  318.     if (count == 0) return;
  319.  
  320.     symno = symbol_value[symbol] + 2*nstates;
  321.  
  322.     froms[symno] = sp1 = sp = NEW2(count, short);
  323.     tos[symno] = sp2 = NEW2(count, short);
  324.  
  325.     for (i = m; i < n; i++)
  326.     {
  327.     if (to_state[i] != default_state)
  328.     {
  329.         *sp1++ = from_state[i];
  330.         *sp2++ = to_state[i];
  331.     }
  332.     }
  333.  
  334.     tally[symno] = count;
  335.     width[symno] = sp1[-1] - sp[0] + 1;
  336. }
  337.  
  338.  
  339. void
  340. sort_actions()
  341. {
  342.   register int i;
  343.   register int j;
  344.   register int k;
  345.   register int t;
  346.   register int w;
  347.  
  348.   order = NEW2(nvectors, short);
  349.   nentries = 0;
  350.  
  351.   for (i = 0; i < nvectors; i++)
  352.     {
  353.       if (tally[i] > 0)
  354.     {
  355.       t = tally[i];
  356.       w = width[i];
  357.       j = nentries - 1;
  358.  
  359.       while (j >= 0 && (width[order[j]] < w))
  360.         j--;
  361.  
  362.       while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
  363.         j--;
  364.  
  365.       for (k = nentries - 1; k > j; k--)
  366.         order[k + 1] = order[k];
  367.  
  368.       order[j + 1] = i;
  369.       nentries++;
  370.     }
  371.     }
  372. }
  373.  
  374. void
  375. pack_table()
  376. {
  377.     register int i;
  378.     register int place;
  379.     register int state;
  380.  
  381.     base = NEW2(nvectors, short);
  382.     pos = NEW2(nentries, short);
  383.  
  384.     maxtable = 1000;
  385.     table = NEW2(maxtable, short);
  386.     check = NEW2(maxtable, short);
  387.  
  388.     lowzero = 0;
  389.     high = 0;
  390.  
  391.     for (i = 0; i < maxtable; i++)
  392.     check[i] = -1;
  393.  
  394.     for (i = 0; i < nentries; i++)
  395.     {
  396.     state = matching_vector(i);
  397.  
  398.     if (state < 0)
  399.         place = pack_vector(i);
  400.     else
  401.         place = base[state];
  402.  
  403.     pos[i] = place;
  404.     base[order[i]] = place;
  405.     }
  406.  
  407.     for (i = 0; i < nvectors; i++)
  408.     {
  409.     FREE(froms[i]);
  410.     FREE(tos[i]);
  411.     }
  412.  
  413.     FREE(froms);
  414.     FREE(tos);
  415.     FREE(pos);
  416. }
  417.  
  418.  
  419. /*  The function matching_vector determines if the vector specified by    */
  420. /*  the input parameter matches a previously considered    vector.  The    */
  421. /*  test at the start of the function checks if the vector represents    */
  422. /*  a row of shifts over terminal symbols or a row of reductions, or a    */
  423. /*  column of shifts over a nonterminal symbol.  Berkeley Yacc does not    */
  424. /*  check if a column of shifts over a nonterminal symbols matches a    */
  425. /*  previously considered vector.  Because of the nature of LR parsing    */
  426. /*  tables, no two columns can match.  Therefore, the only possible    */
  427. /*  match would be between a row and a column.  Such matches are    */
  428. /*  unlikely.  Therefore, to save time, no attempt is made to see if a    */
  429. /*  column matches a previously considered vector.            */
  430. /*                                    */
  431. /*  Matching_vector is poorly designed.  The test could easily be made    */
  432. /*  faster.  Also, it depends on the vectors being in a specific    */
  433. /*  order.                                */
  434.  
  435. int
  436. matching_vector(int vector)
  437. {
  438.     register int i;
  439.     register int j;
  440.     register int k;
  441.     register int t;
  442.     register int w;
  443.     register int match;
  444.     register int prev;
  445.  
  446.     i = order[vector];
  447.     if (i >= 2*nstates)
  448.     return (-1);
  449.  
  450.     t = tally[i];
  451.     w = width[i];
  452.  
  453.     for (prev = vector - 1; prev >= 0; prev--)
  454.     {
  455.     j = order[prev];
  456.     if (width[j] != w || tally[j] != t)
  457.         return (-1);
  458.  
  459.     match = 1;
  460.     for (k = 0; match && k < t; k++)
  461.     {
  462.         if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
  463.         match = 0;
  464.     }
  465.  
  466.     if (match)
  467.         return (j);
  468.     }
  469.  
  470.     return (-1);
  471. }
  472.  
  473.  
  474.  
  475. int
  476. pack_vector(int vector)
  477. {
  478.     register int i, j, k, l;
  479.     register int t;
  480.     register int loc;
  481.     register int ok;
  482.     register short *from;
  483.     register short *to;
  484.     int newmax;
  485.  
  486.     i = order[vector];
  487.     t = tally[i];
  488.     assert(t);
  489.  
  490.     from = froms[i];
  491.     to = tos[i];
  492.  
  493.     j = lowzero - from[0];
  494.     for (k = 1; k < t; ++k)
  495.     if (lowzero - from[k] > j)
  496.         j = lowzero - from[k];
  497.     for (;; ++j)
  498.     {
  499.     if (j == 0)
  500.         continue;
  501.     ok = 1;
  502.     for (k = 0; ok && k < t; k++)
  503.     {
  504.         loc = j + from[k];
  505.         if (loc >= maxtable)
  506.         {
  507.         if (loc >= MAXTABLE)
  508.             fatal("maximum table size exceeded");
  509.  
  510.         do { newmax = maxtable + 200; } while (newmax <= loc);
  511.         table = (short *) realloc(table, newmax);
  512.         check = (short *) realloc(check, newmax);
  513.         for (l  = maxtable; l < newmax; ++l)
  514.         {
  515.             table[l] = 0;
  516.             check[l] = -1;
  517.         }
  518.         maxtable = newmax;
  519.         }
  520.  
  521.         if (check[loc] != -1)
  522.         ok = 0;
  523.     }
  524.     for (k = 0; ok && k < vector; k++)
  525.     {
  526.         if (pos[k] == j)
  527.         ok = 0;
  528.     }
  529.     if (ok)
  530.     {
  531.         for (k = 0; k < t; k++)
  532.         {
  533.         loc = j + from[k];
  534.         table[loc] = to[k];
  535.         check[loc] = from[k];
  536.         if (loc > high) high = loc;
  537.         }
  538.  
  539.         while (check[lowzero] != -1)
  540.         ++lowzero;
  541.  
  542.         return (j);
  543.     }
  544.     }
  545. }
  546.  
  547.  
  548. void
  549. output_base(void)
  550. {
  551.     register int i, j;
  552.  
  553.     fprintf(output_file, "short yysindex[] = {%39d,", base[0]);
  554.  
  555.     j = 10;
  556.     for (i = 1; i < nstates; i++)
  557.     {
  558.     if (j >= 10)
  559.     {
  560.         ++outline;
  561.         putc('\n', output_file);
  562.         j = 1;
  563.     }
  564.     else
  565.         ++j;
  566.  
  567.     fprintf(output_file, "%5d,", base[i]);
  568.     }
  569.  
  570.     outline += 2;
  571.     fprintf(output_file, "\n};\nshort yyrindex[] = {%39d,", base[nstates]);
  572.  
  573.     j = 10;
  574.     for (i = nstates + 1; i < 2*nstates; i++)
  575.     {
  576.     if (j >= 10)
  577.     {
  578.         ++outline;
  579.         putc('\n', output_file);
  580.         j = 1;
  581.     }
  582.     else
  583.         ++j;
  584.  
  585.     fprintf(output_file, "%5d,", base[i]);
  586.     }
  587.  
  588.     outline += 2;
  589.     fprintf(output_file, "\n};\nshort yygindex[] = {%39d,", base[2*nstates]);
  590.  
  591.     j = 10;
  592.     for (i = 2*nstates + 1; i < nvectors - 1; i++)
  593.     {
  594.     if (j >= 10)
  595.     {
  596.         ++outline;
  597.         putc('\n', output_file);
  598.         j = 1;
  599.     }
  600.     else
  601.         ++j;
  602.  
  603.     fprintf(output_file, "%5d,", base[i]);
  604.     }
  605.  
  606.     outline += 2;
  607.     fprintf(output_file, "\n};\n");
  608.     FREE(base);
  609. }
  610.  
  611.  
  612. void
  613. output_table()
  614. {
  615.     register int i;
  616.     register int j;
  617.  
  618.     ++outline;
  619.     fprintf(output_file, "#define\tYYTABLESIZE\t\t%d\n", high);
  620.     fprintf(output_file, "short yytable[] = {%40d,", table[0]);
  621.  
  622.     j = 10;
  623.     for (i = 1; i <= high; i++)
  624.     {
  625.     if (j >= 10)
  626.     {
  627.         ++outline;
  628.         putc('\n', output_file);
  629.         j = 1;
  630.     }
  631.     else
  632.         ++j;
  633.  
  634.     fprintf(output_file, "%5d,", table[i]);
  635.     }
  636.  
  637.     outline += 2;
  638.     fprintf(output_file, "\n};\n");
  639.     FREE(table);
  640. }
  641.  
  642.  
  643. void
  644. output_check(void)
  645. {
  646.     register int i;
  647.     register int j;
  648.  
  649.     fprintf(output_file, "short yycheck[] = {%40d,", check[0]);
  650.  
  651.     j = 10;
  652.     for (i = 1; i <= high; i++)
  653.     {
  654.     if (j >= 10)
  655.     {
  656.         ++outline;
  657.         putc('\n', output_file);
  658.         j = 1;
  659.     }
  660.     else
  661.         ++j;
  662.  
  663.     fprintf(output_file, "%5d,", check[i]);
  664.     }
  665.  
  666.     outline += 2;
  667.     fprintf(output_file, "\n};\n");
  668.     FREE(check);
  669. }
  670.  
  671.  
  672. int
  673. is_C_identifier(char *name)
  674. {
  675.     register char *s;
  676.     register int c;
  677.  
  678.     s = name;
  679.     c = *s;
  680.     if (c == '"')
  681.     {
  682.     c = *++s;
  683.     if (!isalpha(c) && c != '_' && c != '$')
  684.         return (0);
  685.     while ((c = *++s) != '"')
  686.     {
  687.         if (!isalnum(c) && c != '_' && c != '$')
  688.         return (0);
  689.     }
  690.     return (1);
  691.     }
  692.  
  693.     if (!isalpha(c) && c != '_' && c != '$')
  694.     return (0);
  695.     while (c = *++s)
  696.     {
  697.     if (!isalnum(c) && c != '_' && c != '$')
  698.         return (0);
  699.     }
  700.     return (1);
  701. }
  702.  
  703. void
  704. output_defines()
  705. {
  706.     register int c, i;
  707.     register char *s;
  708.  
  709.     for (i = 2; i < ntokens; ++i)
  710.     {
  711.     s = symbol_name[i];
  712.     if (is_C_identifier(s))
  713.     {
  714.         fprintf(output_file, "#define ");
  715.         if (dflag) fprintf(defines_file, "#define ");
  716.         c = *s;
  717.         if (c == '"')
  718.         {
  719.         while ((c = *++s) != '"')
  720.         {
  721.             putc(c, output_file);
  722.             if (dflag) putc(c, defines_file);
  723.         }
  724.         }
  725.         else
  726.         {
  727.         do
  728.         {
  729.             putc(c, output_file);
  730.             if (dflag) putc(c, defines_file);
  731.         }
  732.         while (c = *++s);
  733.         }
  734.         ++outline;
  735.         fprintf(output_file, " %d\n", symbol_value[i]);
  736.         if (dflag) fprintf(defines_file, " %d\n", symbol_value[i]);
  737.     }
  738.     }
  739.  
  740.     ++outline;
  741.     fprintf(output_file, "#define YYERRCODE %d\n", symbol_value[1]);
  742.  
  743.     if (dflag && unionized)
  744.     {
  745.     fclose(union_file);
  746.     union_file = fopen(union_file_name, "r");
  747.     if (union_file == NULL) open_error(union_file_name);
  748.     while ((c = getc(union_file)) != EOF)
  749.         putc(c, defines_file);
  750.     fprintf(defines_file, " YYSTYPE;\nextern YYSTYPE yylval;\n");
  751.     }
  752. }
  753.  
  754. void
  755. output_stored_text()
  756. {
  757.     register int c, last;
  758.  
  759.     fclose(text_file);
  760.     text_file = fopen(text_file_name, "r");
  761.     if (text_file == NULL) open_error(text_file_name);
  762.     while ((c = getc(text_file)) != EOF)
  763.     {
  764.     if (c == '\n') ++outline;
  765.     putc(c, output_file);
  766.     }
  767. }
  768.  
  769. void
  770. output_debug(void)
  771. {
  772.     register int i, j, k, max;
  773.     char **symnam, *s;
  774.  
  775.     if (!lflag)
  776.     {
  777.     ++outline;
  778.     fprintf(output_file, line_format, outline, output_file_name);
  779.     }
  780.     ++outline;
  781.     fprintf(output_file, "#define YYFINAL %d\n", final_state);
  782.     outline += 3;
  783.     fprintf(output_file, "#ifndef YYDEBUG\n#define YYDEBUG %d\n#endif\n",
  784.         tflag);
  785.  
  786.     max = 0;
  787.     for (i = 2; i < ntokens; ++i)
  788.     if (symbol_value[i] > max)
  789.         max = symbol_value[i];
  790.     ++outline;
  791.     fprintf(output_file, "#define YYMAXTOKEN %d\n", max);
  792.  
  793.     symnam = (char **) MALLOC(max*sizeof(char *));
  794.     if (symnam == 0) no_space();
  795.     for (i = 0; i < max; ++i)
  796.     symnam[i] = 0;
  797.     for (i = ntokens - 1; i >= 2; --i)
  798.     symnam[symbol_value[i]] = symbol_name[i];
  799.     symnam[0] = "end-of-file";
  800.  
  801.     ++outline;
  802.     fprintf(output_file, "#if YYDEBUG\nchar *yyname[] = {");
  803.     j = 80;
  804.     for (i = 0; i <= max; ++i)
  805.     {
  806.     if (s = symnam[i])
  807.     {
  808.         if (s[0] == '"')
  809.         {
  810.         k = 7;
  811.         while (*++s != '"')
  812.         {
  813.             if (*s == '\\')
  814.             {
  815.             k += 2;
  816.             if (*++s == '\\')
  817.                 k += 2;
  818.             else
  819.                 ++k;
  820.             }
  821.             else
  822.             ++k;
  823.         }
  824.         j += k;
  825.         if (j > 80)
  826.         {
  827.             ++outline;
  828.             putc('\n', output_file);
  829.             j = k;
  830.         }
  831.         fprintf(output_file, "\"\\\"");
  832.         s = symnam[i];
  833.         while (*++s != '"')
  834.         {
  835.             if (*s == '\\')
  836.             {
  837.             fprintf(output_file, "\\\\");
  838.             if (*++s == '\\')
  839.                 fprintf(output_file, "\\\\");
  840.             else
  841.                 putc(*s, output_file);
  842.             }
  843.             else
  844.             putc(*s, output_file);
  845.         }
  846.         fprintf(output_file, "\\\"\",");
  847.         }
  848.         else if (s[0] == '\'')
  849.         {
  850.         if (s[1] == '"')
  851.         {
  852.             j += 7;
  853.             if (j > 80)
  854.             {
  855.             ++outline;
  856.             putc('\n', output_file);
  857.             j = 7;
  858.             }
  859.             fprintf(output_file, "\"'\\\"'\",");
  860.         }
  861.         else
  862.         {
  863.             k = 5;
  864.             while (*++s != '\'')
  865.             {
  866.             if (*s == '\\')
  867.             {
  868.                 k += 2;
  869.                 ++s;
  870.                 if (*++s == '\\')
  871.                 k += 2;
  872.                 else
  873.                 ++k;
  874.             }
  875.             else
  876.                 ++k;
  877.             }
  878.             j += k;
  879.             if (j > 80)
  880.             {
  881.             ++outline;
  882.             putc('\n', output_file);
  883.             j = k;
  884.             }
  885.             fprintf(output_file, "\"'");
  886.             s = symnam[i];
  887.             while (*++s != '\'')
  888.             {
  889.             if (*s == '\\')
  890.             {
  891.                 fprintf(output_file, "\\\\");
  892.                 if (*++s == '\\')
  893.                 fprintf(output_file, "\\\\");
  894.                 else
  895.                 putc(*s, output_file);
  896.             }
  897.             else
  898.                 putc(*s, output_file);
  899.             }
  900.             fprintf(output_file, "'\",");
  901.         }
  902.         }
  903.         else
  904.         {
  905.         k = strlen(s) + 3;
  906.         j += k;
  907.         if (j > 80)
  908.         {
  909.             ++outline;
  910.             putc('\n', output_file);
  911.             j = k;
  912.         }
  913.         putc('"', output_file);
  914.         do { putc(*s, output_file); } while (*++s);
  915.         fprintf(output_file, "\",");
  916.         }
  917.     }
  918.     else
  919.     {
  920.         j += 2;
  921.         if (j > 80)
  922.         {
  923.         ++outline;
  924.         putc('\n', output_file);
  925.         j = 2;
  926.         }
  927.         fprintf(output_file, "0,");
  928.     }
  929.     }
  930.     outline += 2;
  931.     fprintf(output_file, "\n};\n");
  932.     FREE(symnam);
  933.  
  934.     ++outline;
  935.     fprintf(output_file, "char *yyrule[] = {\n");
  936.     for (i = 2; i < nrules; ++i)
  937.     {
  938.     fprintf(output_file, "\"%s :", symbol_name[rlhs[i]]);
  939.     for (j = rrhs[i]; ritem[j] > 0; ++j)
  940.     {
  941.         s = symbol_name[ritem[j]];
  942.         if (s[0] == '"')
  943.         {
  944.         fprintf(output_file, " \\\"");
  945.         while (*++s != '"')
  946.         {
  947.             if (*s == '\\')
  948.             {
  949.             if (s[1] == '\\')
  950.                 fprintf(output_file, "\\\\\\\\");
  951.             else
  952.                 fprintf(output_file, "\\\\%c", s[1]);
  953.             ++s;
  954.             }
  955.             else
  956.             putc(*s, output_file);
  957.         }
  958.         fprintf(output_file, "\\\"");
  959.         }
  960.         else if (s[0] == '\'')
  961.         {
  962.         if (s[1] == '"')
  963.             fprintf(output_file, " '\\\"'");
  964.         else if (s[1] == '\\')
  965.         {
  966.             if (s[2] == '\\')
  967.             fprintf(output_file, " '\\\\\\\\");
  968.             else
  969.             fprintf(output_file, " '\\\\%c", s[2]);
  970.             s += 2;
  971.             while (*++s != '\'')
  972.             putc(*s, output_file);
  973.             putc('\'', output_file);
  974.         }
  975.         else
  976.             fprintf(output_file, " '%c'", s[1]);
  977.         }
  978.         else
  979.         fprintf(output_file, " %s", s);
  980.     }
  981.     ++outline;
  982.     fprintf(output_file, "\",\n");
  983.     }
  984.  
  985.     outline += 2;
  986.     fprintf(output_file, "};\n#endif\n");
  987. }
  988.  
  989. void
  990. output_stype(void)
  991. {
  992.     if (!unionized)
  993.     {
  994.     outline += 3;
  995.     fprintf(output_file, "#ifndef YYSTYPE\ntypedef int YYSTYPE;\n#endif\n");
  996.     }
  997. }
  998.  
  999. void
  1000. output_trailing_text(void)
  1001. {
  1002.     register int c, last;
  1003.  
  1004.     if (line == 0)
  1005.     return;
  1006.  
  1007.     c = *cptr;
  1008.     if (c == '\n')
  1009.     {
  1010.     ++lineno;
  1011.     if ((c = getc(input_file)) == EOF)
  1012.         return;
  1013.     if (!lflag)
  1014.     {
  1015.         ++outline;
  1016.         fprintf(output_file, line_format, lineno, input_file_name);
  1017.     }
  1018.     if (c == '\n') ++outline;
  1019.     putc(c, output_file);
  1020.     last = c;
  1021.     }
  1022.     else
  1023.     {
  1024.     if (!lflag)
  1025.     {
  1026.         ++outline;
  1027.         fprintf(output_file, line_format, lineno, input_file_name);
  1028.     }
  1029.     do { putc(c, output_file); } while ((c = *++cptr) != '\n');
  1030.     ++outline;
  1031.     putc('\n', output_file);
  1032.     last = '\n';
  1033.     }
  1034.  
  1035.     while ((c = getc(input_file)) != EOF)
  1036.     {
  1037.     if (c == '\n') ++outline;
  1038.     putc(c, output_file);
  1039.     last = c;
  1040.     }
  1041.  
  1042.     if (last != '\n')
  1043.     {
  1044.     ++outline;
  1045.     putc('\n', output_file);
  1046.     }
  1047.     if (!lflag)
  1048.     {
  1049.     ++outline;
  1050.     fprintf(output_file, line_format, outline, output_file_name);
  1051.     }
  1052. }
  1053.  
  1054. void
  1055. output_semantic_actions()
  1056. {
  1057.     register int c, last;
  1058.  
  1059.     fclose(action_file);
  1060.     action_file = fopen(action_file_name, "r");
  1061.     if (action_file == NULL) open_error(action_file_name);
  1062.  
  1063.     last = '\n';
  1064.     while ((c = getc(action_file)) != EOF)
  1065.     {
  1066.     if (c == '\n') ++outline;
  1067.     putc(c, output_file);
  1068.     last = c;
  1069.     }
  1070.  
  1071.     if (last != '\n')
  1072.     {
  1073.     ++outline;
  1074.     putc('\n', output_file);
  1075.     }
  1076.     if (!lflag)
  1077.     {
  1078.     ++outline;
  1079.     fprintf(output_file, line_format, outline, output_file_name);
  1080.     }
  1081. }
  1082.  
  1083. void
  1084. free_itemsets()
  1085. {
  1086.   register core *cp;
  1087.  
  1088.   FREE(state_table);
  1089.  
  1090.   for (cp = first_state; cp; cp = cp->next)
  1091.     FREE(cp);
  1092. }
  1093.  
  1094.  
  1095. void
  1096. free_shifts()
  1097. {
  1098.   register shifts *sp;
  1099.  
  1100.   FREE(shift_table);
  1101.  
  1102.   for (sp = first_shift; sp; sp = sp->next)
  1103.     FREE(sp);
  1104. }
  1105.  
  1106.  
  1107. void
  1108. free_reductions()
  1109. {
  1110.   register reductions *rp;
  1111.  
  1112.   FREE(reduction_table);
  1113.  
  1114.   for (rp = first_reduction; rp; rp = rp->next)
  1115.     FREE(rp);
  1116. }
  1117.