]> git.saurik.com Git - bison.git/blob - src/conflicts.c
* src/gram.h, src/gram.c (symbols): New, similar to state_table
[bison.git] / src / conflicts.c
1 /* Find and resolve or report look-ahead conflicts for bison,
2 Copyright 1984, 1989, 1992, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
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)
9 any later version.
10
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.
15
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
19 02111-1307, USA. */
20
21 #include "system.h"
22 #include "complain.h"
23 #include "getargs.h"
24 #include "symtab.h"
25 #include "files.h"
26 #include "gram.h"
27 #include "state.h"
28 #include "lalr.h"
29 #include "conflicts.h"
30 #include "reader.h"
31 #include "LR0.h"
32
33 /* -1 stands for not specified. */
34 int expected_conflicts = -1;
35 static char *conflicts = NULL;
36
37 static unsigned *shiftset = NULL;
38 static unsigned *lookaheadset = NULL;
39 \f
40
41 static inline void
42 log_resolution (state_t *state, int LAno, int token, char *resolution)
43 {
44 if (verbose_flag)
45 obstack_fgrow4 (&output_obstack,
46 _("\
47 Conflict in state %d between rule %d and token %s resolved as %s.\n"),
48 state->number, LAruleno[LAno], tags[token], resolution);
49 }
50
51
52 /*------------------------------------------------------------------.
53 | Turn off the shift recorded for the specified token in the |
54 | specified state. Used when we resolve a shift-reduce conflict in |
55 | favor of the reduction. |
56 `------------------------------------------------------------------*/
57
58 static void
59 flush_shift (state_t *state, int token)
60 {
61 shifts *shiftp = state->shifts;
62 int i;
63
64 RESETBIT (lookaheadset, token);
65 for (i = 0; i < shiftp->nshifts; i++)
66 if (!SHIFT_IS_DISABLED (shiftp, i) && SHIFT_SYMBOL (shiftp, i) == token)
67 SHIFT_DISABLE (shiftp, i);
68 }
69
70
71 /*-------------------------------------------------------------------.
72 | Turn off the reduce recorded for the specified token for the |
73 | specified lookahead. Used when we resolve a shift-reduce conflict |
74 | in favor of the shift. |
75 `-------------------------------------------------------------------*/
76
77 static void
78 flush_reduce (int lookahead, int token)
79 {
80 RESETBIT (LA (lookahead), token);
81 }
82
83
84 /*------------------------------------------------------------------.
85 | Attempt to resolve shift-reduce conflict for one rule by means of |
86 | precedence declarations. It has already been checked that the |
87 | rule has a precedence. A conflict is resolved by modifying the |
88 | shift or reduce tables so that there is no longer a conflict. |
89 `------------------------------------------------------------------*/
90
91 static void
92 resolve_sr_conflict (state_t *state, int lookahead)
93 {
94 int i;
95 /* find the rule to reduce by to get precedence of reduction */
96 int redprec = rule_table[LAruleno[lookahead]].prec;
97 errs *errp = errs_new (ntokens + 1);
98 errp->nerrs = 0;
99
100 for (i = 0; i < ntokens; i++)
101 if (BITISSET (LA (lookahead), i)
102 && BITISSET (lookaheadset, i)
103 && symbols[i]->prec)
104 {
105 /* Shift-reduce conflict occurs for token number i
106 and it has a precedence.
107 The precedence of shifting is that of token i. */
108 if (symbols[i]->prec < redprec)
109 {
110 log_resolution (state, lookahead, i, _("reduce"));
111 flush_shift (state, i);
112 }
113 else if (symbols[i]->prec > redprec)
114 {
115 log_resolution (state, lookahead, i, _("shift"));
116 flush_reduce (lookahead, i);
117 }
118 else
119 /* Matching precedence levels.
120 For left association, keep only the reduction.
121 For right association, keep only the shift.
122 For nonassociation, keep neither. */
123
124 switch (sassoc[i])
125 {
126 case right_assoc:
127 log_resolution (state, lookahead, i, _("shift"));
128 flush_reduce (lookahead, i);
129 break;
130
131 case left_assoc:
132 log_resolution (state, lookahead, i, _("reduce"));
133 flush_shift (state, i);
134 break;
135
136 case non_assoc:
137 log_resolution (state, lookahead, i, _("an error"));
138 flush_shift (state, i);
139 flush_reduce (lookahead, i);
140 /* Record an explicit error for this token. */
141 errp->errs[errp->nerrs++] = i;
142 break;
143 }
144 }
145
146 /* Some tokens have been explicitly made errors. Allocate a
147 permanent errs structure for this state, to record them. */
148 state->errs = errs_dup (errp);
149 free (errp);
150 }
151
152
153 static void
154 set_conflicts (state_t *state)
155 {
156 int i, j;
157 shifts *shiftp;
158
159 if (state->consistent)
160 return;
161
162 for (i = 0; i < tokensetsize; i++)
163 lookaheadset[i] = 0;
164
165 shiftp = state->shifts;
166 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
167 if (!SHIFT_IS_DISABLED (shiftp, i))
168 SETBIT (lookaheadset, SHIFT_SYMBOL (shiftp, i));
169
170 /* Loop over all rules which require lookahead in this state. First
171 check for shift-reduce conflict, and try to resolve using
172 precedence */
173 for (i = 0; i < state->nlookaheads; ++i)
174 if (rule_table[LAruleno[state->lookaheadsp + i]].prec)
175 for (j = 0; j < tokensetsize; ++j)
176 if (LA (state->lookaheadsp + i)[j] & lookaheadset[j])
177 {
178 resolve_sr_conflict (state, state->lookaheadsp + i);
179 break;
180 }
181
182
183 /* Loop over all rules which require lookahead in this state. Check
184 for conflicts not resolved above. */
185 for (i = 0; i < state->nlookaheads; ++i)
186 {
187 for (j = 0; j < tokensetsize; ++j)
188 if (LA (state->lookaheadsp + i)[j] & lookaheadset[j])
189 conflicts[state->number] = 1;
190
191 for (j = 0; j < tokensetsize; ++j)
192 lookaheadset[j] |= LA (state->lookaheadsp + i)[j];
193 }
194 }
195
196 void
197 solve_conflicts (void)
198 {
199 int i;
200
201 conflicts = XCALLOC (char, nstates);
202 shiftset = XCALLOC (unsigned, tokensetsize);
203 lookaheadset = XCALLOC (unsigned, tokensetsize);
204
205 for (i = 0; i < nstates; i++)
206 set_conflicts (state_table[i]);
207 }
208
209
210 /*---------------------------------------------.
211 | Count the number of shift/reduce conflicts. |
212 `---------------------------------------------*/
213
214 static int
215 count_sr_conflicts (state_t *state)
216 {
217 int i, k;
218 int src_count = 0;
219 shifts *shiftp = state->shifts;
220
221 if (!shiftp)
222 return 0;
223
224 for (i = 0; i < tokensetsize; i++)
225 {
226 shiftset[i] = 0;
227 lookaheadset[i] = 0;
228 }
229
230 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
231 if (!SHIFT_IS_DISABLED (shiftp, i))
232 SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i));
233
234 for (i = 0; i < state->nlookaheads; ++i)
235 for (k = 0; k < tokensetsize; ++k)
236 lookaheadset[k] |= LA (state->lookaheadsp + i)[k];
237
238 for (k = 0; k < tokensetsize; ++k)
239 lookaheadset[k] &= shiftset[k];
240
241 for (i = 0; i < ntokens; i++)
242 if (BITISSET (lookaheadset, i))
243 src_count++;
244
245 return src_count;
246 }
247
248
249 /*----------------------------------------------.
250 | Count the number of reduce/reduce conflicts. |
251 `----------------------------------------------*/
252
253 static int
254 count_rr_conflicts (state_t *state)
255 {
256 int i;
257 int rrc_count = 0;
258
259 if (state->nlookaheads < 2)
260 return 0;
261
262 for (i = 0; i < ntokens; i++)
263 {
264 int count = 0;
265 int j;
266 for (j = 0; j < state->nlookaheads; ++j)
267 if (BITISSET (LA (state->lookaheadsp), state->lookaheadsp + j))
268 count++;
269
270 if (count >= 2)
271 rrc_count++;
272 }
273
274 return rrc_count;
275 }
276
277 /*--------------------------------------------------------------.
278 | Return a human readable string which reports shift/reduce and |
279 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
280 `--------------------------------------------------------------*/
281
282 static const char *
283 conflict_report (int src_num, int rrc_num)
284 {
285 static char res[4096];
286 char *cp = res;
287
288 if (src_num >= 1)
289 {
290 sprintf (cp, ngettext ("%d shift/reduce conflict",
291 "%d shift/reduce conflicts", src_num), src_num);
292 cp += strlen (cp);
293 }
294
295 if (src_num > 0 && rrc_num > 0)
296 {
297 sprintf (cp, " %s ", _("and"));
298 cp += strlen (cp);
299 }
300
301 if (rrc_num >= 1)
302 {
303 sprintf (cp, ngettext ("%d reduce/reduce conflict",
304 "%d reduce/reduce conflicts", rrc_num), rrc_num);
305 cp += strlen (cp);
306 }
307
308 *cp++ = '.';
309 *cp++ = '\n';
310 *cp++ = '\0';
311
312 return res;
313 }
314
315
316 /*-----------------------------------------------------------.
317 | Output the detailed description of states with conflicts. |
318 `-----------------------------------------------------------*/
319
320 void
321 conflicts_output (FILE *out)
322 {
323 bool printed_sth = FALSE;
324 int i;
325 for (i = 0; i < nstates; i++)
326 if (conflicts[i])
327 {
328 fprintf (out, _("State %d contains "), i);
329 fputs (conflict_report (count_sr_conflicts (state_table[i]),
330 count_rr_conflicts (state_table[i])), out);
331 printed_sth = TRUE;
332 }
333 if (printed_sth)
334 fputs ("\n\n", out);
335 }
336
337
338 /*------------------------------------------.
339 | Reporting the total number of conflicts. |
340 `------------------------------------------*/
341
342 void
343 conflicts_print (void)
344 {
345 int i;
346
347 /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is
348 not set, and then we want 0 SR, or else it is specified, in which
349 case we want equality. */
350 int src_ok = 0;
351
352 int src_total = 0;
353 int rrc_total = 0;
354
355 /* Conflicts by state. */
356 for (i = 0; i < nstates; i++)
357 if (conflicts[i])
358 {
359 src_total += count_sr_conflicts (state_table[i]);
360 rrc_total += count_rr_conflicts (state_table[i]);
361 }
362
363 src_ok = src_total == (expected_conflicts == -1 ? 0 : expected_conflicts);
364
365 /* If there are no RR conflicts, and as many SR conflicts as
366 expected, then there is nothing to report. */
367 if (!rrc_total && src_ok)
368 return;
369
370 /* Report the total number of conflicts on STDERR. */
371 if (yacc_flag)
372 {
373 /* If invoked with `--yacc', use the output format specified by
374 POSIX. */
375 fprintf (stderr, _("conflicts: "));
376 if (src_total > 0)
377 fprintf (stderr, _(" %d shift/reduce"), src_total);
378 if (src_total > 0 && rrc_total > 0)
379 fprintf (stderr, ",");
380 if (rrc_total > 0)
381 fprintf (stderr, _(" %d reduce/reduce"), rrc_total);
382 putc ('\n', stderr);
383 }
384 else
385 {
386 fprintf (stderr, _("%s contains "), infile);
387 fputs (conflict_report (src_total, rrc_total), stderr);
388 }
389
390 if (expected_conflicts != -1 && !src_ok)
391 {
392 complain_message_count++;
393 fprintf (stderr, ngettext ("expected %d shift/reduce conflict\n",
394 "expected %d shift/reduce conflicts\n",
395 expected_conflicts),
396 expected_conflicts);
397 }
398 }
399
400
401 void
402 free_conflicts (void)
403 {
404 XFREE (conflicts);
405 XFREE (shiftset);
406 XFREE (lookaheadset);
407 }