From cce90d5b9ab6cd12dd8b145d61ef22a4ae89464c Mon Sep 17 00:00:00 2001 From: Kia <kia@special-circumstanc.es> Date: Fri, 21 Aug 2020 19:09:01 -0600 Subject: [PATCH] renames and add utility function for tree index dereferencing --- CFGBoltzmann.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/CFGBoltzmann.py b/CFGBoltzmann.py index 8821033..f4df62d 100644 --- a/CFGBoltzmann.py +++ b/CFGBoltzmann.py @@ -361,18 +361,36 @@ class CFGBoltzmann: def Gzero_shimmed(self, nonterminal, requested_length): nonterminal_index = self.nonterminals_ordered.index(nonterminal) root_of_tree = TreeNode("START",[]) - operation_site = [] + operation_site_index = [] - return (self.Gzero(nonterminal_index, requested_length, 0, root_of_tree, operation_site), root_of_tree) + return (self.Gzero(nonterminal_index, requested_length, 0, root_of_tree, operation_site_index), root_of_tree) - def Gzero(self, nonterminal_index, requested_length, depth, root_of_tree, operation_site): + def Gzero(self, nonterminal_index, requested_length, depth, root_of_tree, operation_site_index): possibilities = self.Fzero(nonterminal_index, requested_length) chosen_production = self.normalized_choice(possibilities) print(" "* depth +"Gzero entering, with nonterminal", nonterminal_index, "and production rule number", chosen_production) + # We already know we will have this nonterminal show up in our derivation tree; so we add it now + # First, dereference the location to get the operation site: + + opsite = self.indexes_to_item(root_of_tree, operation_site_index) + generated_string = self.Gprim(nonterminal_index, chosen_production, depth, requested_length, depth) return generated_string + + def indexes_to_item(tree, location): + opsite = tree + if (location != []): + for depth_index in location: + opsite = opsite.subnodes[depth_index] + #print("TMP index ", depth_index, " gives ",opsite) + else: + opsite = tree + + return opsite + + # Like Fprim, Gprim takes a nonterminal index, a production index for the nonterminal, # and an index into the production rule (and of course, a requested length). # This lets us walk through the production rule symbol by symbol. -- GitLab