Skip to content

Instantly share code, notes, and snippets.

@devpruthvi
Last active October 24, 2015 03:03
Show Gist options
  • Select an option

  • Save devpruthvi/25eca44ce91d47cbc6f4 to your computer and use it in GitHub Desktop.

Select an option

Save devpruthvi/25eca44ce91d47cbc6f4 to your computer and use it in GitHub Desktop.
#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]={"a","b","c","d","e","f"},final_states[MAX_STATES][MAX_STATE_SIZE]={"q6","q7"},initial_state[MAX_STATE_SIZE] = "q2";
char states[MAX_STATES][MAX_STATE_SIZE] = {"q0","q1","q2","q3","q4","q5","q6","q7","D"};
char transitions[MAX_STATES][MAX_INPUTS][MAX_STATE_SIZE] = {{"D","q0","q6","D","D","D"},
{"q1","D","D","q4","D","D"},
{"q3","D","D","q5","D","D"},
{"D","q0","q6","q4","D","D"},
{"D","D","D","q4","q6","D"},
{"D","D","D","q4","q7","D"},
{"q3","D","D","q5","D","D"},
{"q3","D","D","q5","D","q6"},
{"D","D","D","D","D","D"}};
char *temp_state, *temp_input, id_string[MAX_ID_SIZE], cur_inp[MAX_INPUT_SIZE], cur_state[MAX_STATE_SIZE];
int i, j, k, numinps=6, numstates=9, row_id, col_id,num_final_states;
num_final_states = 2;
while(1)
{
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!");
}
}
@devpruthvi
Copy link
Copy Markdown
Author

reg2 dfa final

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment