]>
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
)
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
]->shifts
;
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
++)
86 if (BITISSET (LA (lookaheadnum
), i
)
87 && BITISSET (lookaheadset
, i
)
89 /* Shift-reduce conflict occurs for token number i
90 and it has a precedence.
91 The precedence of shifting is that of token i. */
93 if (sprec
[i
] < redprec
)
95 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
96 /* flush the shift for this token */
97 RESETBIT (lookaheadset
, i
);
98 flush_shift (state
, i
);
100 else if (sprec
[i
] > redprec
)
102 log_resolution (state
, lookaheadnum
, i
, _("shift"));
103 /* flush the reduce for this token */
104 RESETBIT (LA (lookaheadnum
), i
);
108 /* Matching precedence levels.
109 For left association, keep only the reduction.
110 For right association, keep only the shift.
111 For nonassociation, keep neither. */
116 log_resolution (state
, lookaheadnum
, i
, _("shift"));
120 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
124 log_resolution (state
, lookaheadnum
, i
, _("an error"));
128 if (sassoc
[i
] != right_assoc
)
130 /* flush the shift for this token */
131 RESETBIT (lookaheadset
, i
);
132 flush_shift (state
, i
);
134 if (sassoc
[i
] != left_assoc
)
136 /* flush the reduce for this token */
137 RESETBIT (LA (lookaheadnum
), i
);
139 if (sassoc
[i
] == non_assoc
)
141 /* Record an explicit error for this token. */
147 errp
->nerrs
= errtokens
- errp
->errs
;
148 /* Some tokens have been explicitly made errors. Allocate a
149 permanent errs structure for this state, to record them. */
150 i
= (char *) errtokens
- (char *) errp
;
151 state_table
[state
]->errs
= ERRS_ALLOC (i
+ 1);
152 memcpy (state_table
[state
]->errs
, errp
, i
);
158 set_conflicts (int state
)
163 if (state_table
[state
]->consistent
)
166 for (i
= 0; i
< tokensetsize
; i
++)
169 shiftp
= state_table
[state
]->shifts
;
170 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
171 if (!SHIFT_IS_DISABLED (shiftp
, i
))
172 SETBIT (lookaheadset
, SHIFT_SYMBOL (shiftp
, i
));
174 /* Loop over all rules which require lookahead in this state. First
175 check for shift-reduce conflict, and try to resolve using
177 for (i
= state_table
[state
]->lookaheads
;
178 i
< state_table
[state
+ 1]->lookaheads
;
180 if (rule_table
[LAruleno
[i
]].prec
)
181 for (j
= 0; j
< tokensetsize
; ++j
)
182 if (LA (i
)[j
] & lookaheadset
[j
])
184 resolve_sr_conflict (state
, i
);
189 /* Loop over all rules which require lookahead in this state. Check
190 for conflicts not resolved above. */
191 for (i
= state_table
[state
]->lookaheads
;
192 i
< state_table
[state
+ 1]->lookaheads
;
195 for (j
= 0; j
< tokensetsize
; ++j
)
196 if (LA (i
)[j
] & lookaheadset
[j
])
197 conflicts
[state
] = 1;
199 for (j
= 0; j
< tokensetsize
; ++j
)
200 lookaheadset
[j
] |= LA (i
)[j
];
205 solve_conflicts (void)
209 conflicts
= XCALLOC (char, nstates
);
210 shiftset
= XCALLOC (unsigned, tokensetsize
);
211 lookaheadset
= XCALLOC (unsigned, tokensetsize
);
213 for (i
= 0; i
< nstates
; i
++)
218 /*---------------------------------------------.
219 | Count the number of shift/reduce conflicts. |
220 `---------------------------------------------*/
223 count_sr_conflicts (int state
)
227 shifts
*shiftp
= state_table
[state
]->shifts
;
232 for (i
= 0; i
< tokensetsize
; i
++)
238 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
239 if (!SHIFT_IS_DISABLED (shiftp
, i
))
240 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
242 for (i
= state_table
[state
]->lookaheads
;
243 i
< state_table
[state
+ 1]->lookaheads
;
245 for (k
= 0; k
< tokensetsize
; ++k
)
246 lookaheadset
[k
] |= LA (i
)[k
];
248 for (k
= 0; k
< tokensetsize
; ++k
)
249 lookaheadset
[k
] &= shiftset
[k
];
251 for (i
= 0; i
< ntokens
; i
++)
252 if (BITISSET (lookaheadset
, i
))
259 /*----------------------------------------------.
260 | Count the number of reduce/reduce conflicts. |
261 `----------------------------------------------*/
264 count_rr_conflicts (int state
)
269 int m
= state_table
[state
]->lookaheads
;
270 int n
= state_table
[state
+ 1]->lookaheads
;
275 for (i
= 0; i
< ntokens
; i
++)
279 for (j
= m
; j
< n
; j
++)
280 if (BITISSET (LA (m
), j
))
290 /*--------------------------------------------------------------.
291 | Return a human readable string which reports shift/reduce and |
292 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
293 `--------------------------------------------------------------*/
296 conflict_report (int src_num
, int rrc_num
)
298 static char res
[4096];
303 sprintf (cp
, ngettext ("%d shift/reduce conflict",
304 "%d shift/reduce conflicts", src_num
), src_num
);
308 if (src_num
> 0 && rrc_num
> 0)
310 sprintf (cp
, " %s ", _("and"));
316 sprintf (cp
, ngettext ("%d reduce/reduce conflict",
317 "%d reduce/reduce conflicts", rrc_num
), rrc_num
);
329 /*-----------------------------------------------------------.
330 | Output the detailed description of states with conflicts. |
331 `-----------------------------------------------------------*/
334 conflicts_output (FILE *out
)
336 bool printed_sth
= FALSE
;
338 for (i
= 0; i
< nstates
; i
++)
341 fprintf (out
, _("State %d contains "), i
);
342 fputs (conflict_report (count_sr_conflicts (i
),
343 count_rr_conflicts (i
)), out
);
351 /*------------------------------------------.
352 | Reporting the total number of conflicts. |
353 `------------------------------------------*/
356 conflicts_print (void)
360 /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is
361 not set, and then we want 0 SR, or else it is specified, in which
362 case we want equality. */
368 /* Conflicts by state. */
369 for (i
= 0; i
< nstates
; i
++)
372 src_total
+= count_sr_conflicts (i
);
373 rrc_total
+= count_rr_conflicts (i
);
376 src_ok
= src_total
== (expected_conflicts
== -1 ? 0 : expected_conflicts
);
378 /* If there are no RR conflicts, and as many SR conflicts as
379 expected, then there is nothing to report. */
380 if (!rrc_total
&& src_ok
)
383 /* Report the total number of conflicts on STDERR. */
386 /* If invoked with `--yacc', use the output format specified by
388 fprintf (stderr
, _("conflicts: "));
390 fprintf (stderr
, _(" %d shift/reduce"), src_total
);
391 if (src_total
> 0 && rrc_total
> 0)
392 fprintf (stderr
, ",");
394 fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
);
399 fprintf (stderr
, _("%s contains "), infile
);
400 fputs (conflict_report (src_total
, rrc_total
), stderr
);
403 if (expected_conflicts
!= -1 && !src_ok
)
405 complain_message_count
++;
406 fprintf (stderr
, ngettext ("expected %d shift/reduce conflict\n",
407 "expected %d shift/reduce conflicts\n",
415 print_reductions (FILE *out
, int state
)
425 for (i
= 0; i
< tokensetsize
; i
++)
428 shiftp
= state_table
[state
]->shifts
;
429 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
430 if (!SHIFT_IS_DISABLED (shiftp
, i
))
432 /* if this state has a shift for the error token, don't use a
434 if (SHIFT_IS_ERROR (shiftp
, i
))
436 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
439 errp
= state_table
[state
]->errs
;
441 for (i
= 0; i
< errp
->nerrs
; i
++)
443 SETBIT (shiftset
, errp
->errs
[i
]);
445 m
= state_table
[state
]->lookaheads
;
446 n
= state_table
[state
+ 1]->lookaheads
;
448 if (n
- m
== 1 && !nodefault
)
451 int default_rule
= LAruleno
[m
];
453 for (k
= 0; k
< tokensetsize
; ++k
)
454 lookaheadset
[k
] = LA (m
)[k
] & shiftset
[k
];
456 for (i
= 0; i
< ntokens
; i
++)
457 if (BITISSET (lookaheadset
, i
))
458 fprintf (out
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
459 tags
[i
], default_rule
,
460 tags
[rule_table
[default_rule
].lhs
]);
462 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
463 default_rule
, tags
[rule_table
[default_rule
].lhs
]);
471 int default_rule
= 0;
474 for (i
= m
; i
< n
; i
++)
478 for (k
= 0; k
< tokensetsize
; ++k
)
479 lookaheadset
[k
] = LA (i
)[k
] & ~shiftset
[k
];
481 for (j
= 0; j
< ntokens
; j
++)
482 if (BITISSET (lookaheadset
, j
))
489 default_rule
= LAruleno
[i
];
492 for (k
= 0; k
< tokensetsize
; ++k
)
493 shiftset
[k
] |= lookaheadset
[k
];
496 for (i
= 0; i
< tokensetsize
; i
++)
499 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
500 if (!SHIFT_IS_DISABLED (shiftp
, i
))
501 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
503 for (i
= 0; i
< ntokens
; i
++)
506 int count
= BITISSET (shiftset
, i
);
508 for (j
= m
; j
< n
; j
++)
510 if (BITISSET (LA (m
), j
))
516 _(" %-4s\treduce using rule %d (%s)\n"),
519 tags
[rule_table
[LAruleno
[j
]].lhs
]);
529 _(" %-4s\treduce using rule %d (%s)\n"),
531 LAruleno
[default_LA
],
532 tags
[rule_table
[LAruleno
[default_LA
]].lhs
]);
535 _(" %-4s\t[reduce using rule %d (%s)]\n"),
538 tags
[rule_table
[LAruleno
[j
]].lhs
]);
545 fprintf (out
, _(" $default\treduce using rule %d (%s)\n"),
546 default_rule
, tags
[rule_table
[default_rule
].lhs
]);
552 free_conflicts (void)
556 XFREE (lookaheadset
);