#include /* * This program implements the finite state automaton example given * in assignment 3. */ int main () { char input; int state; /* the initial state is 1 */ state = 1; /* get the first character from input */ input = getchar (); /* while the current input character is not a dollar sign, * do the following code. */ while (input != '$') { if (state == 1) { /* from state 1, go to state 3 on 'a' input */ if (input == 'a') state = 3; /* from state 1, go to state 2 on 'b' input */ else if (input == 'b') state = 2; } else if (state == 2) { /* from state 2, go to state 1 on 'a' input */ if (input == 'a') state = 1; /* from state 2, go to state 2 on 'b' input */ else if (input == 'b') state = 2; } else if (state == 3) { /* from state 3, go to state 1 on 'a' input */ if (input == 'a') state = 1; /* from state 3, go to state 2 on 'b' input */ else if (input == 'b') state = 2; } /* get another input character; it might be a dollar * sign, the while statement will check it. */ input = getchar (); } if (state == 3) /* if we are in the final state, print "accepted." */ printf ("accepted.\n"); else /* if we are *not* in the final state, print "not accepted." */ printf ("not accepted.\n"); }