I have to determine if the strings in the file belong to the AFD. I was able to do just for a string. The goal now is to read more than one string, but I could not do the implementation, it always ends up in a loop or any other error.
What tips do you give?
Language:
L={yxz|y E {a,b,c}+ e z E {1,2,3}+}
Sample file for reading multiple strings:
abcx123
23ax2b1
bx31
a2x3b
bcax321
My code to recognize only one string:
#include <stdio.h>
#include <stdlib.h>
FILE *arquivo;
void voltaLeitura()
{
fseek(arquivo, -1, SEEK_CUR);
}
char getSimbolo()
{
char temp;
if(arquivo==NULL)
{
printf("Erro: arquivo n�o aberto!\n");
return EOF;
}
temp = fgetc(arquivo);
return temp;
}
int AFD()
{
int E=0;
char S='i';
while(S!=EOF)
{
S=getSimbolo();
switch(E)
{
case 0:
if(S=='a'||S=='b'||S=='c')
E=1;
else
E=0;
break;
case 1:
if(S=='x')
E=2;
else if(S=='a'||S=='b'||S=='c')
E=1;
else
E=0;
break;
case 2:
if(S=='1'||S=='2'||S=='3')
E=3;
else if(S=='a'||S=='b'||S=='c')
E=1;
else
E=0;
break;
case 3:
if(S=='1'||S=='2'||S=='3')
E=3;
else
E=4;
break;
}
}
if(E==4) return 1;
return -1;
}
int main()
{
int retorno;
arquivo = fopen("entrada2b.txt", "r");
retorno = AFD();
if(retorno == 1)
printf("Valido\n");
else if(retorno == -1)
printf("Invalido\n");
fclose(arquivo);
return 0;
}