/* Compute look-ahead criteria for bison,
- Copyright 1984, 1986, 1989, 2000 Free Software Foundation, Inc.
+ Copyright 1984, 1986, 1989, 2000, 2001 Free Software Foundation, Inc.
This file is part of Bison, the GNU Compiler Compiler.
extern void berror PARAMS ((const char *));
static int infinity;
-static int maxrhs;
static int ngotos;
-static unsigned *F;
+
+/* And for the famous F variable, which named is so descriptive that a
+ comment is hardly needed. <grin>. */
+static unsigned *F = NULL;
+#define F(Rule) (F + (Rule) * tokensetsize)
+
static short **includes;
static shorts **lookback;
static short **R;
VERTICES[++top] = i;
INDEX[i] = height = top;
- base = F + i * tokensetsize;
- fp3 = base + tokensetsize;
+ base = F (i);
+ fp3 = F (i + 1);
rp = R[i];
if (rp)
INDEX[i] = INDEX[j];
fp1 = base;
- fp2 = F + j * tokensetsize;
+ fp2 = F (j);
while (fp1 < fp3)
*fp1++ |= *fp2++;
break;
fp1 = base;
- fp2 = F + j * tokensetsize;
+ fp2 = F (j);
while (fp1 < fp3)
*fp2++ = *fp1++;
INDEX[i] = 0;
for (i = 0; i < ngotos; i++)
- {
- if (INDEX[i] == 0 && R[i])
- traverse (i);
- }
+ if (INDEX[i] == 0 && R[i])
+ traverse (i);
XFREE (INDEX);
XFREE (VERTICES);
}
+
+/*--------------------.
+| Build STATE_TABLE. |
+`--------------------*/
+
static void
set_state_table (void)
{
for (rp = first_reduction; rp; rp = rp->next)
state_table[rp->number].reduction_table = rp;
}
+
+ /* Initializing the lookaheads members. Please note that it must be
+ performed after having set some of the other members which are
+ used below. Change with extreme caution. */
+ {
+ int i;
+ int count = 0;
+ for (i = 0; i < nstates; i++)
+ {
+ int k;
+ reductions *rp = state_table[i].reduction_table;
+ shifts *sp = state_table[i].shift_table;
+
+ state_table[i].lookaheads = count;
+
+ if (rp
+ && (rp->nreds > 1
+ || (sp && !ISVAR (state_table[sp->shifts[0]].accessing_symbol))))
+ count += rp->nreds;
+ else
+ state_table[i].consistent = 1;
+
+ if (sp)
+ for (k = 0; k < sp->nshifts; k++)
+ if (state_table[sp->shifts[k]].accessing_symbol
+ == error_token_number)
+ {
+ state_table[i].consistent = 0;
+ break;
+ }
+ }
+ state_table[nstates].lookaheads = count;
+ }
}
-static void
-set_maxrhs (void)
+/* Return the size of the longest ride hand side of the rules. */
+static size_t
+maxrhs (void)
{
short *itemp;
int length;
}
}
- maxrhs = max;
+ return max;
}
{
int i;
int j;
- int count = 0;
- reductions *rp;
- shifts *sp;
short *np;
+ reductions *rp;
- for (i = 0; i < nstates; i++)
- {
- int k;
-
- state_table[i].lookaheads = count;
-
- rp = state_table[i].reduction_table;
- sp = state_table[i].shift_table;
- if (rp
- && (rp->nreds > 1
- || (sp && !ISVAR (state_table[sp->shifts[0]].accessing_symbol))))
- count += rp->nreds;
- else
- state_table[i].consistent = 1;
-
- if (sp)
- for (k = 0; k < sp->nshifts; k++)
- if (state_table[sp->shifts[k]].accessing_symbol
- == error_token_number)
- {
- state_table[i].consistent = 0;
- break;
- }
- }
-
- state_table[nstates].lookaheads = count;
+ size_t nLA = state_table[nstates].lookaheads;
+ if (!nLA)
+ nLA = 1;
- if (count == 0)
- {
- LA = XCALLOC (unsigned, 1 * tokensetsize);
- LAruleno = XCALLOC (short, 1);
- lookback = XCALLOC (shorts *, 1);
- }
- else
- {
- LA = XCALLOC (unsigned, count * tokensetsize);
- LAruleno = XCALLOC (short, count);
- lookback = XCALLOC (shorts *, count);
- }
+ LA = XCALLOC (unsigned, nLA * tokensetsize);
+ LAruleno = XCALLOC (short, nLA);
+ lookback = XCALLOC (shorts *, nLA);
np = LAruleno;
for (i = 0; i < nstates; i++)
- {
- if (!state_table[i].consistent)
- {
- if ((rp = state_table[i].reduction_table))
- for (j = 0; j < rp->nreds; j++)
- *np++ = rp->rules[j];
- }
- }
+ if (!state_table[i].consistent)
+ if ((rp = state_table[i].reduction_table))
+ for (j = 0; j < rp->nreds; j++)
+ *np++ = rp->rules[j];
}
digraph (reads);
for (i = 0; i < ngotos; i++)
- {
- if (reads[i])
- XFREE (reads[i]);
- }
+ XFREE (reads[i]);
XFREE (reads);
XFREE (edge);
includes = XCALLOC (short *, ngotos);
edge = XCALLOC (short, ngotos + 1);
- states = XCALLOC (short, maxrhs + 1);
+ states = XCALLOC (short, maxrhs () + 1);
for (i = 0; i < ngotos; i++)
{
states[0] = state1;
stateno = state1;
- for (rp = ritem + rrhs[*rulep]; *rp > 0; rp++)
+ for (rp = ritem + rule_table[*rulep].rhs; *rp > 0; rp++)
{
symbol2 = *rp;
sp = state_table[stateno].shift_table;
digraph (includes);
for (i = 0; i < ngotos; i++)
- {
- if (includes[i])
- XFREE (includes[i]);
- }
+ XFREE (includes[i]);
XFREE (includes);
}
compute_lookaheads (void)
{
int i;
- int n;
- unsigned *fp1;
- unsigned *fp2;
- unsigned *fp3;
shorts *sp;
- unsigned *rowp;
- shorts *sptmp; /* JF */
- rowp = LA;
for (i = 0; i < state_table[nstates].lookaheads; i++)
- {
- fp3 = rowp + tokensetsize;
- for (sp = lookback[i]; sp; sp = sp->next)
- {
- fp1 = rowp;
- fp2 = F + tokensetsize * sp->value;
- while (fp1 < fp3)
- *fp1++ |= *fp2++;
- }
-
- rowp = fp3;
- }
+ for (sp = lookback[i]; sp; sp = sp->next)
+ {
+ unsigned *fp1 = LA (i);
+ unsigned *fp2 = F (sp->value);
+ while (fp1 < LA (i + 1))
+ *fp1++ |= *fp2++;
+ }
+ /* Free LOOKBACK. */
for (i = 0; i < state_table[nstates].lookaheads; i++)
- {
- /* JF removed ref to freed storage */
- for (sp = lookback[i]; sp; sp = sptmp)
- {
- sptmp = sp->next;
- XFREE (sp);
- }
- }
+ LIST_FREE (shorts, lookback[i]);
XFREE (lookback);
XFREE (F);
tokensetsize = WORDSIZE (ntokens);
set_state_table ();
- set_maxrhs ();
initialize_LA ();
set_goto_map ();
initialize_F ();