-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNfa2Dfa.cpp
More file actions
72 lines (59 loc) · 2.35 KB
/
Copy pathNfa2Dfa.cpp
File metadata and controls
72 lines (59 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// Created by Freddie on 9/16/2016.
//
#include "Nfa2Dfa.h"
#include <set>
#include "Simulation.h"
#include <algorithm>
using namespace std;
AutomataDfa* Nfa2Dfa(Automata* nfa, string language){
//counter for amount of new sets on DFA
int counter = 0;
//contains the sets of each node on DFA
vector <set<AutomataNode*>> dTran;
//vector for dfa nodes
vector<AutomataNode*> nodes;
//initial node on dfa
set <AutomataNode*> init;
init.insert(nfa->getInitNode());
dTran.push_back(eClosure(init));
AutomataNode* A = new AutomataNode;
A->setNumber(0);
nodes.push_back(A);
AutomataDfa* dfa = new AutomataDfa(A);
counter = counter + 1;
//int for comparing if a set of nodes has been added to the dfa
int same = 0;
for(int i = 0; i <counter ; i ++){//for on amount of new Nodes on nfa( changes dynamically)
for (int j = 0; j < language.size(); j ++){//for on amount of posibble letter transitions
set<AutomataNode*> toFind = eClosure(move(dTran[i], language[j]));//sets
same =0;
for (int k=0;k<dTran.size();k++){
if(dTran[k]==toFind){
same = same+1;
Transition* t = new Transition(language[j], nodes[k]); //adding transitions to nodes already in the dfa
nodes[i]->addTransition(t);
}
}
if (same ==0) { //is new set
AutomataNode* A = new AutomataNode;
A->setNumber(counter);
Transition* t = new Transition(language[j], A);
nodes[i]->addTransition(t);
nodes.push_back(A);
nodes[nodes.size() - 1]->setFinal(false);
for(set<AutomataNode *>::iterator it2 = toFind.begin(); it2 != toFind.end(); it2++){ //iterates the set to check if a node in it was final on nfa
AutomataNode *state = *it2;
if(state->getTransition().empty()==1){
nodes[nodes.size() - 1]->setFinal(true);
dfa->addFinNode(nodes[nodes.size()-1]);
}
}
dTran.push_back(eClosure(move(dTran[i], language[j])));
counter = counter + 1; // new node was made
}
}
}
dfa->setInitAutomataNode(nodes[0]);
return dfa;
}