Compiler Assignment
Compiler Assignment
Q1. Design a Minimized DFA for the Regular Expression (a/b)*abb i.e. All strings ending with abb.
This will involve three steps:
STEPS:-
Step 1 Construct an NFA with Null moves f rom the given regular expression.
Step 2 Remove Null transition f rom the NFA and convert it into its equivalent DFA.
NFA f or (a+b)*abb
DFA
MIN DFA
DFA STATE Min – DFA a b
STATE
{A,C} 1 2 1
{B} 2 2 3
{D} 3 2 4
{E} 4 2 1
CODE:-
#include <bits/stdc++.h>
struct trans {
int from;
int to;
char symbol;
};
class DFA {
public:
vector<trans> transitions;
set<int> final_state;
int start_state;
DFA() {}
trans new_trans;
new_trans.from = f;
new_trans.to = s;
new_trans.symbol = symb;
transitions.push_back(new_trans);
void display() {
int i;
cout << "Start State: " << get_start_state() << " and ";
};
class NFA {
public:
vector<trans> transitions;
int final_state;
int start_state;
NFA() {}
trans new_trans;
new_trans.from = f;
new_trans.to = s;
new_trans.symbol = symb;
transitions.push_back(new_trans);
void display() {
int i;
cout << "Start State: " << get_start_state() << " and ";
int i;
maxi = transitions[i].from;
mini = transitions[i].from;
mini = transitions[i].to;
adj[transitions[i].from].push_back(
{transitions[i].to, transitions[i].symbol});
return adj;
vector<char> make_symbols_list() {
set<char> s;
vector<char> result;
int i;
s.find(transitions[i].symbol) == s.end()) {
s.insert(transitions[i].symbol);
result.push_back(transitions[i].symbol);
return result;
};
NFA or_op(NFA temp1, NFA temp2, int &state) {
NFA result;
int i;
result.set_transitions(temp1.transitions[i].from, temp1.transitions[i].to,
temp1.transitions[i].symbol);
result.set_transitions(temp2.transitions[i].from, temp2.transitions[i].to,
temp2.transitions[i].symbol);
result.set_final_state(new_final_state);
result.set_start_state(new_start_state);
return result;
NFA result;
int i;
temp1.transitions[i].to != prev_final_state1) {
result.set_transitions(temp1.transitions[i].from,
temp1.transitions[i].to,
temp1.transitions[i].symbol);
if (temp1.transitions[i].from == prev_final_state1) {
temp1.transitions[i].from = prev_start_state2;
result.set_transitions(temp1.transitions[i].from,
temp1.transitions[i].to,
temp1.transitions[i].symbol);
if (temp1.transitions[i].to == prev_final_state1) {
temp1.transitions[i].to = prev_start_state2;
result.set_transitions(temp1.transitions[i].from,
temp1.transitions[i].to,
temp1.transitions[i].symbol);
result.set_transitions(temp2.transitions[i].from, temp2.transitions[i].to,
temp2.transitions[i].symbol);
}
result.set_final_state(prev_final_state2);
result.set_start_state(prev_start_state1);
return result;
NFA result;
int i;
result.set_transitions(temp.transitions[i].from, temp.transitions[i].to,
temp.transitions[i].symbol);
result.set_final_state(new_final_state);
result.set_start_state(new_start_state);
return result;
int state = 0;
int i;
stack<NFA> operands;
stack<char> operators;
NFA *temp_nfa;
if (re[i] != '(' && re[i] != ')' && re[i] != '*' && re[i] != '/' &&
re[i] != '.') {
int s = state++;
int f = state++;
temp_nfa->set_transitions(s, f, re[i]);
temp_nfa->set_final_state(f);
temp_nfa->set_start_state(s);
operands.push(*temp_nfa);
delete (temp_nfa);
operands.pop();
operands.push(kleene_closure(kleene, state));
operators.push(re[i]);
operators.push(re[i]);
operators.push(re[i]);
} else {
NFA a, b;
operators.pop();
b = operands.top();
operands.pop();
a = operands.top();
operands.pop();
if (op == '.') {
operands.push(concat(a, b));
if (op == '/') {
operands.push(or_op(a, b, state));
operators.pop();
if (operators.size() > 0) {
NFA a, b;
while (operators.size() != 0) {
char op = operators.top();
operators.pop();
b = operands.top();
operands.pop();
a = operands.top();
operands.pop();
if (op == '.') {
operands.push(concat(a, b));
if (op == '/') {
operands.push(or_op(a, b, state));
}
return operands.top();
s.insert(j.first);
return;
int state = 1;
DFA result;
int i;
set<int> s;
s.insert(i);
null_enclosures[i] = s;
set<int> st;
queue<set<int>> null_stack;
null_stack.push(null_enclosures[start_state]);
mp[null_enclosures[start_state]] = state++;
while (1) {
if (null_stack.size() == 0)
break;
st = null_stack.front();
null_stack.pop();
set<int> s;
set<int> temp;
set<int> store;
inserter(store, store.end()));
s = store;
set<int> sick;
for (auto j : s) {
set<int> store;
inserter(store, store.end()));
sick = store;
mp[sick] = state++;
null_stack.push(sick);
if (sick.size() > 0) {
if (st.find(start_state) != st.end()) {
result.set_start_state(mp[st]);
if (sick.find(start_state) != sick.end()) {
result.set_start_state(mp[sick]);
if (st.find(final_state) != st.end()) {
result.set_final_state(mp[st]);
if (sick.find(final_state) != sick.end()) {
result.set_final_state(mp[sick]);
return result;
int main() {
string re;
NFA ans;
ans = re_to_nfa(re);
cout << "From RE to NFA using Thompson's construction: " << endl;
"------------"
<< endl;
ans.display();
cout << "From NFA to DFA using Subset construction: " << endl;
ans2.display();