How to use GOTO in C to implement finite automata?

5

This is my first time using goto in programming and I made a simple code here to try to implement automato (it should have much better modes), however my code is crashing at the time of execution, I do not know if it is a bad use of GOTO for my part, could you give me some help?

If anyone has any tips on how I can improve my implementation, I would also appreciate it.

Follow the automaton:

#include<stdio.h>#include<stdlib.h>#include<ctype.h>#include<string.h>intmain(){charstr[80];intcount=0,erro=0;printf("\nType your data entry: ");
scanf ("%79s",str);
int i;
int tam= strlen(str);   
printf("%d", tam);
printf("\nSTRING LIDA: ");

for (i =0; i<tam; i++){
    printf("%c", str[i]);
}



i=0;

goto Q0;


Q0:
    if (i == tam){
        printf("\n\nERROEstado q0 nao e final\n\n");
        erro++;
        goto FINAL;
    }
    if(str[i] == 'a'){
        goto Q1;
        i++;
    }
    if (str[i] == 'b'){
        goto Q2;
        i++;    
    }

Q1:
    if (i == tam){
        printf("\n\nERROEstado q1 nao e final\n\n");
        erro++;
        goto FINAL;
    }
    else if(str[i] == 'a'){
        goto Q0;
        i++;
    }
    else if (str[i] == 'b'){
        goto Q3;
        i++;    
    }

Q2:
    if (i == tam){
        printf("\n\nERROEstado q2 nao e final\n\n");
        erro++;
        goto FINAL;
    }
    else if(str[i] == 'a'){
        goto Q3;
        i++;
    }
    else if (str[i] == 'b'){
        goto Q0;
        i++;    
    }   

Q3:
    if (i == tam){
        goto FINAL;
    }
    else if(str[i] == 'a'){
        goto Q2;
        i++;
    }
    else if (str[i] == 'b'){
        goto Q1;
        i++;    
    }   


FINAL:
    if(erro == 0){
        printf("\n\nEntrada aceita!\n\n");
    }
    else if(erro > 0){
        printf("\n\nERRO: Entrada não aceita\n\n");
    }

return 0;
}
    
asked by anonymous 13.02.2016 / 17:51

1 answer

3
        goto Q3;
        i++;

Your i++ will never be executed, because they are after goto

    
13.02.2016 / 18:05