Advertisement
okpalan

example-state-machine.c

Nov 23rd, 2023
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.43 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. // Define the states as constants
  6. #define Q0 0
  7. #define Q1 1
  8.  
  9. // Define the alphabet as an array of characters
  10. char alphabet[] = {'0', '1'};
  11.  
  12. // Define the transition function as a 2D array of integers
  13. int transition[][2] = {{Q0, Q1}, {Q0, Q1}};
  14.  
  15. // Define the initial state as an integer
  16. int initial = Q0;
  17.  
  18. // Define the final state as an integer
  19. int final = Q1;
  20.  
  21. // Define a function that checks if a character is in the alphabet
  22. int is_in_alphabet(char c) {
  23.   for (int i = 0; i < sizeof(alphabet); i++) {
  24.     if (c == alphabet[i]) {
  25.       return 1;
  26.     }
  27.   }
  28.   return 0;
  29. }
  30.  
  31. // Define a function that runs the state machine on a given input string
  32. int run_state_machine(char* input) {
  33.   // Initialize the current state to the initial state
  34.   int current = initial;
  35.   // Loop through each character of the input string
  36.   for (int i = 0; i < strlen(input); i++) {
  37.     // Check if the character is in the alphabet
  38.     if (is_in_alphabet(input[i])) {
  39.       // Get the index of the character in the alphabet
  40.       int index = input[i] - '0'; // do not understand how this works  in c?
  41.       // Update the current state using the transition function
  42.       current = transition[current][index];
  43.     } else {
  44.       // Reject the input if the character is not in the alphabet
  45.       return 0;
  46.     }
  47.   }
  48.   // Check if the current state is the final state
  49.   if (current == final) {
  50.     // Accept the input if the current state is the final state
  51.     return 1;
  52.   } else {
  53.     // Reject the input if the current state is not the final state
  54.     return 0;
  55.   }
  56. }
  57.  
  58. // Define a function that tests the state machine on some examples
  59. void test_state_machine() {
  60.   // Define some example input strings
  61.   char* examples[] = {"0", "1", "01", "10", "11", "101", "110", "111", "1001", "1010", "1101", "1110", "1111", "0000", "0101", "0011", "0110", "0001", "0010", "0100", "1000", "2", "12", "21", "a", "b", ""};
  62.   // Loop through each example input string
  63.   for (int i = 0; i < sizeof(examples) / sizeof(char*); i++) {
  64.     // Run the state machine on the example input string
  65.     int result = run_state_machine(examples[i]);
  66.     // Print the result
  67.     printf("Input: %s, Output: %s\n", examples[i], result ? "Accepted" : "Rejected");
  68.   }
  69. }
  70.  
  71. // Define the main function
  72. int main() {
  73.   // Test the state machine
  74.   test_state_machine();
  75.   // Exit the program
  76.   return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement