bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

12-state-lookaheads.patch


From: Akim Demaille
Subject: 12-state-lookaheads.patch
Date: Sat, 15 Jun 2002 20:19:42 +0200

Index: ChangeLog
from  Akim Demaille  <address@hidden>
        * src/state.h (state_t): Replace the `lookaheadsp' member, a
        short, with `lookaheads' (bitsetv), `lookaheads_rule' (rule_t**).
        Adjust all dependencies.
        * src/lalr.c (initialize_lookaheads): Split into...
        (states_lookaheads_count, states_lookaheads_initialize): these.
        (lalr): Adjust.
        
        
Index: src/conflicts.c
--- src/conflicts.c Tue, 11 Jun 2002 19:33:39 +0200 akim
+++ src/conflicts.c Sat, 15 Jun 2002 18:10:57 +0200 akim
@@ -256,11 +256,11 @@
      check for shift-reduce conflict, and try to resolve using
      precedence */
   for (i = 0; i < state->nlookaheads; ++i)
-    if (LArule[state->lookaheadsp + i]->prec
-       && LArule[state->lookaheadsp + i]->prec->prec
-       && !bitset_disjoint_p (LA[state->lookaheadsp + i], lookaheadset))
+    if (state->lookaheads_rule[i]->prec
+       && state->lookaheads_rule[i]->prec->prec
+       && !bitset_disjoint_p (state->lookaheads[i], lookaheadset))
       {
-       resolve_sr_conflict (state, state->lookaheadsp + i);
+       resolve_sr_conflict (state, (state->lookaheads - LA) + i);
        break;
       }
 
@@ -268,10 +268,10 @@
      for conflicts not resolved above.  */
   for (i = 0; i < state->nlookaheads; ++i)
     {
-      if (!bitset_disjoint_p (LA[state->lookaheadsp + i], lookaheadset))
+      if (!bitset_disjoint_p (state->lookaheads[i], lookaheadset))
        conflicts[state->number] = 1;
 
-      bitset_or (lookaheadset, lookaheadset, LA[state->lookaheadsp + i]);
+      bitset_or (lookaheadset, lookaheadset, state->lookaheads[i]);
     }
 }
 
@@ -312,7 +312,7 @@
       bitset_set (shiftset, SHIFT_SYMBOL (shiftp, i));
 
   for (i = 0; i < state->nlookaheads; ++i)
-    bitset_or (lookaheadset, lookaheadset, LA[state->lookaheadsp + i]);
+    bitset_or (lookaheadset, lookaheadset, state->lookaheads[i]);
 
   bitset_and (lookaheadset, lookaheadset, shiftset);
 
@@ -340,7 +340,7 @@
       int count = 0;
       int j;
       for (j = 0; j < state->nlookaheads; ++j)
-       if (bitset_test (LA[state->lookaheadsp + j], i))
+       if (bitset_test (state->lookaheads[j], i))
          count++;
 
       if (count >= 2)
Index: src/lalr.c
--- src/lalr.c Sat, 15 Jun 2002 15:39:16 +0200 akim
+++ src/lalr.c Sat, 15 Jun 2002 18:09:02 +0200 akim
@@ -298,15 +298,15 @@
   shorts *sp;
 
   for (i = 0; i < state->nlookaheads; ++i)
-    if (LArule[state->lookaheadsp + i]->number == ruleno)
+    if (state->lookaheads_rule[i]->number == ruleno)
       break;
 
-  assert (LArule[state->lookaheadsp + i]->number == ruleno);
+  assert (state->lookaheads_rule[i]->number == ruleno);
 
   sp = XCALLOC (shorts, 1);
-  sp->next = lookback[state->lookaheadsp + i];
+  sp->next = lookback[(state->lookaheads - LA) + i];
   sp->value = gotono;
-  lookback[state->lookaheadsp + i] = sp;
+  lookback[(state->lookaheads - LA) + i] = sp;
 }
 
 
@@ -506,15 +506,18 @@
 }
 
 
-/*--------------------------------------.
-| Initializing the lookaheads members.  |
-`--------------------------------------*/
+/*-------------------------------------------------------------.
+| Count the number of lookaheads required for each state       |
+| (NLOOKAHEADS member).  Compute the total number of LA, NLA.  |
+`-------------------------------------------------------------*/
 
 static void
-initialize_lookaheads (void)
+states_lookaheads_count (void)
 {
   size_t i;
   nLA = 0;
+
+  /* Count   */
   for (i = 0; i < nstates; i++)
     {
       int k;
@@ -540,12 +543,34 @@
          }
 
       states[i]->nlookaheads = nlookaheads;
-      states[i]->lookaheadsp = nLA;
       nLA += nlookaheads;
     }
 }
 
 
+/*--------------------------------------.
+| Initializing the lookaheads members.  |
+`--------------------------------------*/
+
+static void
+states_lookaheads_initialize (void)
+{
+  size_t i;
+  bitsetv pLA = LA;
+  rule_t **pLArule = LArule;
+
+  /* Count the number of lookaheads required for each state
+     (NLOOKAHEADS member).  */
+  for (i = 0; i < nstates; i++)
+    {
+      states[i]->lookaheads = pLA;
+      states[i]->lookaheads_rule = pLArule;
+      pLA += states[i]->nlookaheads;
+      pLArule += states[i]->nlookaheads;
+    }
+}
+
+
 /*---------------------------------------.
 | Output the lookaheads for each state.  |
 `---------------------------------------*/
@@ -563,10 +588,10 @@
 
       for (j = 0; j < states[i]->nlookaheads; ++j)
        for (k = 0; k < ntokens; ++k)
-         if (bitset_test (LA[states[i]->lookaheadsp + j], k))
+         if (bitset_test (states[i]->lookaheads[j], k))
            fprintf (out, "   on %d (%s) -> rule %d\n",
                     k, symbol_tag_get (symbols[k]),
-                    LArule[states[i]->lookaheadsp + j]->number - 1);
+                    states[i]->lookaheads_rule[j]->number - 1);
     }
   fprintf (out, "Lookaheads: END\n");
 }
@@ -574,8 +599,9 @@
 void
 lalr (void)
 {
-  initialize_lookaheads ();
+  states_lookaheads_count ();
   initialize_LA ();
+  states_lookaheads_initialize ();
   set_goto_map ();
   initialize_F ();
   build_relations ();
Index: src/output.c
--- src/output.c Sat, 15 Jun 2002 15:39:16 +0200 akim
+++ src/output.c Sat, 15 Jun 2002 17:52:39 +0200 akim
@@ -380,8 +380,8 @@
        for (j = 0; j < ntokens; j++)
          /* and record this rule as the rule to use if that
             token follows.  */
-         if (bitset_test (LA[state->lookaheadsp + i], j))
-           actrow[j] = -LArule[state->lookaheadsp + i]->number;
+         if (bitset_test (state->lookaheads[i], j))
+           actrow[j] = -state->lookaheads_rule[i]->number;
     }
 
   /* Now see which tokens are allowed for shifts in this state.  For
@@ -428,7 +428,7 @@
          for (i = 0; i < state->nlookaheads; i++)
            {
              int count = 0;
-             int rule = -LArule[state->lookaheadsp + i]->number;
+             int rule = -state->lookaheads_rule[i]->number;
              int j;
 
              for (j = 0; j < ntokens; j++)
Index: src/print.c
--- src/print.c Sat, 15 Jun 2002 16:36:49 +0200 akim
+++ src/print.c Sat, 15 Jun 2002 17:57:10 +0200 akim
@@ -99,16 +99,16 @@
              /* Look for lookaheads corresponding to this rule. */
              for (j = 0; j < state->nlookaheads; ++j)
                for (k = 0; k < ntokens; ++k)
-                 if (bitset_test (LA[state->lookaheadsp + j], k)
-                     && LArule[state->lookaheadsp + j]->number == rule)
+                 if (bitset_test (state->lookaheads[j], k)
+                     && state->lookaheads_rule[j]->number == rule)
                    nlookaheads++;
              if (nlookaheads)
                {
                  fprintf (out, "  [");
                  for (j = 0; j < state->nlookaheads; ++j)
                    for (k = 0; k < ntokens; ++k)
-                     if (bitset_test (LA[state->lookaheadsp + j], k)
-                         && LArule[state->lookaheadsp + j]->number == rule)
+                     if (bitset_test (state->lookaheads[j], k)
+                         && state->lookaheads_rule[j]->number == rule)
                        fprintf (out, "%s%s",
                                 symbol_tag_get (symbols[k]),
                                 --nlookaheads ? ", " : "");
@@ -225,9 +225,9 @@
 
   if (state->nlookaheads == 1 && !nodefault)
     {
-      rule_t *default_rule = LArule[state->lookaheadsp];
+      rule_t *default_rule = state->lookaheads_rule[0];
 
-      bitset_and (lookaheadset, LA[state->lookaheadsp], shiftset);
+      bitset_and (lookaheadset, state->lookaheads[0], shiftset);
 
       for (i = 0; i < ntokens; i++)
        if (bitset_test (lookaheadset, i))
@@ -252,7 +252,7 @@
            int count = 0;
            int j;
 
-           bitset_andn (lookaheadset, LA[state->lookaheadsp + i], shiftset);
+           bitset_andn (lookaheadset, state->lookaheads[i], shiftset);
 
            for (j = 0; j < ntokens; j++)
              if (bitset_test (lookaheadset, j))
@@ -261,8 +261,8 @@
            if (count > cmax)
              {
                cmax = count;
-               default_LA = state->lookaheadsp + i;
-               default_rule = LArule[state->lookaheadsp + i];
+               default_LA = i;
+               default_rule = state->lookaheads_rule[i];
              }
 
            bitset_or (shiftset, shiftset, lookaheadset);
@@ -281,16 +281,16 @@
          int count = bitset_test (shiftset, i);
 
          for (j = 0; j < state->nlookaheads; ++j)
-           if (bitset_test (LA[state->lookaheadsp + j], i))
+           if (bitset_test (state->lookaheads[j], i))
              {
                if (count == 0)
                  {
-                   if (state->lookaheadsp + j != default_LA)
+                   if (j != default_LA)
                      fprintf (out,
                               _("    %-4s\treduce using rule %d (%s)\n"),
                               symbol_tag_get (symbols[i]),
-                              LArule[state->lookaheadsp + j]->number - 1,
-                              symbol_tag_get_n (LArule[state->lookaheadsp + 
j]->lhs, 1));
+                              state->lookaheads_rule[j]->number - 1,
+                              symbol_tag_get_n 
(state->lookaheads_rule[j]->lhs, 1));
                    else
                      defaulted = 1;
 
@@ -302,14 +302,14 @@
                      fprintf (out,
                               _("    %-4s\treduce using rule %d (%s)\n"),
                               symbol_tag_get (symbols[i]),
-                              LArule[default_LA]->number - 1,
-                              symbol_tag_get_n (LArule[default_LA]->lhs, 1));
+                              state->lookaheads_rule[default_LA]->number - 1,
+                              symbol_tag_get_n 
(state->lookaheads_rule[default_LA]->lhs, 1));
                    defaulted = 0;
                    fprintf (out,
                             _("    %-4s\t[reduce using rule %d (%s)]\n"),
                             symbol_tag_get (symbols[i]),
-                            LArule[state->lookaheadsp + j]->number - 1,
-                            symbol_tag_get_n (LArule[state->lookaheadsp + 
j]->lhs, 1));
+                            state->lookaheads_rule[j]->number - 1,
+                            symbol_tag_get_n (state->lookaheads_rule[j]->lhs, 
1));
                  }
              }
        }
Index: src/print_graph.c
--- src/print_graph.c Sat, 15 Jun 2002 15:39:16 +0200 akim
+++ src/print_graph.c Sat, 15 Jun 2002 17:57:27 +0200 akim
@@ -93,16 +93,16 @@
          /* Look for lookaheads corresponding to this rule. */
          for (j = 0; j < state->nlookaheads; ++j)
            for (k = 0; k < ntokens; ++k)
-             if (bitset_test (LA[state->lookaheadsp + j], k)
-                 && LArule[state->lookaheadsp + j]->number == rule)
+             if (bitset_test (state->lookaheads[j], k)
+                 && state->lookaheads_rule[j]->number == rule)
                nlookaheads++;
          if (nlookaheads)
            {
              obstack_sgrow (oout, "  [");
              for (j = 0; j < state->nlookaheads; ++j)
                for (k = 0; k < ntokens; ++k)
-                 if (bitset_test (LA[state->lookaheadsp + j], k)
-                     && LArule[state->lookaheadsp + j]->number == rule)
+                 if (bitset_test (state->lookaheads[j], k)
+                     && state->lookaheads_rule[j]->number == rule)
                    obstack_fgrow2 (oout, "%s%s",
                                    symbol_tag_get (symbols[k]),
                                    --nlookaheads ? ", " : "");
Index: src/state.h
--- src/state.h Thu, 06 Jun 2002 20:45:16 +0200 akim
+++ src/state.h Sat, 15 Jun 2002 17:49:16 +0200 akim
@@ -88,6 +88,7 @@
 #ifndef STATE_H_
 # define STATE_H_
 
+# include "bitsetv.h"
 
 /*---------.
 | Shifts.  |
@@ -180,9 +181,9 @@
   char consistent;
 
   /* Used in LALR, not LR(0). */
-  /* Pseudo pointer into LA. */
-  short lookaheadsp;
   int nlookaheads;
+  bitsetv lookaheads;
+  rule_t **lookaheads_rule;
 
   /* If some conflicts were solved thanks to precedence/associativity,
      a human readable description of the resolution.  */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]