0% found this document useful (0 votes)
84 views5 pages

Automata

The document contains C++ code that defines functions to test examples and exercises for converting non-deterministic finite automata (NFA) to deterministic finite automata (DFA). It initializes NFAs based on examples from textbook pages, converts them to DFAs using different subset construction methods, and minimizes the resulting DFA states. The main function calls the example testing functions to demonstrate the NFA to DFA conversion process.

Uploaded by

Morcilla Iver
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views5 pages

Automata

The document contains C++ code that defines functions to test examples and exercises for converting non-deterministic finite automata (NFA) to deterministic finite automata (DFA). It initializes NFAs based on examples from textbook pages, converts them to DFAs using different subset construction methods, and minimizes the resulting DFA states. The main function calls the example testing functions to demonstrate the NFA to DFA conversion process.

Uploaded by

Morcilla Iver
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

// 00-FiniteAutomata.cpp : This file contains the 'main' function.

Program
execution begins and ends there.
//
// Author: Thomas Kim
// First Edit: Mar. 26, 2019
//

#include "pch.h"

auto& stream = std::cout;

auto nl = "\n";
auto nL2 = "\n\n";

void display_title(const std::string& desc,


const std::string& fname, bool bRuler=true)
{
static int count = 0; ++count;

std::string ruler("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
"-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\n");

if (bRuler)
stream << ruler << ruler<<nl;

stream << count << ". " << desc


<< "\n\n\tin the function - "<<fname<<nL2;
}

void test_dfa_example_2_4_page_50()
{
display_title("DFA, Deterministic Finite Automata Initialization"
"\n\t-How to initialize"
"\n\t-How to test ACCEPT",
"test_dfa_example_2_4_page_50()");

using namespace tpf::io;

using dfa_t = tpf::DFA<int, char>;


using symbols_t = dfa_t::symbols_t;

dfa_t dfa{ "Example 2.4", "Page - 50" };

dfa.set_1_symbols(0, 1)
.set_2_start_accept_states('0', '0')
.set_3_transitions('2', '1', '3', '0', '0', '3', '1', '2');

stream << dfa;

symbols_t symbols{ 1, 1, 0, 1, 0, 1 };

stream << nl << symbols


<< " accepted ? " << dfa.accept(symbols) << nl << nl;
}

void test_nfa_example_2_7_page_57()
{
display_title("NFA to DFA Compact Conversion"
"\n\t-How to convert NFA to DFA using Compact Conversion",
"test_nfa_example_2_7_page_57()");

using symbol_t = int;


using state_t = int;
using nfa_t = tpf::NFA<symbol_t, state_t>;

nfa_t nfa{ "Example 2.7", "Figure 2.11, Page-57" };

nfa.set_1_symbols(0, 1)
.set_2_start_accept_states(0, 2)
.set_3_transitions({ {0, 1}, {0}, {}, {2}, {}, {} });

stream << "Created NFA ----- " << nL2;


stream << nfa << nl;

stream << "NFA converted to DFA with Compact ------ " << nL2;

stream << nfa.create_dfa() << nl;


}

void test_nfa_example_2_7_page_61_full_subset_construction()
{
display_title("NFA to DFA Conversion with Full Subset Construction",
"test_nfa_example_2_7_page_61_full_subset_construction()");

using symbol_t = int;


using state_t = int;
using nfa_t = tpf::NFA<symbol_t, state_t>;

nfa_t nfa{ "Example 2.7", "Figure 2.12, Page-61" };

nfa.set_1_symbols(0, 1)
.set_2_start_accept_states(0, 2)
.set_3_transitions({ {0, 1}, {0}, {}, {2}, {}, {} });

stream << "NFA created -------- " << nL2;

stream << nfa << nl;

stream << "NFA converted to DFA with Full Subset Construction -------- " <<
nL2;

stream << nfa.create_dfa(false) << nl;


}

void test_nfa_exercise_2_3_1_page_65()
{
display_title("NFA to DFA Conversion More Example",
"test_nfa_exercise_2_3_1_page_65()");

using symbol_t = int;


using state_t = char;
using nfa_t = tpf::NFA<symbol_t, state_t>;

nfa_t nfa{ "Exercise 2.3.1", "Page-65" };

nfa.set_1_symbols(0, 1)
.set_2_start_accept_states('p', 's')
.set_3_transitions({ {'p', 'q'}, {'p'},
{'r'}, {'r'},
{'s'}, { },
{'s'}, {'s'} });

stream << "NFA created -------" << nL2;


stream << nfa << nl;

stream << "Converted to DFA with Full Subset Construction" << nL2;
stream << nfa.create_dfa(false) << nl;

stream << "Converted to DFA with Compact Subset Construction" << nL2;
stream << nfa.create_dfa() << nl;
}

void test_nfa_exercise_2_3_2_page_66()
{
display_title("NFA to DFA More Example",
"test_nfa_exercise_2_3_2_page_66()");

using symbol_t = int;


using state_t = char;
using nfa_t = tpf::NFA<symbol_t, state_t>;

nfa_t nfa{ "Exercise 2.3.2", "Page-66" };

nfa.set_1_symbols(0, 1)
.set_2_start_accept_states('p', 'q', 's')
.set_3_transitions({ {'q', 's'}, {'q'},
{'r'}, {'q', 'r'},
{'s'}, {'p'},
{ }, {'p'} });

stream << "NFA created ------ " << nL2;


stream << nfa << nl;

stream << "NFA to DFA Conversion - Full Subject Construction ------" << nL2;
stream << nfa.create_dfa(false) << nl;

stream << "NFA to DFA Conversion - Compact -------" << nL2;


stream << nfa.create_dfa() << nl;

void test_nfa_exercise_2_3_3_page_66()
{
display_title("NFA to DFA More Example",
"test_nfa_exercise_2_3_3_page_66()");

using symbol_t = int;


using state_t = char;
using nfa_t = tpf::NFA<symbol_t, state_t>;

nfa_t nfa{ "Exercise 2.3.3", "Page-66" };

nfa.set_1_symbols(0, 1)
.set_2_start_accept_states('p', 's', 't')
.set_3_transitions({ {'p', 'q'}, {'p'},
{'r', 's'}, {'t'},
{'p', 'r'}, {'t'},
{ }, { },
{ }, { } });

stream << "NFA created ------ " << nL2;


stream << nfa << nl;

stream << "NFA to DFA Conversion - Full Subject Construction ------" << nL2;
stream << nfa.create_dfa(false) << nl;

stream << "NFA to DFA Conversion - Compact -------" << nL2;


stream << nfa.create_dfa() << nl;
}

void test_dfa_minimization_Myhill_Nerode_Theorem()
{
display_title("Minimization of DFA - Myhill Nerode Theorem",
"test_dfa_minimization_Myhill_Nerode_Theorem()");

using namespace tpf::io;

using symbol_t = int;


using state_t = char;
using dfa_t = tpf::DFA<symbol_t, state_t>;
using states_t = typename dfa_t::states_t;
using symbols_t = typename dfa_t::symbols_t;

dfa_t dfa{ "Minimization of DFA", "By Thomas Kim" };

dfa.set_1_symbols(0, 1);
dfa.set_2_start_accept_states('A', 'C', 'D', 'E');

// DFA | 0 | 1 |
dfa.set_3_transitions( //======+=======+=======+=
'B', 'C', // ->A | B | C |
'A', 'D', // B | A | D |
'E', 'F', // *C | E | F |
'E', 'F', // *D | E | F |
'E', 'F', // *E | E | F |
'F', 'F'); // F | F | F |
//======+=======+=======+=

stream << "DFA Before Minimization -------- " << nL2;


stream << dfa << nL2;

dfa.minimize_states();

stream << "DFA After Minimization -------- " << nL2;


stream << dfa << nl;
}

void test_nfa_exercise_2_3_2_page_66_and_minimize_dfa()
{
display_title("NFA to DFA More Example"
"\n\t1. Create NFA"
"\n\t2. Convert NFA to DFA"
"\n\t3. Minimize DFA States",
"test_nfa_exercise_2_3_2_page_66_and_minimize_dfa()");
using symbol_t = int;
using state_t = char;
using nfa_t = tpf::NFA<symbol_t, state_t>;

nfa_t nfa{ "Exercise 2.3.2", "Page-66" };

nfa.set_1_symbols(0, 1)
.set_2_start_accept_states('p', 'q', 's')
.set_3_transitions({ {'q', 's'}, {'q'},
{'r'}, {'q', 'r'},
{'s'}, {'p'},
{ }, {'p'} });

stream << "NFA created ------ " << nL2;


stream << nfa << nl;

stream << "NFA to DFA Conversion - Full Subject Construction ------" << nL2;
stream << nfa.create_dfa(false) << nl;

stream << "NFA to DFA Conversion, Before Minimized -------" << nL2;
auto dfa = nfa.create_dfa();

stream << dfa << nl;

dfa.minimize_states();

stream << "NFA to DFA Conversion, After Minimized -------" << nL2;
stream << dfa << nl;
}

void test_all_examples()
{
test_dfa_example_2_4_page_50();
test_nfa_example_2_7_page_57();
test_nfa_example_2_7_page_61_full_subset_construction();
test_nfa_exercise_2_3_1_page_65();
test_nfa_exercise_2_3_2_page_66();
test_nfa_exercise_2_3_3_page_66();
test_dfa_minimization_Myhill_Nerode_Theorem();
test_nfa_exercise_2_3_2_page_66_and_minimize_dfa();
}

int main()
{
stream << std::boolalpha << nl;

test_all_examples();

//test_dfa_example_2_4_page_50();
//test_nfa_example_2_7_page_57();
//test_nfa_example_2_7_page_61_full_subset_construction();
//test_nfa_exercise_2_3_1_page_65();
//test_nfa_exercise_2_3_2_page_66();
//test_nfa_exercise_2_3_3_page_66();
//test_dfa_minimization_Myhill_Nerode_Theorem();
//test_nfa_exercise_2_3_2_page_66_and_minimize_dfa();
}

You might also like