]>
git.saurik.com Git - bison.git/blob - src/conflicts.c
bd29bf9717fb730649923328880e0f9c77853be0
   1 /* Find and resolve or report look-ahead conflicts for bison, 
   2    Copyright 1984, 1989, 1992, 2000, 2001 Free Software Foundation, Inc. 
   4    This file is part of Bison, the GNU Compiler Compiler. 
   6    Bison is free software; you can redistribute it and/or modify it 
   7    under the terms of the GNU General Public License as published by 
   8    the Free Software Foundation; either version 2, or (at your option) 
  11    Bison is distributed in the hope that it will be useful, but 
  12    WITHOUT ANY WARRANTY; without even the implied warranty of 
  13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
  14    General Public License for more details. 
  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 the Free 
  18    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
  29 #include "conflicts.h" 
  33 /* -1 stands for not specified. */ 
  34 int expected_conflicts 
= -1; 
  35 static char *conflicts 
= NULL
; 
  37 static unsigned *shiftset 
= NULL
; 
  38 static unsigned *lookaheadset 
= NULL
; 
  42 log_resolution (state_t 
*state
, int LAno
, int token
, char *resolution
) 
  45     obstack_fgrow4 (&output_obstack
, 
  47 Conflict in state %d between rule %d and token %s resolved as %s.\n"), 
  48                     state
->number
, LAruleno
[LAno
], symbols
[token
]->tag
, 
  53 /*------------------------------------------------------------------. 
  54 | Turn off the shift recorded for the specified token in the        | 
  55 | specified state.  Used when we resolve a shift-reduce conflict in | 
  56 | favor of the reduction.                                           | 
  57 `------------------------------------------------------------------*/ 
  60 flush_shift (state_t 
*state
, int token
) 
  62   shifts 
*shiftp 
= state
->shifts
; 
  65   RESETBIT (lookaheadset
, token
); 
  66   for (i 
= 0; i 
< shiftp
->nshifts
; i
++) 
  67     if (!SHIFT_IS_DISABLED (shiftp
, i
) && SHIFT_SYMBOL (shiftp
, i
) == token
) 
  68       SHIFT_DISABLE (shiftp
, i
); 
  72 /*-------------------------------------------------------------------. 
  73 | Turn off the reduce recorded for the specified token for the       | 
  74 | specified lookahead.  Used when we resolve a shift-reduce conflict | 
  75 | in favor of the shift.                                             | 
  76 `-------------------------------------------------------------------*/ 
  79 flush_reduce (int lookahead
, int token
) 
  81   RESETBIT (LA (lookahead
), token
); 
  85 /*------------------------------------------------------------------. 
  86 | Attempt to resolve shift-reduce conflict for one rule by means of | 
  87 | precedence declarations.  It has already been checked that the    | 
  88 | rule has a precedence.  A conflict is resolved by modifying the   | 
  89 | shift or reduce tables so that there is no longer a conflict.     | 
  90 `------------------------------------------------------------------*/ 
  93 resolve_sr_conflict (state_t 
*state
, int lookahead
) 
  96   /* find the rule to reduce by to get precedence of reduction  */ 
  97   int redprec 
= rules
[LAruleno
[lookahead
]].prec
; 
  98   errs 
*errp 
= errs_new (ntokens 
+ 1); 
 101   for (i 
= 0; i 
< ntokens
; i
++) 
 102     if (BITISSET (LA (lookahead
), i
) 
 103         && BITISSET (lookaheadset
, i
) 
 106         /* Shift-reduce conflict occurs for token number i 
 107            and it has a precedence. 
 108            The precedence of shifting is that of token i.  */ 
 109         if (symbols
[i
]->prec 
< redprec
) 
 111             log_resolution (state
, lookahead
, i
, _("reduce")); 
 112             flush_shift (state
, i
); 
 114         else if (symbols
[i
]->prec 
> redprec
) 
 116             log_resolution (state
, lookahead
, i
, _("shift")); 
 117             flush_reduce (lookahead
, i
); 
 120           /* Matching precedence levels. 
 121              For left association, keep only the reduction. 
 122              For right association, keep only the shift. 
 123              For nonassociation, keep neither.  */ 
 125           switch (symbols
[i
]->assoc
) 
 128               log_resolution (state
, lookahead
, i
, _("shift")); 
 129               flush_reduce (lookahead
, i
); 
 133               log_resolution (state
, lookahead
, i
, _("reduce")); 
 134               flush_shift (state
, i
); 
 138               log_resolution (state
, lookahead
, i
, _("an error")); 
 139               flush_shift (state
, i
); 
 140               flush_reduce (lookahead
, i
); 
 141               /* Record an explicit error for this token.  */ 
 142               errp
->errs
[errp
->nerrs
++] = i
; 
 147   /* Some tokens have been explicitly made errors.  Allocate a 
 148      permanent errs structure for this state, to record them.  */ 
 149   state
->errs 
= errs_dup (errp
); 
 155 set_conflicts (state_t 
*state
) 
 160   if (state
->consistent
) 
 163   for (i 
= 0; i 
< tokensetsize
; i
++) 
 166   shiftp 
= state
->shifts
; 
 167   for (i 
= 0; i 
< shiftp
->nshifts 
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++) 
 168     if (!SHIFT_IS_DISABLED (shiftp
, i
)) 
 169       SETBIT (lookaheadset
, SHIFT_SYMBOL (shiftp
, i
)); 
 171   /* Loop over all rules which require lookahead in this state.  First 
 172      check for shift-reduce conflict, and try to resolve using 
 174   for (i 
= 0; i 
< state
->nlookaheads
; ++i
) 
 175     if (rules
[LAruleno
[state
->lookaheadsp 
+ i
]].prec
) 
 176       for (j 
= 0; j 
< tokensetsize
; ++j
) 
 177         if (LA (state
->lookaheadsp 
+ i
)[j
] & lookaheadset
[j
]) 
 179             resolve_sr_conflict (state
, state
->lookaheadsp 
+ i
); 
 184   /* Loop over all rules which require lookahead in this state.  Check 
 185      for conflicts not resolved above.  */ 
 186   for (i 
= 0; i 
< state
->nlookaheads
; ++i
) 
 188       for (j 
= 0; j 
< tokensetsize
; ++j
) 
 189         if (LA (state
->lookaheadsp 
+ i
)[j
] & lookaheadset
[j
]) 
 190           conflicts
[state
->number
] = 1; 
 192       for (j 
= 0; j 
< tokensetsize
; ++j
) 
 193         lookaheadset
[j
] |= LA (state
->lookaheadsp 
+ i
)[j
]; 
 198 solve_conflicts (void) 
 202   conflicts 
= XCALLOC (char, nstates
); 
 203   shiftset 
= XCALLOC (unsigned, tokensetsize
); 
 204   lookaheadset 
= XCALLOC (unsigned, tokensetsize
); 
 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   for (i 
= 0; i 
< tokensetsize
; i
++) 
 231   for (i 
= 0; i 
< shiftp
->nshifts 
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++) 
 232     if (!SHIFT_IS_DISABLED (shiftp
, i
)) 
 233       SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
)); 
 235   for (i 
= 0; i 
< state
->nlookaheads
; ++i
) 
 236     for (k 
= 0; k 
< tokensetsize
; ++k
) 
 237       lookaheadset
[k
] |= LA (state
->lookaheadsp 
+ i
)[k
]; 
 239   for (k 
= 0; k 
< tokensetsize
; ++k
) 
 240     lookaheadset
[k
] &= shiftset
[k
]; 
 242   for (i 
= 0; i 
< ntokens
; i
++) 
 243     if (BITISSET (lookaheadset
, i
)) 
 250 /*----------------------------------------------. 
 251 | Count the number of reduce/reduce conflicts.  | 
 252 `----------------------------------------------*/ 
 255 count_rr_conflicts (state_t 
*state
) 
 260   if (state
->nlookaheads 
< 2) 
 263   for (i 
= 0; i 
< ntokens
; i
++) 
 267       for (j 
= 0; j 
< state
->nlookaheads
; ++j
) 
 268         if (BITISSET (LA (state
->lookaheadsp 
+ j
), i
)) 
 278 /*--------------------------------------------------------------. 
 279 | Return a human readable string which reports shift/reduce and | 
 280 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM).            | 
 281 `--------------------------------------------------------------*/ 
 284 conflict_report (int src_num
, int rrc_num
) 
 286   static char res
[4096]; 
 291       sprintf (cp
, ngettext ("%d shift/reduce conflict", 
 292                              "%d shift/reduce conflicts", src_num
), src_num
); 
 296   if (src_num 
> 0 && rrc_num 
> 0) 
 298       sprintf (cp
, " %s ", _("and")); 
 304       sprintf (cp
, ngettext ("%d reduce/reduce conflict", 
 305                              "%d reduce/reduce conflicts", rrc_num
), rrc_num
); 
 317 /*-----------------------------------------------------------. 
 318 | Output the detailed description of states with conflicts.  | 
 319 `-----------------------------------------------------------*/ 
 322 conflicts_output (FILE *out
) 
 324   bool printed_sth 
= FALSE
; 
 326   for (i 
= 0; i 
< nstates
; i
++) 
 329         fprintf (out
, _("State %d contains "), i
); 
 330         fputs (conflict_report (count_sr_conflicts (states
[i
]), 
 331                                 count_rr_conflicts (states
[i
])), out
); 
 339 /*------------------------------------------. 
 340 | Reporting the total number of conflicts.  | 
 341 `------------------------------------------*/ 
 344 conflicts_print (void) 
 348   /* Is the number of SR conflicts OK?  Either EXPECTED_CONFLICTS is 
 349      not set, and then we want 0 SR, or else it is specified, in which 
 350      case we want equality.  */ 
 356   /* Conflicts by state.  */ 
 357   for (i 
= 0; i 
< nstates
; i
++) 
 360         src_total 
+= count_sr_conflicts (states
[i
]); 
 361         rrc_total 
+= count_rr_conflicts (states
[i
]); 
 364   src_ok 
= src_total 
== (expected_conflicts 
== -1 ? 0 : expected_conflicts
); 
 366   /* If there are no RR conflicts, and as many SR conflicts as 
 367      expected, then there is nothing to report.  */ 
 368   if (!rrc_total 
&& src_ok
) 
 371   /* Report the total number of conflicts on STDERR.  */ 
 374       /* If invoked with `--yacc', use the output format specified by 
 376       fprintf (stderr
, _("conflicts: ")); 
 378         fprintf (stderr
, _(" %d shift/reduce"), src_total
); 
 379       if (src_total 
> 0 && rrc_total 
> 0) 
 380         fprintf (stderr
, ","); 
 382         fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
); 
 387       fprintf (stderr
, _("%s contains "), infile
); 
 388       fputs (conflict_report (src_total
, rrc_total
), stderr
); 
 391   if (expected_conflicts 
!= -1 && !src_ok
) 
 393       complain_message_count
++; 
 394       fprintf (stderr
, ngettext ("expected %d shift/reduce conflict\n", 
 395                                  "expected %d shift/reduce conflicts\n", 
 403 free_conflicts (void) 
 407   XFREE (lookaheadset
);