]>
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 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
7 it 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,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU 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
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
34 #define alloca __builtin_alloca
40 extern char *alloca ();
46 extern int tokensetsize
;
47 extern char *consistent
;
48 extern short *accessing_symbol
;
49 extern shifts
**shift_table
;
51 extern short *LAruleno
;
52 extern short *lookaheads
;
53 extern int verboseflag
;
56 void resolve_sr_conflict();
58 void log_resolution();
59 void total_conflicts();
60 void count_sr_conflicts();
61 void count_rr_conflicts();
66 int expected_conflicts
;
69 static unsigned *shiftset
;
70 static unsigned *lookaheadset
;
78 initialize_conflicts()
81 /* register errs *sp; JF unused */
83 conflicts
= NEW2(nstates
, char);
84 shiftset
= NEW2(tokensetsize
, unsigned);
85 lookaheadset
= NEW2(tokensetsize
, unsigned);
87 err_table
= NEW2(nstates
, errs
*);
91 for (i
= 0; i
< nstates
; i
++)
102 register shifts
*shiftp
;
103 register unsigned *fp2
;
104 register unsigned *fp3
;
105 register unsigned *fp4
;
106 register unsigned *fp1
;
109 if (consistent
[state
]) return;
111 for (i
= 0; i
< tokensetsize
; i
++)
114 shiftp
= shift_table
[state
];
118 for (i
= 0; i
< k
; i
++)
120 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
121 if (ISVAR(symbol
)) break;
122 SETBIT(lookaheadset
, symbol
);
126 k
= lookaheads
[state
+ 1];
127 fp4
= lookaheadset
+ tokensetsize
;
129 /* loop over all rules which require lookahead in this state */
130 /* first check for shift-reduce conflict, and try to resolve using precedence */
132 for (i
= lookaheads
[state
]; i
< k
; i
++)
133 if (rprec
[LAruleno
[i
]])
135 fp1
= LA
+ i
* tokensetsize
;
143 resolve_sr_conflict(state
, i
);
149 /* loop over all rules which require lookahead in this state */
150 /* Check for conflicts not resolved above. */
152 for (i
= lookaheads
[state
]; i
< k
; i
++)
154 fp1
= LA
+ i
* tokensetsize
;
162 conflicts
[state
] = 1;
177 /* Attempt to resolve shift-reduce conflict for one rule
178 by means of precedence declarations.
179 It has already been checked that the rule has a precedence.
180 A conflict is resolved by modifying the shift or reduce tables
181 so that there is no longer a conflict. */
184 resolve_sr_conflict(state
, lookaheadnum
)
190 register unsigned *fp1
;
191 register unsigned *fp2
;
192 register int redprec
;
193 /* Extra parens avoid errors on Ultrix 4.3. */
194 errs
*errp
= (errs
*) alloca ((sizeof(errs
) + ntokens
* sizeof(short)));
195 short *errtokens
= errp
->errs
;
197 /* find the rule to reduce by to get precedence of reduction */
198 redprec
= rprec
[LAruleno
[lookaheadnum
]];
201 fp1
= LA
+ lookaheadnum
* tokensetsize
;
203 for (i
= 0; i
< ntokens
; i
++)
205 if ((mask
& *fp2
& *fp1
) && sprec
[i
])
206 /* Shift-reduce conflict occurs for token number i
207 and it has a precedence.
208 The precedence of shifting is that of token i. */
210 if (sprec
[i
] < redprec
)
212 if (verboseflag
) log_resolution(state
, lookaheadnum
, i
, "reduce");
213 *fp2
&= ~mask
; /* flush the shift for this token */
214 flush_shift(state
, i
);
216 else if (sprec
[i
] > redprec
)
218 if (verboseflag
) log_resolution(state
, lookaheadnum
, i
, "shift");
219 *fp1
&= ~mask
; /* flush the reduce for this token */
223 /* Matching precedence levels.
224 For left association, keep only the reduction.
225 For right association, keep only the shift.
226 For nonassociation, keep neither. */
232 if (verboseflag
) log_resolution(state
, lookaheadnum
, i
, "shift");
236 if (verboseflag
) log_resolution(state
, lookaheadnum
, i
, "reduce");
240 if (verboseflag
) log_resolution(state
, lookaheadnum
, i
, "an error");
244 if (sassoc
[i
] != RIGHT_ASSOC
)
246 *fp2
&= ~mask
; /* flush the shift for this token */
247 flush_shift(state
, i
);
249 if (sassoc
[i
] != LEFT_ASSOC
)
251 *fp1
&= ~mask
; /* flush the reduce for this token */
253 if (sassoc
[i
] == NON_ASSOC
)
255 /* Record an explicit error for this token. */
268 errp
->nerrs
= errtokens
- errp
->errs
;
271 /* Some tokens have been explicitly made errors. Allocate
272 a permanent errs structure for this state, to record them. */
273 i
= (char *) errtokens
- (char *) errp
;
274 err_table
[state
] = (errs
*) xmalloc ((unsigned int)i
);
275 bcopy (errp
, err_table
[state
], i
);
278 err_table
[state
] = 0;
283 /* turn off the shift recorded for the specified token in the specified state.
284 Used when we resolve a shift-reduce conflict in favor of the reduction. */
287 flush_shift(state
, token
)
291 register shifts
*shiftp
;
293 /* register unsigned symbol; JF unused */
295 shiftp
= shift_table
[state
];
300 for (i
= 0; i
< k
; i
++)
302 if (shiftp
->shifts
[i
] && token
== accessing_symbol
[shiftp
->shifts
[i
]])
303 (shiftp
->shifts
[i
]) = 0;
310 log_resolution(state
, LAno
, token
, resolution
)
311 int state
, LAno
, token
;
315 "Conflict in state %d between rule %d and token %s resolved as %s.\n",
316 state
, LAruleno
[LAno
], tags
[token
], resolution
);
328 for (i
= 0; i
< nstates
; i
++)
332 count_sr_conflicts(i
);
333 count_rr_conflicts(i
);
334 src_total
+= src_count
;
335 rrc_total
+= rrc_count
;
344 verbose_conflict_log()
351 for (i
= 0; i
< nstates
; i
++)
355 count_sr_conflicts(i
);
356 count_rr_conflicts(i
);
357 src_total
+= src_count
;
358 rrc_total
+= rrc_count
;
360 fprintf(foutput
, "State %d contains", i
);
363 fprintf(foutput
, " 1 shift/reduce conflict");
364 else if (src_count
> 1)
365 fprintf(foutput
, " %d shift/reduce conflicts", src_count
);
367 if (src_count
> 0 && rrc_count
> 0)
368 fprintf(foutput
, " and");
371 fprintf(foutput
, " 1 reduce/reduce conflict");
372 else if (rrc_count
> 1)
373 fprintf(foutput
, " %d reduce/reduce conflicts", rrc_count
);
387 extern int fixed_outfiles
;
389 if (src_total
== expected_conflicts
&& rrc_total
== 0)
394 /* If invoked under the name `yacc', use the output format
395 specified by POSIX. */
396 fprintf(stderr
, "conflicts: ");
398 fprintf(stderr
, " %d shift/reduce", src_total
);
399 if (src_total
> 0 && rrc_total
> 0)
400 fprintf(stderr
, ",");
402 fprintf(stderr
, " %d reduce/reduce", rrc_total
);
407 fprintf(stderr
, "%s contains", infile
);
410 fprintf(stderr
, " 1 shift/reduce conflict");
411 else if (src_total
> 1)
412 fprintf(stderr
, " %d shift/reduce conflicts", src_total
);
414 if (src_total
> 0 && rrc_total
> 0)
415 fprintf(stderr
, " and");
418 fprintf(stderr
, " 1 reduce/reduce conflict");
419 else if (rrc_total
> 1)
420 fprintf(stderr
, " %d reduce/reduce conflicts", rrc_total
);
429 count_sr_conflicts(state
)
435 register shifts
*shiftp
;
436 register unsigned *fp1
;
437 register unsigned *fp2
;
438 register unsigned *fp3
;
443 shiftp
= shift_table
[state
];
446 for (i
= 0; i
< tokensetsize
; i
++)
453 for (i
= 0; i
< k
; i
++)
455 if (! shiftp
->shifts
[i
]) continue;
456 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
457 if (ISVAR(symbol
)) break;
458 SETBIT(shiftset
, symbol
);
461 k
= lookaheads
[state
+ 1];
462 fp3
= lookaheadset
+ tokensetsize
;
464 for (i
= lookaheads
[state
]; i
< k
; i
++)
466 fp1
= LA
+ i
* tokensetsize
;
481 for (i
= 0; i
< ntokens
; i
++)
497 count_rr_conflicts(state
)
503 register unsigned mask
;
504 register unsigned *baseword
;
505 register unsigned *wordp
;
511 m
= lookaheads
[state
];
512 n
= lookaheads
[state
+ 1];
514 if (n
- m
< 2) return;
517 baseword
= LA
+ m
* tokensetsize
;
518 for (i
= 0; i
< ntokens
; i
++)
523 for (j
= m
; j
< n
; j
++)
528 wordp
+= tokensetsize
;
531 if (count
>= 2) rrc_count
++;
544 print_reductions(state
)
550 register unsigned *fp1
;
551 register unsigned *fp2
;
552 register unsigned *fp3
;
553 register unsigned *fp4
;
556 register unsigned mask
;
559 register int default_LA
;
560 register int default_rule
;
563 register shifts
*shiftp
;
567 for (i
= 0; i
< tokensetsize
; i
++)
570 shiftp
= shift_table
[state
];
574 for (i
= 0; i
< k
; i
++)
576 if (! shiftp
->shifts
[i
]) continue;
577 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
578 if (ISVAR(symbol
)) break;
579 /* if this state has a shift for the error token,
580 don't use a default rule. */
581 if (symbol
== error_token_number
) nodefault
= 1;
582 SETBIT(shiftset
, symbol
);
586 errp
= err_table
[state
];
590 for (i
= 0; i
< k
; i
++)
592 if (! errp
->errs
[i
]) continue;
593 symbol
= errp
->errs
[i
];
594 SETBIT(shiftset
, symbol
);
598 m
= lookaheads
[state
];
599 n
= lookaheads
[state
+ 1];
601 if (n
- m
== 1 && ! nodefault
)
603 default_rule
= LAruleno
[m
];
605 fp1
= LA
+ m
* tokensetsize
;
608 fp4
= lookaheadset
+ tokensetsize
;
611 *fp3
++ = *fp1
++ & *fp2
++;
616 for (i
= 0; i
< ntokens
; i
++)
619 fprintf(foutput
, " %-4s\t[reduce using rule %d (%s)]\n",
620 tags
[i
], default_rule
, tags
[rlhs
[default_rule
]]);
630 fprintf(foutput
, " $default\treduce using rule %d (%s)\n\n",
631 default_rule
, tags
[rlhs
[default_rule
]]);
637 fp4
= lookaheadset
+ tokensetsize
;
640 for (i
= m
; i
< n
; i
++)
642 fp1
= LA
+ i
* tokensetsize
;
647 *fp3
++ = *fp1
++ & ( ~ (*fp2
++));
652 for (j
= 0; j
< ntokens
; j
++)
669 default_rule
= LAruleno
[i
];
679 for (i
= 0; i
< tokensetsize
; i
++)
685 for (i
= 0; i
< k
; i
++)
687 if (! shiftp
->shifts
[i
]) continue;
688 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
689 if (ISVAR(symbol
)) break;
690 SETBIT(shiftset
, symbol
);
695 fp1
= LA
+ m
* tokensetsize
;
697 for (i
= 0; i
< ntokens
; i
++)
707 for (j
= m
; j
< n
; j
++)
716 fprintf(foutput
, " %-4s\treduce using rule %d (%s)\n",
717 tags
[i
], rule
, tags
[rlhs
[rule
]]);
727 rule
= LAruleno
[default_LA
];
728 fprintf(foutput
, " %-4s\treduce using rule %d (%s)\n",
729 tags
[i
], rule
, tags
[rlhs
[rule
]]);
733 fprintf(foutput
, " %-4s\t[reduce using rule %d (%s)]\n",
734 tags
[i
], rule
, tags
[rlhs
[rule
]]);
745 /* This used to be fp1, but I think fp2 is right
746 because fp2 is where the words are fetched to test with mask
754 fprintf(foutput
, " $default\treduce using rule %d (%s)\n",
755 default_rule
, tags
[rlhs
[default_rule
]]);