home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 342a.lha / Yacc_v1.0a / verbose.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-05  |  6.4 KB  |  338 lines

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