Exemplo n.º 1
0
 def initialize_trees(self, batchsize):
     asts = [
         NonterminalNode(self.grammar.start_symbol, parent=None)
         for i in range(batchsize)
     ]  # partial results
     frontiers = [[asts[i]] for i in range(batchsize)
                  ]  # the stacks for DFS, whose top are the next nodes
     return asts, frontiers
Exemplo n.º 2
0
        def recursive_duplicate(node, parent=None):
            if isinstance(node, Token):
                new_node = deepcopy(node)
                old2new[node] = new_node
                return new_node
            elif isinstance(node, TerminalNode):
                new_node = TerminalNode(node.symbol, parent)
                new_node.token = node.token
            else:
                assert isinstance(node, NonterminalNode)
                new_node = NonterminalNode(node.symbol, parent)

            old2new[node] = new_node
            new_node.action = node.action
            if node.pred is None:
                new_node.pred = None
            else:
                new_node.pred = old2new[node.pred]
            new_node.state = node.state
            if isinstance(node, NonterminalNode):
                for c in node.children:
                    new_node.children.append(recursive_duplicate(c, new_node))
            return new_node