Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DFA* nfa_to_dfa(NFA *& nfa) {
- DFA *dfa = new DFA();
- cout <<"nfa size:" << nfa->get_states().size() << endl;
- //initial set.
- set<State*> inital_closure = nfa->get_start_state()->get_epsilon_closure();
- bool acceptance = false;
- int pattern_id = -1;
- for (State *s : inital_closure) {
- acceptance |= s->get_acceptance();
- pattern_id = get_new_pattern(pattern_id, s->get_pattern_id());
- }
- //Start state of DFA.
- State *inital_state = dfa->add_new_start_state(pattern_id, acceptance);
- //stack containing all unmakred states.
- stack <set<State*> > states_stack;
- states_stack.push(inital_closure);
- //map every subset NFA states to equvalent DFA state.
- map<set<State*>, State*> dfa_mapping;
- dfa_mapping[inital_closure] = inital_state;
- while (!states_stack.empty()) {
- set<State*> curr_states = states_stack.top();
- states_stack.pop();
- State *curr_dfa_state = dfa_mapping[curr_states];
- /*
- cout << "Curr State: ";
- for (State *s : curr_states)
- cout << s->get_state_id() << " ";
- cout << endl;
- */
- for (unsigned char c = MIN_CHAR; c <= MAX_CHAR; c++) {
- set<State*> new_states = move(curr_states, c);
- //set<State*> new_states = epsilon_closure(temp);
- if (new_states.empty())
- continue;
- /*
- cout << "Curr transition: " << c << endl;;
- for (State *s : new_states)
- cout << s->get_state_id() << " ";
- cout << endl;
- */
- if (!dfa_mapping.count(new_states)) {
- bool acceptance_state = false;
- int pattern_id = -1;
- for (State *s : new_states) {
- acceptance_state |= s->get_acceptance();
- pattern_id = get_new_pattern(pattern_id, s->get_pattern_id());
- }
- State * new_state = dfa->add_new_end_state(pattern_id, acceptance_state);
- dfa_mapping[new_states] = new_state;
- states_stack.push(new_states);
- }
- curr_dfa_state->add_transition(c, dfa_mapping[new_states]);
- }
- // cout <<"States: "<< states_stack.size() << endl;
- }
- return dfa;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement