home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / language / bison / Old_Bison / C / Conflicts < prev    next >
Encoding:
Text File  |  1990-04-01  |  14.5 KB  |  720 lines

  1. /* Find and resolve or report look-ahead conflicts for bison,
  2.    Copyright (C) 1984, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include "system.h"
  23. #include "machine.h"
  24. #include "new.h"
  25. #include "files.h"
  26. #include "gram.h"
  27. #include "state.h"
  28.  
  29. #define bcopy(src, dst, num) memcpy((dst), (src), (num))
  30.  
  31. extern char **tags;
  32. extern int tokensetsize;
  33. extern char *consistent;
  34. extern short *accessing_symbol;
  35. extern shifts **shift_table;
  36. extern unsigned *LA;
  37. extern short *LAruleno;
  38. extern short *lookaheads;
  39. extern int verboseflag;
  40.  
  41. char any_conflicts;
  42. char *conflicts;
  43. errs **err_table;
  44. int expected_conflicts;
  45.  
  46. static unsigned *shiftset;
  47. static unsigned *lookaheadset;
  48. static int src_total;
  49. static int rrc_total;
  50. static int src_count;
  51. static int rrc_count;
  52.  
  53.  
  54. void initialize_conflicts(void)
  55. {
  56.   register int i;
  57. /*  register errs *sp; JF unused */
  58.  
  59.   conflicts = NEW2(nstates, char);
  60.   shiftset = NEW2(tokensetsize, unsigned);
  61.   lookaheadset = NEW2(tokensetsize, unsigned);
  62.  
  63.   err_table = NEW2(nstates, errs *);
  64.  
  65.   any_conflicts = 0;
  66.  
  67.   for (i = 0; i < nstates; i++)
  68.     set_conflicts(i);
  69. }
  70.  
  71.  
  72. void set_conflicts(int state)
  73. {
  74.   register int i;
  75.   register int k;
  76.   register shifts *shiftp;
  77.   register unsigned *fp2;
  78.   register unsigned *fp3;
  79.   register unsigned *fp4;
  80.   register unsigned *fp1;
  81.   register int symbol;
  82.  
  83.   if (consistent[state]) return;
  84.  
  85.   for (i = 0; i < tokensetsize; i++)
  86.     lookaheadset[i] = 0;
  87.  
  88.   shiftp = shift_table[state];
  89.   if (shiftp)
  90.     {
  91.       k = shiftp->nshifts;
  92.       for (i = 0; i < k; i++)
  93.     {
  94.       symbol = accessing_symbol[shiftp->shifts[i]];
  95.       if (ISVAR(symbol)) break;
  96.       SETBIT(lookaheadset, symbol);
  97.     }
  98.     }
  99.  
  100.   k = lookaheads[state + 1];
  101.   fp4 = lookaheadset + tokensetsize;
  102.  
  103.   /* loop over all rules which require lookahead in this state */
  104.   /* first check for shift-reduce conflict, and try to resolve using precedence  */
  105.  
  106.   for (i = lookaheads[state]; i < k; i++)
  107.     if (rprec[LAruleno[i]])
  108.       {
  109.     fp1 = LA + i * tokensetsize;
  110.     fp2 = fp1;
  111.     fp3 = lookaheadset;
  112.   
  113.     while (fp3 < fp4)
  114.       {
  115.         if (*fp2++ & *fp3++)
  116.           {
  117.         resolve_sr_conflict(state, i);
  118.         break;
  119.           }
  120.       }
  121.       }
  122.  
  123.   /* loop over all rules which require lookahead in this state */
  124.   /* Check for conflicts not resolved above.  */
  125.  
  126.   for (i = lookaheads[state]; i < k; i++)
  127.     {
  128.       fp1 = LA + i * tokensetsize;
  129.       fp2 = fp1;
  130.       fp3 = lookaheadset;
  131.  
  132.       while (fp3 < fp4)
  133.     {
  134.       if (*fp2++ & *fp3++)
  135.         {
  136.           conflicts[state] = 1;
  137.           any_conflicts = 1;
  138.         }
  139.     }
  140.  
  141.       fp2 = fp1;
  142.       fp3 = lookaheadset;
  143.  
  144.       while (fp3 < fp4)
  145.     *fp3++ |= *fp2++;
  146.     }
  147. }
  148.  
  149.  
  150.  
  151. /* Attempt to resolve shift-reduce conflict for one rule
  152. by means of precedence declarations.
  153. It has already been checked that the rule has a precedence.
  154. A conflict is resolved by modifying the shift or reduce tables
  155. so that there is no longer a conflict.  */
  156.  
  157. void resolve_sr_conflict(int state, int lookaheadnum)
  158. {
  159.   register int i;
  160.   register int mask;
  161.   register unsigned *fp1;
  162.   register unsigned *fp2;
  163.   register int redprec;
  164.   errs *errp = (errs *) alloca (sizeof(errs) + ntokens * sizeof(short));
  165.   short *errtokens = errp->errs;
  166.  
  167.   /* find the rule to reduce by to get precedence of reduction  */
  168.   redprec = rprec[LAruleno[lookaheadnum]];
  169.  
  170.   mask = 1;
  171.   fp1 = LA + lookaheadnum * tokensetsize;
  172.   fp2 = lookaheadset;
  173.   for (i = 0; i < ntokens; i++)
  174.     {
  175.       if ((mask & *fp2 & *fp1) && sprec[i])
  176.     /* shift-reduce conflict occurs for token number i and it has a precision.
  177.        The precedence of shifting is that of token i.  */
  178.     {
  179.       if (sprec[i] < redprec)
  180.         {
  181.           if (verboseflag) log_resolution(state, lookaheadnum, i, "reduce");
  182.           *fp2 &= ~mask;  /* flush the shift for this token */
  183.           flush_shift(state, i);
  184.         }
  185.       else if (sprec[i] > redprec)
  186.         {
  187.           if (verboseflag) log_resolution(state, lookaheadnum, i, "shift");
  188.           *fp1 &= ~mask;  /* flush the reduce for this token */
  189.         }
  190.       else
  191.         {
  192.           /* Matching precedence levels.
  193.          For left association, keep only the reduction.
  194.          For right association, keep only the shift.
  195.          For nonassociation, keep neither.  */
  196.  
  197.           switch (sassoc[i])
  198.         {
  199.  
  200.         case RIGHT_ASSOC:
  201.               if (verboseflag) log_resolution(state, lookaheadnum, i, "shift");
  202.           break;
  203.  
  204.         case LEFT_ASSOC:
  205.               if (verboseflag) log_resolution(state, lookaheadnum, i, "reduce");
  206.           break;
  207.  
  208.         case NON_ASSOC:
  209.               if (verboseflag) log_resolution(state, lookaheadnum, i, "an error");
  210.           break;
  211.         }
  212.  
  213.           if (sassoc[i] != RIGHT_ASSOC)
  214.         {
  215.           *fp2 &= ~mask;  /* flush the shift for this token */
  216.           flush_shift(state, i);
  217.         }
  218.           if (sassoc[i] != LEFT_ASSOC)
  219.             {
  220.           *fp1 &= ~mask;  /* flush the reduce for this token */
  221.         }
  222.           if (sassoc[i] == NON_ASSOC)
  223.         {
  224.           /* Record an explicit error for this token.  */
  225.           *errtokens++ = i;
  226.         }
  227.         }
  228.     }
  229.  
  230.       mask <<= 1;
  231.       if (mask == 0)
  232.     {
  233.       mask = 1;
  234.       fp2++;  fp1++;
  235.     }
  236.     }
  237.   errp->nerrs = errtokens - errp->errs;
  238.   if (errp->nerrs)
  239.     {
  240.       /* Some tokens have been explicitly made errors.  Allocate
  241.      a permanent errs structure for this state, to record them.  */
  242.       i = (char *) errtokens - (char *) errp;
  243.       err_table[state] = (errs *) mallocate ((unsigned int)i);
  244.       bcopy (errp, err_table[state], i);
  245.     }
  246.   else
  247.     err_table[state] = 0;
  248. }
  249.  
  250.  
  251.  
  252. /* turn off the shift recorded for the specified token in the specified state.
  253. Used when we resolve a shift-reduce conflict in favor of the reduction.  */
  254.  
  255. void flush_shift(int state, int token)
  256. {
  257.   register shifts *shiftp;
  258.   register int k, i;
  259. /*  register unsigned symbol; JF unused */
  260.  
  261.   shiftp = shift_table[state];
  262.  
  263.   if (shiftp)
  264.     {
  265.       k = shiftp->nshifts;
  266.       for (i = 0; i < k; i++)
  267.     {
  268.       if (shiftp->shifts[i] && token == accessing_symbol[shiftp->shifts[i]])
  269.         (shiftp->shifts[i]) = 0;
  270.     }
  271.     }
  272. }
  273.  
  274.  
  275. void log_resolution(int state, int LAno, int token, char *resolution)
  276. {
  277.   fprintf(foutput,
  278.       "Conflict in state %d between rule %d and token %s resolved as %s.\n",
  279.       state, LAruleno[LAno], tags[token], resolution);
  280. }
  281.  
  282.  
  283. void conflict_log(void)
  284. {
  285.   register int i;
  286.  
  287.   src_total = 0;
  288.   rrc_total = 0;
  289.  
  290.   for (i = 0; i < nstates; i++)
  291.     {
  292.       if (conflicts[i])
  293.     {
  294.       count_sr_conflicts(i);
  295.       count_rr_conflicts(i);
  296.       src_total += src_count;
  297.       rrc_total += rrc_count;
  298.     }
  299.     }
  300.  
  301.   total_conflicts();
  302. }
  303.   
  304.  
  305. void verbose_conflict_log(void)
  306. {
  307.   register int i;
  308.  
  309.   src_total = 0;
  310.   rrc_total = 0;
  311.  
  312.   for (i = 0; i < nstates; i++)
  313.     {
  314.       if (conflicts[i])
  315.     {
  316.       count_sr_conflicts(i);
  317.       count_rr_conflicts(i);
  318.       src_total += src_count;
  319.       rrc_total += rrc_count;
  320.  
  321.       fprintf(foutput, "State %d contains", i);
  322.  
  323.       if (src_count == 1)
  324.         fprintf(foutput, " 1 shift/reduce conflict");
  325.       else if (src_count > 1)
  326.         fprintf(foutput, " %d shift/reduce conflicts", src_count);
  327.  
  328.       if (src_count > 0 && rrc_count > 0)
  329.         fprintf(foutput, " and");
  330.  
  331.       if (rrc_count == 1)
  332.         fprintf(foutput, " 1 reduce/reduce conflict");
  333.       else if (rrc_count > 1)
  334.         fprintf(foutput, " %d reduce/reduce conflicts", rrc_count);
  335.  
  336.       putc('.', foutput);
  337.       putc('\n', foutput);
  338.     }
  339.     }
  340.  
  341.   total_conflicts();
  342. }
  343.  
  344.  
  345. void total_conflicts(void)
  346. {
  347.   extern int fixed_outfiles;
  348.  
  349.   if (src_total == expected_conflicts && rrc_total == 0)
  350.     return;
  351.  
  352.   if (fixed_outfiles)
  353.     {
  354.       /* If invoked with -y (yacc-compatible), use the output format
  355.      specified by POSIX.  */
  356.       fprintf(stderr, "conflicts: ");
  357.       if (src_total > 0)
  358.     fprintf(stderr, " %d shift/reduce", src_total);
  359.       if (src_total > 0 && rrc_total > 0)
  360.     fprintf(stderr, ",");
  361.       if (rrc_total > 0)
  362.     fprintf(stderr, " %d reduce/reduce", rrc_total);
  363.       putc('\n', stderr);
  364.     }
  365.   else
  366.     {
  367.       fprintf(stderr, "%s contains", infile);
  368.  
  369.       if (src_total == 1)
  370.     fprintf(stderr, " 1 shift/reduce conflict");
  371.       else if (src_total > 1)
  372.     fprintf(stderr, " %d shift/reduce conflicts", src_total);
  373.  
  374.       if (src_total > 0 && rrc_total > 0)
  375.     fprintf(stderr, " and");
  376.  
  377.       if (rrc_total == 1)
  378.     fprintf(stderr, " 1 reduce/reduce conflict");
  379.       else if (rrc_total > 1)
  380.     fprintf(stderr, " %d reduce/reduce conflicts", rrc_total);
  381.  
  382.       putc('.', stderr);
  383.       putc('\n', stderr);
  384.     }
  385. }
  386.  
  387.  
  388. void count_sr_conflicts(int state)
  389. {
  390.   register int i;
  391.   register int k;
  392.   register int mask;
  393.   register shifts *shiftp;
  394.   register unsigned *fp1;
  395.   register unsigned *fp2;
  396.   register unsigned *fp3;
  397.   register int symbol;
  398.  
  399.   src_count = 0;
  400.  
  401.   shiftp = shift_table[state];
  402.   if (!shiftp) return;
  403.  
  404.   for (i = 0; i < tokensetsize; i++)
  405.     {
  406.       shiftset[i] = 0;
  407.       lookaheadset[i] = 0;
  408.     }
  409.  
  410.   k = shiftp->nshifts;
  411.   for (i = 0; i < k; i++)
  412.     {
  413.       if (! shiftp->shifts[i]) continue;
  414.       symbol = accessing_symbol[shiftp->shifts[i]];
  415.       if (ISVAR(symbol)) break;
  416.       SETBIT(shiftset, symbol);
  417.     }
  418.  
  419.   k = lookaheads[state + 1];
  420.   fp3 = lookaheadset + tokensetsize;
  421.  
  422.   for (i = lookaheads[state]; i < k; i++)
  423.     {
  424.       fp1 = LA + i * tokensetsize;
  425.       fp2 = lookaheadset;
  426.  
  427.       while (fp2 < fp3)
  428.     *fp2++ |= *fp1++;
  429.     }
  430.  
  431.   fp1 = shiftset;
  432.   fp2 = lookaheadset;
  433.  
  434.   while (fp2 < fp3)
  435.     *fp2++ &= *fp1++;
  436.  
  437.   mask = 1;
  438.   fp2 = lookaheadset;
  439.   for (i = 0; i < ntokens; i++)
  440.     {
  441.       if (mask & *fp2)
  442.     src_count++;
  443.  
  444.       mask <<= 1;
  445.       if (mask == 0)
  446.     {
  447.       mask = 1;
  448.       fp2++;
  449.     }
  450.     }
  451. }
  452.  
  453.  
  454. void count_rr_conflicts(int state)
  455. {
  456.   register int i;
  457.   register int j;
  458.   register int count;
  459.   register unsigned mask;
  460.   register unsigned *baseword;
  461.   register unsigned *wordp;
  462.   register int m;
  463.   register int n;
  464.  
  465.   rrc_count = 0;
  466.  
  467.   m = lookaheads[state];
  468.   n = lookaheads[state + 1];
  469.  
  470.   if (n - m < 2) return;
  471.  
  472.   mask = 1;
  473.   baseword = LA + m * tokensetsize;
  474.   for (i = 0; i < ntokens; i++)
  475.     {
  476.       wordp = baseword;
  477.  
  478.       count = 0;
  479.       for (j = m; j < n; j++)
  480.     {
  481.       if (mask & *wordp)
  482.         count++;
  483.  
  484.       wordp += tokensetsize;
  485.     }
  486.  
  487.       if (count >= 2) rrc_count++;
  488.  
  489.       mask <<= 1;
  490.       if (mask == 0)
  491.     {
  492.       mask = 1;
  493.       baseword++;
  494.     }
  495.     }
  496. }
  497.  
  498.  
  499. void print_reductions(int state)
  500. {
  501.   register int i;
  502.   register int j;
  503.   register int k;
  504.   register unsigned *fp1;
  505.   register unsigned *fp2;
  506.   register unsigned *fp3;
  507.   register unsigned *fp4;
  508.   register int rule;
  509.   register int symbol;
  510.   register unsigned mask;
  511.   register int m;
  512.   register int n;
  513.   register int default_LA;
  514.   register int default_rule;
  515.   register int cmax;
  516.   register int count;
  517.   register shifts *shiftp;
  518.   register errs *errp;
  519.   int nodefault = 0;
  520.  
  521.   for (i = 0; i < tokensetsize; i++)
  522.     shiftset[i] = 0;
  523.  
  524.   shiftp = shift_table[state];
  525.   if (shiftp)
  526.     {
  527.       k = shiftp->nshifts;
  528.       for (i = 0; i < k; i++)
  529.     {
  530.       if (! shiftp->shifts[i]) continue;
  531.       symbol = accessing_symbol[shiftp->shifts[i]];
  532.       if (ISVAR(symbol)) break;
  533.       /* if this state has a shift for the error token,
  534.          don't use a default rule.  */
  535.       if (symbol == error_token_number) nodefault = 1;
  536.       SETBIT(shiftset, symbol);
  537.     }
  538.     }
  539.  
  540.   errp = err_table[state];
  541.   if (errp)
  542.     {
  543.       k = errp->nerrs;
  544.       for (i = 0; i < k; i++)
  545.     {
  546.       if (! errp->errs[i]) continue;
  547.       symbol = errp->errs[i];
  548.       SETBIT(shiftset, symbol);
  549.     }
  550.     }
  551.  
  552.   m = lookaheads[state];
  553.   n = lookaheads[state + 1];
  554.  
  555.   if (n - m == 1 && ! nodefault)
  556.     {
  557.       default_rule = LAruleno[m];
  558.  
  559.       fp1 = LA + m * tokensetsize;
  560.       fp2 = shiftset;
  561.       fp3 = lookaheadset;
  562.       fp4 = lookaheadset + tokensetsize;
  563.  
  564.       while (fp3 < fp4)
  565.     *fp3++ = *fp1++ & *fp2++;
  566.  
  567.       mask = 1;
  568.       fp3 = lookaheadset;
  569.  
  570.       for (i = 0; i < ntokens; i++)
  571.     {
  572.       if (mask & *fp3)
  573.         fprintf(foutput, "    %-4s\t[reduce  %d  (%s)]\n",
  574.             tags[i], default_rule, tags[rlhs[default_rule]]);
  575.  
  576.       mask <<= 1;
  577.       if (mask == 0)
  578.         {
  579.           mask = 1;
  580.           fp3++;
  581.         }
  582.     }
  583.  
  584.       fprintf(foutput, "    $default\treduce  %d  (%s)\n\n",
  585.           default_rule, tags[rlhs[default_rule]]);
  586.     }
  587.   else if (n - m >= 1)
  588.     {
  589.       cmax = 0;
  590.       default_LA = -1;
  591.       fp4 = lookaheadset + tokensetsize;
  592.  
  593.       if (! nodefault)
  594.     for (i = m; i < n; i++)
  595.       {
  596.         fp1 = LA + i * tokensetsize;
  597.         fp2 = shiftset;
  598.         fp3 = lookaheadset;
  599.   
  600.         while (fp3 < fp4)
  601.           *fp3++ = *fp1++ & ( ~ (*fp2++));
  602.   
  603.         count = 0;
  604.         mask = 1;
  605.         fp3 = lookaheadset;
  606.         for (j = 0; j < ntokens; j++)
  607.           {
  608.         if (mask & *fp3)
  609.           count++;
  610.   
  611.         mask <<= 1;
  612.         if (mask == 0)
  613.           {
  614.             mask = 1;
  615.             fp3++;
  616.           }
  617.           }
  618.   
  619.         if (count > cmax)
  620.           {
  621.         cmax = count;
  622.         default_LA = i;
  623.         default_rule = LAruleno[i];
  624.           }
  625.   
  626.         fp2 = shiftset;
  627.         fp3 = lookaheadset;
  628.   
  629.         while (fp3 < fp4)
  630.           *fp2++ |= *fp3++;
  631.       }
  632.  
  633.       for (i = 0; i < tokensetsize; i++)
  634.         shiftset[i] = 0;
  635.  
  636.       if (shiftp)
  637.         {
  638.           k = shiftp->nshifts;
  639.           for (i = 0; i < k; i++)
  640.         {
  641.           if (! shiftp->shifts[i]) continue;
  642.           symbol = accessing_symbol[shiftp->shifts[i]];
  643.           if (ISVAR(symbol)) break;
  644.           SETBIT(shiftset, symbol);
  645.         }
  646.         }
  647.  
  648.       mask = 1;
  649.       fp1 = LA + m * tokensetsize;
  650.       fp2 = shiftset;
  651.       for (i = 0; i < ntokens; i++)
  652.     {
  653.       int defaulted = 0;
  654.  
  655.       if (mask & *fp2)
  656.         count = 1;
  657.       else
  658.         count = 0;
  659.  
  660.       fp3 = fp1;
  661.       for (j = m; j < n; j++)
  662.         {
  663.           if (mask & *fp3)
  664.         {
  665.           if (count == 0)
  666.             {
  667.               if (j != default_LA)
  668.             {
  669.               rule = LAruleno[j];
  670.               fprintf(foutput, "    %-4s\treduce  %d  (%s)\n",
  671.                   tags[i], rule, tags[rlhs[rule]]);
  672.             }
  673.               else defaulted = 1;
  674.  
  675.               count++;
  676.             }
  677.           else
  678.             {
  679.               if (defaulted)
  680.             {
  681.               rule = LAruleno[default_LA];
  682.               fprintf(foutput, "    %-4s\treduce  %d  (%s)\n",
  683.                   tags[i], rule, tags[rlhs[rule]]);
  684.               defaulted = 0;
  685.             }
  686.               rule = LAruleno[j];
  687.               fprintf(foutput, "    %-4s\t[reduce  %d  (%s)]\n",
  688.                   tags[i], rule, tags[rlhs[rule]]);
  689.             }
  690.         }
  691.  
  692.           fp3 += tokensetsize;
  693.         }
  694.  
  695.       mask <<= 1;
  696.       if (mask == 0)
  697.         {
  698.           mask = 1;
  699.           fp1++;
  700.         }
  701.     }
  702.  
  703.       if (default_LA >= 0)
  704.     {
  705.       fprintf(foutput, "    $default\treduce  %d  (%s)\n",
  706.           default_rule, tags[rlhs[default_rule]]);
  707.     }
  708.  
  709.       putc('\n', foutput);
  710.     }
  711. }
  712.  
  713.  
  714. void finalize_conflicts(void)
  715. {
  716.   FREE(conflicts);
  717.   FREE(shiftset);
  718.   FREE(lookaheadset);
  719. }
  720.