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