From 2cec70b9f139dd207022c35fdbe8b49fd1cf230d Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 27 Dec 2001 18:10:48 +0000 Subject: [PATCH 1/1] * src/state.h, src/state.c (errs_new, errs_dup): New. * src/LR0.c (set_state_table): Let all the states have an errs, even if reduced to 0. * src/print.c (print_errs, print_reductions): Adjust. * src/output.c (output_actions, action_row): Adjust. * src/conflicts.c (resolve_sr_conflict): Adjust. --- ChangeLog | 10 ++++++++++ src/LR0.c | 24 +++++++++++------------- src/conflicts.c | 11 ++++------- src/output.c | 15 +++++++-------- src/print.c | 12 ++++-------- src/state.c | 31 +++++++++++++++++++++++++++++++ src/state.h | 12 +++--------- 7 files changed, 70 insertions(+), 45 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6966cac3..c600ff3a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2001-12-27 Akim Demaille + + * src/state.h, src/state.c (errs_new, errs_dup): New. + * src/LR0.c (set_state_table): Let all the states have an errs, + even if reduced to 0. + * src/print.c (print_errs, print_reductions): Adjust. + * src/output.c (output_actions, action_row): Adjust. + * src/conflicts.c (resolve_sr_conflict): Adjust. + + 2001-12-27 Akim Demaille * src/lalr.c (set_goto_map, initialize_F): Use SHIFT_SYMBOL. diff --git a/src/LR0.c b/src/LR0.c index 148e2bc0..4f771d21 100644 --- a/src/LR0.c +++ b/src/LR0.c @@ -541,22 +541,20 @@ save_reductions (void) static void set_state_table (void) { + state_t *sp; state_table = XCALLOC (state_t *, nstates); - { - state_t *sp; - 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 + the states have a shifts and errs, even if reduced to 0. */ + if (!sp->shifts) + sp->shifts = shifts_new (0); + if (!sp->errs) + sp->errs = errs_new (0); + state_table[sp->number] = sp; - } - - /* 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); - } + } } /*-------------------------------------------------------------------. diff --git a/src/conflicts.c b/src/conflicts.c index 01fff4f1..2f9b4c24 100644 --- a/src/conflicts.c +++ b/src/conflicts.c @@ -93,8 +93,8 @@ resolve_sr_conflict (state_t *state, int lookahead) int i; /* find the rule to reduce by to get precedence of reduction */ int redprec = rule_table[LAruleno[lookahead]].prec; - errs *errp = ERRS_ALLOC (ntokens + 1); - short *errtokens = errp->errs; + errs *errp = errs_new (ntokens + 1); + errp->nerrs = 0; for (i = 0; i < ntokens; i++) if (BITISSET (LA (lookahead), i) @@ -137,17 +137,14 @@ resolve_sr_conflict (state_t *state, int lookahead) flush_shift (state, i); flush_reduce (lookahead, i); /* Record an explicit error for this token. */ - *errtokens++ = i; + errp->errs[errp->nerrs++] = i; break; } } - errp->nerrs = errtokens - errp->errs; /* Some tokens have been explicitly made errors. Allocate a permanent errs structure for this state, to record them. */ - i = (char *) errtokens - (char *) errp; - state->errs = ERRS_ALLOC (i + 1); - memcpy (state->errs, errp, i); + state->errs = errs_dup (errp); free (errp); } diff --git a/src/output.c b/src/output.c index dfbfbb50..c424716c 100644 --- a/src/output.c +++ b/src/output.c @@ -375,12 +375,11 @@ action_row (state_t *state) /* See which tokens are an explicit error in this state (due to %nonassoc). For them, record MINSHORT as the action. */ - if (errp) - for (i = 0; i < errp->nerrs; i++) - { - int symbol = errp->errs[i]; - actrow[symbol] = MINSHORT; - } + for (i = 0; i < errp->nerrs; i++) + { + int symbol = errp->errs[i]; + actrow[symbol] = MINSHORT; + } /* Now find the most common reduction and make it the default action for this state. */ @@ -903,9 +902,9 @@ output_actions (void) for (i = 0; i < nstates; ++i) { - XFREE (state_table[i]->shifts); + free (state_table[i]->shifts); XFREE (state_table[i]->reductions); - XFREE (state_table[i]->errs); + free (state_table[i]->errs); free (state_table[i]); } XFREE (state_table); diff --git a/src/print.c b/src/print.c index e1f53a1e..b9c518f7 100644 --- a/src/print.c +++ b/src/print.c @@ -124,16 +124,13 @@ print_errs (FILE *out, state_t *state) errs *errp = state->errs; int i; - if (!errp) - return; - for (i = 0; i < errp->nerrs; ++i) if (errp->errs[i]) fprintf (out, _(" %-4s\terror (nonassociative)\n"), tags[errp->errs[i]]); if (i > 0) - fputc ('\n', out); + fputc ('\n', out); } @@ -192,10 +189,9 @@ print_reductions (FILE *out, state_t *state) SETBIT (shiftset, SHIFT_SYMBOL (shiftp, i)); } - if (errp) - for (i = 0; i < errp->nerrs; i++) - if (errp->errs[i]) - SETBIT (shiftset, errp->errs[i]); + for (i = 0; i < errp->nerrs; i++) + if (errp->errs[i]) + SETBIT (shiftset, errp->errs[i]); if (state->nlookaheads == 1 && !nodefault) { diff --git a/src/state.c b/src/state.c index 7c3b6921..ed4a5082 100644 --- a/src/state.c +++ b/src/state.c @@ -26,6 +26,10 @@ | Create a new array of N shitfs. | `---------------------------------*/ +#define SHIFTS_ALLOC(Nshifts) \ + (shifts *) xcalloc ((unsigned) (sizeof (shifts) \ + + (Nshifts - 1) * sizeof (short)), 1) + shifts * shifts_new (int n) { @@ -33,3 +37,30 @@ shifts_new (int n) res->nshifts = n; return res; } + + +/*-------------------------------. +| Create a new array of N errs. | +`-------------------------------*/ + +#define ERRS_ALLOC(Nerrs) \ + (errs *) xcalloc ((unsigned) (sizeof (errs) \ + + (Nerrs - 1) * sizeof (short)), 1) + + +errs * +errs_new (int n) +{ + errs *res = ERRS_ALLOC (n); + res->nerrs = n; + return res; +} + + +errs * +errs_dup (errs *src) +{ + errs *res = errs_new (src->nerrs); + memcpy (res->errs, src->errs, src->nerrs); + return res; +} diff --git a/src/state.h b/src/state.h index a31af20b..14017afe 100644 --- a/src/state.h +++ b/src/state.h @@ -99,12 +99,7 @@ typedef struct shifts short shifts[1]; } shifts; - -#define SHIFTS_ALLOC(Nshifts) \ - (shifts *) xcalloc ((unsigned) (sizeof (shifts) \ - + (Nshifts - 1) * sizeof (short)), 1) - -shifts * shifts_new PARAMS ((int n)); +shifts *shifts_new PARAMS ((int n)); /* What is the symbol which is shifted by SHIFTS->shifts[Shift]? Can @@ -149,9 +144,8 @@ typedef struct errs short errs[1]; } errs; -#define ERRS_ALLOC(Nerrs) \ - (errs *) xcalloc ((unsigned) (sizeof (errs) \ - + (Nerrs - 1) * sizeof (short)), 1) +errs *errs_new PARAMS ((int n)); +errs *errs_dup PARAMS ((errs *src)); /*-------------. -- 2.45.2