* src/state.h, src/state.c (reductions_new): New.

* src/LR0.c (set_state_table): Let all the states have a
`reductions', even if reduced to 0.
(save_reductions): Adjust.
* src/lalr.c (initialize_LA, initialize_lookaheads): Adjust.
* src/print.c (print_reductions, print_actions): Adjust.
* src/output.c (action_row): Adjust.
This commit is contained in:
Akim Demaille
2001-12-27 18:11:06 +00:00
parent 2cec70b9f1
commit 80dac38c5e
7 changed files with 49 additions and 26 deletions

View File

@@ -1,3 +1,14 @@
2001-12-27 Akim Demaille <akim@epita.fr>
* src/state.h, src/state.c (reductions_new): New.
* src/LR0.c (set_state_table): Let all the states have a
`reductions', even if reduced to 0.
(save_reductions): Adjust.
* src/lalr.c (initialize_LA, initialize_lookaheads): Adjust.
* src/print.c (print_reductions, print_actions): Adjust.
* src/output.c (action_row): Adjust.
2001-12-27 Akim Demaille <akim@epita.fr> 2001-12-27 Akim Demaille <akim@epita.fr>
* src/state.h, src/state.c (errs_new, errs_dup): New. * src/state.h, src/state.c (errs_new, errs_dup): New.

View File

@@ -522,15 +522,8 @@ save_reductions (void)
} }
/* Make a reductions structure and copy the data into it. */ /* Make a reductions structure and copy the data into it. */
this_state->reductions = reductions_new (count);
if (count) shortcpy (this_state->reductions->rules, redset, count);
{
reductions *p = REDUCTIONS_ALLOC (count);
p->nreds = count;
shortcpy (p->rules, redset, count);
this_state->reductions = p;
}
} }
@@ -547,11 +540,14 @@ set_state_table (void)
for (sp = first_state; sp; sp = sp->next) for (sp = first_state; sp; sp = sp->next)
{ {
/* Pessimization, but simplification of the code: make sure all /* Pessimization, but simplification of the code: make sure all
the states have a shifts and errs, even if reduced to 0. */ the states have a shifts, errs, and reductions, even if
reduced to 0. */
if (!sp->shifts) if (!sp->shifts)
sp->shifts = shifts_new (0); sp->shifts = shifts_new (0);
if (!sp->errs) if (!sp->errs)
sp->errs = errs_new (0); sp->errs = errs_new (0);
if (!sp->reductions)
sp->reductions = reductions_new (0);
state_table[sp->number] = sp; state_table[sp->number] = sp;
} }

View File

@@ -137,7 +137,6 @@ initialize_LA (void)
int i; int i;
int j; int j;
short *np; short *np;
reductions *rp;
/* Avoid having to special case 0. */ /* Avoid having to special case 0. */
if (!nLA) if (!nLA)
@@ -150,9 +149,8 @@ initialize_LA (void)
np = LAruleno; np = LAruleno;
for (i = 0; i < nstates; i++) for (i = 0; i < nstates; i++)
if (!state_table[i]->consistent) if (!state_table[i]->consistent)
if ((rp = state_table[i]->reductions)) for (j = 0; j < state_table[i]->reductions->nreds; j++)
for (j = 0; j < rp->nreds; j++) *np++ = state_table[i]->reductions->rules[j];
*np++ = rp->rules[j];
} }
@@ -525,8 +523,12 @@ initialize_lookaheads (void)
reductions *rp = state_table[i]->reductions; reductions *rp = state_table[i]->reductions;
shifts *sp = state_table[i]->shifts; shifts *sp = state_table[i]->shifts;
if (rp /* We need a lookahead either to distinguish different
&& (rp->nreds > 1 || (sp->nshifts && SHIFT_IS_SHIFT (sp, 0)))) reductions (i.e., there are two or more), or to distinguish a
reduction from a shift. Otherwise, it is straightforward,
and the state is `consistent'. */
if (rp->nreds > 1
|| (rp->nreds == 1 && sp->nshifts && SHIFT_IS_SHIFT (sp, 0)))
nlookaheads += rp->nreds; nlookaheads += rp->nreds;
else else
state_table[i]->consistent = 1; state_table[i]->consistent = 1;

View File

@@ -326,7 +326,6 @@ action_row (state_t *state)
int i; int i;
int default_rule = 0; int default_rule = 0;
reductions *redp = state->reductions; reductions *redp = state->reductions;
int nreds = redp ? redp->nreds : 0;
shifts *shiftp = state->shifts; shifts *shiftp = state->shifts;
errs *errp = state->errs; errs *errp = state->errs;
/* set nonzero to inhibit having any default reduction */ /* set nonzero to inhibit having any default reduction */
@@ -335,7 +334,7 @@ action_row (state_t *state)
for (i = 0; i < ntokens; i++) for (i = 0; i < ntokens; i++)
actrow[i] = 0; actrow[i] = 0;
if (nreds >= 1) if (redp->nreds >= 1)
{ {
int j; int j;
/* loop over all the rules available here which require /* loop over all the rules available here which require
@@ -384,7 +383,7 @@ action_row (state_t *state)
/* Now find the most common reduction and make it the default action /* Now find the most common reduction and make it the default action
for this state. */ for this state. */
if (nreds >= 1 && !nodefault) if (redp->nreds >= 1 && !nodefault)
{ {
if (state->consistent) if (state->consistent)
default_rule = redp->rules[0]; default_rule = redp->rules[0];

View File

@@ -167,6 +167,9 @@ print_reductions (FILE *out, state_t *state)
errs *errp = state->errs; errs *errp = state->errs;
int nodefault = 0; int nodefault = 0;
if (redp->nreds == 0)
return;
if (state->consistent) if (state->consistent)
{ {
int rule = redp->rules[0]; int rule = redp->rules[0];
@@ -302,7 +305,7 @@ print_actions (FILE *out, state_t *state)
reductions *redp = state->reductions; reductions *redp = state->reductions;
shifts *shiftp = state->shifts; shifts *shiftp = state->shifts;
if (!shiftp->nshifts && !redp) if (shiftp->nshifts == 0 && redp->nreds == 0)
{ {
if (final_state == state->number) if (final_state == state->number)
fprintf (out, _(" $default\taccept\n")); fprintf (out, _(" $default\taccept\n"));
@@ -313,8 +316,7 @@ print_actions (FILE *out, state_t *state)
print_shifts (out, state); print_shifts (out, state);
print_errs (out, state); print_errs (out, state);
if (redp) print_reductions (out, state);
print_reductions (out, state);
print_gotos (out, state); print_gotos (out, state);
} }

View File

@@ -64,3 +64,19 @@ errs_dup (errs *src)
memcpy (res->errs, src->errs, src->nerrs); memcpy (res->errs, src->errs, src->nerrs);
return res; return res;
} }
/*-------------------------------------.
| Create a new array of N reductions. |
`-------------------------------------*/
#define REDUCTIONS_ALLOC(Nreductions) \
(reductions *) xcalloc ((unsigned) (sizeof (reductions) \
+ (Nreductions - 1) * sizeof (short)), 1)
reductions *
reductions_new (int n)
{
reductions *res = REDUCTIONS_ALLOC (n);
res->nreds = n;
return res;
}

View File

@@ -158,10 +158,7 @@ typedef struct reductions
short rules[1]; short rules[1];
} reductions; } reductions;
#define REDUCTIONS_ALLOC(Nreductions) \ reductions *reductions_new PARAMS ((int n));
(reductions *) xcalloc ((unsigned) (sizeof (reductions) \
+ (Nreductions - 1) * sizeof (short)), 1)
/*----------. /*----------.