def kleene(a1: Automaton) -> Automaton: a2 = a1.deepcopy() for state in a2.acceptstates: a2.add_transition(state, "%", a2.initial.name) new_initial_state = nouvel_etat(a1) a2.add_transition(new_initial_state, EPSILON, a1.initial.name) a2.initial = a2.statesdict[new_initial_state] a2.make_accept(new_initial_state) a2.name = "kleen" return a2
def kleene(a1: Automaton) -> Automaton: """ Return kleene star of the automaton passed in the argument """ a1star = a1.deepcopy() a1star.name = "a1star" for state in a1star.acceptstates: a1star.add_transition(state, EPSILON, a1star.initial.name) new_state_name = nouvel_etat(a1star) a1star.add_transition(new_state_name, EPSILON, a1star.initial.name) a1star.initial = a1star.statesdict[new_state_name] a1star.make_accept(new_state_name) return a1star
def union(a1: Automaton, a2: Automaton) -> Automaton: Union = a1.deepcopy() nom_nouvel_etat = nouvel_etat(a2) for s in a2.states: if s in Union.states: Union.rename_state( s, nouvel_etat(a2)) # ici on modifie a3 directement -> à éviter nom_nouvel_etat = str(int(nom_nouvel_etat) + 1) for (s, a, d) in a2.transitions: Union.add_transition(s, a, d) Union.make_accept(a2.acceptstates) Union.add_transition(nom_nouvel_etat, EPSILON, a1.initial.name) Union.add_transition(nom_nouvel_etat, EPSILON, a2.initial.name) Union.initial.name = nom_nouvel_etat Union.remove_transition(Union.initial.name, EPSILON, Union.initial.name) return Union
def concat(a1: Automaton, a2: Automaton) -> Automaton: a1star_a2 = a1.deepcopy() a1star_a2.name = "a1star_a2" nom_nouvel_etat = nouvel_etat(a1star_a2) for s in a2.states: if s in a1star_a2.states: # l'état de a2 existe dans la copie de a1star a2.rename_state( s, nom_nouvel_etat) # ici on modifie a2 directement -> à éviter nom_nouvel_etat = str(int(nom_nouvel_etat) + 1) # incrémente le compteur for (s, a, d) in a2.transitions: a1star_a2.add_transition(s, a, d) a1star_a2.make_accept(a2.acceptstates) for ac in a1.acceptstates: a1star_a2.add_transition(ac, EPSILON, a2.initial.name) a1star_a2.make_accept(a1.acceptstates, accepts=False) # transforme en états non acceptants return a1star_a2
def concat(a1: Automaton, a2: Automaton) -> Automaton: """ Concatenate 2 automaton and return the result """ a1a2 = a1.deepcopy() a1a2.name = a1.name + a2.name new_state_name = nouvel_etat(a1a2) for s in a2.states: if s in a1a2.states: while new_state_name in a2.states: new_state_name = str(int(new_state_name) + 1) a2.rename_state(s, new_state_name) new_state_name = str(int(new_state_name) + 1) for (s, a, d) in a2.transitions: a1a2.add_transition(s, a, d) a1a2.make_accept(a2.acceptstates) for ac in a1.acceptstates: a1a2.add_transition(ac, EPSILON, a2.initial.name) a1a2.make_accept(a1.acceptstates, accepts=False) return a1a2
def union(a1: Automaton, a2: Automaton) -> Automaton: """ Unite 2 automaton and return the result """ a1ora2 = a1.deepcopy() a1ora2.name = a1.name + "+" + a2.name new_state_name = nouvel_etat(a1ora2) for s in a2.states: if s in a1ora2.states: while new_state_name in a2.states: new_state_name = str(int(new_state_name) + 1) a2.rename_state(s, new_state_name) new_state_name = str(int(new_state_name) + 1) for (s, a, d) in a2.transitions: a1ora2.add_transition(s, a, d) a1ora2.make_accept(a2.acceptstates) new_state_name = nouvel_etat(a1ora2) a1ora2.add_transition(new_state_name, EPSILON, a1ora2.initial.name) a1ora2.add_transition(new_state_name, EPSILON, a2.initial.name) a1ora2.initial = a1ora2.statesdict[new_state_name] return a1ora2