/* ANALIZADOR SINTACTICO DE CONTEXTO LIBRE RECURSIVO DESCENDENTE
PROCESAMIENTO DE LENGUAJE NATURAL
-------------------------------------------------------------
PN = predicado nominal
Pv = predicado verbal
_____________________________________________________
FRASE ----> PN + PV
PN ---> articulo + sustantivo
PN ---> articulo + sustantivo + adjetivo
PN ---> preposicion + PN
PV ---> verbo + PN
PV ---> verbo + adverbio + PN
PV ---> verbo + adverbio
PV ---> verbo
_____________________________________________________
*/
//=============================================
// ALGO ASI ESPERO COMO CODIGO
//=============================================
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int Analiza( );
int EsPredicadoNominal();
int EsPredicadoVerbal();
int EsArticulo(char*);
int EPN_1();
int EPN_2();
struct Pila
{
int pos;
struct Pila *sig;
};
struct Pila *pila = NULL;
char *sustantivo[] = {"puerta", "ventana", "casa", "ninio", ""};
char *verbo[] = {"tiene", "corre", "juega", ""};
char *adverbio[] = {"rapidamente", ""};
char *articulo[] = {"el", "la", "un", ""};
char *preposicion[] = {"hacia", ""};
char *adjetivo[] = {"rojo", "verde", "feo", "alto", ""};
char *Frases[] = { "la puerta"};/*
"el alto ninio corre rapidamente hacia la ventana",
"el ninio rapidamente corre hacia la casa" };*/
char *Frase = NULL, *Palabra = NULL;
char *LONG_LEIDA = 0; // Nos guarda la posiciOn anterior en la cadena en caso de falla
// recuperar la frase original
void RecuperaFrase(char *Frase)
{
free(pila); // borrar lo que hay
pila = (struct Pila*)malloc(sizeof(struct Pila));
pila->pos=0; pila->sig=NULL;
strcpy(Frase, Frases[0]);
}
void LeePalabra(char *Frase)
{
int i = 0;
strcpy(Palabra,"");
while ( Frase[i]!='\0' )
{
if ( Frase[i]==' ' ) return;
else sprintf(Palabra, "%s%c", Palabra, Frase[i] );
i++;
}
}
void MuestraPalabras(char *Frase)
{
int k=0;
while ( Frase[0] != '\0' )
{
LeePalabra(Frase);
printf("%s ==> %i\n", Palabra, (int)strlen(Palabra));
Frase = Frase + strlen(Palabra) + 1; // el 1 es por el espacio
}
}
int RevisaSiSiEs( char *VECTOR[] )
{
int k = 0;
while ( VECTOR[k] )
{
if ( ((int)strlen(VECTOR[k]) != 0) && strcmp(VECTOR[k], Palabra) == 0 ) return 1;
k++;
}
return 0;
}
int EsArticulo( char *Frase ) { LeePalabra(Frase); return RevisaSiSiEs(articulo); }
int EsSustantivo( char *Frase ) { LeePalabra(Frase); return RevisaSiSiEs(sustantivo); }
int EsAdjetivo( char *Frase ) { LeePalabra(Frase); return RevisaSiSiEs(adjetivo); }
int EsPreposicion( char *Frase ) { LeePalabra(Frase); return RevisaSiSiEs(preposicion); }
int EsVerbo( char *Frase ) { LeePalabra(Frase); return RevisaSiSiEs(verbo); }
int EsAdverbio( char *Frase ) { LeePalabra(Frase); return RevisaSiSiEs(adverbio); }
// actualiza el apuntador en la frase
// 1=push
// 0=pop
void ActPosicion(int opc)
{
struct Pila *new = (struct Pila*)malloc(sizeof(struct Pila));
if ( opc ) // vale 1
{
new->sig=pila; new->pos=strlen(Palabra) + 1; pila=new;
Frase = Frase + new->pos;
}
else // vale -1
{
Frase = Frase - pila->pos;
pila = pila->sig;
}
}
int EsPredicadoNominal( )
{
if ( EPN_2() ) return 1;
if ( EPN_1() ) return 1;
return 0;
}
int EPN_2( )
{
//_________________PN ---> articulo + sustantivo + adjetivo
if ( EsArticulo(Frase) )
{
ActPosicion(1);
printf(" articulo ");
if ( EsSustantivo(Frase) )
{
ActPosicion(1);
printf(" sustantivo ");
if ( EsAdjetivo(Frase) )
{
ActPosicion(1);
printf(" adejtivo ");
return 1;
}
ActPosicion(-1);
}
ActPosicion(-1);
}
return 0;
}
int EPN_1( )
{
//_________________PN ---> articulo + sustantivo
if ( EsArticulo(Frase) )
{
ActPosicion(1);
printf(" articulo ");
if ( EsSustantivo(Frase) )
{
ActPosicion(1);
printf(" sustantivo ");
return 1;
}
ActPosicion(-1);
}
return 0;
}
/*
int EsPredicadoNominal(char *Frase)
{
//_________________PN ---> articulo + sustantivo
if ( EsArticulo(Frase) && EsSustantivo(Frase) ) return 1;
RecuperaFrase(Frase); //___No fue el caso anterior. Se recupera lo leIdo
//_________________PN ---> articulo + sustantivo + adjetivo
if ( EsArticulo(Frase) && EsSustantivo(Frase) && EsAdjetivo(Frase) ) return 1;
RecuperaFrase(Frase); //___No fue el caso anterior. Se recupera lo leIdo
//_________________PN ---> preposicion + PN
if ( EsPreposicion(Frase) && EsPredicadoNominal(Frase) ) return 1;
RecuperaFrase(Frase); //___No fue el caso anterior. Se recupera lo leIdo
return 0; // No fuE ningUna de las anteriores
}
int EsPredicadoVerbal(char *Frase)
{
//_________________PV ---> verbo + PN
if ( EsVerbo(Frase) && EsPredicadoNominal(Frase) ) return 1;
RecuperaFrase(Frase); //___No fue el caso anterior. Se recupera lo leIdo
//_________________PV ---> verbo + adverbio + PN
if ( EsVerbo(Frase) && EsAdverbio(Frase) && EsPredicadoNominal(Frase) ) return 1;
RecuperaFrase(Frase); //___No fue el caso anterior. Se recupera lo leIdo
//_________________PV ---> verbo + adverbio
if ( EsVerbo(Frase) && EsAdverbio(Frase) ) return 1;
RecuperaFrase(Frase); //___No fue el caso anterior. Se recupera lo leIdo
//_________________PV ---> verbo
if ( EsVerbo(Frase) ) return 1;
RecuperaFrase(Frase); //___No fue el caso anterior. Se recupera lo leIdo
return 0; // No fuE ningUna de las anteriores
}
*/
int Analiza( )
{
// FRASE ----> PN + PV
//if ( EsPredicadoNominal(Frase) && EsPredicadoVerbal(Frase) ) return 1;
return EsPredicadoNominal( );
}
char *LeeFrase( )
{
// fgets(Frase, 100, stdin); // leE lInea del teclado
return Frases[0];
}
int main()
{
Frase = (char*)malloc(sizeof(char)*100);
Palabra = (char*)malloc(sizeof(char)*20);
pila = (struct Pila*)malloc(sizeof(struct Pila));
pila->pos=0; pila->sig=NULL; // se inicializa la pila de posiciones
int ok = 0;
Frase = LeeFrase( );
// EsArticulo(Frase);
ok = Analiza( );
if (ok) printf("\nFrase correcta ...\n");
else printf("\nFrase incorrecta ...\n");
}