def to_rules(self, rules, lexicon):
    """In-order travesal for the constituency tree."""
    if isinstance(self.rule, LexicalRule):
      if self.rule.lhs not in lexicon:
        lexicon[self.rule.lhs] = [self.rule]
      else:
        lexicon[self.rule.lhs] = [self.rule] + lexicon[self.rule.lhs]
    else:
      rules.insert(0, self.rule)
    for child in self.children:
      if isinstance(child, Derivation):
        child.to_rules(rules, lexicon)
      else:
        lexicon[child] = [child]
    return
In-order travesal for the constituency tree.
language/gscan/data/grammar.py