]>
git.saurik.com Git - bison.git/blob - 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.
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
28 #include "conflicts.h"
32 /* -1 stands for not specified. */
33 int expected_conflicts
= -1;
34 static char *conflicts
= NULL
;
36 static unsigned *shiftset
= NULL
;
37 static unsigned *lookaheadset
= NULL
;
41 log_resolution (state_t
*state
, int LAno
, int token
, char *resolution
)
44 obstack_fgrow4 (&output_obstack
,
46 Conflict in state %d between rule %d and token %s resolved as %s.\n"),
47 state
->number
, LAruleno
[LAno
], tags
[token
], resolution
);
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 `------------------------------------------------------------------*/
58 flush_shift (state_t
*state
, int token
)
60 shifts
*shiftp
= state
->shifts
;
63 RESETBIT (lookaheadset
, token
);
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
);
70 /*-------------------------------------------------------------------.
71 | Turn off the reduce recorded for the specified token for the |
72 | specified lookahead. Used when we resolve a shift-reduce conflict |
73 | in favor of the shift. |
74 `-------------------------------------------------------------------*/
77 flush_reduce (int lookahead
, int token
)
79 RESETBIT (LA (lookahead
), token
);
83 /*------------------------------------------------------------------.
84 | Attempt to resolve shift-reduce conflict for one rule by means of |
85 | precedence declarations. It has already been checked that the |
86 | rule has a precedence. A conflict is resolved by modifying the |
87 | shift or reduce tables so that there is no longer a conflict. |
88 `------------------------------------------------------------------*/
91 resolve_sr_conflict (state_t
*state
, int lookahead
)
94 /* find the rule to reduce by to get precedence of reduction */
95 int redprec
= rule_table
[LAruleno
[lookahead
]].prec
;
96 errs
*errp
= ERRS_ALLOC (ntokens
+ 1);
97 short *errtokens
= errp
->errs
;
99 for (i
= 0; i
< ntokens
; i
++)
100 if (BITISSET (LA (lookahead
), i
)
101 && BITISSET (lookaheadset
, i
)
104 /* Shift-reduce conflict occurs for token number i
105 and it has a precedence.
106 The precedence of shifting is that of token i. */
107 if (sprec
[i
] < redprec
)
109 log_resolution (state
, lookahead
, i
, _("reduce"));
110 flush_shift (state
, i
);
112 else if (sprec
[i
] > redprec
)
114 log_resolution (state
, lookahead
, i
, _("shift"));
115 flush_reduce (lookahead
, i
);
118 /* Matching precedence levels.
119 For left association, keep only the reduction.
120 For right association, keep only the shift.
121 For nonassociation, keep neither. */
126 log_resolution (state
, lookahead
, i
, _("shift"));
127 flush_reduce (lookahead
, i
);
131 log_resolution (state
, lookahead
, i
, _("reduce"));
132 flush_shift (state
, i
);
136 log_resolution (state
, lookahead
, i
, _("an error"));
137 flush_shift (state
, i
);
138 flush_reduce (lookahead
, i
);
139 /* Record an explicit error for this token. */
145 errp
->nerrs
= errtokens
- errp
->errs
;
146 /* Some tokens have been explicitly made errors. Allocate a
147 permanent errs structure for this state, to record them. */
148 i
= (char *) errtokens
- (char *) errp
;
149 state
->errs
= ERRS_ALLOC (i
+ 1);
150 memcpy (state
->errs
, errp
, i
);
156 set_conflicts (state_t
*state
)
161 if (state
->consistent
)
164 for (i
= 0; i
< tokensetsize
; i
++)
167 shiftp
= state
->shifts
;
168 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
169 if (!SHIFT_IS_DISABLED (shiftp
, i
))
170 SETBIT (lookaheadset
, SHIFT_SYMBOL (shiftp
, i
));
172 /* Loop over all rules which require lookahead in this state. First
173 check for shift-reduce conflict, and try to resolve using
175 for (i
= 0; i
< state
->nlookaheads
; ++i
)
176 if (rule_table
[LAruleno
[state
->lookaheadsp
+ i
]].prec
)
177 for (j
= 0; j
< tokensetsize
; ++j
)
178 if (LA (state
->lookaheadsp
+ i
)[j
] & lookaheadset
[j
])
180 resolve_sr_conflict (state
, state
->lookaheadsp
+ i
);
185 /* Loop over all rules which require lookahead in this state. Check
186 for conflicts not resolved above. */
187 for (i
= 0; i
< state
->nlookaheads
; ++i
)
189 for (j
= 0; j
< tokensetsize
; ++j
)
190 if (LA (state
->lookaheadsp
+ i
)[j
] & lookaheadset
[j
])
191 conflicts
[state
->number
] = 1;
193 for (j
= 0; j
< tokensetsize
; ++j
)
194 lookaheadset
[j
] |= LA (state
->lookaheadsp
+ i
)[j
];
199 solve_conflicts (void)
203 conflicts
= XCALLOC (char, nstates
);
204 shiftset
= XCALLOC (unsigned, tokensetsize
);
205 lookaheadset
= XCALLOC (unsigned, tokensetsize
);
207 for (i
= 0; i
< nstates
; i
++)
208 set_conflicts (state_table
[i
]);
212 /*---------------------------------------------.
213 | Count the number of shift/reduce conflicts. |
214 `---------------------------------------------*/
217 count_sr_conflicts (state_t
*state
)
221 shifts
*shiftp
= state
->shifts
;
226 for (i
= 0; i
< tokensetsize
; i
++)
232 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
233 if (!SHIFT_IS_DISABLED (shiftp
, i
))
234 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
236 for (i
= 0; i
< state
->nlookaheads
; ++i
)
237 for (k
= 0; k
< tokensetsize
; ++k
)
238 lookaheadset
[k
] |= LA (state
->lookaheadsp
+ i
)[k
];
240 for (k
= 0; k
< tokensetsize
; ++k
)
241 lookaheadset
[k
] &= shiftset
[k
];
243 for (i
= 0; i
< ntokens
; i
++)
244 if (BITISSET (lookaheadset
, i
))
251 /*----------------------------------------------.
252 | Count the number of reduce/reduce conflicts. |
253 `----------------------------------------------*/
256 count_rr_conflicts (state_t
*state
)
261 if (state
->nlookaheads
< 2)
264 for (i
= 0; i
< ntokens
; i
++)
268 for (j
= 0; j
< state
->nlookaheads
; ++j
)
269 if (BITISSET (LA (state
->lookaheadsp
), state
->lookaheadsp
+ j
))
279 /*--------------------------------------------------------------.
280 | Return a human readable string which reports shift/reduce and |
281 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
282 `--------------------------------------------------------------*/
285 conflict_report (int src_num
, int rrc_num
)
287 static char res
[4096];
292 sprintf (cp
, ngettext ("%d shift/reduce conflict",
293 "%d shift/reduce conflicts", src_num
), src_num
);
297 if (src_num
> 0 && rrc_num
> 0)
299 sprintf (cp
, " %s ", _("and"));
305 sprintf (cp
, ngettext ("%d reduce/reduce conflict",
306 "%d reduce/reduce conflicts", rrc_num
), rrc_num
);
318 /*-----------------------------------------------------------.
319 | Output the detailed description of states with conflicts. |
320 `-----------------------------------------------------------*/
323 conflicts_output (FILE *out
)
325 bool printed_sth
= FALSE
;
327 for (i
= 0; i
< nstates
; i
++)
330 fprintf (out
, _("State %d contains "), i
);
331 fputs (conflict_report (count_sr_conflicts (state_table
[i
]),
332 count_rr_conflicts (state_table
[i
])), out
);
340 /*------------------------------------------.
341 | Reporting the total number of conflicts. |
342 `------------------------------------------*/
345 conflicts_print (void)
349 /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is
350 not set, and then we want 0 SR, or else it is specified, in which
351 case we want equality. */
357 /* Conflicts by state. */
358 for (i
= 0; i
< nstates
; i
++)
361 src_total
+= count_sr_conflicts (state_table
[i
]);
362 rrc_total
+= count_rr_conflicts (state_table
[i
]);
365 src_ok
= src_total
== (expected_conflicts
== -1 ? 0 : expected_conflicts
);
367 /* If there are no RR conflicts, and as many SR conflicts as
368 expected, then there is nothing to report. */
369 if (!rrc_total
&& src_ok
)
372 /* Report the total number of conflicts on STDERR. */
375 /* If invoked with `--yacc', use the output format specified by
377 fprintf (stderr
, _("conflicts: "));
379 fprintf (stderr
, _(" %d shift/reduce"), src_total
);
380 if (src_total
> 0 && rrc_total
> 0)
381 fprintf (stderr
, ",");
383 fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
);
388 fprintf (stderr
, _("%s contains "), infile
);
389 fputs (conflict_report (src_total
, rrc_total
), stderr
);
392 if (expected_conflicts
!= -1 && !src_ok
)
394 complain_message_count
++;
395 fprintf (stderr
, ngettext ("expected %d shift/reduce conflict\n",
396 "expected %d shift/reduce conflicts\n",
404 print_reductions (FILE *out
, state_t
*state
)
407 shifts
*shiftp
= state
->shifts
;
408 errs
*errp
= state
->errs
;
411 for (i
= 0; i
< tokensetsize
; i
++)
414 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
415 if (!SHIFT_IS_DISABLED (shiftp
, i
))
417 /* if this state has a shift for the error token, don't use a
419 if (SHIFT_IS_ERROR (shiftp
, i
))
421 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
425 for (i
= 0; i
< errp
->nerrs
; i
++)
427 SETBIT (shiftset
, errp
->errs
[i
]);
429 if (state
->nlookaheads
== 1 && !nodefault
)
432 int default_rule
= LAruleno
[state
->lookaheadsp
];
434 for (k
= 0; k
< tokensetsize
; ++k
)
435 lookaheadset
[k
] = LA (state
->lookaheadsp
)[k
] & shiftset
[k
];
437 for (i
= 0; i
< ntokens
; i
++)
438 if (BITISSET (lookaheadset
, i
))
439 fprintf (out
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
440 tags
[i
], default_rule
,
441 tags
[rule_table
[default_rule
].lhs
]);
443 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
444 default_rule
, tags
[rule_table
[default_rule
].lhs
]);
446 else if (state
->nlookaheads
>= 1)
450 int default_rule
= 0;
453 for (i
= 0; i
< state
->nlookaheads
; ++i
)
458 for (k
= 0; k
< tokensetsize
; ++k
)
459 lookaheadset
[k
] = LA (state
->lookaheadsp
+ i
)[k
] & ~shiftset
[k
];
461 for (j
= 0; j
< ntokens
; j
++)
462 if (BITISSET (lookaheadset
, j
))
468 default_LA
= state
->lookaheadsp
+ i
;
469 default_rule
= LAruleno
[state
->lookaheadsp
+ i
];
472 for (k
= 0; k
< tokensetsize
; ++k
)
473 shiftset
[k
] |= lookaheadset
[k
];
476 for (i
= 0; i
< tokensetsize
; i
++)
479 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
480 if (!SHIFT_IS_DISABLED (shiftp
, i
))
481 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
483 for (i
= 0; i
< ntokens
; i
++)
487 int count
= BITISSET (shiftset
, i
);
489 for (j
= 0; j
< state
->nlookaheads
; ++j
)
491 if (BITISSET (LA (state
->lookaheadsp
+ j
), i
))
495 if (state
->lookaheadsp
+ j
!= default_LA
)
497 _(" %-4s\treduce using rule %d (%s)\n"),
499 LAruleno
[state
->lookaheadsp
+ j
],
500 tags
[rule_table
[LAruleno
[state
->lookaheadsp
+ j
]].lhs
]);
510 _(" %-4s\treduce using rule %d (%s)\n"),
512 LAruleno
[default_LA
],
513 tags
[rule_table
[LAruleno
[default_LA
]].lhs
]);
516 _(" %-4s\t[reduce using rule %d (%s)]\n"),
518 LAruleno
[state
->lookaheadsp
+ j
],
519 tags
[rule_table
[LAruleno
[state
->lookaheadsp
+ j
]].lhs
]);
526 fprintf (out
, _(" $default\treduce using rule %d (%s)\n"),
527 default_rule
, tags
[rule_table
[default_rule
].lhs
]);
533 free_conflicts (void)
537 XFREE (lookaheadset
);