Question
Attached are the files DFA.hpp and DFA.cpp for a simple data structure to represent a deterministic finite automaton. Your task is to implement the function
Attached are the files DFA.hpp and DFA.cpp for a simple data structure to represent a deterministic finite automaton.
Your task is to implement the function dfa_is_accepted, which takes as input a DFA and a string, and determines if the DFA accepts the string.
The version of g++ on the cs linux machines is recent as of the Summer of '15, which is to say, it is old. Please make sure it compiles there with the --std=c++11 flag, to give us a common baseline for running your code.
Submit your revised version of DFA.cpp, and answer the question: how did you test your code?
DFA.cpp:
DFA.hpp:
CODE
DFA.cpp:
#include
#include "DFA.hpp"
bool dfa_is_accepted (const DFA &m, const std::string &w)
{
return true;
}
std::ostream & operator
out
for (int q = 0; q
out
}
out
out
for (int i = 0; i
if (m.finalStates [i])
out
}
out
return out;
}
std::istream & operator >> (std::istream &in, DFA &m) {
in >> m.numStates;
boost::multi_array
m.transFunc.resize (extents [2][m.numStates]);
for (int q = 0; q
in >> m.transFunc [0][q];
in >> m.transFunc [1][q];
}
in >> m.initialState;
m.finalStates.resize (m.numStates, false);
int k;
in >> k;
for (int i = 0; i
int q;
in >> q;
m.finalStates [q] = true;
}
return in;
}
DFA.hpp:
#ifndef DFA_HPP
#define DFA_HPP
#include
#include
#include
/* numStates is the number of states in the DFA
*
* transFunc is the transition function.
* For simplicity, we number the states 0..numStates-1, and assume the alphabet is {0,1}.
* transFunc [1][5] would be the new state we enter if we encounter a 1 in state 5.
*
* initialState is the initial state.
*
* finalStates is the set of final states.
* finalStates [q] is true if q is a final state, false otherwise.
*/
struct DFA {
int numStates;
boost::multi_array
int initialState;
std::vector
};
/* Read or write a DFA in a specific format:
* an integer, representing the number of states in the DFA
* two integers for each state, the new states reached after reading a 0 or 1
* an integer representing the initial state
* the number of final states
* a list of the final states
*
* For example, we could represent a DFA for strings in which the number of 1 bits is a multiple of 4:
* 4
* 0 1
* 1 2
* 2 3
* 3 0
* 0
* 1 0
*/
std::ostream & operator
std::istream & operator >> (std::istream &in, DFA &m);
// Determines if machine m accepts string w.
bool dfa_is_accepted (const DFA &m, const std::string &w);
#endif
1 #include
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started