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
58
59
60
61
62
63
64
65
#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 = 3;
else
return false;
break;
case 3:
cout << 3 << " ";
if (i == n-1)
return true;
else
return false;
}
}
}
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 << "(00)*" << endl;
cout << "0(1)*0" << endl;
string result = analys(lex7, 5) ? "Цепочка принадлежит грамматике" : "Цепочка не принадлежит грамматике";
cout << endl;
cout << "Результат: " << endl;
cout << result << endl;
}