From 18831f985c76bd141189e34e7410a5ac508300b1 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sun, 31 Mar 2019 09:23:39 +0200 Subject: [PATCH] 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. --- src/lalr.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lalr.c b/src/lalr.c index e749fcc6..70cf33a9 100644 --- a/src/lalr.c +++ b/src/lalr.c @@ -192,7 +192,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); @@ -211,7 +211,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); } } @@ -309,7 +309,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); @@ -361,8 +361,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])