Homework Help Question & Answers

In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a seri...

In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The firstAnd heres one for the machine that accepts the language from problem 6 of assignment 2: 4 0 0 1 102 11θ 202 2 1 3 зез 3 1 3

In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a series of characters separated by a single space - The second line contains the number of states as an integer k 2 1; states will be numbered 0,1,..., k -1. The start state is always state O The third line contains a series of integers separated by a single space representing the accepting states -The remaining lines contain information defining δ, the transition function. Each line contains a state number i, a character c from the alphabet, and a state number j. This means that the transition is from state i to state j on character c For example, here's the input describing the FSM that accepts the language {w e sa,b)*: |wl- 0 mod 5): 1 a 2 1 b 2 2 a 3 2 b 3 3 a 4 3 b 4 4 b 0
And here's one for the machine that accepts the language from problem 6 of assignment 2: 4 0 0 1 102 11θ 202 2 1 3 зез 3 1 3 Write the program so that it always reads from a file called fsm.txt. Once the program reads in and processes the FSM input, it will process input strings. These will be in a single file called strings.txt. Each line of this file contains a string. Your program will read each, run it through the FSM, and then print it and whether it was accepted Call the program fsm.(java,py) and submit it through D2L
0 0
Next > < Previous
ReportAnswer #1

Code:

def read_fsm(filename):
    fh = open(filename,'r')
    contents = fh.readlines()
    sigma = list(contents[0].rstrip().split(' '))
    table = {}
    n = int(contents[1])
    for i in range(n):
        table[i] = {}
    final = list(map(int,contents[2].rstrip().split(' ')))
    
    for line in contents[3:]:
        fro,ip,to = line.split(' ')
        fro = int(fro)
        to = int(to)
        table[fro][ip] = to
    print(table)
    fh.close()
    return table,final
    
def runString(table,final,string):
    current = 0
    for i in string:
        current = table[current][i]
    if current in final:
        print(string,'--> ACCEPT')
    else:
        print(string,'--> REJECT')
        
def readInput(table,final):
    fh = open('Strings.txt','r')
    for line in fh.readlines():
        string = line.rstrip()
        runString(table,final,string)
    fh.close()
    
def main():
    table,final = read_fsm('fsm.txt')
    readInput(table,final)
    
main()
    

Output:

a REJECTED b --> REJECTED ab -REJECTED ba --REJECTED aab --> REJECTED bba --> REJECTED aba -REJECTED abab -->REJECTED aaab --

Know the answer?
Add Answer of:
In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a seri...
Your Answer: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a seri...

    In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a series of characters separated by a single space - The second line contains the number of states as an integer k 2 1; states will be numbered 0,1,..., k -1. The start state is always state O The third line contains a series...

  • Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} ...

    Write Java code to implement a FSM machine that recognizes the language for the alphabet {a,b,c} consisting of all strings that contain two consecutive c's and end with b.                   Your FSM program should include the following three static methods (Java) or functions (C):                                           a.    int nextState(int state, char symbol)                                  A state-transition function that returns the next state based on the                                  current state and an input symbol. This function should also return                                  -1 when an invalid input character is detected.                                  State...

  • Use PYTHON Write a program that will take an input file called input.txt which will contain a Jav...

    use PYTHON Write a program that will take an input file called input.txt which will contain a Java program and parse it and output an annotated version. Your program will track the nesting depth of the braces of the input file and will output an annotated version of the file. Your final program will 1. List the nesting depth of curly braces 2. Ignore braces inside quotes or comments Use Python Assume that all quoted strings begin and end on...

  • Deterministic and Nondeterministic Finite Accepter (Emulator)

    Please write a program in any language.I have a algorithm on the bottom of this page, so it souldn't be too hard.InputInput is a stream of characters from stdin. The newline character terminates a “string” to the acceptor.If a line has only the word , current program is terminated and the next program is read in.OutputThe output is a single word on stdout, either yes or no. No debugging output is allowed on stdout.AlphabetAll printable ASCII characters are considered legal....

  • Task 1: Write a Python program that takes as input from the user the name of a file containing po...

    python code: Task 1: Write a Python program that takes as input from the user the name of a file containing postcode/location information. Each line in the file consists of the postcode followed by a tab followed by a comma-separated list of locations that have that postcode. For example, the file Small.txt contains: 3015 Newport,South Kingsville,Spotswood 3016 Williamstown 3018 Altona,Seaholme 3019 3021 Albanvale,Kealba,Kings Park,St Albans Braybrook, Robinson Your program should create a list of postcode/location pairs with each pair stored...

  • Write a program that will first receive as input the name of an input file and an output file. It will then read in a list of names, id #s, and balances from the input file specified (call it InFile.t...

    Write a program that will first receive as input the name of an input file and an output file. It will then read in a list of names, id #s, and balances from the input file specified (call it InFile.txt) which you will create from the data provided below. The program will then prompt the user for a name to search for, when it finds the name it will output to a file (call it OFile.txt) the person’s id#, name,...

  • Input alphabet {a,b 1. write the CFG for the language of palindromes (5 points) 2. Convert this i...

    Input alphabet {a,b 1. write the CFG for the language of palindromes (5 points) 2. Convert this into PDA (state the accepting condition) (10 points) . Write a PDA for this language that satisfies the conditions required to convert it into CFG (5 points) 4. Convert the PDA from Q3 into CFG (10 points) Input alphabet {a,b 1. write the CFG for the language of palindromes (5 points) 2. Convert this into PDA (state the accepting condition) (10 points) ....

  • . Please design a standard TM (i.e., a single semi-infinite tape, deterministic) for the laın gua...

    . Please design a standard TM (i.e., a single semi-infinite tape, deterministic) for the laın guage of all palindromes over alphabet {a, b} . Please give both the high-level description (text description or pseudo-code algorithm) and the low-level state transition diagram. Please analyze how many steps (one transition is considered as a step) it takes to accept an input palindrome with n symbols . Please design a deterministic Turing machine with two semi-infinite tapes for the same language. Please give...

  • The file lab10movies.cpp contains a program that allows the user to read movie names and movie re...

    Please answer using C++ The file lab10movies.cpp contains a program that allows the user to read movie names and movie release dates from a file called movies.txt. The data is read into a single ended linked list. Once all the data has been read in, the user is given a menu option with 3 choices. Choice 1 will list all movies released before a specified year, Choice 2 will list all movies stored in the linked list, and Choice 3...

  • It wants a diagram Construct a TM M4 which accepts the language [w2w | w in (0,1)). This means, any input that comprises...

    It wants a diagram Construct a TM M4 which accepts the language [w2w | w in (0,1)). This means, any input that comprises two copies of a binary string w separated by a single 2 symbol should be accepted. Any other input should be rejected. The input alphabet is (0,1,2). The tape alphabet contains 0, 1,2,_and you may use additional symbols 3, 4, , 9 if you wish (M4 can be constructed comfortably with just one additional symbol 3). The...

Need Online Homework Help?
Ask a Question
Get FREE Expert Answers
WITHIN MINUTES