]>
git.saurik.com Git - bison.git/blob - src/conflicts.c
1 /* Find and resolve or report look-ahead conflicts for bison,
2 Copyright 1984, 1989, 1992, 2000, 2001 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 errs
**err_table
= NULL
;
33 /* -1 stands for not specified. */
34 int expected_conflicts
= -1;
35 static char *conflicts
= NULL
;
37 static unsigned *shiftset
= NULL
;
38 static unsigned *lookaheadset
= NULL
;
42 log_resolution (int state
, int LAno
, int token
, char *resolution
)
44 obstack_fgrow4 (&output_obstack
,
46 Conflict in state %d between rule %d and token %s resolved as %s.\n"),
47 state
, LAruleno
[LAno
], tags
[token
], resolution
);
51 /*------------------------------------------------------------------.
52 | Turn off the shift recorded for the specified token in the |
53 | specified state. Used when we resolve a shift-reduce conflict in |
54 | favor of the reduction. |
55 `------------------------------------------------------------------*/
58 flush_shift (int state
, int token
)
63 shiftp
= state_table
[state
].shift_table
;
68 for (i
= 0; i
< k
; i
++)
71 && token
== state_table
[shiftp
->shifts
[i
]].accessing_symbol
)
72 (shiftp
->shifts
[i
]) = 0;
78 /*------------------------------------------------------------------.
79 | Attempt to resolve shift-reduce conflict for one rule by means of |
80 | precedence declarations. It has already been checked that the |
81 | rule has a precedence. A conflict is resolved by modifying the |
82 | shift or reduce tables so that there is no longer a conflict. |
83 `------------------------------------------------------------------*/
86 resolve_sr_conflict (int state
, int lookaheadnum
)
93 errs
*errp
= ERRS_ALLOC (ntokens
+ 1);
94 short *errtokens
= errp
->errs
;
96 /* find the rule to reduce by to get precedence of reduction */
97 redprec
= rule_table
[LAruleno
[lookaheadnum
]].prec
;
100 fp1
= LA (lookaheadnum
);
102 for (i
= 0; i
< ntokens
; i
++)
104 if ((mask
& *fp2
& *fp1
) && sprec
[i
])
105 /* Shift-reduce conflict occurs for token number i
106 and it has a precedence.
107 The precedence of shifting is that of token i. */
109 if (sprec
[i
] < redprec
)
111 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
112 *fp2
&= ~mask
; /* flush the shift for this token */
113 flush_shift (state
, i
);
115 else if (sprec
[i
] > redprec
)
117 log_resolution (state
, lookaheadnum
, i
, _("shift"));
118 *fp1
&= ~mask
; /* flush the reduce for this token */
122 /* Matching precedence levels.
123 For left association, keep only the reduction.
124 For right association, keep only the shift.
125 For nonassociation, keep neither. */
130 log_resolution (state
, lookaheadnum
, i
, _("shift"));
134 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
138 log_resolution (state
, lookaheadnum
, i
, _("an error"));
142 if (sassoc
[i
] != right_assoc
)
144 *fp2
&= ~mask
; /* flush the shift for this token */
145 flush_shift (state
, i
);
147 if (sassoc
[i
] != left_assoc
)
149 *fp1
&= ~mask
; /* flush the reduce for this token */
151 if (sassoc
[i
] == non_assoc
)
153 /* Record an explicit error for this token. */
167 errp
->nerrs
= errtokens
- errp
->errs
;
170 /* Some tokens have been explicitly made errors. Allocate
171 a permanent errs structure for this state, to record them. */
172 i
= (char *) errtokens
- (char *) errp
;
173 err_table
[state
] = ERRS_ALLOC (i
+ 1);
174 bcopy (errp
, err_table
[state
], i
);
177 err_table
[state
] = 0;
183 set_conflicts (int state
)
188 if (state_table
[state
].consistent
)
191 for (i
= 0; i
< tokensetsize
; i
++)
194 shiftp
= state_table
[state
].shift_table
;
196 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
197 SETBIT (lookaheadset
, SHIFT_SYMBOL (shiftp
, i
));
199 /* Loop over all rules which require lookahead in this state. First
200 check for shift-reduce conflict, and try to resolve using
202 for (i
= state_table
[state
].lookaheads
;
203 i
< state_table
[state
+ 1].lookaheads
;
205 if (rule_table
[LAruleno
[i
]].prec
)
206 for (j
= 0; j
< tokensetsize
; ++j
)
207 if (LA (i
)[j
] & lookaheadset
[j
])
209 resolve_sr_conflict (state
, i
);
214 /* Loop over all rules which require lookahead in this state. Check
215 for conflicts not resolved above. */
216 for (i
= state_table
[state
].lookaheads
;
217 i
< state_table
[state
+ 1].lookaheads
;
220 for (j
= 0; j
< tokensetsize
; ++j
)
221 if (LA (i
)[j
] & lookaheadset
[j
])
222 conflicts
[state
] = 1;
224 for (j
= 0; j
< tokensetsize
; ++j
)
225 lookaheadset
[j
] |= LA (i
)[j
];
230 solve_conflicts (void)
234 conflicts
= XCALLOC (char, nstates
);
235 shiftset
= XCALLOC (unsigned, tokensetsize
);
236 lookaheadset
= XCALLOC (unsigned, tokensetsize
);
238 err_table
= XCALLOC (errs
*, nstates
);
240 for (i
= 0; i
< nstates
; i
++)
245 /*---------------------------------------------.
246 | Count the number of shift/reduce conflicts. |
247 `---------------------------------------------*/
250 count_sr_conflicts (int state
)
254 shifts
*shiftp
= state_table
[state
].shift_table
;
259 for (i
= 0; i
< tokensetsize
; i
++)
265 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
266 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
268 for (i
= state_table
[state
].lookaheads
;
269 i
< state_table
[state
+ 1].lookaheads
;
271 for (k
= 0; k
< tokensetsize
; ++k
)
272 lookaheadset
[k
] |= LA (i
)[k
];
274 for (k
= 0; k
< tokensetsize
; ++k
)
275 lookaheadset
[k
] &= shiftset
[k
];
277 for (i
= 0; i
< ntokens
; i
++)
278 if (BITISSET (lookaheadset
, i
))
285 /*----------------------------------------------.
286 | Count the number of reduce/reduce conflicts. |
287 `----------------------------------------------*/
290 count_rr_conflicts (int state
)
295 int m
= state_table
[state
].lookaheads
;
296 int n
= state_table
[state
+ 1].lookaheads
;
301 for (i
= 0; i
< ntokens
; i
++)
305 for (j
= m
; j
< n
; j
++)
306 if (BITISSET (LA (m
), j
))
316 /*--------------------------------------------------------------.
317 | Return a human readable string which reports shift/reduce and |
318 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
319 `--------------------------------------------------------------*/
322 conflict_report (int src_num
, int rrc_num
)
324 static char res
[4096];
329 sprintf (cp
, ngettext ("%d shift/reduce conflict",
330 "%d shift/reduce conflicts", src_num
), src_num
);
334 if (src_num
> 0 && rrc_num
> 0)
336 sprintf (cp
, " %s ", _("and"));
342 sprintf (cp
, ngettext ("%d reduce/reduce conflict",
343 "%d reduce/reduce conflicts", rrc_num
), rrc_num
);
355 /*-----------------------------------------------------------.
356 | Output the detailed description of states with conflicts. |
357 `-----------------------------------------------------------*/
360 conflicts_output (FILE *out
)
362 bool printed_sth
= FALSE
;
364 for (i
= 0; i
< nstates
; i
++)
367 fprintf (out
, _("State %d contains "), i
);
368 fputs (conflict_report (count_sr_conflicts (i
),
369 count_rr_conflicts (i
)), out
);
377 /*------------------------------------------.
378 | Reporting the total number of conflicts. |
379 `------------------------------------------*/
382 conflicts_print (void)
386 /* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is
387 not set, and then we want 0 SR, or else it is specified, in which
388 case we want equality. */
394 /* Conflicts by state. */
395 for (i
= 0; i
< nstates
; i
++)
398 src_total
+= count_sr_conflicts (i
);
399 rrc_total
+= count_rr_conflicts (i
);
402 src_ok
= src_total
== (expected_conflicts
== -1 ? 0 : expected_conflicts
);
404 /* If there are no RR conflicts, and as many SR conflicts as
405 expected, then there is nothing to report. */
406 if (!rrc_total
&& src_ok
)
409 /* Report the total number of conflicts on STDERR. */
412 /* If invoked with `--yacc', use the output format specified by
414 fprintf (stderr
, _("conflicts: "));
416 fprintf (stderr
, _(" %d shift/reduce"), src_total
);
417 if (src_total
> 0 && rrc_total
> 0)
418 fprintf (stderr
, ",");
420 fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
);
425 fprintf (stderr
, _("%s contains "), infile
);
426 fputs (conflict_report (src_total
, rrc_total
), stderr
);
429 if (expected_conflicts
!= -1 && !src_ok
)
431 complain_message_count
++;
432 fprintf (stderr
, ngettext ("expected %d shift/reduce conflict\n",
433 "expected %d shift/reduce conflicts\n",
441 print_reductions (FILE *out
, int state
)
451 for (i
= 0; i
< tokensetsize
; i
++)
454 shiftp
= state_table
[state
].shift_table
;
456 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
458 /* if this state has a shift for the error token, don't use a
460 if (SHIFT_IS_ERROR (shiftp
, i
))
462 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
465 errp
= err_table
[state
];
467 for (i
= 0; i
< errp
->nerrs
; i
++)
469 SETBIT (shiftset
, errp
->errs
[i
]);
471 m
= state_table
[state
].lookaheads
;
472 n
= state_table
[state
+ 1].lookaheads
;
474 if (n
- m
== 1 && !nodefault
)
477 int default_rule
= LAruleno
[m
];
479 for (k
= 0; k
< tokensetsize
; ++k
)
480 lookaheadset
[k
] = LA (m
)[k
] & shiftset
[k
];
482 for (i
= 0; i
< ntokens
; i
++)
483 if (BITISSET (lookaheadset
, i
))
484 fprintf (out
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
485 tags
[i
], default_rule
,
486 tags
[rule_table
[default_rule
].lhs
]);
488 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
489 default_rule
, tags
[rule_table
[default_rule
].lhs
]);
497 int default_rule
= 0;
500 for (i
= m
; i
< n
; i
++)
504 for (k
= 0; k
< tokensetsize
; ++k
)
505 lookaheadset
[k
] = LA (i
)[k
] & ~shiftset
[k
];
507 for (j
= 0; j
< ntokens
; j
++)
508 if (BITISSET (lookaheadset
, j
))
515 default_rule
= LAruleno
[i
];
518 for (k
= 0; k
< tokensetsize
; ++k
)
519 shiftset
[k
] |= lookaheadset
[k
];
522 for (i
= 0; i
< tokensetsize
; i
++)
526 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
527 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
529 for (i
= 0; i
< ntokens
; i
++)
532 int count
= BITISSET (shiftset
, i
);
534 for (j
= m
; j
< n
; j
++)
536 if (BITISSET (LA (m
), j
))
542 _(" %-4s\treduce using rule %d (%s)\n"),
545 tags
[rule_table
[LAruleno
[j
]].lhs
]);
555 _(" %-4s\treduce using rule %d (%s)\n"),
557 LAruleno
[default_LA
],
558 tags
[rule_table
[LAruleno
[default_LA
]].lhs
]);
561 _(" %-4s\t[reduce using rule %d (%s)]\n"),
564 tags
[rule_table
[LAruleno
[j
]].lhs
]);
571 fprintf (out
, _(" $default\treduce using rule %d (%s)\n"),
572 default_rule
, tags
[rule_table
[default_rule
].lhs
]);
578 free_conflicts (void)
582 XFREE (lookaheadset
);