def expand(node): if node.symbol[-1] == "?": if node.remainder: rule = node.remainder extra_child_node = DFSNode(rule[0], node.parent, rule[1:]) expand(extra_child_node) else: extra_path = reconstruct_path(node.parent) extra_phrase = ' '.join(extra_path) language.append(extra_phrase) node.symbol = node.symbol[:-1] if is_terminal(node.symbol) or node.symbol in _NO_EXPAND: if node.remainder: rule = node.remainder child_node = DFSNode(rule[0], node, rule[1:]) expand(child_node) else: path = reconstruct_path(node) phrase = ' '.join(path) language.append(phrase) else: children_nodes = [DFSNode(rule[0], node, rule[1:] + node.remainder) for rule in grammar[node.symbol]] for child_node in children_nodes: expand(child_node)
def expand(node): if node.symbol[-1] == "?": if node.remainder: rule = node.remainder extra_child_node = DFSNode(rule[0], node.parent, rule[1:]) expand(extra_child_node) else: extra_path = reconstruct_path(node.parent) extra_phrase = ' '.join(extra_path) language.append(extra_phrase) node.symbol = node.symbol[:-1] if is_terminal(node.symbol) or node.symbol in _NO_EXPAND: if node.remainder: rule = node.remainder child_node = DFSNode(rule[0], node, rule[1:]) expand(child_node) else: path = reconstruct_path(node) phrase = ' '.join(path) language.append(phrase) else: children_nodes = [ DFSNode(rule[0], node, rule[1:] + node.remainder) for rule in grammar[node.symbol] ] for child_node in children_nodes: expand(child_node)
def reconstruct_path(node): terminals = [] while node.parent: if is_terminal(node.symbol): terminals.append(node.symbol[1:-1]) elif node.symbol in _NO_EXPAND: terminals.append(node.symbol) node = node.parent terminals.reverse() return terminals