- XFREE (state_count);
- XFREE (yydefgoto);
-}
-
-
-/*------------------------------------------------------------------.
-| Compute ORDER, a reordering of vectors, in order to decide how to |
-| pack the actions and gotos information into yytable. |
-`------------------------------------------------------------------*/
-
-static void
-sort_actions (void)
-{
- int i;
-
- nentries = 0;
-
- for (i = 0; i < nvectors; i++)
- if (tally[i] > 0)
- {
- int k;
- int t = tally[i];
- int w = width[i];
- int j = nentries - 1;
-
- while (j >= 0 && (width[order[j]] < w))
- j--;
-
- while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
- j--;
-
- for (k = nentries - 1; k > j; k--)
- order[k + 1] = order[k];
-
- order[j + 1] = i;
- nentries++;
- }
-}
-
-
-/* If VECTOR is a state which actions (reflected by FROMS, TOS, TALLY
- and WIDTH of VECTOR) are common to a previous state, return this
- state number.
-
- In any other case, return -1. */
-
-static state_number_t
-matching_state (vector_number_t vector)
-{
- vector_number_t i = order[vector];
- int t;
- int w;
- int prev;
-
- /* If VECTOR is a nterm, return -1. */
- if (i >= (int) nstates)
- return -1;
-
- t = tally[i];
- w = width[i];
-
- for (prev = vector - 1; prev >= 0; prev--)
- {
- vector_number_t j = order[prev];
- int k;
- int match = 1;
-
- /* Given how ORDER was computed, if the WIDTH or TALLY is
- different, there cannot be a matching state. */
- if (width[j] != w || tally[j] != t)
- return -1;
-
- for (k = 0; match && k < t; k++)
- if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
- match = 0;
-
- if (match)
- return j;
- }
-
- return -1;
-}
-
-
-static base_t
-pack_vector (vector_number_t vector)
-{
- vector_number_t i = order[vector];
- int j;
- int t = tally[i];
- int loc = 0;
- base_t *from = froms[i];
- base_t *to = tos[i];
- unsigned int *conflict_to = conflict_tos[i];
-
- assert (t);
-
- for (j = lowzero - from[0]; j < (int) table_size; j++)
- {
- int k;
- int ok = 1;
-
- for (k = 0; ok && k < t; k++)
- {
- loc = j + state_number_as_int (from[k]);
- if (loc > (int) table_size)
- table_grow (loc);
-
- if (table[loc] != 0)
- ok = 0;
- }
-
- for (k = 0; ok && k < vector; k++)
- if (pos[k] == j)
- ok = 0;
-
- if (ok)
- {
- for (k = 0; k < t; k++)
- {
- loc = j + from[k];
- table[loc] = to[k];
- if (glr_parser && conflict_to != NULL)
- conflict_table[loc] = conflict_to[k];
- check[loc] = from[k];
- }
-
- while (table[lowzero] != 0)
- lowzero++;
-
- if (loc > high)
- high = loc;
-
- if (j < BASE_MIN || BASE_MAX < j)
- fatal ("base_t too small to hold %d\n", j);
- return j;
- }
- }
-#define pack_vector_succeeded 0
- assert (pack_vector_succeeded);
- return 0;
-}
-
-
-/*-------------------------------------------------------------.
-| Remap the negative infinite in TAB from NINF to the greatest |
-| possible smallest value. Return it. |
-| |
-| In most case this allows us to use shorts instead of ints in |
-| parsers. |
-`-------------------------------------------------------------*/
-
-static base_t
-table_ninf_remap (base_t tab[], size_t size, base_t ninf)
-{
- base_t res = 0;
- size_t i;
-
- for (i = 0; i < size; i++)
- if (tab[i] < res && tab[i] != ninf)
- res = base[i];
-
- --res;
-
- for (i = 0; i < size; i++)
- if (tab[i] == ninf)
- tab[i] = res;
-
- return res;
-}
-
-static void
-pack_table (void)
-{
- int i;
-
- base = XCALLOC (base_t, nvectors);
- pos = XCALLOC (base_t, nentries);
- table = XCALLOC (base_t, table_size);
- if (glr_parser)
- conflict_table = XCALLOC (unsigned int, table_size);
- check = XCALLOC (base_t, table_size);
-
- lowzero = 0;
- high = 0;
-
- for (i = 0; i < nvectors; i++)
- base[i] = BASE_MIN;
-
- for (i = 0; i < (int) table_size; i++)
- check[i] = -1;
-
- for (i = 0; i < nentries; i++)
- {
- state_number_t state = matching_state (i);
- base_t place;
-
- if (state < 0)
- /* A new set of state actions, or a nonterminal. */
- place = pack_vector (i);
- else
- /* Action of I were already coded for STATE. */
- place = base[state];
-
- pos[i] = place;
- base[order[i]] = place;
- }
-
- /* Use the greatest possible negative infinites. */
- base_ninf = table_ninf_remap (base, nvectors, BASE_MIN);
- table_ninf = table_ninf_remap (table, high + 1, ACTION_MIN);
-
- for (i = 0; i < nvectors; i++)
- {
- XFREE (froms[i]);
- XFREE (tos[i]);
- XFREE (conflict_tos[i]);
- }