Question

In: Computer Science

DFA Python DFA simulator A deterministic finite automaton (DFA) - also known as deterministic finite state...

DFA
Python DFA simulator
A deterministic finite automaton (DFA) - also known as deterministic finite state machine - is a finite state machine that accepts/rejects finite strings of symbols and only produces a unique computation (or run) of the automaton for each input string.
Write a DFA simulator using the python programming language.
Read your DFA from a textfile. First line contains a list of the final states (as integers), separated by a space. Rest of file contains the transitions in form: startstate, blank, symbol read, blank, tostate
1.Prompt user for name of file.
2.From there, program prompts the user for strings to test for acceptance on DFA.
3.Show the trace of the transitions through the machine along with whether the string is accepted or rejected.
4.Continue reading strings until user enters "quit."
Example input file... (ab)*
0
0a1
ob2
1a2
1b0
2a2
2b2
Files to Upload
•Upload a total of five (5) files: python file; output file (text file); input file (textile containing the DFa); a diagram of the DFA.

Solutions

Expert Solution

Solution:-

#include<iostream>

#include<stdio.h>

#include<string>

#include<fstream>

#include<vector>

using namespace std;

// Defining structure to hold current, next state and input character

struct state{

int current;

char input;

int next;

};

// This will contain list of final states

std::vector<int> finalStates;

// This will contain list of all states

std::vector<struct state> list_states;

/*

This method will read data from input file as provided by the user.

It will parse the file and find all states of the DFA and final states.

*/

void readData(){

char filename[255];

cout << "Please provide filename" << endl;

scanf("%[^\n]", filename);

cout << filename << endl;

try{

ifstream inFile;

inFile.open(filename);

string line;

int count = 0;

// reading data from file line by line.

while(getline(inFile, line)){

// converting string to char pointers

char str=const_cast< char >(line.c_str());

// Splitting string into tokens based on seperator " "

char *token = strtok(str, " ");

int index = 0;

state stat;

// if count is 0 it means its reading the first time i.e. the final states

if(count == 0){

while (token != NULL)

{

finalStates.push_back(stoi(token));

token = strtok(NULL, " ");

}

count++;

}else{

// otherwise, it reads each state line by line and prepares the list of states

while (token != NULL)

{

if(index == 0){

stat.current = stoi(token);

}else if(index == 1){

stat.input = token[0];

}else{

stat.next = stoi(token);

}

token = strtok(NULL, " ");

index++;

}

list_states.push_back(stat);

}

}

//Closing the file

inFile.close();

} catch (const char* msg) {

cerr << msg << endl;

}

}

/*

This method will check the input string and checks if it is accepted by the DFA or not

*/

bool checkInputString(string userInput){

// Considering fist state as input state

int currentState = list_states[0].current;

for(int i=0; i < strlen(userInput.c_str()); i++){

// Check in all states if the currenct state and current input character matches with any state

for(int j=0; j < list_states.size(); j++){

// if it matches then move to the next state.

if(currentState == list_states[j].current && userInput[i] == list_states[j].input){

currentState = list_states[j].next;

break;

}

}

}

// Finally, checks if current state is also in any of the final states, if so then input string is accepted

// otherwise not

bool found = false;

for(int i=0; i < finalStates.size(); i++){

if(finalStates[i] == currentState){

found = true;

break;

}

}

return found;

}

int main(){

// calling readData method

readData();

string userInput = "quit";

ofstream outputfile;

outputfile.open ("output.txt");

// Taking user's input until he/she quits.

do{

cout << "Provide input string (quit to exit)"<< endl;

cin >> userInput;

if(userInput != "quit"){

bool found = checkInputString(userInput);

if(found){

cout << "Accepted" << endl;

outputfile << userInput << ": " << "Accepted" << endl;

}else{

cout << "Not Accepted" << endl;

outputfile << userInput << ": " << "Not Accepted" << endl;

}

}

}while(userInput != "quit");

outputfile.close();

}

Output file containing information about each input with result is shown below [output.txt]:

abaaabb: Accepted
ababa: Not Accepted
aaaaaabbbababaaab: Not Accepted
aba: Accepted

Thank you...!


Related Solutions

Automata Theory and Formal Languages Instructions: Draw the DFA (Deterministic Finite Automaton) of the following: DFA...
Automata Theory and Formal Languages Instructions: Draw the DFA (Deterministic Finite Automaton) of the following: DFA in which set of all strings can be accepted which end with ‘a’ DFA in which set of all strings can be accepted which start with ab DFA in which set of all strings can be accepted which contain ab DFA in which set of all strings can be accepted which ends with ab
Automata Theory and Formal Languages Instructions: Draw the DFA (Deterministic Finite Automaton) of the following: DFA...
Automata Theory and Formal Languages Instructions: Draw the DFA (Deterministic Finite Automaton) of the following: DFA for binary number divisible by 2 DFA for binary number divisible by 3 DFA in which set of all strings can be accepted which start with a DFA in which set of all strings can be accepted which contains ‘a’
Create a Deterministic finite automaton that takes in binary strings and accepts them which has both...
Create a Deterministic finite automaton that takes in binary strings and accepts them which has both an odd number of zeros and a sum that is divisible by 3 (but no others). For example, 0111 should be accepted, but not 011 or 111.
Description: In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language...
Description: In this assignment, you will implement a deterministic finite automata (DFA) using C++ programming language to extract all matching patterns (substrings) from a given input DNA sequence string. The alphabet for generating DNA sequences is {A, T, G, C}. Write a regular expression that represents all DNA strings that begin with ‘A’ and end with ‘T’. Note: assume empty string is not a valid string. Design a deterministic finite automaton to recognize the regular expression. Write a program which...
We demonstrated that if A is recognized by a nondeterministic finite state automaton, it can be...
We demonstrated that if A is recognized by a nondeterministic finite state automaton, it can be recognized by a deterministic finite-state automaton, by giving an algorithm for constructing a machine M' that is deterministic and accepts the same language as a nondeterministic machine M. By the construction algorithm we used, how many states will M' have if M has n states. Justify your answer
What is a finite-state machine ? What is a pushdown automaton ? What is a Turing...
What is a finite-state machine ? What is a pushdown automaton ? What is a Turing machine? What is a Turing complete language? Compare the finite-state machine , pushdown automaton , and Turing machine.?
Give the finite-state automaton and the regular grammar for the following: (a) (ab v ba)* v...
Give the finite-state automaton and the regular grammar for the following: (a) (ab v ba)* v (ab)* (b) (11*)*(110 v 01) (c) All strings over {0,1} containing the string 010 (d) All strings over {0,1} which do not contain the string 010
Given a non-deterministic automaton M = (K, Σ, s, ∆, F ) for K = {q0,...
Given a non-deterministic automaton M = (K, Σ, s, ∆, F ) for K = {q0, q1, q2, q3}, s = q0, Σ = {a, b}, F = {q1, q2, q3}, and ∆ = {(q0, a, q1), (q0, b, q3), (q1, a, q2), (q1, b, q1), (q3, a, q3), (q3, b, q2)} 1. (1pts) Draw the diagram of M 2. (2pts) Explain why M is not deterministic 3 (5pts) DRAW a diagram of a deterministic M0 such that M ≈...
Design an FA (Finite automaton) and RE to accept the set of all strings over the...
Design an FA (Finite automaton) and RE to accept the set of all strings over the alphabet {0,1} which contains even number of 0's and odd number of 1's.
Run the  Python Queue Line Simulator  three times Python Queue Line """ File: pyQueueSim.py Author: JD """ import...
Run the  Python Queue Line Simulator  three times Python Queue Line """ File: pyQueueSim.py Author: JD """ import random print("Queue as a customer line\n") queue = []              # Empty que y = int(0) # Queue up some customers for i in range(1,20):     x = random.randint(1, 20)     if x >= 2 and x<= 8:       queue.append(x)      # Add to the front        # Simulate cumstomer line processing while True:    x = random.randint(1, 20)    if x >= 2 and x<= 8:       queue.append(x *2)      # Add to the front...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT