-3

I tried to do a scanner function with goto to check if the vector with my dfa can be accepted,

I'm having a problem with the logic, when I call the function the code starts to print in loop and it doesn't work. here is my code, i need help:

#include <stdio.h>
#define REJEITA 0
#define ACEITA 1
#define TAM 10

int scanner(char palavra[], int contador);

int main() {
  char palavra[TAM] = "45"; // Palavra teste
  int contador = 0;
  scanner(palavra, contador);
  printf("TEST\n");
  return 0;
}

int scanner(char palavra[], int contador) {
 
q1:
//c = palavra[contador];

  if (palavra[contador] >= "0" || palavra[contador] <= "9") {
    contador += 1;
    // printf("Palavra ACEITA pelo automato");
    goto q2;
    return (ACEITA);
  }

  else if (palavra[contador] == "+" || palavra[contador] == "-" ) {
    printf("Palavra REJEITADA pelo automato");
    contador += 1;
    goto q5;
    return (REJEITA);
  } else
    goto q5;

q2:

  if (palavra[contador] >= "0" || palavra[contador] <= "9") {
    contador += 1;
    printf("Palavra ACEITA pelo automato");
    goto q2;
    return (ACEITA);
  }

  else if (palavra[contador] == "." ) {
    contador += 1;
    printf("Palavra ACEITA pelo automato");
    goto q3;
    return (ACEITA);

  }

  else if (palavra[contador] == "+" || palavra[contador] == "," ||
           palavra[contador] == "-") {
    contador += 1;
    printf("Palavra REJEITADA pelo automato");
    goto q5;
    return (REJEITA);
  }

  // else if (c == "\0") return (ACEITA);
  else
    goto q5;
  return (REJEITA);

q3:

  if (palavra[contador] >= "0" || palavra[contador] <= "9") {
    contador += 1;
    printf("Palavra ACEITA pelo automato");
    goto q3;
    return (ACEITA);
  }

  else if (palavra[contador] == "+" || palavra[contador] == "," ||
           palavra[contador] == "- "|| palavra[contador] == ".") {
    contador += 1;
    printf("Palavra REJEITADA pelo automato");
    goto q5;
    return (REJEITA);
  }

  // else if (c == "\0") return (ACEITA);
  else
    goto q5;
  return (REJEITA);

q5: // termina ou invalida
  printf("Palavra rejeitada pelo automato");
  return (REJEITA);
}

I'm waiting to be able to return if afd is accepted or not in main

  • 2
    1. What's this program is supposed to do? 2. All `return`s just below `goto` are unreachable. – जलजनक Mar 28 '22 at 01:22
  • 2
    The `goto` statement was first 'considered harmful' nearly 60 years ago, and it has been omitted from most modern languages. It is rather late in the day to start using them now. The statements after `q2` aren't used by anybody else, so they can be inline where you have `goto q2`. and can be rewritten as a `while` loop. Similarly for the others. – user207421 Mar 28 '22 at 01:24
  • If you're trying to implement a state machine, you will find it easiest to draw the machine, at least for small machines. Attaching the drawing to your question would help us, too. There are online services which help you create nice drawings, but for five states you could just draw it neatly on a sheet of paper. – rici Mar 28 '22 at 02:13

1 Answers1

0

You should avoid the use of goto statements, as they lead to spaghetti code and unstructured programming. Use control structures like if and else, or switch instead.

Felix An
  • 309
  • 4
  • 13