home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / BYACC.ZIP / RCS / VERBOSE.C_V < prev    next >
Text File  |  1992-06-10  |  7KB  |  361 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.  
  26. static short *null_rules;
  27.  
  28. verbose()
  29. {
  30.     register int i;
  31.  
  32.     if (!vflag) return;
  33.  
  34.     null_rules = (short *) MALLOC(nrules*sizeof(short));
  35.     if (null_rules == 0) no_space();
  36.     fprintf(verbose_file, "\f\n");
  37.     for (i = 0; i < nstates; i++)
  38.     print_state(i);
  39.     FREE(null_rules);
  40.  
  41.     if (nunused)
  42.     log_unused();
  43.     if (SRtotal || RRtotal)
  44.     log_conflicts();
  45.  
  46.     fprintf(verbose_file, "\n\n%d terminals, %d nonterminals\n", ntokens,
  47.         nvars);
  48.     fprintf(verbose_file, "%d grammar rules, %d states\n", nrules - 2, nstates);
  49. }
  50.  
  51.  
  52. log_unused()
  53. {
  54.     register int i;
  55.     register short *p;
  56.  
  57.     fprintf(verbose_file, "\n\nRules never reduced:\n");
  58.     for (i = 3; i < nrules; ++i)
  59.     {
  60.     if (!rules_used[i])
  61.     {
  62.         fprintf(verbose_file, "\t%s :", symbol_name[rlhs[i]]);
  63.         for (p = ritem + rrhs[i]; *p >= 0; ++p)
  64.         fprintf(verbose_file, " %s", symbol_name[*p]);
  65.         fprintf(verbose_file, "  (%d)\n", i - 2);
  66.     }
  67.     }
  68. }
  69.  
  70.  
  71. log_conflicts()
  72. {
  73.     register int i;
  74.  
  75.     fprintf(verbose_file, "\n\n");
  76.     for (i = 0; i < nstates; i++)
  77.     {
  78.     if (SRconflicts[i] || RRconflicts[i])
  79.     {
  80.         fprintf(verbose_file, "State %d contains ", i);
  81.         if (SRconflicts[i] == 1)
  82.         fprintf(verbose_file, "1 shift/reduce conflict");
  83.         else if (SRconflicts[i] > 1)
  84.         fprintf(verbose_file, "%d shift/reduce conflicts",
  85.             SRconflicts[i]);
  86.         if (SRconflicts[i] && RRconflicts[i])
  87.         fprintf(verbose_file, ", ");
  88.         if (RRconflicts[i] == 1)
  89.         fprintf(verbose_file, "1 reduce/reduce conflict");
  90.         else if (RRconflicts[i] > 1)
  91.         fprintf(verbose_file, "%d reduce/reduce conflicts",
  92.             RRconflicts[i]);
  93.         fprintf(verbose_file, ".\n");
  94.     }
  95.     }
  96. }
  97.  
  98.  
  99. print_state(state)
  100. int state;
  101. {
  102.     if (state)
  103.     fprintf(verbose_file, "\n\n");
  104.     if (SRconflicts[state] || RRconflicts[state])
  105.     print_conflicts(state);
  106.     fprintf(verbose_file, "state %d\n", state);
  107.     print_core(state);
  108.     print_nulls(state);
  109.     print_actions(state);
  110. }
  111.  
  112.  
  113. print_conflicts(state)
  114. int state;
  115. {
  116.     register int symbol;
  117.     register action *p, *q, *r;
  118.  
  119.     for (p = parser[state]; p; p = q->next)
  120.     {
  121.     q = p;
  122.     if (p->action_code == ERROR || p->suppressed == 2)
  123.         continue;
  124.  
  125.     symbol = p->symbol;
  126.     while (q->next && q->next->symbol == symbol)
  127.         q = q->next;
  128.     if (state == final_state && symbol == 0)
  129.     {
  130.         r = p;
  131.         for (;;)
  132.         {
  133.         fprintf(verbose_file, "%d: shift/reduce conflict \
  134. (accept, reduce %d) on $end\n", state, r->number);
  135.         if (r == q) break;
  136.         r = r->next;
  137.         }
  138.     }
  139.     else if (p != q)
  140.     {
  141.         r = p->next;
  142.         if (p->action_code == SHIFT)
  143.         {
  144.         for (;;)
  145.         {
  146.             if (r->action_code == REDUCE && p->suppressed != 2)
  147.             fprintf(verbose_file, "%d: shift/reduce conflict \
  148. (shift %d, reduce %d) on %s\n", state, p->number, r->number,
  149.                 symbol_name[symbol]);
  150.             if (r == q) break;
  151.             r = r->next;
  152.         }
  153.         }
  154.         else
  155.         {
  156.         for (;;)
  157.         {
  158.             if (r->action_code == REDUCE && p->suppressed != 2)
  159.             fprintf(verbose_file, "%d: reduce/reduce conflict \
  160. (reduce %d, reduce %d) on %s\n", state, p->number, r->number,
  161.                 symbol_name[symbol]);
  162.             if (r == q) break;
  163.             r = r->next;
  164.         }
  165.         }
  166.     }
  167.     }
  168. }
  169.  
  170.  
  171. print_core(state)
  172. int state;
  173. {
  174.     register int i;
  175.     register int k;
  176.     register int rule;
  177.     register core *statep;
  178.     register short *sp;
  179.     register short *sp1;
  180.  
  181.     statep = state_table[state];
  182.     k = statep->nitems;
  183.  
  184.     for (i = 0; i < k; i++)
  185.     {
  186.     sp1 = sp = ritem + statep->items[i];
  187.  
  188.     while (*sp >= 0) ++sp;
  189.     rule = -(*sp);
  190.     fprintf(verbose_file, "\t%s : ", symbol_name[rlhs[rule]]);
  191.  
  192.         for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
  193.         fprintf(verbose_file, "%s ", symbol_name[*sp]);
  194.  
  195.     putc('.', verbose_file);
  196.  
  197.     while (*sp >= 0)
  198.     {
  199.         fprintf(verbose_file, " %s", symbol_name[*sp]);
  200.         sp++;
  201.     }
  202.     fprintf(verbose_file, "  (%d)\n", -2 - *sp);
  203.     }
  204. }
  205.  
  206.  
  207. print_nulls(state)
  208. int state;
  209. {
  210.     register action *p;
  211.     register int i, j, k, nnulls;
  212.  
  213.     nnulls = 0;
  214.     for (p = parser[state]; p; p = p->next)
  215.     {
  216.     if (p->action_code == REDUCE && p->suppressed == 0)
  217.     {
  218.         i = p->number;
  219.         if (rrhs[i] + 1 == rrhs[i+1])
  220.         {
  221.         for (j = 0; j < nnulls && i < null_rules[j]; ++j)
  222.             continue;
  223.         if (j == nnulls)
  224.         {
  225.             ++nnulls;
  226.             null_rules[j] = i;
  227.         }
  228.         else if (i != null_rules[j])
  229.         {
  230.             ++nnulls;
  231.             for (k = nnulls - 1; k > j; --k)
  232.             null_rules[k] = null_rules[k-1];
  233.             null_rules[j] = i;
  234.         }
  235.         }
  236.     }
  237.     }
  238.  
  239.     for (i = 0; i < nnulls; ++i)
  240.     {
  241.     j = null_rules[i];
  242.     fprintf(verbose_file, "\t%s : .  (%d)\n", symbol_name[rlhs[j]],
  243.         j - 2);
  244.     }
  245.     fprintf(verbose_file, "\n");
  246. }
  247.  
  248.  
  249. print_actions(stateno)
  250. int stateno;
  251. {
  252.     register action *p;
  253.     register shifts *sp;
  254.     register int as;
  255.  
  256.     if (stateno == final_state)
  257.     fprintf(verbose_file, "\t$end  accept\n");
  258.  
  259.     p = parser[stateno];
  260.     if (p)
  261.     {
  262.     print_shifts(p);
  263.     print_reductions(p, defred[stateno]);
  264.     }
  265.  
  266.     sp = shift_table[stateno];
  267.     if (sp && sp->nshifts > 0)
  268.     {
  269.     as = accessing_symbol[sp->shifts[sp->nshifts - 1]];
  270.     if (ISVAR(as))
  271.         print_gotos(stateno);
  272.     }
  273. }
  274.  
  275.  
  276. print_shifts(p)
  277. register action *p;
  278. {
  279.     register int count;
  280.     register action *q;
  281.  
  282.     count = 0;
  283.     for (q = p; q; q = q->next)
  284.     {
  285.     if (q->suppressed < 2 && q->action_code == SHIFT)
  286.         ++count;
  287.     }
  288.  
  289.     if (count > 0)
  290.     {
  291.     for (; p; p = p->next)
  292.     {
  293.         if (p->action_code == SHIFT && p->suppressed == 0)
  294.         fprintf(verbose_file, "\t%s  shift %d\n",
  295.                 symbol_name[p->symbol], p->number);
  296.     }
  297.     }
  298. }
  299.  
  300.  
  301. print_reductions(p, defred)
  302. register action *p;
  303. register int defred;
  304. {
  305.     register int k, anyreds;
  306.     register action *q;
  307.  
  308.     anyreds = 0;
  309.     for (q = p; q ; q = q->next)
  310.     {
  311.     if (q->action_code == REDUCE && q->suppressed < 2)
  312.     {
  313.         anyreds = 1;
  314.         break;
  315.     }
  316.     }
  317.  
  318.     if (anyreds == 0)
  319.     fprintf(verbose_file, "\t.  error\n");
  320.     else
  321.     {
  322.     for (; p; p = p->next)
  323.     {
  324.         if (p->action_code == REDUCE && p->number != defred)
  325.         {
  326.         k = p->number - 2;
  327.         if (p->suppressed == 0)
  328.             fprintf(verbose_file, "\t%s  reduce %d\n",
  329.                 symbol_name[p->symbol], k);
  330.         }
  331.     }
  332.  
  333.         if (defred > 0)
  334.         fprintf(verbose_file, "\t.  reduce %d\n", defred - 2);
  335.     }
  336. }
  337.  
  338.  
  339. print_gotos(stateno)
  340. int stateno;
  341. {
  342.     register int i, k;
  343.     register int as;
  344.     register short *to_state;
  345.     register shifts *sp;
  346.  
  347.     putc('\n', verbose_file);
  348.     sp = shift_table[stateno];
  349.     to_state = sp->shifts;
  350.     for (i = sp->nshifts - 1; i >= 0; i--)
  351.     {
  352.     k = to_state[i];
  353.     as = accessing_symbol[k];
  354.     if (ISVAR(as))
  355.         fprintf(verbose_file, "\t%s  goto %d\n", symbol_name[as], k);
  356.     else
  357.         break;
  358.     }
  359. }
  360. @
  361.