]>
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; | |
d7913476 | 93 | errs *errp = (errs *) xcalloc (sizeof (errs) + ntokens * sizeof (short), 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; |
d7913476 | 173 | err_table[state] = (errs *) xcalloc ((unsigned int) 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 | { |
c29240e7 AD |
185 | int i; |
186 | int k; | |
187 | shifts *shiftp; | |
188 | unsigned *fp2; | |
189 | unsigned *fp3; | |
190 | unsigned *fp4; | |
191 | unsigned *fp1; | |
192 | int symbol; | |
193 | ||
de326cc0 | 194 | if (state_table[state].consistent) |
c29240e7 | 195 | return; |
08089d5d | 196 | |
c29240e7 AD |
197 | for (i = 0; i < tokensetsize; i++) |
198 | lookaheadset[i] = 0; | |
08089d5d | 199 | |
90b4416b | 200 | shiftp = state_table[state].shift_table; |
08089d5d DM |
201 | if (shiftp) |
202 | { | |
203 | k = shiftp->nshifts; | |
204 | for (i = 0; i < k; i++) | |
205 | { | |
9703cc49 | 206 | symbol = state_table[shiftp->shifts[i]].accessing_symbol; |
c29240e7 AD |
207 | if (ISVAR (symbol)) |
208 | break; | |
209 | SETBIT (lookaheadset, symbol); | |
08089d5d DM |
210 | } |
211 | } | |
08089d5d | 212 | |
f004bf6a | 213 | k = state_table[state + 1].lookaheads; |
c29240e7 | 214 | fp4 = lookaheadset + tokensetsize; |
08089d5d | 215 | |
c29240e7 AD |
216 | /* Loop over all rules which require lookahead in this state. First |
217 | check for shift-reduce conflict, and try to resolve using | |
218 | precedence */ | |
f004bf6a | 219 | for (i = state_table[state].lookaheads; i < k; i++) |
652a871c | 220 | if (rule_table[LAruleno[i]].prec) |
c29240e7 | 221 | { |
bb527fc2 | 222 | fp1 = LA (i); |
c29240e7 AD |
223 | fp2 = fp1; |
224 | fp3 = lookaheadset; | |
08089d5d | 225 | |
c29240e7 AD |
226 | while (fp3 < fp4) |
227 | { | |
228 | if (*fp2++ & *fp3++) | |
229 | { | |
230 | resolve_sr_conflict (state, i); | |
231 | break; | |
232 | } | |
233 | } | |
234 | } | |
08089d5d | 235 | |
08089d5d | 236 | |
c29240e7 AD |
237 | /* Loop over all rules which require lookahead in this state. Check |
238 | for conflicts not resolved above. */ | |
f004bf6a | 239 | for (i = state_table[state].lookaheads; i < k; i++) |
08089d5d | 240 | { |
bb527fc2 | 241 | fp1 = LA (i); |
c29240e7 AD |
242 | fp2 = fp1; |
243 | fp3 = lookaheadset; | |
244 | ||
245 | while (fp3 < fp4) | |
0df87bb6 AD |
246 | if (*fp2++ & *fp3++) |
247 | conflicts[state] = 1; | |
08089d5d | 248 | |
c29240e7 AD |
249 | fp2 = fp1; |
250 | fp3 = lookaheadset; | |
ceed8467 | 251 | |
c29240e7 AD |
252 | while (fp3 < fp4) |
253 | *fp3++ |= *fp2++; | |
254 | } | |
255 | } | |
08089d5d DM |
256 | |
257 | void | |
342b8b6e | 258 | solve_conflicts (void) |
08089d5d | 259 | { |
c29240e7 | 260 | int i; |
08089d5d | 261 | |
d7913476 AD |
262 | conflicts = XCALLOC (char, nstates); |
263 | shiftset = XCALLOC (unsigned, tokensetsize); | |
264 | lookaheadset = XCALLOC (unsigned, tokensetsize); | |
08089d5d | 265 | |
d7913476 | 266 | err_table = XCALLOC (errs *, nstates); |
08089d5d | 267 | |
c29240e7 AD |
268 | for (i = 0; i < nstates; i++) |
269 | set_conflicts (i); | |
08089d5d DM |
270 | } |
271 | ||
272 | ||
c29240e7 AD |
273 | /*---------------------------------------------. |
274 | | Count the number of shift/reduce conflicts. | | |
275 | `---------------------------------------------*/ | |
276 | ||
0df87bb6 | 277 | static int |
d2729d44 | 278 | count_sr_conflicts (int state) |
08089d5d | 279 | { |
c29240e7 AD |
280 | int i; |
281 | int k; | |
282 | int mask; | |
283 | shifts *shiftp; | |
284 | unsigned *fp1; | |
285 | unsigned *fp2; | |
286 | unsigned *fp3; | |
287 | int symbol; | |
08089d5d | 288 | |
0df87bb6 | 289 | int src_count = 0; |
08089d5d | 290 | |
90b4416b | 291 | shiftp = state_table[state].shift_table; |
c29240e7 | 292 | if (!shiftp) |
0df87bb6 | 293 | return 0; |
08089d5d DM |
294 | |
295 | for (i = 0; i < tokensetsize; i++) | |
296 | { | |
297 | shiftset[i] = 0; | |
298 | lookaheadset[i] = 0; | |
299 | } | |
300 | ||
301 | k = shiftp->nshifts; | |
302 | for (i = 0; i < k; i++) | |
303 | { | |
c29240e7 AD |
304 | if (!shiftp->shifts[i]) |
305 | continue; | |
9703cc49 | 306 | symbol = state_table[shiftp->shifts[i]].accessing_symbol; |
c29240e7 AD |
307 | if (ISVAR (symbol)) |
308 | break; | |
309 | SETBIT (shiftset, symbol); | |
08089d5d DM |
310 | } |
311 | ||
f004bf6a | 312 | k = state_table[state + 1].lookaheads; |
08089d5d DM |
313 | fp3 = lookaheadset + tokensetsize; |
314 | ||
f004bf6a | 315 | for (i = state_table[state].lookaheads; i < k; i++) |
08089d5d | 316 | { |
bb527fc2 | 317 | fp1 = LA (i); |
08089d5d DM |
318 | fp2 = lookaheadset; |
319 | ||
320 | while (fp2 < fp3) | |
321 | *fp2++ |= *fp1++; | |
322 | } | |
323 | ||
324 | fp1 = shiftset; | |
325 | fp2 = lookaheadset; | |
326 | ||
327 | while (fp2 < fp3) | |
328 | *fp2++ &= *fp1++; | |
329 | ||
330 | mask = 1; | |
331 | fp2 = lookaheadset; | |
332 | for (i = 0; i < ntokens; i++) | |
333 | { | |
334 | if (mask & *fp2) | |
335 | src_count++; | |
336 | ||
337 | mask <<= 1; | |
338 | if (mask == 0) | |
339 | { | |
340 | mask = 1; | |
341 | fp2++; | |
342 | } | |
343 | } | |
0df87bb6 AD |
344 | |
345 | return src_count; | |
08089d5d DM |
346 | } |
347 | ||
348 | ||
c29240e7 AD |
349 | /*----------------------------------------------. |
350 | | Count the number of reduce/reduce conflicts. | | |
351 | `----------------------------------------------*/ | |
352 | ||
0df87bb6 | 353 | static int |
d2729d44 | 354 | count_rr_conflicts (int state) |
08089d5d | 355 | { |
c29240e7 | 356 | int i; |
c29240e7 AD |
357 | unsigned mask; |
358 | unsigned *baseword; | |
08089d5d | 359 | |
0df87bb6 | 360 | int rrc_count = 0; |
08089d5d | 361 | |
f004bf6a AD |
362 | int m = state_table[state].lookaheads; |
363 | int n = state_table[state + 1].lookaheads; | |
08089d5d | 364 | |
c29240e7 | 365 | if (n - m < 2) |
0df87bb6 | 366 | return 0; |
08089d5d DM |
367 | |
368 | mask = 1; | |
bb527fc2 | 369 | baseword = LA (m); |
08089d5d DM |
370 | for (i = 0; i < ntokens; i++) |
371 | { | |
0df87bb6 | 372 | unsigned *wordp = baseword; |
08089d5d | 373 | |
0df87bb6 AD |
374 | int count = 0; |
375 | int j; | |
08089d5d DM |
376 | for (j = m; j < n; j++) |
377 | { | |
378 | if (mask & *wordp) | |
379 | count++; | |
380 | ||
381 | wordp += tokensetsize; | |
382 | } | |
383 | ||
c29240e7 AD |
384 | if (count >= 2) |
385 | rrc_count++; | |
08089d5d DM |
386 | |
387 | mask <<= 1; | |
388 | if (mask == 0) | |
389 | { | |
390 | mask = 1; | |
391 | baseword++; | |
392 | } | |
393 | } | |
0df87bb6 AD |
394 | |
395 | return rrc_count; | |
08089d5d DM |
396 | } |
397 | ||
ff4423cc AD |
398 | /*--------------------------------------------------------------. |
399 | | Return a human readable string which reports shift/reduce and | | |
400 | | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). | | |
401 | `--------------------------------------------------------------*/ | |
c29240e7 | 402 | |
ff4423cc AD |
403 | static const char * |
404 | conflict_report (int src_num, int rrc_num) | |
c29240e7 | 405 | { |
ff4423cc AD |
406 | static char res[4096]; |
407 | char *cp = res; | |
408 | ||
7da99ede | 409 | if (src_num >= 1) |
22c821f3 | 410 | { |
7da99ede AD |
411 | sprintf (cp, ngettext ("%d shift/reduce conflict", |
412 | "%d shift/reduce conflicts", src_num), src_num); | |
22c821f3 AD |
413 | cp += strlen (cp); |
414 | } | |
c29240e7 | 415 | |
0619caf0 | 416 | if (src_num > 0 && rrc_num > 0) |
22c821f3 | 417 | { |
7da99ede | 418 | sprintf (cp, " %s ", _("and")); |
22c821f3 AD |
419 | cp += strlen (cp); |
420 | } | |
c29240e7 | 421 | |
7da99ede | 422 | if (rrc_num >= 1) |
22c821f3 | 423 | { |
7da99ede AD |
424 | sprintf (cp, ngettext ("%d reduce/reduce conflict", |
425 | "%d reduce/reduce conflicts", rrc_num), rrc_num); | |
22c821f3 AD |
426 | cp += strlen (cp); |
427 | } | |
ff4423cc AD |
428 | |
429 | *cp++ = '.'; | |
430 | *cp++ = '\n'; | |
431 | *cp++ = '\0'; | |
c29240e7 | 432 | |
ff4423cc | 433 | return res; |
c29240e7 AD |
434 | } |
435 | ||
436 | ||
0df87bb6 AD |
437 | /*-----------------------------------------------------------. |
438 | | Output the detailed description of states with conflicts. | | |
439 | `-----------------------------------------------------------*/ | |
c29240e7 AD |
440 | |
441 | void | |
0df87bb6 | 442 | conflicts_output (FILE *out) |
c29240e7 AD |
443 | { |
444 | int i; | |
0df87bb6 AD |
445 | for (i = 0; i < nstates; i++) |
446 | if (conflicts[i]) | |
447 | { | |
7da99ede | 448 | fprintf (out, _("State %d contains "), i); |
0df87bb6 AD |
449 | fputs (conflict_report (count_sr_conflicts (i), |
450 | count_rr_conflicts (i)), out); | |
451 | } | |
452 | } | |
c29240e7 | 453 | |
c29240e7 | 454 | |
0df87bb6 AD |
455 | /*------------------------------------------. |
456 | | Reporting the total number of conflicts. | | |
457 | `------------------------------------------*/ | |
0619caf0 | 458 | |
0df87bb6 AD |
459 | void |
460 | conflicts_print (void) | |
461 | { | |
462 | int i; | |
463 | ||
464 | int src_total = 0; | |
465 | int rrc_total = 0; | |
466 | ||
467 | /* Conflicts by state. */ | |
468 | for (i = 0; i < nstates; i++) | |
469 | if (conflicts[i]) | |
470 | { | |
471 | src_total += count_sr_conflicts (i); | |
472 | rrc_total += count_rr_conflicts (i); | |
473 | } | |
c29240e7 | 474 | |
0619caf0 | 475 | /* Report the total number of conflicts on STDERR. */ |
0df87bb6 | 476 | if (src_total || rrc_total) |
09b503c8 AD |
477 | { |
478 | if (yacc_flag) | |
479 | { | |
480 | /* If invoked with `--yacc', use the output format specified by | |
481 | POSIX. */ | |
482 | fprintf (stderr, _("conflicts: ")); | |
483 | if (src_total > 0) | |
484 | fprintf (stderr, _(" %d shift/reduce"), src_total); | |
485 | if (src_total > 0 && rrc_total > 0) | |
486 | fprintf (stderr, ","); | |
487 | if (rrc_total > 0) | |
488 | fprintf (stderr, _(" %d reduce/reduce"), rrc_total); | |
489 | putc ('\n', stderr); | |
490 | } | |
491 | else | |
492 | { | |
493 | fprintf (stderr, _("%s contains "), infile); | |
494 | fputs (conflict_report (src_total, rrc_total), stderr); | |
495 | } | |
496 | } | |
7da99ede AD |
497 | |
498 | if (expected_conflicts != -1 | |
499 | && src_total != expected_conflicts) | |
500 | { | |
501 | complain_message_count++; | |
502 | fprintf (stderr, ngettext ("expected %d shift/reduce conflict", | |
503 | "expected %d shift/reduce conflicts", | |
504 | expected_conflicts), | |
505 | expected_conflicts); | |
506 | } | |
c29240e7 AD |
507 | } |
508 | ||
509 | ||
08089d5d | 510 | void |
c73a41af | 511 | print_reductions (FILE *out, int state) |
08089d5d | 512 | { |
c29240e7 AD |
513 | int i; |
514 | int j; | |
515 | int k; | |
516 | unsigned *fp1; | |
517 | unsigned *fp2; | |
518 | unsigned *fp3; | |
519 | unsigned *fp4; | |
520 | int rule; | |
521 | int symbol; | |
522 | unsigned mask; | |
523 | int m; | |
524 | int n; | |
525 | int default_LA; | |
526 | int default_rule = 0; | |
527 | int cmax; | |
528 | int count; | |
529 | shifts *shiftp; | |
530 | errs *errp; | |
08089d5d DM |
531 | int nodefault = 0; |
532 | ||
533 | for (i = 0; i < tokensetsize; i++) | |
534 | shiftset[i] = 0; | |
535 | ||
90b4416b | 536 | shiftp = state_table[state].shift_table; |
08089d5d DM |
537 | if (shiftp) |
538 | { | |
539 | k = shiftp->nshifts; | |
540 | for (i = 0; i < k; i++) | |
541 | { | |
c29240e7 AD |
542 | if (!shiftp->shifts[i]) |
543 | continue; | |
9703cc49 | 544 | symbol = state_table[shiftp->shifts[i]].accessing_symbol; |
c29240e7 AD |
545 | if (ISVAR (symbol)) |
546 | break; | |
08089d5d DM |
547 | /* if this state has a shift for the error token, |
548 | don't use a default rule. */ | |
c29240e7 AD |
549 | if (symbol == error_token_number) |
550 | nodefault = 1; | |
551 | SETBIT (shiftset, symbol); | |
08089d5d DM |
552 | } |
553 | } | |
554 | ||
555 | errp = err_table[state]; | |
556 | if (errp) | |
557 | { | |
558 | k = errp->nerrs; | |
559 | for (i = 0; i < k; i++) | |
560 | { | |
c29240e7 AD |
561 | if (!errp->errs[i]) |
562 | continue; | |
08089d5d | 563 | symbol = errp->errs[i]; |
c29240e7 | 564 | SETBIT (shiftset, symbol); |
08089d5d DM |
565 | } |
566 | } | |
567 | ||
f004bf6a AD |
568 | m = state_table[state].lookaheads; |
569 | n = state_table[state + 1].lookaheads; | |
08089d5d | 570 | |
c29240e7 | 571 | if (n - m == 1 && !nodefault) |
08089d5d DM |
572 | { |
573 | default_rule = LAruleno[m]; | |
574 | ||
bb527fc2 | 575 | fp1 = LA (m); |
08089d5d DM |
576 | fp2 = shiftset; |
577 | fp3 = lookaheadset; | |
578 | fp4 = lookaheadset + tokensetsize; | |
579 | ||
580 | while (fp3 < fp4) | |
581 | *fp3++ = *fp1++ & *fp2++; | |
582 | ||
583 | mask = 1; | |
584 | fp3 = lookaheadset; | |
585 | ||
586 | for (i = 0; i < ntokens; i++) | |
587 | { | |
588 | if (mask & *fp3) | |
c73a41af | 589 | fprintf (out, _(" %-4s\t[reduce using rule %d (%s)]\n"), |
b2ed6e58 AD |
590 | tags[i], default_rule, |
591 | tags[rule_table[default_rule].lhs]); | |
08089d5d DM |
592 | |
593 | mask <<= 1; | |
594 | if (mask == 0) | |
595 | { | |
596 | mask = 1; | |
597 | fp3++; | |
598 | } | |
599 | } | |
600 | ||
c73a41af | 601 | fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"), |
b2ed6e58 | 602 | default_rule, tags[rule_table[default_rule].lhs]); |
08089d5d DM |
603 | } |
604 | else if (n - m >= 1) | |
605 | { | |
606 | cmax = 0; | |
607 | default_LA = -1; | |
608 | fp4 = lookaheadset + tokensetsize; | |
609 | ||
c29240e7 | 610 | if (!nodefault) |
08089d5d DM |
611 | for (i = m; i < n; i++) |
612 | { | |
bb527fc2 | 613 | fp1 = LA (i); |
08089d5d DM |
614 | fp2 = shiftset; |
615 | fp3 = lookaheadset; | |
ceed8467 | 616 | |
08089d5d | 617 | while (fp3 < fp4) |
b65534e5 | 618 | *fp3++ = *fp1++ & (~(*fp2++)); |
ceed8467 | 619 | |
08089d5d DM |
620 | count = 0; |
621 | mask = 1; | |
622 | fp3 = lookaheadset; | |
623 | for (j = 0; j < ntokens; j++) | |
624 | { | |
625 | if (mask & *fp3) | |
626 | count++; | |
ceed8467 | 627 | |
08089d5d DM |
628 | mask <<= 1; |
629 | if (mask == 0) | |
630 | { | |
631 | mask = 1; | |
632 | fp3++; | |
633 | } | |
634 | } | |
ceed8467 | 635 | |
08089d5d DM |
636 | if (count > cmax) |
637 | { | |
638 | cmax = count; | |
639 | default_LA = i; | |
640 | default_rule = LAruleno[i]; | |
641 | } | |
ceed8467 | 642 | |
08089d5d DM |
643 | fp2 = shiftset; |
644 | fp3 = lookaheadset; | |
ceed8467 | 645 | |
08089d5d DM |
646 | while (fp3 < fp4) |
647 | *fp2++ |= *fp3++; | |
648 | } | |
649 | ||
650 | for (i = 0; i < tokensetsize; i++) | |
c29240e7 | 651 | shiftset[i] = 0; |
08089d5d DM |
652 | |
653 | if (shiftp) | |
c29240e7 AD |
654 | { |
655 | k = shiftp->nshifts; | |
656 | for (i = 0; i < k; i++) | |
08089d5d | 657 | { |
c29240e7 AD |
658 | if (!shiftp->shifts[i]) |
659 | continue; | |
9703cc49 | 660 | symbol = state_table[shiftp->shifts[i]].accessing_symbol; |
c29240e7 AD |
661 | if (ISVAR (symbol)) |
662 | break; | |
663 | SETBIT (shiftset, symbol); | |
08089d5d | 664 | } |
c29240e7 | 665 | } |
08089d5d DM |
666 | |
667 | mask = 1; | |
bb527fc2 | 668 | fp1 = LA (m); |
08089d5d DM |
669 | fp2 = shiftset; |
670 | for (i = 0; i < ntokens; i++) | |
671 | { | |
672 | int defaulted = 0; | |
673 | ||
674 | if (mask & *fp2) | |
675 | count = 1; | |
676 | else | |
677 | count = 0; | |
678 | ||
679 | fp3 = fp1; | |
680 | for (j = m; j < n; j++) | |
681 | { | |
682 | if (mask & *fp3) | |
683 | { | |
684 | if (count == 0) | |
685 | { | |
686 | if (j != default_LA) | |
687 | { | |
688 | rule = LAruleno[j]; | |
c73a41af | 689 | fprintf (out, |
c29240e7 | 690 | _(" %-4s\treduce using rule %d (%s)\n"), |
b2ed6e58 | 691 | tags[i], rule, tags[rule_table[rule].lhs]); |
08089d5d | 692 | } |
c29240e7 AD |
693 | else |
694 | defaulted = 1; | |
08089d5d DM |
695 | |
696 | count++; | |
697 | } | |
698 | else | |
699 | { | |
700 | if (defaulted) | |
701 | { | |
702 | rule = LAruleno[default_LA]; | |
c73a41af | 703 | fprintf (out, |
c29240e7 | 704 | _(" %-4s\treduce using rule %d (%s)\n"), |
b2ed6e58 | 705 | tags[i], rule, tags[rule_table[rule].lhs]); |
08089d5d DM |
706 | defaulted = 0; |
707 | } | |
708 | rule = LAruleno[j]; | |
c73a41af | 709 | fprintf (out, |
c29240e7 | 710 | _(" %-4s\t[reduce using rule %d (%s)]\n"), |
b2ed6e58 | 711 | tags[i], rule, tags[rule_table[rule].lhs]); |
08089d5d DM |
712 | } |
713 | } | |
714 | ||
715 | fp3 += tokensetsize; | |
716 | } | |
717 | ||
718 | mask <<= 1; | |
719 | if (mask == 0) | |
720 | { | |
721 | mask = 1; | |
808e2021 | 722 | /* We tried incrementing just fp1, and just fp2; both seem wrong. |
c29240e7 | 723 | It seems necessary to increment both in sync. */ |
808e2021 | 724 | fp1++; |
08089d5d DM |
725 | fp2++; |
726 | } | |
727 | } | |
728 | ||
729 | if (default_LA >= 0) | |
c73a41af | 730 | fprintf (out, _(" $default\treduce using rule %d (%s)\n"), |
b2ed6e58 | 731 | default_rule, tags[rule_table[default_rule].lhs]); |
08089d5d DM |
732 | } |
733 | } | |
734 | ||
735 | ||
736 | void | |
342b8b6e | 737 | free_conflicts (void) |
08089d5d | 738 | { |
d7913476 AD |
739 | XFREE (conflicts); |
740 | XFREE (shiftset); | |
741 | XFREE (lookaheadset); | |
08089d5d | 742 | } |