]>
git.saurik.com Git - bison.git/blob - src/conflicts.c
1 /* Find and resolve or report look-ahead conflicts for bison,
2 Copyright (C) 1984, 1989, 1992, 2000 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
30 extern int fixed_outfiles
;
32 extern void initialize_conflicts
PARAMS ((void));
33 extern void conflict_log
PARAMS ((void));
34 extern void verbose_conflict_log
PARAMS ((void));
35 extern void print_reductions
PARAMS ((int));
36 extern void finalize_conflicts
PARAMS ((void));
40 int expected_conflicts
;
41 static char *conflicts
;
44 static unsigned *shiftset
;
45 static unsigned *lookaheadset
;
53 log_resolution (int state
, int LAno
, int token
, char *resolution
)
58 Conflict in state %d between rule %d and token %s resolved as %s.\n"),
59 state
, LAruleno
[LAno
], tags
[token
], resolution
);
63 /*------------------------------------------------------------------.
64 | Turn off the shift recorded for the specified token in the |
65 | specified state. Used when we resolve a shift-reduce conflict in |
66 | favor of the reduction. |
67 `------------------------------------------------------------------*/
70 flush_shift (int state
, int token
)
75 shiftp
= shift_table
[state
];
80 for (i
= 0; i
< k
; i
++)
83 && token
== accessing_symbol
[shiftp
->shifts
[i
]])
84 (shiftp
->shifts
[i
]) = 0;
90 /*------------------------------------------------------------------.
91 | Attempt to resolve shift-reduce conflict for one rule by means of |
92 | precedence declarations. It has already been checked that the |
93 | rule has a precedence. A conflict is resolved by modifying the |
94 | shift or reduce tables so that there is no longer a conflict. |
95 `------------------------------------------------------------------*/
98 resolve_sr_conflict (int state
, int lookaheadnum
)
105 errs
*errp
= (errs
*) xmalloc (sizeof (errs
) + ntokens
* sizeof (short));
106 short *errtokens
= errp
->errs
;
108 /* find the rule to reduce by to get precedence of reduction */
109 redprec
= rprec
[LAruleno
[lookaheadnum
]];
112 fp1
= LA
+ lookaheadnum
* tokensetsize
;
114 for (i
= 0; i
< ntokens
; i
++)
116 if ((mask
& *fp2
& *fp1
) && sprec
[i
])
117 /* Shift-reduce conflict occurs for token number i
118 and it has a precedence.
119 The precedence of shifting is that of token i. */
121 if (sprec
[i
] < redprec
)
123 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
124 *fp2
&= ~mask
; /* flush the shift for this token */
125 flush_shift (state
, i
);
127 else if (sprec
[i
] > redprec
)
129 log_resolution (state
, lookaheadnum
, i
, _("shift"));
130 *fp1
&= ~mask
; /* flush the reduce for this token */
134 /* Matching precedence levels.
135 For left association, keep only the reduction.
136 For right association, keep only the shift.
137 For nonassociation, keep neither. */
142 log_resolution (state
, lookaheadnum
, i
, _("shift"));
146 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
150 log_resolution (state
, lookaheadnum
, i
, _("an error"));
154 if (sassoc
[i
] != RIGHT_ASSOC
)
156 *fp2
&= ~mask
; /* flush the shift for this token */
157 flush_shift (state
, i
);
159 if (sassoc
[i
] != LEFT_ASSOC
)
161 *fp1
&= ~mask
; /* flush the reduce for this token */
163 if (sassoc
[i
] == NON_ASSOC
)
165 /* Record an explicit error for this token. */
179 errp
->nerrs
= errtokens
- errp
->errs
;
182 /* Some tokens have been explicitly made errors. Allocate
183 a permanent errs structure for this state, to record them. */
184 i
= (char *) errtokens
- (char *) errp
;
185 err_table
[state
] = (errs
*) xmalloc ((unsigned int) i
);
186 bcopy (errp
, err_table
[state
], i
);
189 err_table
[state
] = 0;
195 set_conflicts (int state
)
206 if (consistent
[state
])
209 for (i
= 0; i
< tokensetsize
; i
++)
212 shiftp
= shift_table
[state
];
216 for (i
= 0; i
< k
; i
++)
218 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
221 SETBIT (lookaheadset
, symbol
);
225 k
= lookaheads
[state
+ 1];
226 fp4
= lookaheadset
+ tokensetsize
;
228 /* Loop over all rules which require lookahead in this state. First
229 check for shift-reduce conflict, and try to resolve using
231 for (i
= lookaheads
[state
]; i
< k
; i
++)
232 if (rprec
[LAruleno
[i
]])
234 fp1
= LA
+ i
* tokensetsize
;
242 resolve_sr_conflict (state
, i
);
249 /* Loop over all rules which require lookahead in this state. Check
250 for conflicts not resolved above. */
251 for (i
= lookaheads
[state
]; i
< k
; i
++)
253 fp1
= LA
+ i
* tokensetsize
;
261 conflicts
[state
] = 1;
275 initialize_conflicts (void)
278 /* errs *sp; JF unused */
280 conflicts
= NEW2 (nstates
, char);
281 shiftset
= NEW2 (tokensetsize
, unsigned);
282 lookaheadset
= NEW2 (tokensetsize
, unsigned);
284 err_table
= NEW2 (nstates
, errs
*);
288 for (i
= 0; i
< nstates
; i
++)
300 /*---------------------------------------------.
301 | Count the number of shift/reduce conflicts. |
302 `---------------------------------------------*/
305 count_sr_conflicts (int state
)
318 shiftp
= shift_table
[state
];
322 for (i
= 0; i
< tokensetsize
; i
++)
329 for (i
= 0; i
< k
; i
++)
331 if (!shiftp
->shifts
[i
])
333 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
336 SETBIT (shiftset
, symbol
);
339 k
= lookaheads
[state
+ 1];
340 fp3
= lookaheadset
+ tokensetsize
;
342 for (i
= lookaheads
[state
]; i
< k
; i
++)
344 fp1
= LA
+ i
* tokensetsize
;
359 for (i
= 0; i
< ntokens
; i
++)
374 /*----------------------------------------------.
375 | Count the number of reduce/reduce conflicts. |
376 `----------------------------------------------*/
379 count_rr_conflicts (int state
)
392 m
= lookaheads
[state
];
393 n
= lookaheads
[state
+ 1];
399 baseword
= LA
+ m
* tokensetsize
;
400 for (i
= 0; i
< ntokens
; i
++)
405 for (j
= m
; j
< n
; j
++)
410 wordp
+= tokensetsize
;
425 /*------------------------------------.
426 | Give a report about the conflicts. |
427 `------------------------------------*/
430 total_conflicts (void)
432 if (src_total
== expected_conflicts
&& rrc_total
== 0)
437 /* If invoked under the name `yacc', use the output format
438 specified by POSIX. */
439 fprintf (stderr
, _("conflicts: "));
441 fprintf (stderr
, _(" %d shift/reduce"), src_total
);
442 if (src_total
> 0 && rrc_total
> 0)
443 fprintf (stderr
, ",");
445 fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
);
450 fprintf (stderr
, _("%s contains"), infile
);
453 fprintf (stderr
, _(" 1 shift/reduce conflict"));
454 else if (src_total
> 1)
455 fprintf (stderr
, _(" %d shift/reduce conflicts"), src_total
);
457 if (src_total
> 0 && rrc_total
> 0)
458 fprintf (stderr
, _(" and"));
461 fprintf (stderr
, _(" 1 reduce/reduce conflict"));
462 else if (rrc_total
> 1)
463 fprintf (stderr
, _(" %d reduce/reduce conflicts"), rrc_total
);
471 /*---------------------------------------------.
472 | Compute and give a report on the conflicts. |
473 `---------------------------------------------*/
483 for (i
= 0; i
< nstates
; i
++)
487 count_sr_conflicts (i
);
488 count_rr_conflicts (i
);
489 src_total
+= src_count
;
490 rrc_total
+= rrc_count
;
499 verbose_conflict_log (void)
506 for (i
= 0; i
< nstates
; i
++)
510 count_sr_conflicts (i
);
511 count_rr_conflicts (i
);
512 src_total
+= src_count
;
513 rrc_total
+= rrc_count
;
515 fprintf (foutput
, _("State %d contains"), i
);
518 fprintf (foutput
, _(" 1 shift/reduce conflict"));
519 else if (src_count
> 1)
520 fprintf (foutput
, _(" %d shift/reduce conflicts"), src_count
);
522 if (src_count
> 0 && rrc_count
> 0)
523 fprintf (foutput
, _(" and"));
526 fprintf (foutput
, _(" 1 reduce/reduce conflict"));
527 else if (rrc_count
> 1)
528 fprintf (foutput
, _(" %d reduce/reduce conflicts"), rrc_count
);
531 putc ('\n', foutput
);
544 print_reductions (int state
)
559 int default_rule
= 0;
566 for (i
= 0; i
< tokensetsize
; i
++)
569 shiftp
= shift_table
[state
];
573 for (i
= 0; i
< k
; i
++)
575 if (!shiftp
->shifts
[i
])
577 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
580 /* if this state has a shift for the error token,
581 don't use a default rule. */
582 if (symbol
== error_token_number
)
584 SETBIT (shiftset
, symbol
);
588 errp
= err_table
[state
];
592 for (i
= 0; i
< k
; i
++)
596 symbol
= errp
->errs
[i
];
597 SETBIT (shiftset
, symbol
);
601 m
= lookaheads
[state
];
602 n
= lookaheads
[state
+ 1];
604 if (n
- m
== 1 && !nodefault
)
606 default_rule
= LAruleno
[m
];
608 fp1
= LA
+ m
* tokensetsize
;
611 fp4
= lookaheadset
+ tokensetsize
;
614 *fp3
++ = *fp1
++ & *fp2
++;
619 for (i
= 0; i
< ntokens
; i
++)
622 fprintf (foutput
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
623 tags
[i
], default_rule
, tags
[rlhs
[default_rule
]]);
633 fprintf (foutput
, _(" $default\treduce using rule %d (%s)\n\n"),
634 default_rule
, tags
[rlhs
[default_rule
]]);
640 fp4
= lookaheadset
+ tokensetsize
;
643 for (i
= m
; i
< n
; i
++)
645 fp1
= LA
+ i
* tokensetsize
;
650 *fp3
++ = *fp1
++ & (~(*fp2
++));
655 for (j
= 0; j
< ntokens
; j
++)
672 default_rule
= LAruleno
[i
];
682 for (i
= 0; i
< tokensetsize
; i
++)
688 for (i
= 0; i
< k
; i
++)
690 if (!shiftp
->shifts
[i
])
692 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
695 SETBIT (shiftset
, symbol
);
700 fp1
= LA
+ m
* tokensetsize
;
702 for (i
= 0; i
< ntokens
; i
++)
712 for (j
= m
; j
< n
; j
++)
722 _(" %-4s\treduce using rule %d (%s)\n"),
723 tags
[i
], rule
, tags
[rlhs
[rule
]]);
734 rule
= LAruleno
[default_LA
];
736 _(" %-4s\treduce using rule %d (%s)\n"),
737 tags
[i
], rule
, tags
[rlhs
[rule
]]);
742 _(" %-4s\t[reduce using rule %d (%s)]\n"),
743 tags
[i
], rule
, tags
[rlhs
[rule
]]);
754 /* We tried incrementing just fp1, and just fp2; both seem wrong.
755 It seems necessary to increment both in sync. */
763 fprintf (foutput
, _(" $default\treduce using rule %d (%s)\n"),
764 default_rule
, tags
[rlhs
[default_rule
]]);
767 putc ('\n', foutput
);
773 finalize_conflicts (void)