bison-patches
[Top][All Lists]
Advanced

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

lalr: don't overbook memory


From: Akim Demaille
Subject: lalr: don't overbook memory
Date: Sun, 31 Mar 2019 13:58:33 +0200

commit 771a46c25537dc6ee965e79728662a6823851943
Author: Akim Demaille <address@hidden>
Date:   Sun Mar 31 09:23:39 2019 +0200

    lalr: don't overbook memory
    
    I never understood why we book ngotos+1 slots for relations between
    gotos: there are at most ngotos images, not ngotos+1 (and "includes"
    does have cases where a goto is in relation with itself, so it's not
    ngotos-1).
    
    Maybe bbf37f2534a8e5a6b4e28047f0a10903e6dc73f9 explains the +1: a bug
    left us register a goto several times on occasion, and the +1 might
    have been a means to avoid this problem in most cases.  Now that this
    bug is addressed, we should no longer overbook memory, if only for the
    clarity of the code ("why ngotos+1 instead of ngotos?").
    
    * src/lalr.c: A goto has at most ngotos images, not ngotos+1.
    While at it, avoid useless repeated call to map_goto introduced in
    bbf37f2534a8e5a6b4e28047f0a10903e6dc73f9.

diff --git a/src/lalr.c b/src/lalr.c
index c48c726d..2717190c 100644
--- a/src/lalr.c
+++ b/src/lalr.c
@@ -188,7 +188,7 @@ static void
 initialize_goto_follows (void)
 {
   goto_number **reads = xnmalloc (ngotos, sizeof *reads);
-  goto_number *edge = xnmalloc (ngotos + 1, sizeof *edge);
+  goto_number *edge = xnmalloc (ngotos, sizeof *edge);
   goto_number nedges = 0;
 
   goto_follows = bitsetv_create (ngotos, ntokens, BITSET_FIXED);
@@ -207,7 +207,7 @@ initialize_goto_follows (void)
           symbol_number sym = TRANSITION_SYMBOL (trans, j);
           if (nullable[sym - ntokens])
             {
-              assert (nedges < ngotos + 1);
+              assert (nedges < ngotos);
               edge[nedges++] = map_goto (dst, sym);
             }
         }
@@ -253,7 +253,7 @@ add_lookback_edge (state *s, rule const *r, goto_number 
gotono)
 static void
 build_relations (void)
 {
-  goto_number *edge = xnmalloc (ngotos + 1, sizeof *edge);
+  goto_number *edge = xnmalloc (ngotos, sizeof *edge);
   state_number *path = xnmalloc (ritem_longest_rhs () + 1, sizeof *path);
 
   includes = xnmalloc (ngotos, sizeof *includes);
@@ -304,8 +304,8 @@ build_relations (void)
                   found = edge[j] == g;
                 if (!found)
                   {
-                    assert (nedges < ngotos + 1);
-                    edge[nedges++] = map_goto (path[p], sym);
+                    assert (nedges < ngotos);
+                    edge[nedges++] = g;
                   }
               }
               if (!nullable[sym - ntokens])




reply via email to

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