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