def add_category(base_name): assert is_cat(base_name) name = base_name while name in self.categories: name = name + '_' self.categories.add(name) return name
def validate_parse(self): assert isinstance(self.rule, Rule), 'Not a Rule: %s' % self.rule assert isinstance(self.children, Iterable) assert len(self.children) == len(self.rule.rhs) for i in range(len(self.rule.rhs)): if is_cat(self.rule.rhs[i]): assert self.rule.rhs[i] == self.children[i].rule.lhs else: assert self.rule.rhs[i] == self.children[i]
def add_rule(self, rule): if rule.contains_optionals(): self.add_rule_containing_optional(rule) elif rule.is_lexical(): self.lexical_rules[rule.rhs].append(rule) elif rule.is_unary(): self.unary_rules[rule.rhs].append(rule) elif rule.is_binary(): self.binary_rules[rule.rhs].append(rule) elif all([is_cat(rhsi) for rhsi in rule.rhs]): self.add_n_ary_rule(rule) else: raise Exception('RHS mixes terminals and non-terminals: %s' % rule)