Write Required Functions Script Prompts User Name File Representing Finite Automaton Indic Q43902850
Write the required functions and script that prompts the userfor the name of a file representing a finite automaton: indicatingits states and input->statetransitions; reads the information in the file(storing the finite automaton in a dictionary); prints thefinite-automaton/dictionary in a special form; prompts the user forthe name of a file storing the start-state and inputs to process(each line in the file contains this combination); repeatedlyprocesses these lines, computing the results of the finiteautomaton on each input, and then prints the results. Note that afinite automaton is really a program; in thisproblem we are reading a program from a file and then executing it(running the finite automaton) on various inputs. So, we are reallywriting a compiler/interpreter for a small programming language.
A finite automaton (FA) is an machine that issometimes called a Deterministic Finite Automaton (DFA; see thenext problem for an NDFA: a non-deterministic finite automaton). AnFA is described by its states and itstransitions: each transition fora state specifies an input andwhat state in the FA that input leads to. We canillustrate an FA as a graph with state labels in circles and edgelabels for transitions (see below).
Input and Output:
Read a file that describes a FA: each line contains a state and anarbitrary number of input->state transitions.Build a dictionary such that each key is a strstate and whose associated value is another dictionary specifyingall the transitions from that state: this second dictionary haskeys that are str inputs and associated values arestr states. The first token on each line is thestr state and the remaining tokens (always comingin pairs) are str inputs and their resultingstates. All tokens (which can comprise any number of characters)are separated by one semicolon character. We annotate thisdictionary as {str:{str:str}}.
For example, the input file faparity.txtcontains the following lines (which could appear in this order, orany other and still specify the same FA):
even;0;even;1;odd odd;0;odd;1;evenHere is a picture of the parity FA. It graphicallyillustrates the two states (evenand odd) and their transitions,using inputs (0and1) that always lead back to one of these twostates.

Here, the state even (meaning it has seen aneven number of 1 inputs so far) is a key in themain dictionary. Its value is a dictionary with two key/value pairs0->even and1->odd. It means that in theeven state, if the input is a 0the FA stays in the even state; if the input is a1 the FA goes to the odd state.And similarly (the next line) means that for theodd state, if the input is a 0the FA stays in the odd state; if the input is a1 the FA goes back to the evenstate. So, seeing an input of 0 keeps the FA inthe same state; seeing an input of 1 flips the FAinto the other state.
Print the finite automaton, one state (and its transitions) perline; the states are printed alphabetically and the transitiondictionary for each state is printed as a list ofinput/state items (tuples) such that these are printedalphabetically by the inputs.
For example, the file above would print as:
The Contents of the file picked for this Finite Automaton even transitions: [(‘0’, ‘even’), (‘1’, ‘odd’)] odd transitions: [(‘0’, ‘odd’), (‘1’, ‘even’)]
Note that there are multiple data files for this program:faparity.txt andfadivisibleby3.txt; test/debug your program on thefirst file; when you are done, test it on the last file. Draw theFA represented by each for to ensure that your code correctlyprints and computes with it. Important: This taskis not to write a Python code that simulates theParity FA; it is to write code that simulates anyFA, whose description it reads from a file.
Next, repeatedly read and process lines from a second inputfile, computing the results of the finite automaton running on thespecified start-state with its inputs; then print out the resultsin a special form. Each line in the file contains a start-statefollowed by a sequence of inputs (all separated by semicolons). Thestart-state will be a state in the FA (it is a key in the outerdictionary) the inputs may specify legal or illegal transitions(may or may not be keys in some inner dictonary).
For example, the input file fainputparity.txtcontains the following three lines:
even;1;0;1;1;0;1 even;1;0;1;1;0;x odd;1;0;1;1;0;1The first line means, the start-state is even andthe inputs are 1, 0,1, 1, 0, and1.
The result of processing each line is to print the start-state,and then each input and the new state it transitions to, andfinally print the stop-state. For the parity FAand the first line in this file, it should print
Start state = even Input = 1; new state = odd Input = 0; new state = odd Input = 1; new state = even Input = 1; new state = odd Input = 0; new state = odd Input = 1; new state = evenStop state = even
Note that the second line contains an input xwhich is not a legal input allowed in any state; any such inputshould stop the simulation for that line only, continuing to starta new simulation for all following lines (as illustrated in theSample Interaction).
Functions and Script:
Write the following functions and script. I am providing linecounts for these function bodies not as requirements, but toindicate the lengths of well-written Pythonic code.
- read_fa has an open (file) parameter; itreturns the dictionary representing the finite automaton;hint: I used splicing and the zipfunction to build the inner dictionaries. (body is 6 lines).
- fa_as_str has a dictionary parameter(representing the FA); it returns a multi-line string (each line isended by ‘n’), which when printed shows thecontents of the FA in the appropriate textual form: sortedalphabetically by state, with a state’s transitions sorted by theirinput value (body is 4 lines; can you do it in 1?).
- process has a dictionary parameter(representing the FA), a str parameter(representing the start-state), and a listparameter (representing a list ofstr inputs); it returns a listthat contains the start-state followed by tuplesthat show the input and resulting state after each transition. Forthe example shown above, process returns thefollowing list.[‘even’, (‘1’, ‘odd’), (‘0’, ‘odd’), (‘1’, ‘even’), (‘1’, ‘odd’), (‘0’, ‘odd’), (‘1’, ‘even’)]Finally, if an input is illegal (is not the key in some transitionfor the current state), say ‘x’, for the parityFA, then processshould terminate with the lasttuple in the list indicating aproblem: (‘x’, None) (body is 9 lines).
- interpret has a listparameter (the list result producedby process); it returns a multi-linestring (each line is ended by ‘n’), which whenprinted illustrates the results of processing an FA on an input inthe appropriate textual form. See how it prints the examplelist argument shown above in the output furtherabove. Also see the Sample Interaction below tosee how it prints input errors: see the middle example(body is 9 lines).
- Write a script at the bottom of this module (in if__name__ == ‘__main__’:) that prompts the user to enterthe file describing the FA, prints it, prompts the user to enterthe file containing lines of start-states and input, simulates theFA on each line, printing the results in the appropriate textualform (body is 7 lines).
Sample Interaction:
The program, as specified, will have the following interaction:user-typed information appears in italics. Your outputshould match this one. Pick the file name containing this Finite Automaton: faparity.txt The Contents of the file picked for this Finite Automaton even transitions: [(‘0’, ‘even’), (‘1’, ‘odd’)] odd transitions: [(‘0’, ‘odd’), (‘1’, ‘even’)] Pick the file name containing a sequence of start-states and subsequent inputs: fainputparity.txt Commence tracing this FA from its start-state Start state = even Input = 1; new state = odd Input = 0; new state = odd Input = 1; new state = even Input = 1; new state = odd Input = 0; new state = odd Input = 1; new state = even Stop state = even Commence tracing this FA from its start-state Start state = even Input = 1; new state = odd Input = 0; new state = odd Input = 1; new state = even Input = 1; new state = odd Input = 0; new state = odd Input = x; illegal input: simulation terminated Stop state = None Commence tracing this FA from its start-state Start state = odd Input = 1; new state = even Input = 0; new state = even Input = 1; new state = odd Input = 1; new state = even Input = 0; new state = even Input = 1; new state = odd Stop state = odd
You can also try the fadivisibleby3.txt finiteautomaton file, which determines whether an integer (sequence ofdigits) is divisible by 3: it is divisible if thefinite automaton stops in state rem0. It’s inputfile fainputdivisibleby3.txt tries thenumber12,435,711, which is divisible by3 and number 823, which is notdivisible by 3: dividing 823 by3 leaves a remainder of 1.
even odd Show transcribed image text even odd
Expert Answer
Answer to Write the required functions and script that prompts the user for the name of a file representing a finite automaton: in…
OR