thanks for the reply sir,
i have coded the following so far, and i think i am stucked here. actually i
know what should i do, but can not succed to do it. please look at this code
and tell me if i can make the conversion from this structure, and iff
possible (dont wanna bother you) an optional solution from here.
thanks beforehand,
I don't understand why you're using such primitive data structures to represent
what looks like some kind of graph based automaton. Shouldn't the nodes be
dynamically allocated ?
For example, here's how I'd start with a DFA (NFA is similar)
typedef char Symbol_t; // the symbols in our regular language
typedef std::vector<Symbol_t> Word_t;
typedef std::map<Symbol_t, State*> transition_t;
struct State
{
// for each Symbol, we have a transition from this node
// to another node.
std::map<Symbol_t , State*> transition;
// for convenience: each state should probably have a label.
std::string id;
};
// declare the interface
class DFA
{
virtual bool accepted (const std::vector<Symbol_t>& v) = 0;
};
class GraphDFA
{
std::list<State> states;
State* s0; // initial state
std::vector<State*> acceptStates;
public:
// test to see if a word is accepted. This code might be wrong, and probably is ...
bool accepted ( const std::vector<Symbol_t>& v )
{
State* s = s0;
transition_t::const_iterator j;
for (Word_t::const_iterator it
= v.begin(); it!=v.end(); ++it )
{
j = s->transition.find(*it);
// no transition defined: implicit fail state transition.
if (j == s->end())
return false;
else
s = j->second;
}
// done reading the word
return (std::find ( acceptStates.begin(),acceptStates.end(), s )!=acceptStates.end());
}
};
As I said, there are two ways that you could proceed with the NFA -> DFA conversion.
One would be to try to convert the graphs. This would require constructing one vertex for
each subset of the states of the NFA. The simplest way to do that would be to have 1 bit
representing each state, and then you have a binary number representing each state (look up
the proof that the size of the powerset is 2^N if you don't understand this). Then you'd
need to create a graph for each one.
A simpler way would be to re-implement the interface by subclassing. You'd still need a
data structure that could hold sets of states, for example s1 of type
std::set<State*> because the current state in your new DFA is a list of NFA
states. You compute a new set<State*> s2 from this by following all the
transition arrows from each state in the set s1, and appending the endpoints of those
transitions to s2. That at least enables you to test whether or not a word is accepted.
If you want some verbose printout of the graph structure, you can still do that without
constructing the graph. What you'd need to do is iterate over all states (all possible
set<State*> s with each element in your alphabet) and compute the transition arrows
from each (in same manner as in accept method) and find a way to describe the states.
HTH,