]>
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 errs
**err_table
= NULL
;
33 /* -1 stands for not specified. */
34 int expected_conflicts
= -1;
35 static char *conflicts
= NULL
;
37 static unsigned *shiftset
= NULL
;
38 static unsigned *lookaheadset
= NULL
;
42 log_resolution (int 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
, 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 (int state
, int token
)
60 shifts
*shiftp
= state_table
[state
].shift_table
;
63 for (i
= 0; i
< shiftp
->nshifts
; i
++)
64 if (!SHIFT_IS_DISABLED (shiftp
, i
) && SHIFT_SYMBOL (shiftp
, i
) == token
)
65 SHIFT_DISABLE (shiftp
, i
);
69 /*------------------------------------------------------------------.
70 | Attempt to resolve shift-reduce conflict for one rule by means of |
71 | precedence declarations. It has already been checked that the |
72 | rule has a precedence. A conflict is resolved by modifying the |
73 | shift or reduce tables so that there is no longer a conflict. |
74 `------------------------------------------------------------------*/
77 resolve_sr_conflict (int state
, int lookaheadnum
)
80 /* find the rule to reduce by to get precedence of reduction */
81 int redprec
= rule_table
[LAruleno
[lookaheadnum
]].prec
;
82 errs
*errp
= ERRS_ALLOC (ntokens
+ 1);
83 short *errtokens
= errp
->errs
;
85 for (i
= 0; i
< ntokens
; i
++)
87 if (BITISSET (LA (lookaheadnum
), i
)
88 && BITISSET (lookaheadset
, i
)
90 /* Shift-reduce conflict occurs for token number i
91 and it has a precedence.
92 The precedence of shifting is that of token i. */
94 if (sprec
[i
] < redprec
)
96 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
97 /* flush the shift for this token */
98 RESETBIT (lookaheadset
, i
);
99 flush_shift (state
, i
);
101 else if (sprec
[i
] > redprec
)
103 log_resolution (state
, lookaheadnum
, i
, _("shift"));
104 /* flush the reduce for this token */
105 RESETBIT (LA (lookaheadnum
), i
);
109 /* Matching precedence levels.
110 For left association, keep only the reduction.
111 For right association, keep only the shift.
112 For nonassociation, keep neither. */
117 log_resolution (state
, lookaheadnum
, i
, _("shift"));
121 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
125 log_resolution (state
, lookaheadnum
, i
, _("an error"));
129 if (sassoc
[i
] != right_assoc
)
131 /* flush the shift for this token */
132 RESETBIT (lookaheadset
, i
);
133 flush_shift (state
, i
);
135 if (sassoc
[i
] != left_assoc
)
137 /* flush the reduce for this token */
138 RESETBIT (LA (lookaheadnum
), i
);
140 if (sassoc
[i
] == non_assoc
)
142 /* Record an explicit error for this token. */
148 errp
->nerrs
= errtokens
- errp
->errs
;
151 /* Some tokens have been explicitly made errors. Allocate
152 a permanent errs structure for this state, to record them. */
153 i
= (char *) errtokens
- (char *) errp
;
154 err_table
[state
] = ERRS_ALLOC (i
+ 1);
155 bcopy (errp
, err_table
[state
], i
);
158 err_table
[state
] = 0;
164 set_conflicts (int state
)
169 if (state_table
[state
].consistent
)
172 for (i
= 0; i
< tokensetsize
; i
++)
175 shiftp
= state_table
[state
].shift_table
;
176 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
177 if (!SHIFT_IS_DISABLED (shiftp
, i
))
178 SETBIT (lookaheadset
, SHIFT_SYMBOL (shiftp
, i
));
180 /* Loop over all rules which require lookahead in this state. First
181 check for shift-reduce conflict, and try to resolve using
183 for (i
= state_table
[state
].lookaheads
;
184 i
< state_table
[state
+ 1].lookaheads
;
186 if (rule_table
[LAruleno
[i
]].prec
)
187 for (j
= 0; j
< tokensetsize
; ++j
)
188 if (LA (i
)[j
] & lookaheadset
[j
])
190 resolve_sr_conflict (state
, i
);
195 /* Loop over all rules which require lookahead in this state. Check
196 for conflicts not resolved above. */
197 for (i
= state_table
[state
].lookaheads
;
198 i
< state_table
[state
+ 1].lookaheads
;
201 for (j
= 0; j
< tokensetsize
; ++j
)
202 if (LA (i
)[j
] & lookaheadset
[j
])
203 conflicts
[state
] = 1;
205 for (j
= 0; j
< tokensetsize
; ++j
)
206 lookaheadset
[j
] |= LA (i
)[j
];
211 solve_conflicts (void)
215 conflicts
= XCALLOC (char, nstates
);
216 shiftset
= XCALLOC (unsigned, tokensetsize
);
217 lookaheadset
= XCALLOC (unsigned, tokensetsize
);
219 err_table
= XCALLOC (errs
*, nstates
);
221 for (i
= 0; i
< nstates
; i
++)
226 /*---------------------------------------------.
227 | Count the number of shift/reduce conflicts. |
228 `---------------------------------------------*/
231 count_sr_conflicts (int state
)
235 shifts
*shiftp
= state_table
[state
].shift_table
;
240 for (i
= 0; i
< tokensetsize
; i
++)
246 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
247 if (!SHIFT_IS_DISABLED (shiftp
, i
))
248 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
250 for (i
= state_table
[state
].lookaheads
;
251 i
< state_table
[state
+ 1].lookaheads
;
253 for (k
= 0; k
< tokensetsize
; ++k
)
254 lookaheadset
[k
] |= LA (i
)[k
];
256 for (k
= 0; k
< tokensetsize
; ++k
)
257 lookaheadset
[k
] &= shiftset
[k
];
259 for (i
= 0; i
< ntokens
; i
++)
260 if (BITISSET (lookaheadset
, i
))
267 /*----------------------------------------------.
268 | Count the number of reduce/reduce conflicts. |
269 `----------------------------------------------*/
272 count_rr_conflicts (int state
)
277 int m
= state_table
[state
].lookaheads
;
278 int n
= state_table
[state
+ 1].lookaheads
;
283 for (i
= 0; i
< ntokens
; i
++)
287 for (j
= m
; j
< n
; j
++)
288 if (BITISSET (LA (m
), j
))
298 /*--------------------------------------------------------------.
299 | Return a human readable string which reports shift/reduce and |
300 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
301 `--------------------------------------------------------------*/
304 conflict_report (int src_num
, int rrc_num
)
306 static char res
[4096];
311 sprintf (cp
, ngettext ("%d shift/reduce conflict",
312 "%d shift/reduce conflicts", src_num
), src_num
);
316 if (src_num
> 0 && rrc_num
> 0)
318 sprintf (cp
, " %s ", _("and"));
324 sprintf (cp
, ngettext ("%d reduce/reduce conflict",
325 "%d reduce/reduce conflicts", rrc_num
), rrc_num
);
337 /*-----------------------------------------------------------.
338 | Output the detailed description of states with conflicts. |
339 `-----------------------------------------------------------*/
342 conflicts_output (FILE *out
)
344 bool printed_sth
= FALSE
;
346 for (i
= 0; i
< nstates
; i
++)
349 fprintf (out
, _("State %d contains "), i
);
350 fputs (conflict_report (count_sr_conflicts (i
),
351 count_rr_conflicts (i
)), out
);
359 /*------------------------------------------.
360 | Reporting the total number of conflicts. |
361 `------------------------------------------*/
364 conflicts_print (void)
368 /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is
369 not set, and then we want 0 SR, or else it is specified, in which
370 case we want equality. */
376 /* Conflicts by state. */
377 for (i
= 0; i
< nstates
; i
++)
380 src_total
+= count_sr_conflicts (i
);
381 rrc_total
+= count_rr_conflicts (i
);
384 src_ok
= src_total
== (expected_conflicts
== -1 ? 0 : expected_conflicts
);
386 /* If there are no RR conflicts, and as many SR conflicts as
387 expected, then there is nothing to report. */
388 if (!rrc_total
&& src_ok
)
391 /* Report the total number of conflicts on STDERR. */
394 /* If invoked with `--yacc', use the output format specified by
396 fprintf (stderr
, _("conflicts: "));
398 fprintf (stderr
, _(" %d shift/reduce"), src_total
);
399 if (src_total
> 0 && rrc_total
> 0)
400 fprintf (stderr
, ",");
402 fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
);
407 fprintf (stderr
, _("%s contains "), infile
);
408 fputs (conflict_report (src_total
, rrc_total
), stderr
);
411 if (expected_conflicts
!= -1 && !src_ok
)
413 complain_message_count
++;
414 fprintf (stderr
, ngettext ("expected %d shift/reduce conflict\n",
415 "expected %d shift/reduce conflicts\n",
423 print_reductions (FILE *out
, int state
)
433 for (i
= 0; i
< tokensetsize
; i
++)
436 shiftp
= state_table
[state
].shift_table
;
437 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
438 if (!SHIFT_IS_DISABLED (shiftp
, i
))
440 /* if this state has a shift for the error token, don't use a
442 if (SHIFT_IS_ERROR (shiftp
, i
))
444 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
447 errp
= err_table
[state
];
449 for (i
= 0; i
< errp
->nerrs
; i
++)
451 SETBIT (shiftset
, errp
->errs
[i
]);
453 m
= state_table
[state
].lookaheads
;
454 n
= state_table
[state
+ 1].lookaheads
;
456 if (n
- m
== 1 && !nodefault
)
459 int default_rule
= LAruleno
[m
];
461 for (k
= 0; k
< tokensetsize
; ++k
)
462 lookaheadset
[k
] = LA (m
)[k
] & shiftset
[k
];
464 for (i
= 0; i
< ntokens
; i
++)
465 if (BITISSET (lookaheadset
, i
))
466 fprintf (out
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
467 tags
[i
], default_rule
,
468 tags
[rule_table
[default_rule
].lhs
]);
470 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
471 default_rule
, tags
[rule_table
[default_rule
].lhs
]);
479 int default_rule
= 0;
482 for (i
= m
; i
< n
; i
++)
486 for (k
= 0; k
< tokensetsize
; ++k
)
487 lookaheadset
[k
] = LA (i
)[k
] & ~shiftset
[k
];
489 for (j
= 0; j
< ntokens
; j
++)
490 if (BITISSET (lookaheadset
, j
))
497 default_rule
= LAruleno
[i
];
500 for (k
= 0; k
< tokensetsize
; ++k
)
501 shiftset
[k
] |= lookaheadset
[k
];
504 for (i
= 0; i
< tokensetsize
; i
++)
507 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
508 if (!SHIFT_IS_DISABLED (shiftp
, i
))
509 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
511 for (i
= 0; i
< ntokens
; i
++)
514 int count
= BITISSET (shiftset
, i
);
516 for (j
= m
; j
< n
; j
++)
518 if (BITISSET (LA (m
), j
))
524 _(" %-4s\treduce using rule %d (%s)\n"),
527 tags
[rule_table
[LAruleno
[j
]].lhs
]);
537 _(" %-4s\treduce using rule %d (%s)\n"),
539 LAruleno
[default_LA
],
540 tags
[rule_table
[LAruleno
[default_LA
]].lhs
]);
543 _(" %-4s\t[reduce using rule %d (%s)]\n"),
546 tags
[rule_table
[LAruleno
[j
]].lhs
]);
553 fprintf (out
, _(" $default\treduce using rule %d (%s)\n"),
554 default_rule
, tags
[rule_table
[default_rule
].lhs
]);
560 free_conflicts (void)
564 XFREE (lookaheadset
);