]>
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
27 #include "conflicts.h"
31 errs
**err_table
= NULL
;
32 int expected_conflicts
;
33 static char *conflicts
= NULL
;
35 static unsigned *shiftset
= NULL
;
36 static unsigned *lookaheadset
= NULL
;
40 log_resolution (int state
, int LAno
, int token
, char *resolution
)
42 obstack_fgrow4 (&output_obstack
,
44 Conflict in state %d between rule %d and token %s resolved as %s.\n"),
45 state
, LAruleno
[LAno
], tags
[token
], resolution
);
49 /*------------------------------------------------------------------.
50 | Turn off the shift recorded for the specified token in the |
51 | specified state. Used when we resolve a shift-reduce conflict in |
52 | favor of the reduction. |
53 `------------------------------------------------------------------*/
56 flush_shift (int state
, int token
)
61 shiftp
= shift_table
[state
];
66 for (i
= 0; i
< k
; i
++)
69 && token
== accessing_symbol
[shiftp
->shifts
[i
]])
70 (shiftp
->shifts
[i
]) = 0;
76 /*------------------------------------------------------------------.
77 | Attempt to resolve shift-reduce conflict for one rule by means of |
78 | precedence declarations. It has already been checked that the |
79 | rule has a precedence. A conflict is resolved by modifying the |
80 | shift or reduce tables so that there is no longer a conflict. |
81 `------------------------------------------------------------------*/
84 resolve_sr_conflict (int state
, int lookaheadnum
)
91 errs
*errp
= (errs
*) xcalloc (sizeof (errs
) + ntokens
* sizeof (short), 1);
92 short *errtokens
= errp
->errs
;
94 /* find the rule to reduce by to get precedence of reduction */
95 redprec
= rprec
[LAruleno
[lookaheadnum
]];
98 fp1
= LA
+ lookaheadnum
* tokensetsize
;
100 for (i
= 0; i
< ntokens
; i
++)
102 if ((mask
& *fp2
& *fp1
) && sprec
[i
])
103 /* Shift-reduce conflict occurs for token number i
104 and it has a precedence.
105 The precedence of shifting is that of token i. */
107 if (sprec
[i
] < redprec
)
109 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
110 *fp2
&= ~mask
; /* flush the shift for this token */
111 flush_shift (state
, i
);
113 else if (sprec
[i
] > redprec
)
115 log_resolution (state
, lookaheadnum
, i
, _("shift"));
116 *fp1
&= ~mask
; /* flush the reduce for this token */
120 /* Matching precedence levels.
121 For left association, keep only the reduction.
122 For right association, keep only the shift.
123 For nonassociation, keep neither. */
128 log_resolution (state
, lookaheadnum
, i
, _("shift"));
132 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
136 log_resolution (state
, lookaheadnum
, i
, _("an error"));
140 if (sassoc
[i
] != right_assoc
)
142 *fp2
&= ~mask
; /* flush the shift for this token */
143 flush_shift (state
, i
);
145 if (sassoc
[i
] != left_assoc
)
147 *fp1
&= ~mask
; /* flush the reduce for this token */
149 if (sassoc
[i
] == non_assoc
)
151 /* Record an explicit error for this token. */
165 errp
->nerrs
= errtokens
- errp
->errs
;
168 /* Some tokens have been explicitly made errors. Allocate
169 a permanent errs structure for this state, to record them. */
170 i
= (char *) errtokens
- (char *) errp
;
171 err_table
[state
] = (errs
*) xcalloc ((unsigned int) i
, 1);
172 bcopy (errp
, err_table
[state
], i
);
175 err_table
[state
] = 0;
181 set_conflicts (int state
)
192 if (consistent
[state
])
195 for (i
= 0; i
< tokensetsize
; i
++)
198 shiftp
= shift_table
[state
];
202 for (i
= 0; i
< k
; i
++)
204 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
207 SETBIT (lookaheadset
, symbol
);
211 k
= lookaheads
[state
+ 1];
212 fp4
= lookaheadset
+ tokensetsize
;
214 /* Loop over all rules which require lookahead in this state. First
215 check for shift-reduce conflict, and try to resolve using
217 for (i
= lookaheads
[state
]; i
< k
; i
++)
218 if (rprec
[LAruleno
[i
]])
220 fp1
= LA
+ i
* tokensetsize
;
228 resolve_sr_conflict (state
, i
);
235 /* Loop over all rules which require lookahead in this state. Check
236 for conflicts not resolved above. */
237 for (i
= lookaheads
[state
]; i
< k
; i
++)
239 fp1
= LA
+ i
* tokensetsize
;
245 conflicts
[state
] = 1;
256 solve_conflicts (void)
260 conflicts
= XCALLOC (char, nstates
);
261 shiftset
= XCALLOC (unsigned, tokensetsize
);
262 lookaheadset
= XCALLOC (unsigned, tokensetsize
);
264 err_table
= XCALLOC (errs
*, nstates
);
266 for (i
= 0; i
< nstates
; i
++)
271 /*---------------------------------------------.
272 | Count the number of shift/reduce conflicts. |
273 `---------------------------------------------*/
276 count_sr_conflicts (int state
)
289 shiftp
= shift_table
[state
];
293 for (i
= 0; i
< tokensetsize
; i
++)
300 for (i
= 0; i
< k
; i
++)
302 if (!shiftp
->shifts
[i
])
304 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
307 SETBIT (shiftset
, symbol
);
310 k
= lookaheads
[state
+ 1];
311 fp3
= lookaheadset
+ tokensetsize
;
313 for (i
= lookaheads
[state
]; i
< k
; i
++)
315 fp1
= LA
+ i
* tokensetsize
;
330 for (i
= 0; i
< ntokens
; i
++)
347 /*----------------------------------------------.
348 | Count the number of reduce/reduce conflicts. |
349 `----------------------------------------------*/
352 count_rr_conflicts (int state
)
360 int m
= lookaheads
[state
];
361 int n
= lookaheads
[state
+ 1];
367 baseword
= LA
+ m
* tokensetsize
;
368 for (i
= 0; i
< ntokens
; i
++)
370 unsigned *wordp
= baseword
;
374 for (j
= m
; j
< n
; j
++)
379 wordp
+= tokensetsize
;
396 /*--------------------------------------------------------------.
397 | Return a human readable string which reports shift/reduce and |
398 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
399 `--------------------------------------------------------------*/
402 conflict_report (int src_num
, int rrc_num
)
404 static char res
[4096];
409 sprintf (cp
, _(" 1 shift/reduce conflict"));
412 else if (src_num
> 1)
414 sprintf (cp
, _(" %d shift/reduce conflicts"), src_num
);
418 if (src_num
> 0 && rrc_num
> 0)
420 sprintf (cp
, _(" and"));
426 sprintf (cp
, _(" 1 reduce/reduce conflict"));
429 else if (rrc_num
> 1)
431 sprintf (cp
, _(" %d reduce/reduce conflicts"), rrc_num
);
443 /*-----------------------------------------------------------.
444 | Output the detailed description of states with conflicts. |
445 `-----------------------------------------------------------*/
448 conflicts_output (FILE *out
)
451 for (i
= 0; i
< nstates
; i
++)
454 fprintf (out
, _("State %d contains"), i
);
455 fputs (conflict_report (count_sr_conflicts (i
),
456 count_rr_conflicts (i
)), out
);
461 /*------------------------------------------.
462 | Reporting the total number of conflicts. |
463 `------------------------------------------*/
466 conflicts_print (void)
473 /* Conflicts by state. */
474 for (i
= 0; i
< nstates
; i
++)
477 src_total
+= count_sr_conflicts (i
);
478 rrc_total
+= count_rr_conflicts (i
);
481 /* Report the total number of conflicts on STDERR. */
482 if (src_total
|| rrc_total
)
485 /* If invoked with `--yacc', use the output format specified by
487 fprintf (stderr
, _("conflicts: "));
489 fprintf (stderr
, _(" %d shift/reduce"), src_total
);
490 if (src_total
> 0 && rrc_total
> 0)
491 fprintf (stderr
, ",");
493 fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
);
498 fprintf (stderr
, _("%s contains"), infile
);
499 fputs (conflict_report (src_total
, rrc_total
), stderr
);
505 print_reductions (int state
)
520 int default_rule
= 0;
527 for (i
= 0; i
< tokensetsize
; i
++)
530 shiftp
= shift_table
[state
];
534 for (i
= 0; i
< k
; i
++)
536 if (!shiftp
->shifts
[i
])
538 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
541 /* if this state has a shift for the error token,
542 don't use a default rule. */
543 if (symbol
== error_token_number
)
545 SETBIT (shiftset
, symbol
);
549 errp
= err_table
[state
];
553 for (i
= 0; i
< k
; i
++)
557 symbol
= errp
->errs
[i
];
558 SETBIT (shiftset
, symbol
);
562 m
= lookaheads
[state
];
563 n
= lookaheads
[state
+ 1];
565 if (n
- m
== 1 && !nodefault
)
567 default_rule
= LAruleno
[m
];
569 fp1
= LA
+ m
* tokensetsize
;
572 fp4
= lookaheadset
+ tokensetsize
;
575 *fp3
++ = *fp1
++ & *fp2
++;
580 for (i
= 0; i
< ntokens
; i
++)
583 obstack_fgrow3 (&output_obstack
,
584 _(" %-4s\t[reduce using rule %d (%s)]\n"),
585 tags
[i
], default_rule
, tags
[rlhs
[default_rule
]]);
595 obstack_fgrow2 (&output_obstack
,
596 _(" $default\treduce using rule %d (%s)\n\n"),
597 default_rule
, tags
[rlhs
[default_rule
]]);
603 fp4
= lookaheadset
+ tokensetsize
;
606 for (i
= m
; i
< n
; i
++)
608 fp1
= LA
+ i
* tokensetsize
;
613 *fp3
++ = *fp1
++ & (~(*fp2
++));
618 for (j
= 0; j
< ntokens
; j
++)
635 default_rule
= LAruleno
[i
];
645 for (i
= 0; i
< tokensetsize
; i
++)
651 for (i
= 0; i
< k
; i
++)
653 if (!shiftp
->shifts
[i
])
655 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
658 SETBIT (shiftset
, symbol
);
663 fp1
= LA
+ m
* tokensetsize
;
665 for (i
= 0; i
< ntokens
; i
++)
675 for (j
= m
; j
< n
; j
++)
684 obstack_fgrow3 (&output_obstack
,
685 _(" %-4s\treduce using rule %d (%s)\n"),
686 tags
[i
], rule
, tags
[rlhs
[rule
]]);
697 rule
= LAruleno
[default_LA
];
698 obstack_fgrow3 (&output_obstack
,
699 _(" %-4s\treduce using rule %d (%s)\n"),
700 tags
[i
], rule
, tags
[rlhs
[rule
]]);
704 obstack_fgrow3 (&output_obstack
,
705 _(" %-4s\t[reduce using rule %d (%s)]\n"),
706 tags
[i
], rule
, tags
[rlhs
[rule
]]);
717 /* We tried incrementing just fp1, and just fp2; both seem wrong.
718 It seems necessary to increment both in sync. */
725 obstack_fgrow2 (&output_obstack
,
726 _(" $default\treduce using rule %d (%s)\n"),
727 default_rule
, tags
[rlhs
[default_rule
]]);
729 obstack_1grow (&output_obstack
, '\n');
735 free_conflicts (void)
739 XFREE (lookaheadset
);