From 939a32aab1a4731ad828510783ff868392cb4ffd Mon Sep 17 00:00:00 2001 From: Kia <kia@special-circumstanc.es> Date: Mon, 24 Aug 2020 16:48:14 -0600 Subject: [PATCH] add a quick-and-dirty special-casing for handling the start symbol in the derivation tree to avoid it spuriously appearing twice --- CFGBoltzmann.py | 18 ++++++++++++------ python_arborist.py | 4 ++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CFGBoltzmann.py b/CFGBoltzmann.py index c39feb3..d640950 100644 --- a/CFGBoltzmann.py +++ b/CFGBoltzmann.py @@ -360,10 +360,9 @@ class CFGBoltzmann: def Gzero_shimmed(self, nonterminal, requested_length): nonterminal_index = self.nonterminals_ordered.index(nonterminal) - root_of_tree = TreeNode(nonterminal,[]) - nonterminal_root = root_of_tree + root_of_tree = TreeNode(None,[]) - return (self.Gzero(nonterminal_index, requested_length, 0, root_of_tree, nonterminal_root), root_of_tree) + return (self.Gzero(nonterminal_index, requested_length, 0, root_of_tree, None), root_of_tree) @@ -382,10 +381,17 @@ class CFGBoltzmann: # 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: - newnode = TreeNode(self.nonterminals_ordered[nonterminal_index], []) + if (nonterminal_root == None): + root_of_tree.language_element = self.nonterminals_ordered[nonterminal_index] + nonterminal_root = root_of_tree + else: + newnode = TreeNode(self.nonterminals_ordered[nonterminal_index], []) + nonterminal_root.subnodes.append(newnode) + nonterminal_root = newnode + + + - nonterminal_root.subnodes.append(newnode) - nonterminal_root = newnode generated_string = self.Gprim(nonterminal_index, chosen_production, 0, requested_length, depth, root_of_tree, nonterminal_root) diff --git a/python_arborist.py b/python_arborist.py index 92554be..db3f369 100644 --- a/python_arborist.py +++ b/python_arborist.py @@ -316,7 +316,7 @@ list_of_terminals = [INTEGER, ADDOP, MULTOP, OPENPAREN, CLOSEPAREN] z = CFGBoltzmann.CFGBoltzmann(rules, list_of_nonterminals, list_of_terminals) cooked_rules = z.preprocessor() -bgen = z.Gzero_shimmed(EXPRESSION, 1) +bgen = z.Gzero_shimmed(EXPRESSION, 5) parse_me = bgen[0] @@ -431,7 +431,7 @@ def deserializer(serialized_array, last_idx_written): -the_tree = deserializer(serialized_tree_final, 8) +the_tree = deserializer(serialized_tree_final, 30) walk_the_tree(the_tree,0) -- GitLab