bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

04-conflict-resolution.patch


From: Akim Demaille
Subject: 04-conflict-resolution.patch
Date: Sun, 26 May 2002 22:24:18 +0200

Index: ChangeLog
from  Akim Demaille  <address@hidden>

        * src/state.h (state_t): `solved_conflicts' is a new member.
        * src/LR0.c (new_state): Set it to 0.
        * src/conflicts.h, src/conflicts.c (print_conflicts)
        (free_conflicts, solve_conflicts): Rename as...
        (conflicts_print, conflicts_free, conflicts_solve): these.
        Adjust callers.
        * src/conflicts.c (enum conflict_resolution_e)
        (solved_conflicts_obstack): New, used by...
        (log_resolution): this.
        Adjust to attach the conflict resolution to each state.
        Complete the description with the precedence/associativity
        information.
        (resolve_sr_conflict): Adjust.
        * src/print.c (print_state): Output its solved_conflicts.
        * tests/conflicts.at (Unresolved SR Conflicts)
        (Solved SR Conflicts): Exercise --report=all.

Index: NEWS
--- NEWS Sun, 26 May 2002 20:27:03 +0200 akim
+++ NEWS Sun, 26 May 2002 22:10:27 +0200 akim
@@ -73,7 +73,7 @@
 * Semantic parser
   This old option, which has been broken for ages, is removed.

-* New translations
+* New translation
   Croatian, thanks to Denis Lackovic.

 * Incorrect token definitions
@@ -87,9 +87,14 @@
 * Reports
   In addition to --verbose, bison supports --report=THINGS, which
   produces additional information:
-
-  `itemset'      complete the core item sets with their closure
-  `lookahead'    explicitly associate lookaheads to items
+  - itemset
+    complete the core item sets with their closure
+  - lookahead
+    explicitly associate lookaheads to items
+  - solved
+    describe shift/reduce conflicts solving.
+    Bison used to systematically output this information on top of
+    the report.  Solved conflicts are now attached to their states.

 
 Changes in version 1.35, 2002-03-25:
Index: TODO
--- TODO Sat, 25 May 2002 18:16:05 +0200 akim
+++ TODO Sun, 26 May 2002 22:17:23 +0200 akim
@@ -40,22 +40,6 @@
 Provide better assistance for understanding the conflicts by providing
 a sample text exhibiting the (LALR) ambiguity.

-* report
-Solved conflicts should not be reported in the beginning of the file.
-Rather they should be reported within each state description.  Also,
-now that the symbol providing the precedence of a rule is kept, it is
-possible to explain why a conflict was solved this way.  E.g., instead
-of
-
-   Conflict in state 8 between rule 2 and token '+' resolved as reduce.
-
-we can (in state 8) report something like
-
-   Conflict between rule 2 and token '+' resolved as reduce
-   because '*' < '+'.
-
-or something like that.
-
 * Coding system independence
 Paul notes:

Index: src/LR0.c
--- src/LR0.c Sun, 26 May 2002 20:36:39 +0200 akim
+++ src/LR0.c Sun, 26 May 2002 21:27:01 +0200 akim
@@ -200,8 +200,9 @@
   p = STATE_ALLOC (core_size);
   p->accessing_symbol = symbol;
   p->number = nstates;
-  p->nitems = core_size;
+  p->solved_conflicts = NULL;

+  p->nitems = core_size;
   memcpy (p->items, core, core_size * sizeof (core[0]));

   /* If this is the eoftoken, and this is not the initial state, then
Index: src/conflicts.c
--- src/conflicts.c Sat, 25 May 2002 18:16:05 +0200 akim
+++ src/conflicts.c Sun, 26 May 2002 22:17:39 +0200 akim
@@ -35,22 +35,93 @@
 /* -1 stands for not specified. */
 int expected_conflicts = -1;
 static char *conflicts = NULL;
+struct obstack solved_conflicts_obstack;

 static bitset shiftset;
 static bitset lookaheadset;
+
 

+enum conflict_resolution_e
+  {
+    shift_resolution,
+    reduce_resolution,
+    left_resolution,
+    right_resolution,
+    nonassoc_resolution,
+  };
+
+
 static inline void
-log_resolution (state_t *state, int LAno, int token, const char *resolution)
+log_resolution (state_t *state, int lookahead, int token,
+               enum conflict_resolution_e resolution)
 {
-  if (report_flag & report_states)
-    obstack_fgrow4 (&output_obstack,
-                   _("\
-Conflict in state %d between rule %d and token %s resolved as %s.\n"),
-                   state->number,
-                   LArule[LAno]->number,
-                   symbols[token]->tag,
-                   resolution);
+  if (report_flag & report_solved_conflicts)
+    {
+      /* The description of the resolution. */
+      switch (resolution)
+       {
+       case shift_resolution:
+       case left_resolution:
+         obstack_fgrow2 (&solved_conflicts_obstack,
+                         _("\
+    Conflict between rule %d and token %s resolved as shift"),
+                         LArule[lookahead]->number,
+                         symbols[token]->tag);
+         break;
+       case reduce_resolution:
+       case right_resolution:
+         obstack_fgrow2 (&solved_conflicts_obstack,
+                         _("\
+    Conflict between rule %d and token %s resolved as reduce"),
+                         LArule[lookahead]->number,
+                         symbols[token]->tag);
+         break;
+       case nonassoc_resolution:
+         obstack_fgrow2 (&solved_conflicts_obstack,
+                         _("\
+    Conflict between rule %d and token %s resolved as an error"),
+                         LArule[lookahead]->number,
+                         symbols[token]->tag);
+         break;
+       }
+
+      /* The reason. */
+      switch (resolution)
+       {
+       case shift_resolution:
+         obstack_fgrow2 (&solved_conflicts_obstack,
+                         " (%s < %s)",
+                         LArule[lookahead]->prec->tag,
+                         symbols[token]->tag);
+         break;
+
+       case reduce_resolution:
+         obstack_fgrow2 (&solved_conflicts_obstack,
+                         " (%s < %s)",
+                         symbols[token]->tag,
+                         LArule[lookahead]->prec->tag);
+         break;
+
+       case left_resolution:
+         obstack_printf (&solved_conflicts_obstack,
+                         " (%%left %s)",
+                         symbols[token]->tag);
+         break;
+
+       case right_resolution:
+         obstack_fgrow1 (&solved_conflicts_obstack,
+                         " (%%right %s)",
+                         symbols[token]->tag);
+         break;
+       case nonassoc_resolution:
+         obstack_fgrow1 (&solved_conflicts_obstack,
+                         " (%%nonassoc %s)",
+                         symbols[token]->tag);
+         break;
+       }
+      obstack_sgrow (&solved_conflicts_obstack, ".\n");
+    }
 }


@@ -112,12 +183,12 @@
           The precedence of shifting is that of token i.  */
        if (symbols[i]->prec < redprec)
          {
-           log_resolution (state, lookahead, i, _("reduce"));
+           log_resolution (state, lookahead, i, reduce_resolution);
            flush_shift (state, i);
          }
        else if (symbols[i]->prec > redprec)
          {
-           log_resolution (state, lookahead, i, _("shift"));
+           log_resolution (state, lookahead, i, shift_resolution);
            flush_reduce (lookahead, i);
          }
        else
@@ -129,17 +200,17 @@
          switch (symbols[i]->assoc)
            {
            case right_assoc:
-             log_resolution (state, lookahead, i, _("shift"));
+             log_resolution (state, lookahead, i, right_resolution);
              flush_reduce (lookahead, i);
              break;

            case left_assoc:
-             log_resolution (state, lookahead, i, _("reduce"));
+             log_resolution (state, lookahead, i, left_resolution);
              flush_shift (state, i);
              break;

            case non_assoc:
-             log_resolution (state, lookahead, i, _("an error"));
+             log_resolution (state, lookahead, i, nonassoc_resolution);
              flush_shift (state, i);
              flush_reduce (lookahead, i);
              /* Record an explicit error for this token.  */
@@ -152,6 +223,12 @@
      permanent errs structure for this state, to record them.  */
   state->errs = errs_dup (errp);
   free (errp);
+
+  if (obstack_object_size (&solved_conflicts_obstack))
+    {
+      obstack_1grow (&solved_conflicts_obstack, '\0');
+      state->solved_conflicts = obstack_finish (&solved_conflicts_obstack);
+    }
 }


@@ -195,13 +272,14 @@
 }

 void
-solve_conflicts (void)
+conflicts_solve (void)
 {
   size_t i;

   conflicts = XCALLOC (char, nstates);
   shiftset = bitset_create (ntokens, BITSET_FIXED);
   lookaheadset = bitset_create (ntokens, BITSET_FIXED);
+  obstack_init (&solved_conflicts_obstack);

   for (i = 0; i < nstates; i++)
     set_conflicts (states[i]);
@@ -393,9 +471,10 @@


 void
-free_conflicts (void)
+conflicts_free (void)
 {
   XFREE (conflicts);
   bitset_free (shiftset);
   bitset_free (lookaheadset);
+  obstack_free (&solved_conflicts_obstack, NULL);
 }
Index: src/conflicts.h
--- src/conflicts.h Wed, 26 Dec 2001 23:00:34 +0100 akim
+++ src/conflicts.h Sun, 26 May 2002 22:11:10 +0200 akim
@@ -1,5 +1,5 @@
 /* Find and resolve or report look-ahead conflicts for bison,
-   Copyright 2000, 2001 Free Software Foundation, Inc.
+   Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.

    This file is part of Bison, the GNU Compiler Compiler.

@@ -22,10 +22,10 @@
 # define CONFLICTS_H_
 # include "state.h"

-void solve_conflicts PARAMS ((void));
+void conflicts_solve PARAMS ((void));
 void conflicts_print PARAMS ((void));
 void conflicts_output PARAMS ((FILE *out));
-void free_conflicts PARAMS ((void));
+void conflicts_free PARAMS ((void));

 /* Were there conflicts? */
 extern int expected_conflicts;
Index: src/getargs.c
--- src/getargs.c Sat, 25 May 2002 19:19:52 +0200 akim
+++ src/getargs.c Sun, 26 May 2002 21:22:26 +0200 akim
@@ -55,6 +55,7 @@
   "state", "states",
   "itemset", "itemsets",
   "lookahead", "lookaheads",
+  "solved",
   "all",
   0
 };
@@ -65,6 +66,7 @@
   report_states, report_states,
   report_states | report_itemsets, report_states | report_itemsets,
   report_states | report_lookaheads, report_states | report_lookaheads,
+  report_states | report_solved_conflicts,
   report_all
 };

@@ -140,6 +142,7 @@ Output:\n\
   `state'        describe the states\n\
   `itemset'      complete the core item sets with their closure\n\
   `lookahead'    explicitly associate lookaheads to items\n\
+  `solved'       describe shift/reduce conflicts solving\n\
   `all'          include all the above information\n\
   `none'         disable the report\n\
 "), stream);
Index: src/getargs.h
--- src/getargs.h Sat, 25 May 2002 18:16:05 +0200 akim
+++ src/getargs.h Sun, 26 May 2002 21:21:06 +0200 akim
@@ -43,6 +43,7 @@
     report_states = 1 << 0,
     report_itemsets = 1 << 1,
     report_lookaheads = 1 << 2,
+    report_solved_conflicts = 1 << 3,
     report_all = ~0
   };

Index: src/main.c
--- src/main.c Sat, 25 May 2002 18:16:05 +0200 akim
+++ src/main.c Sun, 26 May 2002 22:11:21 +0200 akim
@@ -84,7 +84,7 @@
      lookahead is not enough to disambiguate the parsing.  In file
      conflicts.  Also resolve s/r conflicts based on precedence
      declarations.  */
-  solve_conflicts ();
+  conflicts_solve ();
   conflicts_print ();

   /* Output file names. */
@@ -107,7 +107,7 @@
   output ();

   reduce_free ();
-  free_conflicts ();
+  conflicts_free ();
   free_nullable ();
   free_derives ();
   grammar_free ();
Index: src/print.c
--- src/print.c Sun, 26 May 2002 20:36:39 +0200 akim
+++ src/print.c Sun, 26 May 2002 21:19:58 +0200 akim
@@ -364,6 +364,9 @@
   fputs ("\n\n", out);
   print_core (out, state);
   print_actions (out, state);
+  if ((report_flag & report_solved_conflicts)
+      && state->solved_conflicts)
+    fputs (state->solved_conflicts, out);
   fputs ("\n\n", out);
 }
 
Index: src/state.h
--- src/state.h Sun, 26 May 2002 20:36:39 +0200 akim
+++ src/state.h Sun, 26 May 2002 21:26:38 +0200 akim
@@ -184,7 +184,12 @@
   short lookaheadsp;
   int nlookaheads;

-  /* Its items. */
+  /* If some conflicts were solved thanks to precedence/associativity,
+     a human readable description of the resolution.  */
+  const char *solved_conflicts;
+
+  /* Its items.  Must be last, since ITEMS can be arbitrarily large.
+     */
   unsigned short nitems;
   item_number_t items[1];
 } state_t;
Index: tests/conflicts.at
--- tests/conflicts.at Mon, 06 May 2002 19:46:46 +0200 akim
+++ tests/conflicts.at Sun, 26 May 2002 22:23:03 +0200 akim
@@ -136,7 +136,7 @@ expr: expr '<' expr
 exp: exp OP exp | NUM;
 ]])

-AT_CHECK([bison input.y -o input.c -v], 0, [],
+AT_CHECK([bison input.y -o input.c --report=all], 0, [],
 [input.y contains 1 shift/reduce conflict.
 ])

@@ -172,6 +172,8 @@ exp: exp OP exp | NUM;
 state 0

     $axiom  ->  . exp $   (rule 0)
+    exp  ->  . exp OP exp   (rule 1)
+    exp  ->  . NUM   (rule 2)

     NUM        shift, and go to state 1

@@ -206,7 +208,9 @@ exp: exp OP exp | NUM;

 state 4

+    exp  ->  . exp OP exp   (rule 1)
     exp  ->  exp OP . exp   (rule 1)
+    exp  ->  . NUM   (rule 2)

     NUM        shift, and go to state 1

@@ -216,8 +220,8 @@ exp: exp OP exp | NUM;

 state 5

-    exp  ->  exp . OP exp   (rule 1)
-    exp  ->  exp OP exp .   (rule 1)
+    exp  ->  exp . OP exp  [$, OP]   (rule 1)
+    exp  ->  exp OP exp .  [$, OP]   (rule 1)

     OP         shift, and go to state 4

@@ -244,14 +248,11 @@ exp: exp OP exp | NUM;
 exp: exp OP exp | NUM;
 ]])

-AT_CHECK([bison input.y -o input.c -v], 0, [], [])
+AT_CHECK([bison input.y -o input.c --report=all], 0, [], [])

 # Check the contents of the report.
 AT_CHECK([cat input.output], [],
-[[Conflict in state 5 between rule 2 and token OP resolved as shift.
-
-
-Grammar
+[[Grammar

   Number, Line, Rule
     0   4 $axiom -> exp $
@@ -278,6 +279,8 @@ exp: exp OP exp | NUM;
 state 0

     $axiom  ->  . exp $   (rule 0)
+    exp  ->  . exp OP exp   (rule 1)
+    exp  ->  . NUM   (rule 2)

     NUM        shift, and go to state 1

@@ -312,7 +315,9 @@ exp: exp OP exp | NUM;

 state 4

+    exp  ->  . exp OP exp   (rule 1)
     exp  ->  exp OP . exp   (rule 1)
+    exp  ->  . NUM   (rule 2)

     NUM        shift, and go to state 1

@@ -322,13 +327,14 @@ exp: exp OP exp | NUM;

 state 5

-    exp  ->  exp . OP exp   (rule 1)
-    exp  ->  exp OP exp .   (rule 1)
+    exp  ->  exp . OP exp  [$]   (rule 1)
+    exp  ->  exp OP exp .  [$]   (rule 1)

     OP         shift, and go to state 4

     $default   reduce using rule 1 (exp)

+    Conflict between rule 2 and token OP resolved as reduce (%right OP).


 ]])



reply via email to

[Prev in Thread] Current Thread [Next in Thread]