]>
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 tokensetsize
;
31 extern char *consistent
;
32 extern short *accessing_symbol
;
33 extern shifts
**shift_table
;
35 extern short *LAruleno
;
36 extern short *lookaheads
;
37 extern int fixed_outfiles
;
39 extern void initialize_conflicts
PARAMS ((void));
40 extern void conflict_log
PARAMS ((void));
41 extern void verbose_conflict_log
PARAMS ((void));
42 extern void print_reductions
PARAMS ((int));
43 extern void finalize_conflicts
PARAMS ((void));
47 int expected_conflicts
;
48 static char *conflicts
;
51 static unsigned *shiftset
;
52 static unsigned *lookaheadset
;
60 log_resolution (int state
, int LAno
, int token
, char *resolution
)
65 Conflict in state %d between rule %d and token %s resolved as %s.\n"),
66 state
, LAruleno
[LAno
], tags
[token
], resolution
);
70 /*------------------------------------------------------------------.
71 | Turn off the shift recorded for the specified token in the |
72 | specified state. Used when we resolve a shift-reduce conflict in |
73 | favor of the reduction. |
74 `------------------------------------------------------------------*/
77 flush_shift (int state
, int token
)
82 shiftp
= shift_table
[state
];
87 for (i
= 0; i
< k
; i
++)
90 && token
== accessing_symbol
[shiftp
->shifts
[i
]])
91 (shiftp
->shifts
[i
]) = 0;
97 /*------------------------------------------------------------------.
98 | Attempt to resolve shift-reduce conflict for one rule by means of |
99 | precedence declarations. It has already been checked that the |
100 | rule has a precedence. A conflict is resolved by modifying the |
101 | shift or reduce tables so that there is no longer a conflict. |
102 `------------------------------------------------------------------*/
105 resolve_sr_conflict (int state
, int lookaheadnum
)
112 errs
*errp
= (errs
*) xmalloc (sizeof (errs
) + ntokens
* sizeof (short));
113 short *errtokens
= errp
->errs
;
115 /* find the rule to reduce by to get precedence of reduction */
116 redprec
= rprec
[LAruleno
[lookaheadnum
]];
119 fp1
= LA
+ lookaheadnum
* tokensetsize
;
121 for (i
= 0; i
< ntokens
; i
++)
123 if ((mask
& *fp2
& *fp1
) && sprec
[i
])
124 /* Shift-reduce conflict occurs for token number i
125 and it has a precedence.
126 The precedence of shifting is that of token i. */
128 if (sprec
[i
] < redprec
)
130 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
131 *fp2
&= ~mask
; /* flush the shift for this token */
132 flush_shift (state
, i
);
134 else if (sprec
[i
] > redprec
)
136 log_resolution (state
, lookaheadnum
, i
, _("shift"));
137 *fp1
&= ~mask
; /* flush the reduce for this token */
141 /* Matching precedence levels.
142 For left association, keep only the reduction.
143 For right association, keep only the shift.
144 For nonassociation, keep neither. */
149 log_resolution (state
, lookaheadnum
, i
, _("shift"));
153 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
157 log_resolution (state
, lookaheadnum
, i
, _("an error"));
161 if (sassoc
[i
] != RIGHT_ASSOC
)
163 *fp2
&= ~mask
; /* flush the shift for this token */
164 flush_shift (state
, i
);
166 if (sassoc
[i
] != LEFT_ASSOC
)
168 *fp1
&= ~mask
; /* flush the reduce for this token */
170 if (sassoc
[i
] == NON_ASSOC
)
172 /* Record an explicit error for this token. */
186 errp
->nerrs
= errtokens
- errp
->errs
;
189 /* Some tokens have been explicitly made errors. Allocate
190 a permanent errs structure for this state, to record them. */
191 i
= (char *) errtokens
- (char *) errp
;
192 err_table
[state
] = (errs
*) xmalloc ((unsigned int) i
);
193 bcopy (errp
, err_table
[state
], i
);
196 err_table
[state
] = 0;
202 set_conflicts (int state
)
213 if (consistent
[state
])
216 for (i
= 0; i
< tokensetsize
; i
++)
219 shiftp
= shift_table
[state
];
223 for (i
= 0; i
< k
; i
++)
225 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
228 SETBIT (lookaheadset
, symbol
);
232 k
= lookaheads
[state
+ 1];
233 fp4
= lookaheadset
+ tokensetsize
;
235 /* Loop over all rules which require lookahead in this state. First
236 check for shift-reduce conflict, and try to resolve using
238 for (i
= lookaheads
[state
]; i
< k
; i
++)
239 if (rprec
[LAruleno
[i
]])
241 fp1
= LA
+ i
* tokensetsize
;
249 resolve_sr_conflict (state
, i
);
256 /* Loop over all rules which require lookahead in this state. Check
257 for conflicts not resolved above. */
258 for (i
= lookaheads
[state
]; i
< k
; i
++)
260 fp1
= LA
+ i
* tokensetsize
;
268 conflicts
[state
] = 1;
282 initialize_conflicts (void)
285 /* errs *sp; JF unused */
287 conflicts
= NEW2 (nstates
, char);
288 shiftset
= NEW2 (tokensetsize
, unsigned);
289 lookaheadset
= NEW2 (tokensetsize
, unsigned);
291 err_table
= NEW2 (nstates
, errs
*);
295 for (i
= 0; i
< nstates
; i
++)
307 /*---------------------------------------------.
308 | Count the number of shift/reduce conflicts. |
309 `---------------------------------------------*/
312 count_sr_conflicts (int state
)
325 shiftp
= shift_table
[state
];
329 for (i
= 0; i
< tokensetsize
; i
++)
336 for (i
= 0; i
< k
; i
++)
338 if (!shiftp
->shifts
[i
])
340 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
343 SETBIT (shiftset
, symbol
);
346 k
= lookaheads
[state
+ 1];
347 fp3
= lookaheadset
+ tokensetsize
;
349 for (i
= lookaheads
[state
]; i
< k
; i
++)
351 fp1
= LA
+ i
* tokensetsize
;
366 for (i
= 0; i
< ntokens
; i
++)
381 /*----------------------------------------------.
382 | Count the number of reduce/reduce conflicts. |
383 `----------------------------------------------*/
386 count_rr_conflicts (int state
)
399 m
= lookaheads
[state
];
400 n
= lookaheads
[state
+ 1];
406 baseword
= LA
+ m
* tokensetsize
;
407 for (i
= 0; i
< ntokens
; i
++)
412 for (j
= m
; j
< n
; j
++)
417 wordp
+= tokensetsize
;
432 /*------------------------------------.
433 | Give a report about the conflicts. |
434 `------------------------------------*/
437 total_conflicts (void)
439 if (src_total
== expected_conflicts
&& rrc_total
== 0)
444 /* If invoked under the name `yacc', use the output format
445 specified by POSIX. */
446 fprintf (stderr
, _("conflicts: "));
448 fprintf (stderr
, _(" %d shift/reduce"), src_total
);
449 if (src_total
> 0 && rrc_total
> 0)
450 fprintf (stderr
, ",");
452 fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
);
457 fprintf (stderr
, _("%s contains"), infile
);
460 fprintf (stderr
, _(" 1 shift/reduce conflict"));
461 else if (src_total
> 1)
462 fprintf (stderr
, _(" %d shift/reduce conflicts"), src_total
);
464 if (src_total
> 0 && rrc_total
> 0)
465 fprintf (stderr
, _(" and"));
468 fprintf (stderr
, _(" 1 reduce/reduce conflict"));
469 else if (rrc_total
> 1)
470 fprintf (stderr
, _(" %d reduce/reduce conflicts"), rrc_total
);
478 /*---------------------------------------------.
479 | Compute and give a report on the conflicts. |
480 `---------------------------------------------*/
490 for (i
= 0; i
< nstates
; i
++)
494 count_sr_conflicts (i
);
495 count_rr_conflicts (i
);
496 src_total
+= src_count
;
497 rrc_total
+= rrc_count
;
506 verbose_conflict_log (void)
513 for (i
= 0; i
< nstates
; i
++)
517 count_sr_conflicts (i
);
518 count_rr_conflicts (i
);
519 src_total
+= src_count
;
520 rrc_total
+= rrc_count
;
522 fprintf (foutput
, _("State %d contains"), i
);
525 fprintf (foutput
, _(" 1 shift/reduce conflict"));
526 else if (src_count
> 1)
527 fprintf (foutput
, _(" %d shift/reduce conflicts"), src_count
);
529 if (src_count
> 0 && rrc_count
> 0)
530 fprintf (foutput
, _(" and"));
533 fprintf (foutput
, _(" 1 reduce/reduce conflict"));
534 else if (rrc_count
> 1)
535 fprintf (foutput
, _(" %d reduce/reduce conflicts"), rrc_count
);
538 putc ('\n', foutput
);
551 print_reductions (int state
)
566 int default_rule
= 0;
573 for (i
= 0; i
< tokensetsize
; i
++)
576 shiftp
= shift_table
[state
];
580 for (i
= 0; i
< k
; i
++)
582 if (!shiftp
->shifts
[i
])
584 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
587 /* if this state has a shift for the error token,
588 don't use a default rule. */
589 if (symbol
== error_token_number
)
591 SETBIT (shiftset
, symbol
);
595 errp
= err_table
[state
];
599 for (i
= 0; i
< k
; i
++)
603 symbol
= errp
->errs
[i
];
604 SETBIT (shiftset
, symbol
);
608 m
= lookaheads
[state
];
609 n
= lookaheads
[state
+ 1];
611 if (n
- m
== 1 && !nodefault
)
613 default_rule
= LAruleno
[m
];
615 fp1
= LA
+ m
* tokensetsize
;
618 fp4
= lookaheadset
+ tokensetsize
;
621 *fp3
++ = *fp1
++ & *fp2
++;
626 for (i
= 0; i
< ntokens
; i
++)
629 fprintf (foutput
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
630 tags
[i
], default_rule
, tags
[rlhs
[default_rule
]]);
640 fprintf (foutput
, _(" $default\treduce using rule %d (%s)\n\n"),
641 default_rule
, tags
[rlhs
[default_rule
]]);
647 fp4
= lookaheadset
+ tokensetsize
;
650 for (i
= m
; i
< n
; i
++)
652 fp1
= LA
+ i
* tokensetsize
;
657 *fp3
++ = *fp1
++ & (~(*fp2
++));
662 for (j
= 0; j
< ntokens
; j
++)
679 default_rule
= LAruleno
[i
];
689 for (i
= 0; i
< tokensetsize
; i
++)
695 for (i
= 0; i
< k
; i
++)
697 if (!shiftp
->shifts
[i
])
699 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
702 SETBIT (shiftset
, symbol
);
707 fp1
= LA
+ m
* tokensetsize
;
709 for (i
= 0; i
< ntokens
; i
++)
719 for (j
= m
; j
< n
; j
++)
729 _(" %-4s\treduce using rule %d (%s)\n"),
730 tags
[i
], rule
, tags
[rlhs
[rule
]]);
741 rule
= LAruleno
[default_LA
];
743 _(" %-4s\treduce using rule %d (%s)\n"),
744 tags
[i
], rule
, tags
[rlhs
[rule
]]);
749 _(" %-4s\t[reduce using rule %d (%s)]\n"),
750 tags
[i
], rule
, tags
[rlhs
[rule
]]);
761 /* We tried incrementing just fp1, and just fp2; both seem wrong.
762 It seems necessary to increment both in sync. */
770 fprintf (foutput
, _(" $default\treduce using rule %d (%s)\n"),
771 default_rule
, tags
[rlhs
[default_rule
]]);
774 putc ('\n', foutput
);
780 finalize_conflicts (void)