Program to construct a DFA to check if a given integer is unsigned or not | Python3 program for the above approach ; Function to construct DFA as per the given conditions ; If at state 0 and a digit has occurred then set it to state 1 ; Similarly for all other states ; Function to build and connect the DFA states ; Connect all the states to the dead state ; Function call to make DFA as per the given conditions ; Function call to check whether an integer in the form of is unsigned integer or not ; Build the DFA ; Stores the current state ; Traverse the string ; If at a certain state a digit occurs then change the current state according to the DFA ; Or + / - sign ; Or decimal occurred ; Or any other character ; Or e / E or exponent sign ; State 1 , 4 , 8 will give the final answer ; Driver Code
digits , sign = "0123456789" , " + - " NEW_LINE dot , ex = " . " , " eE " NEW_LINE dfa = [ [ 0 for i in range ( 5 ) ] for i in range ( 11 ) ] NEW_LINE def makeDFA ( ) : NEW_LINE INDENT global dfa NEW_LINE dfa [ 0 ] [ 0 ] = 1 NEW_LINE dfa [ 1 ] [ 0 ] = 1 NEW_LINE dfa [ 1 ] [ 2 ] = 3 NEW_LINE dfa [ 1 ] [ 3 ] = 2 NEW_LINE dfa [ 1 ] [ 4 ] = 6 NEW_LINE dfa [ 3 ] [ 0 ] = 4 NEW_LINE dfa [ 4 ] [ 0 ] = 4 NEW_LINE dfa [ 4 ] [ 3 ] = 5 NEW_LINE dfa [ 4 ] [ 4 ] = 6 NEW_LINE dfa [ 6 ] [ 0 ] = 8 NEW_LINE dfa [ 6 ] [ 1 ] = 7 NEW_LINE dfa [ 7 ] [ 0 ] = 8 NEW_LINE dfa [ 8 ] [ 0 ] = 8 NEW_LINE dfa [ 8 ] [ 3 ] = 9 NEW_LINE DEDENT def buildDFA ( ) : NEW_LINE INDENT global dfa NEW_LINE for i in range ( 11 ) : NEW_LINE INDENT for j in range ( 5 ) : NEW_LINE INDENT dfa [ i ] [ j ] = 10 NEW_LINE DEDENT DEDENT makeDFA ( ) NEW_LINE DEDENT def checkDFA ( s ) : NEW_LINE INDENT buildDFA ( ) NEW_LINE currentstate = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] in digits ) : NEW_LINE INDENT currentstate = dfa [ currentstate ] [ 0 ] NEW_LINE DEDENT elif ( s [ i ] in sign ) : NEW_LINE INDENT currentstate = dfa [ currentstate ] [ 1 ] NEW_LINE DEDENT elif ( s [ i ] in dot ) : NEW_LINE INDENT currentstate = dfa [ currentstate ] [ 2 ] NEW_LINE DEDENT elif ( s [ i ] in ex ) : NEW_LINE INDENT currentstate = dfa [ currentstate ] [ 4 ] NEW_LINE DEDENT else : NEW_LINE INDENT currentstate = dfa [ currentstate ] [ 3 ] NEW_LINE DEDENT DEDENT if ( currentstate == 1 or currentstate == 4 or currentstate == 8 ) : NEW_LINE INDENT print ( " Unsigned ▁ integer " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ an ▁ unsigned ▁ integer " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "1729" NEW_LINE checkDFA ( S ) NEW_LINE DEDENT