#!/usr/bin/python3 from tack import Lit, Nonterminal class Nonterminals: def __getattr__(self, name): val = Nonterminal(name) setattr(self, name, val) return val def delsubtrees(tree, tags): return (delsubtrees(tree[0], tags) if type(tree) is list and len(tree) == 2 and type(tree[1]) is tuple and tree[1][0] in tags else delsubtrees(tree[1], tags) if type(tree) is list and len(tree) == 2 and type(tree[0]) is tuple and tree[0][0] in tags else [delsubtrees(t, tags) for t in tree] if type(tree) is list else (tree[0], delsubtrees(tree[1], tags)) if type(tree) is tuple and len(tree) == 2 else tree if type(tree) is str else 'FUCK') def deltags(tree, tags): return (deltags(tree[1], tags) if type(tree) is tuple and len(tree) == 2 and tree[0] in tags else tree if type(tree) is str else [deltags(item, tags) for item in tree] if type(tree) is list else (tree[0], deltags(tree[1], tags)) if type(tree) is tuple and len(tree) == 2 else 'FUCK') if __name__ == '__main__': g = Nonterminals() g.sp.rule = Lit(' ') | '\n' | '\t' _ = g._ g._.rule = g.sp + _ | '' g.digit.rule = Lit('0') | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' g.digits.rule = g.digit + g.digits | g.digit ns = g.digits g.num.rule = (ns + (Lit('.') + ns | '.') | Lit('.') + ns | ns) + _ g.e.rule = g.num + '**' + _ + g.num | g.num g.m.rule = g.e + (Lit('/') | '*') + _ + g.m | g.e g.a.rule = g.m + (Lit('+') | '-') + _ + g.a | g.m t, length = g.a.parse('13 + 4 * 5 + 4 ** 3 * 1') print(length, t) print() print(deltags(delsubtrees(t, '_'), set(['a', 'm', 'e', 'digit', 'digits'])))