]>
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
)
189 if (state_table
[state
].consistent
)
192 for (i
= 0; i
< tokensetsize
; i
++)
195 shiftp
= state_table
[state
].shift_table
;
197 for (i
= 0; i
< shiftp
->nshifts
; i
++)
199 symbol
= state_table
[shiftp
->shifts
[i
]].accessing_symbol
;
202 SETBIT (lookaheadset
, symbol
);
205 /* Loop over all rules which require lookahead in this state. First
206 check for shift-reduce conflict, and try to resolve using
208 for (i
= state_table
[state
].lookaheads
;
209 i
< state_table
[state
+ 1].lookaheads
;
211 if (rule_table
[LAruleno
[i
]].prec
)
212 for (j
= 0; j
< tokensetsize
; ++j
)
213 if (LA (i
)[j
] & lookaheadset
[j
])
215 resolve_sr_conflict (state
, i
);
220 /* Loop over all rules which require lookahead in this state. Check
221 for conflicts not resolved above. */
222 for (i
= state_table
[state
].lookaheads
;
223 i
< state_table
[state
+ 1].lookaheads
;
226 for (j
= 0; j
< tokensetsize
; ++j
)
227 if (LA (i
)[j
] & lookaheadset
[j
])
228 conflicts
[state
] = 1;
230 for (j
= 0; j
< tokensetsize
; ++j
)
231 lookaheadset
[j
] |= LA (i
)[j
];
236 solve_conflicts (void)
240 conflicts
= XCALLOC (char, nstates
);
241 shiftset
= XCALLOC (unsigned, tokensetsize
);
242 lookaheadset
= XCALLOC (unsigned, tokensetsize
);
244 err_table
= XCALLOC (errs
*, nstates
);
246 for (i
= 0; i
< nstates
; i
++)
251 /*---------------------------------------------.
252 | Count the number of shift/reduce conflicts. |
253 `---------------------------------------------*/
256 count_sr_conflicts (int state
)
269 shiftp
= state_table
[state
].shift_table
;
273 for (i
= 0; i
< tokensetsize
; i
++)
280 for (i
= 0; i
< k
; i
++)
282 if (!shiftp
->shifts
[i
])
284 symbol
= state_table
[shiftp
->shifts
[i
]].accessing_symbol
;
287 SETBIT (shiftset
, symbol
);
290 k
= state_table
[state
+ 1].lookaheads
;
291 fp3
= lookaheadset
+ tokensetsize
;
293 for (i
= state_table
[state
].lookaheads
; i
< k
; i
++)
310 for (i
= 0; i
< ntokens
; i
++)
327 /*----------------------------------------------.
328 | Count the number of reduce/reduce conflicts. |
329 `----------------------------------------------*/
332 count_rr_conflicts (int state
)
340 int m
= state_table
[state
].lookaheads
;
341 int n
= state_table
[state
+ 1].lookaheads
;
348 for (i
= 0; i
< ntokens
; i
++)
350 unsigned *wordp
= baseword
;
354 for (j
= m
; j
< n
; j
++)
359 wordp
+= tokensetsize
;
376 /*--------------------------------------------------------------.
377 | Return a human readable string which reports shift/reduce and |
378 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
379 `--------------------------------------------------------------*/
382 conflict_report (int src_num
, int rrc_num
)
384 static char res
[4096];
389 sprintf (cp
, ngettext ("%d shift/reduce conflict",
390 "%d shift/reduce conflicts", src_num
), src_num
);
394 if (src_num
> 0 && rrc_num
> 0)
396 sprintf (cp
, " %s ", _("and"));
402 sprintf (cp
, ngettext ("%d reduce/reduce conflict",
403 "%d reduce/reduce conflicts", rrc_num
), rrc_num
);
415 /*-----------------------------------------------------------.
416 | Output the detailed description of states with conflicts. |
417 `-----------------------------------------------------------*/
420 conflicts_output (FILE *out
)
423 for (i
= 0; i
< nstates
; i
++)
426 fprintf (out
, _("State %d contains "), i
);
427 fputs (conflict_report (count_sr_conflicts (i
),
428 count_rr_conflicts (i
)), out
);
433 /*------------------------------------------.
434 | Reporting the total number of conflicts. |
435 `------------------------------------------*/
438 conflicts_print (void)
445 /* Conflicts by state. */
446 for (i
= 0; i
< nstates
; i
++)
449 src_total
+= count_sr_conflicts (i
);
450 rrc_total
+= count_rr_conflicts (i
);
453 /* Report the total number of conflicts on STDERR. */
454 if (src_total
|| rrc_total
)
458 /* If invoked with `--yacc', use the output format specified by
460 fprintf (stderr
, _("conflicts: "));
462 fprintf (stderr
, _(" %d shift/reduce"), src_total
);
463 if (src_total
> 0 && rrc_total
> 0)
464 fprintf (stderr
, ",");
466 fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
);
471 fprintf (stderr
, _("%s contains "), infile
);
472 fputs (conflict_report (src_total
, rrc_total
), stderr
);
476 if (expected_conflicts
!= -1
477 && src_total
!= expected_conflicts
)
479 complain_message_count
++;
480 fprintf (stderr
, ngettext ("expected %d shift/reduce conflict\n",
481 "expected %d shift/reduce conflicts\n",
489 print_reductions (FILE *out
, int state
)
504 int default_rule
= 0;
511 for (i
= 0; i
< tokensetsize
; i
++)
514 shiftp
= state_table
[state
].shift_table
;
518 for (i
= 0; i
< k
; i
++)
520 if (!shiftp
->shifts
[i
])
522 symbol
= state_table
[shiftp
->shifts
[i
]].accessing_symbol
;
525 /* if this state has a shift for the error token,
526 don't use a default rule. */
527 if (symbol
== error_token_number
)
529 SETBIT (shiftset
, symbol
);
533 errp
= err_table
[state
];
537 for (i
= 0; i
< k
; i
++)
541 symbol
= errp
->errs
[i
];
542 SETBIT (shiftset
, symbol
);
546 m
= state_table
[state
].lookaheads
;
547 n
= state_table
[state
+ 1].lookaheads
;
549 if (n
- m
== 1 && !nodefault
)
551 default_rule
= LAruleno
[m
];
556 fp4
= lookaheadset
+ tokensetsize
;
559 *fp3
++ = *fp1
++ & *fp2
++;
564 for (i
= 0; i
< ntokens
; i
++)
567 fprintf (out
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
568 tags
[i
], default_rule
,
569 tags
[rule_table
[default_rule
].lhs
]);
579 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
580 default_rule
, tags
[rule_table
[default_rule
].lhs
]);
586 fp4
= lookaheadset
+ tokensetsize
;
589 for (i
= m
; i
< n
; i
++)
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
= state_table
[shiftp
->shifts
[i
]].accessing_symbol
;
641 SETBIT (shiftset
, symbol
);
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
[rule_table
[rule
].lhs
]);
680 rule
= LAruleno
[default_LA
];
682 _(" %-4s\treduce using rule %d (%s)\n"),
683 tags
[i
], rule
, tags
[rule_table
[rule
].lhs
]);
688 _(" %-4s\t[reduce using rule %d (%s)]\n"),
689 tags
[i
], rule
, tags
[rule_table
[rule
].lhs
]);
700 /* We tried incrementing just fp1, and just fp2; both seem wrong.
701 It seems necessary to increment both in sync. */
708 fprintf (out
, _(" $default\treduce using rule %d (%s)\n"),
709 default_rule
, tags
[rule_table
[default_rule
].lhs
]);
715 free_conflicts (void)
719 XFREE (lookaheadset
);