Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
AutomataConcatenation.h
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
26#include <automaton/FSM/NFA.h>
27#include <automaton/FSM/DFA.h>
28
30
32
39public:
48 template < class AutomatonType >
49 requires isDFA < AutomatonType > || isNFA < AutomatonType >
51};
52
53template < class AutomatonType >
54requires isDFA < AutomatonType > || isNFA < AutomatonType >
56 static const unsigned NONE = 0;
57 static const unsigned FIRST = 1;
58 static const unsigned SECOND = 2;
59
61
62 for ( const auto & q : first.getStates ( ) )
63 res.addState ( { q, FIRST } );
64 for ( const auto & q : second.getStates ( ) )
65 res.addState ( { q, SECOND } );
66
67 res.addInputSymbols ( first.getInputAlphabet ( ) );
68 res.addInputSymbols ( second.getInputAlphabet ( ) );
69
70 for ( const auto & t : first.getTransitions ( ) ) {
71 res.addTransition ( { t.first.first, FIRST }, t.first.second, { t.second, FIRST } );
72
73 if ( first.getFinalStates ( ).contains ( t.second ) )
74 res.addTransition ( { t.first.first, FIRST }, t.first.second, { second.getInitialState ( ), SECOND } );
75 }
76
77 for ( const auto& t : second.getTransitions ( ) )
78 res.addTransition ( { t.first.first, SECOND }, t.first.second, { t.second, SECOND } );
79
80 for ( const auto & q : second.getFinalStates ( ) )
81 res.addFinalState ( { q, SECOND } );
82
83 if ( first.getFinalStates ( ).contains ( first.getInitialState ( ) ) ) {
84 ext::pair < typename AutomatonType::StateType, unsigned > q01q02 ( label::InitialStateLabel::instance < typename AutomatonType::StateType > ( ), NONE );
85 res.addState ( q01q02 );
86 res.setInitialState ( q01q02 );
87
88 for ( const auto & t : first.getTransitionsFromState ( first.getInitialState ( ) ) ) {
89 res.addTransition ( q01q02, t.first.second, { t.second, FIRST } );
90
91 if ( first.getFinalStates ( ).contains ( t.second ) )
92 res.addTransition ( q01q02, t.first.second, { second.getInitialState ( ), SECOND } );
93 }
94
95 for ( const auto & t : second.getTransitionsFromState ( second.getInitialState ( ) ) )
96 res.addTransition ( q01q02, t.first.second, { t.second, SECOND } );
97
98 if ( second.getFinalStates().contains(second.getInitialState()))
99 res.addFinalState ( q01q02 );
100 }
101
102 return res;
103}
104
105} /* namespace automaton::transform */
Nondeterministic finite automaton. Accepts regular languages.
Definition: NFA.h:66
Definition: AutomataConcatenation.h:38
static automaton::NFA< typename AutomatonType::SymbolType, ext::pair< typename AutomatonType::StateType, unsigned > > concatenation(const AutomatonType &first, const AutomatonType &second)
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
q
Definition: SingleInitialStateEpsilonTransition.h:85
Definition: AutomataConcatenation.h:31
return res
Definition: AutomataConcatenation.h:102
t first second
Definition: AutomataConcatenation.h:78