// Question 2a: NFA for the regular expression r = *(a(a|bb)) using Thompson's Construction
#include <iostream>
#include <vector>
#include <map>
#include <string>
struct State {
int id;
std::map<char, std::vector<int>> transitions; // key: input symbol, value: list of next state IDs
bool isFinal;
State(int stateId) : id(stateId), isFinal(false) {}
};
struct NFA {
std::vector<State> states;
int startState;
void addState(bool final = false) {
states.emplace_back(states.size());
states.back().isFinal = final;
}
void addTransition(int from, char symbol, int to) {
states[from].transitions[symbol].push_back(to);
}
};
int main() {
NFA nfa;
// Create states
nfa.addState(); // State 0: Start state
nfa.addState(); // State 1
nfa.addState(); // State 2
nfa.addState(); // State 3
nfa.addState(); // State 4: Final state
nfa.startState = 0;
// Add transitions for a(a|bb)
nfa.addTransition(0, 'ε', 1);
nfa.addTransition(1, 'a', 2);
nfa.addTransition(2, 'ε', 3);
// a|bb
// Branch for 'a'
nfa.addTransition(3, 'ε', 4);
nfa.addTransition(4, 'a', 5);
nfa.addTransition(5, 'ε', 6);
// Branch for 'bb'
nfa.addTransition(3, 'ε', 7);
nfa.addTransition(7, 'b', 8);
nfa.addTransition(8, 'b', 9);
nfa.addTransition(9, 'ε', 6);
// Final state
nfa.states[6].isFinal = true;
// Apply Kleene Star
// Add new start and final states with ε-transitions
nfa.addState(); // New start state
nfa.addTransition(10, 'ε', 0);
nfa.addTransition(10, 'ε', 11);
nfa.addTransition(6, 'ε', 0);
nfa.addTransition(6, 'ε', 11);
nfa.states[11].isFinal = true;
// Update start state
nfa.startState = 10;
// Display NFA
for(auto &state : nfa.states){
std::cout << "State " << state.id << (state.isFinal ? " (Final)" : "") << ":\n";
for(auto &trans : state.transitions){
std::cout << " --" << (trans.first == 'ε' ? "ε" : std::string(1, trans.first)) << "--> ";
for(auto &dest : trans.second){
std::cout << dest << " ";
}
std::cout << "\n";
}
}
return 0;
}
// Question 2b: DFA obtained from the NFA using Subset Construction
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <queue>
struct DFA_State {
int id;
std::map<char, int> transitions; // key: input symbol, value: next state ID
bool isFinal;
};
struct DFA {
std::vector<DFA_State> states;
int startState;
void addState(bool final = false) {
states.emplace_back();
states.back().id = states.size() - 1;
states.back().isFinal = final;
}
};
int main() {
// Assume NFA has been constructed as per Question 2a
// For brevity, we represent NFA transitions here
// This is a simplified representation
// In practice, you'd extract this from the NFA structure
// Define NFA transitions
std::map<int, std::map<char, std::vector<int>>> nfaTransitions;
// Populate with NFA transitions from Question 2a
// ...
// Placeholder for NFA transitions
// Please replace with actual transitions from NFA
// Initialize DFA
DFA dfa;
// Add start state
dfa.addState(false);
dfa.startState = 0;
// Implement subset construction algorithm
// ...
// Display DFA
for(auto &state : dfa.states){
std::cout << "DFA State " << state.id << (state.isFinal ? " (Final)" : "") << ":\n";
for(auto &trans : state.transitions){
std::cout << " --" << trans.first << "--> " << trans.second << "\n";
}
}
return 0;
}
// Question 2c: Minimized DFA from the DFA obtained in 2b
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
// Assuming the DFA from Question 2b is already defined
struct Minimized_DFA_State {
int id;
std::map<char, int> transitions;
bool isFinal;
};
struct Minimized_DFA {
std::vector<Minimized_DFA_State> states;
int startState;
};
int main() {
// Placeholder for DFA states and transitions from Question 2b
// Implement DFA minimization algorithm (e.g., Hopcroft's algorithm)
// ...
// Initialize Minimized DFA
Minimized_DFA minDFA;
// Populate minDFA.states and minDFA.transitions based on minimization
// Display Minimized DFA
for(auto &state : minDFA.states){
std::cout << "Minimized DFA State " << state.id << (state.isFinal ? " (Final)" : "") << ":\n";
for(auto &trans : state.transitions){
std::cout << " --" << trans.first << "--> " << trans.second << "\n";
}
}
return 0;
}