X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/f004bf6ac4bdfe0b7dcd554eac8c8899512f9475..3feec03433c1d0b0aed2a17dd6ffde6a37667647:/src/lalr.c diff --git a/src/lalr.c b/src/lalr.c index 9d11c42d..c6d3f5c3 100644 --- a/src/lalr.c +++ b/src/lalr.c @@ -1,5 +1,5 @@ /* 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. @@ -48,9 +48,13 @@ short *to_state; 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. . */ +static unsigned *F = NULL; +#define F(Rule) (F + (Rule) * tokensetsize) + static short **includes; static shorts **lookback; static short **R; @@ -74,8 +78,8 @@ traverse (int i) 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) @@ -89,7 +93,7 @@ traverse (int i) INDEX[i] = INDEX[j]; fp1 = base; - fp2 = F + j * tokensetsize; + fp2 = F (j); while (fp1 < fp3) *fp1++ |= *fp2++; @@ -107,7 +111,7 @@ traverse (int i) break; fp1 = base; - fp2 = F + j * tokensetsize; + fp2 = F (j); while (fp1 < fp3) *fp2++ = *fp1++; @@ -132,15 +136,18 @@ digraph (short **relation) 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) { @@ -169,11 +176,45 @@ 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; @@ -195,7 +236,7 @@ set_maxrhs (void) } } - maxrhs = max; + return max; } @@ -204,61 +245,23 @@ initialize_LA (void) { 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]; } @@ -431,10 +434,7 @@ initialize_F (void) digraph (reads); for (i = 0; i < ngotos; i++) - { - if (reads[i]) - XFREE (reads[i]); - } + XFREE (reads[i]); XFREE (reads); XFREE (edge); @@ -547,7 +547,7 @@ build_relations (void) 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++) { @@ -630,10 +630,7 @@ compute_FOLLOWS (void) digraph (includes); for (i = 0; i < ngotos; i++) - { - if (includes[i]) - XFREE (includes[i]); - } + XFREE (includes[i]); XFREE (includes); } @@ -643,32 +640,21 @@ static void 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 */ + shorts *sptmp; for (sp = lookback[i]; sp; sp = sptmp) { sptmp = sp->next; @@ -687,7 +673,6 @@ lalr (void) tokensetsize = WORDSIZE (ntokens); set_state_table (); - set_maxrhs (); initialize_LA (); set_goto_map (); initialize_F ();