Advertisement
Nahid8195

Untitled

Nov 27th, 2022
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. #define t_states 3
  4. #define f_states 1
  5. #define alphabets 2
  6. #define unknown_symbol 0
  7. #define accepted 1
  8. #define not_accepted 2
  9.  
  10. enum states {q0, q1, q2};
  11. enum inputs {a, b};
  12.  
  13. int a_states[f_states] = {q2};
  14. char characters[alphabets] = {'a', 'b'};
  15. int transition_table[t_states][alphabets];
  16. int current_state = q0;
  17.  
  18. void Transition()
  19. {
  20.    /* transition_table[q0][a] = q1;
  21.     transition_table[q0][b] = q1;
  22.     transition_table[q1][a] = q1;
  23.     transition_table[q1][b] = q2;
  24.     transition_table[q2][a] = q2;
  25.     transition_table[q2][b] = q2;
  26.     */
  27.     transition_table[q0][a] = q1;
  28.     transition_table[q0][b] = q1;
  29.     transition_table[q1][a] = q1;
  30.     transition_table[q1][b] = q2;
  31.     transition_table[q2][a] = q2;
  32.     transition_table[q2][b] = q0;
  33. }
  34.  
  35. int DFA(char current_symbol)
  36. {
  37.     int i, j;
  38.     for(i=0; i<alphabets; i++)
  39.     {
  40.         if(current_symbol == characters[i])
  41.         {
  42.             break;
  43.         }
  44.     }
  45.  
  46.     if(i == alphabets)
  47.     {
  48.         return unknown_symbol;
  49.     }
  50.  
  51.     for(j=0; j<f_states; j++)
  52.     {
  53.         if((current_state = transition_table[current_state][i]) == a_states[j])
  54.         {
  55.             return accepted;
  56.         }
  57.     }
  58.  
  59.     return not_accepted;
  60. }
  61.  
  62. int main()
  63. {
  64.     char user_input;
  65.     int result;
  66.     Transition();
  67.  
  68.     printf("Enter a string: ");
  69.  
  70.     while((user_input = getchar()) != '\n')
  71.     {
  72.         result = DFA(user_input);
  73.         if(result == unknown_symbol)
  74.         {
  75.             break;
  76.         }
  77.     }
  78.     switch(result)
  79.     {
  80.     case unknown_symbol:
  81.         printf("Unknown Symbol\n");
  82.         break;
  83.     case accepted:
  84.         printf("Accepted\n");
  85.         break;
  86.     case not_accepted:
  87.         printf("Not Accepted\n");
  88.         break;
  89.     }
  90.  
  91.     return 0;
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement