You are on page 1of 3

Finding ε – closure of all states of any given NFA with ε transition.

#include <stdio.h>

#include <stdbool.h>

#include <string.h>

#define MAX_STATES 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);

int main() {

int numStates;

State nfa[MAX_STATES];

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


scanf("%d", &numStates);

// 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);

printf("ε-Closure of State %d: ", i);

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

if (closure[j]) {

printf("%d ", j);

printf("\n");

return 0;
}

You might also like