Created
July 26, 2015 07:52
-
-
Save devpruthvi/0acfb953ab4dd8142ea6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<malloc.h> | |
#include<string.h> | |
#define MAX_INPUTS 10 | |
#define MAX_INPUT_SIZE 5 | |
#define MAX_STATES 20 | |
#define MAX_STATE_SIZE 5 | |
#define MAX_ID_SIZE 100 | |
int get_id(char str[], char str_arr[][MAX_STATE_SIZE], int len) | |
{ | |
int i; | |
for (i = 0; i<len; i++) | |
{ | |
if (strcmp(str, str_arr[i]) == 0) | |
return i; | |
} | |
return -1; | |
} | |
int main() | |
{ | |
char inputs[MAX_INPUTS][MAX_INPUT_SIZE],initial_states[MAX_STATE_SIZE],final_states[MAX_STATES][MAX_STATE_SIZE],initial_state[MAX_STATE_SIZE]; | |
char states[MAX_STATES][MAX_STATE_SIZE]; | |
char*** transitions; | |
char temp_state[MAX_STATE_SIZE], temp_input[MAX_INPUT_SIZE], id_string[MAX_ID_SIZE], cur_inp[MAX_INPUT_SIZE], cur_state[MAX_STATE_SIZE]; | |
int i, j, k, numinps, numstates, row_id, col_id,num_final_states; | |
printf("Enter number of input states: "); | |
scanf("%d", &numstates); | |
printf("Enter number of input symbols: "); | |
scanf("%d", &numinps); | |
transitions = (char ***)malloc(sizeof(char**)*numstates); | |
printf("Enter the input states: \n"); | |
for (i = 0; i<numstates; i++) | |
{ | |
scanf(" %s", temp_state); | |
strcpy(states[i], temp_state); | |
} | |
printf("Enter the input symbols: \n"); | |
for (i = 0; i<numinps; i++) | |
{ | |
scanf(" %s", temp_input); | |
strcpy(inputs[i], temp_input); | |
} | |
printf("Enter the transitions:\n"); | |
for (i = 0; i<numstates; i++) | |
{ | |
transitions[i] = (char **)malloc(sizeof(char)*MAX_INPUT_SIZE*numinps); | |
for (j = 0; j<numinps; j++) | |
{ | |
transitions[i][j] = (char *)malloc(sizeof(char)*MAX_STATE_SIZE); | |
printf("\n( %s , %s ) => ", states[i], inputs[j]); | |
scanf(" %s", temp_state); | |
strcpy(transitions[i][j], temp_state); | |
} | |
} | |
printf("Enter the initial state of DFA: "); | |
scanf(" %s",initial_state); | |
printf("Enter the number of final states in the DFA: "); | |
scanf("%d",&num_final_states); | |
printf("Enter the final states: "); | |
for(i=0;i<num_final_states;i++) | |
{ | |
scanf("%s",temp_state); | |
strcpy(final_states[i],temp_state); | |
} | |
printf("Enter a String to find ID: "); | |
scanf(" %s", id_string); | |
strcpy(cur_state, initial_state); | |
for (i = 0; i<strlen(id_string); i++) | |
{ | |
cur_inp[0] = id_string[i]; | |
cur_inp[1] = '\0'; | |
row_id = get_id(cur_inp, inputs, numinps); | |
if (row_id == -1) | |
{ | |
printf("Input symbol is not present in sigma"); | |
return 0; | |
} | |
col_id = get_id(cur_state, states, numstates); | |
strcpy(cur_state, transitions[col_id][row_id]); | |
printf("\n---- %s => %s ----\n", cur_inp, cur_state); | |
} | |
if(get_id(cur_state,final_states,num_final_states) != -1) | |
printf("The string is accepted!"); | |
else | |
printf("The string is NOT accepted!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment