diff --git a/CFGBoltzmann.py b/CFGBoltzmann.py index fefd67097483002afdeaf89be21813243e38baf6..a9ffd3483eebd54b4136aefeb81f49aaace257dc 100644 --- a/CFGBoltzmann.py +++ b/CFGBoltzmann.py @@ -361,42 +361,27 @@ class CFGBoltzmann: def Gzero_shimmed(self, nonterminal, requested_length): nonterminal_index = self.nonterminals_ordered.index(nonterminal) root_of_tree = TreeNode(nonterminal,[]) - operation_site_index = [] + nonterminal_root = root_of_tree - return (self.Gzero(nonterminal_index, requested_length, 0, root_of_tree, operation_site_index), root_of_tree) + return (self.Gzero(nonterminal_index, requested_length, 0, root_of_tree, nonterminal_root), root_of_tree) - def Gzero(self, nonterminal_index, requested_length, depth, root_of_tree, operation_site_index): + def Gzero(self, nonterminal_index, requested_length, depth, root_of_tree, nonterminal_root): 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) + print(" "* depth +"Gzero entering, with nonterminal", nonterminal_index, "and production rule number", chosen_production, "depth =",depth) # 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) - newnode = TreeNode(self.nonterminals_ordered[nonterminal_index], []) - opsite.subnodes.append(newnode) - operation_site_index.append(len(opsite.subnodes) - 1) - - print("OSID", operation_site_index) + nonterminal_root.subnodes.append(newnode) + nonterminal_root = newnode - generated_string = self.Gprim(nonterminal_index, chosen_production, 0, requested_length, depth, root_of_tree, operation_site_index) + generated_string = self.Gprim(nonterminal_index, chosen_production, 0, requested_length, depth, root_of_tree, nonterminal_root) return generated_string - def indexes_to_item(self, 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). @@ -433,7 +418,7 @@ class CFGBoltzmann: # \----------------------------------------------------------/ - def Gprim(self, nonterminal_index, chosen_production, how_far_into_the_RHS, exact_length_total, depth, root_of_tree, operation_site_index): + def Gprim(self, nonterminal_index, chosen_production, how_far_into_the_RHS, exact_length_total, depth, root_of_tree, nonterminal_root): #print(" "* depth + "GPRIM arguments are", nonterminal_index, chosen_production, how_far_into_the_RHS, exact_length_total) @@ -443,8 +428,8 @@ class CFGBoltzmann: # The case analysis hinges on what is at X_ijk. RHS_in_question = self.processed_rulepack[nonterminal_index][1][chosen_production] - print(" "* depth +"Our RHS is", RHS_in_question) - print(" "* depth +"WE ARE PROCESSING index", how_far_into_the_RHS, "and our remaining length is", exact_length_total) + #print(" "* depth +"Our RHS is", RHS_in_question) + #print(" "* depth +"WE ARE PROCESSING index", how_far_into_the_RHS, "and our remaining length is", exact_length_total) xijk = RHS_in_question[how_far_into_the_RHS] @@ -455,28 +440,23 @@ class CFGBoltzmann: # are we at the end of the rule if (how_far_into_the_RHS == (len(RHS_in_question) - 1)): # CASE A - print(" "* depth +"GPRIM CASE A RETURNING", [xijk]) - print(" "* depth +"Gprim ending the production of a nonterminal, with terminal", xijk) - opsite = self.indexes_to_item(root_of_tree, operation_site_index) + print(" "* depth +"GPRIM CASE A PRODUCES", [xijk], "with depth", depth) + #print(" "* depth +"Gprim ending the production of a nonterminal, with terminal", xijk) newnode = TreeNode(xijk, []) - opsite.subnodes.append(newnode) - - print("OSID", operation_site_index) + nonterminal_root.subnodes.append(newnode) return [xijk] else: # CASE B - print(" "* depth +"GPRIM CASE B pointing @", [xijk]) - reduct = self.Gprim(nonterminal_index, chosen_production, how_far_into_the_RHS + 1, exact_length_total - 1, depth+1, root_of_tree, operation_site_index) + print(" "* depth +"GPRIM CASE B FIRST PRODUCES", [xijk], "with depth", depth) + reduct = self.Gprim(nonterminal_index, chosen_production, how_far_into_the_RHS + 1, exact_length_total - 1, depth+1, root_of_tree, nonterminal_root) retstring = [xijk] + reduct - opsite = self.indexes_to_item(root_of_tree, operation_site_index) - + print(" "* depth +"GPRIM CASE B THEN ADDS ON", reduct, "with depth", depth) newnode = TreeNode(xijk, []) - opsite.subnodes.append(newnode) - print("OSID", operation_site_index) + nonterminal_root.subnodes.append(newnode) print(" "* depth +"Gprim inside the production of a nonterminal, adding terminal", xijk) @@ -496,7 +476,7 @@ class CFGBoltzmann: if (how_far_into_the_RHS == (len(RHS_in_question) - 1)): # CASE C # print(" "* depth +"CASE C STARTING") - retstring = self.Gzero(new_nonterminal_index, exact_length_total, depth+1, root_of_tree, operation_site_index) + retstring = self.Gzero(new_nonterminal_index, exact_length_total, depth+1, root_of_tree, nonterminal_root) # print(" "* depth +"CASE C returning", retstring) return retstring @@ -514,12 +494,12 @@ class CFGBoltzmann: # hit the nonterminal X_ijk with Gzero - nonterminal_generates = self.Gzero(new_nonterminal_index, split_choice, depth+1, root_of_tree, operation_site_index) + nonterminal_generates = self.Gzero(new_nonterminal_index, split_choice, depth+1, root_of_tree, nonterminal_root) # print (" "* depth +"CASE D NONTERMINAL @ XIJK generates", nonterminal_generates) # and then the remaining part of the rule - rest_of_rule_generates = self.Gprim(nonterminal_index, chosen_production, how_far_into_the_RHS + 1, exact_length_total - split_choice, depth+1, root_of_tree, operation_site_index) + rest_of_rule_generates = self.Gprim(nonterminal_index, chosen_production, how_far_into_the_RHS + 1, exact_length_total - split_choice, depth+1, root_of_tree, nonterminal_root) # print ("CASE D REST OF RULE generates", rest_of_rule_generates) retstring = nonterminal_generates + rest_of_rule_generates @@ -645,11 +625,11 @@ class CFGBoltzmann: -z = CFGBoltzmann(rules, list_of_nonterminals, list_of_terminals) +#z = CFGBoltzmann(rules, list_of_nonterminals, list_of_terminals) -rulepack_cooked = z.preprocessor() +#rulepack_cooked = z.preprocessor() -qq = z.Gzero_shimmed(nonterminals.EXPRESSION, 3) -print ("AND THE FINAL ANSWER IS","\n\n\n",qq, "\n\n\n") +#qq = z.Gzero_shimmed(nonterminals.EXPRESSION, 3) +#print ("AND THE FINAL ANSWER IS","\n\n\n",qq, "\n\n\n")