include iostream include string using namespace std bool analysator ch

 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
80
#include <iostream>
#include <string>
using namespace std;
bool analysator(char lex[], int n)
{
char state = 'H';
cout << "Исходная строка: " << endl;
for (int i = 0; i < n; i++)
{
cout << lex[i] << " ";
}
cout << endl;
cout << "История команд:" << endl;
for (int i = 0; i < n; i++)
{
switch (state)
{
case 'H':
cout << state << " ";
if (lex[i] == '0')
state = 'R';
else
return false;
break;
case 'R':
cout << state << " ";
if (lex[i] == '0')
state = 'T';
if (lex[i] == '1')
state = 'C';
break;
case 'T':
cout << state << " ";
if (i == n - 1)
state = 'C';
if (lex[i] == '0')
break;
else
return false;
break;
case 'C':
cout << state << " ";
if (lex[i] == '1' && i == n - 1)
return false;
if (lex[i] == '1')
break;
if (lex[i] == '0')
state = 'P';
case 'P':
return true;
break;
case 'S':
return true;
break;
}
}
}
int main()
{
setlocale(LC_ALL, "Russian");
char lex1[] = {'1', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '1'}; //12
char lex2[] = {'0', '1', '1', '1', '1', '1', '1', '1', '0'}; // 9
char lex3[] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'}; //14
char lex4[] = {'0', '0', '0', '0', '0', '0', '0', '0', '1', '1'}; // 10
char lex5[] = {'0', '1', '1', '1', '1', '1', '1', '1', '1', '1'}; // 10
char lex6[] = {'0', '1', '0'}; // 3
char lex7[] = {'0', '0', '0', '1', '0'}; // 5
cout << "Грамматика: " << endl;
cout << "S -> B0|C0" << endl;
cout << "B -> B0|0" << endl;
cout << "C -> C1|A1" << endl;
cout << "A -> 0" << endl;
cout << "Генерируемый язык: " << endl;
cout << "(0)+0" << endl;
cout << "0(1)+0" << endl;
string result = analysator(lex7, 5) ? "Цепочка принадлежит грамматике" : "Цепочка не принадлежит грамматике";
cout << endl;
cout << "Результат: " << endl;
cout << result << endl;
}