Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. DFA* nfa_to_dfa(NFA *& nfa) {
  2. DFA *dfa = new DFA();
  3. cout <<"nfa size:" << nfa->get_states().size() << endl;
  4. //initial set.
  5. set<State*> inital_closure = nfa->get_start_state()->get_epsilon_closure();
  6. bool acceptance = false;
  7. int pattern_id = -1;
  8. for (State *s : inital_closure) {
  9. acceptance |= s->get_acceptance();
  10. pattern_id = get_new_pattern(pattern_id, s->get_pattern_id());
  11. }
  12.  
  13. //Start state of DFA.
  14. State *inital_state = dfa->add_new_start_state(pattern_id, acceptance);
  15.  
  16. //stack containing all unmakred states.
  17. stack <set<State*> > states_stack;
  18. states_stack.push(inital_closure);
  19.  
  20. //map every subset NFA states to equvalent DFA state.
  21. map<set<State*>, State*> dfa_mapping;
  22. dfa_mapping[inital_closure] = inital_state;
  23. while (!states_stack.empty()) {
  24. set<State*> curr_states = states_stack.top();
  25. states_stack.pop();
  26. State *curr_dfa_state = dfa_mapping[curr_states];
  27. /*
  28. cout << "Curr State: ";
  29. for (State *s : curr_states)
  30. cout << s->get_state_id() << " ";
  31. cout << endl;
  32. */
  33. for (unsigned char c = MIN_CHAR; c <= MAX_CHAR; c++) {
  34. set<State*> new_states = move(curr_states, c);
  35. //set<State*> new_states = epsilon_closure(temp);
  36. if (new_states.empty())
  37. continue;
  38. /*
  39. cout << "Curr transition: " << c << endl;;
  40. for (State *s : new_states)
  41. cout << s->get_state_id() << " ";
  42. cout << endl;
  43. */
  44. if (!dfa_mapping.count(new_states)) {
  45. bool acceptance_state = false;
  46. int pattern_id = -1;
  47. for (State *s : new_states) {
  48. acceptance_state |= s->get_acceptance();
  49. pattern_id = get_new_pattern(pattern_id, s->get_pattern_id());
  50. }
  51. State * new_state = dfa->add_new_end_state(pattern_id, acceptance_state);
  52. dfa_mapping[new_states] = new_state;
  53. states_stack.push(new_states);
  54. }
  55. curr_dfa_state->add_transition(c, dfa_mapping[new_states]);
  56. }
  57. // cout <<"States: "<< states_stack.size() << endl;
  58. }
  59. return dfa;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement