X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/c7ca99d4b03860bb258914fbc228272031eec77c..b275314e2dbe2eb5d4706f728aefe132fa93b9c5:/src/state.c diff --git a/src/state.c b/src/state.c index 874c5863..92180895 100644 --- a/src/state.c +++ b/src/state.c @@ -35,19 +35,34 @@ | Create a new array of N shifts/gotos. | `---------------------------------------*/ -#define SHIFTS_ALLOC(Nshifts) \ - (shifts *) xcalloc ((unsigned) (sizeof (shifts) \ - + (Nshifts - 1) * sizeof (short)), 1) +#define TRANSITIONS_ALLOC(Num) \ + (transitions_t *) xcalloc ((sizeof (transitions_t) \ + + (Num - 1) * sizeof (state_number_t)), 1) -shifts * -shifts_new (int n) +static transitions_t * +transitions_new (int num, state_number_t *the_states) { - shifts *res = SHIFTS_ALLOC (n); - res->nshifts = n; + transitions_t *res = TRANSITIONS_ALLOC (num); + res->num = num; + memcpy (res->states, the_states, num * sizeof (the_states[0])); return res; } +/*-------------------------------------------------------------------. +| Return the state such these TRANSITIONS contain a shift/goto to it | +| on SYMBOL. Aborts if none found. | +`-------------------------------------------------------------------*/ + +state_t * +transitions_to (transitions_t *shifts, symbol_number_t s) +{ + int j; + for (j = 0; j < shifts->num; j++) + if (TRANSITION_SYMBOL (shifts, j) == s) + return states[shifts->states[j]]; + abort (); +} /*--------------------. @@ -59,25 +74,17 @@ shifts_new (int n) | Create a new array of N errs. | `-------------------------------*/ -#define ERRS_ALLOC(Nerrs) \ - (errs *) xcalloc ((unsigned) (sizeof (errs) \ - + (Nerrs - 1) * sizeof (short)), 1) +#define ERRS_ALLOC(Nerrs) \ + (errs_t *) xcalloc ((sizeof (errs_t) \ + + (Nerrs - 1) * sizeof (symbol_number_t)), 1) -errs * -errs_new (int n) +errs_t * +errs_new (int num, symbol_number_t *tokens) { - 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 * sizeof (src->errs[0])); + errs_t *res = ERRS_ALLOC (num); + res->num = num; + memcpy (res->symbols, tokens, num * sizeof (tokens[0])); return res; } @@ -93,15 +100,16 @@ errs_dup (errs *src) | Create a new array of N reductions. | `-------------------------------------*/ -#define REDUCTIONS_ALLOC(Nreductions) \ - (reductions *) xcalloc ((unsigned) (sizeof (reductions) \ - + (Nreductions - 1) * sizeof (short)), 1) +#define REDUCTIONS_ALLOC(Nreductions) \ + (reductions_t *) xcalloc ((sizeof (reductions_t) \ + + (Nreductions - 1) * sizeof (rule_number_t)), 1) -reductions * -reductions_new (int n) +static reductions_t * +reductions_new (int num, rule_number_t *reductions) { - reductions *res = REDUCTIONS_ALLOC (n); - res->nreds = n; + reductions_t *res = REDUCTIONS_ALLOC (num); + res->num = num; + memcpy (res->rules, reductions, num * sizeof (reductions[0])); return res; } @@ -121,9 +129,10 @@ state_t *final_state = NULL; (state_t *) xcalloc ((unsigned) (sizeof (state_t) \ + (Nitems - 1) * sizeof (item_number_t)), 1) -/*------------------------------------------------------------. -| Create a new state with ACCESSING_SYMBOL, for those items. | -`------------------------------------------------------------*/ +/*------------------------------------------------------------------. +| Create a new state with ACCESSING_SYMBOL, for those items. Store | +| it in the state hash table. | +`------------------------------------------------------------------*/ state_t * state_new (symbol_number_t accessing_symbol, @@ -143,10 +152,63 @@ state_new (symbol_number_t accessing_symbol, res->nitems = core_size; memcpy (res->items, core, core_size * sizeof (core[0])); + state_hash_insert (res); + return res; } +/*-------------. +| Free STATE. | +`-------------*/ + +static void +state_free (state_t *state) +{ + free (state->transitions); + free (state->reductions); + free (state->errs); + free (state); +} + + +/*-------------------------------. +| Set the transitions of STATE. | +`-------------------------------*/ + +void +state_transitions_set (state_t *state, int num, state_number_t *transitions) +{ + assert (!state->transitions); + state->transitions = transitions_new (num, transitions); +} + + +/*------------------------------. +| Set the reductions of STATE. | +`------------------------------*/ + +void +state_reductions_set (state_t *state, int num, rule_number_t *reductions) +{ + assert (!state->reductions); + state->reductions = reductions_new (num, reductions); +} + + +/*------------------------. +| Set the errs of STATE. | +`------------------------*/ + +void +state_errs_set (state_t *state, int num, symbol_number_t *tokens) +{ + assert (!state->errs); + state->errs = errs_new (num, tokens); +} + + + /*--------------------------------------------------------------. | Print on OUT all the lookaheads such that this STATE wants to | | reduce this RULE. | @@ -156,12 +218,12 @@ void state_rule_lookaheads_print (state_t *state, rule_t *rule, FILE *out) { int j, k; + bitset_iterator biter; int nlookaheads = 0; /* Count the number of lookaheads corresponding to this rule. */ for (j = 0; j < state->nlookaheads; ++j) - for (k = 0; k < ntokens; ++k) - if (bitset_test (state->lookaheads[j], k) - && state->lookaheads_rule[j]->number == rule->number) + BITSET_FOR_EACH (biter, state->lookaheads[j], k, 0) + if (state->lookaheads_rule[j]->number == rule->number) nlookaheads++; /* Print them if there are. */ @@ -169,11 +231,10 @@ state_rule_lookaheads_print (state_t *state, rule_t *rule, FILE *out) { fprintf (out, " ["); for (j = 0; j < state->nlookaheads; ++j) - for (k = 0; k < ntokens; ++k) - if (bitset_test (state->lookaheads[j], k) - && state->lookaheads_rule[j]->number == rule->number) + BITSET_FOR_EACH (biter, state->lookaheads[j], k, 0) + if (state->lookaheads_rule[j]->number == rule->number) fprintf (out, "%s%s", - symbol_tag_get (symbols[k]), + symbols[k]->tag, --nlookaheads ? ", " : ""); fprintf (out, "]"); } @@ -284,13 +345,7 @@ void states_free (void) { state_number_t i; - for (i = 0; i < nstates; ++i) - { - free (states[i]->shifts); - XFREE (states[i]->reductions); - free (states[i]->errs); - free (states[i]); - } - XFREE (states); + state_free (states[i]); + free (states); }