]>
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));
45 static void set_conflicts
PARAMS((int));
46 static void resolve_sr_conflict
PARAMS((int, int));
47 static void flush_shift
PARAMS((int, int));
48 static void log_resolution
PARAMS((int, int, int, char *));
49 static void total_conflicts
PARAMS((void));
50 static void count_sr_conflicts
PARAMS((int));
51 static void count_rr_conflicts
PARAMS((int));
55 int expected_conflicts
;
56 static char *conflicts
;
59 static unsigned *shiftset
;
60 static unsigned *lookaheadset
;
68 initialize_conflicts (void)
71 /* register errs *sp; JF unused */
73 conflicts
= NEW2(nstates
, char);
74 shiftset
= NEW2(tokensetsize
, unsigned);
75 lookaheadset
= NEW2(tokensetsize
, unsigned);
77 err_table
= NEW2(nstates
, errs
*);
81 for (i
= 0; i
< nstates
; i
++)
87 set_conflicts (int state
)
91 register shifts
*shiftp
;
92 register unsigned *fp2
;
93 register unsigned *fp3
;
94 register unsigned *fp4
;
95 register unsigned *fp1
;
98 if (consistent
[state
]) return;
100 for (i
= 0; i
< tokensetsize
; i
++)
103 shiftp
= shift_table
[state
];
107 for (i
= 0; i
< k
; i
++)
109 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
110 if (ISVAR(symbol
)) break;
111 SETBIT(lookaheadset
, symbol
);
115 k
= lookaheads
[state
+ 1];
116 fp4
= lookaheadset
+ tokensetsize
;
118 /* loop over all rules which require lookahead in this state */
119 /* first check for shift-reduce conflict, and try to resolve using precedence */
121 for (i
= lookaheads
[state
]; i
< k
; i
++)
122 if (rprec
[LAruleno
[i
]])
124 fp1
= LA
+ i
* tokensetsize
;
132 resolve_sr_conflict(state
, i
);
138 /* loop over all rules which require lookahead in this state */
139 /* Check for conflicts not resolved above. */
141 for (i
= lookaheads
[state
]; i
< k
; i
++)
143 fp1
= LA
+ i
* tokensetsize
;
151 conflicts
[state
] = 1;
166 /* Attempt to resolve shift-reduce conflict for one rule
167 by means of precedence declarations.
168 It has already been checked that the rule has a precedence.
169 A conflict is resolved by modifying the shift or reduce tables
170 so that there is no longer a conflict. */
173 resolve_sr_conflict (int state
, int lookaheadnum
)
177 register unsigned *fp1
;
178 register unsigned *fp2
;
179 register int redprec
;
180 errs
*errp
= (errs
*) xmalloc (sizeof(errs
) + ntokens
* sizeof(short));
181 short *errtokens
= errp
->errs
;
183 /* find the rule to reduce by to get precedence of reduction */
184 redprec
= rprec
[LAruleno
[lookaheadnum
]];
187 fp1
= LA
+ lookaheadnum
* tokensetsize
;
189 for (i
= 0; i
< ntokens
; i
++)
191 if ((mask
& *fp2
& *fp1
) && sprec
[i
])
192 /* Shift-reduce conflict occurs for token number i
193 and it has a precedence.
194 The precedence of shifting is that of token i. */
196 if (sprec
[i
] < redprec
)
198 if (verboseflag
) log_resolution(state
, lookaheadnum
, i
, _("reduce"));
199 *fp2
&= ~mask
; /* flush the shift for this token */
200 flush_shift(state
, i
);
202 else if (sprec
[i
] > redprec
)
204 if (verboseflag
) log_resolution(state
, lookaheadnum
, i
, _("shift"));
205 *fp1
&= ~mask
; /* flush the reduce for this token */
209 /* Matching precedence levels.
210 For left association, keep only the reduction.
211 For right association, keep only the shift.
212 For nonassociation, keep neither. */
218 if (verboseflag
) log_resolution(state
, lookaheadnum
, i
, _("shift"));
222 if (verboseflag
) log_resolution(state
, lookaheadnum
, i
, _("reduce"));
226 if (verboseflag
) log_resolution(state
, lookaheadnum
, i
, _("an error"));
230 if (sassoc
[i
] != RIGHT_ASSOC
)
232 *fp2
&= ~mask
; /* flush the shift for this token */
233 flush_shift(state
, i
);
235 if (sassoc
[i
] != LEFT_ASSOC
)
237 *fp1
&= ~mask
; /* flush the reduce for this token */
239 if (sassoc
[i
] == NON_ASSOC
)
241 /* Record an explicit error for this token. */
254 errp
->nerrs
= errtokens
- errp
->errs
;
257 /* Some tokens have been explicitly made errors. Allocate
258 a permanent errs structure for this state, to record them. */
259 i
= (char *) errtokens
- (char *) errp
;
260 err_table
[state
] = (errs
*) xmalloc ((unsigned int)i
);
261 bcopy (errp
, err_table
[state
], i
);
264 err_table
[state
] = 0;
270 /* turn off the shift recorded for the specified token in the specified state.
271 Used when we resolve a shift-reduce conflict in favor of the reduction. */
274 flush_shift (int state
, int token
)
276 register shifts
*shiftp
;
278 /* register unsigned symbol; JF unused */
280 shiftp
= shift_table
[state
];
285 for (i
= 0; i
< k
; i
++)
287 if (shiftp
->shifts
[i
] && token
== accessing_symbol
[shiftp
->shifts
[i
]])
288 (shiftp
->shifts
[i
]) = 0;
295 log_resolution (int state
, int LAno
, int token
, char *resolution
)
298 _("Conflict in state %d between rule %d and token %s resolved as %s.\n"),
299 state
, LAruleno
[LAno
], tags
[token
], resolution
);
311 for (i
= 0; i
< nstates
; i
++)
315 count_sr_conflicts(i
);
316 count_rr_conflicts(i
);
317 src_total
+= src_count
;
318 rrc_total
+= rrc_count
;
327 verbose_conflict_log (void)
334 for (i
= 0; i
< nstates
; i
++)
338 count_sr_conflicts(i
);
339 count_rr_conflicts(i
);
340 src_total
+= src_count
;
341 rrc_total
+= rrc_count
;
343 fprintf(foutput
, _("State %d contains"), i
);
346 fprintf(foutput
, _(" 1 shift/reduce conflict"));
347 else if (src_count
> 1)
348 fprintf(foutput
, _(" %d shift/reduce conflicts"), src_count
);
350 if (src_count
> 0 && rrc_count
> 0)
351 fprintf(foutput
, _(" and"));
354 fprintf(foutput
, _(" 1 reduce/reduce conflict"));
355 else if (rrc_count
> 1)
356 fprintf(foutput
, _(" %d reduce/reduce conflicts"), rrc_count
);
368 total_conflicts (void)
370 if (src_total
== expected_conflicts
&& rrc_total
== 0)
375 /* If invoked under the name `yacc', use the output format
376 specified by POSIX. */
377 fprintf(stderr
, _("conflicts: "));
379 fprintf(stderr
, _(" %d shift/reduce"), src_total
);
380 if (src_total
> 0 && rrc_total
> 0)
381 fprintf(stderr
, ",");
383 fprintf(stderr
, _(" %d reduce/reduce"), rrc_total
);
388 fprintf(stderr
, _("%s contains"), infile
);
391 fprintf(stderr
, _(" 1 shift/reduce conflict"));
392 else if (src_total
> 1)
393 fprintf(stderr
, _(" %d shift/reduce conflicts"), src_total
);
395 if (src_total
> 0 && rrc_total
> 0)
396 fprintf(stderr
, _(" and"));
399 fprintf(stderr
, _(" 1 reduce/reduce conflict"));
400 else if (rrc_total
> 1)
401 fprintf(stderr
, _(" %d reduce/reduce conflicts"), rrc_total
);
410 count_sr_conflicts (int state
)
415 register shifts
*shiftp
;
416 register unsigned *fp1
;
417 register unsigned *fp2
;
418 register unsigned *fp3
;
423 shiftp
= shift_table
[state
];
426 for (i
= 0; i
< tokensetsize
; i
++)
433 for (i
= 0; i
< k
; i
++)
435 if (! shiftp
->shifts
[i
]) continue;
436 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
437 if (ISVAR(symbol
)) break;
438 SETBIT(shiftset
, symbol
);
441 k
= lookaheads
[state
+ 1];
442 fp3
= lookaheadset
+ tokensetsize
;
444 for (i
= lookaheads
[state
]; i
< k
; i
++)
446 fp1
= LA
+ i
* tokensetsize
;
461 for (i
= 0; i
< ntokens
; i
++)
477 count_rr_conflicts (int state
)
482 register unsigned mask
;
483 register unsigned *baseword
;
484 register unsigned *wordp
;
490 m
= lookaheads
[state
];
491 n
= lookaheads
[state
+ 1];
493 if (n
- m
< 2) return;
496 baseword
= LA
+ m
* tokensetsize
;
497 for (i
= 0; i
< ntokens
; i
++)
502 for (j
= m
; j
< n
; j
++)
507 wordp
+= tokensetsize
;
510 if (count
>= 2) rrc_count
++;
523 print_reductions (int state
)
528 register unsigned *fp1
;
529 register unsigned *fp2
;
530 register unsigned *fp3
;
531 register unsigned *fp4
;
534 register unsigned mask
;
537 register int default_LA
;
538 register int default_rule
= 0;
541 register shifts
*shiftp
;
545 for (i
= 0; i
< tokensetsize
; i
++)
548 shiftp
= shift_table
[state
];
552 for (i
= 0; i
< k
; i
++)
554 if (! shiftp
->shifts
[i
]) continue;
555 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
556 if (ISVAR(symbol
)) break;
557 /* if this state has a shift for the error token,
558 don't use a default rule. */
559 if (symbol
== error_token_number
) nodefault
= 1;
560 SETBIT(shiftset
, symbol
);
564 errp
= err_table
[state
];
568 for (i
= 0; i
< k
; i
++)
570 if (! errp
->errs
[i
]) continue;
571 symbol
= errp
->errs
[i
];
572 SETBIT(shiftset
, symbol
);
576 m
= lookaheads
[state
];
577 n
= lookaheads
[state
+ 1];
579 if (n
- m
== 1 && ! nodefault
)
581 default_rule
= LAruleno
[m
];
583 fp1
= LA
+ m
* tokensetsize
;
586 fp4
= lookaheadset
+ tokensetsize
;
589 *fp3
++ = *fp1
++ & *fp2
++;
594 for (i
= 0; i
< ntokens
; i
++)
597 fprintf(foutput
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
598 tags
[i
], default_rule
, tags
[rlhs
[default_rule
]]);
608 fprintf(foutput
, _(" $default\treduce using rule %d (%s)\n\n"),
609 default_rule
, tags
[rlhs
[default_rule
]]);
615 fp4
= lookaheadset
+ tokensetsize
;
618 for (i
= m
; i
< n
; i
++)
620 fp1
= LA
+ i
* tokensetsize
;
625 *fp3
++ = *fp1
++ & (~(*fp2
++));
630 for (j
= 0; j
< ntokens
; j
++)
647 default_rule
= LAruleno
[i
];
657 for (i
= 0; i
< tokensetsize
; i
++)
663 for (i
= 0; i
< k
; i
++)
665 if (! shiftp
->shifts
[i
]) continue;
666 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
667 if (ISVAR(symbol
)) break;
668 SETBIT(shiftset
, symbol
);
673 fp1
= LA
+ m
* tokensetsize
;
675 for (i
= 0; i
< ntokens
; i
++)
685 for (j
= m
; j
< n
; j
++)
694 fprintf(foutput
, _(" %-4s\treduce using rule %d (%s)\n"),
695 tags
[i
], rule
, tags
[rlhs
[rule
]]);
705 rule
= LAruleno
[default_LA
];
706 fprintf(foutput
, _(" %-4s\treduce using rule %d (%s)\n"),
707 tags
[i
], rule
, tags
[rlhs
[rule
]]);
711 fprintf(foutput
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
712 tags
[i
], rule
, tags
[rlhs
[rule
]]);
723 /* We tried incrementing just fp1, and just fp2; both seem wrong.
724 It seems necessary to increment both in sync. */
732 fprintf(foutput
, _(" $default\treduce using rule %d (%s)\n"),
733 default_rule
, tags
[rlhs
[default_rule
]]);
742 finalize_conflicts (void)