def compareREs(*exprs): import string fsas = map(compileRE, exprs) def setName(set): set = map(lambda x:x.label, set) if len(set) == 1: return set[0] else: import string return string.join(set[:-1], ', ') + ' and ' + set[-1] print 'Comparing', setName(fsas) processed = [] sets = [] for fsa in fsas: if fsa in processed: continue set = [fsa] for other in fsas: if other != fsa and other not in processed and FSA.equivalent(fsa, other): set.append(other) sets.append(set) processed.extend(set) for set in sets: if len(set) > 1: print setName(set), 'are equivalent' if len(sets) > 1: for set in sets: others = filter(lambda a,b=set:a not in b, fsas) only = FSA.difference(set[0], reduce(FSA.union, others)) if not only.isEmpty(): es = (len(set) == 1 and 'es') or '' print 'Only', setName(set), 'match'+es, decompileFSA(only)
def simplify(str): return decompileFSA(compileRE(str).minimized())