]> git.saurik.com Git - bison.git/blobdiff - src/conflicts.c
Merge branch 'maint'
[bison.git] / src / conflicts.c
index 18c659da72381a8a65dc2fae10c87367ecf43d83..f8ed17727eb36bda9ce9bec2e54388a51ddb5914 100644 (file)
@@ -73,7 +73,7 @@ log_resolution (rule *r, symbol_number token,
         {
         case shift_resolution:
         case right_resolution:
         {
         case shift_resolution:
         case right_resolution:
-          obstack_fgrow2 (&solved_conflicts_obstack,
+          obstack_printf (&solved_conflicts_obstack,
                           _("    Conflict between rule %d and token %s"
                             " resolved as shift"),
                           r->number,
                           _("    Conflict between rule %d and token %s"
                             " resolved as shift"),
                           r->number,
@@ -82,7 +82,7 @@ log_resolution (rule *r, symbol_number token,
 
         case reduce_resolution:
         case left_resolution:
 
         case reduce_resolution:
         case left_resolution:
-          obstack_fgrow2 (&solved_conflicts_obstack,
+          obstack_printf (&solved_conflicts_obstack,
                           _("    Conflict between rule %d and token %s"
                             " resolved as reduce"),
                           r->number,
                           _("    Conflict between rule %d and token %s"
                             " resolved as reduce"),
                           r->number,
@@ -90,7 +90,7 @@ log_resolution (rule *r, symbol_number token,
           break;
 
         case nonassoc_resolution:
           break;
 
         case nonassoc_resolution:
-          obstack_fgrow2 (&solved_conflicts_obstack,
+          obstack_printf (&solved_conflicts_obstack,
                           _("    Conflict between rule %d and token %s"
                             " resolved as an error"),
                           r->number,
                           _("    Conflict between rule %d and token %s"
                             " resolved as an error"),
                           r->number,
@@ -102,33 +102,33 @@ log_resolution (rule *r, symbol_number token,
       switch (resolution)
         {
         case shift_resolution:
       switch (resolution)
         {
         case shift_resolution:
-          obstack_fgrow2 (&solved_conflicts_obstack,
+          obstack_printf (&solved_conflicts_obstack,
                           " (%s < %s)",
                           r->prec->tag,
                           symbols[token]->tag);
           break;
 
         case reduce_resolution:
                           " (%s < %s)",
                           r->prec->tag,
                           symbols[token]->tag);
           break;
 
         case reduce_resolution:
-          obstack_fgrow2 (&solved_conflicts_obstack,
+          obstack_printf (&solved_conflicts_obstack,
                           " (%s < %s)",
                           symbols[token]->tag,
                           r->prec->tag);
           break;
 
         case left_resolution:
                           " (%s < %s)",
                           symbols[token]->tag,
                           r->prec->tag);
           break;
 
         case left_resolution:
-          obstack_fgrow1 (&solved_conflicts_obstack,
+          obstack_printf (&solved_conflicts_obstack,
                           " (%%left %s)",
                           symbols[token]->tag);
           break;
 
         case right_resolution:
                           " (%%left %s)",
                           symbols[token]->tag);
           break;
 
         case right_resolution:
-          obstack_fgrow1 (&solved_conflicts_obstack,
+          obstack_printf (&solved_conflicts_obstack,
                           " (%%right %s)",
                           symbols[token]->tag);
           break;
 
         case nonassoc_resolution:
                           " (%%right %s)",
                           symbols[token]->tag);
           break;
 
         case nonassoc_resolution:
-          obstack_fgrow1 (&solved_conflicts_obstack,
+          obstack_printf (&solved_conflicts_obstack,
                           " (%%nonassoc %s)",
                           symbols[token]->tag);
           break;
                           " (%%nonassoc %s)",
                           symbols[token]->tag);
           break;
@@ -145,7 +145,7 @@ log_resolution (rule *r, symbol_number token,
         {
         case shift_resolution:
         case right_resolution:
         {
         case shift_resolution:
         case right_resolution:
-          obstack_fgrow2 (&solved_conflicts_xml_obstack,
+          obstack_printf (&solved_conflicts_xml_obstack,
                           "        <resolution rule=\"%d\" symbol=\"%s\""
                           " type=\"shift\">",
                           r->number,
                           "        <resolution rule=\"%d\" symbol=\"%s\""
                           " type=\"shift\">",
                           r->number,
@@ -154,7 +154,7 @@ log_resolution (rule *r, symbol_number token,
 
         case reduce_resolution:
         case left_resolution:
 
         case reduce_resolution:
         case left_resolution:
-          obstack_fgrow2 (&solved_conflicts_xml_obstack,
+          obstack_printf (&solved_conflicts_xml_obstack,
                           "        <resolution rule=\"%d\" symbol=\"%s\""
                           " type=\"reduce\">",
                           r->number,
                           "        <resolution rule=\"%d\" symbol=\"%s\""
                           " type=\"reduce\">",
                           r->number,
@@ -162,7 +162,7 @@ log_resolution (rule *r, symbol_number token,
           break;
 
         case nonassoc_resolution:
           break;
 
         case nonassoc_resolution:
-          obstack_fgrow2 (&solved_conflicts_xml_obstack,
+          obstack_printf (&solved_conflicts_xml_obstack,
                           "        <resolution rule=\"%d\" symbol=\"%s\""
                           " type=\"error\">",
                           r->number,
                           "        <resolution rule=\"%d\" symbol=\"%s\""
                           " type=\"error\">",
                           r->number,
@@ -174,33 +174,33 @@ log_resolution (rule *r, symbol_number token,
       switch (resolution)
         {
         case shift_resolution:
       switch (resolution)
         {
         case shift_resolution:
-          obstack_fgrow2 (&solved_conflicts_xml_obstack,
+          obstack_printf (&solved_conflicts_xml_obstack,
                           "%s &lt; %s",
                           xml_escape_n (0, r->prec->tag),
                           xml_escape_n (1, symbols[token]->tag));
           break;
 
         case reduce_resolution:
                           "%s &lt; %s",
                           xml_escape_n (0, r->prec->tag),
                           xml_escape_n (1, symbols[token]->tag));
           break;
 
         case reduce_resolution:
-          obstack_fgrow2 (&solved_conflicts_xml_obstack,
+          obstack_printf (&solved_conflicts_xml_obstack,
                           "%s &lt; %s",
                           xml_escape_n (0, symbols[token]->tag),
                           xml_escape_n (1, r->prec->tag));
           break;
 
         case left_resolution:
                           "%s &lt; %s",
                           xml_escape_n (0, symbols[token]->tag),
                           xml_escape_n (1, r->prec->tag));
           break;
 
         case left_resolution:
-          obstack_fgrow1 (&solved_conflicts_xml_obstack,
+          obstack_printf (&solved_conflicts_xml_obstack,
                           "%%left %s",
                           xml_escape (symbols[token]->tag));
           break;
 
         case right_resolution:
                           "%%left %s",
                           xml_escape (symbols[token]->tag));
           break;
 
         case right_resolution:
-          obstack_fgrow1 (&solved_conflicts_xml_obstack,
+          obstack_printf (&solved_conflicts_xml_obstack,
                           "%%right %s",
                           xml_escape (symbols[token]->tag));
           break;
 
         case nonassoc_resolution:
                           "%%right %s",
                           xml_escape (symbols[token]->tag));
           break;
 
         case nonassoc_resolution:
-          obstack_fgrow1 (&solved_conflicts_xml_obstack,
+          obstack_printf (&solved_conflicts_xml_obstack,
                           "%%nonassoc %s",
                           xml_escape (symbols[token]->tag));
       break;
                           "%%nonassoc %s",
                           xml_escape (symbols[token]->tag));
       break;
@@ -360,15 +360,9 @@ set_conflicts (state *s, symbol **errors)
       state_errs_set (s, nerrs, errors);
     }
   if (obstack_object_size (&solved_conflicts_obstack))
       state_errs_set (s, nerrs, errors);
     }
   if (obstack_object_size (&solved_conflicts_obstack))
-    {
-      obstack_1grow (&solved_conflicts_obstack, '\0');
-      s->solved_conflicts = obstack_finish (&solved_conflicts_obstack);
-    }
+    s->solved_conflicts = obstack_finish0 (&solved_conflicts_obstack);
   if (obstack_object_size (&solved_conflicts_xml_obstack))
   if (obstack_object_size (&solved_conflicts_xml_obstack))
-    {
-      obstack_1grow (&solved_conflicts_xml_obstack, '\0');
-      s->solved_conflicts_xml = obstack_finish (&solved_conflicts_xml_obstack);
-    }
+    s->solved_conflicts_xml = obstack_finish0 (&solved_conflicts_xml_obstack);
 
   /* Loop over all rules which require lookahead in this state.  Check
      for conflicts not resolved above.  */
 
   /* Loop over all rules which require lookahead in this state.  Check
      for conflicts not resolved above.  */
@@ -428,11 +422,10 @@ conflicts_update_state_numbers (state_number old_to_new[],
 | Count the number of shift/reduce conflicts.  |
 `---------------------------------------------*/
 
 | Count the number of shift/reduce conflicts.  |
 `---------------------------------------------*/
 
-static int
-count_sr_conflicts (state *s)
+static size_t
+count_state_sr_conflicts (state *s)
 {
   int i;
 {
   int i;
-  int src_count = 0;
   transitions *trans = s->transitions;
   reductions *reds = s->reductions;
 
   transitions *trans = s->transitions;
   reductions *reds = s->reductions;
 
@@ -450,56 +443,66 @@ count_sr_conflicts (state *s)
 
   bitset_and (lookahead_set, lookahead_set, shift_set);
 
 
   bitset_and (lookahead_set, lookahead_set, shift_set);
 
-  src_count = bitset_count (lookahead_set);
+  return bitset_count (lookahead_set);
+}
+
+/*---------------------------------------------.
+| The total number of shift/reduce conflicts.  |
+`---------------------------------------------*/
+
+static size_t
+count_sr_conflicts (void)
+{
+  size_t res = 0;
+  state_number i;
 
 
-  return src_count;
+  /* Conflicts by state.  */
+  for (i = 0; i < nstates; i++)
+    if (conflicts[i])
+      res += count_state_sr_conflicts (states[i]);
+  return res;
 }
 
 
 }
 
 
+
 /*----------------------------------------------------------------.
 | Count the number of reduce/reduce conflicts.  If ONE_PER_TOKEN, |
 | count one conflict for each token that has any reduce/reduce    |
 | conflicts.  Otherwise, count one conflict for each pair of      |
 | conflicting reductions.                                         |
 /*----------------------------------------------------------------.
 | Count the number of reduce/reduce conflicts.  If ONE_PER_TOKEN, |
 | count one conflict for each token that has any reduce/reduce    |
 | conflicts.  Otherwise, count one conflict for each pair of      |
 | conflicting reductions.                                         |
-+`----------------------------------------------------------------*/
+`----------------------------------------------------------------*/
 
 
-static int
-count_rr_conflicts (state *s, bool one_per_token)
+static size_t
+count_state_rr_conflicts (state *s, bool one_per_token)
 {
   int i;
   reductions *reds = s->reductions;
 {
   int i;
   reductions *reds = s->reductions;
-  int rrc_count = 0;
+  size_t res = 0;
 
   for (i = 0; i < ntokens; i++)
     {
       int count = 0;
       int j;
       for (j = 0; j < reds->num; ++j)
 
   for (i = 0; i < ntokens; i++)
     {
       int count = 0;
       int j;
       for (j = 0; j < reds->num; ++j)
-        if (bitset_test (reds->lookahead_tokens[j], i))
-          count++;
-
+        count += bitset_test (reds->lookahead_tokens[j], i);
       if (count >= 2)
       if (count >= 2)
-        rrc_count += one_per_token ? 1 : count-1;
+        res += one_per_token ? 1 : count-1;
     }
 
     }
 
-  return rrc_count;
+  return res;
 }
 
 }
 
-
-/*--------------------------------------------------------.
-| Report the number of conflicts, using the Yacc format.  |
-`--------------------------------------------------------*/
-
-static void
-conflict_report (FILE *out, int src_num, int rrc_num)
+static size_t
+count_rr_conflicts (bool one_per_token)
 {
 {
-  if (src_num && rrc_num)
-    fprintf (out, _("conflicts: %d shift/reduce, %d reduce/reduce\n"),
-             src_num, rrc_num);
-  else if (src_num)
-    fprintf (out, _("conflicts: %d shift/reduce\n"), src_num);
-  else if (rrc_num)
-    fprintf (out, _("conflicts: %d reduce/reduce\n"), rrc_num);
+  size_t res = 0;
+  state_number i;
+
+  /* Conflicts by state.  */
+  for (i = 0; i < nstates; i++)
+    if (conflicts[i])
+      res += count_state_rr_conflicts (states[i], one_per_token);
+  return res;
 }
 
 
 }
 
 
@@ -517,9 +520,17 @@ conflicts_output (FILE *out)
       state *s = states[i];
       if (conflicts[i])
         {
       state *s = states[i];
       if (conflicts[i])
         {
+          int src = count_state_sr_conflicts (s);
+          int rrc = count_state_rr_conflicts (s, true);
           fprintf (out, _("State %d "), i);
           fprintf (out, _("State %d "), i);
-          conflict_report (out, count_sr_conflicts (s),
-                           count_rr_conflicts (s, true));
+          if (src && rrc)
+            fprintf (out,
+                     _("conflicts: %d shift/reduce, %d reduce/reduce\n"),
+                     src, rrc);
+          else if (src)
+            fprintf (out, _("conflicts: %d shift/reduce\n"), src);
+          else if (rrc)
+            fprintf (out, _("conflicts: %d reduce/reduce\n"), rrc);
           printed_sth = true;
         }
     }
           printed_sth = true;
         }
     }
@@ -537,18 +548,7 @@ conflicts_output (FILE *out)
 int
 conflicts_total_count (void)
 {
 int
 conflicts_total_count (void)
 {
-  state_number i;
-  int count;
-
-  /* Conflicts by state.  */
-  count = 0;
-  for (i = 0; i < nstates; i++)
-    if (conflicts[i])
-      {
-        count += count_sr_conflicts (states[i]);
-        count += count_rr_conflicts (states[i], false);
-      }
-  return count;
+  return count_sr_conflicts () + count_rr_conflicts (false);
 }
 
 
 }
 
 
@@ -559,75 +559,57 @@ conflicts_total_count (void)
 void
 conflicts_print (void)
 {
 void
 conflicts_print (void)
 {
-  /* Is the number of SR conflicts OK?  Either EXPECTED_CONFLICTS is
-     not set, and then we want 0 SR, or else it is specified, in which
-     case we want equality.  */
-  bool src_ok;
-  bool rrc_ok;
-
-  int src_total = 0;
-  int rrc_total = 0;
-  int src_expected;
-  int rrc_expected;
-
-  /* Conflicts by state.  */
-  {
-    state_number i;
-
-    for (i = 0; i < nstates; i++)
-      if (conflicts[i])
-        {
-          src_total += count_sr_conflicts (states[i]);
-          rrc_total += count_rr_conflicts (states[i], true);
-        }
-  }
-
-  if (! glr_parser && rrc_total > 0 && expected_rr_conflicts != -1)
+  if (! glr_parser && expected_rr_conflicts != -1)
     {
     {
-      warn (_("%%expect-rr applies only to GLR parsers"));
+      complain (NULL, Wother, _("%%expect-rr applies only to GLR parsers"));
       expected_rr_conflicts = -1;
     }
 
       expected_rr_conflicts = -1;
     }
 
-  src_expected = expected_sr_conflicts == -1 ? 0 : expected_sr_conflicts;
-  rrc_expected = expected_rr_conflicts == -1 ? 0 : expected_rr_conflicts;
-  src_ok = src_total == src_expected;
-  rrc_ok = rrc_total == rrc_expected;
-
-  /* If there are as many RR conflicts and SR conflicts as
-     expected, then there is nothing to report.  */
-  if (rrc_ok & src_ok)
-    return;
-
-  /* Report the total number of conflicts on STDERR.  */
-  if (expected_sr_conflicts == -1 && expected_rr_conflicts == -1)
-    {
-      if (!(warnings_flag & warnings_conflicts_sr))
-        src_total = 0;
-      if (!(warnings_flag & warnings_conflicts_rr))
-        rrc_total = 0;
-    }
-  if (src_total | rrc_total)
-    {
-      if (expected_sr_conflicts == -1 && expected_rr_conflicts == -1)
-        set_warning_issued ();
-      if (! yacc_flag)
-        fprintf (stderr, "%s: ", current_file);
-      conflict_report (stderr, src_total, rrc_total);
-    }
+  /* Screams for factoring, but almost useless because of the
+     different strings to translate.  */
+  {
+    int total = count_sr_conflicts ();
+    // If %expect is not used, but %expect-rr is, then expect 0 sr.
+    int expected =
+      (expected_sr_conflicts == -1 && expected_rr_conflicts != -1)
+      ? 0
+      : expected_sr_conflicts;
+    if (expected != -1)
+      {
+        if (expected != total)
+          complain (NULL, complaint,
+                    _("shift/reduce conflicts: %d found, %d expected"),
+                    total, expected);
+      }
+    else if (total)
+      complain (NULL, Wconflicts_sr,
+                ngettext ("%d shift/reduce conflict",
+                          "%d shift/reduce conflicts",
+                          total),
+                total);
+  }
 
 
-  if (expected_sr_conflicts != -1 || expected_rr_conflicts != -1)
-    {
-      if (! src_ok)
-        complain (ngettext ("expected %d shift/reduce conflict",
-                            "expected %d shift/reduce conflicts",
-                            src_expected),
-                  src_expected);
-      if (! rrc_ok)
-        complain (ngettext ("expected %d reduce/reduce conflict",
-                            "expected %d reduce/reduce conflicts",
-                            rrc_expected),
-                  rrc_expected);
-    }
+  {
+    int total = count_rr_conflicts (true);
+    // If %expect-rr is not used, but %expect is, then expect 0 rr.
+    int expected =
+      (expected_rr_conflicts == -1 && expected_sr_conflicts != -1)
+      ? 0
+      : expected_rr_conflicts;
+    if (expected != -1)
+      {
+        if (expected != total)
+          complain (NULL, complaint,
+                    _("reduce/reduce conflicts: %d found, %d expected"),
+                    total, expected);
+      }
+    else if (total)
+      complain (NULL, Wconflicts_rr,
+                ngettext ("%d reduce/reduce conflict",
+                          "%d reduce/reduce conflicts",
+                          total),
+                total);
+  }
 }
 
 
 }