]> git.saurik.com Git - bison.git/blob - src/conflicts.c
* src/LR0.c: Attach shifts to states as soon as they are
[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 "files.h"
25 #include "gram.h"
26 #include "state.h"
27 #include "lalr.h"
28 #include "conflicts.h"
29 #include "reader.h"
30 #include "LR0.h"
31
32 /* -1 stands for not specified. */
33 int expected_conflicts = -1;
34 static char *conflicts = NULL;
35
36 static unsigned *shiftset = NULL;
37 static unsigned *lookaheadset = NULL;
38 \f
39
40 static inline void
41 log_resolution (int state, int LAno, int token, char *resolution)
42 {
43 obstack_fgrow4 (&output_obstack,
44 _("\
45 Conflict in state %d between rule %d and token %s resolved as %s.\n"),
46 state, LAruleno[LAno], tags[token], resolution);
47 }
48
49
50 /*------------------------------------------------------------------.
51 | Turn off the shift recorded for the specified token in the |
52 | specified state. Used when we resolve a shift-reduce conflict in |
53 | favor of the reduction. |
54 `------------------------------------------------------------------*/
55
56 static void
57 flush_shift (int state, int token)
58 {
59 shifts *shiftp = state_table[state]->shifts;
60 int i;
61
62 for (i = 0; i < shiftp->nshifts; i++)
63 if (!SHIFT_IS_DISABLED (shiftp, i) && SHIFT_SYMBOL (shiftp, i) == token)
64 SHIFT_DISABLE (shiftp, i);
65 }
66
67
68 /*------------------------------------------------------------------.
69 | Attempt to resolve shift-reduce conflict for one rule by means of |
70 | precedence declarations. It has already been checked that the |
71 | rule has a precedence. A conflict is resolved by modifying the |
72 | shift or reduce tables so that there is no longer a conflict. |
73 `------------------------------------------------------------------*/
74
75 static void
76 resolve_sr_conflict (int state, int lookaheadnum)
77 {
78 int i;
79 /* find the rule to reduce by to get precedence of reduction */
80 int redprec = rule_table[LAruleno[lookaheadnum]].prec;
81 errs *errp = ERRS_ALLOC (ntokens + 1);
82 short *errtokens = errp->errs;
83
84 for (i = 0; i < ntokens; i++)
85 if (BITISSET (LA (lookaheadnum), i)
86 && BITISSET (lookaheadset, i)
87 && sprec[i])
88 /* Shift-reduce conflict occurs for token number i
89 and it has a precedence.
90 The precedence of shifting is that of token i. */
91 {
92 if (sprec[i] < redprec)
93 {
94 log_resolution (state, lookaheadnum, i, _("reduce"));
95 /* flush the shift for this token */
96 RESETBIT (lookaheadset, i);
97 flush_shift (state, i);
98 }
99 else if (sprec[i] > redprec)
100 {
101 log_resolution (state, lookaheadnum, i, _("shift"));
102 /* flush the reduce for this token */
103 RESETBIT (LA (lookaheadnum), i);
104 }
105 else
106 {
107 /* Matching precedence levels.
108 For left association, keep only the reduction.
109 For right association, keep only the shift.
110 For nonassociation, keep neither. */
111
112 switch (sassoc[i])
113 {
114 case right_assoc:
115 log_resolution (state, lookaheadnum, i, _("shift"));
116 break;
117
118 case left_assoc:
119 log_resolution (state, lookaheadnum, i, _("reduce"));
120 break;
121
122 case non_assoc:
123 log_resolution (state, lookaheadnum, i, _("an error"));
124 break;
125 }
126
127 if (sassoc[i] != right_assoc)
128 {
129 /* flush the shift for this token */
130 RESETBIT (lookaheadset, i);
131 flush_shift (state, i);
132 }
133 if (sassoc[i] != left_assoc)
134 {
135 /* flush the reduce for this token */
136 RESETBIT (LA (lookaheadnum), i);
137 }
138 if (sassoc[i] == non_assoc)
139 {
140 /* Record an explicit error for this token. */
141 *errtokens++ = i;
142 }
143 }
144 }
145
146 errp->nerrs = errtokens - errp->errs;
147 /* Some tokens have been explicitly made errors. Allocate a
148 permanent errs structure for this state, to record them. */
149 i = (char *) errtokens - (char *) errp;
150 state_table[state]->errs = ERRS_ALLOC (i + 1);
151 memcpy (state_table[state]->errs, errp, i);
152 free (errp);
153 }
154
155
156 static void
157 set_conflicts (int state)
158 {
159 int i, j;
160 shifts *shiftp;
161
162 if (state_table[state]->consistent)
163 return;
164
165 for (i = 0; i < tokensetsize; i++)
166 lookaheadset[i] = 0;
167
168 shiftp = state_table[state]->shifts;
169 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
170 if (!SHIFT_IS_DISABLED (shiftp, i))
171 SETBIT (lookaheadset, SHIFT_SYMBOL (shiftp, i));
172
173 /* Loop over all rules which require lookahead in this state. First
174 check for shift-reduce conflict, and try to resolve using
175 precedence */
176 for (i = state_table[state]->lookaheads;
177 i < state_table[state + 1]->lookaheads;
178 ++i)
179 if (rule_table[LAruleno[i]].prec)
180 for (j = 0; j < tokensetsize; ++j)
181 if (LA (i)[j] & lookaheadset[j])
182 {
183 resolve_sr_conflict (state, i);
184 break;
185 }
186
187
188 /* Loop over all rules which require lookahead in this state. Check
189 for conflicts not resolved above. */
190 for (i = state_table[state]->lookaheads;
191 i < state_table[state + 1]->lookaheads;
192 ++i)
193 {
194 for (j = 0; j < tokensetsize; ++j)
195 if (LA (i)[j] & lookaheadset[j])
196 conflicts[state] = 1;
197
198 for (j = 0; j < tokensetsize; ++j)
199 lookaheadset[j] |= LA (i)[j];
200 }
201 }
202
203 void
204 solve_conflicts (void)
205 {
206 int i;
207
208 conflicts = XCALLOC (char, nstates);
209 shiftset = XCALLOC (unsigned, tokensetsize);
210 lookaheadset = XCALLOC (unsigned, tokensetsize);
211
212 for (i = 0; i < nstates; i++)
213 set_conflicts (i);
214 }
215
216
217 /*---------------------------------------------.
218 | Count the number of shift/reduce conflicts. |
219 `---------------------------------------------*/
220
221 static int
222 count_sr_conflicts (int state)
223 {
224 int i, k;
225 int src_count = 0;
226 shifts *shiftp = state_table[state]->shifts;
227
228 if (!shiftp)
229 return 0;
230
231 for (i = 0; i < tokensetsize; i++)
232 {
233 shiftset[i] = 0;
234 lookaheadset[i] = 0;
235 }
236
237 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
238 if (!SHIFT_IS_DISABLED (shiftp, i))
239 SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i));
240
241 for (i = state_table[state]->lookaheads;
242 i < state_table[state + 1]->lookaheads;
243 ++i)
244 for (k = 0; k < tokensetsize; ++k)
245 lookaheadset[k] |= LA (i)[k];
246
247 for (k = 0; k < tokensetsize; ++k)
248 lookaheadset[k] &= shiftset[k];
249
250 for (i = 0; i < ntokens; i++)
251 if (BITISSET (lookaheadset, i))
252 src_count++;
253
254 return src_count;
255 }
256
257
258 /*----------------------------------------------.
259 | Count the number of reduce/reduce conflicts. |
260 `----------------------------------------------*/
261
262 static int
263 count_rr_conflicts (int state)
264 {
265 int i;
266 int rrc_count = 0;
267
268 int m = state_table[state]->lookaheads;
269 int n = state_table[state + 1]->lookaheads;
270
271 if (n - m < 2)
272 return 0;
273
274 for (i = 0; i < ntokens; i++)
275 {
276 int count = 0;
277 int j;
278 for (j = m; j < n; j++)
279 if (BITISSET (LA (m), j))
280 count++;
281
282 if (count >= 2)
283 rrc_count++;
284 }
285
286 return rrc_count;
287 }
288
289 /*--------------------------------------------------------------.
290 | Return a human readable string which reports shift/reduce and |
291 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
292 `--------------------------------------------------------------*/
293
294 static const char *
295 conflict_report (int src_num, int rrc_num)
296 {
297 static char res[4096];
298 char *cp = res;
299
300 if (src_num >= 1)
301 {
302 sprintf (cp, ngettext ("%d shift/reduce conflict",
303 "%d shift/reduce conflicts", src_num), src_num);
304 cp += strlen (cp);
305 }
306
307 if (src_num > 0 && rrc_num > 0)
308 {
309 sprintf (cp, " %s ", _("and"));
310 cp += strlen (cp);
311 }
312
313 if (rrc_num >= 1)
314 {
315 sprintf (cp, ngettext ("%d reduce/reduce conflict",
316 "%d reduce/reduce conflicts", rrc_num), rrc_num);
317 cp += strlen (cp);
318 }
319
320 *cp++ = '.';
321 *cp++ = '\n';
322 *cp++ = '\0';
323
324 return res;
325 }
326
327
328 /*-----------------------------------------------------------.
329 | Output the detailed description of states with conflicts. |
330 `-----------------------------------------------------------*/
331
332 void
333 conflicts_output (FILE *out)
334 {
335 bool printed_sth = FALSE;
336 int i;
337 for (i = 0; i < nstates; i++)
338 if (conflicts[i])
339 {
340 fprintf (out, _("State %d contains "), i);
341 fputs (conflict_report (count_sr_conflicts (i),
342 count_rr_conflicts (i)), out);
343 printed_sth = TRUE;
344 }
345 if (printed_sth)
346 fputs ("\n\n", out);
347 }
348
349
350 /*------------------------------------------.
351 | Reporting the total number of conflicts. |
352 `------------------------------------------*/
353
354 void
355 conflicts_print (void)
356 {
357 int i;
358
359 /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is
360 not set, and then we want 0 SR, or else it is specified, in which
361 case we want equality. */
362 int src_ok = 0;
363
364 int src_total = 0;
365 int rrc_total = 0;
366
367 /* Conflicts by state. */
368 for (i = 0; i < nstates; i++)
369 if (conflicts[i])
370 {
371 src_total += count_sr_conflicts (i);
372 rrc_total += count_rr_conflicts (i);
373 }
374
375 src_ok = src_total == (expected_conflicts == -1 ? 0 : expected_conflicts);
376
377 /* If there are no RR conflicts, and as many SR conflicts as
378 expected, then there is nothing to report. */
379 if (!rrc_total && src_ok)
380 return;
381
382 /* Report the total number of conflicts on STDERR. */
383 if (yacc_flag)
384 {
385 /* If invoked with `--yacc', use the output format specified by
386 POSIX. */
387 fprintf (stderr, _("conflicts: "));
388 if (src_total > 0)
389 fprintf (stderr, _(" %d shift/reduce"), src_total);
390 if (src_total > 0 && rrc_total > 0)
391 fprintf (stderr, ",");
392 if (rrc_total > 0)
393 fprintf (stderr, _(" %d reduce/reduce"), rrc_total);
394 putc ('\n', stderr);
395 }
396 else
397 {
398 fprintf (stderr, _("%s contains "), infile);
399 fputs (conflict_report (src_total, rrc_total), stderr);
400 }
401
402 if (expected_conflicts != -1 && !src_ok)
403 {
404 complain_message_count++;
405 fprintf (stderr, ngettext ("expected %d shift/reduce conflict\n",
406 "expected %d shift/reduce conflicts\n",
407 expected_conflicts),
408 expected_conflicts);
409 }
410 }
411
412
413 void
414 print_reductions (FILE *out, int state)
415 {
416 int i;
417 int j;
418 int m;
419 int n;
420 shifts *shiftp;
421 errs *errp;
422 int nodefault = 0;
423
424 for (i = 0; i < tokensetsize; i++)
425 shiftset[i] = 0;
426
427 shiftp = state_table[state]->shifts;
428 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
429 if (!SHIFT_IS_DISABLED (shiftp, i))
430 {
431 /* if this state has a shift for the error token, don't use a
432 default rule. */
433 if (SHIFT_IS_ERROR (shiftp, i))
434 nodefault = 1;
435 SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i));
436 }
437
438 errp = state_table[state]->errs;
439 if (errp)
440 for (i = 0; i < errp->nerrs; i++)
441 if (errp->errs[i])
442 SETBIT (shiftset, errp->errs[i]);
443
444 m = state_table[state]->lookaheads;
445 n = state_table[state + 1]->lookaheads;
446
447 if (n - m == 1 && !nodefault)
448 {
449 int k;
450 int default_rule = LAruleno[m];
451
452 for (k = 0; k < tokensetsize; ++k)
453 lookaheadset[k] = LA (m)[k] & shiftset[k];
454
455 for (i = 0; i < ntokens; i++)
456 if (BITISSET (lookaheadset, i))
457 fprintf (out, _(" %-4s\t[reduce using rule %d (%s)]\n"),
458 tags[i], default_rule,
459 tags[rule_table[default_rule].lhs]);
460
461 fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"),
462 default_rule, tags[rule_table[default_rule].lhs]);
463 }
464 else if (n - m >= 1)
465 {
466 int k;
467
468 int cmax = 0;
469 int default_LA = -1;
470 int default_rule = 0;
471
472 if (!nodefault)
473 for (i = m; i < n; i++)
474 {
475 int count = 0;
476
477 for (k = 0; k < tokensetsize; ++k)
478 lookaheadset[k] = LA (i)[k] & ~shiftset[k];
479
480 for (j = 0; j < ntokens; j++)
481 if (BITISSET (lookaheadset, j))
482 count++;
483
484 if (count > cmax)
485 {
486 cmax = count;
487 default_LA = i;
488 default_rule = LAruleno[i];
489 }
490
491 for (k = 0; k < tokensetsize; ++k)
492 shiftset[k] |= lookaheadset[k];
493 }
494
495 for (i = 0; i < tokensetsize; i++)
496 shiftset[i] = 0;
497
498 for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
499 if (!SHIFT_IS_DISABLED (shiftp, i))
500 SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i));
501
502 for (i = 0; i < ntokens; i++)
503 {
504 int defaulted = 0;
505 int count = BITISSET (shiftset, i);
506
507 for (j = m; j < n; j++)
508 {
509 if (BITISSET (LA (m), j))
510 {
511 if (count == 0)
512 {
513 if (j != default_LA)
514 fprintf (out,
515 _(" %-4s\treduce using rule %d (%s)\n"),
516 tags[i],
517 LAruleno[j],
518 tags[rule_table[LAruleno[j]].lhs]);
519 else
520 defaulted = 1;
521
522 count++;
523 }
524 else
525 {
526 if (defaulted)
527 fprintf (out,
528 _(" %-4s\treduce using rule %d (%s)\n"),
529 tags[i],
530 LAruleno[default_LA],
531 tags[rule_table[LAruleno[default_LA]].lhs]);
532 defaulted = 0;
533 fprintf (out,
534 _(" %-4s\t[reduce using rule %d (%s)]\n"),
535 tags[i],
536 LAruleno[j],
537 tags[rule_table[LAruleno[j]].lhs]);
538 }
539 }
540 }
541 }
542
543 if (default_LA >= 0)
544 fprintf (out, _(" $default\treduce using rule %d (%s)\n"),
545 default_rule, tags[rule_table[default_rule].lhs]);
546 }
547 }
548
549
550 void
551 free_conflicts (void)
552 {
553 XFREE (conflicts);
554 XFREE (shiftset);
555 XFREE (lookaheadset);
556 }