home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / BYACC.ZIP / RCS / OUTPUT.C_V < prev    next >
Text File  |  1992-06-10  |  20KB  |  1,141 lines

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