-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDFA.cpp
More file actions
37 lines (35 loc) · 1.41 KB
/
Copy pathPDFA.cpp
File metadata and controls
37 lines (35 loc) · 1.41 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
#include "PDFA.h"
bool PartialDeterministicFiniteAutomaton::accepts(std::string word) {
int state = p;
// Loop through the input
for (int i = 0; i < word.length(); i++) {
char current = word[i];
bool found = false;
// Loop through the transition function to find the next state
std::tuple<int, char, int> transition;
for (std::set<std::tuple<int, char, int>>::iterator itr = delta.begin(); itr != delta.end(); ++itr) {
transition = *itr;
// Check if the function matches the state and character
if (std::get<0>(transition) == state && std::get<1>(transition) == current) {
found = true;
break;
}
}
// Check if a transition was found
if (found) {
// Apply the transition function
state = std::get<2>(transition);
} else {
return false;
}
}
return acc.count(state);
};
PartialDeterministicFiniteAutomaton::PartialDeterministicFiniteAutomaton(std::set<int> states, int initialState,
std::set<std::tuple<int, char, int>> partialTransitionFunction,
std::set<int> acceptingStates) {
X = states;
p = initialState;
delta = partialTransitionFunction;
acc = acceptingStates;
}