#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;
}