avatar
Untitled

Oaspete 50 10th May, 2019

                                           
                         #include "stdafx.h"
#include <iostream>
#include <string.h>

using namespace std;

//tip 2
char* N = "SAB";
char* T = "abl";
char* P[][2] = {
	{ "S", "aA" },
	{ "A", "aA" },
	{ "A", "bB" },
	{ "B", "l" }
};
char S = 'S';


void afisareComponenteGramatica(char* N, char* T, char* P[][2], int nP, int S)
{
	std::cout << " N: "; for (int i = 0; i < strlen(N); ++i){std::cout << N[i] << " ";}
	
	std::cout << endl << " T: ";

	for (int i = 0; i < strlen(T); ++i)
	{
		std::cout << T[i] << " ";
	}
	std::cout << endl << "P: " << endl;
	for (int i = 0; i < nP; ++i)
	{
		std::cout << P[i][0] << " -> " << P[i][1] << endl;
	}
	std::cout << " S: " << S << " " << endl;
}


int tipGramatica(char* N, char* T, char* P[][2], char S, int nP) 
{
	int i,l,r;

	for (i = 0; i < nP; ++i)
	{
		if (strlen(P[i][0]) != 1 || strlen(P[i][1]) > 2)break;
		if (strlen(P[i][1]) == 1 && strchr(T, P[i][1][0]))continue;
		if (strlen(P[i][1]) == 2 && strchr(T, P[i][1][0]) && strchr(N, P[i][1][1]))continue;
		if (strlen(P[i][1]) == 2 && strchr(N, P[i][1][0]) && strchr(T, P[i][1][1]))continue;
		break;
	}
	if (i == nP)return 3;

	for (i = 0; i < nP; ++i)if (strlen(P[i][0]) != 1 || strchr(N, P[i][0][0]) == 0)break;if (i == nP)return 2;

	for (i = 0;i < nP;++i)if(strlen(P[i][0]) != 1 || strchr(N, P[i][0][0]) == 0)break;if (i == nP)return 2;

	for (i = 0;i < nP;++i)
	{
		l = 0, r = 0;
		while (l + r < strlen(P[i][0]) && l + r < strlen(P[i][1]))
		{
			if (P[i][0][l] == P[i][1][l])l++;
			else if ((P[i][0][strlen(P[i][0])] - i - r) == (P[i][l][strlen(P[i][1]) - l - r]))r++;
			else break;
		}
		if (!(l + r + 1 == strlen(P[i][0]) && strchr(N, P[i][0][l])))break;
	}
	if (i == nP)return 1;


	return 0;
}


bool isChomsky(char *N, char* T, char *P[][2], int nP, char S)
{
	for (int i = 0; i < nP;++i)
	{
		if (strlen(P[i][0]) != 1 || strlen(P[i][1]) > 2)return false;
		if (strlen(P[i][1]) == 2 && strchr(N,P[i][1][0]) && strchr(N,P[i][1][1]))continue;
		if (strlen(P[i][1]) == 1 && strchr(T, P[i][1][0]))continue;
		if (P[i][0][0] == 3 && strlen(P[i][1]) == 0) continue;
		return false;
	}
	return true;
}


bool isGreibach(char *N, char* T, char *P[][2], int nP, char S)
{
	for (int i = 0; i < nP;++i)
	{
		if (strlen(P[i][0]) != 1)return false;
		if (strlen(P[i][1]) == 2 && strchr(T, P[i][1][0]))continue;
		if (P[i][0][0] == 3 && strlen(P[i][1]) == 0)continue;
		return false;
	}
	return true;
}

bool isOperator(char *N, char* T, char *P[][2], int nP, char S)
{
	for (int i = 0; i < nP;++i)
	{
		if (strlen(P[i][0]) != 1)return false;
		for (int j = 1;j < strlen(P[i][1]);++j)if (strchr(N, P[i][1][j - 1]) && strchr(N, P[i][1][j]))return false;
	}
	return true;
}




int main()
{
	afisareComponenteGramatica(N, T, P, 4,S);
	cout << "Gramatica este de tip " << tipGramatica(N, T, P, S, 4) << "." << endl;

	cout << endl;
	if (isChomsky(N, T, P, 4, S) == true)
	{
		cout << "Este Chomsky" << endl;
	}
	else
	{
		cout << "NU este Chomsky" << endl;
	}
	cout << endl;
	if (isGreibach(N, T, P, 4, S) == true)
	{
		cout << "Este Greibach" << endl;
	}
	else
	{
		cout << "NU este Greibach" << endl;
	}
	cout << endl;
	if (isOperator(N, T, P, 4, S) == true)
	{
		cout << "Este Operator" << endl;
	}
	else
	{
		cout << "NU este Operator" << endl;
	}

	return 0;
}
                      
                                       
Pentru a distribui această postare, copiați această adresă URL și trimiteți-o prietenilor
RAW Paste Data