]>
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, 2002 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
30 #include "conflicts.h"
34 /* -1 stands for not specified. */
35 int expected_conflicts
= -1;
36 static char *conflicts
= NULL
;
38 static bitset shiftset
;
39 static bitset lookaheadset
;
43 log_resolution (state_t
*state
, int LAno
, int token
, char *resolution
)
46 obstack_fgrow4 (&output_obstack
,
48 Conflict in state %d between rule %d and token %s resolved as %s.\n"),
49 state
->number
, LAruleno
[LAno
], symbols
[token
]->tag
,
54 /*------------------------------------------------------------------.
55 | Turn off the shift recorded for the specified token in the |
56 | specified state. Used when we resolve a shift-reduce conflict in |
57 | favor of the reduction. |
58 `------------------------------------------------------------------*/
61 flush_shift (state_t
*state
, int token
)
63 shifts
*shiftp
= state
->shifts
;
66 bitset_reset (lookaheadset
, token
);
67 for (i
= 0; i
< shiftp
->nshifts
; i
++)
68 if (!SHIFT_IS_DISABLED (shiftp
, i
) && SHIFT_SYMBOL (shiftp
, i
) == token
)
69 SHIFT_DISABLE (shiftp
, i
);
73 /*-------------------------------------------------------------------.
74 | Turn off the reduce recorded for the specified token for the |
75 | specified lookahead. Used when we resolve a shift-reduce conflict |
76 | in favor of the shift. |
77 `-------------------------------------------------------------------*/
80 flush_reduce (int lookahead
, int token
)
82 bitset_reset (LA
[lookahead
], token
);
86 /*------------------------------------------------------------------.
87 | Attempt to resolve shift-reduce conflict for one rule by means of |
88 | precedence declarations. It has already been checked that the |
89 | rule has a precedence. A conflict is resolved by modifying the |
90 | shift or reduce tables so that there is no longer a conflict. |
91 `------------------------------------------------------------------*/
94 resolve_sr_conflict (state_t
*state
, int lookahead
)
97 /* find the rule to reduce by to get precedence of reduction */
98 int redprec
= rules
[LAruleno
[lookahead
]].prec
;
99 errs
*errp
= errs_new (ntokens
+ 1);
102 for (i
= 0; i
< ntokens
; i
++)
103 if (bitset_test (LA
[lookahead
], i
)
104 && bitset_test (lookaheadset
, i
)
107 /* Shift-reduce conflict occurs for token number i
108 and it has a precedence.
109 The precedence of shifting is that of token i. */
110 if (symbols
[i
]->prec
< redprec
)
112 log_resolution (state
, lookahead
, i
, _("reduce"));
113 flush_shift (state
, i
);
115 else if (symbols
[i
]->prec
> redprec
)
117 log_resolution (state
, lookahead
, i
, _("shift"));
118 flush_reduce (lookahead
, i
);
121 /* Matching precedence levels.
122 For left association, keep only the reduction.
123 For right association, keep only the shift.
124 For nonassociation, keep neither. */
126 switch (symbols
[i
]->assoc
)
129 log_resolution (state
, lookahead
, i
, _("shift"));
130 flush_reduce (lookahead
, i
);
134 log_resolution (state
, lookahead
, i
, _("reduce"));
135 flush_shift (state
, i
);
139 log_resolution (state
, lookahead
, i
, _("an error"));
140 flush_shift (state
, i
);
141 flush_reduce (lookahead
, i
);
142 /* Record an explicit error for this token. */
143 errp
->errs
[errp
->nerrs
++] = i
;
148 /* Some tokens have been explicitly made errors. Allocate a
149 permanent errs structure for this state, to record them. */
150 state
->errs
= errs_dup (errp
);
156 set_conflicts (state_t
*state
)
161 if (state
->consistent
)
164 bitset_zero (lookaheadset
);
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 bitset_set (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 && !bitset_disjoint_p (LA
[state
->lookaheadsp
+ i
], lookaheadset
))
178 resolve_sr_conflict (state
, state
->lookaheadsp
+ i
);
182 /* Loop over all rules which require lookahead in this state. Check
183 for conflicts not resolved above. */
184 for (i
= 0; i
< state
->nlookaheads
; ++i
)
186 if (!bitset_disjoint_p (LA
[state
->lookaheadsp
+ i
], lookaheadset
))
187 conflicts
[state
->number
] = 1;
189 bitset_or (lookaheadset
, lookaheadset
, LA
[state
->lookaheadsp
+ i
]);
194 solve_conflicts (void)
198 conflicts
= XCALLOC (char, nstates
);
199 shiftset
= bitset_create (ntokens
, BITSET_FIXED
);
200 lookaheadset
= bitset_create (ntokens
, BITSET_FIXED
);
202 for (i
= 0; i
< nstates
; i
++)
203 set_conflicts (states
[i
]);
207 /*---------------------------------------------.
208 | Count the number of shift/reduce conflicts. |
209 `---------------------------------------------*/
212 count_sr_conflicts (state_t
*state
)
216 shifts
*shiftp
= state
->shifts
;
221 bitset_zero (lookaheadset
);
222 bitset_zero (shiftset
);
224 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
225 if (!SHIFT_IS_DISABLED (shiftp
, i
))
226 bitset_set (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
228 for (i
= 0; i
< state
->nlookaheads
; ++i
)
229 bitset_or (lookaheadset
, lookaheadset
, LA
[state
->lookaheadsp
+ i
]);
231 bitset_and (lookaheadset
, lookaheadset
, shiftset
);
233 src_count
= bitset_count (lookaheadset
);
239 /*----------------------------------------------.
240 | Count the number of reduce/reduce conflicts. |
241 `----------------------------------------------*/
244 count_rr_conflicts (state_t
*state
)
249 if (state
->nlookaheads
< 2)
252 for (i
= 0; i
< ntokens
; i
++)
256 for (j
= 0; j
< state
->nlookaheads
; ++j
)
257 if (bitset_test (LA
[state
->lookaheadsp
+ j
], i
))
267 /*--------------------------------------------------------------.
268 | Return a human readable string which reports shift/reduce and |
269 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
270 `--------------------------------------------------------------*/
273 conflict_report (int src_num
, int rrc_num
)
275 static char res
[4096];
280 sprintf (cp
, ngettext ("%d shift/reduce conflict",
281 "%d shift/reduce conflicts", src_num
), src_num
);
285 if (src_num
> 0 && rrc_num
> 0)
287 sprintf (cp
, " %s ", _("and"));
293 sprintf (cp
, ngettext ("%d reduce/reduce conflict",
294 "%d reduce/reduce conflicts", rrc_num
), rrc_num
);
306 /*-----------------------------------------------------------.
307 | Output the detailed description of states with conflicts. |
308 `-----------------------------------------------------------*/
311 conflicts_output (FILE *out
)
313 bool printed_sth
= FALSE
;
315 for (i
= 0; i
< nstates
; i
++)
318 fprintf (out
, _("State %d contains "), i
);
319 fputs (conflict_report (count_sr_conflicts (states
[i
]),
320 count_rr_conflicts (states
[i
])), out
);
328 /*------------------------------------------.
329 | Reporting the total number of conflicts. |
330 `------------------------------------------*/
333 conflicts_print (void)
337 /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is
338 not set, and then we want 0 SR, or else it is specified, in which
339 case we want equality. */
345 /* Conflicts by state. */
346 for (i
= 0; i
< nstates
; i
++)
349 src_total
+= count_sr_conflicts (states
[i
]);
350 rrc_total
+= count_rr_conflicts (states
[i
]);
353 src_ok
= src_total
== (expected_conflicts
== -1 ? 0 : expected_conflicts
);
355 /* If there are no RR conflicts, and as many SR conflicts as
356 expected, then there is nothing to report. */
357 if (!rrc_total
&& src_ok
)
360 /* Report the total number of conflicts on STDERR. */
363 /* If invoked with `--yacc', use the output format specified by
365 fprintf (stderr
, _("conflicts: "));
367 fprintf (stderr
, _(" %d shift/reduce"), src_total
);
368 if (src_total
> 0 && rrc_total
> 0)
369 fprintf (stderr
, ",");
371 fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
);
376 fprintf (stderr
, _("%s contains "), infile
);
377 fputs (conflict_report (src_total
, rrc_total
), stderr
);
380 if (expected_conflicts
!= -1 && !src_ok
)
382 complain_message_count
++;
383 fprintf (stderr
, ngettext ("expected %d shift/reduce conflict\n",
384 "expected %d shift/reduce conflicts\n",
392 free_conflicts (void)
395 bitset_free (shiftset
);
396 bitset_free (lookaheadset
);