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