Ministerul Educaţiei şi Ştiinţei

al Republicii Moldova

Universitatea Tehnică a Moldovei

Lucrare de laborator
nr.3
la Limbaje Formale şi Proiectarea Compilatoarelor

Tema:

Forma Chomsky

Varianta 9
A efectuat:

st.grupei FAF-131
Chele Natalia

A verificat:

Irina Cojocaru

Sarcina lucrării:
1. Eliminaţi ε producţii

Eliminăm redenumirile: O producţie de forma XY. A->a 5. 8. AaS 6.2. BA 8.B->aS. DAB } 2. 11. Eliminăm ε producţii: pentru producţia C ε P'=1. A->a 4. 7. BA P''={ 1SbbS. BbS 9. SbA 2. 5. BbS 8. 6. Redenumirile din P' sunt: SB.B->a. P. CAB 11. S) VN=S. CAB 11. SBC 3.S 4. Eliminaţi redenumirile 3. C. X şi Y neterminale.C->AB.B->aAa. SBC 3.A->bAaAb. se numeşte redenumire.S->BC 4. A. B. BA 7. VT. Eliminaţi simbolurile inaccesibile 4.B->bAaAb. A bAaAb 7. Aduceţi la Forma Normală Chomsky Varianta 9 G=(VN. D VT=a. A bAaAb 6. DAB 1. AaS 5. 9. . 12. SbA 2. Eliminaţi simbolurile neproductive 5.D->AB. BaAa 9.A->aS. b P=1. BaAa 10. C-> ε 10.A->a. 10. 2.

12.S->BC 4. 12.A->aS. S->YS.D->AB. S->a.A->bAaAb. 6.A->a. 9. Z->AX. S->XZ.3. 7. A->XS. 8. 5. 8. Eliminăm simbolurile neproductive: P''={ 1SbbS. 11.B->aS. A->a. B->XZ. B->XS. B->a.B->a. . S->aAa.B->aAa. Y->b. 10.D->AB.A->aS. 7.Aducem la Forma Normală Chomsky O gramatică оn forma normală Chomsky este o gramatică cu reguli de forma PV= { S->bA. X->a. S->BC. 5. S->XS. Z3->AY. B->YS. S->bAaAb. 5. S->XZ1. S->YA.B->bAaAb. 6. Z1->AZ2.C->AB.B->aS. A->YZ1. 2.C->AB. 2. 9.A->bAaAb.A->a.B->bAaAb.B->a.B->aAa. Eliminăm simbolurile inaccesibile: Iniţial '' P ={ 1SbbS. 10. 4.S->BC 4. Z2->XZ3. B->YZ1. 11.

char *z. init: f=getch(). do{ init1: cout<<k++<<'. cout << "Dati numarul de productii nr=". if(f=='\b') { putchar(8). cout << ' '. int *nr. system("cls").} else goto init.cout<<f. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE).Ne[125]. void gotoxy(short x.c1. int i.PR[50]. cout<<"->". j=0. putchar(8). . short y) { COORD pos = {x. void inter(char *s.R[20] [10]. void constr(char (*l)[10]. cin >> nr.f.h> #include <dos. i=0. char l[255][10].h> #include <stdlib. int j). y}.AC[50].Vt[20].'.int &c).h> #include <windows.h. void constr1(char (*l)[10]. pos). } main() { int i. //Defines gotoxy() for ANSI C compilers. int *nr).h> #include <stdio.k.c.C->AB. if(f>='A' && f<='Z'){l[i][j++]=f.h> #include <string.h> #include <iostream> #include<iomanip> using namespace std.g. cout << "Dati productiile posibile Vn->(Vn+Vt)*\n".NAC[50].ies1.Vn[20]. } Listingul programului: #include <conio.nr.NPR[50].n.j. k=1. do{ f=getch().

if(c1==0)break.i<nr.c++. for(i=0. cout<<f.&nr). do{ c=0.i<nr.h++) if(l[i][0]==Ne[h]) c1=0. cout<<f.i++) if(l[i][1]=='e')l[i][1]='###BOT_TEXT###'. if(j<=0) { k--. } if(c1!=0) for(h=0.j++) { c1=0. for(h=0.i++) { for(j=1.Ne[h]. } else if(f=='\r' && j!=1) { l[i][j]='###BOT_TEXT###'.l[i][j]. //eliminarea epsilon productii propriu-zisa for(i=0. .i++) if(l[i][1]=='e') Ne[k++]=l[i][0]. } if(((f>='A' && f<='Z') || (f>='a' && f<='z')) && f!='e') { l[i][j++]=f. if(c1!=0) { Ne[k++]=l[i][0].j--. l[i][j]='###BOT_TEXT###'. /*********eliminarea epsilon productii propriu-zisa********/ //determinarea multimii Ne k=0. } while(++i<nr).h++) if(l[i][j]==Ne[h])c1++. for(i=0.i<nr. break. constr(l. Ne[k]='###BOT_TEXT###'. } }while(1). } } }while(c!=0). break. } } if(f=='e'&&j==1) { l[i][j++]=f.Ne[h]. goto init1. Ne[k]='###BOT_TEXT###'. cout<<'\n'.

h++) if(Ne[h]=='S')k++.i.i++) for(j=1.j++) if(R[j][0]==l[i][1]) for(h=0.Vn[i]. if(k!=0) strcpy(l[nr++]. for(i=0. R[i][1]='###BOT_TEXT###'.i++) for(j=0.Vn[j].(l[j]+1)).'<< l[i][0]<< "->"<< (l[i]+1).j++) if(l[i][j]==Ne[h]) constr1(l.Ne[h]. /*********eliminarea redenumirilor********/ //determinarea multimii Vt k=0.l[i][j].i++) { R[i][0]=Vn[i]. cout<<"\nProductiile obtinute dupa eliminarea ###BOT_TEXT###quot;e productii###BOT_TEXT###quot;".j<nr. for(h=0. for(h=0.h++) if(R[h][0]==l[i][0]) { inter(R[j].&nr.i++) { cout<< "\n"<<++k<<'.j++) if(l[i][j]>='A' && l[i][j]<='Z') { c=0.k).i<nr. strcpy((l[nr++]+1). } Vn[k]='###BOT_TEXT###'.&nr).i<nr. k=0. } }while(k!=0). for(i=0. for(i=0.l[i][j].i<nr. for(h=0.R[h].j). if(c==0)Vn[k++]=l[i][j].h++) { l[nr][0]=R[i][h].i++) if(l[i][1]>='A' && l[i][1]<='Z' && l[i][2]=='###BOT_TEXT###') l[i][1]='###BOT_TEXT###'. constr(l.h<k.j++) if(l[j][0]==Vn[i]) for(h=1.h++) for(i=0.R[i][h]. k=0.i<nr. for(i=0. } getch().do{ k=nr.Vn[i]. }while(k!=nr).Ne[h]. } do{ k=0."Se"). for(i=0.i++) if(l[i][1]>='A' && l[i][1]<='Z' && l[i][2]=='###BOT_TEXT###') for(j=0.h++) if(Vn[h]==l[i][j])c++. constr(l.&nr). for(i=0.Vn[h].i<nr. } .i++) for(j=0.

c++.i<nr.i<nr.NAC[h].l[i][j]. if(c>1)l[i][1]='###BOT_TEXT###'. cout<<"\nProductiile obtinute dupa eliminarea epsilon productii si redenumirilor". for(h=0.i<nr. k=0.i++) { c=0. if(c1==0) { AC[k++]=l[i][j].AC[h].j++) { for(h=0.AC[g]. } }while(c!=0).i++) { cout<< "\n"<<++k<<'. for(i=0.j<nr.h<k.AC[h]. } }break. for(h=0. AC[k]='###BOT_TEXT###'.h++) if(AC[h]==Vt[i]) c++.'<< l[i][0]<< "->"<< (l[i]+1).l[i][j].&nr). for(g=0.Vn[i].i++) { c=0.h++) if(l[i][j]==NAC[h]) { . for(i=0.g++) if(l[i][j]==AC[g]) c1++. } NAC[k]='###BOT_TEXT###'. if(c==0)NAC[k++]=Vn[i]. do{ c=0.j++) if(!strcmp(l[i]. if(c==0)NAC[k++]=Vt[i].j++) { c1=0. for(i=0. } for(i=0.for(i=0.h++) if(l[i][0]==AC[h]) { for(j=1. //cout<<"\nAC="<<AC.i<nr.i++) for(j=0. cout<<"\nNAC="<<NAC. for(i=0.Vt[i].} constr(l.l[j]))c++. } /*****************Determinarea elementelor inaccesibile***********/ AC[0]=l[0][0]. k=0.h++) if(AC[h]==Vn[i]) c++.i++) for(h=0. for(j=0.i++) { c=0.k=1.

} } }while(c!=0). } ies:break.j++) { c1=0.h++) if(l[i][j]==Vt[h])c1++. cout<<"\nProductiile obtinute dupa eliminarea elementelor inaccesibile".l[i][j]. } } if(c1!=0) for(h=0.h<k. //cout<<"\nPR="<<PR. k=0. //cout<<"\nNPR="<<NPR.h++) if(PR[h]==Vn[i]) c++. } NPR[k]='###BOT_TEXT###'. for(h=0.i++) { cout<< "\n"<<++k<<'. if(c==0)Vt[k++]=l[i][j]. for(i=0.h++) if(Vt[h]==l[i][j])c++.i++) { for(j=1.h++) if(l[i][j]==PR[h])c1++. for(i=0. /*****************Determinarea elementelor neproductive***********/ k=0.i<nr. } getch(). } Vt[k]='###BOT_TEXT###'.h<k. c++. else { c1=0.PR[h].i++) for(j=1. goto ies.i++) .h<k. do{ c=0.Vn[i]. } constr(l.i++) { c=0. if(c1!=0) continue.Vt[h]. for(h=0. PR[k]='###BOT_TEXT###'. k=0.&nr). for(h=0.j++) if(l[i][j]>='a' && l[i][j]<='z' && l[i][j]!='e') { c=0. if(c==0)NPR[k++]=Vn[i].l[i][j].i<nr.i<nr. if(c1!=0) { PR[k++]=l[i][0]. break.i<nr. for(i=0. for(i=0. getch().l[i][1]='###BOT_TEXT###'.'<< l[i][0]<< "->"<< (l[i]+1). for(h=0. k=0. for(i=0.h++) if(l[i][0]==PR[h])c1=0.

} constr(l. } . l[i][j]=f.Vn[h].k=i. for(h=0.i<nr.'<< l[i][0]<< "->"<< (l[i]+1). Vn[k]='###BOT_TEXT###'. c=0. /*****************FNC***********/ cout<<"\nFNC". } l[nr][0]=f. cout<<"\nProductiile obtinute dupa eliminarea elementelor neproductive". k=0. } cout<<"\nPasul 1". getch().i++) { cout<< "\n"<<++k<<'. } getch(). for(g=i.h++) if(l[g][h]==l[i][j] && (g!=i || h!=j))l[g][h]=f.g<nr. for(i=0.i++) if(l[i][2]!='###BOT_TEXT###') for(j=1. l[nr][2]='###BOT_TEXT###'. nr++.g<nr.Vn[i].h++) if(l[i][j]==NPR[h]) { l[i][1]='###BOT_TEXT###'. break. goto ies1.for(j=0. for(g=0.j++) if(l[i][j]>='a' && l[i][j]<='z') { do{ c=0. f++. for(i=0. for(i=0. } }while(c!=0). Vn[k++]=f.'<< l[g][0]<< "->"<< (l[g]+1). if(f>'Z') { cout<<"\nInsuficienta de litere".l[g][h].i<nr.NPR[h].i++). exit(0).h++) if(f==Vn[h]) { c++.l[i][j].j++) { for(h=0.g++) if(l[g][2]!='###BOT_TEXT###') for(h=1. } ies1:break.g++) { cout<< "\n"<<++c<<'. f='A'. l[nr][1]=l[i][j].&nr).l[i][j].

exit(0). } }while(n!=0). } if (c==0) { l[h][2]=f.h++) if(f==Vn[h]) { c++.h++) { c=0. getch(). for (g=2. f++. nr++.l[i][j].g<nr.Vn[h]. for (j=2. } } l[i][2]=f. for(i=0. int *nr){ . c=0.'<< l[g][0]<< "->"<< (l[g]+1).g++) l[nr][g-1]=l[i][g]. do{ c=0. } }while(c!=0). return 0. cout<<"\nPasul 2". l[h][3]='###BOT_TEXT###'. break. } void constr(char (*l)[10]. for(g=0. l[i][3]='###BOT_TEXT###'.h<nr. do{ n=0. Vn[k++]=f.getch(). break.l[i][g]. if(f>'Z') { cout<<"\nInsuficienta de litere". } l[nr][0]=f.i<nr. for(h=0. h=0. } getch(). Vn[k]='###BOT_TEXT###'.i++) if(l[i][2]!='###BOT_TEXT###' && l[i][3]!='###BOT_TEXT###') { n++.g++) { cout<< "\n"<<++h<<'. l[nr][g-1]='###BOT_TEXT###'.j++) if (l[i][j]!=l[h][j]) { c=1. for (h=i+1.

l[*nr])==0)h++. .l[i]).producţiile. } } s[++k1]='###BOT_TEXT###'.h++) strcpy(l[h].l[*nr][h].s[i].j.i++) if(strcmp(l[i]. redenumirile . if(k==0) { s[++k1]=z[j]. Apoi conform algoritmului am realizat cei doi paşi de transformare a gramaticii оn FNC.j++) { k=0.i++) if(l[i][1]=='###BOT_TEXT###'){ h=i.k1.h++) l[*nr][h]=l[*nr][h+1]. (*nr)--. i--. for(i=0.i<*nr. h=0. Pentru aceasta am eliminat . for(h=j. int j){ int h. } Concluzie: Efectuind această lucrare practică am simplificat o gramatică şi am adus-o la forma normală Chomsky.i<*nr. } void constr1(char (*l)[10].} int i. elementele neproductive şi inaccesibile. for(j=h+1. for(i=0.j++.i++) k1=i. for(j=0.int &c){ int i.z[j].l[j]). strcpy(l[*nr]. char *z.j<*nr. c++.k.j.h.s[h]. } void inter(char *s. int *nr.h++) if(s[h]==z[j])k++. int i.h. for(h=0. for(i=0. if(h==0)(*nr)++.