]>
Commit | Line | Data |
---|---|---|
08089d5d | 1 | /* Find and resolve or report look-ahead conflicts for bison, |
22c821f3 | 2 | Copyright 1984, 1989, 1992, 2000, 2001 Free Software Foundation, Inc. |
08089d5d | 3 | |
ceed8467 | 4 | This file is part of Bison, the GNU Compiler Compiler. |
08089d5d | 5 | |
ceed8467 AD |
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) | |
9 | any later version. | |
08089d5d | 10 | |
ceed8467 AD |
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. | |
08089d5d | 15 | |
ceed8467 AD |
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 | |
19 | 02111-1307, USA. */ | |
08089d5d | 20 | |
08089d5d | 21 | #include "system.h" |
7da99ede | 22 | #include "complain.h" |
ceed8467 | 23 | #include "getargs.h" |
08089d5d DM |
24 | #include "files.h" |
25 | #include "gram.h" | |
26 | #include "state.h" | |
720d742f | 27 | #include "lalr.h" |
0619caf0 | 28 | #include "conflicts.h" |
b2ca4022 AD |
29 | #include "reader.h" |
30 | #include "LR0.h" | |
d2729d44 | 31 | |
342b8b6e | 32 | errs **err_table = NULL; |
7da99ede AD |
33 | /* -1 stands for not specified. */ |
34 | int expected_conflicts = -1; | |
342b8b6e | 35 | static char *conflicts = NULL; |
08089d5d | 36 | |
342b8b6e AD |
37 | static unsigned *shiftset = NULL; |
38 | static unsigned *lookaheadset = NULL; | |
c29240e7 | 39 | \f |
08089d5d | 40 | |
c29240e7 AD |
41 | static inline void |
42 | log_resolution (int state, int LAno, int token, char *resolution) | |
08089d5d | 43 | { |
ff4423cc AD |
44 | obstack_fgrow4 (&output_obstack, |
45 | _("\ | |
c29240e7 | 46 | Conflict in state %d between rule %d and token %s resolved as %s.\n"), |
ff4423cc | 47 | state, LAruleno[LAno], tags[token], resolution); |
08089d5d DM |
48 | } |
49 | ||
50 | ||
c29240e7 AD |
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 | `------------------------------------------------------------------*/ | |
56 | ||
4a120d45 | 57 | static void |
c29240e7 | 58 | flush_shift (int state, int token) |
08089d5d | 59 | { |
c29240e7 AD |
60 | shifts *shiftp; |
61 | int k, i; | |
08089d5d | 62 | |
90b4416b | 63 | shiftp = state_table[state].shift_table; |
c29240e7 | 64 | |
08089d5d DM |
65 | if (shiftp) |
66 | { | |
67 | k = shiftp->nshifts; | |
68 | for (i = 0; i < k; i++) | |
69 | { | |
c29240e7 | 70 | if (shiftp->shifts[i] |
9703cc49 | 71 | && token == state_table[shiftp->shifts[i]].accessing_symbol) |
c29240e7 | 72 | (shiftp->shifts[i]) = 0; |
08089d5d | 73 | } |
08089d5d DM |
74 | } |
75 | } | |
76 | ||
77 | ||
c29240e7 AD |
78 | /*------------------------------------------------------------------. |
79 | | Attempt to resolve shift-reduce conflict for one rule by means of | | |
80 | | precedence declarations. It has already been checked that the | | |
81 | | rule has a precedence. A conflict is resolved by modifying the | | |
82 | | shift or reduce tables so that there is no longer a conflict. | | |
83 | `------------------------------------------------------------------*/ | |
08089d5d | 84 | |
4a120d45 | 85 | static void |
d2729d44 | 86 | resolve_sr_conflict (int state, int lookaheadnum) |
08089d5d | 87 | { |
c29240e7 AD |
88 | int i; |
89 | int mask; | |
90 | unsigned *fp1; | |
91 | unsigned *fp2; | |
92 | int redprec; | |
f59c437a | 93 | errs *errp = ERRS_ALLOC (ntokens + 1); |
08089d5d DM |
94 | short *errtokens = errp->errs; |
95 | ||
96 | /* find the rule to reduce by to get precedence of reduction */ | |
652a871c | 97 | redprec = rule_table[LAruleno[lookaheadnum]].prec; |
08089d5d DM |
98 | |
99 | mask = 1; | |
bb527fc2 | 100 | fp1 = LA (lookaheadnum); |
08089d5d DM |
101 | fp2 = lookaheadset; |
102 | for (i = 0; i < ntokens; i++) | |
103 | { | |
104 | if ((mask & *fp2 & *fp1) && sprec[i]) | |
105 | /* Shift-reduce conflict occurs for token number i | |
106 | and it has a precedence. | |
107 | The precedence of shifting is that of token i. */ | |
108 | { | |
109 | if (sprec[i] < redprec) | |
110 | { | |
c29240e7 AD |
111 | log_resolution (state, lookaheadnum, i, _("reduce")); |
112 | *fp2 &= ~mask; /* flush the shift for this token */ | |
113 | flush_shift (state, i); | |
08089d5d DM |
114 | } |
115 | else if (sprec[i] > redprec) | |
116 | { | |
c29240e7 AD |
117 | log_resolution (state, lookaheadnum, i, _("shift")); |
118 | *fp1 &= ~mask; /* flush the reduce for this token */ | |
08089d5d DM |
119 | } |
120 | else | |
121 | { | |
122 | /* Matching precedence levels. | |
c29240e7 AD |
123 | For left association, keep only the reduction. |
124 | For right association, keep only the shift. | |
125 | For nonassociation, keep neither. */ | |
08089d5d DM |
126 | |
127 | switch (sassoc[i]) | |
128 | { | |
d7020c20 | 129 | case right_assoc: |
c29240e7 | 130 | log_resolution (state, lookaheadnum, i, _("shift")); |
08089d5d DM |
131 | break; |
132 | ||
d7020c20 | 133 | case left_assoc: |
c29240e7 | 134 | log_resolution (state, lookaheadnum, i, _("reduce")); |
08089d5d DM |
135 | break; |
136 | ||
d7020c20 | 137 | case non_assoc: |
c29240e7 | 138 | log_resolution (state, lookaheadnum, i, _("an error")); |
08089d5d DM |
139 | break; |
140 | } | |
141 | ||
d7020c20 | 142 | if (sassoc[i] != right_assoc) |
08089d5d | 143 | { |
c29240e7 AD |
144 | *fp2 &= ~mask; /* flush the shift for this token */ |
145 | flush_shift (state, i); | |
08089d5d | 146 | } |
d7020c20 | 147 | if (sassoc[i] != left_assoc) |
c29240e7 AD |
148 | { |
149 | *fp1 &= ~mask; /* flush the reduce for this token */ | |
08089d5d | 150 | } |
d7020c20 | 151 | if (sassoc[i] == non_assoc) |
08089d5d DM |
152 | { |
153 | /* Record an explicit error for this token. */ | |
154 | *errtokens++ = i; | |
155 | } | |
156 | } | |
157 | } | |
158 | ||
159 | mask <<= 1; | |
160 | if (mask == 0) | |
161 | { | |
162 | mask = 1; | |
c29240e7 AD |
163 | fp2++; |
164 | fp1++; | |
08089d5d DM |
165 | } |
166 | } | |
167 | errp->nerrs = errtokens - errp->errs; | |
168 | if (errp->nerrs) | |
169 | { | |
170 | /* Some tokens have been explicitly made errors. Allocate | |
c29240e7 | 171 | a permanent errs structure for this state, to record them. */ |
08089d5d | 172 | i = (char *) errtokens - (char *) errp; |
f59c437a | 173 | err_table[state] = ERRS_ALLOC (i + 1); |
08089d5d DM |
174 | bcopy (errp, err_table[state], i); |
175 | } | |
176 | else | |
177 | err_table[state] = 0; | |
c29240e7 | 178 | free (errp); |
08089d5d DM |
179 | } |
180 | ||
181 | ||
4a120d45 | 182 | static void |
c29240e7 | 183 | set_conflicts (int state) |
08089d5d | 184 | { |
d8cf039f | 185 | int i, j; |
c29240e7 | 186 | shifts *shiftp; |
c29240e7 AD |
187 | int symbol; |
188 | ||
de326cc0 | 189 | if (state_table[state].consistent) |
c29240e7 | 190 | return; |
08089d5d | 191 | |
c29240e7 AD |
192 | for (i = 0; i < tokensetsize; i++) |
193 | lookaheadset[i] = 0; | |
08089d5d | 194 | |
90b4416b | 195 | shiftp = state_table[state].shift_table; |
08089d5d | 196 | if (shiftp) |
d8cf039f AD |
197 | for (i = 0; i < shiftp->nshifts; i++) |
198 | { | |
199 | symbol = state_table[shiftp->shifts[i]].accessing_symbol; | |
200 | if (ISVAR (symbol)) | |
201 | break; | |
202 | SETBIT (lookaheadset, symbol); | |
203 | } | |
08089d5d | 204 | |
c29240e7 AD |
205 | /* Loop over all rules which require lookahead in this state. First |
206 | check for shift-reduce conflict, and try to resolve using | |
207 | precedence */ | |
d8cf039f AD |
208 | for (i = state_table[state].lookaheads; |
209 | i < state_table[state + 1].lookaheads; | |
210 | ++i) | |
652a871c | 211 | if (rule_table[LAruleno[i]].prec) |
d8cf039f AD |
212 | for (j = 0; j < tokensetsize; ++j) |
213 | if (LA (i)[j] & lookaheadset[j]) | |
c29240e7 | 214 | { |
d8cf039f AD |
215 | resolve_sr_conflict (state, i); |
216 | break; | |
c29240e7 | 217 | } |
08089d5d | 218 | |
08089d5d | 219 | |
c29240e7 AD |
220 | /* Loop over all rules which require lookahead in this state. Check |
221 | for conflicts not resolved above. */ | |
d8cf039f AD |
222 | for (i = state_table[state].lookaheads; |
223 | i < state_table[state + 1].lookaheads; | |
224 | ++i) | |
08089d5d | 225 | { |
d8cf039f AD |
226 | for (j = 0; j < tokensetsize; ++j) |
227 | if (LA (i)[j] & lookaheadset[j]) | |
0df87bb6 | 228 | conflicts[state] = 1; |
08089d5d | 229 | |
d8cf039f AD |
230 | for (j = 0; j < tokensetsize; ++j) |
231 | lookaheadset[j] |= LA (i)[j]; | |
c29240e7 AD |
232 | } |
233 | } | |
08089d5d DM |
234 | |
235 | void | |
342b8b6e | 236 | solve_conflicts (void) |
08089d5d | 237 | { |
c29240e7 | 238 | int i; |
08089d5d | 239 | |
d7913476 AD |
240 | conflicts = XCALLOC (char, nstates); |
241 | shiftset = XCALLOC (unsigned, tokensetsize); | |
242 | lookaheadset = XCALLOC (unsigned, tokensetsize); | |
08089d5d | 243 | |
d7913476 | 244 | err_table = XCALLOC (errs *, nstates); |
08089d5d | 245 | |
c29240e7 AD |
246 | for (i = 0; i < nstates; i++) |
247 | set_conflicts (i); | |
08089d5d DM |
248 | } |
249 | ||
250 | ||
c29240e7 AD |
251 | /*---------------------------------------------. |
252 | | Count the number of shift/reduce conflicts. | | |
253 | `---------------------------------------------*/ | |
254 | ||
0df87bb6 | 255 | static int |
d2729d44 | 256 | count_sr_conflicts (int state) |
08089d5d | 257 | { |
c29240e7 AD |
258 | int i; |
259 | int k; | |
260 | int mask; | |
261 | shifts *shiftp; | |
262 | unsigned *fp1; | |
263 | unsigned *fp2; | |
264 | unsigned *fp3; | |
265 | int symbol; | |
08089d5d | 266 | |
0df87bb6 | 267 | int src_count = 0; |
08089d5d | 268 | |
90b4416b | 269 | shiftp = state_table[state].shift_table; |
c29240e7 | 270 | if (!shiftp) |
0df87bb6 | 271 | return 0; |
08089d5d DM |
272 | |
273 | for (i = 0; i < tokensetsize; i++) | |
274 | { | |
275 | shiftset[i] = 0; | |
276 | lookaheadset[i] = 0; | |
277 | } | |
278 | ||
279 | k = shiftp->nshifts; | |
280 | for (i = 0; i < k; i++) | |
281 | { | |
c29240e7 AD |
282 | if (!shiftp->shifts[i]) |
283 | continue; | |
9703cc49 | 284 | symbol = state_table[shiftp->shifts[i]].accessing_symbol; |
c29240e7 AD |
285 | if (ISVAR (symbol)) |
286 | break; | |
287 | SETBIT (shiftset, symbol); | |
08089d5d DM |
288 | } |
289 | ||
f004bf6a | 290 | k = state_table[state + 1].lookaheads; |
08089d5d DM |
291 | fp3 = lookaheadset + tokensetsize; |
292 | ||
f004bf6a | 293 | for (i = state_table[state].lookaheads; i < k; i++) |
08089d5d | 294 | { |
bb527fc2 | 295 | fp1 = LA (i); |
08089d5d DM |
296 | fp2 = lookaheadset; |
297 | ||
298 | while (fp2 < fp3) | |
299 | *fp2++ |= *fp1++; | |
300 | } | |
301 | ||
302 | fp1 = shiftset; | |
303 | fp2 = lookaheadset; | |
304 | ||
305 | while (fp2 < fp3) | |
306 | *fp2++ &= *fp1++; | |
307 | ||
308 | mask = 1; | |
309 | fp2 = lookaheadset; | |
310 | for (i = 0; i < ntokens; i++) | |
311 | { | |
312 | if (mask & *fp2) | |
313 | src_count++; | |
314 | ||
315 | mask <<= 1; | |
316 | if (mask == 0) | |
317 | { | |
318 | mask = 1; | |
319 | fp2++; | |
320 | } | |
321 | } | |
0df87bb6 AD |
322 | |
323 | return src_count; | |
08089d5d DM |
324 | } |
325 | ||
326 | ||
c29240e7 AD |
327 | /*----------------------------------------------. |
328 | | Count the number of reduce/reduce conflicts. | | |
329 | `----------------------------------------------*/ | |
330 | ||
0df87bb6 | 331 | static int |
d2729d44 | 332 | count_rr_conflicts (int state) |
08089d5d | 333 | { |
c29240e7 | 334 | int i; |
c29240e7 AD |
335 | unsigned mask; |
336 | unsigned *baseword; | |
08089d5d | 337 | |
0df87bb6 | 338 | int rrc_count = 0; |
08089d5d | 339 | |
f004bf6a AD |
340 | int m = state_table[state].lookaheads; |
341 | int n = state_table[state + 1].lookaheads; | |
08089d5d | 342 | |
c29240e7 | 343 | if (n - m < 2) |
0df87bb6 | 344 | return 0; |
08089d5d DM |
345 | |
346 | mask = 1; | |
bb527fc2 | 347 | baseword = LA (m); |
08089d5d DM |
348 | for (i = 0; i < ntokens; i++) |
349 | { | |
0df87bb6 | 350 | unsigned *wordp = baseword; |
08089d5d | 351 | |
0df87bb6 AD |
352 | int count = 0; |
353 | int j; | |
08089d5d DM |
354 | for (j = m; j < n; j++) |
355 | { | |
356 | if (mask & *wordp) | |
357 | count++; | |
358 | ||
359 | wordp += tokensetsize; | |
360 | } | |
361 | ||
c29240e7 AD |
362 | if (count >= 2) |
363 | rrc_count++; | |
08089d5d DM |
364 | |
365 | mask <<= 1; | |
366 | if (mask == 0) | |
367 | { | |
368 | mask = 1; | |
369 | baseword++; | |
370 | } | |
371 | } | |
0df87bb6 AD |
372 | |
373 | return rrc_count; | |
08089d5d DM |
374 | } |
375 | ||
ff4423cc AD |
376 | /*--------------------------------------------------------------. |
377 | | Return a human readable string which reports shift/reduce and | | |
378 | | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). | | |
379 | `--------------------------------------------------------------*/ | |
c29240e7 | 380 | |
ff4423cc AD |
381 | static const char * |
382 | conflict_report (int src_num, int rrc_num) | |
c29240e7 | 383 | { |
ff4423cc AD |
384 | static char res[4096]; |
385 | char *cp = res; | |
386 | ||
7da99ede | 387 | if (src_num >= 1) |
22c821f3 | 388 | { |
7da99ede AD |
389 | sprintf (cp, ngettext ("%d shift/reduce conflict", |
390 | "%d shift/reduce conflicts", src_num), src_num); | |
22c821f3 AD |
391 | cp += strlen (cp); |
392 | } | |
c29240e7 | 393 | |
0619caf0 | 394 | if (src_num > 0 && rrc_num > 0) |
22c821f3 | 395 | { |
7da99ede | 396 | sprintf (cp, " %s ", _("and")); |
22c821f3 AD |
397 | cp += strlen (cp); |
398 | } | |
c29240e7 | 399 | |
7da99ede | 400 | if (rrc_num >= 1) |
22c821f3 | 401 | { |
7da99ede AD |
402 | sprintf (cp, ngettext ("%d reduce/reduce conflict", |
403 | "%d reduce/reduce conflicts", rrc_num), rrc_num); | |
22c821f3 AD |
404 | cp += strlen (cp); |
405 | } | |
ff4423cc AD |
406 | |
407 | *cp++ = '.'; | |
408 | *cp++ = '\n'; | |
409 | *cp++ = '\0'; | |
c29240e7 | 410 | |
ff4423cc | 411 | return res; |
c29240e7 AD |
412 | } |
413 | ||
414 | ||
0df87bb6 AD |
415 | /*-----------------------------------------------------------. |
416 | | Output the detailed description of states with conflicts. | | |
417 | `-----------------------------------------------------------*/ | |
c29240e7 AD |
418 | |
419 | void | |
0df87bb6 | 420 | conflicts_output (FILE *out) |
c29240e7 AD |
421 | { |
422 | int i; | |
0df87bb6 AD |
423 | for (i = 0; i < nstates; i++) |
424 | if (conflicts[i]) | |
425 | { | |
7da99ede | 426 | fprintf (out, _("State %d contains "), i); |
0df87bb6 AD |
427 | fputs (conflict_report (count_sr_conflicts (i), |
428 | count_rr_conflicts (i)), out); | |
429 | } | |
430 | } | |
c29240e7 | 431 | |
c29240e7 | 432 | |
0df87bb6 AD |
433 | /*------------------------------------------. |
434 | | Reporting the total number of conflicts. | | |
435 | `------------------------------------------*/ | |
0619caf0 | 436 | |
0df87bb6 AD |
437 | void |
438 | conflicts_print (void) | |
439 | { | |
440 | int i; | |
441 | ||
a034c8b8 AD |
442 | /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is |
443 | not set, and then we want 0 SR, or else it is specified, in which | |
444 | case we want equality. */ | |
445 | int src_ok = 0; | |
446 | ||
0df87bb6 AD |
447 | int src_total = 0; |
448 | int rrc_total = 0; | |
449 | ||
450 | /* Conflicts by state. */ | |
451 | for (i = 0; i < nstates; i++) | |
452 | if (conflicts[i]) | |
453 | { | |
454 | src_total += count_sr_conflicts (i); | |
455 | rrc_total += count_rr_conflicts (i); | |
456 | } | |
c29240e7 | 457 | |
a034c8b8 AD |
458 | src_ok = src_total == (expected_conflicts == -1 ? 0 : expected_conflicts); |
459 | ||
460 | /* If there are no RR conflicts, and as many SR conflicts as | |
461 | expected, then there is nothing to report. */ | |
462 | if (!rrc_total && src_ok) | |
463 | return; | |
464 | ||
0619caf0 | 465 | /* Report the total number of conflicts on STDERR. */ |
a034c8b8 | 466 | if (yacc_flag) |
09b503c8 | 467 | { |
a034c8b8 AD |
468 | /* If invoked with `--yacc', use the output format specified by |
469 | POSIX. */ | |
470 | fprintf (stderr, _("conflicts: ")); | |
471 | if (src_total > 0) | |
472 | fprintf (stderr, _(" %d shift/reduce"), src_total); | |
473 | if (src_total > 0 && rrc_total > 0) | |
474 | fprintf (stderr, ","); | |
475 | if (rrc_total > 0) | |
476 | fprintf (stderr, _(" %d reduce/reduce"), rrc_total); | |
477 | putc ('\n', stderr); | |
478 | } | |
479 | else | |
480 | { | |
481 | fprintf (stderr, _("%s contains "), infile); | |
482 | fputs (conflict_report (src_total, rrc_total), stderr); | |
09b503c8 | 483 | } |
7da99ede | 484 | |
a034c8b8 | 485 | if (expected_conflicts != -1 && !src_ok) |
7da99ede AD |
486 | { |
487 | complain_message_count++; | |
81e895c0 AD |
488 | fprintf (stderr, ngettext ("expected %d shift/reduce conflict\n", |
489 | "expected %d shift/reduce conflicts\n", | |
7da99ede AD |
490 | expected_conflicts), |
491 | expected_conflicts); | |
492 | } | |
c29240e7 AD |
493 | } |
494 | ||
495 | ||
08089d5d | 496 | void |
c73a41af | 497 | print_reductions (FILE *out, int state) |
08089d5d | 498 | { |
c29240e7 AD |
499 | int i; |
500 | int j; | |
501 | int k; | |
502 | unsigned *fp1; | |
503 | unsigned *fp2; | |
504 | unsigned *fp3; | |
505 | unsigned *fp4; | |
506 | int rule; | |
507 | int symbol; | |
508 | unsigned mask; | |
509 | int m; | |
510 | int n; | |
511 | int default_LA; | |
512 | int default_rule = 0; | |
513 | int cmax; | |
514 | int count; | |
515 | shifts *shiftp; | |
516 | errs *errp; | |
08089d5d DM |
517 | int nodefault = 0; |
518 | ||
519 | for (i = 0; i < tokensetsize; i++) | |
520 | shiftset[i] = 0; | |
521 | ||
90b4416b | 522 | shiftp = state_table[state].shift_table; |
08089d5d DM |
523 | if (shiftp) |
524 | { | |
525 | k = shiftp->nshifts; | |
526 | for (i = 0; i < k; i++) | |
527 | { | |
c29240e7 AD |
528 | if (!shiftp->shifts[i]) |
529 | continue; | |
9703cc49 | 530 | symbol = state_table[shiftp->shifts[i]].accessing_symbol; |
c29240e7 AD |
531 | if (ISVAR (symbol)) |
532 | break; | |
08089d5d DM |
533 | /* if this state has a shift for the error token, |
534 | don't use a default rule. */ | |
c29240e7 AD |
535 | if (symbol == error_token_number) |
536 | nodefault = 1; | |
537 | SETBIT (shiftset, symbol); | |
08089d5d DM |
538 | } |
539 | } | |
540 | ||
541 | errp = err_table[state]; | |
542 | if (errp) | |
543 | { | |
544 | k = errp->nerrs; | |
545 | for (i = 0; i < k; i++) | |
546 | { | |
c29240e7 AD |
547 | if (!errp->errs[i]) |
548 | continue; | |
08089d5d | 549 | symbol = errp->errs[i]; |
c29240e7 | 550 | SETBIT (shiftset, symbol); |
08089d5d DM |
551 | } |
552 | } | |
553 | ||
f004bf6a AD |
554 | m = state_table[state].lookaheads; |
555 | n = state_table[state + 1].lookaheads; | |
08089d5d | 556 | |
c29240e7 | 557 | if (n - m == 1 && !nodefault) |
08089d5d DM |
558 | { |
559 | default_rule = LAruleno[m]; | |
560 | ||
bb527fc2 | 561 | fp1 = LA (m); |
08089d5d DM |
562 | fp2 = shiftset; |
563 | fp3 = lookaheadset; | |
564 | fp4 = lookaheadset + tokensetsize; | |
565 | ||
566 | while (fp3 < fp4) | |
567 | *fp3++ = *fp1++ & *fp2++; | |
568 | ||
569 | mask = 1; | |
570 | fp3 = lookaheadset; | |
571 | ||
572 | for (i = 0; i < ntokens; i++) | |
573 | { | |
574 | if (mask & *fp3) | |
c73a41af | 575 | fprintf (out, _(" %-4s\t[reduce using rule %d (%s)]\n"), |
b2ed6e58 AD |
576 | tags[i], default_rule, |
577 | tags[rule_table[default_rule].lhs]); | |
08089d5d DM |
578 | |
579 | mask <<= 1; | |
580 | if (mask == 0) | |
581 | { | |
582 | mask = 1; | |
583 | fp3++; | |
584 | } | |
585 | } | |
586 | ||
c73a41af | 587 | fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"), |
b2ed6e58 | 588 | default_rule, tags[rule_table[default_rule].lhs]); |
08089d5d DM |
589 | } |
590 | else if (n - m >= 1) | |
591 | { | |
592 | cmax = 0; | |
593 | default_LA = -1; | |
594 | fp4 = lookaheadset + tokensetsize; | |
595 | ||
c29240e7 | 596 | if (!nodefault) |
08089d5d DM |
597 | for (i = m; i < n; i++) |
598 | { | |
bb527fc2 | 599 | fp1 = LA (i); |
08089d5d DM |
600 | fp2 = shiftset; |
601 | fp3 = lookaheadset; | |
ceed8467 | 602 | |
08089d5d | 603 | while (fp3 < fp4) |
b65534e5 | 604 | *fp3++ = *fp1++ & (~(*fp2++)); |
ceed8467 | 605 | |
08089d5d DM |
606 | count = 0; |
607 | mask = 1; | |
608 | fp3 = lookaheadset; | |
609 | for (j = 0; j < ntokens; j++) | |
610 | { | |
611 | if (mask & *fp3) | |
612 | count++; | |
ceed8467 | 613 | |
08089d5d DM |
614 | mask <<= 1; |
615 | if (mask == 0) | |
616 | { | |
617 | mask = 1; | |
618 | fp3++; | |
619 | } | |
620 | } | |
ceed8467 | 621 | |
08089d5d DM |
622 | if (count > cmax) |
623 | { | |
624 | cmax = count; | |
625 | default_LA = i; | |
626 | default_rule = LAruleno[i]; | |
627 | } | |
ceed8467 | 628 | |
08089d5d DM |
629 | fp2 = shiftset; |
630 | fp3 = lookaheadset; | |
ceed8467 | 631 | |
08089d5d DM |
632 | while (fp3 < fp4) |
633 | *fp2++ |= *fp3++; | |
634 | } | |
635 | ||
636 | for (i = 0; i < tokensetsize; i++) | |
c29240e7 | 637 | shiftset[i] = 0; |
08089d5d DM |
638 | |
639 | if (shiftp) | |
c29240e7 AD |
640 | { |
641 | k = shiftp->nshifts; | |
642 | for (i = 0; i < k; i++) | |
08089d5d | 643 | { |
c29240e7 AD |
644 | if (!shiftp->shifts[i]) |
645 | continue; | |
9703cc49 | 646 | symbol = state_table[shiftp->shifts[i]].accessing_symbol; |
c29240e7 AD |
647 | if (ISVAR (symbol)) |
648 | break; | |
649 | SETBIT (shiftset, symbol); | |
08089d5d | 650 | } |
c29240e7 | 651 | } |
08089d5d DM |
652 | |
653 | mask = 1; | |
bb527fc2 | 654 | fp1 = LA (m); |
08089d5d DM |
655 | fp2 = shiftset; |
656 | for (i = 0; i < ntokens; i++) | |
657 | { | |
658 | int defaulted = 0; | |
659 | ||
660 | if (mask & *fp2) | |
661 | count = 1; | |
662 | else | |
663 | count = 0; | |
664 | ||
665 | fp3 = fp1; | |
666 | for (j = m; j < n; j++) | |
667 | { | |
668 | if (mask & *fp3) | |
669 | { | |
670 | if (count == 0) | |
671 | { | |
672 | if (j != default_LA) | |
673 | { | |
674 | rule = LAruleno[j]; | |
c73a41af | 675 | fprintf (out, |
c29240e7 | 676 | _(" %-4s\treduce using rule %d (%s)\n"), |
b2ed6e58 | 677 | tags[i], rule, tags[rule_table[rule].lhs]); |
08089d5d | 678 | } |
c29240e7 AD |
679 | else |
680 | defaulted = 1; | |
08089d5d DM |
681 | |
682 | count++; | |
683 | } | |
684 | else | |
685 | { | |
686 | if (defaulted) | |
687 | { | |
688 | rule = LAruleno[default_LA]; | |
c73a41af | 689 | fprintf (out, |
c29240e7 | 690 | _(" %-4s\treduce using rule %d (%s)\n"), |
b2ed6e58 | 691 | tags[i], rule, tags[rule_table[rule].lhs]); |
08089d5d DM |
692 | defaulted = 0; |
693 | } | |
694 | rule = LAruleno[j]; | |
c73a41af | 695 | fprintf (out, |
c29240e7 | 696 | _(" %-4s\t[reduce using rule %d (%s)]\n"), |
b2ed6e58 | 697 | tags[i], rule, tags[rule_table[rule].lhs]); |
08089d5d DM |
698 | } |
699 | } | |
700 | ||
701 | fp3 += tokensetsize; | |
702 | } | |
703 | ||
704 | mask <<= 1; | |
705 | if (mask == 0) | |
706 | { | |
707 | mask = 1; | |
808e2021 | 708 | /* We tried incrementing just fp1, and just fp2; both seem wrong. |
c29240e7 | 709 | It seems necessary to increment both in sync. */ |
808e2021 | 710 | fp1++; |
08089d5d DM |
711 | fp2++; |
712 | } | |
713 | } | |
714 | ||
715 | if (default_LA >= 0) | |
c73a41af | 716 | fprintf (out, _(" $default\treduce using rule %d (%s)\n"), |
b2ed6e58 | 717 | default_rule, tags[rule_table[default_rule].lhs]); |
08089d5d DM |
718 | } |
719 | } | |
720 | ||
721 | ||
722 | void | |
342b8b6e | 723 | free_conflicts (void) |
08089d5d | 724 | { |
d7913476 AD |
725 | XFREE (conflicts); |
726 | XFREE (shiftset); | |
727 | XFREE (lookaheadset); | |
08089d5d | 728 | } |