Implementation of an DFA for reading multiple strings

Asked

Viewed 49 times

1

I have to determine if the strings in the file belong to AFD. I was able to do just one string. The goal now is to read more than one string, but I could not do the implementation, always ends up entering a loop or any other error.

What tips do you give?

Parlance:

L={yxz|y E {a,b,c}+ e z E {1,2,3}+}

Example of file for reading multiple strings :

abcx123
23ax2b1
bx31
a2x3b
bcax321

My code to recognize only a 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&#65533;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;
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.