def state_minimization(automaton): partition = DisjointSet(*range(automaton.states)) ## partition = { NON-FINALS | FINALS } # Your code here partition.merge([x for x in automaton.finals]) non_finals = [ nf for nf in filter(lambda x: not x in automaton.finals, range(automaton.states)) ] partition.merge(non_finals) while True: new_partition = DisjointSet(*range(automaton.states)) ## Split each group if needed (use distinguish_states(group, automaton, partition)) # Your code here for gr in partition.groups: for gr in distinguish_states(gr, automaton, partition): new_partition.merge(gr) if len(new_partition) == len(partition): break partition = new_partition return partition
def state_minimization(automaton): partition = DisjointSet(*range(automaton.states)) finals = list(automaton.finals) non_finals = [ state for state in range(automaton.states) if not state in automaton.finals ] partition.merge(finals) partition.merge(non_finals) while True: new_partition = DisjointSet(*range(automaton.states)) for group in partition.groups: new_groups = distinguish_states(group, automaton, partition) for new_group in new_groups: new_partition.merge(new_group) if len(new_partition) == len(partition): break partition = new_partition return partition
def state_minimization(automaton): K = DisjointSet(*ss(automaton.states)) K.merge(s for s in automaton.finals) K.merge(s for s in ss(automaton.states) if s not in automaton.finals) while KK: c = DisjointSet(*ss(automaton.states)) for R in K.groups: for h in distinguish_states(R, automaton, K): c.merge(h) if g(c) == g(K): break K = c return K
def state_minimization(automaton): partition = DisjointSet(*range(automaton.states)) ## partition = { NON-FINALS | FINALS } partition.merge(automaton.finals) partition.merge( [x for x in range(automaton.states) if x not in automaton.finals]) while True: new_partition = DisjointSet(*range(automaton.states)) for group in partition.groups: for subgroup in distinguish_states(group, automaton, partition): new_partition.merge(subgroup) if len(new_partition) == len(partition): break partition = new_partition return partition
def from_lr1_to_lalr(lr1_automaton: State): pending = [s for s in lr1_automaton] ds = DisjointSet(*pending) while pending: to_merge = [] pivot = center_of(pending[0].state) for i, x in enumerate(pending): if center_of(x.state) == pivot: to_merge.append(x) pending[i] = None pending = [x for x in pending if x] ds.merge(to_merge) for g in ds.groups: r = g[0].representative for x in [x for x in g if r != x]: for prod in r.value.state: for prod2 in x.value.state: if prod.Center() == prod2.Center(): new_lookahead = {x for x in prod.lookaheads} new_lookahead.update({x for x in prod2.lookaheads}) prod.lookaheads = frozenset(new_lookahead) return build_automaton_from_ds(lr1_automaton, ds)
def state_minimization(automaton): partition = DisjointSet(*range(automaton.states)) not_final = {s for s in range(automaton.states)} final = {s for s in automaton.finals} not_final.difference_update(final) partition.merge(not_final) partition.merge(final) while True: new_partition = DisjointSet(*range(automaton.states)) for group in partition.groups: new_groups = distinguish_states(group, automaton, partition) for g in new_groups: new_partition.merge(g) if len(new_partition) == len(partition): break partition = new_partition return partition
def state_minimization(automaton): partition = DisjointSet(*range(automaton.states)) ## partition = { NON-FINALS | FINALS } # Your code here non_finals = [ state for state in range(automaton.states) if state not in automaton.finals ] finals = [state for state in automaton.finals] partition.merge(non_finals) partition.merge(finals) while True: new_partition = DisjointSet(*range(automaton.states)) ## Split each group if needed (use distinguish_states(group, automaton, partition)) # Your code here for group in partition.groups: if len(group) > 1: new_partition.merge([i.value for i in group]) aux_partition = DisjointSet(*range(automaton.states)) for group in new_partition.groups: new_groups = distinguish_states(group, automaton, new_partition) for i in new_groups: aux_partition.merge(i) new_partition = aux_partition if len(new_partition) == len(partition): break partition = new_partition return partition