Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
DeterminizeVPAPart.hxx
Go to the documentation of this file.
1
7
10#include <alib/set>
11
12namespace automaton {
13
14namespace determinize {
15
16template < class InputSymbolType, class PushdownStoreSymbolType, class StateType >
18 using DeterministicStateType = ext::set < ext::pair < StateType, StateType > >;
19 using DeterministicPushdownStoreSymbolType = ext::pair < DeterministicStateType, InputSymbolType >;
20
21 DeterministicStateType initialLabel = createIdentity(npda.getInitialStates());
22 DeterministicPushdownStoreSymbolType bottom = ext::make_pair ( DeterministicStateType { }, alphabet::BottomOfTheStackSymbol::instance < InputSymbolType > ( ) );
23
25
26 dpda.setCallInputAlphabet(npda.getCallInputAlphabet());
27 dpda.setLocalInputAlphabet(npda.getLocalInputAlphabet());
28 dpda.setReturnInputAlphabet(npda.getReturnInputAlphabet());
29
32
33 std::queue < DeterministicStateType > dirtyStates;
34 dirtyStates.push ( initialLabel );
35
36 std::queue < ext::pair<DeterministicStateType, DeterministicPushdownStoreSymbolType> > dirtyStateSymbols;
37 dirtyStateSymbols.push ( ext::make_pair ( initialLabel, bottom ) );
38
39 while ( ! dirtyStateSymbols.empty ( ) || ! dirtyStates.empty ( ) ) {
40 if ( ! dirtyStateSymbols.empty ( ) ) {
41 DeterministicStateType state = std::move ( dirtyStateSymbols.front ( ).first );
42 DeterministicPushdownStoreSymbolType pdaSymbol = std::move ( dirtyStateSymbols.front ( ).second );
43 dirtyStateSymbols.pop ( );
44
45 for ( const InputSymbolType & input : npda.getReturnInputAlphabet ( ) ) {
46 DeterministicStateType to;
47
48 if ( pdaSymbol == dpda.getBottomOfTheStackSymbol ( ) )
49 to = retInitial ( state, input, npda );
50 else
51 to = ret ( state, pdaSymbol, input, npda );
52
53 if ( to.empty ( ) )
54 continue;
55
56 if ( dpda.addState ( to ) )
57 dirtyStates.push ( to );
58
59 localClosure [ pdaSymbol.first ].insert ( to );
60
61 updateTopSymbols ( localClosure, topSymbols, dirtyStateSymbols, to, topSymbols [ pdaSymbol.first ] );
62
63 dpda.addReturnTransition ( state, input, pdaSymbol, std::move ( to ) );
64 }
65 } else { // ! dirtyStates.empty ( )
66 DeterministicStateType state = std::move ( dirtyStates.front ( ) );
67 dirtyStates.pop ( );
68
69 for ( const InputSymbolType & input : npda.getLocalInputAlphabet ( ) ) {
70 DeterministicStateType to = local ( state, input, npda );
71
72 if ( to.empty ( ) )
73 continue;
74
75 if ( dpda.addState ( to ) )
76 dirtyStates.push ( to );
77
78 localClosure [ state ].insert ( to );
79
80 updateTopSymbols ( localClosure, topSymbols, dirtyStateSymbols, to, topSymbols [ state ] );
81
82 dpda.addLocalTransition ( state, input, std::move ( to ) );
83 }
84 for ( const InputSymbolType & input : npda.getCallInputAlphabet ( ) ) {
85 DeterministicStateType to = call ( state, input, npda );
87
88 if ( to.empty ( ) )
89 continue;
90
91 if ( dpda.addState ( to ) )
92 dirtyStates.push ( to );
93
94 updateTopSymbols ( localClosure, topSymbols, dirtyStateSymbols, to, { pdaSymbol } );
95
96 dpda.addPushdownStoreSymbol ( pdaSymbol );
97 dpda.addCallTransition ( state, input, std::move ( to ), std::move ( pdaSymbol ) );
98 }
99 }
100 }
101
102 const ext::set<StateType>& finalLabels = npda.getFinalStates();
103 for(const DeterministicStateType & state : dpda.getStates()) {
104 ext::set<StateType> labels = retrieveDSubSet(state);
105
106 if(!ext::excludes(finalLabels.begin(), finalLabels.end(), labels.begin(), labels.end()))
107 dpda.addFinalState(state);
108 }
109
110 return dpda;
111}
112
113} /* namespace determinize */
114
115} /* namespace automaton */
Deterministic visibly pushdown automaton. Accepts subset of context free languages.
Definition: VisiblyPushdownDPDA.h:86
Nondeterministic visibly pushdown automaton. Accepts subset of context free languages.
Definition: VisiblyPushdownNPDA.h:81
const ext::set< StateType > & getInitialStates() const &
Definition: VisiblyPushdownNPDA.h:131
const ext::set< InputSymbolType > & getCallInputAlphabet() const &
Definition: VisiblyPushdownNPDA.h:365
const ext::set< StateType > & getFinalStates() const &
Definition: VisiblyPushdownNPDA.h:229
const ext::set< InputSymbolType > & getReturnInputAlphabet() const &
Definition: VisiblyPushdownNPDA.h:423
const ext::set< InputSymbolType > & getLocalInputAlphabet() const &
Definition: VisiblyPushdownNPDA.h:481
static automaton::DFA< SymbolType, StateType > determinize(const automaton::DFA< SymbolType, StateType > &automaton)
Definition: Determinize.h:276
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
std::pair< iterator, bool > insert(const T &key, const R &value)
Insert variant with explicit key and value parameters.
Definition: map.hpp:118
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Definition: set.hpp:44
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: set.hpp:99
auto end() &
Inherited behavior of end for non-const instance.
Definition: set.hpp:129
void updateTopSymbols(ext::map< DeterministicStateType, ext::set< DeterministicStateType > > &localClosure, ext::map< DeterministicStateType, ext::set< DeterministicPushdownStoreSymbolType > > &topSymbols, std::queue< ext::pair< DeterministicStateType, DeterministicPushdownStoreSymbolType > > &dirtyStateSymbols, const DeterministicStateType &state, const ext::set< DeterministicPushdownStoreSymbolType > &newSymbols)
Definition: RHDPDACommon.h:126
ext::set< ext::pair< StateType, StateType > > call(const ext::set< ext::pair< StateType, StateType > > &S, const InputSymbolType &input, const N &nondeterministic)
Definition: RHDPDACommon.h:94
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
ext::set< ext::pair< StateType, StateType > > createIdentity(const ext::set< StateType > &states)
Definition: RHDPDACommon.h:30
ext::set< StateType > retrieveDSubSet(const ext::set< ext::pair< StateType, StateType > > &localOperation)
Definition: RHDPDACommon.h:21
ext::set< ext::pair< StateType, StateType > > retInitial(const ext::set< ext::pair< StateType, StateType > > &S, const InputSymbolType &input, const N &nondeterministic)
Definition: RHDPDACommon.h:39
Definition: ToGrammar.h:31
bool excludes(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, Compare comp)
Tests two sorted ranges wheter all elements from the second are not present in the first.
Definition: algorithm.hpp:46
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79