X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/4b3d3a8e4629cc3b0a6d011d80e1997f8306c304..f1886bb2808a471b18e1416e0f821d08850d116e:/src/lalr.c

diff --git a/src/lalr.c b/src/lalr.c
index 6d4a431a..2c0c942a 100644
--- a/src/lalr.c
+++ b/src/lalr.c
@@ -52,8 +52,12 @@ typedef struct goto_list_s
 } goto_list_t;
 
 
-rule_t **LArule = NULL;
-bitsetv LA = NULL;
+/* LA is a LR by NTOKENS matrix of bits.  LA[l, i] is 1 if the rule
+   LArule[l] is applicable in the appropriate state when the next
+   token is symbol i.  If LA[l, i] and LA[l, j] are both 1 for i != j,
+   it is a conflict.  */
+
+static bitsetv LA = NULL;
 size_t nLA;
 
 
@@ -67,29 +71,6 @@ static goto_list_t **lookback;
 
 
 
-static void
-initialize_LA (void)
-{
-  state_number_t i;
-  int j;
-  rule_t **np;
-
-  /* Avoid having to special case 0.  */
-  if (!nLA)
-    nLA = 1;
-
-  LA = bitsetv_create (nLA, ntokens, BITSET_FIXED);
-  LArule = XCALLOC (rule_t *, nLA);
-  lookback = XCALLOC (goto_list_t *, nLA);
-
-  np = LArule;
-  for (i = 0; i < nstates; i++)
-    if (!states[i]->consistent)
-      for (j = 0; j < states[i]->reductions->num; j++)
-	*np++ = &rules[states[i]->reductions->rules[j]];
-}
-
-
 static void
 set_goto_map (void)
 {
@@ -141,7 +122,7 @@ set_goto_map (void)
 	{
 	  int k = temp_map[TRANSITION_SYMBOL (sp, i)]++;
 	  from_state[k] = state;
-	  to_state[k] = sp->states[i];
+	  to_state[k] = sp->states[i]->number;
 	}
     }
 
@@ -200,7 +181,7 @@ initialize_F (void)
       transitions_t *sp = states[stateno]->transitions;
 
       int j;
-      for (j = 0; j < sp->num && TRANSITION_IS_SHIFT (sp, j); j++)
+      FOR_EACH_SHIFT (sp, j)
 	bitset_set (F[i], TRANSITION_SYMBOL (sp, j));
 
       for (; j < sp->num; j++)
@@ -230,21 +211,13 @@ initialize_F (void)
 
 
 static void
-add_lookback_edge (state_t *state, rule_number_t ruleno, int gotono)
+add_lookback_edge (state_t *state, rule_t *rule, int gotono)
 {
-  int i;
-  goto_list_t *sp;
-
-  for (i = 0; i < state->nlookaheads; ++i)
-    if (state->lookaheads_rule[i]->number == ruleno)
-      break;
-
-  assert (state->lookaheads_rule[i]->number == ruleno);
-
-  sp = XCALLOC (goto_list_t, 1);
-  sp->next = lookback[(state->lookaheads - LA) + i];
+  int r = state_reduction_find (state, rule);
+  goto_list_t *sp = XCALLOC (goto_list_t, 1);
+  sp->next = lookback[(state->reductions->lookaheads - LA) + r];
   sp->value = gotono;
-  lookback[(state->lookaheads - LA) + i] = sp;
+  lookback[(state->reductions->lookaheads - LA) + r] = sp;
 }
 
 
@@ -262,9 +235,9 @@ build_relations (void)
     {
       int nedges = 0;
       symbol_number_t symbol1 = states[to_state[i]]->accessing_symbol;
-      rule_number_t *rulep;
+      rule_t **rulep;
 
-      for (rulep = derives[symbol1]; *rulep >= 0; rulep++)
+      for (rulep = derives[symbol1]; *rulep; rulep++)
 	{
 	  int done;
 	  int length = 1;
@@ -272,7 +245,7 @@ build_relations (void)
 	  state_t *state = states[from_state[i]];
 	  states1[0] = state->number;
 
-	  for (rp = rules[*rulep].rhs; *rp >= 0; rp++)
+	  for (rp = (*rulep)->rhs; *rp >= 0; rp++)
 	    {
 	      state = transitions_to (state->transitions,
 				      item_number_as_symbol_number (*rp));
@@ -351,67 +324,72 @@ compute_lookaheads (void)
 }
 
 
-/*-------------------------------------------------------------.
-| Count the number of lookaheads required for each state       |
-| (NLOOKAHEADS member).  Compute the total number of LA, NLA.  |
-`-------------------------------------------------------------*/
+/*---------------------------------------------------------------.
+| Count the number of lookaheads required for STATE (NLOOKAHEADS |
+| member).                                                       |
+`---------------------------------------------------------------*/
 
-static void
-states_lookaheads_count (void)
+static int
+state_lookaheads_count (state_t *state)
 {
-  state_number_t i;
-  nLA = 0;
-
-  /* Count   */
-  for (i = 0; i < nstates; i++)
-    {
-      int k;
-      int nlookaheads = 0;
-      reductions_t *rp = states[i]->reductions;
-      transitions_t *sp = states[i]->transitions;
-
-      /* We need a lookahead either to distinguish different
-	 reductions (i.e., there are two or more), or to distinguish a
-	 reduction from a shift.  Otherwise, it is straightforward,
-	 and the state is `consistent'.  */
-      if (rp->num > 1
-	  || (rp->num == 1 && sp->num && TRANSITION_IS_SHIFT (sp, 0)))
-	nlookaheads += rp->num;
-      else
-	states[i]->consistent = 1;
-
-      for (k = 0; k < sp->num; k++)
-	if (TRANSITION_IS_ERROR (sp, k))
-	  {
-	    states[i]->consistent = 0;
-	    break;
-	  }
+  int k;
+  int nlookaheads = 0;
+  reductions_t *rp = state->reductions;
+  transitions_t *sp = state->transitions;
+
+  /* We need a lookahead either to distinguish different
+     reductions (i.e., there are two or more), or to distinguish a
+     reduction from a shift.  Otherwise, it is straightforward,
+     and the state is `consistent'.  */
+  if (rp->num > 1
+      || (rp->num == 1 && sp->num &&
+	  !TRANSITION_IS_DISABLED (sp, 0) && TRANSITION_IS_SHIFT (sp, 0)))
+    nlookaheads += rp->num;
+  else
+    state->consistent = 1;
+
+  for (k = 0; k < sp->num; k++)
+    if (!TRANSITION_IS_DISABLED (sp, k) && TRANSITION_IS_ERROR (sp, k))
+      {
+	state->consistent = 0;
+	break;
+      }
 
-      states[i]->nlookaheads = nlookaheads;
-      nLA += nlookaheads;
-    }
+  return nlookaheads;
 }
 
 
-/*--------------------------------------.
-| Initializing the lookaheads members.  |
-`--------------------------------------*/
+/*----------------------------------------------.
+| Compute LA, NLA, and the lookaheads members.  |
+`----------------------------------------------*/
 
 static void
-states_lookaheads_initialize (void)
+initialize_LA (void)
 {
   state_number_t i;
-  bitsetv pLA = LA;
-  rule_t **pLArule = LArule;
+  bitsetv pLA;
+
+  /* Compute the total number of reductions requiring a lookahead.  */
+  nLA = 0;
+  for (i = 0; i < nstates; i++)
+    nLA += state_lookaheads_count (states[i]);
+  /* Avoid having to special case 0.  */
+  if (!nLA)
+    nLA = 1;
 
-  /* Initialize the members LOOKAHEADS and LOOKAHEADS_RULE for each
-     state.  */
+  pLA = LA = bitsetv_create (nLA, ntokens, BITSET_FIXED);
+  lookback = XCALLOC (goto_list_t *, nLA);
+
+  /* Initialize the members LOOKAHEADS for each state which reductions
+     require lookaheads.  */
   for (i = 0; i < nstates; i++)
     {
-      states[i]->lookaheads = pLA;
-      states[i]->lookaheads_rule = pLArule;
-      pLA += states[i]->nlookaheads;
-      pLArule += states[i]->nlookaheads;
+      int count = state_lookaheads_count (states[i]);
+      if (count)
+	{
+	  states[i]->reductions->lookaheads = pLA;
+	  pLA += count;
+	}
     }
 }
 
@@ -428,18 +406,26 @@ lookaheads_print (FILE *out)
   fprintf (out, "Lookaheads: BEGIN\n");
   for (i = 0; i < nstates; ++i)
     {
+      reductions_t *reds = states[i]->reductions;
       bitset_iterator iter;
+      int nlookaheads = 0;
+
+      if (reds->lookaheads)
+	for (k = 0; k < reds->num; ++k)
+	  if (reds->lookaheads[k])
+	    ++nlookaheads;
 
       fprintf (out, "State %d: %d lookaheads\n",
-	       i, states[i]->nlookaheads);
+	       i, nlookaheads);
 
-      for (j = 0; j < states[i]->nlookaheads; ++j)
-	BITSET_FOR_EACH (iter, states[i]->lookaheads[j], k, 0)
-	{
-	  fprintf (out, "   on %d (%s) -> rule %d\n",
-		   k, symbols[k]->tag,
-		   states[i]->lookaheads_rule[j]->number);
-	};
+      if (reds->lookaheads)
+	for (j = 0; j < reds->num; ++j)
+	  BITSET_FOR_EACH (iter, reds->lookaheads[j], k, 0)
+	  {
+	    fprintf (out, "   on %d (%s) -> rule %d\n",
+		     k, symbols[k]->tag,
+		     reds->rules[j]->number);
+	  };
     }
   fprintf (out, "Lookaheads: END\n");
 }
@@ -447,15 +433,23 @@ lookaheads_print (FILE *out)
 void
 lalr (void)
 {
-  states_lookaheads_count ();
   initialize_LA ();
-  states_lookaheads_initialize ();
   set_goto_map ();
   initialize_F ();
   build_relations ();
   compute_FOLLOWS ();
   compute_lookaheads ();
 
-  if (trace_flag)
+  if (trace_flag & trace_sets)
     lookaheads_print (stderr);
 }
+
+
+void
+lalr_free (void)
+{
+  state_number_t s;
+  for (s = 0; s < nstates; ++s)
+    states[s]->reductions->lookaheads = NULL;
+  bitsetv_free (LA);
+}