Files
bison/src/conflicts.c
Akim Demaille e9955c8373 Have Bison grammars parsed by a Bison grammar.
* src/reader.c, src/reader.h (prologue_augment): New.
* src/reader.c (copy_definition): Remove.
* src/reader.h, src/reader.c (gram_start_symbol_set, prologue_augment)
(grammar_symbol_append, grammar_rule_begin, grammar_midrule_action)
(grammar_current_rule_prec_set, grammar_current_rule_check)
(grammar_current_rule_symbol_append)
(grammar_current_rule_action_append): Export.
* src/parse-gram.y (symbol_list_new, symbol_list_symbol_append_
(symbol_list_action_append): Remove.
Hook the routines from reader.
* src/scan-gram.l: In INITIAL, characters and strings are tokens.
* src/system.h (ATTRIBUTE_NORETURN, ATTRIBUTE_UNUSED): Now.
* src/reader.c (read_declarations): Remove, unused.
* src/parse-gram.y: Handle the epilogue.
* src/reader.h, src/reader.c (gram_start_symbol_set): Rename as...
(grammar_start_symbol_set): this.
* src/scan-gram.l: Be sure to ``use'' yycontrol to keep GCC quiet.
* src/reader.c (readgram): Remove, unused.
(reader): Adjust to insert eoftoken and axiom where appropriate.
* src/reader.c (copy_dollar): Replace with...
* src/scan-gram.h (handle_dollar): this.
* src/parse-gram.y: Remove `%thong'.
* src/reader.c (copy_at): Replace with...
* src/scan-gram.h (handle_at): this.
* src/complain.h, src/complain.c (warn_at, complain_at, fatal_at):
New.
* src/scan-gram.l (YY_LINES): Keep lineno synchronized for the
time being.
* src/reader.h, src/reader.c (grammar_rule_end): New.
* src/parse.y (current_type, current_class): New.
Implement `%nterm', `%token' support.
Merge `%term' into `%token'.
(string_as_id): New.
* src/symtab.h, src/symtab.c (symbol_make_alias): Don't pass the
type name.
* src/parse-gram.y: Be sure to handle properly the beginning of
rules.
* src/parse-gram.y: Handle %type.
* src/reader.c (grammar_rule_end): Call grammar_current_rule_check.
* src/parse-gram.y: More directives support.
* src/options.c: No longer handle source directives.
* src/parse-gram.y: Fix %output.
* src/parse-gram.y: Handle %union.
Use the prologue locations.
* src/reader.c (parse_union_decl): Remove.
* src/reader.h, src/reader.c (epilogue_set): New.
* src/parse-gram.y: Use it.
* data/bison.simple, data/bison.c++: b4_stype is now either not
defined, then default to int, or to the contents of %union,
without `union' itself.
Adjust.
* src/muscle_tab.c (muscle_init): Don't predefine `stype'.
* src/output.c (actions_output): Don't output braces, as they are
already handled by the scanner.
* src/scan-gram.l (SC_CHARACTER): Set the user_token_number of
characters to themselves.
* tests/reduce.at (Reduced Automaton): End the grammars with %% so
that the epilogue has a proper #line.
* src/parse-gram.y: Handle precedence/associativity.
* src/symtab.c (symbol_precedence_set): Requires the symbol to be
a terminal.
* src/scan-gram.l (SC_BRACED_CODE): Catch strings and characters.
* tests/calc.at: Do not use `%token "foo"' as it makes not sense
at all to define terminals that cannot be emitted.
* src/scan-gram.l: Escape M4 characters.
* src/scan-gram.l: Working properly with escapes in user
strings/characters.
* tests/torture.at (AT_DATA_TRIANGULAR_GRAMMAR)
(AT_DATA_HORIZONTAL_GRAMMAR): Respect the `%token ID NUM STRING'
grammar.
Use more modest sizes, as for the time being the parser does not
release memory, and therefore the process swallows a huge amount
of memory.
* tests/torture.at (AT_DATA_LOOKAHEADS_GRAMMAR): Adjust to the
stricter %token grammar.
* src/symtab.h (associativity): Add `undef_assoc'.
(symbol_precedence_set): Do nothing when passed an undef_assoc.
* src/symtab.c (symbol_check_alias_consistence): Adjust.
* tests/regression.at (Invalid %directive): Remove, as it is now
meaningless.
(Invalid inputs): Adjust to the new error messages.
(Token definitions): The new grammar doesn't allow too many
eccentricities.
* src/lex.h, src/lex.c: Remove.
* src/reader.c (lastprec, skip_to_char, read_signed_integer)
(copy_character, copy_string2, copy_string, copy_identifier)
(copy_comment, parse_token_decl, parse_type_decl, parse_assoc_decl)
(parse_muscle_decl, parse_dquoted_param, parse_skel_decl)
(parse_action): Remove.
* po/POTFILES.in: Adjust.
2002-06-11 20:16:05 +00:00

485 lines
12 KiB
C
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* Find and resolve or report look-ahead conflicts for bison,
Copyright (C) 1984, 1989, 1992, 2000, 2001, 2002
Free Software Foundation, Inc.
This file is part of Bison, the GNU Compiler Compiler.
Bison is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
Bison is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with Bison; see the file COPYING. If not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA. */
#include "system.h"
#include "bitset.h"
#include "complain.h"
#include "getargs.h"
#include "symtab.h"
#include "files.h"
#include "gram.h"
#include "state.h"
#include "lalr.h"
#include "conflicts.h"
#include "reader.h"
#include "LR0.h"
/* -1 stands for not specified. */
int expected_conflicts = -1;
static char *conflicts = NULL;
struct obstack solved_conflicts_obstack;
static bitset shiftset;
static bitset lookaheadset;
enum conflict_resolution_e
{
shift_resolution,
reduce_resolution,
left_resolution,
right_resolution,
nonassoc_resolution
};
static inline void
log_resolution (int lookahead, int token,
enum conflict_resolution_e resolution)
{
if (report_flag & report_solved_conflicts)
{
/* The description of the resolution. */
switch (resolution)
{
case shift_resolution:
case left_resolution:
obstack_fgrow2 (&solved_conflicts_obstack,
_("\
Conflict between rule %d and token %s resolved as shift"),
LArule[lookahead]->number,
symbols[token]->tag);
break;
case reduce_resolution:
case right_resolution:
obstack_fgrow2 (&solved_conflicts_obstack,
_("\
Conflict between rule %d and token %s resolved as reduce"),
LArule[lookahead]->number,
symbols[token]->tag);
break;
case nonassoc_resolution:
obstack_fgrow2 (&solved_conflicts_obstack,
_("\
Conflict between rule %d and token %s resolved as an error"),
LArule[lookahead]->number,
symbols[token]->tag);
break;
}
/* The reason. */
switch (resolution)
{
case shift_resolution:
obstack_fgrow2 (&solved_conflicts_obstack,
" (%s < %s)",
LArule[lookahead]->prec->tag,
symbols[token]->tag);
break;
case reduce_resolution:
obstack_fgrow2 (&solved_conflicts_obstack,
" (%s < %s)",
symbols[token]->tag,
LArule[lookahead]->prec->tag);
break;
case left_resolution:
obstack_fgrow1 (&solved_conflicts_obstack,
" (%%left %s)",
symbols[token]->tag);
break;
case right_resolution:
obstack_fgrow1 (&solved_conflicts_obstack,
" (%%right %s)",
symbols[token]->tag);
break;
case nonassoc_resolution:
obstack_fgrow1 (&solved_conflicts_obstack,
" (%%nonassoc %s)",
symbols[token]->tag);
break;
}
obstack_sgrow (&solved_conflicts_obstack, ".\n");
}
}
/*------------------------------------------------------------------.
| Turn off the shift recorded for the specified token in the |
| specified state. Used when we resolve a shift-reduce conflict in |
| favor of the reduction. |
`------------------------------------------------------------------*/
static void
flush_shift (state_t *state, int token)
{
shifts *shiftp = state->shifts;
int i;
bitset_reset (lookaheadset, token);
for (i = 0; i < shiftp->nshifts; i++)
if (!SHIFT_IS_DISABLED (shiftp, i) && SHIFT_SYMBOL (shiftp, i) == token)
SHIFT_DISABLE (shiftp, i);
}
/*-------------------------------------------------------------------.
| Turn off the reduce recorded for the specified token for the |
| specified lookahead. Used when we resolve a shift-reduce conflict |
| in favor of the shift. |
`-------------------------------------------------------------------*/
static void
flush_reduce (int lookahead, int token)
{
bitset_reset (LA[lookahead], token);
}
/*------------------------------------------------------------------.
| Attempt to resolve shift-reduce conflict for one rule by means of |
| precedence declarations. It has already been checked that the |
| rule has a precedence. A conflict is resolved by modifying the |
| shift or reduce tables so that there is no longer a conflict. |
`------------------------------------------------------------------*/
static void
resolve_sr_conflict (state_t *state, int lookahead)
{
int i;
/* find the rule to reduce by to get precedence of reduction */
int redprec = LArule[lookahead]->prec->prec;
errs *errp = errs_new (ntokens + 1);
errp->nerrs = 0;
for (i = 0; i < ntokens; i++)
if (bitset_test (LA[lookahead], i)
&& bitset_test (lookaheadset, i)
&& symbols[i]->prec)
{
/* Shift-reduce conflict occurs for token number i
and it has a precedence.
The precedence of shifting is that of token i. */
if (symbols[i]->prec < redprec)
{
log_resolution (lookahead, i, reduce_resolution);
flush_shift (state, i);
}
else if (symbols[i]->prec > redprec)
{
log_resolution (lookahead, i, shift_resolution);
flush_reduce (lookahead, i);
}
else
/* Matching precedence levels.
For left association, keep only the reduction.
For right association, keep only the shift.
For nonassociation, keep neither. */
switch (symbols[i]->assoc)
{
case right_assoc:
log_resolution (lookahead, i, right_resolution);
flush_reduce (lookahead, i);
break;
case left_assoc:
log_resolution (lookahead, i, left_resolution);
flush_shift (state, i);
break;
case non_assoc:
log_resolution (lookahead, i, nonassoc_resolution);
flush_shift (state, i);
flush_reduce (lookahead, i);
/* Record an explicit error for this token. */
errp->errs[errp->nerrs++] = i;
break;
case undef_assoc:
assert (symbols[i]->assoc != undef_assoc);
break;
}
}
/* Some tokens have been explicitly made errors. Allocate a
permanent errs structure for this state, to record them. */
state->errs = errs_dup (errp);
free (errp);
if (obstack_object_size (&solved_conflicts_obstack))
{
obstack_1grow (&solved_conflicts_obstack, '\0');
state->solved_conflicts = obstack_finish (&solved_conflicts_obstack);
}
}
static void
set_conflicts (state_t *state)
{
int i;
shifts *shiftp;
if (state->consistent)
return;
bitset_zero (lookaheadset);
shiftp = state->shifts;
for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
if (!SHIFT_IS_DISABLED (shiftp, i))
bitset_set (lookaheadset, SHIFT_SYMBOL (shiftp, i));
/* Loop over all rules which require lookahead in this state. First
check for shift-reduce conflict, and try to resolve using
precedence */
for (i = 0; i < state->nlookaheads; ++i)
if (LArule[state->lookaheadsp + i]->prec
&& LArule[state->lookaheadsp + i]->prec->prec
&& !bitset_disjoint_p (LA[state->lookaheadsp + i], lookaheadset))
{
resolve_sr_conflict (state, state->lookaheadsp + i);
break;
}
/* Loop over all rules which require lookahead in this state. Check
for conflicts not resolved above. */
for (i = 0; i < state->nlookaheads; ++i)
{
if (!bitset_disjoint_p (LA[state->lookaheadsp + i], lookaheadset))
conflicts[state->number] = 1;
bitset_or (lookaheadset, lookaheadset, LA[state->lookaheadsp + i]);
}
}
void
conflicts_solve (void)
{
size_t i;
conflicts = XCALLOC (char, nstates);
shiftset = bitset_create (ntokens, BITSET_FIXED);
lookaheadset = bitset_create (ntokens, BITSET_FIXED);
obstack_init (&solved_conflicts_obstack);
for (i = 0; i < nstates; i++)
set_conflicts (states[i]);
}
/*---------------------------------------------.
| Count the number of shift/reduce conflicts. |
`---------------------------------------------*/
static int
count_sr_conflicts (state_t *state)
{
int i;
int src_count = 0;
shifts *shiftp = state->shifts;
if (!shiftp)
return 0;
bitset_zero (lookaheadset);
bitset_zero (shiftset);
for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
if (!SHIFT_IS_DISABLED (shiftp, i))
bitset_set (shiftset, SHIFT_SYMBOL (shiftp, i));
for (i = 0; i < state->nlookaheads; ++i)
bitset_or (lookaheadset, lookaheadset, LA[state->lookaheadsp + i]);
bitset_and (lookaheadset, lookaheadset, shiftset);
src_count = bitset_count (lookaheadset);
return src_count;
}
/*----------------------------------------------.
| Count the number of reduce/reduce conflicts. |
`----------------------------------------------*/
static int
count_rr_conflicts (state_t *state)
{
int i;
int rrc_count = 0;
if (state->nlookaheads < 2)
return 0;
for (i = 0; i < ntokens; i++)
{
int count = 0;
int j;
for (j = 0; j < state->nlookaheads; ++j)
if (bitset_test (LA[state->lookaheadsp + j], i))
count++;
if (count >= 2)
rrc_count++;
}
return rrc_count;
}
/*--------------------------------------------------------------.
| Return a human readable string which reports shift/reduce and |
| reduce/reduce conflict numbers (SRC_NUM, RRC_NUM). |
`--------------------------------------------------------------*/
static const char *
conflict_report (int src_num, int rrc_num)
{
static char res[4096];
char *cp = res;
if (src_num >= 1)
{
sprintf (cp, ngettext ("%d shift/reduce conflict",
"%d shift/reduce conflicts", src_num), src_num);
cp += strlen (cp);
}
if (src_num > 0 && rrc_num > 0)
{
sprintf (cp, " %s ", _("and"));
cp += strlen (cp);
}
if (rrc_num >= 1)
{
sprintf (cp, ngettext ("%d reduce/reduce conflict",
"%d reduce/reduce conflicts", rrc_num), rrc_num);
cp += strlen (cp);
}
*cp++ = '.';
*cp++ = '\n';
*cp++ = '\0';
return res;
}
/*-----------------------------------------------------------.
| Output the detailed description of states with conflicts. |
`-----------------------------------------------------------*/
void
conflicts_output (FILE *out)
{
bool printed_sth = FALSE;
size_t i;
for (i = 0; i < nstates; i++)
if (conflicts[i])
{
fprintf (out, _("State %d contains "), i);
fputs (conflict_report (count_sr_conflicts (states[i]),
count_rr_conflicts (states[i])), out);
printed_sth = TRUE;
}
if (printed_sth)
fputs ("\n\n", out);
}
/*------------------------------------------.
| Reporting the total number of conflicts. |
`------------------------------------------*/
void
conflicts_print (void)
{
size_t i;
/* Is the number of SR conflicts OK? Either EXPECTED_CONFLICTS is
not set, and then we want 0 SR, or else it is specified, in which
case we want equality. */
int src_ok = 0;
int src_total = 0;
int rrc_total = 0;
/* Conflicts by state. */
for (i = 0; i < nstates; i++)
if (conflicts[i])
{
src_total += count_sr_conflicts (states[i]);
rrc_total += count_rr_conflicts (states[i]);
}
src_ok = src_total == (expected_conflicts == -1 ? 0 : expected_conflicts);
/* If there are no RR conflicts, and as many SR conflicts as
expected, then there is nothing to report. */
if (!rrc_total && src_ok)
return;
/* Report the total number of conflicts on STDERR. */
if (yacc_flag)
{
/* If invoked with `--yacc', use the output format specified by
POSIX. */
fprintf (stderr, _("conflicts: "));
if (src_total > 0)
fprintf (stderr, _(" %d shift/reduce"), src_total);
if (src_total > 0 && rrc_total > 0)
fprintf (stderr, ",");
if (rrc_total > 0)
fprintf (stderr, _(" %d reduce/reduce"), rrc_total);
putc ('\n', stderr);
}
else
{
fprintf (stderr, _("%s contains "), infile);
fputs (conflict_report (src_total, rrc_total), stderr);
}
if (expected_conflicts != -1 && !src_ok)
{
complain_message_count++;
fprintf (stderr, ngettext ("expected %d shift/reduce conflict\n",
"expected %d shift/reduce conflicts\n",
expected_conflicts),
expected_conflicts);
}
}
void
conflicts_free (void)
{
XFREE (conflicts);
bitset_free (shiftset);
bitset_free (lookaheadset);
obstack_free (&solved_conflicts_obstack, NULL);
}