Question

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
Add a comment Improve this question Transcribed image text
Answer #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 --

Add a comment
Know the answer?
Add Answer to:
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:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

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...

  • Capitalization JAVA In this program, you will read a file line-by-line. For each line of data...

    Capitalization JAVA In this program, you will read a file line-by-line. For each line of data (a string), you will process the words (or tokens) of that line one at a time. Your program will capitalize each word and print them to the screen separated by a single space. You will then print a single linefeed (i.e., newline character) after processing each line – thus your program will maintain the same line breaks as the input file. Your program should...

  • FOR JAVA Write a program that takes two command line arguments: an input file and an...

    FOR JAVA Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message. Notes: This program can be written in a single main method Remember that...

  • Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list...

    Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list of positive and negative integers (duplicates are possible) separated by spaces and/or line breaks. Zero may be included. After reading the integers, the program saves them in a singly linked list in the same order in which they appear in the input file. Then, without changing the linked list, the program should print whether there exists two subsets of the list whose sums are...

  • IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and...

    IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. For example if file input is: This is a test File output should be 1. This is a test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BELOW FROM NOTEPAD FILE. This file contains lines of text to determine if you can properly read and write from a file.

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • 2. Write a program to read two lists of names from two input files and then...

    2. Write a program to read two lists of names from two input files and then match the names in the two lists using Co-sequential Match based on a single loop. Output the names common to both the lists to an output file, In Java

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • write a program in c++ Read an infix expression from an input file and convert to...

    write a program in c++ Read an infix expression from an input file and convert to postfix. Instead of displaying directly on the screen, first place in a queue, and then display the contents of the queue on the screen. Precondition: The expression will be read from a file (input.txt) that contains a single line. There will be no spaces between the operands and the operators. The following operators are allowed: ( ) + - * / The normal rules...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT