]>
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
28 #include "conflicts.h"
31 extern int fixed_outfiles
;
33 int any_conflicts
= 0;
35 int expected_conflicts
;
36 static char *conflicts
;
38 static unsigned *shiftset
;
39 static unsigned *lookaheadset
;
47 log_resolution (int state
, int LAno
, int token
, char *resolution
)
52 Conflict in state %d between rule %d and token %s resolved as %s.\n"),
53 state
, LAruleno
[LAno
], tags
[token
], resolution
);
57 /*------------------------------------------------------------------.
58 | Turn off the shift recorded for the specified token in the |
59 | specified state. Used when we resolve a shift-reduce conflict in |
60 | favor of the reduction. |
61 `------------------------------------------------------------------*/
64 flush_shift (int state
, int token
)
69 shiftp
= shift_table
[state
];
74 for (i
= 0; i
< k
; i
++)
77 && token
== accessing_symbol
[shiftp
->shifts
[i
]])
78 (shiftp
->shifts
[i
]) = 0;
84 /*------------------------------------------------------------------.
85 | Attempt to resolve shift-reduce conflict for one rule by means of |
86 | precedence declarations. It has already been checked that the |
87 | rule has a precedence. A conflict is resolved by modifying the |
88 | shift or reduce tables so that there is no longer a conflict. |
89 `------------------------------------------------------------------*/
92 resolve_sr_conflict (int state
, int lookaheadnum
)
99 errs
*errp
= (errs
*) xmalloc (sizeof (errs
) + ntokens
* sizeof (short));
100 short *errtokens
= errp
->errs
;
102 /* find the rule to reduce by to get precedence of reduction */
103 redprec
= rprec
[LAruleno
[lookaheadnum
]];
106 fp1
= LA
+ lookaheadnum
* tokensetsize
;
108 for (i
= 0; i
< ntokens
; i
++)
110 if ((mask
& *fp2
& *fp1
) && sprec
[i
])
111 /* Shift-reduce conflict occurs for token number i
112 and it has a precedence.
113 The precedence of shifting is that of token i. */
115 if (sprec
[i
] < redprec
)
117 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
118 *fp2
&= ~mask
; /* flush the shift for this token */
119 flush_shift (state
, i
);
121 else if (sprec
[i
] > redprec
)
123 log_resolution (state
, lookaheadnum
, i
, _("shift"));
124 *fp1
&= ~mask
; /* flush the reduce for this token */
128 /* Matching precedence levels.
129 For left association, keep only the reduction.
130 For right association, keep only the shift.
131 For nonassociation, keep neither. */
136 log_resolution (state
, lookaheadnum
, i
, _("shift"));
140 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
144 log_resolution (state
, lookaheadnum
, i
, _("an error"));
148 if (sassoc
[i
] != RIGHT_ASSOC
)
150 *fp2
&= ~mask
; /* flush the shift for this token */
151 flush_shift (state
, i
);
153 if (sassoc
[i
] != LEFT_ASSOC
)
155 *fp1
&= ~mask
; /* flush the reduce for this token */
157 if (sassoc
[i
] == NON_ASSOC
)
159 /* Record an explicit error for this token. */
173 errp
->nerrs
= errtokens
- errp
->errs
;
176 /* Some tokens have been explicitly made errors. Allocate
177 a permanent errs structure for this state, to record them. */
178 i
= (char *) errtokens
- (char *) errp
;
179 err_table
[state
] = (errs
*) xmalloc ((unsigned int) i
);
180 bcopy (errp
, err_table
[state
], i
);
183 err_table
[state
] = 0;
189 set_conflicts (int state
)
200 if (consistent
[state
])
203 for (i
= 0; i
< tokensetsize
; i
++)
206 shiftp
= shift_table
[state
];
210 for (i
= 0; i
< k
; i
++)
212 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
215 SETBIT (lookaheadset
, symbol
);
219 k
= lookaheads
[state
+ 1];
220 fp4
= lookaheadset
+ tokensetsize
;
222 /* Loop over all rules which require lookahead in this state. First
223 check for shift-reduce conflict, and try to resolve using
225 for (i
= lookaheads
[state
]; i
< k
; i
++)
226 if (rprec
[LAruleno
[i
]])
228 fp1
= LA
+ i
* tokensetsize
;
236 resolve_sr_conflict (state
, i
);
243 /* Loop over all rules which require lookahead in this state. Check
244 for conflicts not resolved above. */
245 for (i
= lookaheads
[state
]; i
< k
; i
++)
247 fp1
= LA
+ i
* tokensetsize
;
255 conflicts
[state
] = 1;
269 initialize_conflicts (void)
273 conflicts
= NEW2 (nstates
, char);
274 shiftset
= NEW2 (tokensetsize
, unsigned);
275 lookaheadset
= NEW2 (tokensetsize
, unsigned);
277 err_table
= NEW2 (nstates
, errs
*);
281 for (i
= 0; i
< nstates
; i
++)
286 /*---------------------------------------------.
287 | Count the number of shift/reduce conflicts. |
288 `---------------------------------------------*/
291 count_sr_conflicts (int state
)
304 shiftp
= shift_table
[state
];
308 for (i
= 0; i
< tokensetsize
; i
++)
315 for (i
= 0; i
< k
; i
++)
317 if (!shiftp
->shifts
[i
])
319 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
322 SETBIT (shiftset
, symbol
);
325 k
= lookaheads
[state
+ 1];
326 fp3
= lookaheadset
+ tokensetsize
;
328 for (i
= lookaheads
[state
]; i
< k
; i
++)
330 fp1
= LA
+ i
* tokensetsize
;
345 for (i
= 0; i
< ntokens
; i
++)
360 /*----------------------------------------------.
361 | Count the number of reduce/reduce conflicts. |
362 `----------------------------------------------*/
365 count_rr_conflicts (int state
)
378 m
= lookaheads
[state
];
379 n
= lookaheads
[state
+ 1];
385 baseword
= LA
+ m
* tokensetsize
;
386 for (i
= 0; i
< ntokens
; i
++)
391 for (j
= m
; j
< n
; j
++)
396 wordp
+= tokensetsize
;
411 /*----------------------------------------------------------.
412 | Output to OUT a human readable report on shift/reduce and |
413 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
414 `----------------------------------------------------------*/
417 conflict_report (FILE *out
, int src_num
, int rrc_num
)
420 fprintf (out
, _(" 1 shift/reduce conflict"));
421 else if (src_num
> 1)
422 fprintf (out
, _(" %d shift/reduce conflicts"), src_num
);
424 if (src_num
> 0 && rrc_num
> 0)
425 fprintf (out
, _(" and"));
428 fprintf (out
, _(" 1 reduce/reduce conflict"));
429 else if (rrc_num
> 1)
430 fprintf (out
, _(" %d reduce/reduce conflicts"), rrc_num
);
437 /*---------------------------------------------.
438 | Compute and give a report on the conflicts. |
439 `---------------------------------------------*/
442 print_conflicts (void)
449 /* Count the total number of conflicts, and if wanted, give a
450 detailed report in FOUTPUT. */
451 for (i
= 0; i
< nstates
; i
++)
455 count_sr_conflicts (i
);
456 count_rr_conflicts (i
);
457 src_total
+= src_count
;
458 rrc_total
+= rrc_count
;
462 fprintf (foutput
, _("State %d contains"), i
);
463 conflict_report (foutput
, src_count
, rrc_count
);
468 /* Report the total number of conflicts on STDERR. */
471 /* If invoked with `--yacc', use the output format specified by
473 fprintf (stderr
, _("conflicts: "));
475 fprintf (stderr
, _(" %d shift/reduce"), src_total
);
476 if (src_total
> 0 && rrc_total
> 0)
477 fprintf (stderr
, ",");
479 fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
);
484 fprintf (stderr
, _("%s contains"), infile
);
485 conflict_report (stderr
, src_total
, rrc_total
);
491 print_reductions (int state
)
506 int default_rule
= 0;
513 for (i
= 0; i
< tokensetsize
; i
++)
516 shiftp
= shift_table
[state
];
520 for (i
= 0; i
< k
; i
++)
522 if (!shiftp
->shifts
[i
])
524 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
527 /* if this state has a shift for the error token,
528 don't use a default rule. */
529 if (symbol
== error_token_number
)
531 SETBIT (shiftset
, symbol
);
535 errp
= err_table
[state
];
539 for (i
= 0; i
< k
; i
++)
543 symbol
= errp
->errs
[i
];
544 SETBIT (shiftset
, symbol
);
548 m
= lookaheads
[state
];
549 n
= lookaheads
[state
+ 1];
551 if (n
- m
== 1 && !nodefault
)
553 default_rule
= LAruleno
[m
];
555 fp1
= LA
+ m
* tokensetsize
;
558 fp4
= lookaheadset
+ tokensetsize
;
561 *fp3
++ = *fp1
++ & *fp2
++;
566 for (i
= 0; i
< ntokens
; i
++)
569 fprintf (foutput
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
570 tags
[i
], default_rule
, tags
[rlhs
[default_rule
]]);
580 fprintf (foutput
, _(" $default\treduce using rule %d (%s)\n\n"),
581 default_rule
, tags
[rlhs
[default_rule
]]);
587 fp4
= lookaheadset
+ tokensetsize
;
590 for (i
= m
; i
< n
; i
++)
592 fp1
= LA
+ i
* tokensetsize
;
597 *fp3
++ = *fp1
++ & (~(*fp2
++));
602 for (j
= 0; j
< ntokens
; j
++)
619 default_rule
= LAruleno
[i
];
629 for (i
= 0; i
< tokensetsize
; i
++)
635 for (i
= 0; i
< k
; i
++)
637 if (!shiftp
->shifts
[i
])
639 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
642 SETBIT (shiftset
, symbol
);
647 fp1
= LA
+ m
* tokensetsize
;
649 for (i
= 0; i
< ntokens
; i
++)
659 for (j
= m
; j
< n
; j
++)
669 _(" %-4s\treduce using rule %d (%s)\n"),
670 tags
[i
], rule
, tags
[rlhs
[rule
]]);
681 rule
= LAruleno
[default_LA
];
683 _(" %-4s\treduce using rule %d (%s)\n"),
684 tags
[i
], rule
, tags
[rlhs
[rule
]]);
689 _(" %-4s\t[reduce using rule %d (%s)]\n"),
690 tags
[i
], rule
, tags
[rlhs
[rule
]]);
701 /* We tried incrementing just fp1, and just fp2; both seem wrong.
702 It seems necessary to increment both in sync. */
710 fprintf (foutput
, _(" $default\treduce using rule %d (%s)\n"),
711 default_rule
, tags
[rlhs
[default_rule
]]);
714 putc ('\n', foutput
);
720 finalize_conflicts (void)