]>
git.saurik.com Git - bison.git/blob - src/conflicts.c
626db9348306f187874ba1dd9d7614dbe0fff820
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 int any_conflicts
= 0;
32 errs
**err_table
= NULL
;
33 int expected_conflicts
;
34 static char *conflicts
= NULL
;
36 static unsigned *shiftset
= NULL
;
37 static unsigned *lookaheadset
= NULL
;
45 log_resolution (int state
, int LAno
, int token
, char *resolution
)
47 obstack_fgrow4 (&output_obstack
,
49 Conflict in state %d between rule %d and token %s resolved as %s.\n"),
50 state
, LAruleno
[LAno
], tags
[token
], resolution
);
54 /*------------------------------------------------------------------.
55 | Turn off the shift recorded for the specified token in the |
56 | specified state. Used when we resolve a shift-reduce conflict in |
57 | favor of the reduction. |
58 `------------------------------------------------------------------*/
61 flush_shift (int state
, int token
)
66 shiftp
= shift_table
[state
];
71 for (i
= 0; i
< k
; i
++)
74 && token
== accessing_symbol
[shiftp
->shifts
[i
]])
75 (shiftp
->shifts
[i
]) = 0;
81 /*------------------------------------------------------------------.
82 | Attempt to resolve shift-reduce conflict for one rule by means of |
83 | precedence declarations. It has already been checked that the |
84 | rule has a precedence. A conflict is resolved by modifying the |
85 | shift or reduce tables so that there is no longer a conflict. |
86 `------------------------------------------------------------------*/
89 resolve_sr_conflict (int state
, int lookaheadnum
)
96 errs
*errp
= (errs
*) xcalloc (sizeof (errs
) + ntokens
* sizeof (short), 1);
97 short *errtokens
= errp
->errs
;
99 /* find the rule to reduce by to get precedence of reduction */
100 redprec
= rprec
[LAruleno
[lookaheadnum
]];
103 fp1
= LA
+ lookaheadnum
* tokensetsize
;
105 for (i
= 0; i
< ntokens
; i
++)
107 if ((mask
& *fp2
& *fp1
) && sprec
[i
])
108 /* Shift-reduce conflict occurs for token number i
109 and it has a precedence.
110 The precedence of shifting is that of token i. */
112 if (sprec
[i
] < redprec
)
114 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
115 *fp2
&= ~mask
; /* flush the shift for this token */
116 flush_shift (state
, i
);
118 else if (sprec
[i
] > redprec
)
120 log_resolution (state
, lookaheadnum
, i
, _("shift"));
121 *fp1
&= ~mask
; /* flush the reduce for this token */
125 /* Matching precedence levels.
126 For left association, keep only the reduction.
127 For right association, keep only the shift.
128 For nonassociation, keep neither. */
133 log_resolution (state
, lookaheadnum
, i
, _("shift"));
137 log_resolution (state
, lookaheadnum
, i
, _("reduce"));
141 log_resolution (state
, lookaheadnum
, i
, _("an error"));
145 if (sassoc
[i
] != right_assoc
)
147 *fp2
&= ~mask
; /* flush the shift for this token */
148 flush_shift (state
, i
);
150 if (sassoc
[i
] != left_assoc
)
152 *fp1
&= ~mask
; /* flush the reduce for this token */
154 if (sassoc
[i
] == non_assoc
)
156 /* Record an explicit error for this token. */
170 errp
->nerrs
= errtokens
- errp
->errs
;
173 /* Some tokens have been explicitly made errors. Allocate
174 a permanent errs structure for this state, to record them. */
175 i
= (char *) errtokens
- (char *) errp
;
176 err_table
[state
] = (errs
*) xcalloc ((unsigned int) i
, 1);
177 bcopy (errp
, err_table
[state
], i
);
180 err_table
[state
] = 0;
186 set_conflicts (int state
)
197 if (consistent
[state
])
200 for (i
= 0; i
< tokensetsize
; i
++)
203 shiftp
= shift_table
[state
];
207 for (i
= 0; i
< k
; i
++)
209 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
212 SETBIT (lookaheadset
, symbol
);
216 k
= lookaheads
[state
+ 1];
217 fp4
= lookaheadset
+ tokensetsize
;
219 /* Loop over all rules which require lookahead in this state. First
220 check for shift-reduce conflict, and try to resolve using
222 for (i
= lookaheads
[state
]; i
< k
; i
++)
223 if (rprec
[LAruleno
[i
]])
225 fp1
= LA
+ i
* tokensetsize
;
233 resolve_sr_conflict (state
, i
);
240 /* Loop over all rules which require lookahead in this state. Check
241 for conflicts not resolved above. */
242 for (i
= lookaheads
[state
]; i
< k
; i
++)
244 fp1
= LA
+ i
* tokensetsize
;
252 conflicts
[state
] = 1;
266 solve_conflicts (void)
270 conflicts
= XCALLOC (char, nstates
);
271 shiftset
= XCALLOC (unsigned, tokensetsize
);
272 lookaheadset
= XCALLOC (unsigned, tokensetsize
);
274 err_table
= XCALLOC (errs
*, nstates
);
278 for (i
= 0; i
< nstates
; i
++)
283 /*---------------------------------------------.
284 | Count the number of shift/reduce conflicts. |
285 `---------------------------------------------*/
288 count_sr_conflicts (int state
)
301 shiftp
= shift_table
[state
];
305 for (i
= 0; i
< tokensetsize
; i
++)
312 for (i
= 0; i
< k
; i
++)
314 if (!shiftp
->shifts
[i
])
316 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
319 SETBIT (shiftset
, symbol
);
322 k
= lookaheads
[state
+ 1];
323 fp3
= lookaheadset
+ tokensetsize
;
325 for (i
= lookaheads
[state
]; i
< k
; i
++)
327 fp1
= LA
+ i
* tokensetsize
;
342 for (i
= 0; i
< ntokens
; i
++)
357 /*----------------------------------------------.
358 | Count the number of reduce/reduce conflicts. |
359 `----------------------------------------------*/
362 count_rr_conflicts (int state
)
375 m
= lookaheads
[state
];
376 n
= lookaheads
[state
+ 1];
382 baseword
= LA
+ m
* tokensetsize
;
383 for (i
= 0; i
< ntokens
; i
++)
388 for (j
= m
; j
< n
; j
++)
393 wordp
+= tokensetsize
;
408 /*--------------------------------------------------------------.
409 | Return a human readable string which reports shift/reduce and |
410 | reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
411 `--------------------------------------------------------------*/
414 conflict_report (int src_num
, int rrc_num
)
416 static char res
[4096];
421 sprintf (cp
, _(" 1 shift/reduce conflict"));
424 else if (src_num
> 1)
426 sprintf (cp
, _(" %d shift/reduce conflicts"), src_num
);
430 if (src_num
> 0 && rrc_num
> 0)
432 sprintf (cp
, _(" and"));
438 sprintf (cp
, _(" 1 reduce/reduce conflict"));
441 else if (rrc_num
> 1)
443 sprintf (cp
, _(" %d reduce/reduce conflicts"), rrc_num
);
455 /*---------------------------------------------.
456 | Compute and give a report on the conflicts. |
457 `---------------------------------------------*/
460 print_conflicts (FILE *out
)
467 /* Count the total number of conflicts, and if wanted, give a
468 detailed report in FOUTPUT. */
469 for (i
= 0; i
< nstates
; i
++)
473 count_sr_conflicts (i
);
474 count_rr_conflicts (i
);
475 src_total
+= src_count
;
476 rrc_total
+= rrc_count
;
480 fprintf (out
, _("State %d contains"), i
);
481 fputs (conflict_report (src_count
, rrc_count
), out
);
486 /* Report the total number of conflicts on STDERR. */
489 /* If invoked with `--yacc', use the output format specified by
491 fprintf (stderr
, _("conflicts: "));
493 fprintf (stderr
, _(" %d shift/reduce"), src_total
);
494 if (src_total
> 0 && rrc_total
> 0)
495 fprintf (stderr
, ",");
497 fprintf (stderr
, _(" %d reduce/reduce"), rrc_total
);
502 fprintf (stderr
, _("%s contains"), infile
);
503 fputs (conflict_report (src_total
, rrc_total
), stderr
);
509 print_reductions (int state
)
524 int default_rule
= 0;
531 for (i
= 0; i
< tokensetsize
; i
++)
534 shiftp
= shift_table
[state
];
538 for (i
= 0; i
< k
; i
++)
540 if (!shiftp
->shifts
[i
])
542 symbol
= accessing_symbol
[shiftp
->shifts
[i
]];
545 /* if this state has a shift for the error token,
546 don't use a default rule. */
547 if (symbol
== error_token_number
)
549 SETBIT (shiftset
, symbol
);
553 errp
= err_table
[state
];
557 for (i
= 0; i
< k
; i
++)
561 symbol
= errp
->errs
[i
];
562 SETBIT (shiftset
, symbol
);
566 m
= lookaheads
[state
];
567 n
= lookaheads
[state
+ 1];
569 if (n
- m
== 1 && !nodefault
)
571 default_rule
= LAruleno
[m
];
573 fp1
= LA
+ m
* tokensetsize
;
576 fp4
= lookaheadset
+ tokensetsize
;
579 *fp3
++ = *fp1
++ & *fp2
++;
584 for (i
= 0; i
< ntokens
; i
++)
587 obstack_fgrow3 (&output_obstack
,
588 _(" %-4s\t[reduce using rule %d (%s)]\n"),
589 tags
[i
], default_rule
, tags
[rlhs
[default_rule
]]);
599 obstack_fgrow2 (&output_obstack
,
600 _(" $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
= accessing_symbol
[shiftp
->shifts
[i
]];
662 SETBIT (shiftset
, symbol
);
667 fp1
= LA
+ m
* tokensetsize
;
669 for (i
= 0; i
< ntokens
; i
++)
679 for (j
= m
; j
< n
; j
++)
688 obstack_fgrow3 (&output_obstack
,
689 _(" %-4s\treduce using rule %d (%s)\n"),
690 tags
[i
], rule
, tags
[rlhs
[rule
]]);
701 rule
= LAruleno
[default_LA
];
702 obstack_fgrow3 (&output_obstack
,
703 _(" %-4s\treduce using rule %d (%s)\n"),
704 tags
[i
], rule
, tags
[rlhs
[rule
]]);
708 obstack_fgrow3 (&output_obstack
,
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 obstack_fgrow2 (&output_obstack
,
730 _(" $default\treduce using rule %d (%s)\n"),
731 default_rule
, tags
[rlhs
[default_rule
]]);
733 obstack_1grow (&output_obstack
, '\n');
739 free_conflicts (void)
743 XFREE (lookaheadset
);