Package: automathon
AutomathonCreated by: Robin Hafid Quintero LopezA Python library for simulating and visualizing finite automata.LinksGitHub repository:https://github.com/rohaquinlop/automathonPyPI:https://pypi.org/project/automathon/Twitter:https://twitter.com/RobinHafidContact:rohaquinlop301@gmail.comInstallationPyPIpipinstallautomathonYou also need to install Graphviz on your computer (download page,installation procedure for Windows,archived versions).Make sure that the directory containing thedotexecutable is on your systems’ path.UpgradePyPIpipinstallautomathon--upgradeDeterministic Finite AutomataRepresenting the previous automatafromautomathonimportDFAQ={'q0','q1','q2'}sigma={'0','1'}delta={'q0':{'0':'q0','1':'q1'},'q1':{'0':'q2','1':'q0'},'q2':{'0':'q1','1':'q2'}}initialState='q0'F={'q0'}automata1=DFA(Q,sigma,delta,initialState,F)## This is an example about creating a DFA with the libraryVerify if the automata is validautomata1.is_valid()# TrueVerify if the automata accept a stringautomata1.accept("001001")#Trueautomata1.accept("00100")#FalseGet the automata's complementnotautomata1=automata1.complement()notautomata1.accept("00100")#TrueVisualize the automataFor both, DFA and NFA, the view method enables to visualize the automaton, receives as parameter a String as the file name for the png and svg files.More information about the graphviz attributeshere.automata1.view("DFA Visualization")# Add custom stylingautomata1.view(file_name="DFA Custom Styling",node_attr={'fontsize':'20'},edge_attr={'fontsize':'20pt'})Convert DFA to NFAIf you need to use a DFA and operate with a NFA you can convert your DFA to NFA class, using the functiongetNFA.getNFAconvert your DFA to NFA class and returns its conversion.automata1NFA=automata1.get_nfa()automata1NFA.view("DFA to NFA")Product of two automatasTheproductfunction receives a DFA and  returns the product of two DFAs, your actual DFA and the given as parameter.Q={'A','B'}sigma={'0','1'}delta={'A':{'0':'A','1':'B'},'B':{'0':'B','1':'A'}}initialState='A'F={'B'}dfa=DFA(Q,sigma,delta,initialState,F)Q1={'R','S','T','U'}sigma1={'0','1'}delta1={'R':{'0':'S','1':'R'},'S':{'0':'T','1':'R'},'T':{'0':'U','1':'R'},'U':{'0':'U','1':'U'}}initialState1='R'F1={'U'}dfa1=DFA(Q1,sigma1,delta1,initialState1,F1)dfa2=dfa.product(dfa1)assertdfa2.is_valid()==Trueassertdfa2.accept("0001")==Trueassertdfa2.accept("00010010")==FalseUnion of two automatasTheunionfunction receives a DFA and returns the union of two DFAs, your actual DFA and the given as parameter.Q={'A','B'}sigma={'0','1'}delta={'A':{'0':'A','1':'B'},'B':{'0':'B','1':'A'}}initialState='A'F={'B'}dfa=DFA(Q,sigma,delta,initialState,F)Q1={'R','S','T','U'}sigma1={'0','1'}delta1={'R':{'0':'S','1':'R'},'S':{'0':'T','1':'R'},'T':{'0':'U','1':'R'},'U':{'0':'U','1':'U'}}initialState1='R'F1={'U'}dfa1=DFA(Q1,sigma1,delta1,initialState1,F1)dfa2=dfa.union(dfa1)assertdfa2.accept("00010010")==Trueassertdfa2.accept("0011000")==Trueassertdfa.is_valid()==TrueNon-Deterministic Finite AutomataImage taken from:http://www.r9paul.org/blog/2008/nondeterministic-finite-state-machine/Representing the previous automatafromautomathonimportNFA## Epsilon Transition is denoted by '' -> Empty stringQ={'q1','q2','q3','q4'}sigma={'0','1'}delta={'q1':{'0':{'q1'},'1':{'q1','q2'}},'q2':{'0':{'q3'},'':{'q3'}},'q3':{'1':{'q4'},},'q4':{'0':{'q4'},'1':{'q4'},},}initialState='q1'F={'q4'}automata2=NFA(Q,sigma,delta,initialState,F)## This is an example about creating a NFA with the libraryVerify if the automata is validautomata2.is_valid()# TrueVerify if the automata accept a stringautomata2.accept("0000011")#Trueautomata2.accept("000001")#FalseGet the automata's complementnotautomata2=automata1.complement()notautomata2.accept("000001")#TrueVisualize the automataautomata2.view("NFA Visualization")# Add custom stylingautomata2.view(file_name="NFA Custom Styling",node_attr={'fontsize':'20'},edge_attr={'fontsize':'20pt'})Remove Epsilon transitions from NFAautomata3=automata2.remove_epsilon_transitions()automata3.view("NFA without EpsilonTransitions")Convert NFA to DFAautomata4=automata3.get_dfa()automata4.view("NFA to DFA")Product of two automatasTheproductfunction receives a NFA and returns the product of two NFAs, your actual NFA and the given as parameter.Q={'A','B'}sigma={'a','b'}delta={'A':{'a':{'B'},'b':{'A'}},'B':{'a':{'A'},'b':{'B'}}}initialState='A'F={'A'}nfa=NFA(Q,sigma,delta,initialState,F)Q1={'C','D'}sigma1={'a','b'}delta1={'C':{'a':{'C'},'b':{'D'}},'D':{'a':{'D'},'b':{'C'}}}initialState1='C'F1={'C'}nfa1=NFA(Q1,sigma1,delta1,initialState1,F1)nfa2=nfa.product(nfa1)assertnfa2.is_valid()==Trueassertnfa2.accept('')==Trueassertnfa2.accept('bb')==Trueassertnfa2.accept('b')==Falseassertnfa2.accept('bbaa')==Trueassertnfa2.accept('bbaaa')==FalseUnion of two automatasTheunionfunction receives a NFA and returns the union of two NFAs, your actual NFA and the given as parameter.Q={'A'}sigma={'a'}delta={'A':{'a':{'A'}}}initialState='A'F={'A'}nfa=NFA(Q,sigma,delta,initialState,F)Q1={'C','D','E'}sigma1={'a','b'}delta1={'C':{'b':{'D'},},'D':{'a':{'E'},'b':{'D'}}}initialState1='C'F1={'E'}nfa1=NFA(Q1,sigma1,delta1,initialState1,F1)nfa2=nfa.union(nfa1)assertnfa2.is_valid()==Trueassertnfa2.accept("aaaaaa")==Trueassertnfa2.accept("aaaabbbbaaa")==Falseassertnfa2.accept("bbbbbbbbba")==Trueassertnfa2.accept("aaaaaaaab")==FalseErrorsErrors that can be returned during the execution, and the cases that can appear.SigmaError:The automata contain an initial state, or a final state that's not defined on Q.The automata contain a delta transition that's not defined on Q or in Sigma.InputError:The automata is trying to consume a letter that's not defined in sigma.