def form_commands_from_template(self, derivation_template, derivation_rules):
    """Similar to parent's function but use new Derivation class."""

    # Replace each lexical rule with the possible words from the lexicon.
    replaced_template = []
    previous_symbol = None
    lexicon = {}
    for symbol in derivation_template:
      if isinstance(symbol, grammar.Nonterminal):
        # pytype: disable=attribute-error
        possible_words = [s.name for s in self.rules[symbol]]
        for rule in self.rules[symbol]:
          lexicon[rule.name] = rule
        if previous_symbol == symbol:
          previous_words = replaced_template.pop()
          first_words, second_words = self.split_on_category(previous_words)
          replaced_template.append(first_words)
          replaced_template.append(second_words)
        else:
          replaced_template.append(possible_words)
      else:
        lexicon[symbol.name] = symbol
        replaced_template.append([symbol.name])
      previous_symbol = symbol

    # Generate all possible commands from the templates.
    all_commands = list(itertools.product(*replaced_template))
    all_derivations = []
    for command in all_commands:
      command_lexicon = {}
      for word, symbol in zip(command, derivation_template):
        if symbol not in command_lexicon:
          command_lexicon[symbol] = [lexicon[word]]
        else:
          command_lexicon[symbol] = [lexicon[word]] + command_lexicon[symbol]
        if isinstance(symbol, grammar.Nonterminal):
          if word not in self.command_statistics[symbol].keys():
            self.command_statistics[symbol][word] = 1
          else:
            self.command_statistics[symbol][word] += 1
      derivation = Derivation.from_rules(
          derivation_rules.copy(), symbol=ROOT, lexicon=command_lexicon)
      if " ".join(derivation.words()) != " ".join(command):
        raise ValueError("Derivation and command not the same.")
      # pytype: enable=attribute-error
      all_derivations.append(derivation)
    return all_derivations
Similar to parent's function but use new Derivation class.
language/gscan/data/grammar.py