jueves, 14 de enero de 2010

Calculo de Seguidores

//C.Gramatica.h//
#ifndef GRAMATICA_H
#define GRAMATICA_H
#include
#include
using namespace std;
typedef struct _Gramatica
{ char* NT;
char* Alfa;
char* Iniciales;
char* Seguidres;}
Gramatical;
class CGramatica
{ private: int cantProds;
Gramatical *Prod;
public:
int CmpTerms(const char next);
char* Terminales;
CGramatica();
~CGramatica();
void setNT(const int ind,const char* nNT);
void setAlfa(const int ind, const char* nAlfa);
void setIniciales(const int ind, const char* nInis);
void setSeguidores(const int ind, const char* nSeg);
char* getNoterminal(const int ind);
char* getAlfa(const int ind);
char* getIniciales(const int ind);
char* getSeguidores(const int ind);
int getCantProds(); void setCantProds();
void CargarGramatica();
void agregarProduccion(const char* nNT,const char* nAlfa,const char* nInis,const char* nSeg);
void CargarIniciales();
void CalcularSeguidores();
void CalcularPorNT(int indm, char* Slista);
int BuscarProduccion(const char* nNT);};
#endif
//CalcSeguidores.cpp//
#include "CGramatica.h"
#include "CGramatica.cpp"
#include
#include
using namespace std;
int main(int argc, char** argv)
{ CGramatica ObjCarga;
ObjCarga.CargarGramatica();
ObjCarga.CargarIniciales();
ObjCarga.CalcularSeguidores();
fstream b;
b.open("Seguidores.txt",ios::out);
for(int m=0;m<=ObjCarga.getCantProds();m++)
{ b< char SF[100];SF[0]=0;
for(int k=0;k{ char Ss[2];
bool flag=false;
char Sg[100];
Sg[0]=0;
strcat(Sg,ObjCarga.getSeguidores(m));
for(int n=0;n{ if(Sg[n]==Sg[k]){flag=true;}
} if(flag==false)
{ Ss[0]=Sg[k];
Ss[1]=0;
strcat(SF,Ss);
strcat(SF,",");
} }
cout<<"\nSF="< cout<<"SFfin"< if(SF[strlen(SF)-1]==',')
{ SF[strlen(SF)-1]=' ';
cout<<"SFfin"< } b<if(m!=ObjCarga.getCantProds())
{ b<<"\n"; } }
return 0;}
//CGramatica.cpp//
#include "CGramatica.h"
#include
CGramatica::CGramatica()
{ this->Prod=NULL; this->cantProds=-1;}
CGramatica::~CGramatica()
{ int i=this->cantProds;
if(this->Prod) { for(; i>=0;i--)
{ delete[] this->Prod[i].NT;
delete[] this->Prod[i].Alfa;
delete[] this->Prod[i].Iniciales;
delete[] this->Prod[i].Seguidres; }
delete[] this->Prod; }}
void CGramatica::setNT(const int ind,const char* nNT)
{ delete[] this->Prod[ind].NT; this->Prod[ind].NT = new char [ strlen( nNT ) + 1 ]; strcpy(this->Prod[ind].NT,nNT);}
void CGramatica::setAlfa(const int ind, const char* nAlfa){ delete[] this->Prod[ind].Alfa; this->Prod[ind].Alfa = new char [ strlen( nAlfa ) + 1 ]; strcpy(this->Prod[ind].Alfa,nAlfa);}void CGramatica::setIniciales(const int ind, const char* nInis){
delete[] this->Prod[ind].Iniciales; this->Prod[ind].Iniciales = new char [ strlen( nInis ) + 1 ];
strcpy(this->Prod[ind].Iniciales,nInis);}
void CGramatica::setSeguidores(const int ind, const char* nSeg){
char* temp; int t=strlen(this->Prod[ind].Seguidres)+1; temp = new char [strlen( nSeg ) + t ]; strcpy(temp,this->Prod[ind].Seguidres);
delete[] this->Prod[ind].Seguidres;
strcat(temp,nSeg);
this->Prod[ind].Seguidres = new char [ strlen( temp ) + 1 ]; strcpy(this->Prod[ind].Seguidres,temp);

}
void CGramatica::setCantProds(){ this->cantProds++;}
char* CGramatica::getNoterminal(const int ind){ return this->Prod[ind].NT;}char* CGramatica::getAlfa(const int ind){ return this->Prod[ind].Alfa;}
char* CGramatica::getIniciales(const int ind){ return this->Prod[ind].Iniciales;}char* CGramatica::getSeguidores(const int ind){ return this->Prod[ind].Seguidres;}
int CGramatica::getCantProds(){ return this->cantProds;}
void CGramatica::CargarGramatica(){ char buffer[512];
char NT[20];
char Alfa[512];
int i=0;
int j=0;
int k=0;
fstream a;
a.open("Gramatica.txt", ios::in);
a.getline(buffer,512);
cout<a.getline(buffer,512);
for(i=0;buffer[i]!='=';i++){} i++;
char Terms[30];
for(j=0;buffer[i];i++) { if(buffer[i]==','buffer[i]==' '){continue;}
else { Terms[j]=buffer[i]; j++; } } Terms[j]=0; this->Terminales=new char[strlen(Terms)+1]; strcpy(this->Terminales,Terms);
cout<<"\nTERMINALES"<Terminales<<"\n";
cout< a.getline(buffer,512); for( j=0, i=0; buffer[ i ]; i++ ) { if( buffer[ i ] == ' ' ) continue;
if( buffer[ i ] != '-' && buffer[ i+1 ] != '>' )
{ NT[ j ] = buffer[ i ];
j++;
continue;
}
else
{ NT[ j ] = 0;
i += 2;
break; } }
for(j=0;buffer[i];i++) { if(buffer[i]==' ') continue;
Alfa[j]=buffer[i];
j++;
}
Alfa[j]=0;
cout<<"NNTT="<int oks=this->BuscarProduccion(NT);
if(oks>=0) { cout<<"%%%%%";
char tempA[50];
strcpy(tempA,this->Prod[oks].Alfa);
strcat(tempA,",");
strcat(tempA,Alfa);
delete[] this->Prod[oks].Alfa; this->Prod[oks].Alfa = new char [strlen(tempA) + 1];
strcpy(this->Prod[oks].Alfa,tempA); }
else { cout<<":("; char n[2];n[0]=0;
this->agregarProduccion(NT,Alfa,"",n); } } a.close(); cout<<"!";
cout<<"\nTERMINÓ LECTURA DE GRAMATICA\n"; }void CGramatica::agregarProduccion(const char* nNT, const char* nAlfa, const char* nInis,const char* nSeg){ cout<<"AGREGAR PROD PARA"< Gramatical *temporal; this->cantProds++; i=0; temporal = new Gramatical[ (this->cantProds) + 1 ];
for( ; i <>cantProds ; i++) { temporal[i].NT = this->Prod[i].NT; temporal[i].Alfa = this->Prod[i].Alfa; temporal[i].Iniciales = this->Prod[i].Iniciales; temporal[i].Seguidres = this->Prod[i].Seguidres; }
delete[] this->Prod;
temporal[ i ].NT = new char[ strlen( nNT ) +1 ];
temporal[ i ].Alfa = new char[ strlen( nAlfa ) +1 ];
temporal[ i ].Iniciales = new char[ strlen( nInis ) +1 ];
temporal[ i ].Seguidres = new char[ strlen( nSeg ) +1 ];
strcpy(temporal[i].NT,nNT);
strcpy(temporal[i].Alfa,nAlfa);
strcpy(temporal[i].Seguidres,nSeg);
strcpy(temporal[i].Iniciales,nInis);
this->Prod = temporal;}
void CGramatica::CargarIniciales(){ fstream In; In.open("Iniciales.txt",ios::in); if(In.fail()) {cout<<"No se pudo abrir el archivo de Iniciales\n";} char buffer[512]; char NT[20]; char Inis[100]; int i=0,j=0,k=0; while(!In.eof()) { In.getline(buffer,512); for(i=0,j=0;buffer[ i ];i++) { if(buffer[ i ]==' ') continue; if(buffer[ i ]!='=') { NT[j]=buffer[ i ];
j++;
continue; }
else { NT[j]=0; i++; break; }
} int ind = this->BuscarProduccion(NT);
if(ind!=-2) { for(j=0;buffer[i];i++) { if(buffer[i]==' 'buffer[i]==',') {continue;} else { Inis[j]=buffer[i]; j++;
} } Inis[j]=0; }
else { cout<<"\nError: No se encontró el elemento en las estructuras\n"; return; } this->setIniciales(ind,Inis);
} for(int m=0;m<=this->cantProds;m++) { cout<<"\nPos"<Prod[m].NT; cout<<"\nAlfa="<Prod[m].Alfa; cout<<"\nIniciales"<Prod[m].Iniciales; } In.close();}int CGramatica::BuscarProduccion(const char* nNT){ int i=0; while(i<=this->cantProds) { if(strcmp(this->Prod[i].NT,nNT)==0){ return i;} i++; }
return -2;}void CGramatica::CalcularSeguidores(){ int i=0; char Seguis[50];
for(;i<=this->cantProds;i++) { cout<<"\nMANDE";
if(strlen(this->Prod[i].Seguidres)==0){CalcularPorNT(i,Seguis);} }}
void CGramatica::CalcularPorNT(int indice, char* Slista){
int tamA=0,ind=0; char p[100]="";
char SAct[100];SAct[0]=0; while(ind<=this->cantProds) { cout<<"\nEntre a Calcular\n"; cout<Prod[indice].NT;
tamA=strlen(this->Prod[ind].Alfa);
char tempA [3]; char next[3];
for(int i=0,j=0,k=0;iProd[ind].Alfa[i]; cout<<"\nFOR-"; tempA[j]=this->Prod[ind].Alfa[i]; j++; tempA[j]=0;
cout<<"\n----"< int oks=this->CmpTerms(tempA[0]); if(oks!=-1) { cout<<"\nSi es terminal y se brinca";
tempA[0]=0;tempA[1]=0;tempA[2]=0;
j=0; continue; } else { cout<<"\n"< if(this->Prod[ind].Alfa[i+1]==39) { cout<<"\nAPOSTROFE"; i++;tempA[j=39;j++;tempA[j]=0;
} else {tempA[j]=0;j++;}
cout<<"\nTEMPA"<Prod[indice].NT;
if(strcmp(this->Prod[indice].NT,tempA)==0) { cout<<"Es el buscado"; } else {cout<<"\nNO ES EL BUSCADO";j=0;continue;}
if((!this->Prod[ind].Alfa[i+1])(this->Prod[ind].Alfa[i+1]==',')) { cout<<"\n"< cout<<"\nCOMO ES FINAL COMPARA CON="<getNoterminal(ind);
cout<<"\nalfa[i]="<Prod[ind].Alfa[i]<Prod[ind].Alfa[i+1]; i
f(strcmp(tempA,this->getNoterminal(ind==0) { cout<<"\nIGUALES";
j=0;
continue; }
else{cout<<"\nDIFERENTES";}
if(strlen(this->Prod[ind].Seguidres)==0) { cout<<"\nNO SE HAN CALCULADO SEGUIDORES PARA"<Prod[ind].NT;
this->CalcularPorNT(ind,SAct); strcat( SAct, Slista );
k = strlen( Slista ); }
else { cout<<"\n Como estan calculados, SE ANADEN A Slista";
strcat(SAct,this->Prod[ind].Seguidres); cout<<"SActual:"< cout<<"\nNEXT="<Prod[ind].Alfa[i+1]; next[0]=this->Prod[ind].Alfa[i+1];i++; next[1]=0;
cout<<"\n=NEXT="< cout<<"\n"<Terminales<<"\n";
int ind2=this->CmpTerms(next[0]); cout<<"\nIND2"<cout<<"\n"< i++; cout<<"Checo a ver si es 39="<Prod[ind].Alfa[i]<<"\\"; if(this->Prod[ind].Alfa[i]==39) { next[1]=this->Prod[ind].Alfa[i++]; next[2]=0; } else { next[1]=0; } int ok1=this->BuscarProduccion(next); cout<<"\nOK1="<Prod[ok1].Iniciales); cout<<"Next="<Prod[ok1].Iniciales);t++) {
if(this->Prod[ok1].Iniciales[t]=='#') { vac=true; break; } } if(vac==true) { cout<<"\nSI TENIA VACIO EN INCIALES"; if(strlen(this->Prod[ind].Seguidres)==0) { cout<<"\nNO SE HAN CALCULADO SEGUIDORES PARA"<Prod[ind].NT; this->CalcularPorNT(ind,SAct); strcat( SAct, Slista ); k = strlen( Slista ); } else { strcat(SAct,this->Prod[ind].Seguidres); cout<<"SActual:"< } } } } } ind++; }
cout<<"IND"<cout<<"SEGUIS RESCATADOS"<char SF[100];for(int h=0,g=0;SAct[h];h++){ if(SAct[h]=='#') continue;
SF[g]=SAct[h];g++;}SF[g]=0;
this->setSeguidores(indice,SF);

strcpy( Slista, SF );

}int CGramatica::CmpTerms(const char next){ char* Ts; Ts=this->Terminales; for(int s=0;sTerminales);s++) {
if(Ts[s]==next){return s;} } return -1;}

No hay comentarios:

Publicar un comentario