Sunteți pe pagina 1din 18

# Ministerul Educaiei i tiinei

al Republicii Moldova

## Universitatea Tehnic a Moldovei

Facultatea Calculatoare Informatic i Microelectronic
Tehnologii Informationale

Tema:

A efectuat:
113

A verificat:

lector superior
Duca L.

Chiinu 2012
1

## Tema: Forma Normal Chomsky (FNC)

Sarcina lucrrii:
1. Eliminai producii
2. Eliminai redenumirile
3. Eliminai simbolurile neproductive
4. Eliminai simbolurile inaccesibile
5. Aducei la Forma Normal Chomsky
Varianta 0
G=(VN, VT, P, S) VN=S, A, B, C, D VT=a, b
P=1. SaB
2. SAC
3. Aa
4. AASC
5. ABC
7. Bb
8. Bbs
9. C
10. CBA
11. EaB
12.Dabc
1. Eliminm producii:
a) N
b) pentru producia A
P'=1. SaB
2. SAC
3. SA
4. Aa
5. AASC
6. AAS

N A
N A

11. CBA
12. Dabc
13. Dab}

7. ABC
9. Bb
10. Bbs
2. Eliminm redenumirile:
O producie de forma XY, X i Y neterminale, se numete redenumire.
Redenumirile din P' sunt: SA,AB;
RS S , RA={A,S},RB={B,A}

P''={
1.SaB
2. SAC
3. Sa
4. SASC
5. SAC
6. SBC

8.Aa
9.AASC
10.AAS
11.ABC

## 3. Eliminm simbolurile neproductive:

PROD G A | A V N , A v, v VT

NEPROD G VN \ PROD G

V N S , A, B, C , D

PROD G S , A, B

NEPRODG S, A, B, C, D, E \ S, A, B C, D, E
P'''={
1.SaB
2.SAC
3.Sa
4.SASC
5.SAC
6.SBC
8.Aa
9.AASC
10.AAS
11.ABC
13.Bb
14.Bbs
15.Ba
16.BASC
17.BAS
18.BBC

13.Bb
14.Bbs
15.Ba
16.BASC
17.BAS
18.BBC
20.CBA
21.EaB
23.Dabc
24.Dab

## 4. Eliminm simbolurile inaccesibile:

Iniial ACCES G S
ACCES G x | S x

INACCES G VN VT \ ACCES G

ACCES G a, b, A, C , S , B, D

VN S , A, B, C , D VT a, b

## 5. Aducem la Forma Normal Chomsky

O gramatic n forma normal Chomsky este o gramatic cu reguli de forma A BC,
Di, unde A,B,C,D VN i i VT
PV={1. S X1 X2
2. X1a
3. X2b
4. SAC
5. Sa
6. SyC
7. yAS
8. SAS
9. SBC
10. S X1D
11. Aa
12. AyC
13. AaS
14. ABC
15. A X1D
16. Bb
17. B X2S
18. Ba
19.ByC
20.BAS
21.BBC
22.B X1D

Listingul Programului:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <windows.h>
using namespace std;
int color(int culoare)
{
HANDLE h;
h=GetStdHandle(STD_OUTPUT_HANDLE);
// return SetConsoleTextAttribute(h,culoare);
};
//_______________________________________________
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
};
//_______________________________________________
int wherex()
{
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hStdOut, &csbi);
return csbi.dwCursorPosition.X;
};
int wherey()
{
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hStdOut, &csbi);
return csbi.dwCursorPosition.Y;
};
//_______________________________________________
void constr(char (*l)[10], int *nr);
void constr1(char (*l)[10], int *nr, int i, int j);
void inter(char *s, char *z,int &c);
main()
5

{
int i,j,h,g,nr,n,c,c1,k;
char l[255][10],R[20][10],Ne[125],Vt[20],Vn[20],PR[50],NPR[50],AC[50],NAC[50],f;
color(10);cout << " Dati numarul de Productii: ";
color(11);cout<<endl<<"
n = "; cin >> nr; color(10);
cout <<endl<< " Dati Productiile: "<<endl<<endl;
i=0;
k=1;
do{
init1:
color(8);cout<<k++<<'.';color(11);
j=0;
init:
f=getch();
if(f>='A' && f<='Z'){l[i][j++]=f;cout<<f;}
else goto init;
color(12);
cout<<"->";color(11);
do{
f=getch();
if(f=='\b')
{
putchar(8);
cout << ' ';
putchar(8);
j--;
if(j<=0)
{
gotoxy(1,wherey());
k--;
goto init1;
}
}
if(f=='e'&&j==1)
{
l[i][j++]=f;
cout<<f;
l[i][j]='\0';
break;
}
if(((f>='A' && f<='Z') || (f>='a' && f<='z')) && f!='e')
{
l[i][j++]=f;
cout<<f;
}
else if(f=='\r' && j!=1)
{
l[i][j]='\0';
break;
}
}while(1);
cout<<'\n';
} while(++i<nr);
6

## /*********eliminarea epsilon productii propriu-zisa********/

//determinarea multimii Ne
k=0;
for(i=0;i<nr;i++)
if(l[i][1]=='e') Ne[k++]=l[i][0];
Ne[k]='\0';
do{
c=0;
for(i=0;i<nr;i++)
{
for(j=1;l[i][j];j++)
{
c1=0;
for(h=0;Ne[h];h++)
if(l[i][j]==Ne[h])c1++;
if(c1==0)break;
}
if(c1!=0)
for(h=0;Ne[h];h++)
if(l[i][0]==Ne[h]) c1=0;
if(c1!=0)
{
Ne[k++]=l[i][0];
Ne[k]='\0';c++;
}
}
}while(c!=0);
//eliminarea epsilon productii propriu-zisa
for(i=0;i<nr;i++)
if(l[i][1]=='e')l[i][1]='\0';
constr(l,&nr);
do{
k=nr;
for(h=0;Ne[h];h++)
for(i=0;i<nr;i++)
for(j=1;l[i][j];j++)
if(l[i][j]==Ne[h]) constr1(l,&nr,i,j);
}while(k!=nr);
constr(l,&nr);
k=0;
for(h=0;Ne[h];h++)
if(Ne[h]=='S')k++;
if(k!=0) strcpy(l[nr++],"Se");
color(10);
cout<<endl<<"____________________________________________"<<endl;
cout<<endl<<"\n Productiile obtinute dupa eliminarea \"e
productii\" :"<<endl<<endl;
k=0;
for(i=0;i<nr;i++)
{
7

cout<< "\n";color(8);
cout<<++k<<'.';color(11);cout<< l[i][0];
color(12);cout<< "->";
color(11);cout<< (l[i]+1);
}
getch();
color(10);
cout<<endl<<"____________________________________________"<<endl;
/*********eliminarea redenumirilor********/
//determinarea multimii Vt
k=0;
for(i=0;i<nr;i++)
for(j=0;l[i][j];j++)
if(l[i][j]>='A' && l[i][j]<='Z')
{
c=0;
for(h=0;h<k;h++)
if(Vn[h]==l[i][j])c++;
if(c==0)Vn[k++]=l[i][j];
}
Vn[k]='\0';
for(i=0;Vn[i];i++)
{
R[i][0]=Vn[i];
R[i][1]='\0';
}
do{
k=0;
for(i=0;i<nr;i++)
if(l[i][1]>='A' && l[i][1]<='Z' && l[i][2]=='\0')
for(j=0;Vn[j];j++)
if(R[j][0]==l[i][1])
for(h=0;Vn[h];h++)
if(R[h][0]==l[i][0])
{
inter(R[j],R[h],k);
}
}while(k!=0);
for(i=0;i<nr;i++)
if(l[i][1]>='A' && l[i][1]<='Z' && l[i][2]=='\0')
l[i][1]='\0';
constr(l,&nr);
for(i=0;Vn[i];i++)
for(j=0;j<nr;j++)
if(l[j][0]==Vn[i])
for(h=1;R[i][h];h++)
{
l[nr][0]=R[i][h];
strcpy((l[nr++]+1),(l[j]+1));
}
8

for(i=0;i<nr;i++)
{
c=0;
for(j=0;j<nr;j++)
if(!strcmp(l[i],l[j]))c++;
if(c>1)l[i][1]='\0';}
constr(l,&nr);
cout<<"\n Dupa eliminarea Redenumirilor:"<<endl<<endl;
k=0;
for(i=0;i<nr;i++)
{
color(8);cout<< "\n"<<++k<<'.';
color(11);cout<< l[i][0];
color(12);cout<< "->";
color(11);cout<< (l[i]+1);
}
color(10);
cout<<endl<<"____________________________________________"<<endl;
/*****************Determinarea elementelor neproductive***********/
k=0;
for(i=0;i<nr;i++)
for(j=1;l[i][j];j++)
if(l[i][j]>='a' && l[i][j]<='z' && l[i][j]!='e')
{
c=0;
for(h=0;h<k;h++)
if(Vt[h]==l[i][j])c++;
if(c==0)Vt[k++]=l[i][j];
}
Vt[k]='\0';
getch();
k=0;
do{
c=0;
for(i=0;i<nr;i++)
{
for(j=1;l[i][j];j++)
{
c1=0;
for(h=0;Vt[h];h++) if(l[i][j]==Vt[h])c1++;
for(h=0;h<k;h++) if(l[i][j]==PR[h])c1++;
if(c1!=0) continue;
else {
c1=0;
break;
}
}
if(c1!=0)
for(h=0;h<k;h++) if(l[i][0]==PR[h])c1=0;
if(c1!=0)
{
PR[k++]=l[i][0];
9

c++;
}
}
}while(c!=0);
PR[k]='\0';
//cout<<"\nPR="<<PR;
k=0;
for(i=0;Vn[i];i++)
{
c=0;
for(h=0;PR[h];h++)
if(PR[h]==Vn[i]) c++;
if(c==0)NPR[k++]=Vn[i];
}
NPR[k]='\0';
//cout<<"\nNPR="<<NPR;
for(i=0;i<nr;i++)
for(j=0;l[i][j];j++)
{
for(h=0;NPR[h];h++)
if(l[i][j]==NPR[h])
{
l[i][1]='\0';
goto ies1;
}
ies1: break;
}
constr(l,&nr);
cout<<"\n Dupa eliminarea Elementelor Neproductive:"<<endl<<endl;
k=0;
for(i=0;i<nr;i++)
{
color(8);cout<< "\n"<<++k<<'.';
color(11);cout<< l[i][0];
color(12);cout<< "->";
color(11);cout<< (l[i]+1);
}
getch();color(10);
cout<<endl<<"____________________________________________"<<endl;
/*****************Determinarea elementelor inaccesibile***********/
AC[0]=l[0][0];k=1;
do{
c=0;
for(i=0;i<nr;i++)
for(h=0;h<k;h++)
if(l[i][0]==AC[h])
{
for(j=1;l[i][j];j++)
{
c1=0;
for(g=0;AC[g];g++)
if(l[i][j]==AC[g]) c1++;
if(c1==0)
10

{
AC[k++]=l[i][j];
c++;
}
}break;
}
}while(c!=0);
AC[k]='\0';
//cout<<"\nAC="<<AC;
k=0;
for(i=0;Vn[i];i++)
{
c=0;
for(h=0;AC[h];h++)
if(AC[h]==Vn[i]) c++;
if(c==0)NAC[k++]=Vn[i];
}
for(i=0;Vt[i];i++)
{
c=0;
for(h=0;AC[h];h++)
if(AC[h]==Vt[i]) c++;
if(c==0)NAC[k++]=Vt[i];
}
NAC[k]='\0';color(12);
cout<<"\n
NAC="<<NAC<<endl;
for(i=0;i<nr;i++)
for(j=0;l[i][j];j++)
{
for(h=0;NAC[h];h++)
if(l[i][j]==NAC[h])
{
l[i][1]='\0';
goto ies;
}
ies:break;
}
constr(l,&nr);color(10);
cout<<"\n Dupa eliminarea Elementelor Inaccesibile:"<<endl<<endl;
k=0;
for(i=0;i<nr;i++)
{
color(8);cout<< "\n"<<++k<<'.';
color(11);cout<< l[i][0];
color(12);cout<< "->";
color(11);cout<< (l[i]+1);
}
getch();
cout<<endl<<"____________________________________________"<<endl;
/*****************FNC***********/
color(12);cout<<"\n
FNC:"<<endl<<endl;
11

f='A';
for(i=0;Vn[i];i++);k=i;
for(i=0;i<nr;i++)
if(l[i][2]!='\0')
for(j=1;l[i][j];j++)
if(l[i][j]>='a' && l[i][j]<='z')
{
do{
c=0;
for(h=0;Vn[h];h++)
if(f==Vn[h])
{
c++;
f++;
break;
}
}while(c!=0);
if(f>'Z')
{
color(12);cout<<"\nInsuficienta de litere ! ! !"<<endl;
getch();
exit(0);
}
l[nr][0]=f;
l[nr][1]=l[i][j];
l[nr][2]='\0';
nr++;
for(g=i;g<nr;g++)
if(l[g][2]!='\0')
for(h=1;l[g][h];h++)
if(l[g][h]==l[i][j] && (g!=i || h!=j))l[g][h]=f;
l[i][j]=f;
Vn[k++]=f;
Vn[k]='\0';
}
color(10);cout<<"\n Pasul 1:"<<endl<<endl;
c=0;
for(g=0;g<nr;g++)
{
color(8);cout<< "\n"<<++c<<'.';
color(11);cout<< l[g][0];
color(12);cout<< "->";
color(11);cout<< (l[g]+1);
}
getch();color(10);
cout<<endl<<"____________________________________________"<<endl;
cout<<"\n Pasul 2:"<<endl<<endl;
do{
n=0;
for(i=0;i<nr;i++)
if(l[i][2]!='\0' && l[i][3]!='\0')
{
12

n++;
c=0;
do{
c=0;
for(h=0;Vn[h];h++)
if(f==Vn[h])
{
c++;
f++;
break;
}
}while(c!=0);
if(f>'Z')
{
color(12);cout<<"\nInsuficienta de litere ! ! ! "<<endl;
getch();
exit(0);
}
l[nr][0]=f;
for (g=2;l[i][g];g++)
l[nr][g-1]=l[i][g];
l[nr][g-1]='\0';
for (h=i+1;h<nr;h++)
{
c=0;
for (j=2;l[i][j];j++)
if (l[i][j]!=l[h][j])
{
c=1;
break;
}
if (c==0)
{
l[h][2]=f;
l[h][3]='\0';
}
}
l[i][2]=f;
l[i][3]='\0';
nr++;
Vn[k++]=f;
Vn[k]='\0';
}
}while(n!=0);
h=0;
for(g=0;g<nr;g++)
{
color(8);cout<< "\n"<<++h<<'.';
color(11);cout<< l[g][0];
color(12);cout<< "->";
color(11);cout<< (l[g]+1);
}
getch();
13

return 0;
}
void constr(char (*l)[10], int *nr){
int i,j,h;
for(i=0;i<*nr;i++)
if(l[i][1]=='\0'){
h=i;
for(j=h+1;j<*nr;j++,h++)
strcpy(l[h],l[j]);
(*nr)--;
i--;
}}
void constr1(char (*l)[10], int *nr, int i, int j){
int h;
strcpy(l[*nr],l[i]);
for(h=j;l[*nr][h];h++)
l[*nr][h]=l[*nr][h+1];
h=0;
for(i=0;i<*nr;i++)
if(strcmp(l[i],l[*nr])==0)h++;
if(h==0)(*nr)++;}
void inter(char *s, char *z,int &c){
int i,j,h,k,k1;
for(i=0;s[i];i++)
k1=i;
for(j=0;z[j];j++)
{
k=0;
for(h=0;s[h];h++)
if(s[h]==z[j])k++;
if(k==0)
{
s[++k1]=z[j];
c++;
}}
s[++k1]='\0';}
Productiile obtinute dupa eliminarea e productii:

14

15

16

Pasul2:

17

18