Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ToRegExpStateElimination.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
27
28#include <automaton/FSM/DFA.h>
29#include <automaton/FSM/NFA.h>
33
38
40
42
43namespace automaton {
44
45namespace convert {
46
52public:
61 template < class T >
63
64private:
71 template < class SymbolType, class StateType >
72 static void extendExtendedNFA(automaton::ExtendedNFA < SymbolType, StateType > & automaton );
73
84 template < class SymbolType, class StateType >
86
95 template < class SymbolType, class StateType >
97};
98
99template < class T >
101 using SymbolType = typename T::SymbolType;
102 using StateType = typename T::StateType;
103
104 if ( automaton.getFinalStates ( ).empty ( ) )
106
107 // steps 1 + 2
109 extendExtendedNFA ( extendedAutomaton );
110
111 // step 3 - Exterminate!
112 // select all states that are neither final nor initial
113 ext::set < StateType > statesToEliminate = extendedAutomaton.getStates ( );
114 statesToEliminate.erase ( extendedAutomaton.getInitialState ( ) );
115 statesToEliminate.erase ( * extendedAutomaton.getFinalStates ( ).begin ( ) );
116
117 for ( const StateType & state : statesToEliminate )
118 extendedAutomaton = eliminateState ( extendedAutomaton, state );
119
120 // step 4
121 regexp::UnboundedRegExpStructure < SymbolType > finalStateLoop = regexp::transform::RegExpIterate::iterate ( transitionsToRegExp ( extendedAutomaton, * extendedAutomaton.getFinalStates ( ).begin ( ), * extendedAutomaton.getFinalStates ( ).begin ( ) ) );
122 regexp::UnboundedRegExpStructure < SymbolType > initialToFinalState = regexp::transform::RegExpConcatenate::concatenate ( transitionsToRegExp ( extendedAutomaton, extendedAutomaton.getInitialState ( ), *extendedAutomaton.getFinalStates ( ).begin ( ) ),finalStateLoop );
124}
125
126template < class SymbolType, class StateType >
127automaton::ExtendedNFA < SymbolType, StateType > ToRegExpStateElimination::eliminateState ( const automaton::ExtendedNFA < SymbolType, StateType > & extendedAutomaton, const StateType & q ) {
128 automaton::ExtendedNFA < SymbolType, StateType > newAutomaton ( extendedAutomaton.getInitialState ( ) ); // sure that q is neither initial nor final (follows from step 2 - extending ExtendedNFA)
129 newAutomaton.setStates ( extendedAutomaton.getStates ( ) );
130 newAutomaton.removeState ( q ); // preserve all states but q (the one to eliminate)
131 newAutomaton.setInputAlphabet ( extendedAutomaton.getInputAlphabet ( ) );
132 newAutomaton.setFinalStates ( extendedAutomaton.getFinalStates ( ) );
133
134 for ( const StateType & p: newAutomaton.getStates ( ) ) {
135 for ( const StateType & r : newAutomaton.getStates ( ) ) {
136 regexp::UnboundedRegExpStructure < SymbolType > concat = transitionsToRegExp ( extendedAutomaton, p, q );
137 concat = regexp::transform::RegExpConcatenate::concatenate ( concat, regexp::transform::RegExpIterate::iterate ( transitionsToRegExp ( extendedAutomaton, q, q ) ) );
138 concat = regexp::transform::RegExpConcatenate::concatenate ( concat, transitionsToRegExp ( extendedAutomaton, q, r ) );
139
140 regexp::UnboundedRegExpStructure < SymbolType > alt = regexp::transform::RegExpAlternate::alternate ( concat, transitionsToRegExp ( extendedAutomaton, p, r ) );
141
142 newAutomaton.addTransition ( p, regexp::simplify::RegExpOptimize::optimize ( alt ), r );
143 }
144 }
145
146 return newAutomaton;
147}
148
149template < class SymbolType, class StateType >
150regexp::UnboundedRegExpStructure < SymbolType > ToRegExpStateElimination::transitionsToRegExp ( const automaton::ExtendedNFA < SymbolType, StateType > & automaton, const StateType & from, const StateType & to ) {
152
153 for ( const auto & transition: automaton.getTransitionsFromState ( from ) )
154 if ( transition.second == to )
155 ret = regexp::transform::RegExpAlternate::alternate ( ret, transition.first.second );
156
158}
159
160template < class SymbolType, class StateType >
161void ToRegExpStateElimination::extendExtendedNFA ( automaton::ExtendedNFA < SymbolType, StateType > & automaton ) {
162 const StateType & initState = automaton.getInitialState ( );
163 if ( automaton.getFinalStates ( ).contains ( initState ) || ! automaton.getTransitionsToState ( initState ).empty ( ) ) {
164 StateType q0 = common::createUnique ( initState, automaton.getStates ( ) );
165 automaton.addState ( q0 );
166
168 automaton.addTransition ( q0, regexp, initState );
169
170 automaton.setInitialState ( q0 );
171 }
172
173 if ( automaton.getFinalStates ( ).size ( ) > 1 ) {
174 StateType f = common::createUnique ( label::FinalStateLabel::instance < StateType > ( ), automaton.getStates ( ) );
175 automaton.addState ( f );
176
177 for ( const StateType & state : automaton.getFinalStates ( ) ) {
179 automaton.addTransition ( state, regexp, f );
180 }
181
182 ext::set < StateType > newFinalStates;
183 newFinalStates.insert ( f );
184 automaton.setFinalStates ( newFinalStates );
185 }
186}
187
188} /* namespace convert */
189
190} /* namespace automaton */
191
Extended nondeterministic finite automaton. Accepts regular languages. The automaton has a regular ex...
Definition: ExtendedNFA.h:80
const StateType & getInitialState() const &
Definition: ExtendedNFA.h:156
const ext::set< SymbolType > & getInputAlphabet() const &
Definition: ExtendedNFA.h:283
const ext::set< StateType > & getStates() const &
Definition: ExtendedNFA.h:185
const ext::set< StateType > & getFinalStates() const &
Definition: ExtendedNFA.h:234
Definition: ToRegExpStateElimination.h:51
static regexp::UnboundedRegExp< typename T::SymbolType > convert(const T &automaton)
Definition: ToRegExpStateElimination.h:100
Definition: set.hpp:44
Represents the empty expression in the regular expression. The node can't have any children.
Definition: UnboundedRegExpEmpty.h:41
Represents the epsilon expression in the regular expression. The node can't have any children.
Definition: UnboundedRegExpEpsilon.h:41
Represents unbounded regular expression structure. Regular expression is stored as a tree of Unbounde...
Definition: UnboundedRegExpStructure.h:47
Unbounded regular expression represents regular expression. It describes regular languages....
Definition: UnboundedRegExp.h:80
static regexp::UnboundedRegExp< SymbolType > optimize(const regexp::UnboundedRegExp< SymbolType > &regexp)
static regexp::FormalRegExp< SymbolType > alternate(const regexp::FormalRegExp< SymbolType > &first, const regexp::FormalRegExp< SymbolType > &second)
Definition: RegExpAlternate.h:57
static regexp::FormalRegExp< SymbolType > concatenate(const regexp::FormalRegExp< SymbolType > &first, const regexp::FormalRegExp< SymbolType > &second)
Definition: RegExpConcatenate.h:57
static regexp::FormalRegExp< SymbolType > iterate(const regexp::FormalRegExp< SymbolType > &regexp)
Definition: RegExpIterate.h:56
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
ext::set< ext::pair< StateType, StateType > > ret(const ext::set< ext::pair< StateType, StateType > > &S, const DeterministicPushdownStoreSymbolType &pdaSymbol, const InputSymbolType &input, const N &nondeterministic)
Definition: RHDPDACommon.h:57
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
q
Definition: SingleInitialStateEpsilonTransition.h:85
StateType q0
Definition: SingleInitialState.h:96
Definition: ToGrammar.h:31
T createUnique(T object, const Alphabets &... alphabets)
Definition: createUnique.hpp:46
Definition: converterCommon.hpp:8
Definition: ToAutomaton.h:15