]>
git.saurik.com Git - bison.git/blob - src/conflicts.c
   1 /* Find and resolve or report look-ahead conflicts for bison, 
   2    Copyright (C) 1984, 1989, 1992, 2000, 2001, 2002 
   3    Free Software Foundation, Inc. 
   5    This file is part of Bison, the GNU Compiler Compiler. 
   7    Bison is free software; you can redistribute it and/or modify it 
   8    under the terms of the GNU General Public License as published by 
   9    the Free Software Foundation; either version 2, or (at your option) 
  12    Bison is distributed in the hope that it will be useful, but 
  13    WITHOUT ANY WARRANTY; without even the implied warranty of 
  14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
  15    General Public License for more details. 
  17    You should have received a copy of the GNU General Public License 
  18    along with Bison; see the file COPYING.  If not, write to the Free 
  19    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
  31 #include "conflicts.h" 
  35 /* -1 stands for not specified. */ 
  36 int expected_conflicts 
= -1; 
  37 static char *conflicts 
= NULL
; 
  39 static bitset shiftset
; 
  40 static bitset lookaheadset
; 
  44 log_resolution (state_t 
*state
, int LAno
, int token
, const char *resolution
) 
  47     obstack_fgrow4 (&output_obstack
, 
  49 Conflict in state %d between rule %d and token %s resolved as %s.\n"), 
  57 /*------------------------------------------------------------------. 
  58 | Turn off the shift recorded for the specified token in the        | 
  59 | specified state.  Used when we resolve a shift-reduce conflict in | 
  60 | favor of the reduction.                                           | 
  61 `------------------------------------------------------------------*/ 
  64 flush_shift (state_t 
*state
, int token
) 
  66   shifts 
*shiftp 
= state
->shifts
; 
  69   bitset_reset (lookaheadset
, token
); 
  70   for (i 
= 0; i 
< shiftp
->nshifts
; i
++) 
  71     if (!SHIFT_IS_DISABLED (shiftp
, i
) && SHIFT_SYMBOL (shiftp
, i
) == token
) 
  72       SHIFT_DISABLE (shiftp
, i
); 
  76 /*-------------------------------------------------------------------. 
  77 | Turn off the reduce recorded for the specified token for the       | 
  78 | specified lookahead.  Used when we resolve a shift-reduce conflict | 
  79 | in favor of the shift.                                             | 
  80 `-------------------------------------------------------------------*/ 
  83 flush_reduce (int lookahead
, int token
) 
  85   bitset_reset (LA
[lookahead
], token
); 
  89 /*------------------------------------------------------------------. 
  90 | Attempt to resolve shift-reduce conflict for one rule by means of | 
  91 | precedence declarations.  It has already been checked that the    | 
  92 | rule has a precedence.  A conflict is resolved by modifying the   | 
  93 | shift or reduce tables so that there is no longer a conflict.     | 
  94 `------------------------------------------------------------------*/ 
  97 resolve_sr_conflict (state_t 
*state
, int lookahead
) 
 100   /* find the rule to reduce by to get precedence of reduction  */ 
 101   int redprec 
= LArule
[lookahead
]->prec
->prec
; 
 102   errs 
*errp 
= errs_new (ntokens 
+ 1); 
 105   for (i 
= 0; i 
< ntokens
; i
++) 
 106     if (bitset_test (LA
[lookahead
], i
) 
 107         && bitset_test (lookaheadset
, i
) 
 110         /* Shift-reduce conflict occurs for token number i 
 111            and it has a precedence. 
 112            The precedence of shifting is that of token i.  */ 
 113         if (symbols
[i
]->prec 
< redprec
) 
 115             log_resolution (state
, lookahead
, i
, _("reduce")); 
 116             flush_shift (state
, i
); 
 118         else if (symbols
[i
]->prec 
> redprec
) 
 120             log_resolution (state
, lookahead
, i
, _("shift")); 
 121             flush_reduce (lookahead
, i
); 
 124           /* Matching precedence levels. 
 125              For left association, keep only the reduction. 
 126              For right association, keep only the shift. 
 127              For nonassociation, keep neither.  */ 
 129           switch (symbols
[i
]->assoc
) 
 132               log_resolution (state
, lookahead
, i
, _("shift")); 
 133               flush_reduce (lookahead
, i
); 
 137               log_resolution (state
, lookahead
, i
, _("reduce")); 
 138               flush_shift (state
, i
); 
 142               log_resolution (state
, lookahead
, i
, _("an error")); 
 143               flush_shift (state
, i
); 
 144               flush_reduce (lookahead
, i
); 
 145               /* Record an explicit error for this token.  */ 
 146               errp
->errs
[errp
->nerrs
++] = i
; 
 151   /* Some tokens have been explicitly made errors.  Allocate a 
 152      permanent errs structure for this state, to record them.  */ 
 153   state
->errs 
= errs_dup (errp
); 
 159 set_conflicts (state_t 
*state
) 
 164   if (state
->consistent
) 
 167   bitset_zero (lookaheadset
); 
 169   shiftp 
= state
->shifts
; 
 170   for (i 
= 0; i 
< shiftp
->nshifts 
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++) 
 171     if (!SHIFT_IS_DISABLED (shiftp
, i
)) 
 172       bitset_set (lookaheadset
, SHIFT_SYMBOL (shiftp
, i
)); 
 174   /* Loop over all rules which require lookahead in this state.  First 
 175      check for shift-reduce conflict, and try to resolve using 
 177   for (i 
= 0; i 
< state
->nlookaheads
; ++i
) 
 178     if (LArule
[state
->lookaheadsp 
+ i
]->prec
 
 179         && LArule
[state
->lookaheadsp 
+ i
]->prec
->prec
 
 180         && !bitset_disjoint_p (LA
[state
->lookaheadsp 
+ i
], lookaheadset
)) 
 182         resolve_sr_conflict (state
, state
->lookaheadsp 
+ i
); 
 186   /* Loop over all rules which require lookahead in this state.  Check 
 187      for conflicts not resolved above.  */ 
 188   for (i 
= 0; i 
< state
->nlookaheads
; ++i
) 
 190       if (!bitset_disjoint_p (LA
[state
->lookaheadsp 
+ i
], lookaheadset
)) 
 191         conflicts
[state
->number
] = 1; 
 193       bitset_or (lookaheadset
, lookaheadset
, LA
[state
->lookaheadsp 
+ i
]); 
 198 solve_conflicts (void) 
 202   conflicts 
= XCALLOC (char, nstates
); 
 203   shiftset 
= bitset_create (ntokens
, BITSET_FIXED
); 
 204   lookaheadset 
= bitset_create (ntokens
, BITSET_FIXED
); 
 206   for (i 
= 0; i 
< nstates
; i
++) 
 207     set_conflicts (states
[i
]); 
 211 /*---------------------------------------------. 
 212 | Count the number of shift/reduce conflicts.  | 
 213 `---------------------------------------------*/ 
 216 count_sr_conflicts (state_t 
*state
) 
 220   shifts 
*shiftp 
= state
->shifts
; 
 225   bitset_zero (lookaheadset
); 
 226   bitset_zero (shiftset
); 
 228   for (i 
= 0; i 
< shiftp
->nshifts 
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++) 
 229     if (!SHIFT_IS_DISABLED (shiftp
, i
)) 
 230       bitset_set (shiftset
, SHIFT_SYMBOL (shiftp
, i
)); 
 232   for (i 
= 0; i 
< state
->nlookaheads
; ++i
) 
 233     bitset_or (lookaheadset
, lookaheadset
, LA
[state
->lookaheadsp 
+ i
]); 
 235   bitset_and (lookaheadset
, lookaheadset
, shiftset
); 
 237   src_count 
= bitset_count (lookaheadset
); 
 243 /*----------------------------------------------. 
 244 | Count the number of reduce/reduce conflicts.  | 
 245 `----------------------------------------------*/ 
 248 count_rr_conflicts (state_t 
*state
) 
 253   if (state
->nlookaheads 
< 2) 
 256   for (i 
= 0; i 
< ntokens
; i
++) 
 260       for (j 
= 0; j 
< state
->nlookaheads
; ++j
) 
 261         if (bitset_test (LA
[state
->lookaheadsp 
+ j
], i
)) 
 271 /*--------------------------------------------------------------. 
 272 | Return a human readable string which reports shift/reduce and | 
 273 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM).            | 
 274 `--------------------------------------------------------------*/ 
 277 conflict_report (int src_num
, int rrc_num
) 
 279   static char res
[4096]; 
 284       sprintf (cp
, ngettext ("%d shift/reduce conflict", 
 285                              "%d shift/reduce conflicts", src_num
), src_num
); 
 289   if (src_num 
> 0 && rrc_num 
> 0) 
 291       sprintf (cp
, " %s ", _("and")); 
 297       sprintf (cp
, ngettext ("%d reduce/reduce conflict", 
 298                              "%d reduce/reduce conflicts", rrc_num
), rrc_num
); 
 310 /*-----------------------------------------------------------. 
 311 | Output the detailed description of states with conflicts.  | 
 312 `-----------------------------------------------------------*/ 
 315 conflicts_output (FILE *out
) 
 317   bool printed_sth 
= FALSE
; 
 319   for (i 
= 0; i 
< nstates
; i
++) 
 322         fprintf (out
, _("State %d contains "), i
); 
 323         fputs (conflict_report (count_sr_conflicts (states
[i
]), 
 324                                 count_rr_conflicts (states
[i
])), out
); 
 332 /*------------------------------------------. 
 333 | Reporting the total number of conflicts.  | 
 334 `------------------------------------------*/ 
 337 conflicts_print (void) 
 341   /* Is the number of SR conflicts OK?  Either EXPECTED_CONFLICTS is 
 342      not set, and then we want 0 SR, or else it is specified, in which 
 343      case we want equality.  */ 
 349   /* Conflicts by state.  */ 
 350   for (i 
= 0; i 
< nstates
; i
++) 
 353         src_total 
+= count_sr_conflicts (states
[i
]); 
 354         rrc_total 
+= count_rr_conflicts (states
[i
]); 
 357   src_ok 
= src_total 
== (expected_conflicts 
== -1 ? 0 : expected_conflicts
); 
 359   /* If there are no RR conflicts, and as many SR conflicts as 
 360      expected, then there is nothing to report.  */ 
 361   if (!rrc_total 
&& src_ok
) 
 364   /* Report the total number of conflicts on STDERR.  */ 
 367       /* If invoked with `--yacc', use the output format specified by 
 369       fprintf (stderr
, _("conflicts: ")); 
 371         fprintf (stderr
, _(" %d shift/reduce"), src_total
); 
 372       if (src_total 
> 0 && rrc_total 
> 0) 
 373         fprintf (stderr
, ","); 
 375         fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
); 
 380       fprintf (stderr
, _("%s contains "), infile
); 
 381       fputs (conflict_report (src_total
, rrc_total
), stderr
); 
 384   if (expected_conflicts 
!= -1 && !src_ok
) 
 386       complain_message_count
++; 
 387       fprintf (stderr
, ngettext ("expected %d shift/reduce conflict\n", 
 388                                  "expected %d shift/reduce conflicts\n", 
 396 free_conflicts (void) 
 399   bitset_free (shiftset
); 
 400   bitset_free (lookaheadset
);