def _build_recons_rules(self, rules):
    expand1s = {r.origin for r in rules if (r.options and r.options.expand1)}
    aliases = defaultdict(list)
    for r in rules:
        if r.alias:
            aliases[r.origin].append(r.alias)
    rule_names = {r.origin for r in rules}
    nonterminals = {sym for sym in rule_names if (sym.name.startswith('_') or (sym in expand1s) or (sym in aliases))}
    for r in rules:
        recons_exp = [(sym if (sym in nonterminals) else Terminal(sym.name)) for sym in r.expansion if (not is_discarded_terminal(sym))]
        if (recons_exp == [r.origin]):
            continue
        sym = (NonTerminal(r.alias) if r.alias else r.origin)
        (yield Rule(sym, recons_exp, alias=MakeMatchTree(sym.name, r.expansion)))
    for (origin, rule_aliases) in aliases.items():
        for alias in rule_aliases:
            (yield Rule(origin, [Terminal(alias)], alias=MakeMatchTree(origin.name, [NonTerminal(alias)])))
        (yield Rule(origin, [Terminal(origin.name)], alias=MakeMatchTree(origin.name, [origin])))
def _build_recons_rules(self, rules):
    expand1s = {r.origin for r in rules if (r.options and r.options.expand1)}
    aliases = defaultdict(list)
    for r in rules:
        if r.alias:
            aliases[r.origin].append(r.alias)
    rule_names = {r.origin for r in rules}
    nonterminals = {sym for sym in rule_names if (sym.name.startswith('_') or (sym in expand1s) or (sym in aliases))}
    for r in rules:
        recons_exp = [(sym if (sym in nonterminals) else Terminal(sym.name)) for sym in r.expansion if (not is_discarded_terminal(sym))]
        if (recons_exp == [r.origin]):
            continue
        sym = (NonTerminal(r.alias) if r.alias else r.origin)
        (yield Rule(sym, recons_exp, alias=MakeMatchTree(sym.name, r.expansion)))
    for (origin, rule_aliases) in aliases.items():
        for alias in rule_aliases:
            (yield Rule(origin, [Terminal(alias)], alias=MakeMatchTree(origin.name, [NonTerminal(alias)])))
        (yield Rule(origin, [Terminal(origin.name)], alias=MakeMatchTree(origin.name, [origin])))