Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <bits/stdc++.h>
- #define t_states 3
- #define f_states 1
- #define alphabets 2
- #define unknown_symbol 0
- #define accepted 1
- #define not_accepted 2
- enum states {q0, q1, q2};
- enum inputs {a, b};
- int a_states[f_states] = {q2};
- char characters[alphabets] = {'a', 'b'};
- int transition_table[t_states][alphabets];
- int current_state = q0;
- void Transition()
- {
- /* transition_table[q0][a] = q1;
- transition_table[q0][b] = q1;
- transition_table[q1][a] = q1;
- transition_table[q1][b] = q2;
- transition_table[q2][a] = q2;
- transition_table[q2][b] = q2;
- */
- transition_table[q0][a] = q1;
- transition_table[q0][b] = q1;
- transition_table[q1][a] = q1;
- transition_table[q1][b] = q2;
- transition_table[q2][a] = q2;
- transition_table[q2][b] = q0;
- }
- int DFA(char current_symbol)
- {
- int i, j;
- for(i=0; i<alphabets; i++)
- {
- if(current_symbol == characters[i])
- {
- break;
- }
- }
- if(i == alphabets)
- {
- return unknown_symbol;
- }
- for(j=0; j<f_states; j++)
- {
- if((current_state = transition_table[current_state][i]) == a_states[j])
- {
- return accepted;
- }
- }
- return not_accepted;
- }
- int main()
- {
- char user_input;
- int result;
- Transition();
- printf("Enter a string: ");
- while((user_input = getchar()) != '\n')
- {
- result = DFA(user_input);
- if(result == unknown_symbol)
- {
- break;
- }
- }
- switch(result)
- {
- case unknown_symbol:
- printf("Unknown Symbol\n");
- break;
- case accepted:
- printf("Accepted\n");
- break;
- case not_accepted:
- printf("Not Accepted\n");
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement