/* * This program generates a random 5-state finite state automaton over * the alphabet {a, b}, using the first command line argument to seed * the random number generator. */ #include #include #include #define NUM_STATES 5 #define ALPHABET_SIZE 2 #define INITIAL_STATE 0 #define FINAL_STATE (NUM_STATES-1) #define MAX_EXAMPLES 100 int fsa_states[NUM_STATES][ALPHABET_SIZE]; /* array of states */ char yes_examples[MAX_EXAMPLES][1000], /* yes example strings */ no_examples[MAX_EXAMPLES][1000]; /* no example strings */ /* this function move the string parameter to the next string in * lexicographic order */ void increment_string (char *s) { /* if null string, set it equal to "a" */ if (!s[0]) { s[0] = 'a'; s[1] = 0; } else if (s[0] == 'a') /* if the first character is 'a', make it 'b' */ s[0] = 'b'; else { /* otherwise, recursively increment the rest of the string */ s[0] = 'a'; increment_string (&s[1]); } } /* make a raindom fsa * void randomize_fsa (void) { int i, j; for (i=0; i=0; i--) { printf ("%20s\t%20s\n", yes_examples[i], no_examples[i]); } } /* main function */ int main (int argc, char *argv[]) { int i, seed, count; /* if wrong number of args, complain and exit */ if (argc != 2) { fprintf (stderr, "Usage: %s \n", argv[0]); exit (1); } /* the seed to srand() will be the sum of digits in the login * name, so each student has a unique (hopefully) seed */ seed = 0; for (i=0; argv[1][i]; i++) seed += argv[1][i]; /* seed the random number generator */ srand (seed); /* make a random fsa */ randomize_fsa (); /* count the number of strings out of 100 accepted by this fsa */ count = count_accepting (100); /* while that number is less than 30 or greater than 60... */ while (count < 30 || count > 60) { /* ...get a different fsa */ randomize_fsa (); count = count_accepting (100); } /* print the fsa rules in a table */ printf ("state#\ta\tb\n"); for (i=0; i