From 199b3fe6318a218856cdba211b2aa6cae3aed194 Mon Sep 17 00:00:00 2001
From: Kia <kia@special-circumstanc.es>
Date: Thu, 6 Aug 2020 14:37:32 -0600
Subject: [PATCH] sorta working

---
 cfg_utils.py | 40 +++++++++++++++++++++++++++++-----------
 1 file changed, 29 insertions(+), 11 deletions(-)

diff --git a/cfg_utils.py b/cfg_utils.py
index 0bb5b50..49e8093 100644
--- a/cfg_utils.py
+++ b/cfg_utils.py
@@ -102,7 +102,7 @@ def indexes_to_item(tree, location):
 
 def random_leaf_random_rule(tree, leafcache, ruleset):
 
-    while True:
+    for x in range(256):
         proband_cache_index = random.randrange(len(leafcache))
         location = leafcache[proband_cache_index]
 
@@ -112,12 +112,16 @@ def random_leaf_random_rule(tree, leafcache, ruleset):
         rule = ruleset[proband_rule_index]
 
         if (opsite.language_element == rule[0]):
-            break
+            return (proband_cache_index, proband_rule_index)
+
+
+    # we failed somehow, just return None
+    return None
+
 
 
 
 
-    return (proband_cache_index, proband_rule_index)
 
 def apply_rule(tree, leafcache, cache_index, ruleset, rule_number):
     # first, we isolate the place on the tree that we are going to do the graft.
@@ -180,17 +184,31 @@ walk_the_tree(test_tree)
 
 leafcache = [[]]
 
+def the_tree_farm():
+    for x in range(10):
+        selected = random_leaf_random_rule(test_tree, leafcache, rules)
+        if (selected == None):
+            # we have achieved what we need! all leaves are terminal items
+            return test_tree
+        else:
+            (next_index_cache, next_rule) = selected
+
+        print ("next cache index is", next_index_cache, "next rule is", next_rule)
+        apply_rule(test_tree, leafcache, next_index_cache, rules, next_rule)
+
+        print("and the tree looks like")
+        walk_the_tree(test_tree)
 
-for x in range(10):
-    (next_index_cache, next_rule) = random_leaf_random_rule(test_tree, leafcache, rules)
-    print ("next cache index is", next_index_cache, "next rule is", next_rule)
-    apply_rule(test_tree, leafcache, next_index_cache, rules, next_rule)
+        print("updated leafcache")
+        print(leafcache)
 
-    print("and the tree looks like")
-    walk_the_tree(test_tree)
+while True:
+    produced_tree = the_tree_farm()
+    if(produced_tree != None):
+        print("SUCCESS!!!!")
+        walk_the_tree(produced_tree)
+        break
 
-    print("updated leafcache")
-    print(leafcache)
 
 # Furthermore, we also note that the description of a context-free grammar is *itself* context-free
 # so if we take the CFG-description grammar (BNF or something isomorphic to it), and use Boltzmann
-- 
GitLab