there are LR(0) states and LALR(1) states.
* src/lalr.h (state_t): Remove.
(state_table): Be state_t **, not state_t *.
* src/state.h (core, CORE_ALLOC): Rename as...
(state_t, STATE_ALLOC): this.
Add the LALR(1) members: shifts, reductions, errs.
* src/LR0.c (state_table): Rename as...
(state_hash): this, to avoid name clashes with the global
`state_table'.
* src/print_graph.c, src/LR0.c, src/LR0.h, src/conflicts.c
* src/lalr.c, src/lalr.h, src/output.c, src/print.c: Adjust.
+2001-12-10 Akim Demaille <akim@epita.fr>
+
+ One structure for states is enough, even though theoretically
+ there are LR(0) states and LALR(1) states.
+
+ * src/lalr.h (state_t): Remove.
+ (state_table): Be state_t **, not state_t *.
+ * src/state.h (core, CORE_ALLOC): Rename as...
+ (state_t, STATE_ALLOC): this.
+ Add the LALR(1) members: shifts, reductions, errs.
+ * src/LR0.c (state_table): Rename as...
+ (state_hash): this, to avoid name clashes with the global
+ `state_table'.
+ * src/print_graph.c, src/LR0.c, src/LR0.h, src/conflicts.c
+ * src/lalr.c, src/lalr.h, src/output.c, src/print.c: Adjust.
+
+
2001-12-10 Akim Demaille <akim@epita.fr>
Bison dumps core on bash.y.
int nstates;
int final_state;
-core *first_state = NULL;
+state_t *first_state = NULL;
shifts *first_shift = NULL;
reductions *first_reduction = NULL;
-static core *this_state = NULL;
-static core *last_state = NULL;
+static state_t *this_state = NULL;
+static state_t *last_state = NULL;
static shifts *last_shift = NULL;
static reductions *last_reduction = NULL;
/* hash table for states, to recognize equivalent ones. */
-#define STATE_TABLE_SIZE 1009
-static core **state_table = NULL;
+#define STATE_HASH_SIZE 1009
+static state_t **state_hash = NULL;
\f
static void
shiftset = XCALLOC (short, nsyms);
redset = XCALLOC (short, nrules + 1);
- state_table = XCALLOC (core *, STATE_TABLE_SIZE);
+ state_hash = XCALLOC (state_t *, STATE_HASH_SIZE);
}
free (kernel_base);
free (kernel_size);
XFREE (kernel_items);
- free (state_table);
+ free (state_hash);
}
| necessary. |
`-----------------------------------------------------------------*/
-static core *
+static state_t *
new_state (int symbol)
{
- core *p;
+ state_t *p;
if (trace_flag)
fprintf (stderr, "Entering new_state, state = %d, symbol = %d (%s)\n",
if (nstates >= MAXSHORT)
fatal (_("too many states (max %d)"), MAXSHORT);
- p = CORE_ALLOC (kernel_size[symbol]);
+ p = STATE_ALLOC (kernel_size[symbol]);
p->accessing_symbol = symbol;
p->number = nstates;
p->nitems = kernel_size[symbol];
{
int key;
int i;
- core *sp;
+ state_t *sp;
if (trace_flag)
fprintf (stderr, "Entering get_state, state = %d, symbol = %d (%s)\n",
key = 0;
for (i = 0; i < kernel_size[symbol]; ++i)
key += kernel_base[symbol][i];
- key = key % STATE_TABLE_SIZE;
- sp = state_table[key];
+ key = key % STATE_HASH_SIZE;
+ sp = state_hash[key];
if (sp)
{
}
else /* bucket is empty */
{
- state_table[key] = sp = new_state (symbol);
+ state_hash[key] = sp = new_state (symbol);
}
if (trace_flag)
static void
new_states (void)
{
- first_state = last_state = this_state = CORE_ALLOC (0);
+ first_state = last_state = this_state = STATE_ALLOC (0);
nstates = 1;
}
static void
insert_start_shift (void)
{
- core *statep;
+ state_t *statep;
shifts *sp;
- statep = CORE_ALLOC (0);
+ statep = STATE_ALLOC (0);
statep->number = nstates;
statep->accessing_symbol = start_symbol;
static void
augment_automaton (void)
{
- core *statep;
+ state_t *statep;
shifts *sp;
shifts *sp1 = NULL;
/* Make the final state--the one that follows a shift from the
next-to-final state.
The symbol for that shift is 0 (end-of-file). */
- statep = CORE_ALLOC (0);
+ statep = STATE_ALLOC (0);
statep->number = nstates;
last_state->next = statep;
last_state = statep;
final_state = nstates;
/* Make the termination state. */
- statep = CORE_ALLOC (0);
+ statep = STATE_ALLOC (0);
statep->number = nstates++;
last_state->next = statep;
last_state = statep;
extern int nstates;
extern int final_state;
-extern core *first_state;
+extern state_t *first_state;
extern shifts *first_shift;
extern reductions *first_reduction;
static void
flush_shift (int state, int token)
{
- shifts *shiftp = state_table[state].shifts;
+ shifts *shiftp = state_table[state]->shifts;
int i;
for (i = 0; i < shiftp->nshifts; i++)
/* Some tokens have been explicitly made errors. Allocate a
permanent errs structure for this state, to record them. */
i = (char *) errtokens - (char *) errp;
- state_table[state].errs = ERRS_ALLOC (i + 1);
- memcpy (state_table[state].errs, errp, i);
+ state_table[state]->errs = ERRS_ALLOC (i + 1);
+ memcpy (state_table[state]->errs, errp, i);
free (errp);
}
int i, j;
shifts *shiftp;
- if (state_table[state].consistent)
+ if (state_table[state]->consistent)
return;
for (i = 0; i < tokensetsize; i++)
lookaheadset[i] = 0;
- shiftp = state_table[state].shifts;
+ shiftp = state_table[state]->shifts;
for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
if (!SHIFT_IS_DISABLED (shiftp, i))
SETBIT (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 = state_table[state].lookaheads;
- i < state_table[state + 1].lookaheads;
+ for (i = state_table[state]->lookaheads;
+ i < state_table[state + 1]->lookaheads;
++i)
if (rule_table[LAruleno[i]].prec)
for (j = 0; j < tokensetsize; ++j)
/* Loop over all rules which require lookahead in this state. Check
for conflicts not resolved above. */
- for (i = state_table[state].lookaheads;
- i < state_table[state + 1].lookaheads;
+ for (i = state_table[state]->lookaheads;
+ i < state_table[state + 1]->lookaheads;
++i)
{
for (j = 0; j < tokensetsize; ++j)
{
int i, k;
int src_count = 0;
- shifts *shiftp = state_table[state].shifts;
+ shifts *shiftp = state_table[state]->shifts;
if (!shiftp)
return 0;
if (!SHIFT_IS_DISABLED (shiftp, i))
SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i));
- for (i = state_table[state].lookaheads;
- i < state_table[state + 1].lookaheads;
+ for (i = state_table[state]->lookaheads;
+ i < state_table[state + 1]->lookaheads;
++i)
for (k = 0; k < tokensetsize; ++k)
lookaheadset[k] |= LA (i)[k];
int i;
int rrc_count = 0;
- int m = state_table[state].lookaheads;
- int n = state_table[state + 1].lookaheads;
+ int m = state_table[state]->lookaheads;
+ int n = state_table[state + 1]->lookaheads;
if (n - m < 2)
return 0;
for (i = 0; i < tokensetsize; i++)
shiftset[i] = 0;
- shiftp = state_table[state].shifts;
+ shiftp = state_table[state]->shifts;
for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++)
if (!SHIFT_IS_DISABLED (shiftp, i))
{
SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i));
}
- errp = state_table[state].errs;
+ errp = state_table[state]->errs;
if (errp)
for (i = 0; i < errp->nerrs; i++)
if (errp->errs[i])
SETBIT (shiftset, errp->errs[i]);
- m = state_table[state].lookaheads;
- n = state_table[state + 1].lookaheads;
+ m = state_table[state]->lookaheads;
+ n = state_table[state + 1]->lookaheads;
if (n - m == 1 && !nodefault)
{
/* All the decorated states, indexed by the state number. Warning:
there is a state_TABLE in LR0.c, but it is different and static.
*/
-state_t *state_table = NULL;
+state_t **state_table = NULL;
int tokensetsize;
short *LAruleno;
/* NSTATES + 1 because lookahead for the pseudo state number NSTATES
might be used (see conflicts.c). It is too opaque for me to
provide a probably less hacky implementation. --akim */
- state_table = XCALLOC (state_t, nstates + 1);
+ state_table = XCALLOC (state_t *, nstates + 1);
{
- core *sp;
+ state_t *sp;
for (sp = first_state; sp; sp = sp->next)
- {
- state_table[sp->number].state = sp;
- state_table[sp->number].accessing_symbol = sp->accessing_symbol;
- }
+ state_table[sp->number] = sp;
}
{
shifts *sp;
for (sp = first_shift; sp; sp = sp->next)
- state_table[sp->number].shifts = sp;
+ state_table[sp->number]->shifts = sp;
}
{
reductions *rp;
for (rp = first_reduction; rp; rp = rp->next)
- state_table[rp->number].reductions = rp;
+ state_table[rp->number]->reductions = rp;
}
- /* Pessimization, but simplification of the code: make sense all the
+ /* Pessimization, but simplification of the code: make sure all the
states have a shifts, even if reduced to 0 shifts. */
{
int i;
for (i = 0; i < nstates; i++)
- if (!state_table[i].shifts)
- state_table[i].shifts = shifts_new (0);
+ if (!state_table[i]->shifts)
+ state_table[i]->shifts = shifts_new (0);
}
/* Initializing the lookaheads members. Please note that it must be
for (i = 0; i < nstates; i++)
{
int k;
- reductions *rp = state_table[i].reductions;
- shifts *sp = state_table[i].shifts;
+ reductions *rp = state_table[i]->reductions;
+ shifts *sp = state_table[i]->shifts;
- state_table[i].lookaheads = count;
+ state_table[i]->lookaheads = count;
if (rp
&& (rp->nreds > 1 || (sp->nshifts && SHIFT_IS_SHIFT (sp, 0))))
count += rp->nreds;
else
- state_table[i].consistent = 1;
+ state_table[i]->consistent = 1;
for (k = 0; k < sp->nshifts; k++)
if (SHIFT_IS_ERROR (sp, k))
{
- state_table[i].consistent = 0;
+ state_table[i]->consistent = 0;
break;
}
}
- state_table[nstates].lookaheads = count;
+
+ /* Seems to be needed by conflicts.c. */
+ state_table[nstates] = STATE_ALLOC (0);
+ state_table[nstates]->lookaheads = count;
}
}
short *np;
reductions *rp;
- size_t nLA = state_table[nstates].lookaheads;
+ size_t nLA = state_table[nstates]->lookaheads;
if (!nLA)
nLA = 1;
np = LAruleno;
for (i = 0; i < nstates; i++)
- if (!state_table[i].consistent)
- if ((rp = state_table[i].reductions))
+ if (!state_table[i]->consistent)
+ if ((rp = state_table[i]->reductions))
for (j = 0; j < rp->nreds; j++)
*np++ = rp->rules[j];
}
for (sp = first_shift; sp; sp = sp->next)
for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i)
{
- symbol = state_table[sp->shifts[i]].accessing_symbol;
+ symbol = state_table[sp->shifts[i]]->accessing_symbol;
if (ngotos == MAXSHORT)
fatal (_("too many gotos (max %d)"), MAXSHORT);
for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i)
{
state2 = sp->shifts[i];
- symbol = state_table[state2].accessing_symbol;
+ symbol = state_table[state2]->accessing_symbol;
k = temp_map[symbol]++;
from_state[k] = state1;
for (i = 0; i < ngotos; i++)
{
int stateno = to_state[i];
- shifts *sp = state_table[stateno].shifts;
+ shifts *sp = state_table[stateno]->shifts;
int j;
for (j = 0; j < sp->nshifts && SHIFT_IS_SHIFT (sp, j); j++)
{
- int symbol = state_table[sp->shifts[j]].accessing_symbol;
+ int symbol = state_table[sp->shifts[j]]->accessing_symbol;
SETBIT (F (i), symbol);
}
for (; j < sp->nshifts; j++)
{
- int symbol = state_table[sp->shifts[j]].accessing_symbol;
+ int symbol = state_table[sp->shifts[j]]->accessing_symbol;
if (nullable[symbol])
edge[nedges++] = map_goto (stateno, symbol);
}
int found;
shorts *sp;
- i = state_table[stateno].lookaheads;
- k = state_table[stateno + 1].lookaheads;
+ i = state_table[stateno]->lookaheads;
+ k = state_table[stateno + 1]->lookaheads;
found = 0;
while (!found && i < k)
{
{
int nedges = 0;
int state1 = from_state[i];
- int symbol1 = state_table[to_state[i]].accessing_symbol;
+ int symbol1 = state_table[to_state[i]]->accessing_symbol;
short *rulep;
for (rulep = derives[symbol1]; *rulep > 0; rulep++)
for (rp = ritem + rule_table[*rulep].rhs; *rp > 0; rp++)
{
- shifts *sp = state_table[stateno].shifts;
+ shifts *sp = state_table[stateno]->shifts;
int j;
for (j = 0; j < sp->nshifts; j++)
{
stateno = sp->shifts[j];
- if (state_table[stateno].accessing_symbol == *rp)
+ if (state_table[stateno]->accessing_symbol == *rp)
break;
}
states[length++] = stateno;
}
- if (!state_table[stateno].consistent)
+ if (!state_table[stateno]->consistent)
add_lookback_edge (stateno, *rulep, i);
length--;
int i;
shorts *sp;
- for (i = 0; i < state_table[nstates].lookaheads; i++)
+ for (i = 0; i < state_table[nstates]->lookaheads; i++)
for (sp = lookback[i]; sp; sp = sp->next)
{
int size = LA (i + 1) - LA (i);
}
/* Free LOOKBACK. */
- for (i = 0; i < state_table[nstates].lookaheads; i++)
+ for (i = 0; i < state_table[nstates]->lookaheads; i++)
LIST_FREE (shorts, lookback[i]);
XFREE (lookback);
#define LA(Rule) (LA + (Rule) * tokensetsize)
-/* A structure decorating a state, with additional information. */
-typedef struct state_s
-{
- /* A state. */
- core *state;
-
- /* Its accessing symbol. */
- short accessing_symbol;
-
- shifts *shifts;
- reductions *reductions;
- errs *errs;
-
- /* Nonzero if no lookahead is needed to decide what to do in state
- S. */
- char consistent;
-
- short lookaheads;
-} state_t;
-
-/* All the decorated states, indexed by the state number. Warning:
- there is a state_TABLE in LR0.c, but it is different and static.
- */
-extern state_t *state_table;
+/* All the states, indexed by the state number. */
+extern state_t **state_table;
extern int tokensetsize;
int i;
short *values = (short *) alloca (sizeof (short) * nstates);
for (i = 0; i < nstates; ++i)
- values[i] = state_table[i].accessing_symbol;
+ values[i] = state_table[i]->accessing_symbol;
output_table_data (&output_obstack, values,
0, 1, nstates);
muscle_insert ("stos", obstack_finish (&output_obstack));
default_rule = 0;
nreds = 0;
- redp = state_table[state].reductions;
+ redp = state_table[state]->reductions;
if (redp)
{
{
/* loop over all the rules available here which require
lookahead */
- m = state_table[state].lookaheads;
- n = state_table[state + 1].lookaheads;
+ m = state_table[state]->lookaheads;
+ n = state_table[state + 1]->lookaheads;
for (i = n - 1; i >= m; i--)
/* and find each token which the rule finds acceptable
/* Now see which tokens are allowed for shifts in this state. For
them, record the shift as the thing to do. So shift is preferred
to reduce. */
- shiftp = state_table[state].shifts;
+ shiftp = state_table[state]->shifts;
for (i = 0; i < shiftp->nshifts; i++)
{
shift_state = shiftp->shifts[i];
if (!shift_state)
continue;
- symbol = state_table[shift_state].accessing_symbol;
+ symbol = state_table[shift_state]->accessing_symbol;
if (ISVAR (symbol))
break;
/* See which tokens are an explicit error in this state (due to
%nonassoc). For them, record MINSHORT as the action. */
- errp = state_table[state].errs;
+ errp = state_table[state]->errs;
if (errp)
{
if (nreds >= 1 && !nodefault)
{
- if (state_table[state].consistent)
+ if (state_table[state]->consistent)
default_rule = redp->rules[0];
else
{
output_table ();
output_check ();
+ LIST_FREE (state_t, first_state);
XFREE (state_table);
}
{
obstack_init (&output_obstack);
- LIST_FREE (core, first_state);
-
output_token_translations ();
output_gram ();
print_core (FILE *out, int state)
{
int i;
- short *sitems = state_table[state].state->items;
- int snitems = state_table[state].state->nitems;
+ short *sitems = state_table[state]->items;
+ int snitems = state_table[state]->nitems;
/* New experimental feature: if TRACE_FLAGS output all the items of
a state, not only its kernel. */
{
int i;
- shifts *shiftp = state_table[state].shifts;
- reductions *redp = state_table[state].reductions;
- errs *errp = state_table[state].errs;
+ shifts *shiftp = state_table[state]->shifts;
+ reductions *redp = state_table[state]->reductions;
+ errs *errp = state_table[state]->errs;
if (!shiftp->nshifts && !redp)
{
if (!SHIFT_IS_DISABLED (shiftp, i))
{
int state1 = shiftp->shifts[i];
- int symbol = state_table[state1].accessing_symbol;
+ int symbol = state_table[state1]->accessing_symbol;
/* The following line used to be turned off. */
if (ISVAR (symbol))
break;
fputc ('\n', out);
}
- if (state_table[state].consistent && redp)
+ if (state_table[state]->consistent && redp)
{
int rule = redp->rules[0];
int symbol = rule_table[rule].lhs;
if (!SHIFT_IS_DISABLED (shiftp, i))
{
int state1 = shiftp->shifts[i];
- int symbol = state_table[state1].accessing_symbol;
+ int symbol = state_table[state1]->accessing_symbol;
fprintf (out, _(" %-4s\tgo to state %d\n"),
tags[symbol], state1);
}
print_core (int state, struct obstack *node_obstack)
{
int i;
- core *statep = state_table[state].state;
+ state_t *statep = state_table[state];
if (!statep->nitems)
return;
{
int i;
- shifts *shiftp = state_table[state].shifts;
- reductions *redp = state_table[state].reductions;
+ shifts *shiftp = state_table[state]->shifts;
+ reductions *redp = state_table[state]->reductions;
#if 0
- errs *errp = err_table[state];
+ errs *errp = state_table[state]->errs;
#endif
static char buff[10];
if (!SHIFT_IS_DISABLED (shiftp, i))
{
int state1 = shiftp->shifts[i];
- int symbol = state_table[state1].accessing_symbol;
+ int symbol = state_table[state1]->accessing_symbol;
new_edge (&edge);
obstack_1grow (node_obstack, '\n');
}
- if (state_table[state].consistent && redp)
+ if (state_table[state]->consistent && redp)
{
rule = redp->rules[0];
symbol = rule_table[rule].lhs;
if (!SHIFT_IS_DISABLED (shiftp, i))
{
int state1 = shiftp->shifts[i];
- int symbol = state_table[state1].accessing_symbol;
+ int symbol = state_table[state1]->accessing_symbol;
new_edge (&edge);
open_edge (&edge, fgraph);
# define STATE_H_
-/*-------.
-| Core. |
-`-------*/
-
-typedef struct core
-{
- struct core *next;
- struct core *link;
- short number;
- short accessing_symbol;
- short nitems;
- short items[1];
-} core;
-
-#define CORE_ALLOC(Nitems) \
- (core *) xcalloc ((unsigned) (sizeof (core) \
- + (Nitems - 1) * sizeof (short)), 1)
-
/*---------.
| Shifts. |
`---------*/
case of gotos. */
#define SHIFT_SYMBOL(Shifts, Shift) \
- (state_table[Shifts->shifts[Shift]].accessing_symbol)
+ (state_table[Shifts->shifts[Shift]]->accessing_symbol)
/* Is the SHIFTS->shifts[Shift] a real shift? (as opposed to gotos.) */
(reductions *) xcalloc ((unsigned) (sizeof (reductions) \
+ (Nreductions - 1) * sizeof (short)), 1)
+
+
+/*----------.
+| State_t. |
+`----------*/
+
+typedef struct state_s
+{
+ struct state_s *next;
+ struct state_s *link;
+
+ short number;
+ short accessing_symbol;
+ shifts *shifts;
+ reductions *reductions;
+ errs *errs;
+
+ /* Nonzero if no lookahead is needed to decide what to do in state S. */
+ char consistent;
+
+ /* Used in LALR, not LR(0). */
+ short lookaheads;
+
+ /* Its items. */
+ short nitems;
+ short items[1];
+} state_t;
+
+#define STATE_ALLOC(Nitems) \
+ (state_t *) xcalloc ((unsigned) (sizeof (state_t) \
+ + (Nitems - 1) * sizeof (short)), 1)
+
#endif /* !STATE_H_ */