def get_node_type_id(self, node): if isinstance(node, ASTNode): type_repr = typename(node.type) return self.node_type_to_id[type_repr] else: # assert isinstance(node, str) # it is a type type_repr = typename(node) return self.node_type_to_id[type_repr]
def pretty_print_helper(self, sb, depth, new_line=False): if new_line: sb.write('\n') for i in range(depth): sb.write(' ') sb.write('(') sb.write(typename(self.type)) if self.label is not None: sb.write('{%s}' % self.label) if self.value is not None: sb.write('{val=%s}' % self.value) if len(self.children) == 0: sb.write(')') return sb.write(' ') new_line = True for child in self.children: child.pretty_print_helper(sb, depth + 2, new_line) sb.write('\n') for i in range(depth): sb.write(' ') sb.write(')')
def __repr__(self): parent = typename(self.type) if self.label is not None: parent += '{%s}' % self.label if self.value is not None: parent += '{val=%s}' % self.value return '%s -> %s' % (parent, ', '.join( [repr(c) for c in self.children]))
def __init__(self, rules): """ instantiate a grammar with a set of production rules of type Rule """ self.rules = rules self.rule_index = defaultdict(list) self.rule_to_id = OrderedDict() node_types = set() lhs_nodes = set() rhs_nodes = set() for rule in self.rules: self.rule_index[rule.parent].append(rule) # we also store all unique node types for node in rule.nodes: node_types.add(typename(node.type)) lhs_nodes.add(rule.parent) for child in rule.children: rhs_nodes.add(child.as_type_node) root_node = lhs_nodes - rhs_nodes try: assert len(root_node) == 1 except AssertionError: print(root_node) raise AssertionError self.root_node = next(iter(root_node)) self.terminal_nodes = rhs_nodes - lhs_nodes self.terminal_types = set([n.type for n in self.terminal_nodes]) self.node_type_to_id = OrderedDict() for i, type in enumerate(node_types, start=0): self.node_type_to_id[type] = i for gid, rule in enumerate(rules, start=0): self.rule_to_id[rule] = gid self.id_to_rule = OrderedDict( (v, k) for (k, v) in list(self.rule_to_id.items()))
def __repr__(self): repr_str = '' # if not self.is_leaf: repr_str += '(' repr_str += typename(self.type) if self.label is not None: repr_str += '{%s}' % self.label if self.value is not None: repr_str += '{val=%s}' % self.value # if not self.is_leaf: for child in self.children: repr_str += ' ' + child.__repr__() repr_str += ')' return repr_str