Home | History | Annotate | Download | only in src
      1 /* Find and resolve or report lookahead conflicts for bison,
      2 
      3    Copyright (C) 1984, 1989, 1992, 2000-2007, 2009-2012 Free Software
      4    Foundation, Inc.
      5 
      6    This file is part of Bison, the GNU Compiler Compiler.
      7 
      8    This program is free software: you can redistribute it and/or modify
      9    it under the terms of the GNU General Public License as published by
     10    the Free Software Foundation, either version 3 of the License, or
     11    (at your option) any later version.
     12 
     13    This program is distributed in the hope that it will be useful,
     14    but WITHOUT ANY WARRANTY; without even the implied warranty of
     15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16    GNU General Public License for more details.
     17 
     18    You should have received a copy of the GNU General Public License
     19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     20 
     21 #include <config.h>
     22 #include "system.h"
     23 
     24 #include <bitset.h>
     25 
     26 #include "LR0.h"
     27 #include "complain.h"
     28 #include "conflicts.h"
     29 #include "files.h"
     30 #include "getargs.h"
     31 #include "gram.h"
     32 #include "lalr.h"
     33 #include "print-xml.h"
     34 #include "reader.h"
     35 #include "state.h"
     36 #include "symtab.h"
     37 
     38 /* -1 stands for not specified. */
     39 int expected_sr_conflicts = -1;
     40 int expected_rr_conflicts = -1;
     41 static char *conflicts;
     42 static struct obstack solved_conflicts_obstack;
     43 static struct obstack solved_conflicts_xml_obstack;
     44 
     45 static bitset shift_set;
     46 static bitset lookahead_set;
     47 
     48 
     49 
     51 enum conflict_resolution
     52   {
     53     shift_resolution,
     54     reduce_resolution,
     55     left_resolution,
     56     right_resolution,
     57     nonassoc_resolution
     58   };
     59 
     60 
     61 /*----------------------------------------------------------------.
     62 | Explain how an SR conflict between TOKEN and RULE was resolved: |
     63 | RESOLUTION.                                                     |
     64 `----------------------------------------------------------------*/
     65 
     66 static inline void
     67 log_resolution (rule *r, symbol_number token,
     68 		enum conflict_resolution resolution)
     69 {
     70   if (report_flag & report_solved_conflicts)
     71     {
     72       /* The description of the resolution. */
     73       switch (resolution)
     74 	{
     75 	case shift_resolution:
     76 	case right_resolution:
     77 	  obstack_printf (&solved_conflicts_obstack,
     78 			  _("    Conflict between rule %d and token %s"
     79 			    " resolved as shift"),
     80 			  r->number,
     81 			  symbols[token]->tag);
     82 	  break;
     83 
     84 	case reduce_resolution:
     85 	case left_resolution:
     86 	  obstack_printf (&solved_conflicts_obstack,
     87 			  _("    Conflict between rule %d and token %s"
     88 			    " resolved as reduce"),
     89 			  r->number,
     90 			  symbols[token]->tag);
     91 	  break;
     92 
     93 	case nonassoc_resolution:
     94 	  obstack_printf (&solved_conflicts_obstack,
     95 			  _("    Conflict between rule %d and token %s"
     96 			    " resolved as an error"),
     97 			  r->number,
     98 			  symbols[token]->tag);
     99 	  break;
    100 	}
    101 
    102       /* The reason. */
    103       switch (resolution)
    104 	{
    105 	case shift_resolution:
    106 	  obstack_printf (&solved_conflicts_obstack,
    107 			  " (%s < %s)",
    108 			  r->prec->tag,
    109 			  symbols[token]->tag);
    110 	  break;
    111 
    112 	case reduce_resolution:
    113 	  obstack_printf (&solved_conflicts_obstack,
    114 			  " (%s < %s)",
    115 			  symbols[token]->tag,
    116 			  r->prec->tag);
    117 	  break;
    118 
    119 	case left_resolution:
    120 	  obstack_printf (&solved_conflicts_obstack,
    121 			  " (%%left %s)",
    122 			  symbols[token]->tag);
    123 	  break;
    124 
    125 	case right_resolution:
    126 	  obstack_printf (&solved_conflicts_obstack,
    127 			  " (%%right %s)",
    128 			  symbols[token]->tag);
    129 	  break;
    130 
    131 	case nonassoc_resolution:
    132 	  obstack_printf (&solved_conflicts_obstack,
    133 			  " (%%nonassoc %s)",
    134 			  symbols[token]->tag);
    135 	  break;
    136 	}
    137 
    138       obstack_sgrow (&solved_conflicts_obstack, ".\n");
    139     }
    140 
    141   /* XML report */
    142   if (xml_flag)
    143     {
    144       /* The description of the resolution. */
    145       switch (resolution)
    146         {
    147         case shift_resolution:
    148         case right_resolution:
    149           obstack_printf (&solved_conflicts_xml_obstack,
    150                           "        <resolution rule=\"%d\" symbol=\"%s\""
    151                           " type=\"shift\">",
    152                           r->number,
    153                           xml_escape (symbols[token]->tag));
    154           break;
    155 
    156         case reduce_resolution:
    157         case left_resolution:
    158           obstack_printf (&solved_conflicts_xml_obstack,
    159                           "        <resolution rule=\"%d\" symbol=\"%s\""
    160                           " type=\"reduce\">",
    161                           r->number,
    162                           xml_escape (symbols[token]->tag));
    163           break;
    164 
    165         case nonassoc_resolution:
    166           obstack_printf (&solved_conflicts_xml_obstack,
    167                           "        <resolution rule=\"%d\" symbol=\"%s\""
    168                           " type=\"error\">",
    169                           r->number,
    170                           xml_escape (symbols[token]->tag));
    171           break;
    172         }
    173 
    174       /* The reason. */
    175       switch (resolution)
    176         {
    177         case shift_resolution:
    178           obstack_printf (&solved_conflicts_xml_obstack,
    179                           "%s &lt; %s",
    180                           xml_escape_n (0, r->prec->tag),
    181                           xml_escape_n (1, symbols[token]->tag));
    182           break;
    183 
    184         case reduce_resolution:
    185           obstack_printf (&solved_conflicts_xml_obstack,
    186                           "%s &lt; %s",
    187                           xml_escape_n (0, symbols[token]->tag),
    188                           xml_escape_n (1, r->prec->tag));
    189           break;
    190 
    191         case left_resolution:
    192           obstack_printf (&solved_conflicts_xml_obstack,
    193                           "%%left %s",
    194                           xml_escape (symbols[token]->tag));
    195           break;
    196 
    197         case right_resolution:
    198           obstack_printf (&solved_conflicts_xml_obstack,
    199                           "%%right %s",
    200                           xml_escape (symbols[token]->tag));
    201           break;
    202 
    203         case nonassoc_resolution:
    204           obstack_printf (&solved_conflicts_xml_obstack,
    205                           "%%nonassoc %s",
    206                           xml_escape (symbols[token]->tag));
    207       break;
    208         }
    209 
    210       obstack_sgrow (&solved_conflicts_xml_obstack, "</resolution>\n");
    211     }
    212 }
    213 
    214 
    215 /*------------------------------------------------------------------.
    216 | Turn off the shift recorded for the specified token in the        |
    217 | specified state.  Used when we resolve a shift-reduce conflict in |
    218 | favor of the reduction or as an error (%nonassoc).                |
    219 `------------------------------------------------------------------*/
    220 
    221 static void
    222 flush_shift (state *s, int token)
    223 {
    224   transitions *trans = s->transitions;
    225   int i;
    226 
    227   bitset_reset (lookahead_set, token);
    228   for (i = 0; i < trans->num; i++)
    229     if (!TRANSITION_IS_DISABLED (trans, i)
    230 	&& TRANSITION_SYMBOL (trans, i) == token)
    231       TRANSITION_DISABLE (trans, i);
    232 }
    233 
    234 
    235 /*--------------------------------------------------------------------.
    236 | Turn off the reduce recorded for the specified token in the         |
    237 | specified lookahead set.  Used when we resolve a shift-reduce       |
    238 | conflict in favor of the shift or as an error (%nonassoc).          |
    239 `--------------------------------------------------------------------*/
    240 
    241 static void
    242 flush_reduce (bitset lookahead_tokens, int token)
    243 {
    244   bitset_reset (lookahead_tokens, token);
    245 }
    246 
    247 
    248 /*------------------------------------------------------------------.
    249 | Attempt to resolve shift-reduce conflict for one rule by means of |
    250 | precedence declarations.  It has already been checked that the    |
    251 | rule has a precedence.  A conflict is resolved by modifying the   |
    252 | shift or reduce tables so that there is no longer a conflict.     |
    253 |                                                                   |
    254 | RULENO is the number of the lookahead bitset to consider.         |
    255 |                                                                   |
    256 | ERRORS and NERRS can be used to store discovered explicit         |
    257 | errors.                                                           |
    258 `------------------------------------------------------------------*/
    259 
    260 static void
    261 resolve_sr_conflict (state *s, int ruleno, symbol **errors, int *nerrs)
    262 {
    263   symbol_number i;
    264   reductions *reds = s->reductions;
    265   /* Find the rule to reduce by to get precedence of reduction.  */
    266   rule *redrule = reds->rules[ruleno];
    267   int redprec = redrule->prec->prec;
    268   bitset lookahead_tokens = reds->lookahead_tokens[ruleno];
    269 
    270   for (i = 0; i < ntokens; i++)
    271     if (bitset_test (lookahead_tokens, i)
    272 	&& bitset_test (lookahead_set, i)
    273 	&& symbols[i]->prec)
    274       {
    275 	/* Shift-reduce conflict occurs for token number i
    276 	   and it has a precedence.
    277 	   The precedence of shifting is that of token i.  */
    278 	if (symbols[i]->prec < redprec)
    279 	  {
    280 	    log_resolution (redrule, i, reduce_resolution);
    281 	    flush_shift (s, i);
    282 	  }
    283 	else if (symbols[i]->prec > redprec)
    284 	  {
    285 	    log_resolution (redrule, i, shift_resolution);
    286 	    flush_reduce (lookahead_tokens, i);
    287 	  }
    288 	else
    289 	  /* Matching precedence levels.
    290 	     For left association, keep only the reduction.
    291 	     For right association, keep only the shift.
    292 	     For nonassociation, keep neither.  */
    293 
    294 	  switch (symbols[i]->assoc)
    295 	    {
    296 	    default:
    297 	      abort ();
    298 
    299 	    case right_assoc:
    300 	      log_resolution (redrule, i, right_resolution);
    301 	      flush_reduce (lookahead_tokens, i);
    302 	      break;
    303 
    304 	    case left_assoc:
    305 	      log_resolution (redrule, i, left_resolution);
    306 	      flush_shift (s, i);
    307 	      break;
    308 
    309 	    case non_assoc:
    310 	      log_resolution (redrule, i, nonassoc_resolution);
    311 	      flush_shift (s, i);
    312 	      flush_reduce (lookahead_tokens, i);
    313 	      /* Record an explicit error for this token.  */
    314 	      errors[(*nerrs)++] = symbols[i];
    315 	      break;
    316 	    }
    317       }
    318 }
    319 
    320 
    321 /*-------------------------------------------------------------------.
    322 | Solve the S/R conflicts of state S using the                       |
    323 | precedence/associativity, and flag it inconsistent if it still has |
    324 | conflicts.  ERRORS can be used as storage to compute the list of   |
    325 | lookahead tokens on which S raises a syntax error (%nonassoc).     |
    326 `-------------------------------------------------------------------*/
    327 
    328 static void
    329 set_conflicts (state *s, symbol **errors)
    330 {
    331   int i;
    332   transitions *trans = s->transitions;
    333   reductions *reds = s->reductions;
    334   int nerrs = 0;
    335 
    336   if (s->consistent)
    337     return;
    338 
    339   bitset_zero (lookahead_set);
    340 
    341   FOR_EACH_SHIFT (trans, i)
    342     bitset_set (lookahead_set, TRANSITION_SYMBOL (trans, i));
    343 
    344   /* Loop over all rules which require lookahead in this state.  First
    345      check for shift-reduce conflict, and try to resolve using
    346      precedence.  */
    347   for (i = 0; i < reds->num; ++i)
    348     if (reds->rules[i]->prec && reds->rules[i]->prec->prec
    349 	&& !bitset_disjoint_p (reds->lookahead_tokens[i], lookahead_set))
    350       resolve_sr_conflict (s, i, errors, &nerrs);
    351 
    352   if (nerrs)
    353     {
    354       /* Some tokens have been explicitly made errors.  Allocate a
    355          permanent errs structure for this state, to record them.  */
    356       state_errs_set (s, nerrs, errors);
    357     }
    358   if (obstack_object_size (&solved_conflicts_obstack))
    359     {
    360       obstack_1grow (&solved_conflicts_obstack, '\0');
    361       s->solved_conflicts = obstack_finish (&solved_conflicts_obstack);
    362     }
    363   if (obstack_object_size (&solved_conflicts_xml_obstack))
    364     {
    365       obstack_1grow (&solved_conflicts_xml_obstack, '\0');
    366       s->solved_conflicts_xml = obstack_finish (&solved_conflicts_xml_obstack);
    367     }
    368 
    369   /* Loop over all rules which require lookahead in this state.  Check
    370      for conflicts not resolved above.  */
    371   for (i = 0; i < reds->num; ++i)
    372     {
    373       if (!bitset_disjoint_p (reds->lookahead_tokens[i], lookahead_set))
    374 	conflicts[s->number] = 1;
    375       bitset_or (lookahead_set, lookahead_set, reds->lookahead_tokens[i]);
    376     }
    377 }
    378 
    379 
    380 /*----------------------------------------------------------------.
    381 | Solve all the S/R conflicts using the precedence/associativity, |
    382 | and flag as inconsistent the states that still have conflicts.  |
    383 `----------------------------------------------------------------*/
    384 
    385 void
    386 conflicts_solve (void)
    387 {
    388   state_number i;
    389   /* List of lookahead tokens on which we explicitly raise a syntax error.  */
    390   symbol **errors = xnmalloc (ntokens + 1, sizeof *errors);
    391 
    392   conflicts = xcalloc (nstates, sizeof *conflicts);
    393   shift_set = bitset_create (ntokens, BITSET_FIXED);
    394   lookahead_set = bitset_create (ntokens, BITSET_FIXED);
    395   obstack_init (&solved_conflicts_obstack);
    396   obstack_init (&solved_conflicts_xml_obstack);
    397 
    398   for (i = 0; i < nstates; i++)
    399     {
    400       set_conflicts (states[i], errors);
    401 
    402       /* For uniformity of the code, make sure all the states have a valid
    403 	 `errs' member.  */
    404       if (!states[i]->errs)
    405 	states[i]->errs = errs_new (0, 0);
    406     }
    407 
    408   free (errors);
    409 }
    410 
    411 
    412 void
    413 conflicts_update_state_numbers (state_number old_to_new[],
    414                                 state_number nstates_old)
    415 {
    416   state_number i;
    417   for (i = 0; i < nstates_old; ++i)
    418     if (old_to_new[i] != nstates_old)
    419       conflicts[old_to_new[i]] = conflicts[i];
    420 }
    421 
    422 
    423 /*---------------------------------------------.
    424 | Count the number of shift/reduce conflicts.  |
    425 `---------------------------------------------*/
    426 
    427 static int
    428 count_sr_conflicts (state *s)
    429 {
    430   int i;
    431   int src_count = 0;
    432   transitions *trans = s->transitions;
    433   reductions *reds = s->reductions;
    434 
    435   if (!trans)
    436     return 0;
    437 
    438   bitset_zero (lookahead_set);
    439   bitset_zero (shift_set);
    440 
    441   FOR_EACH_SHIFT (trans, i)
    442     bitset_set (shift_set, TRANSITION_SYMBOL (trans, i));
    443 
    444   for (i = 0; i < reds->num; ++i)
    445     bitset_or (lookahead_set, lookahead_set, reds->lookahead_tokens[i]);
    446 
    447   bitset_and (lookahead_set, lookahead_set, shift_set);
    448 
    449   src_count = bitset_count (lookahead_set);
    450 
    451   return src_count;
    452 }
    453 
    454 
    455 /*----------------------------------------------------------------.
    456 | Count the number of reduce/reduce conflicts.  If ONE_PER_TOKEN, |
    457 | count one conflict for each token that has any reduce/reduce    |
    458 | conflicts.  Otherwise, count one conflict for each pair of      |
    459 | conflicting reductions.                                         |
    460 +`----------------------------------------------------------------*/
    461 
    462 static int
    463 count_rr_conflicts (state *s, bool one_per_token)
    464 {
    465   int i;
    466   reductions *reds = s->reductions;
    467   int rrc_count = 0;
    468 
    469   for (i = 0; i < ntokens; i++)
    470     {
    471       int count = 0;
    472       int j;
    473       for (j = 0; j < reds->num; ++j)
    474 	if (bitset_test (reds->lookahead_tokens[j], i))
    475 	  count++;
    476 
    477       if (count >= 2)
    478 	rrc_count += one_per_token ? 1 : count-1;
    479     }
    480 
    481   return rrc_count;
    482 }
    483 
    484 
    485 /*--------------------------------------------------------.
    486 | Report the number of conflicts, using the Yacc format.  |
    487 `--------------------------------------------------------*/
    488 
    489 static void
    490 conflict_report (FILE *out, int src_num, int rrc_num)
    491 {
    492   if (src_num && rrc_num)
    493     fprintf (out, _("conflicts: %d shift/reduce, %d reduce/reduce\n"),
    494 	     src_num, rrc_num);
    495   else if (src_num)
    496     fprintf (out, _("conflicts: %d shift/reduce\n"), src_num);
    497   else if (rrc_num)
    498     fprintf (out, _("conflicts: %d reduce/reduce\n"), rrc_num);
    499 }
    500 
    501 
    502 /*-----------------------------------------------------------.
    503 | Output the detailed description of states with conflicts.  |
    504 `-----------------------------------------------------------*/
    505 
    506 void
    507 conflicts_output (FILE *out)
    508 {
    509   bool printed_sth = false;
    510   state_number i;
    511   for (i = 0; i < nstates; i++)
    512     {
    513       state *s = states[i];
    514       if (conflicts[i])
    515 	{
    516 	  fprintf (out, _("State %d "), i);
    517 	  conflict_report (out, count_sr_conflicts (s),
    518 			   count_rr_conflicts (s, true));
    519 	  printed_sth = true;
    520 	}
    521     }
    522   if (printed_sth)
    523     fputs ("\n\n", out);
    524 }
    525 
    526 /*--------------------------------------------------------.
    527 | Total the number of S/R and R/R conflicts.  Unlike the  |
    528 | code in conflicts_output, however, count EACH pair of   |
    529 | reductions for the same state and lookahead as one      |
    530 | conflict.						  |
    531 `--------------------------------------------------------*/
    532 
    533 int
    534 conflicts_total_count (void)
    535 {
    536   state_number i;
    537   int count;
    538 
    539   /* Conflicts by state.  */
    540   count = 0;
    541   for (i = 0; i < nstates; i++)
    542     if (conflicts[i])
    543       {
    544 	count += count_sr_conflicts (states[i]);
    545 	count += count_rr_conflicts (states[i], false);
    546       }
    547   return count;
    548 }
    549 
    550 
    551 /*------------------------------------------.
    552 | Reporting the total number of conflicts.  |
    553 `------------------------------------------*/
    554 
    555 void
    556 conflicts_print (void)
    557 {
    558   /* Is the number of SR conflicts OK?  Either EXPECTED_CONFLICTS is
    559      not set, and then we want 0 SR, or else it is specified, in which
    560      case we want equality.  */
    561   bool src_ok;
    562   bool rrc_ok;
    563 
    564   int src_total = 0;
    565   int rrc_total = 0;
    566   int src_expected;
    567   int rrc_expected;
    568 
    569   /* Conflicts by state.  */
    570   {
    571     state_number i;
    572 
    573     for (i = 0; i < nstates; i++)
    574       if (conflicts[i])
    575 	{
    576 	  src_total += count_sr_conflicts (states[i]);
    577 	  rrc_total += count_rr_conflicts (states[i], true);
    578 	}
    579   }
    580 
    581   if (! glr_parser && rrc_total > 0 && expected_rr_conflicts != -1)
    582     {
    583       warn (_("%%expect-rr applies only to GLR parsers"));
    584       expected_rr_conflicts = -1;
    585     }
    586 
    587   src_expected = expected_sr_conflicts == -1 ? 0 : expected_sr_conflicts;
    588   rrc_expected = expected_rr_conflicts == -1 ? 0 : expected_rr_conflicts;
    589   src_ok = src_total == src_expected;
    590   rrc_ok = rrc_total == rrc_expected;
    591 
    592   /* If there are as many RR conflicts and SR conflicts as
    593      expected, then there is nothing to report.  */
    594   if (rrc_ok & src_ok)
    595     return;
    596 
    597   /* Report the total number of conflicts on STDERR.  */
    598   if (expected_sr_conflicts == -1 && expected_rr_conflicts == -1)
    599     {
    600       if (!(warnings_flag & warnings_conflicts_sr))
    601         src_total = 0;
    602       if (!(warnings_flag & warnings_conflicts_rr))
    603         rrc_total = 0;
    604     }
    605   if (src_total | rrc_total)
    606     {
    607       if (expected_sr_conflicts == -1 && expected_rr_conflicts == -1)
    608         set_warning_issued ();
    609       if (! yacc_flag)
    610 	fprintf (stderr, "%s: ", current_file);
    611       conflict_report (stderr, src_total, rrc_total);
    612     }
    613 
    614   if (expected_sr_conflicts != -1 || expected_rr_conflicts != -1)
    615     {
    616       if (! src_ok)
    617 	complain (ngettext ("expected %d shift/reduce conflict",
    618 			    "expected %d shift/reduce conflicts",
    619 			    src_expected),
    620 		  src_expected);
    621       if (! rrc_ok)
    622 	complain (ngettext ("expected %d reduce/reduce conflict",
    623 			    "expected %d reduce/reduce conflicts",
    624 			    rrc_expected),
    625 		  rrc_expected);
    626     }
    627 }
    628 
    629 
    630 void
    631 conflicts_free (void)
    632 {
    633   free (conflicts);
    634   bitset_free (shift_set);
    635   bitset_free (lookahead_set);
    636   obstack_free (&solved_conflicts_obstack, NULL);
    637   obstack_free (&solved_conflicts_xml_obstack, NULL);
    638 }
    639