From a845a69707ff13ac1382ae28378c890b584e36a4 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Mon, 19 Nov 2001 10:08:34 +0000 Subject: [PATCH] * src/lalr.c (initialize_LA): Only initialize LA. Let... (set_state_table): handle the `lookaheads' members. --- ChangeLog | 6 ++++ src/lalr.c | 93 ++++++++++++++++++++++++++---------------------------- 2 files changed, 50 insertions(+), 49 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3f2dcdbb..b3df6f0c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2001-11-19 Akim Demaille + + * src/lalr.c (initialize_LA): Only initialize LA. Let... + (set_state_table): handle the `lookaheads' members. + + 2001-11-19 Akim Demaille * src/lalr.h (lookaheads): Removed array, which contents is now diff --git a/src/lalr.c b/src/lalr.c index 9d11c42d..ce076d4d 100644 --- a/src/lalr.c +++ b/src/lalr.c @@ -169,6 +169,39 @@ 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; + } } @@ -204,61 +237,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]; } -- 2.49.0