]>
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. */
33 #define alloca __builtin_alloca
39 extern char *alloca ();
45 extern int tokensetsize
;
46 extern char *consistent
;
47 extern short *accessing_symbol
;
48 extern shifts
**shift_table
;
50 extern short *LAruleno
;
51 extern short *lookaheads
;
52 extern int verboseflag
;
55 void resolve_sr_conflict();
57 void log_resolution();
58 void total_conflicts();
59 void count_sr_conflicts();
60 void count_rr_conflicts();
65 int expected_conflicts
;
68 static unsigned *shiftset
;
69 static unsigned *lookaheadset
;
77 initialize_conflicts()
80 /* register errs *sp; JF unused */
82 conflicts
= NEW2(nstates
, char);
83 shiftset
= NEW2(tokensetsize
, unsigned);
84 lookaheadset
= NEW2(tokensetsize
, unsigned);
86 err_table
= NEW2(nstates
, errs
*);
90 for (i
= 0; i
< nstates
; i
++)
101 register shifts
*shiftp
;
102 register unsigned *fp2
;
103 register unsigned *fp3
;
104 register unsigned *fp4
;
105 register unsigned *fp1
;
108 if (consistent
[state
]) return;
110 for (i
= 0; i
< tokensetsize
; i
++)
113 shiftp
= shift_table
[state
];
117 for (i
= 0; i
< k
; i
++)
119 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
120 if (ISVAR(symbol
)) break;
121 SETBIT(lookaheadset
, symbol
);
125 k
= lookaheads
[state
+ 1];
126 fp4
= lookaheadset
+ tokensetsize
;
128 /* loop over all rules which require lookahead in this state */
129 /* first check for shift-reduce conflict, and try to resolve using precedence */
131 for (i
= lookaheads
[state
]; i
< k
; i
++)
132 if (rprec
[LAruleno
[i
]])
134 fp1
= LA
+ i
* tokensetsize
;
142 resolve_sr_conflict(state
, i
);
148 /* loop over all rules which require lookahead in this state */
149 /* Check for conflicts not resolved above. */
151 for (i
= lookaheads
[state
]; i
< k
; i
++)
153 fp1
= LA
+ i
* tokensetsize
;
161 conflicts
[state
] = 1;
176 /* Attempt to resolve shift-reduce conflict for one rule
177 by means of precedence declarations.
178 It has already been checked that the rule has a precedence.
179 A conflict is resolved by modifying the shift or reduce tables
180 so that there is no longer a conflict. */
183 resolve_sr_conflict(state
, lookaheadnum
)
189 register unsigned *fp1
;
190 register unsigned *fp2
;
191 register int redprec
;
192 /* Extra parens avoid errors on Ultrix 4.3. */
193 errs
*errp
= (errs
*) alloca ((sizeof(errs
) + ntokens
* sizeof(short)));
194 short *errtokens
= errp
->errs
;
196 /* find the rule to reduce by to get precedence of reduction */
197 redprec
= rprec
[LAruleno
[lookaheadnum
]];
200 fp1
= LA
+ lookaheadnum
* tokensetsize
;
202 for (i
= 0; i
< ntokens
; i
++)
204 if ((mask
& *fp2
& *fp1
) && sprec
[i
])
205 /* Shift-reduce conflict occurs for token number i
206 and it has a precedence.
207 The precedence of shifting is that of token i. */
209 if (sprec
[i
] < redprec
)
211 if (verboseflag
) log_resolution(state
, lookaheadnum
, i
, "reduce");
212 *fp2
&= ~mask
; /* flush the shift for this token */
213 flush_shift(state
, i
);
215 else if (sprec
[i
] > redprec
)
217 if (verboseflag
) log_resolution(state
, lookaheadnum
, i
, "shift");
218 *fp1
&= ~mask
; /* flush the reduce for this token */
222 /* Matching precedence levels.
223 For left association, keep only the reduction.
224 For right association, keep only the shift.
225 For nonassociation, keep neither. */
231 if (verboseflag
) log_resolution(state
, lookaheadnum
, i
, "shift");
235 if (verboseflag
) log_resolution(state
, lookaheadnum
, i
, "reduce");
239 if (verboseflag
) log_resolution(state
, lookaheadnum
, i
, "an error");
243 if (sassoc
[i
] != RIGHT_ASSOC
)
245 *fp2
&= ~mask
; /* flush the shift for this token */
246 flush_shift(state
, i
);
248 if (sassoc
[i
] != LEFT_ASSOC
)
250 *fp1
&= ~mask
; /* flush the reduce for this token */
252 if (sassoc
[i
] == NON_ASSOC
)
254 /* Record an explicit error for this token. */
267 errp
->nerrs
= errtokens
- errp
->errs
;
270 /* Some tokens have been explicitly made errors. Allocate
271 a permanent errs structure for this state, to record them. */
272 i
= (char *) errtokens
- (char *) errp
;
273 err_table
[state
] = (errs
*) xmalloc ((unsigned int)i
);
274 bcopy (errp
, err_table
[state
], i
);
277 err_table
[state
] = 0;
282 /* turn off the shift recorded for the specified token in the specified state.
283 Used when we resolve a shift-reduce conflict in favor of the reduction. */
286 flush_shift(state
, token
)
290 register shifts
*shiftp
;
292 /* register unsigned symbol; JF unused */
294 shiftp
= shift_table
[state
];
299 for (i
= 0; i
< k
; i
++)
301 if (shiftp
->shifts
[i
] && token
== accessing_symbol
[shiftp
->shifts
[i
]])
302 (shiftp
->shifts
[i
]) = 0;
309 log_resolution(state
, LAno
, token
, resolution
)
310 int state
, LAno
, token
;
314 "Conflict in state %d between rule %d and token %s resolved as %s.\n",
315 state
, LAruleno
[LAno
], tags
[token
], resolution
);
327 for (i
= 0; i
< nstates
; i
++)
331 count_sr_conflicts(i
);
332 count_rr_conflicts(i
);
333 src_total
+= src_count
;
334 rrc_total
+= rrc_count
;
343 verbose_conflict_log()
350 for (i
= 0; i
< nstates
; i
++)
354 count_sr_conflicts(i
);
355 count_rr_conflicts(i
);
356 src_total
+= src_count
;
357 rrc_total
+= rrc_count
;
359 fprintf(foutput
, "State %d contains", i
);
362 fprintf(foutput
, " 1 shift/reduce conflict");
363 else if (src_count
> 1)
364 fprintf(foutput
, " %d shift/reduce conflicts", src_count
);
366 if (src_count
> 0 && rrc_count
> 0)
367 fprintf(foutput
, " and");
370 fprintf(foutput
, " 1 reduce/reduce conflict");
371 else if (rrc_count
> 1)
372 fprintf(foutput
, " %d reduce/reduce conflicts", rrc_count
);
386 extern int fixed_outfiles
;
388 if (src_total
== expected_conflicts
&& rrc_total
== 0)
393 /* If invoked under the name `yacc', use the output format
394 specified by POSIX. */
395 fprintf(stderr
, "conflicts: ");
397 fprintf(stderr
, " %d shift/reduce", src_total
);
398 if (src_total
> 0 && rrc_total
> 0)
399 fprintf(stderr
, ",");
401 fprintf(stderr
, " %d reduce/reduce", rrc_total
);
406 fprintf(stderr
, "%s contains", infile
);
409 fprintf(stderr
, " 1 shift/reduce conflict");
410 else if (src_total
> 1)
411 fprintf(stderr
, " %d shift/reduce conflicts", src_total
);
413 if (src_total
> 0 && rrc_total
> 0)
414 fprintf(stderr
, " and");
417 fprintf(stderr
, " 1 reduce/reduce conflict");
418 else if (rrc_total
> 1)
419 fprintf(stderr
, " %d reduce/reduce conflicts", rrc_total
);
428 count_sr_conflicts(state
)
434 register shifts
*shiftp
;
435 register unsigned *fp1
;
436 register unsigned *fp2
;
437 register unsigned *fp3
;
442 shiftp
= shift_table
[state
];
445 for (i
= 0; i
< tokensetsize
; i
++)
452 for (i
= 0; i
< k
; i
++)
454 if (! shiftp
->shifts
[i
]) continue;
455 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
456 if (ISVAR(symbol
)) break;
457 SETBIT(shiftset
, symbol
);
460 k
= lookaheads
[state
+ 1];
461 fp3
= lookaheadset
+ tokensetsize
;
463 for (i
= lookaheads
[state
]; i
< k
; i
++)
465 fp1
= LA
+ i
* tokensetsize
;
480 for (i
= 0; i
< ntokens
; i
++)
496 count_rr_conflicts(state
)
502 register unsigned mask
;
503 register unsigned *baseword
;
504 register unsigned *wordp
;
510 m
= lookaheads
[state
];
511 n
= lookaheads
[state
+ 1];
513 if (n
- m
< 2) return;
516 baseword
= LA
+ m
* tokensetsize
;
517 for (i
= 0; i
< ntokens
; i
++)
522 for (j
= m
; j
< n
; j
++)
527 wordp
+= tokensetsize
;
530 if (count
>= 2) rrc_count
++;
543 print_reductions(state
)
549 register unsigned *fp1
;
550 register unsigned *fp2
;
551 register unsigned *fp3
;
552 register unsigned *fp4
;
555 register unsigned mask
;
558 register int default_LA
;
559 register int default_rule
;
562 register shifts
*shiftp
;
566 for (i
= 0; i
< tokensetsize
; i
++)
569 shiftp
= shift_table
[state
];
573 for (i
= 0; i
< k
; i
++)
575 if (! shiftp
->shifts
[i
]) continue;
576 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
577 if (ISVAR(symbol
)) break;
578 /* if this state has a shift for the error token,
579 don't use a default rule. */
580 if (symbol
== error_token_number
) nodefault
= 1;
581 SETBIT(shiftset
, symbol
);
585 errp
= err_table
[state
];
589 for (i
= 0; i
< k
; i
++)
591 if (! errp
->errs
[i
]) continue;
592 symbol
= errp
->errs
[i
];
593 SETBIT(shiftset
, symbol
);
597 m
= lookaheads
[state
];
598 n
= lookaheads
[state
+ 1];
600 if (n
- m
== 1 && ! nodefault
)
602 default_rule
= LAruleno
[m
];
604 fp1
= LA
+ m
* tokensetsize
;
607 fp4
= lookaheadset
+ tokensetsize
;
610 *fp3
++ = *fp1
++ & *fp2
++;
615 for (i
= 0; i
< ntokens
; i
++)
618 fprintf(foutput
, " %-4s\t[reduce using rule %d (%s)]\n",
619 tags
[i
], default_rule
, tags
[rlhs
[default_rule
]]);
629 fprintf(foutput
, " $default\treduce using rule %d (%s)\n\n",
630 default_rule
, tags
[rlhs
[default_rule
]]);
636 fp4
= lookaheadset
+ tokensetsize
;
639 for (i
= m
; i
< n
; i
++)
641 fp1
= LA
+ i
* tokensetsize
;
646 *fp3
++ = *fp1
++ & ( ~ (*fp2
++));
651 for (j
= 0; j
< ntokens
; j
++)
668 default_rule
= LAruleno
[i
];
678 for (i
= 0; i
< tokensetsize
; i
++)
684 for (i
= 0; i
< k
; i
++)
686 if (! shiftp
->shifts
[i
]) continue;
687 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
688 if (ISVAR(symbol
)) break;
689 SETBIT(shiftset
, symbol
);
694 fp1
= LA
+ m
* tokensetsize
;
696 for (i
= 0; i
< ntokens
; i
++)
706 for (j
= m
; j
< n
; j
++)
715 fprintf(foutput
, " %-4s\treduce using rule %d (%s)\n",
716 tags
[i
], rule
, tags
[rlhs
[rule
]]);
726 rule
= LAruleno
[default_LA
];
727 fprintf(foutput
, " %-4s\treduce using rule %d (%s)\n",
728 tags
[i
], rule
, tags
[rlhs
[rule
]]);
732 fprintf(foutput
, " %-4s\t[reduce using rule %d (%s)]\n",
733 tags
[i
], rule
, tags
[rlhs
[rule
]]);
744 /* This used to be fp1, but I think fp2 is right
745 because fp2 is where the words are fetched to test with mask
753 fprintf(foutput
, " $default\treduce using rule %d (%s)\n",
754 default_rule
, tags
[rlhs
[default_rule
]]);