Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

University Of Engineering & Technology Lahore, Narowal Campus

Department of Computer Science


Compiler Construction

EXPERIMENT#01

Submitted To:
Iqra Muneer

Submitted By:
Meesha Munir (2020-CS-517)
DFA to NFA

Introduction:
A DFA (deterministic finite automaton) is a finite state machine that processes input symbols, one at a time, moving through
a series of states according to a predefined set of rules. A DFA (Deterministic Finite Automaton) and an NFA
(Nondeterministic Finite Automaton) are both theoretical models used in automata theory, a branch of theoretical computer
science. They are used to recognize and classify languages in the realm of formal languages and automata .
A DFA is formally defined as a quintuple (Q, Σ, δ, q₀, F), where:

 Q represents the finite set of states.


 Σ is the input alphabet, a finite set of symbols.
 δ is the transition function, which maps a state and an input symbol to a new stateq ₀ denotes the start state.
 F represents the set of accepting states.
An NFA is formally defined as a quintuple (Q, Σ, δ, q₀, F), where:

 Q represents the finite set of states.


 Σ is the input alphabet, a finite set of symbols.
 δ is the transition function, which maps a state, an input symbol, and an ε (epsilon) transition to a set of new states.
 q₀ denotes the start state.
 F represents the set of accepting states.

Determinism vs. nondeterminism:


A DFA operates deterministically, meaning that for a given input symbol and current state, there is only one possible
transition. On the other hand, an NFA operates non deterministically, allowing multiple possible transitions or no transition
at all.
Converting DFA to NFA:
To convert a DFA to an NFA, we can create an equivalent NFA by adding ε transitions between states where the DFA
would transition on the same input symbol. This process expands the nondeterministic behavior of the automaton.

Example:
Consider a simple DFA that recognizes strings over the alphabet {0, 1} where the number of 0's is even. The DFA is defined
as follows:
States: {q0, q1}
Alphabet: {0, 1}
Transition Table:
| Current State | Input Symbol | Next State |
|---------------|--------------|------------|
| q0 |0 | q1 |
| q0 |1 | q0 |
| q1 |0 | q0 |
| q1 |1 | q1 |

Initial State: q0
Accepting State: q0

Code:
#include <iostream>
#include <vector>
#include <set>
using namespace std;

class DFA {
private:
int numStates;
int numSymbols;
vector<vector<int>> transitions;
set<int> acceptingStates;

public:
DFA(int states, int symbols, const vector<vector<int>>& trans, const set<int>& accepts)
: numStates(states), numSymbols(symbols), transitions(trans), acceptingStates(accepts) {}

void convertToNFA() {
// Create new start state and add transition
numStates++;
transitions.push_back(vector<int>(numSymbols, numStates - 1));

// Add ε-transitions from accepting states to new start state


for (int state : acceptingStates) {
for (int i = 0; i < numSymbols; i++) {
transitions[state][i] = numStates - 1;
}
}

// Print the resulting NFA


cout << "Number of States: " << numStates << endl;
cout << "Number of Symbols: " << numSymbols << endl;
cout << "Transitions:" << endl;
for (int i = 0; i < numStates; i++) {
for (int j = 0; j < numSymbols; j++) {
cout << "δ(q" << i << ", " << char('0' + j) << ") = q" << transitions[i][j] << endl;
}
}
}
};

int main() {
// Example DFA
int states = 2;
int symbols = 2;
vector<vector<int>> transitions = {{1, 0}, {0, 1}};
set<int> acceptingStates = {0};
DFA dfa(states, symbols, transitions, acceptingStates);
dfa.convertToNFA();

return 0;
}

Output:

You might also like