cex: isolate missing API from gl_list

Akim Demaille <[email protected]>
Newsgroups gmane.comp.parsers.bison.patches
Message-ID <[email protected]>

> Le 16 mai 2020 à 10:34, Akim Demaille <[email protected]> a écrit :
> 
> I'm also pushing more stylistic changes, as I try to understand the first crash (test 247).  There are many uninitialized variables.  We avoid that.  We actually even try to be "functional" and assign variables only once, when possible.

Another case.

commit 487c26e269e0a8cc7474db582c13621d369361f5
Author: Akim Demaille <[email protected]>
Date:   Sat May 16 10:41:06 2020 +0200

    cex: isolate missing API from gl_list
    
    * src/counterexample.c (list_get_end): New.
    Use it.
    Reduce scopes.

diff --git a/TODO b/TODO
index 006284eb..4265674e 100644
--- a/TODO
+++ b/TODO
@@ -1,4 +1,17 @@
 * Bison 3.7
+** Cex
+Don't do this (counterexample.c):
+
+// This is the fastest way to get the tail node from the gl_list API.
+gl_list_node_t
+list_get_end (gl_list_t list)
+{
+  gl_list_node_t sentinel = gl_list_add_last (list, NULL);
+  gl_list_node_t res = gl_list_previous_node (list, sentinel);
+  gl_list_remove_node (list, sentinel);
+  return res;
+}
+
 ** Bistromathic
 - Hitting tab on a line with a syntax error is ugly
 
diff --git a/src/counterexample.c b/src/counterexample.c
index 2a8fb276..ea7bd87b 100644
--- a/src/counterexample.c
+++ b/src/counterexample.c
@@ -58,6 +58,16 @@
 
 #define CUMULATIVE_TIME_LIMIT 120.0f
 
+// This is the fastest way to get the tail node from the gl_list API.
+gl_list_node_t
+list_get_end (gl_list_t list)
+{
+  gl_list_node_t sentinel = gl_list_add_last (list, NULL);
+  gl_list_node_t res = gl_list_previous_node (list, sentinel);
+  gl_list_remove_node (list, sentinel);
+  return res;
+}
+
 typedef struct
 {
   const derivation *d1;
@@ -266,19 +276,17 @@ complete_diverging_example (symbol_number conflict_sym,
       gl_list_add_last (result, derivation_dot ());
       lookahead_required = true;
     }
-  // This is the fastest way to get the tail node from the gl_list API
-  gl_list_node_t tmpd = gl_list_add_last (derivs, NULL);
-  gl_list_node_t tmps = gl_list_add_last (path, NULL);
-  gl_list_node_t deriv = gl_list_previous_node (derivs, tmpd);
-  gl_list_node_t state_node = gl_list_previous_node (path, tmps);
-  gl_list_remove_node (derivs, tmpd);
-  gl_list_remove_node (path, tmps);
-  // we go backwards through the path to create the derivation tree bottom-up.
+
+  gl_list_node_t deriv = list_get_end (derivs);
+
+  // We go backwards through the path to create the derivation tree bottom-up.
   // Effectively this loops through each production once, and generates a
   // derivation of the left hand side by appending all of the rhs symbols.
   // this becomes the derivation of the non-terminal after the dot in the
   // next production, and all of the other symbols of the rule are added as normal.
-  for (; state_node != NULL; state_node = gl_list_previous_node (path, state_node))
+  for (gl_list_node_t state_node = list_get_end (path);
+       state_node != NULL;
+       state_node = gl_list_previous_node (path, state_node))
     {
       state_item *si = (state_item *) gl_list_node_value (path, state_node);
       item_number *item = si->item;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.