]>
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"
32 int any_conflicts
= 0;
34 int expected_conflicts
;
35 static char *conflicts
;
37 static unsigned *shiftset
;
38 static unsigned *lookaheadset
;
46 log_resolution (int state
, int LAno
, int token
, char *resolution
)
51 Conflict in state %d between rule %d and token %s resolved as %s.\n"),
52 state
, LAruleno
[LAno
], tags
[token
], resolution
);
56 /*------------------------------------------------------------------.
57 | Turn off the shift recorded for the specified token in the |
58 | specified state. Used when we resolve a shift-reduce conflict in |
59 | favor of the reduction. |
60 `------------------------------------------------------------------*/
63 flush_shift (int state
, int token
)
68 shiftp
= shift_table
[state
];
73 for (i
= 0; i
< k
; i
++)
76 && token
== accessing_symbol
[shiftp
->shifts
[i
]])
77 (shiftp
->shifts
[i
]) = 0;
83 /*------------------------------------------------------------------.
84 | Attempt to resolve shift-reduce conflict for one rule by means of |
85 | precedence declarations. It has already been checked that the |
86 | rule has a precedence. A conflict is resolved by modifying the |
87 | shift or reduce tables so that there is no longer a conflict. |
88 `------------------------------------------------------------------*/
91 resolve_sr_conflict (int state
, int lookaheadnum
)
98 errs
*errp
= (errs
*) xcalloc (sizeof (errs
) + ntokens
* sizeof (short), 1);
99 short *errtokens
= errp
->errs
;
101 /* find the rule to reduce by to get precedence of reduction */
102 redprec
= rprec
[LAruleno
[lookaheadnum
]];
105 fp1
= LA
+ lookaheadnum
* tokensetsize
;
107 for (i
= 0; i
< ntokens
; i
++)
109 if ((mask
& *fp2
& *fp1
) && sprec
[i
])
110 /* Shift-reduce conflict occurs for token number i
111 and it has a precedence.
112 The precedence of shifting is that of token i. */
114 if (sprec
[i
] < redprec
)
116 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
117 *fp2
&= ~mask
; /* flush the shift for this token */
118 flush_shift (state
, i
);
120 else if (sprec
[i
] > redprec
)
122 log_resolution (state
, lookaheadnum
, i
, _("shift"));
123 *fp1
&= ~mask
; /* flush the reduce for this token */
127 /* Matching precedence levels.
128 For left association, keep only the reduction.
129 For right association, keep only the shift.
130 For nonassociation, keep neither. */
135 log_resolution (state
, lookaheadnum
, i
, _("shift"));
139 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
143 log_resolution (state
, lookaheadnum
, i
, _("an error"));
147 if (sassoc
[i
] != right_assoc
)
149 *fp2
&= ~mask
; /* flush the shift for this token */
150 flush_shift (state
, i
);
152 if (sassoc
[i
] != left_assoc
)
154 *fp1
&= ~mask
; /* flush the reduce for this token */
156 if (sassoc
[i
] == non_assoc
)
158 /* Record an explicit error for this token. */
172 errp
->nerrs
= errtokens
- errp
->errs
;
175 /* Some tokens have been explicitly made errors. Allocate
176 a permanent errs structure for this state, to record them. */
177 i
= (char *) errtokens
- (char *) errp
;
178 err_table
[state
] = (errs
*) xcalloc ((unsigned int) i
, 1);
179 bcopy (errp
, err_table
[state
], i
);
182 err_table
[state
] = 0;
188 set_conflicts (int state
)
199 if (consistent
[state
])
202 for (i
= 0; i
< tokensetsize
; i
++)
205 shiftp
= shift_table
[state
];
209 for (i
= 0; i
< k
; i
++)
211 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
214 SETBIT (lookaheadset
, symbol
);
218 k
= lookaheads
[state
+ 1];
219 fp4
= lookaheadset
+ tokensetsize
;
221 /* Loop over all rules which require lookahead in this state. First
222 check for shift-reduce conflict, and try to resolve using
224 for (i
= lookaheads
[state
]; i
< k
; i
++)
225 if (rprec
[LAruleno
[i
]])
227 fp1
= LA
+ i
* tokensetsize
;
235 resolve_sr_conflict (state
, i
);
242 /* Loop over all rules which require lookahead in this state. Check
243 for conflicts not resolved above. */
244 for (i
= lookaheads
[state
]; i
< k
; i
++)
246 fp1
= LA
+ i
* tokensetsize
;
254 conflicts
[state
] = 1;
268 initialize_conflicts (void)
272 conflicts
= XCALLOC (char, nstates
);
273 shiftset
= XCALLOC (unsigned, tokensetsize
);
274 lookaheadset
= XCALLOC (unsigned, tokensetsize
);
276 err_table
= XCALLOC (errs
*, nstates
);
280 for (i
= 0; i
< nstates
; i
++)
285 /*---------------------------------------------.
286 | Count the number of shift/reduce conflicts. |
287 `---------------------------------------------*/
290 count_sr_conflicts (int state
)
303 shiftp
= shift_table
[state
];
307 for (i
= 0; i
< tokensetsize
; i
++)
314 for (i
= 0; i
< k
; i
++)
316 if (!shiftp
->shifts
[i
])
318 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
321 SETBIT (shiftset
, symbol
);
324 k
= lookaheads
[state
+ 1];
325 fp3
= lookaheadset
+ tokensetsize
;
327 for (i
= lookaheads
[state
]; i
< k
; i
++)
329 fp1
= LA
+ i
* tokensetsize
;
344 for (i
= 0; i
< ntokens
; i
++)
359 /*----------------------------------------------.
360 | Count the number of reduce/reduce conflicts. |
361 `----------------------------------------------*/
364 count_rr_conflicts (int state
)
377 m
= lookaheads
[state
];
378 n
= lookaheads
[state
+ 1];
384 baseword
= LA
+ m
* tokensetsize
;
385 for (i
= 0; i
< ntokens
; i
++)
390 for (j
= m
; j
< n
; j
++)
395 wordp
+= tokensetsize
;
410 /*----------------------------------------------------------.
411 | Output to OUT a human readable report on shift/reduce and |
412 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
413 `----------------------------------------------------------*/
416 conflict_report (FILE *out
, int src_num
, int rrc_num
)
419 fprintf (out
, _(" 1 shift/reduce conflict"));
420 else if (src_num
> 1)
421 fprintf (out
, _(" %d shift/reduce conflicts"), src_num
);
423 if (src_num
> 0 && rrc_num
> 0)
424 fprintf (out
, _(" and"));
427 fprintf (out
, _(" 1 reduce/reduce conflict"));
428 else if (rrc_num
> 1)
429 fprintf (out
, _(" %d reduce/reduce conflicts"), rrc_num
);
436 /*---------------------------------------------.
437 | Compute and give a report on the conflicts. |
438 `---------------------------------------------*/
441 print_conflicts (void)
448 /* Count the total number of conflicts, and if wanted, give a
449 detailed report in FOUTPUT. */
450 for (i
= 0; i
< nstates
; i
++)
454 count_sr_conflicts (i
);
455 count_rr_conflicts (i
);
456 src_total
+= src_count
;
457 rrc_total
+= rrc_count
;
461 fprintf (foutput
, _("State %d contains"), i
);
462 conflict_report (foutput
, src_count
, rrc_count
);
467 /* Report the total number of conflicts on STDERR. */
470 /* If invoked with `--yacc', use the output format specified by
472 fprintf (stderr
, _("conflicts: "));
474 fprintf (stderr
, _(" %d shift/reduce"), src_total
);
475 if (src_total
> 0 && rrc_total
> 0)
476 fprintf (stderr
, ",");
478 fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
);
483 fprintf (stderr
, _("%s contains"), infile
);
484 conflict_report (stderr
, src_total
, rrc_total
);
490 print_reductions (int state
)
505 int default_rule
= 0;
512 for (i
= 0; i
< tokensetsize
; i
++)
515 shiftp
= shift_table
[state
];
519 for (i
= 0; i
< k
; i
++)
521 if (!shiftp
->shifts
[i
])
523 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
526 /* if this state has a shift for the error token,
527 don't use a default rule. */
528 if (symbol
== error_token_number
)
530 SETBIT (shiftset
, symbol
);
534 errp
= err_table
[state
];
538 for (i
= 0; i
< k
; i
++)
542 symbol
= errp
->errs
[i
];
543 SETBIT (shiftset
, symbol
);
547 m
= lookaheads
[state
];
548 n
= lookaheads
[state
+ 1];
550 if (n
- m
== 1 && !nodefault
)
552 default_rule
= LAruleno
[m
];
554 fp1
= LA
+ m
* tokensetsize
;
557 fp4
= lookaheadset
+ tokensetsize
;
560 *fp3
++ = *fp1
++ & *fp2
++;
565 for (i
= 0; i
< ntokens
; i
++)
568 fprintf (foutput
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
569 tags
[i
], default_rule
, tags
[rlhs
[default_rule
]]);
579 fprintf (foutput
, _(" $default\treduce using rule %d (%s)\n\n"),
580 default_rule
, tags
[rlhs
[default_rule
]]);
586 fp4
= lookaheadset
+ tokensetsize
;
589 for (i
= m
; i
< n
; i
++)
591 fp1
= LA
+ i
* tokensetsize
;
596 *fp3
++ = *fp1
++ & (~(*fp2
++));
601 for (j
= 0; j
< ntokens
; j
++)
618 default_rule
= LAruleno
[i
];
628 for (i
= 0; i
< tokensetsize
; i
++)
634 for (i
= 0; i
< k
; i
++)
636 if (!shiftp
->shifts
[i
])
638 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
641 SETBIT (shiftset
, symbol
);
646 fp1
= LA
+ m
* tokensetsize
;
648 for (i
= 0; i
< ntokens
; i
++)
658 for (j
= m
; j
< n
; j
++)
668 _(" %-4s\treduce using rule %d (%s)\n"),
669 tags
[i
], rule
, tags
[rlhs
[rule
]]);
680 rule
= LAruleno
[default_LA
];
682 _(" %-4s\treduce using rule %d (%s)\n"),
683 tags
[i
], rule
, tags
[rlhs
[rule
]]);
688 _(" %-4s\t[reduce using rule %d (%s)]\n"),
689 tags
[i
], rule
, tags
[rlhs
[rule
]]);
700 /* We tried incrementing just fp1, and just fp2; both seem wrong.
701 It seems necessary to increment both in sync. */
709 fprintf (foutput
, _(" $default\treduce using rule %d (%s)\n"),
710 default_rule
, tags
[rlhs
[default_rule
]]);
713 putc ('\n', foutput
);
719 finalize_conflicts (void)
723 XFREE (lookaheadset
);