Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- FILE *fisier;
- char linie[100], atom1[30], tipAtom1[6], atom2[30], tipAtom2[6], cuvinteCheie[16][15];
- int pozitie, numarCuvinteCheie, liniaCurenta=1, penultimaIndentare, ultimaIndentare, ultimulIdentificator=-1, numarIdentificatori, esteIndiceAccesat;
- struct identificator
- {
- char nume[30];
- char tip[6];
- }id[40];
- void analizeaza();
- void preiaCuvinteCheie();
- void urmatorulAtom();
- int esteCuvantCheie(char *sir);
- int pozitieIdentificator(char *sir);
- void adaugaIdentificator(char *sir);
- void Eroare(char *mesaj);
- void Atentionare(char *mesaj);
- void treciPesteSpatiiOptionale();
- int A(int indentare, int obligatoriu);
- int B(int indentare);
- int C(int indentare,int flag);
- int D(int indentare);
- int E(int flag1, int flag2, int flag3);
- int F(int indentare);
- int G(int indentare);
- int H();
- int I();
- int K(int indentare);
- int L(int indentare);
- int M();
- int N();
- int O();
- int P();
- int Q();
- int R();
- int S(int indentare);
- int T();
- int U(int indentare);
- int V(int indentare);
- int W();
- int X();
- int Y(int indentare);
- int Z(int indentare, int obligatoriu);
- int en();
- int e0();
- int e1();
- int e2();
- int ec();
- int co();
- int coa();
- int in();
- int main()
- {
- analizeaza();
- return 0;
- }
- void analizeaza()
- {
- preiaCuvinteCheie();
- fisier=fopen("cod.txt","r");
- fgets(linie,100,fisier);
- urmatorulAtom();
- if(A(0,1)>0)
- puts("Programul nu are erori.");
- fclose(fisier);
- }
- void preiaCuvinteCheie()
- {
- FILE *f;
- int p=0,c;
- f=fopen("cuvinteCheie.txt","r");
- while(!feof(f))
- {
- c=fgetc(f);
- if(c!='\n')
- cuvinteCheie[numarCuvinteCheie][p++]=c;
- else
- {
- numarCuvinteCheie++;
- p=0;
- }
- }
- fclose(f);
- }
- void urmatorulAtom()
- {
- int p=pozitie;
- if(strcmp(tipAtom2,"spati"))
- {
- strcpy(atom1,atom2);
- strcpy(tipAtom1,tipAtom2);
- if(!strcmp(tipAtom2,"ranou"))
- {
- fgets(linie,100,fisier);
- if(!feof(fisier))
- p=pozitie=0;
- else
- return ;
- liniaCurenta++;
- ultimaIndentare=0;
- }
- }
- if(isalpha(linie[p]))
- {
- strcpy(tipAtom2,"ident");
- while(isalnum(linie[p]))
- p++;
- }
- else if(isdigit(linie[p]))
- {
- strcpy(tipAtom2,"nripo");
- while(isdigit(linie[p]))
- {
- p++;
- if(linie[p]=='.')
- {
- p++;
- strcpy(tipAtom2,"nrrpo");
- }
- }
- }
- else if(linie[p]=='-'&&isdigit(linie[p+1])&&!strstr("nripo,nrine,nrrpo,nrrne,ident",tipAtom1)&&strcmp(atom1,")"))
- {
- strcpy(tipAtom2,"nrine");
- p++;
- while(isdigit(linie[p]))
- {
- p++;
- if(linie[p]=='.')
- {
- p++;
- strcpy(tipAtom2,"nrrne");
- }
- }
- }
- else if(strchr("()[]{}",linie[p]))
- {
- strcpy(tipAtom2,"paran");
- p++;
- }
- else if(strchr("+-*/%",linie[p]))
- {
- strcpy(tipAtom2,"opari");
- p++;
- }
- else if(strchr("!=<>",linie[p]))
- {
- strcpy(tipAtom2,"oprel");
- p++;
- if(strchr("!<>",linie[p-1])&&linie[p]=='=')
- p++;
- }
- else if(linie[p]==' '||linie[p]=='\t')
- {
- strcpy(tipAtom2,"spati");
- while(linie[p]==' '||linie[p]=='\t')
- p++;
- if(pozitie==0)
- ultimaIndentare=p-pozitie;
- }
- else if(linie[p]==',')
- {
- strcpy(tipAtom2,"virgu");
- p++;
- }
- else if(linie[p]=='"')
- {
- strcpy(tipAtom2,"sirca");
- p++;
- while(linie[p]!='"')
- if(linie[p]=='\n')
- {
- Eroare("lipsesc \" de sfarsit");
- break;
- }
- else
- p++;
- if(linie[p]=='"')
- p++;
- }
- else if(linie[p]=='\n')
- {
- strcpy(tipAtom2,"ranou");
- ultimaIndentare=0;
- }
- else
- {
- strcpy(tipAtom2,"necun");
- p++;
- }
- //printf("%d %d\n",pozitie,p);
- strncpy(atom2,linie+pozitie,p-pozitie);
- atom2[p-pozitie]=0;
- if(esteCuvantCheie(atom2))
- strcpy(tipAtom2,"cheie");
- pozitie=p;
- /*
- if(!strcmp(tipAtom2,"ranou"))
- puts("rand");
- else
- puts(atom2);
- */
- }
- int esteCuvantCheie(char *sir)
- {
- int i;
- for(i=0;i<numarCuvinteCheie;i++)
- if(!strcmp(sir,cuvinteCheie[i]))
- return 1;
- return 0;
- }
- int pozitieIdentificator(char *sir)
- {
- int i;
- for(i=0;i<numarIdentificatori;i++)
- if(!strcmp(sir,id[i].nume))
- return i;
- return -1;
- }
- void adaugaIdentificator(char *sir)
- {
- int i;
- for(i=0;i<numarIdentificatori;i++)
- if(!strcmp(sir,id[i].nume))
- {
- ultimulIdentificator=i;
- return ;
- }
- strcpy(id[ultimulIdentificator=numarIdentificatori++].nume,sir);
- }
- void Eroare(char *mesaj)
- {
- printf("Eroare la linia %d: %s.\n",liniaCurenta,mesaj);
- }
- void Atentionare(char *mesaj)
- {
- printf("Atentionare la linia %d: %s.\n",liniaCurenta,mesaj);
- }
- void treciPesteSpatiiOptionale()
- {
- if(!strcmp(tipAtom2,"spati"))
- urmatorulAtom();
- }
- int A(int indentare, int obligatoriu)
- {
- int o;
- if(ultimaIndentare>indentare)
- {
- Eroare("indentare neasteptata");
- return -1;
- }
- else if((o=B(indentare))>0)
- return 1;
- else if(o<0)
- return -1;
- else if((o=D(indentare))>0)
- return 1;
- else if(o<0)
- return -1;
- else if((o=G(indentare))>0)
- return 1;
- else if(o<0)
- return -1;
- else if((o=K(indentare))>0)
- return 1;
- else if(o<0)
- return -1;
- else if((o=L(indentare))>0)
- return 1;
- else if(o<0)
- return -1;
- else if((o=V(indentare))>0)
- return 1;
- else if(o<0)
- return -1;
- else if(obligatoriu)
- {
- Eroare("lipseste comanda");
- return -1;
- }
- return 0;
- }
- int B(int indentare)
- {
- if(!strcmp(atom2,"Preia"))
- {
- urmatorulAtom();
- if(!strcmp(tipAtom2,"spati"))
- {
- urmatorulAtom();
- if(E(1,1,1)>0)
- {
- if(C(indentare,1)>=0)
- return 1;
- else
- return -1;
- }
- else
- {
- return -1;
- }
- }
- else
- {
- Eroare("lipseste spatiu dupa cuvantul cheie 'Preia'");
- return -1;
- }
- }
- else
- return 0;
- }
- int C(int indentare,int flag)
- {
- treciPesteSpatiiOptionale();
- if(!strcmp(tipAtom2,"virgu"))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- if(!flag&&!strcmp(atom2,"sir"))
- {
- Atentionare("nu este nevoie de cuvantul cheie 'sir' pentru a afisa o variabila de tip sir de caractere");
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- }
- if(E(flag,1,1)>0)
- {
- return C(indentare,flag);
- /*
- if(C(indentare,flag)>0)
- return 1;
- else
- return -1;
- */
- }
- else
- {
- return -1;
- }
- }
- else if(!strcmp(tipAtom2,"ranou"))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- return Z(indentare,0);
- /*
- int o;
- if((o=Z(indentare))>0)
- return 1;
- else if(o==0)
- return 0;
- else
- return -1;
- */
- }
- else if(!strcmp(atom2,";"))
- {
- Atentionare("nu este nevoie de ; la sfarsitul unei instructiuni");
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- return C(indentare,flag);
- }
- else
- {
- char s[50];
- sprintf(s,"cuvant necunoscut: '%s' ",atom2);
- Eroare(s);
- return -1;
- }
- }
- int D(int indentare)
- {
- if(!strcmp(atom2,"Afiseaza"))
- {
- urmatorulAtom();
- if(!strcmp(tipAtom2,"spati"))
- {
- urmatorulAtom();
- if(!strcmp(tipAtom2,"sirca")||E(0,1,1)>0)
- {
- if(!strcmp(tipAtom2,"sirca"))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- }
- if(C(indentare,0)>=0)
- return 1;
- else
- return -1;
- }
- else
- return -1;
- }
- else
- {
- Eroare("lipseste spatiu dupa cuvantul cheie 'Afiseaza'");
- return -1;
- }
- }
- else
- return 0;
- }
- int E(int flag1, int flag2, int flag3)
- {
- char s[50];
- int o=0,p;
- esteIndiceAccesat=0;
- if(!strcmp(atom2,"sir"))
- {
- o=1;
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- }
- if(!strcmp(tipAtom2,"ident"))
- {
- if(flag1)
- {
- adaugaIdentificator(atom2);
- p=ultimulIdentificator;
- }
- else
- {
- int p=pozitieIdentificator(atom2);
- if(p<0)
- {
- sprintf(s,"variabila '%s' nu a fost declarata",atom2);
- Eroare(s);
- return -1;
- }
- else
- ultimulIdentificator=p;
- }
- urmatorulAtom();
- if(!strcmp(atom2,"("))
- {
- while(!strcmp(atom2,"("))
- {
- urmatorulAtom();
- if(en())
- {
- if(!strcmp(atom2,")"))
- {
- urmatorulAtom();
- if(flag1)
- strcpy(id[p].tip,"colec");
- esteIndiceAccesat=1;
- }
- else
- {
- Eroare("lipseste )");
- return -1;
- }
- }
- else
- {
- Eroare("indicele colectiei trebuie sa fie expresie numerica");
- return -1;
- }
- /*
- if(!strcmp(tipAtom2,"ident"))
- {
- int p=pozitieIdentificator(atom2);
- if(p>=0)
- {
- if(strstr("nmsir,numar,sirca",id[p].tip))
- {
- urmatorulAtom();
- if(!strcmp(atom2,")"))
- {
- urmatorulAtom();
- if(flag1)
- strcpy(id[ultimulIdentificator].tip,"colec");
- }
- else
- {
- Eroare("lipseste )");
- return -1;
- }
- }
- else
- {
- sprintf(s,"variabila indice %s nu poate fi colectie de date",atom2);
- Eroare(s);
- return -1;
- }
- }
- else
- {
- sprintf(s,"variabila indice %s nu a fost declarata",atom2);
- Eroare(s);
- return -1;
- }
- }
- else
- {
- Eroare("lipseste indicele colectiei");
- return -1;
- }
- */
- }
- return 1;
- }
- else
- {
- if(flag1)
- {
- if(o==0)
- strcpy(id[ultimulIdentificator].tip,"numar");
- else if(o==1)
- strcpy(id[ultimulIdentificator].tip,"sirca");
- }
- else if (flag3)
- {
- int p=pozitieIdentificator(atom1);
- if(!strcmp(id[p].tip,"colec"))
- {
- sprintf(s,"lipseste referirea la un element al variabilei colectie '%s'",atom1);
- Eroare(s);
- return -1;
- }
- }
- }
- return 1;
- }
- else if(flag2)
- {
- if(flag1)
- Eroare("lipseste un nume de variabila");
- else
- Eroare("lipseste un nume de variabila sau un sir de caractere");
- return -1;
- }
- return 0;
- }
- int F(int indentare)
- {
- treciPesteSpatiiOptionale();
- if(!strcmp(tipAtom2,"virgu"))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- if(H()>0)
- {
- return F(indentare);
- /*
- if(F(indentare)>0)
- return 1;
- else
- return -1;
- */
- }
- else
- {
- Eroare("lipseste un element dupa , ");
- return -1;
- }
- }
- else if(!strcmp(tipAtom2,"ranou"))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- return Z(indentare,0);
- /*
- if(Z(indentare)>0)
- return 1;
- else
- return -1;
- */
- }
- else
- return -1;
- }
- int G(int indentare)
- {
- if(!strcmp(atom2,"Fie"))
- {
- urmatorulAtom();
- if(!strcmp(tipAtom2,"spati"))
- {
- urmatorulAtom();
- if(H()>0)
- {
- if(F(indentare)>=0)
- return 1;
- else
- return -1;
- }
- else
- {
- return -1;
- }
- }
- else
- {
- Eroare("lipseste spatiu dupa cuvantul cheie 'Fie'");
- return -1;
- }
- }
- else
- return 0;
- }
- int H()
- {
- if(!strcmp(atom2,"sir"))
- {
- Atentionare("nu este nevoie de cuvantul cheie 'sir' pentru a declara o variabila de tip sir de caractere");
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- }
- char s[50];
- int p=pozitieIdentificator(atom2);
- if(p>=0)
- strcpy(s,id[p].tip);
- if(E(1,1,0)>0)
- {
- if(p>=0)
- strcpy(id[p].tip,s);
- if(!strcmp(atom2,"="))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- int p=ultimulIdentificator,o=I();
- if(o<0)
- {
- strcpy(id[p].tip,"");
- Eroare("lipseste element dupa = ");
- return -1;
- }
- else if(!esteIndiceAccesat)
- {
- if(o==1)
- strcpy(id[p].tip,"numar");
- else if(o==2)
- strcpy(id[p].tip,"sirca");
- else if(o==3)
- strcpy(id[p].tip,"colec");
- }
- return 1;
- }
- else
- {
- Eroare("lipseste = dupa numele de variabila la declarare");
- return -1;
- }
- }
- else
- return -1;
- }
- int I()
- {
- if(strstr("nripo,nrine,nrrpo,nrrne",tipAtom2)||!strcmp(atom2,"-")||!strcmp(atom2,"("))
- {
- if(en()>0)
- return 1;
- else
- return -1;
- }
- else if(!strcmp(tipAtom2,"sirca"))
- {
- if(ec()>0)
- return 2;
- else
- return -1;
- }
- else if(!strcmp(atom2,"{"))
- {
- if(co()>0)
- return 3;
- else
- return -1;
- }
- else if(!strcmp(tipAtom2,"ident"))
- {
- int p=pozitieIdentificator(atom2);
- if(p<0)
- {
- char s[50];
- sprintf(s,"variabila '%s' nu a fost declarata",atom2);
- Eroare(s);
- return -1;
- }
- if(!strcmp(id[p].tip,"numar"))
- {
- if(en()>0)
- return 1;
- else
- return -1;
- }
- else if(!strcmp(id[p].tip,"nmsir"))
- {
- if(en()>0)
- return 1;
- else
- return -1;
- }
- else if(!strcmp(id[p].tip,"sirca"))
- {
- if(ec()>0)
- return 2;
- else
- return -1;
- }
- else if(!strcmp(id[p].tip,"colec"))
- {
- int poz=pozitie;
- char s[6];
- strcpy(s,tipAtom2);
- urmatorulAtom();
- if(strstr("spati,ranou",tipAtom2)||!strcmp(atom2,",")||!strcmp(atom2,"}"))
- return 3;
- pozitie=poz;
- strcpy(atom2,id[p].nume);
- strcpy(tipAtom2,s);
- if(ec()>0)
- return 2;
- pozitie=poz;
- strcpy(atom2,id[p].nume);
- strcpy(tipAtom2,s);
- if(en()>0)
- return 1;
- return -1;
- }
- else
- return -1;
- }
- else
- return -1;
- }
- int K(int indentare)
- {
- if(!strcmp(atom2,"Daca"))
- {
- urmatorulAtom();
- if(!strcmp(tipAtom2,"spati"))
- {
- urmatorulAtom();
- if(S(indentare)>0)
- return 1;
- else
- return -1;
- }
- else
- {
- Eroare("lipseste spatiu dupa cuvantul cheie 'Daca'");
- return -1;
- }
- }
- else
- return 0;
- }
- int L(int indentare)
- {
- if(!strcmp(atom2,"Repeta"))
- {
- urmatorulAtom();
- if(!strcmp(tipAtom2,"spati"))
- {
- urmatorulAtom();
- if(!strcmp(atom2,"daca"))
- {
- urmatorulAtom();
- if(!strcmp(tipAtom2,"spati"))
- {
- urmatorulAtom();
- if(S(indentare)>0)
- return 1;
- else
- return -1;
- }
- else
- {
- Eroare("lipseste spatiu dupa cuvantul cheie 'daca'");
- return -1;
- }
- }
- else
- {
- Eroare("lipseste cuvantul cheie 'daca'");
- return -1;
- }
- }
- else
- {
- Eroare("lipseste spatiu dupa cuvantul cheie 'Repeta'");
- return -1;
- }
- }
- else
- return 0;
- }
- int M()
- {
- if(!strcmp(atom2,"nu"))
- {
- urmatorulAtom();
- if(!strcmp(tipAtom2,"spati"))
- urmatorulAtom();
- else
- {
- Eroare("lipseste spatiu dupa cuvantul cheie 'nu'");
- return -1;
- }
- }
- if(N()>0)
- return 1;
- else
- return -1;
- }
- int N()
- {
- if(O()>0)
- {
- int o=0;
- if(!strcmp(tipAtom2,"spati"))
- {
- urmatorulAtom();
- o=1;
- }
- if(!strcmp(atom2,"si"))
- {
- if(o==0)
- {
- Eroare("lipseste spatiu inainte de cuvantul cheie 'si'");
- return -1;
- }
- urmatorulAtom();
- if(!strcmp(tipAtom2,"spati"))
- {
- urmatorulAtom();
- if(N()>0)
- return 1;
- else
- return -1;
- }
- else
- {
- Eroare("lipseste spatiu dupa cuvantul cheie 'si'");
- return -1;
- }
- }
- else
- return 1;
- }
- else
- return -1;
- }
- int O()
- {
- if(P()>0)
- {
- int o=0,p=pozitie;
- char s[50];
- strcpy(s,tipAtom2);
- if(!strcmp(tipAtom2,"spati"))
- {
- urmatorulAtom();
- o=1;
- }
- if(!strcmp(atom2,"sau"))
- {
- if(o==0)
- {
- Eroare("lipseste spatiu inainte de cuvantul cheie 'sau'");
- return -1;
- }
- urmatorulAtom();
- if(!strcmp(tipAtom2,"spati"))
- {
- urmatorulAtom();
- if(O()>0)
- return 1;
- else
- return -1;
- }
- else
- {
- Eroare("lipseste spatiu dupa cuvantul cheie 'sau'");
- return -1;
- }
- }
- else
- {
- pozitie=p;
- strcpy(tipAtom2,s);
- return 1;
- }
- }
- else
- return -1;
- }
- int P()
- {
- if(!strcmp(atom2,"("))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- if(N()>0)
- {
- //urmatorulAtom();
- //treciPesteSpatiiOptionale();
- if(!strcmp(atom2,")"))
- return 1;
- else
- {
- Eroare("lipseste )");
- return -1;
- }
- }
- else
- return -1;
- }
- else if(Q()>0)
- return 1;
- else
- return -1;
- }
- int Q()
- {
- if(en()>0)
- {
- if(!strcmp(tipAtom2,"oprel"))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- if(en()>0)
- return 1;
- }
- }
- return -1;
- }
- int R()
- {
- }
- int S(int indentare)
- {
- if(M()>0)
- {
- if(Y(indentare)>=0)
- {
- if(U(indentare)>=0)
- return 1;
- else
- return -1;
- }
- else
- return -1;
- }
- else
- {
- Eroare("conditie incorecta");
- return -1;
- }
- }
- int T()
- {
- }
- int U(int indentare)
- {
- if(!strcmp(atom2,"Altfel"))
- {
- if(ultimaIndentare==indentare)
- {
- urmatorulAtom();
- if(Y(indentare)>=0)
- return 1;
- else
- return -1;
- }
- else
- {
- Eroare("ramura altfel trebuie sa aiba aceeasi indentare cu instructiunea asociata");
- return -1;
- }
- }
- else
- return 0;
- }
- int V(int indentare)
- {
- if(!strcmp(atom2,"Parcurge"))
- {
- urmatorulAtom();
- if(!strcmp(tipAtom2,"spati"))
- {
- urmatorulAtom();
- if(!strcmp(tipAtom2,"ident"))
- {
- adaugaIdentificator(atom2);
- strcpy(id[ultimulIdentificator].tip,"nmsir");
- urmatorulAtom();
- if(!strcmp(tipAtom2,"spati"))
- {
- urmatorulAtom();
- if(!strcmp(atom2,"din"))
- {
- urmatorulAtom();
- if(!strcmp(tipAtom2,"spati"))
- {
- urmatorulAtom();
- if(W()>0)
- {
- if(Y(indentare)>=0)
- return 1;
- else
- return -1;
- }
- else
- return -1;
- }
- else
- {
- Eroare("lipseste spatiu dupa cuvantul cheie 'din'");
- return -1;
- }
- }
- else
- {
- Eroare("lipseste cuvantul cheie 'din'");
- return -1;
- }
- }
- else
- {
- Eroare("lipseste spatiu dupa variabila iterator");
- return -1;
- }
- }
- else
- {
- Eroare("lipseste variabila iterator");
- return -1;
- }
- }
- else
- {
- Eroare("lipseste spatiu dupa cuvantul cheie 'Parcurge'");
- return -1;
- }
- }
- else
- return 0;
- }
- int W()
- {
- if(E(0,0,0)>0)
- {
- if(strstr("nmsir,sirca,colec",id[ultimulIdentificator].tip))
- return 1;
- else
- {
- Eroare("doar sirurile de caractere si colectiile pot fi iterate");
- return -1;
- }
- }
- else if(in()>0)
- {
- urmatorulAtom();
- if(X()>=0)
- return 1;
- else
- return -1;
- }
- else
- {
- Eroare("intervalul sau variabila care va fi iterata este scrisa incorect");
- return -1;
- }
- }
- int X()
- {
- if(!strcmp(tipAtom2,"spati"))
- {
- urmatorulAtom();
- if(!strcmp(atom2,"cu"))
- {
- urmatorulAtom();
- if(!strcmp(tipAtom2,"spati"))
- {
- urmatorulAtom();
- if(!strcmp(atom2,"pas"))
- {
- urmatorulAtom();
- if(!strcmp(tipAtom2,"spati"))
- {
- urmatorulAtom();
- if(en()>0)
- {
- //urmatorulAtom();
- //treciPesteSpatiiOptionale();
- return 1;
- }
- else
- {
- Eroare("expresia numerica ce indica pasul este scrisa incorect");
- return -1;
- }
- }
- else
- {
- Eroare("lipseste spatiu dupa cuvantul cheie 'pas'");
- return -1;
- }
- }
- else
- {
- Eroare("lipseste cuvantul cheie 'pas'");
- return -1;
- }
- }
- else
- {
- Eroare("lipseste spatiu dupa cuvantul cheie 'cu'");
- return -1;
- }
- }
- else
- return 0;
- }
- else
- return 0;
- }
- int Y(int indentare)
- {
- if(!strcmp(tipAtom2,"ranou"))
- {
- urmatorulAtom();
- if(ultimaIndentare>indentare)
- {
- urmatorulAtom();
- int o=Z(ultimaIndentare,1);
- if(o>=0)
- {
- /*
- if(Z(0)>0)
- return 1;
- else
- return -1;
- */
- return Z(indentare,0);
- }
- else
- return -1;
- }
- else
- {
- Eroare("lipseste indentare dupa instructiune");
- return -1;
- }
- }
- else
- {
- Eroare("lipseste trecerea la un rand nou dupa instructiune");
- return -1;
- }
- }
- int Z(int indentare, int obligatoriu)
- {
- //printf("%d %d\n",ultimaIndentare,indentare);
- if(ultimaIndentare<indentare)
- return 0;
- return A(indentare,obligatoriu);
- }
- int en()
- {
- if(!strcmp(atom2,"-"))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- }
- if(e0()>0)
- return 1;
- else
- {
- Eroare("expresie incorecta");
- return -1;
- }
- }
- int e0()
- {
- if(e1()>0)
- {
- while(!strcmp(atom2,"+")||!strcmp(atom2,"-"))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- if(e1()<0)
- return -1;
- }
- return 1;
- }
- else
- return -1;
- }
- int e1()
- {
- if(e2()>0)
- {
- while(!strcmp(atom2,"*")||!strcmp(atom2,"/")||!strcmp(atom2,"%"))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- if(e2()<0)
- return -1;
- }
- return 1;
- }
- else
- return -1;
- }
- int e2()
- {
- if(!strcmp(atom2,"("))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- if(en()>0)
- {
- if(!strcmp(atom2,")"))
- {
- urmatorulAtom();
- //treciPesteSpatiiOptionale();
- return 1;
- }
- else
- {
- Eroare("lipseste )");
- return -1;
- }
- }
- }
- else if(strstr("nripo,nrine,nrrpo,nrrne",tipAtom2))
- {
- if(strstr("nrine,nrrne",tipAtom2)&&strstr("-,+",atom1))
- {
- Eroare("+ sau - nu pot aparea inaintea unui numar negativ");
- return -1;
- }
- else
- {
- urmatorulAtom();
- //treciPesteSpatiiOptionale();
- return 1;
- }
- }
- else if(E(0,0,1)>0)
- {
- if(strcmp(id[ultimulIdentificator].tip,"sirca"))
- return 1;
- else
- {
- char s[100];
- sprintf(s,"variabila de tip sir de caractere '%s' nu poate aparea intr-o expresie numerica",id[ultimulIdentificator].nume);
- Eroare(s);
- return -1;
- }
- }
- else if(!strcmp(tipAtom2,"sirca"))
- {
- Eroare("un numar nu poate fi adaugat unui sir de caractere");
- return -1;
- }
- else
- return -1;
- }
- int ec()
- {
- while(1)
- {
- if(E(0,0,1)>0&&!strcmp(id[ultimulIdentificator].tip,"numar"))
- {
- Eroare("un sir de caractere nu poate fi concatenat (+) cu un numar");
- return -1;
- }
- if(!strcmp(tipAtom2,"ranou")||!strcmp(atom2,",")||!strcmp(atom2,"}"))
- return 1;
- if(strcmp(tipAtom2,"sirca")&&strcmp(atom2,"+"))
- {
- Eroare("un sir de caractere poate fi doar concatenat (+) cu un altul");
- return -1;
- }
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- }
- }
- int co()
- {
- if(!strcmp(tipAtom2,"ident"))
- {
- int p=pozitieIdentificator(atom2);
- if(p>=0)
- {
- if(!strcmp(id[p].tip,"colec"))
- return 1;
- else
- return 0;
- }
- else
- {
- char s[50];
- sprintf(s,"variabila %s nu a fost declarata",id[p].nume);
- Eroare(s);
- return -1;
- }
- }
- else if(!strcmp(atom2,"{"))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- if(!strcmp(atom2,"}"))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- return 1;
- }
- if(I()>0)
- {
- if(coa()>=0)
- if(!strcmp(atom2,"}"))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- return 1;
- }
- else
- {
- Eroare("lipseste }");
- return -1;
- }
- }
- return -1;
- }
- else
- return 0;
- }
- int coa()
- {
- if(!strcmp(atom2,","))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- if(I()>0)
- {
- //urmatorulAtom();
- treciPesteSpatiiOptionale();
- if(coa()>=0)
- return 1;
- }
- else
- {
- Eroare("lipseste un element dupa , ");
- return -1;
- }
- }
- else
- return 0;
- }
- int in()
- {
- if(!strcmp(atom2,"[")||!strcmp(atom2,"("))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- if(en()>0)
- {
- if(!strcmp(atom2,","))
- {
- urmatorulAtom();
- treciPesteSpatiiOptionale();
- if(en()>0)
- {
- if(!strcmp(atom2,"]")||!strcmp(atom2,")"))
- return 1;
- else
- {
- Eroare("lipseste inchiderea intervalului");
- return -1;
- }
- }
- else
- {
- Eroare("al doilea capat al unui interval trebuie sa fie expresie numerica");
- return -1;
- }
- }
- else
- {
- Eroare("capetele unui interval trebuie sa fie separate prin , ");
- return -1;
- }
- }
- else
- {
- Eroare("primul capat al unui interval trebuie sa fie expresie numerica");
- return -1;
- }
- }
- else
- return 0;
- }
- //cod.txt
- Preia n
- Parcurge i din [0,n) cu pas 2
- Preia v(i)
- Fie nr=0, s=0
- Parcurge x din v
- Repeta daca x!=0
- Fie s=s+x%10
- Fie x=x/10
- Altfel
- Fie nr=nr+1
- Fie m=s/nr
- Afiseaza "Media este ", m
- //cuvinteCheie.txt
- Preia
- sir
- Afiseaza
- Fie
- Daca
- Repeta
- daca
- Altfel
- nu
- si
- sau
- Parcurge
- din
- cu
- pas
- //Productii
- A -> B | D | G | K | L | V
- B -> Preia_EC
- C -> , EC | \n Z
- D -> Afiseaza_EC
- E -> idnum | sir idsir
- F -> , HF | \n Z
- G -> Fie HF
- H -> idnum=I
- I -> expnum | expcar | colectie
- K -> Daca_S
- L -> Repeta_daca_S
- M -> nu_N | N
- N -> O_si_N | O
- O -> P_sau_O | P
- P -> Q | (N)
- Q -> idnumsir rel R
- R -> idnumsir | nr | sir
- S -> M Y U
- T -> tab Z T | λ
- U -> Altfel Y | λ
- V -> Parcurge_idnum_din_W Y
- W -> intervalX | idsir | idcol
- X -> _cu_pas_expnum | λ
- Y -> \n tab Z T
- Z -> A | λ
- id -> litera(litera)*(cifra)*
- idnum -> id | idcol(nr)
- idsir -> id | idsir(nr) | idcol(nr)
- idcol -> id | idcol(nr)
- nr -> -nrp | nrp
- nrp -> nrip(.(cifra)*|λ)
- nrip -> cifra(cifra)*
- rel -> = | != | < | <= | > | >=
- tab -> 4x_
- e -> (- | λ) e0
- e0 -> e1 ((+ | -) e1)*
- e1 -> e2 ((* | / | %) e2)*
- e2 -> (e0) | nr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement