]>
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 (int state
, int LAno
, int token
, char *resolution
)
43 obstack_fgrow4 (&output_obstack
,
45 Conflict in state %d between rule %d and token %s resolved as %s.\n"),
46 state
, LAruleno
[LAno
], tags
[token
], resolution
);
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 `------------------------------------------------------------------*/
57 flush_shift (int state
, int token
)
59 shifts
*shiftp
= state_table
[state
].shifts
;
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
);
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 `------------------------------------------------------------------*/
76 resolve_sr_conflict (int state
, int lookaheadnum
)
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
;
84 for (i
= 0; i
< ntokens
; i
++)
85 if (BITISSET (LA (lookaheadnum
), i
)
86 && BITISSET (lookaheadset
, 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. */
92 if (sprec
[i
] < redprec
)
94 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
95 /* flush the shift for this token */
96 RESETBIT (lookaheadset
, i
);
97 flush_shift (state
, i
);
99 else if (sprec
[i
] > redprec
)
101 log_resolution (state
, lookaheadnum
, i
, _("shift"));
102 /* flush the reduce for this token */
103 RESETBIT (LA (lookaheadnum
), i
);
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. */
115 log_resolution (state
, lookaheadnum
, i
, _("shift"));
119 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
123 log_resolution (state
, lookaheadnum
, i
, _("an error"));
127 if (sassoc
[i
] != right_assoc
)
129 /* flush the shift for this token */
130 RESETBIT (lookaheadset
, i
);
131 flush_shift (state
, i
);
133 if (sassoc
[i
] != left_assoc
)
135 /* flush the reduce for this token */
136 RESETBIT (LA (lookaheadnum
), i
);
138 if (sassoc
[i
] == non_assoc
)
140 /* Record an explicit error for this token. */
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
);
157 set_conflicts (int state
)
162 if (state_table
[state
].consistent
)
165 for (i
= 0; i
< tokensetsize
; i
++)
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
));
173 /* Loop over all rules which require lookahead in this state. First
174 check for shift-reduce conflict, and try to resolve using
176 for (i
= state_table
[state
].lookaheads
;
177 i
< state_table
[state
+ 1].lookaheads
;
179 if (rule_table
[LAruleno
[i
]].prec
)
180 for (j
= 0; j
< tokensetsize
; ++j
)
181 if (LA (i
)[j
] & lookaheadset
[j
])
183 resolve_sr_conflict (state
, i
);
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
;
194 for (j
= 0; j
< tokensetsize
; ++j
)
195 if (LA (i
)[j
] & lookaheadset
[j
])
196 conflicts
[state
] = 1;
198 for (j
= 0; j
< tokensetsize
; ++j
)
199 lookaheadset
[j
] |= LA (i
)[j
];
204 solve_conflicts (void)
208 conflicts
= XCALLOC (char, nstates
);
209 shiftset
= XCALLOC (unsigned, tokensetsize
);
210 lookaheadset
= XCALLOC (unsigned, tokensetsize
);
212 for (i
= 0; i
< nstates
; i
++)
217 /*---------------------------------------------.
218 | Count the number of shift/reduce conflicts. |
219 `---------------------------------------------*/
222 count_sr_conflicts (int state
)
226 shifts
*shiftp
= state_table
[state
].shifts
;
231 for (i
= 0; i
< tokensetsize
; i
++)
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
));
241 for (i
= state_table
[state
].lookaheads
;
242 i
< state_table
[state
+ 1].lookaheads
;
244 for (k
= 0; k
< tokensetsize
; ++k
)
245 lookaheadset
[k
] |= LA (i
)[k
];
247 for (k
= 0; k
< tokensetsize
; ++k
)
248 lookaheadset
[k
] &= shiftset
[k
];
250 for (i
= 0; i
< ntokens
; i
++)
251 if (BITISSET (lookaheadset
, i
))
258 /*----------------------------------------------.
259 | Count the number of reduce/reduce conflicts. |
260 `----------------------------------------------*/
263 count_rr_conflicts (int state
)
268 int m
= state_table
[state
].lookaheads
;
269 int n
= state_table
[state
+ 1].lookaheads
;
274 for (i
= 0; i
< ntokens
; i
++)
278 for (j
= m
; j
< n
; j
++)
279 if (BITISSET (LA (m
), j
))
289 /*--------------------------------------------------------------.
290 | Return a human readable string which reports shift/reduce and |
291 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
292 `--------------------------------------------------------------*/
295 conflict_report (int src_num
, int rrc_num
)
297 static char res
[4096];
302 sprintf (cp
, ngettext ("%d shift/reduce conflict",
303 "%d shift/reduce conflicts", src_num
), src_num
);
307 if (src_num
> 0 && rrc_num
> 0)
309 sprintf (cp
, " %s ", _("and"));
315 sprintf (cp
, ngettext ("%d reduce/reduce conflict",
316 "%d reduce/reduce conflicts", rrc_num
), rrc_num
);
328 /*-----------------------------------------------------------.
329 | Output the detailed description of states with conflicts. |
330 `-----------------------------------------------------------*/
333 conflicts_output (FILE *out
)
335 bool printed_sth
= FALSE
;
337 for (i
= 0; i
< nstates
; i
++)
340 fprintf (out
, _("State %d contains "), i
);
341 fputs (conflict_report (count_sr_conflicts (i
),
342 count_rr_conflicts (i
)), out
);
350 /*------------------------------------------.
351 | Reporting the total number of conflicts. |
352 `------------------------------------------*/
355 conflicts_print (void)
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. */
367 /* Conflicts by state. */
368 for (i
= 0; i
< nstates
; i
++)
371 src_total
+= count_sr_conflicts (i
);
372 rrc_total
+= count_rr_conflicts (i
);
375 src_ok
= src_total
== (expected_conflicts
== -1 ? 0 : expected_conflicts
);
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
)
382 /* Report the total number of conflicts on STDERR. */
385 /* If invoked with `--yacc', use the output format specified by
387 fprintf (stderr
, _("conflicts: "));
389 fprintf (stderr
, _(" %d shift/reduce"), src_total
);
390 if (src_total
> 0 && rrc_total
> 0)
391 fprintf (stderr
, ",");
393 fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
);
398 fprintf (stderr
, _("%s contains "), infile
);
399 fputs (conflict_report (src_total
, rrc_total
), stderr
);
402 if (expected_conflicts
!= -1 && !src_ok
)
404 complain_message_count
++;
405 fprintf (stderr
, ngettext ("expected %d shift/reduce conflict\n",
406 "expected %d shift/reduce conflicts\n",
414 print_reductions (FILE *out
, int state
)
424 for (i
= 0; i
< tokensetsize
; i
++)
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
))
431 /* if this state has a shift for the error token, don't use a
433 if (SHIFT_IS_ERROR (shiftp
, i
))
435 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
438 errp
= state_table
[state
].errs
;
440 for (i
= 0; i
< errp
->nerrs
; i
++)
442 SETBIT (shiftset
, errp
->errs
[i
]);
444 m
= state_table
[state
].lookaheads
;
445 n
= state_table
[state
+ 1].lookaheads
;
447 if (n
- m
== 1 && !nodefault
)
450 int default_rule
= LAruleno
[m
];
452 for (k
= 0; k
< tokensetsize
; ++k
)
453 lookaheadset
[k
] = LA (m
)[k
] & shiftset
[k
];
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
]);
461 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
462 default_rule
, tags
[rule_table
[default_rule
].lhs
]);
470 int default_rule
= 0;
473 for (i
= m
; i
< n
; i
++)
477 for (k
= 0; k
< tokensetsize
; ++k
)
478 lookaheadset
[k
] = LA (i
)[k
] & ~shiftset
[k
];
480 for (j
= 0; j
< ntokens
; j
++)
481 if (BITISSET (lookaheadset
, j
))
488 default_rule
= LAruleno
[i
];
491 for (k
= 0; k
< tokensetsize
; ++k
)
492 shiftset
[k
] |= lookaheadset
[k
];
495 for (i
= 0; i
< tokensetsize
; i
++)
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
));
502 for (i
= 0; i
< ntokens
; i
++)
505 int count
= BITISSET (shiftset
, i
);
507 for (j
= m
; j
< n
; j
++)
509 if (BITISSET (LA (m
), j
))
515 _(" %-4s\treduce using rule %d (%s)\n"),
518 tags
[rule_table
[LAruleno
[j
]].lhs
]);
528 _(" %-4s\treduce using rule %d (%s)\n"),
530 LAruleno
[default_LA
],
531 tags
[rule_table
[LAruleno
[default_LA
]].lhs
]);
534 _(" %-4s\t[reduce using rule %d (%s)]\n"),
537 tags
[rule_table
[LAruleno
[j
]].lhs
]);
544 fprintf (out
, _(" $default\treduce using rule %d (%s)\n"),
545 default_rule
, tags
[rule_table
[default_rule
].lhs
]);
551 free_conflicts (void)
555 XFREE (lookaheadset
);