From a1a651e7cf678e6bb78d027e4fabf938dbdf40ae Mon Sep 17 00:00:00 2001 From: Kia <kia@special-circumstanc.es> Date: Wed, 19 Aug 2020 17:58:51 -0600 Subject: [PATCH] add code for working with trees --- CFGBoltzmann.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/CFGBoltzmann.py b/CFGBoltzmann.py index 8dab0fd..b05fef5 100644 --- a/CFGBoltzmann.py +++ b/CFGBoltzmann.py @@ -4,6 +4,26 @@ import random from enum import * + +class TreeNode: + def __init__(self, language_element, subnodes): + self.language_element = language_element + self.subnodes = subnodes + + def __str__(self): + return str(self.language_element) + " " + str(self.subnodes) + + +def walk_the_tree(tree, level = 1): + if tree == None: + return + + print(" " * (level -1) + "|---" + str(tree.language_element) + " with " + str(len(tree.subnodes)) + " subnodes") + for subnode in tree.subnodes: + walk_the_tree(subnode, level + 1) + + + CFG = collections.namedtuple("CFG", "nonterminals terminals rules start") # We define a context-free grammar as: # 1. A list of nonterminal symbols -- GitLab