-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
61 lines (53 loc) · 1.62 KB
/
main.cpp
File metadata and controls
61 lines (53 loc) · 1.62 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
//This is an example on a deterministic finite state machine with a 2D Array
//The machine accepts all words ending by the sequence "ananas" in a given string
#include <iostream>
#include <string>
using namespace std;
//The recursive function delta_hat represents the transition function
int delta_hat(int array[][4], std::string sigma, int state, std::string word, int error)
{
int number = -1;
if(word.empty())
return state;
for(int i=0; i< (int)sigma.length(); i++) //the int cast is just to avoid a warning in gcc
{
if(sigma.at(i) == word.at(0))
number = i;
}
if(number == -1)
{
for(int i=0; i< error; i++)
cout << " ";
cout << "^" << endl;
cout << "word contents undefined symbol!" << endl;
return -1;
}
state = array[number][state];
word = word.substr(1, word.length()); //word is shortend by the first char
return delta_hat(array, sigma, state, word, ++error);
}
int main()
{
//z0 z1 z2 z3 z4 z5 zE
int array[3][7] = {/*a*/ {1, 1, 3, 1, 5, 1, 1},
/*n*/ {0, 2, 0, 4, 0, 4, 0},
/*s*/ {0, 0, 0, 0, 0, 6, 0}};
std::string sigma = "ans";
cout << "Please enter the word to check:" << endl;
std::string word;
cin >> word;
int test = delta_hat(array, sigma, 0, word, 0);
switch(test)
{
case -1:
cout << "Error while parsing word" << endl;
break;
case 6:
cout << "word accepted!" << endl;
break;
default:
cout << "word not accepted!" << endl;
break;
}
return 0;
}