Skip to content

Instantly share code, notes, and snippets.

@devpruthvi
Created August 8, 2015 01:29
Show Gist options
  • Save devpruthvi/65749ff1944ef10f57b4 to your computer and use it in GitHub Desktop.
Save devpruthvi/65749ff1944ef10f57b4 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]={"0","1"},final_states[MAX_STATES][MAX_STATE_SIZE]={"q4","q5","q6","q7"},initial_state[MAX_STATE_SIZE] = "q0";
char states[MAX_STATES][MAX_STATE_SIZE] = {"q0","q1","q2","q3","q4","q5","q6","q7"};
char transitions[MAX_STATES][MAX_INPUTS][MAX_STATE_SIZE] = {{"q0","q1"},
{"q2","q3"},
{"q4","q5"},
{"q6","q7"},
{"q0","q1"},
{"q2","q3"},
{"q4","q5"},
{"q6","q7"}};
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=2, numstates=8, row_id, col_id,num_final_states;
num_final_states = 4;
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
Author

reg1

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