You are on page 1of 4

Converting NFA with ε transition to NFA without ε transition.

#include <stdio.h>

#include <stdbool.h>

#include <string.h>

#define MAX_STATES 100

#define MAX_ALPHABET 100

// Structure to represent a state in the NFA

typedef struct {

int state;

bool epsilon;

int transitions[MAX_STATES];

int numTransitions;

} State;

// Function to compute the ε-closure of a state

void computeEpsilonClosure(State nfa[], bool visited[], int currentState, int closure[])

visited[currentState] = true; // Mark the current state as visited

closure[currentState] = 1; // Add the current state to the closure

// For each transition from the current state

for (int i = 0; i < nfa[currentState].numTransitions; i++)

int nextState = nfa[currentState].transitions[i];

// If the transition is an ε transition and the next state hasn't been visited

if (nfa[currentState].epsilon && !visited[nextState]) {

computeEpsilonClosure(nfa, visited, nextState, closure);

// Function to compute the set of states reachable from a given set of states on a given input symbol

void computeStateSet(State nfa[], int states[], int numStates, int symbol, int resultSet[])

{
int index = 0;

for (int i = 0; i < numStates; i++)

int currentState = states[i];

// For each transition from the current state

for (int j = 0; j < nfa[currentState].numTransitions; j++)

int nextState = nfa[currentState].transitions[j];

// If the transition matches the input symbol, add the next state to the result set

if (!nfa[nextState].epsilon && symbol == nextState) {

resultSet[index++] = nextState;

int main() {

int numStates, numSymbols;

State nfa[MAX_STATES];

int dfaStates[MAX_STATES][MAX_ALPHABET] = { 0 };

int dfaNumStates = 0;

printf("Enter the number of states in the NFA: ");

scanf("%d", &numStates);

printf("Enter the number of input symbols: ");

scanf("%d", &numSymbols);

// Read the NFA

for (int i = 0; i < numStates; i++)

printf("Enter details for State %d:\n", i);

nfa[i].state = i;

printf("Is State %d an epsilon state? (0 for no, 1 for yes): ", i);
scanf("%d", &nfa[i].epsilon);

printf("Enter the number of transitions for State %d: ", i);

scanf("%d", &nfa[i].numTransitions);

printf("Enter the transition states for State %d:\n", i);

for (int j = 0; j < nfa[i].numTransitions; j++)

scanf("%d", &nfa[i].transitions[j]);

printf("\n");

// Compute the ε-closure for each state

for (int i = 0; i < numStates; i++)

bool visited[MAX_STATES] = { false };

int closure[MAX_STATES] = { 0 };

computeEpsilonClosure(nfa, visited, i, closure);

// For each input symbol

for (int j = 0; j < numSymbols; j++)

int symbol = j;

// Compute the set of states reachable from the ε-closure of the current state on the input symbol

int resultSet[MAX_STATES] = { 0 };

computeStateSet(nfa, closure, numStates, symbol, resultSet);

// Add the resulting state set to the DFA states if it is not empty and not already in the DFA states

if (resultSet[0] != 0)

bool newState = true;

for (int k = 0; k < dfaNumStates; k++)

if (memcmp(resultSet, dfaStates[k], sizeof(resultSet)) == 0)

{
newState = false;

break;

if (newState)

memcpy(dfaStates[dfaNumStates++], resultSet, sizeof(resultSet));

// Print the resulting DFA states

printf("\nDFA States:\n");

for (int i = 0; i < dfaNumStates; i++)

printf("State %d: ", i);

for (int j = 0; j < numSymbols; j++) {

printf("%d ", dfaStates[i][j]);

printf("\n");

return 0;

You might also like