]>
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 int any_conflicts
= 0;
33 errs
**err_table
= NULL
;
34 int expected_conflicts
;
35 static char *conflicts
= NULL
;
37 static unsigned *shiftset
= NULL
;
38 static unsigned *lookaheadset
= NULL
;
46 log_resolution (int state
, int LAno
, int token
, char *resolution
)
48 obstack_fgrow4 (&output_obstack
,
50 Conflict in state %d between rule %d and token %s resolved as %s.\n"),
51 state
, LAruleno
[LAno
], tags
[token
], resolution
);
55 /*------------------------------------------------------------------.
56 | Turn off the shift recorded for the specified token in the |
57 | specified state. Used when we resolve a shift-reduce conflict in |
58 | favor of the reduction. |
59 `------------------------------------------------------------------*/
62 flush_shift (int state
, int token
)
67 shiftp
= shift_table
[state
];
72 for (i
= 0; i
< k
; i
++)
75 && token
== accessing_symbol
[shiftp
->shifts
[i
]])
76 (shiftp
->shifts
[i
]) = 0;
82 /*------------------------------------------------------------------.
83 | Attempt to resolve shift-reduce conflict for one rule by means of |
84 | precedence declarations. It has already been checked that the |
85 | rule has a precedence. A conflict is resolved by modifying the |
86 | shift or reduce tables so that there is no longer a conflict. |
87 `------------------------------------------------------------------*/
90 resolve_sr_conflict (int state
, int lookaheadnum
)
97 errs
*errp
= (errs
*) xcalloc (sizeof (errs
) + ntokens
* sizeof (short), 1);
98 short *errtokens
= errp
->errs
;
100 /* find the rule to reduce by to get precedence of reduction */
101 redprec
= rprec
[LAruleno
[lookaheadnum
]];
104 fp1
= LA
+ lookaheadnum
* tokensetsize
;
106 for (i
= 0; i
< ntokens
; i
++)
108 if ((mask
& *fp2
& *fp1
) && sprec
[i
])
109 /* Shift-reduce conflict occurs for token number i
110 and it has a precedence.
111 The precedence of shifting is that of token i. */
113 if (sprec
[i
] < redprec
)
115 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
116 *fp2
&= ~mask
; /* flush the shift for this token */
117 flush_shift (state
, i
);
119 else if (sprec
[i
] > redprec
)
121 log_resolution (state
, lookaheadnum
, i
, _("shift"));
122 *fp1
&= ~mask
; /* flush the reduce for this token */
126 /* Matching precedence levels.
127 For left association, keep only the reduction.
128 For right association, keep only the shift.
129 For nonassociation, keep neither. */
134 log_resolution (state
, lookaheadnum
, i
, _("shift"));
138 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
142 log_resolution (state
, lookaheadnum
, i
, _("an error"));
146 if (sassoc
[i
] != right_assoc
)
148 *fp2
&= ~mask
; /* flush the shift for this token */
149 flush_shift (state
, i
);
151 if (sassoc
[i
] != left_assoc
)
153 *fp1
&= ~mask
; /* flush the reduce for this token */
155 if (sassoc
[i
] == non_assoc
)
157 /* Record an explicit error for this token. */
171 errp
->nerrs
= errtokens
- errp
->errs
;
174 /* Some tokens have been explicitly made errors. Allocate
175 a permanent errs structure for this state, to record them. */
176 i
= (char *) errtokens
- (char *) errp
;
177 err_table
[state
] = (errs
*) xcalloc ((unsigned int) i
, 1);
178 bcopy (errp
, err_table
[state
], i
);
181 err_table
[state
] = 0;
187 set_conflicts (int state
)
198 if (consistent
[state
])
201 for (i
= 0; i
< tokensetsize
; i
++)
204 shiftp
= shift_table
[state
];
208 for (i
= 0; i
< k
; i
++)
210 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
213 SETBIT (lookaheadset
, symbol
);
217 k
= lookaheads
[state
+ 1];
218 fp4
= lookaheadset
+ tokensetsize
;
220 /* Loop over all rules which require lookahead in this state. First
221 check for shift-reduce conflict, and try to resolve using
223 for (i
= lookaheads
[state
]; i
< k
; i
++)
224 if (rprec
[LAruleno
[i
]])
226 fp1
= LA
+ i
* tokensetsize
;
234 resolve_sr_conflict (state
, i
);
241 /* Loop over all rules which require lookahead in this state. Check
242 for conflicts not resolved above. */
243 for (i
= lookaheads
[state
]; i
< k
; i
++)
245 fp1
= LA
+ i
* tokensetsize
;
253 conflicts
[state
] = 1;
267 solve_conflicts (void)
271 conflicts
= XCALLOC (char, nstates
);
272 shiftset
= XCALLOC (unsigned, tokensetsize
);
273 lookaheadset
= XCALLOC (unsigned, tokensetsize
);
275 err_table
= XCALLOC (errs
*, nstates
);
279 for (i
= 0; i
< nstates
; i
++)
284 /*---------------------------------------------.
285 | Count the number of shift/reduce conflicts. |
286 `---------------------------------------------*/
289 count_sr_conflicts (int state
)
302 shiftp
= shift_table
[state
];
306 for (i
= 0; i
< tokensetsize
; i
++)
313 for (i
= 0; i
< k
; i
++)
315 if (!shiftp
->shifts
[i
])
317 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
320 SETBIT (shiftset
, symbol
);
323 k
= lookaheads
[state
+ 1];
324 fp3
= lookaheadset
+ tokensetsize
;
326 for (i
= lookaheads
[state
]; i
< k
; i
++)
328 fp1
= LA
+ i
* tokensetsize
;
343 for (i
= 0; i
< ntokens
; i
++)
358 /*----------------------------------------------.
359 | Count the number of reduce/reduce conflicts. |
360 `----------------------------------------------*/
363 count_rr_conflicts (int state
)
376 m
= lookaheads
[state
];
377 n
= lookaheads
[state
+ 1];
383 baseword
= LA
+ m
* tokensetsize
;
384 for (i
= 0; i
< ntokens
; i
++)
389 for (j
= m
; j
< n
; j
++)
394 wordp
+= tokensetsize
;
409 /*--------------------------------------------------------------.
410 | Return a human readable string which reports shift/reduce and |
411 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
412 `--------------------------------------------------------------*/
415 conflict_report (int src_num
, int rrc_num
)
417 static char res
[4096];
422 sprintf (cp
, _(" 1 shift/reduce conflict"));
425 else if (src_num
> 1)
427 sprintf (cp
, _(" %d shift/reduce conflicts"), src_num
);
431 if (src_num
> 0 && rrc_num
> 0)
433 sprintf (cp
, _(" and"));
439 sprintf (cp
, _(" 1 reduce/reduce conflict"));
442 else if (rrc_num
> 1)
444 sprintf (cp
, _(" %d reduce/reduce conflicts"), rrc_num
);
456 /*---------------------------------------------.
457 | Compute and give a report on the conflicts. |
458 `---------------------------------------------*/
461 print_conflicts (FILE *out
)
468 /* Count the total number of conflicts, and if wanted, give a
469 detailed report in FOUTPUT. */
470 for (i
= 0; i
< nstates
; i
++)
474 count_sr_conflicts (i
);
475 count_rr_conflicts (i
);
476 src_total
+= src_count
;
477 rrc_total
+= rrc_count
;
481 fprintf (out
, _("State %d contains"), i
);
482 fputs (conflict_report (src_count
, rrc_count
), out
);
487 /* Report the total number of conflicts on STDERR. */
490 /* If invoked with `--yacc', use the output format specified by
492 fprintf (stderr
, _("conflicts: "));
494 fprintf (stderr
, _(" %d shift/reduce"), src_total
);
495 if (src_total
> 0 && rrc_total
> 0)
496 fprintf (stderr
, ",");
498 fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
);
503 fprintf (stderr
, _("%s contains"), infile
);
504 fputs (conflict_report (src_total
, rrc_total
), stderr
);
510 print_reductions (int state
)
525 int default_rule
= 0;
532 for (i
= 0; i
< tokensetsize
; i
++)
535 shiftp
= shift_table
[state
];
539 for (i
= 0; i
< k
; i
++)
541 if (!shiftp
->shifts
[i
])
543 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
546 /* if this state has a shift for the error token,
547 don't use a default rule. */
548 if (symbol
== error_token_number
)
550 SETBIT (shiftset
, symbol
);
554 errp
= err_table
[state
];
558 for (i
= 0; i
< k
; i
++)
562 symbol
= errp
->errs
[i
];
563 SETBIT (shiftset
, symbol
);
567 m
= lookaheads
[state
];
568 n
= lookaheads
[state
+ 1];
570 if (n
- m
== 1 && !nodefault
)
572 default_rule
= LAruleno
[m
];
574 fp1
= LA
+ m
* tokensetsize
;
577 fp4
= lookaheadset
+ tokensetsize
;
580 *fp3
++ = *fp1
++ & *fp2
++;
585 for (i
= 0; i
< ntokens
; i
++)
588 obstack_fgrow3 (&output_obstack
,
589 _(" %-4s\t[reduce using rule %d (%s)]\n"),
590 tags
[i
], default_rule
, tags
[rlhs
[default_rule
]]);
600 obstack_fgrow2 (&output_obstack
,
601 _(" $default\treduce using rule %d (%s)\n\n"),
602 default_rule
, tags
[rlhs
[default_rule
]]);
608 fp4
= lookaheadset
+ tokensetsize
;
611 for (i
= m
; i
< n
; i
++)
613 fp1
= LA
+ i
* tokensetsize
;
618 *fp3
++ = *fp1
++ & (~(*fp2
++));
623 for (j
= 0; j
< ntokens
; j
++)
640 default_rule
= LAruleno
[i
];
650 for (i
= 0; i
< tokensetsize
; i
++)
656 for (i
= 0; i
< k
; i
++)
658 if (!shiftp
->shifts
[i
])
660 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
663 SETBIT (shiftset
, symbol
);
668 fp1
= LA
+ m
* tokensetsize
;
670 for (i
= 0; i
< ntokens
; i
++)
680 for (j
= m
; j
< n
; j
++)
689 obstack_fgrow3 (&output_obstack
,
690 _(" %-4s\treduce using rule %d (%s)\n"),
691 tags
[i
], rule
, tags
[rlhs
[rule
]]);
702 rule
= LAruleno
[default_LA
];
703 obstack_fgrow3 (&output_obstack
,
704 _(" %-4s\treduce using rule %d (%s)\n"),
705 tags
[i
], rule
, tags
[rlhs
[rule
]]);
709 obstack_fgrow3 (&output_obstack
,
710 _(" %-4s\t[reduce using rule %d (%s)]\n"),
711 tags
[i
], rule
, tags
[rlhs
[rule
]]);
722 /* We tried incrementing just fp1, and just fp2; both seem wrong.
723 It seems necessary to increment both in sync. */
730 obstack_fgrow2 (&output_obstack
,
731 _(" $default\treduce using rule %d (%s)\n"),
732 default_rule
, tags
[rlhs
[default_rule
]]);
734 obstack_1grow (&output_obstack
, '\n');
740 free_conflicts (void)
744 XFREE (lookaheadset
);