-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFA.cpp
79 lines (76 loc) · 2.11 KB
/
DFA.cpp
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
73
74
75
76
77
78
79
#include "DFA.h"
#include <string>
#include "Transition.h"
using namespace std;
DFA::DFA(list<string> q, list<char> sigma, list<Transition> delta, string q0, list<string> finalstate)
{
// setting the main data
Q0 = q0;
Q = list<string>(q);
Sigma = list<char>(sigma);
Delta = list<Transition>(delta);
FinalState = list<string>(finalstate);
}
DFA::~DFA()
{
// clearing all the variables after finishing with each one of them
// memory managment
Q0.clear();
Q.clear();
Sigma.clear();
Delta.clear();
FinalState.clear();
}
void DFA::Accepts(string input)
{
string currentState = Q0; // setting at the begging the start state
string steps = ""; // contain all the steps taken so far
// the first loop go through the input string
for (int i = 0, inputSize = input.length(); i < inputSize; i++)
{
// get the letter
char letter = input.at(i);
bool found = false; // use to know if we found a transition or not duing the for loop
// search all transitions for a one that have the same start state and same letter
for (list<Transition>::iterator it = Delta.begin(); it != Delta.end(); it++)
{
if (it->getStartState() == currentState && it->getToken() == letter)
{
found = true;
currentState = it->getEndState();
steps += it->toString();
break;
}
}
if (!found)
{
// if there was no transition at all print the following instead
if (steps.length() == 0)
{
steps = "This was the first transition and it failed\n";
}
// print that the input is wrong regarding this states
// then print the steps taken so far if there was any
cout << steps << "No transitions for current state and letter" << endl;
return; // in order to exist the program
}
}
// now check and see if the current state is one of the final states
bool correct = false;
for (list<string>::iterator it = FinalState.begin(); it != FinalState.end(); it++)
{
if (currentState == *it)
{
correct = true;
break;
}
}
if (correct)
{
cout << "CORRECT" << endl << steps;
}
else
{
cout << "FAILED" << endl << steps;
}
}