]> git.saurik.com Git - bison.git/blobdiff - src/state.c
* src/state.h (reductions_t): Rename member `nreds' as num.
[bison.git] / src / state.c
index 8148702e57e05ce1446dc2edf0805c18dae45321..37add6d1f37839e9992d30f3c3a8220feeca62fe 100644 (file)
 | Create a new array of N shifts/gotos.  |
 `---------------------------------------*/
 
-#define SHIFTS_ALLOC(Nshifts)                                          \
-  (shifts_t *) xcalloc ((unsigned) (sizeof (shifts_t)                  \
+#define TRANSITIONS_ALLOC(Nshifts)                                             \
+  (transitions_t *) xcalloc ((unsigned) (sizeof (transitions_t)                        \
                                   + (Nshifts - 1) * sizeof (state_number_t)), 1)
 
-static shifts_t *
-shifts_new (int nshifts, state_number_t *shifts)
+static transitions_t *
+transitions_new (int num, state_number_t *the_states)
 {
-  shifts_t *res = SHIFTS_ALLOC (nshifts);
-  res->nshifts = nshifts;
-  memcpy (res->shifts, shifts, nshifts * sizeof (shifts[0]));
+  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 ();
+}
 
 
                        /*--------------------.
@@ -61,24 +75,24 @@ shifts_new (int nshifts, state_number_t *shifts)
 `-------------------------------*/
 
 #define ERRS_ALLOC(Nerrs)                                              \
-  (errs *) xcalloc ((unsigned) (sizeof (errs)                          \
-                                  + (Nerrs - 1) * sizeof (short)), 1)
+  (errs_t *) xcalloc ((unsigned) (sizeof (errs_t)                      \
+                                  + (Nerrs - 1) * sizeof (symbol_number_t)), 1)
 
 
-errs *
+errs_t *
 errs_new (int n)
 {
-  errs *res = ERRS_ALLOC (n);
-  res->nerrs = n;
+  errs_t *res = ERRS_ALLOC (n);
+  res->num = n;
   return res;
 }
 
 
-errs *
-errs_dup (errs *src)
+errs_t *
+errs_dup (errs_t *src)
 {
-  errs *res = errs_new (src->nerrs);
-  memcpy (res->errs, src->errs, src->nerrs * sizeof (src->errs[0]));
+  errs_t *res = errs_new (src->num);
+  memcpy (res->symbols, src->symbols, src->num * sizeof (src->symbols[0]));
   return res;
 }
 
@@ -95,14 +109,15 @@ errs_dup (errs *src)
 `-------------------------------------*/
 
 #define REDUCTIONS_ALLOC(Nreductions)                                  \
-  (reductions *) xcalloc ((unsigned) (sizeof (reductions)              \
-                                  + (Nreductions - 1) * sizeof (short)), 1)
+  (reductions_t *) xcalloc ((unsigned) (sizeof (reductions_t)          \
+                                  + (Nreductions - 1) * sizeof (rule_number_t)), 1)
 
-reductions *
-reductions_new (int n)
+static reductions_t *
+reductions_new (int nreductions, short *reductions)
 {
-  reductions *res = REDUCTIONS_ALLOC (n);
-  res->nreds = n;
+  reductions_t *res = REDUCTIONS_ALLOC (nreductions);
+  res->num = nreductions;
+  memcpy (res->rules, reductions, nreductions * sizeof (reductions[0]));
   return res;
 }
 
@@ -153,9 +168,20 @@ state_new (symbol_number_t accessing_symbol,
 `--------------------------*/
 
 void
-state_shifts_set (state_t *state, int nshifts, state_number_t *shifts)
+state_transitions_set (state_t *state, int nshifts, state_number_t *shifts)
 {
-  state->shifts = shifts_new (nshifts, shifts);
+  state->shifts = transitions_new (nshifts, shifts);
+}
+
+
+/*------------------------------.
+| Set the reductions of STATE.  |
+`------------------------------*/
+
+void
+state_reductions_set (state_t *state, int nreductions, short *reductions)
+{
+  state->reductions = reductions_new (nreductions, reductions);
 }
 
 
@@ -172,22 +198,24 @@ state_rule_lookaheads_print (state_t *state, rule_t *rule, FILE *out)
   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_EXECUTE (state->lookaheads[j], 0, k,
+    {
+      if (state->lookaheads_rule[j]->number == rule->number)
        nlookaheads++;
+    });
 
   /* Print them if there are.  */
   if (nlookaheads)
     {
       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_EXECUTE (state->lookaheads[j], 0, k,
+       {
+         if (state->lookaheads_rule[j]->number == rule->number)
            fprintf (out, "%s%s",
                     symbol_tag_get (symbols[k]),
                     --nlookaheads ? ", " : "");
+       });
       fprintf (out, "]");
     }
 }