include iostream include string using namespace std bool analys char l

 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
#include <iostream>
#include <string>
using namespace std;
bool analys(char lex[], int n)
{
int STATE = 1;
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 1:
cout << 1 << " ";
if (lex[i] == '0')
STATE = 2;
else
return false;
break;
case 2:
cout << 2 << " ";
if (lex[i] == '1')
break;
if (lex[i] == '0')
STATE = 1;
else
return false;
break;
}
}
return true;
}
int main()
{
setlocale(LC_ALL, "Russian");
char lex1[] = {'1', '0', '0', '0', '1', '0', '0', '1', '0'};
char lex2[] = {'0', '1', '1', '1', '1', '1', '1', '1', '0'};
char lex3[] = {'0', '0', '0', '0', '0', '0', '0', '0', '0'};
char lex4[] = {'0', '0', '0', '0', '0', '0', '0', '0', '1'};
cout << "Грамматика: " << endl;
cout << "S -> B0|C0" << endl;
cout << "B -> B0|0" << endl;
cout << "C -> C1|A1" << endl;
cout << "A -> 0" << endl;
cout << "Генерируемый язык: " << endl;
cout << "(00)*" << endl;
cout << "0(11)*0" << endl;
string result = analys(lex2, 9) ? "Цепочка принадлежит грамматике" : "Цепочка не принадлежит грамматике";
cout << endl;
cout << "Результат: " << endl;
cout << result << endl;
}