7.9k views
0 votes
Create a C language program that can be used to construct any arbitrary Deterministic Finite Automaton corresponding to the FDA definition above. a. Create structs for the: automaton, a state, and a transition. For example, the automaton should have a "states" field, which captures its set of states as a linked list.

User YuAo
by
4.7k points

1 Answer

7 votes

Answer:

see the explanation

Step-by-step explanation:

/* C Program to construct Deterministic Finite Automaton */

#include <stdio.h>

#include <DFA.h>

#include <stdlib.h>

#include <math.h>

#include <string.h>

#include <stdbool.h>

struct node{

struct node *initialStateID0;

struct node *presentStateID1;

};

printf("Please enter the total number of states:");

scanf("%d",&count);

//To create the Deterministic Finite Automata

DFA* create_dfa DFA(){

q=(struct node *)malloc(sizeof(struct node)*count);

dfa->initialStateID = -1;

dfa->presentStateID = -1;

dfa->totalNumOfStates = 0;

return dfa;

}

//To make the next transition

void NextTransition(DFA* dfa, char c)

{

int tID;

for (tID = 0; tID < pPresentState->numOfTransitions; tID++){

if (pPresentState->transitions[tID].condition(c))

{

dfa->presentStateID = pPresentState->transitions[tID].toStateID;

return;

}

}

dfa->presentStateID = pPresentState->defaultToStateID;

}

//To Add the state to DFA by using number of states

void State_add (DFA* pDFA, DFAState* newState)

{

newState->ID = pDFA->numOfStates;

pDFA->states[pDFA->numOfStates] = newState;

pDFA->numOfStates++;

}

void transition_Add (DFA* dfa, int fromStateID, int(*condition)(char), int toStateID)

{

DFAState* state = dfa->states[fromStateID];

state->transitions[state->numOfTransitions].toStateID = toStateID;

state->numOfTransitions++;

}

void reset(DFA* dfa)

{

dfa->presentStateID = dfa->initialStateID;

}

User Orsay
by
3.6k points