def randomTree(lexicon): if isinstance(lexicon, str): return Node(lexicon) rule = random.choice(lexicon.getRules()) n = Node(lexicon.name) n.children = [randomTree(l) for l in rule.body] return n
def parsing_table(self): rules = self.parsing_production_string() if len(rules) == 0: self.root = Node("empty") else: rule_index = 0 self.root = Node(rules[0][0]) self.parsing_table_rec(self.root, rules, rule_index)
def mostrarArvore(self): # Montando os segmentos para utilizar o PPTREEE nos = [] nos.append(Node('0')) for key in self.arvore.keys(): if(key != 0): nos.append(Node(str(key), nos[self.arvore[key][0]])) print('\r\nArvore de segmentos do job ' + str(self.nome)) print_tree(nos[0])
def traverse(stem, grp): for key in list(grp): if isinstance(grp[key], h5py.Group): thisnode = Node(key, stem) traverse(thisnode, grp[key]) elif isinstance(grp[key], h5py.Dataset): thisnode = Node(f"{key} {str(grp[key].shape)}", stem)
def make_bsp_rooms(width, height): head = Node(Area(0, 0, width, height, "vert")) split_area(head, 0) rooms = get_leaves(head) rooms = list(map(lambda r: r.name, rooms)) return rooms
def split_area(a, cur_depth, max_depth=7, min_size=26): p = a.name # p == parent if randint(0, 5) == 0 and cur_depth > 3: return None if cur_depth > max_depth or p.w < min_size or p.h < min_size: return None else: if p.split_mode == None or p.split_mode == "horiz": a1, a2 = split_vert(p, min_size) else: a1, a2 = split_horiz(p, min_size) c1 = Node(a1, a) c2 = Node(a2, a) split_area(c1, cur_depth + 1) split_area(c2, cur_depth + 1)
def node( self, name, pnode = None, claim = True ): self.assert_is_node( pnode ) if claim and name in self.ifcfgs: self.set_used( name ) self.assert_is_node( pnode ) return Node( name, pnode, )
def parsing_table_rec(self, father, rules, rule_index): if rule_index == len(rules): return len(rules) prod = rules[rule_index][1] for term in prod: sibling = Node(name=term, parent=father) if term in self.grammar.nonterminals: rule_index = self.parsing_table_rec(sibling, rules, rule_index + 1) return rule_index
def toNode(self): node = Node(self.rule.head.name) itChildState = iter(self.children) for lexicon in self.rule.body: if isinstance( lexicon, Lexicon ): # if the next element in the rule is a lexicon state = next(itChildState) node.children.append( state.toNode() ) # pop out a state from the children, and get node from it elif isinstance( lexicon, str): # if the next element in the rule is a string node.children.append( Node(lexicon)) # create a node with that string else: raise Exception("Unknown element in rule: {}".format( repr(lexicon))) return node
def main(): if len(sys.argv[1:]) == 0: args = sys.stdin else: args = sys.argv[1:] for line in args: line = line.strip() root = Node(line) file = h5py.File(line, "r") traverse(root, file) print_tree(root)
def gen_pptree(self, parent): Node("if", parent) self.expr.gen_pptree(Node("<bool_expr>", parent)) Node("then", parent) self.blk.gen_pptree(Node("<expr>", parent)) Node("else", parent) self.blk2.gen_pptree(Node("<expr>", parent))
def build_tree(node, parent): # sanitize and prep the data using sensible defaults if necessary if 'function_name' in node: func_name = node['function_name'] else: func_name = "<module>" if 'arguments' in node: args = node['arguments'] else: args = {} if 'return' in node: ret = node['return'] else: ret = "None" # create a pptree Node for the current node in the call tree root = Node(build_string(func_name, args, ret), parent) # recursively add Nodes for the children of the # current node in the call tree for next in node['seq']: build_tree(next, root)
def gen_pptree(self, parent): Node("id", parent) Node("=", parent) self.expr.gen_pptree(Node("<expr>", parent))
def gen_pptree(self, parent): op = ('==', '!=', '>', '>=', '<', '<=') self.expr1.gen_pptree(Node("<expr>", parent)) Node(op[self.operator], parent) self.expr2.gen_pptree(Node("<expr>", parent))
def gen_pptree(self, parent): op = ('+', '-', '*', '/') self.expr1.gen_pptree(Node("<expr>", parent)) Node(op[self.op], parent) self.expr1.gen_pptree(Node("<expr>", parent))
def gen_pptree(self, parent): Node("lit_int", parent)
def gen_pptree(self, parent): Node("id", parent)
def gen_pptree(self, parent): for stmt in self.stmts: stmt.gen_pptree(Node("<stmt>", parent))
def gen_pptree(self, parent): Node("repeat", parent) self.blk.gen_pptree(Node("<block>", parent)) Node("until", parent) self.expr.gen_pptree(Node("<bool_expr>", parent))
def gen_pptree(self, parent): Node("print", parent) Node("(", parent) self.expr.gen_pptree(Node("<expr>", parent)) Node(")", parent)
def visualize(d): root = Node("root") build_tree(d, root) print_tree(root)