]>
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
*) xcalloc (sizeof (errs
) + ntokens
* sizeof (short), 1);
94 short *errtokens
= errp
->errs
;
96 /* find the rule to reduce by to get precedence of reduction */
97 redprec
= rprec
[LAruleno
[lookaheadnum
]];
100 fp1
= LA
+ lookaheadnum
* tokensetsize
;
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
*) xcalloc ((unsigned int) i
, 1);
174 bcopy (errp
, err_table
[state
], i
);
177 err_table
[state
] = 0;
183 set_conflicts (int state
)
194 if (consistent
[state
])
197 for (i
= 0; i
< tokensetsize
; i
++)
200 shiftp
= state_table
[state
].shift_table
;
204 for (i
= 0; i
< k
; i
++)
206 symbol
= state_table
[shiftp
->shifts
[i
]].accessing_symbol
;
209 SETBIT (lookaheadset
, symbol
);
213 k
= lookaheads
[state
+ 1];
214 fp4
= lookaheadset
+ tokensetsize
;
216 /* Loop over all rules which require lookahead in this state. First
217 check for shift-reduce conflict, and try to resolve using
219 for (i
= lookaheads
[state
]; i
< k
; i
++)
220 if (rprec
[LAruleno
[i
]])
222 fp1
= LA
+ i
* tokensetsize
;
230 resolve_sr_conflict (state
, i
);
237 /* Loop over all rules which require lookahead in this state. Check
238 for conflicts not resolved above. */
239 for (i
= lookaheads
[state
]; i
< k
; i
++)
241 fp1
= LA
+ i
* tokensetsize
;
247 conflicts
[state
] = 1;
258 solve_conflicts (void)
262 conflicts
= XCALLOC (char, nstates
);
263 shiftset
= XCALLOC (unsigned, tokensetsize
);
264 lookaheadset
= XCALLOC (unsigned, tokensetsize
);
266 err_table
= XCALLOC (errs
*, nstates
);
268 for (i
= 0; i
< nstates
; i
++)
273 /*---------------------------------------------.
274 | Count the number of shift/reduce conflicts. |
275 `---------------------------------------------*/
278 count_sr_conflicts (int state
)
291 shiftp
= state_table
[state
].shift_table
;
295 for (i
= 0; i
< tokensetsize
; i
++)
302 for (i
= 0; i
< k
; i
++)
304 if (!shiftp
->shifts
[i
])
306 symbol
= state_table
[shiftp
->shifts
[i
]].accessing_symbol
;
309 SETBIT (shiftset
, symbol
);
312 k
= lookaheads
[state
+ 1];
313 fp3
= lookaheadset
+ tokensetsize
;
315 for (i
= lookaheads
[state
]; i
< k
; i
++)
317 fp1
= LA
+ i
* tokensetsize
;
332 for (i
= 0; i
< ntokens
; i
++)
349 /*----------------------------------------------.
350 | Count the number of reduce/reduce conflicts. |
351 `----------------------------------------------*/
354 count_rr_conflicts (int state
)
362 int m
= lookaheads
[state
];
363 int n
= lookaheads
[state
+ 1];
369 baseword
= LA
+ m
* tokensetsize
;
370 for (i
= 0; i
< ntokens
; i
++)
372 unsigned *wordp
= baseword
;
376 for (j
= m
; j
< n
; j
++)
381 wordp
+= tokensetsize
;
398 /*--------------------------------------------------------------.
399 | Return a human readable string which reports shift/reduce and |
400 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
401 `--------------------------------------------------------------*/
404 conflict_report (int src_num
, int rrc_num
)
406 static char res
[4096];
411 sprintf (cp
, ngettext ("%d shift/reduce conflict",
412 "%d shift/reduce conflicts", src_num
), src_num
);
416 if (src_num
> 0 && rrc_num
> 0)
418 sprintf (cp
, " %s ", _("and"));
424 sprintf (cp
, ngettext ("%d reduce/reduce conflict",
425 "%d reduce/reduce conflicts", rrc_num
), rrc_num
);
437 /*-----------------------------------------------------------.
438 | Output the detailed description of states with conflicts. |
439 `-----------------------------------------------------------*/
442 conflicts_output (FILE *out
)
445 for (i
= 0; i
< nstates
; i
++)
448 fprintf (out
, _("State %d contains "), i
);
449 fputs (conflict_report (count_sr_conflicts (i
),
450 count_rr_conflicts (i
)), out
);
455 /*------------------------------------------.
456 | Reporting the total number of conflicts. |
457 `------------------------------------------*/
460 conflicts_print (void)
467 /* Conflicts by state. */
468 for (i
= 0; i
< nstates
; i
++)
471 src_total
+= count_sr_conflicts (i
);
472 rrc_total
+= count_rr_conflicts (i
);
475 /* Report the total number of conflicts on STDERR. */
476 if (src_total
|| rrc_total
)
480 /* If invoked with `--yacc', use the output format specified by
482 fprintf (stderr
, _("conflicts: "));
484 fprintf (stderr
, _(" %d shift/reduce"), src_total
);
485 if (src_total
> 0 && rrc_total
> 0)
486 fprintf (stderr
, ",");
488 fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
);
493 fprintf (stderr
, _("%s contains "), infile
);
494 fputs (conflict_report (src_total
, rrc_total
), stderr
);
498 if (expected_conflicts
!= -1
499 && src_total
!= expected_conflicts
)
501 complain_message_count
++;
502 fprintf (stderr
, ngettext ("expected %d shift/reduce conflict",
503 "expected %d shift/reduce conflicts",
511 print_reductions (FILE *out
, int state
)
526 int default_rule
= 0;
533 for (i
= 0; i
< tokensetsize
; i
++)
536 shiftp
= state_table
[state
].shift_table
;
540 for (i
= 0; i
< k
; i
++)
542 if (!shiftp
->shifts
[i
])
544 symbol
= state_table
[shiftp
->shifts
[i
]].accessing_symbol
;
547 /* if this state has a shift for the error token,
548 don't use a default rule. */
549 if (symbol
== error_token_number
)
551 SETBIT (shiftset
, symbol
);
555 errp
= err_table
[state
];
559 for (i
= 0; i
< k
; i
++)
563 symbol
= errp
->errs
[i
];
564 SETBIT (shiftset
, symbol
);
568 m
= lookaheads
[state
];
569 n
= lookaheads
[state
+ 1];
571 if (n
- m
== 1 && !nodefault
)
573 default_rule
= LAruleno
[m
];
575 fp1
= LA
+ m
* tokensetsize
;
578 fp4
= lookaheadset
+ tokensetsize
;
581 *fp3
++ = *fp1
++ & *fp2
++;
586 for (i
= 0; i
< ntokens
; i
++)
589 fprintf (out
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
590 tags
[i
], default_rule
, tags
[rlhs
[default_rule
]]);
600 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
601 default_rule
, tags
[rlhs
[default_rule
]]);
607 fp4
= lookaheadset
+ tokensetsize
;
610 for (i
= m
; i
< n
; i
++)
612 fp1
= LA
+ i
* tokensetsize
;
617 *fp3
++ = *fp1
++ & (~(*fp2
++));
622 for (j
= 0; j
< ntokens
; j
++)
639 default_rule
= LAruleno
[i
];
649 for (i
= 0; i
< tokensetsize
; i
++)
655 for (i
= 0; i
< k
; i
++)
657 if (!shiftp
->shifts
[i
])
659 symbol
= state_table
[shiftp
->shifts
[i
]].accessing_symbol
;
662 SETBIT (shiftset
, symbol
);
667 fp1
= LA
+ m
* tokensetsize
;
669 for (i
= 0; i
< ntokens
; i
++)
679 for (j
= m
; j
< n
; j
++)
689 _(" %-4s\treduce using rule %d (%s)\n"),
690 tags
[i
], rule
, tags
[rlhs
[rule
]]);
701 rule
= LAruleno
[default_LA
];
703 _(" %-4s\treduce using rule %d (%s)\n"),
704 tags
[i
], rule
, tags
[rlhs
[rule
]]);
709 _(" %-4s\t[reduce using rule %d (%s)]\n"),
710 tags
[i
], rule
, tags
[rlhs
[rule
]]);
721 /* We tried incrementing just fp1, and just fp2; both seem wrong.
722 It seems necessary to increment both in sync. */
729 fprintf (out
, _(" $default\treduce using rule %d (%s)\n"),
730 default_rule
, tags
[rlhs
[default_rule
]]);
736 free_conflicts (void)
740 XFREE (lookaheadset
);