
Ok = ok and (self.links = node.links)ĭef _init_(self, from_node, etiquette, to_node):

I was however ever so slightly inspired by John Coleman's answer. Here is my version of a dfa implementation if you're looking for a more object-oriented one. Sample Run(strings ending with abb are accepted): enter the string: abb Trans(transition, input, final, state, i) #input string for next transition is input If j = len(input)-1 and (str(state) in final): #last symbol is read and current state lies in the set of final states If each < 4: #move further only if you are at non-hypothetical state Trans(transition, input, final, start, i)ĭef trans(transition, input, final, state, i):įor each in transition)]: #check for each possibility I=0 #counter to remember the number of symbols read Input = list(input) #copy the input in list because python strings are immutable and thus can't be changed directlyįor index in range(len(input)): #parse the string of a,b in 0,1 for simplicity Here is the Python code: #nfa simulation for (a|b)*abb e.g, if you are at q1, and the current input symbol is 'a', you go to state 4 and stop computing further. You directly go to the state 4 and say input is not accepted for current progress(Check other possibilities by going back). It is helpful when you can't read the input from the current state. Once you go to that state, you can't move further.

The transitions can be represented using list of lists as follows: transition = ,], ,], ,], ,],]] Here I present a recursive solution for NFA. For example, the following DFA from the Wikipedia article on DFAsĬan be represented by a dictionary like this: dfa = ,'1011101')įor NFAs you could store sets of possible states rather than individual states in the transition dictionaries and use the random module to pick the next state from the set of possible states. For each state create a dictionary which is keyed by the letters of the alphabet and then a global dictionary which is keyed by the states. A straightforward way to represent a DFA is as a dictionary of dictionaries.
