Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
PDAToRHPDA.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
28#include <automaton/PDA/NPDA.h>
29#include <automaton/PDA/DPDA.h>
30
32
33#include <alib/set>
36
37namespace automaton {
38
39namespace transform {
40
45public:
51 template < class InputSymbolType, class PushdownStoreSymbolType, class StateType >
53
57 template < class InputSymbolType, class PushdownStoreSymbolType, class StateType >
59
63 template < class InputSymbolType, class PushdownStoreSymbolType, class StateType >
65
69 template < class InputSymbolType, class PushdownStoreSymbolType, class StateType >
71};
72
73template < class InputSymbolType, class PushdownStoreSymbolType, class StateType >
75 return pda;
76}
77
78template < class InputSymbolType, class PushdownStoreSymbolType, class StateType >
80 return pda;
81}
82
83template < class InputSymbolType, class PushdownStoreSymbolType, class StateType >
85 ext::variant < StateType, std::string > q0 = common::createUnique ( label::InitialStateLabel::instance < StateType > ( ), pda.getStates ( ) );
86
87 RealTimeHeightDeterministicDPDA < InputSymbolType, PushdownStoreSymbolType, ext::variant < StateType, std::string > > res ( q0, alphabet::BottomOfTheStackSymbol::instance < InputSymbolType > ( ) );
88
89 res.setInputAlphabet ( pda.getInputAlphabet ( ) );
90
91 for ( const auto & state : pda.getStates ( ) )
92 res.addState ( state );
93 for ( const auto & state : pda.getFinalStates ( ) )
94 res.addFinalState ( state );
95
96 ext::set < InputSymbolType > pushdownStoreAlphabet = pda.getPushdownStoreAlphabet ( );
97 pushdownStoreAlphabet.insert ( alphabet::BottomOfTheStackSymbol::instance < InputSymbolType > ( ) );
98 res.setPushdownStoreAlphabet ( pushdownStoreAlphabet );
99
100 res.addCallTransition ( q0, pda.getInitialState ( ), pda.getInitialSymbol ( ) );
101
102 std::string us ( "us" );
103 int i = 0;
104
105 for ( const auto & transition : pda.getTransitions ( ) ) {
106 const auto & to = transition.second;
107
108 if ( ( std::get < 2 > ( transition.first ).empty ( ) ) && ( to.second.empty ( ) ) ) {
109 res.addLocalTransition ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ), to.first );
110 } else if ( ( std::get < 2 > ( transition.first ).size ( ) == 1 ) && ( to.second.empty ( ) ) ) {
111 res.addReturnTransition ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ), std::get < 2 > ( transition.first )[0], to.first );
112 } else if ( ( std::get < 2 > ( transition.first ).empty ( ) ) && ( to.second.size ( ) == 1 ) ) {
113 res.addCallTransition ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ), to.first, to.second[0] );
114 } else {
115 int popPushIndex = 0;
116 int popPushSymbols = std::get < 2 > ( transition.first ).size ( ) + to.second.size ( );
117
119 for ( const InputSymbolType & pop :std::get < 2 > ( transition.first ) ) {
120 ext::variant < StateType, std::string > fromState = ( popPushIndex == 0 ) ? ext::variant < StateType, std::string > ( std::get < 0 > ( transition.first ) ) : ext::variant < StateType, std::string > ( lastUS );
121
122 if ( popPushIndex != 0 ) lastUS = common::createUnique ( us + ext::to_string ( ++i ), res.getStates ( ) );
123
124 ext::variant < StateType, std::string > toState = ( popPushIndex == popPushSymbols - 1 ) ? ext::variant < StateType, std::string > ( to.first ) : ext::variant < StateType, std::string > ( lastUS );
125
126 res.addState ( fromState );
127 res.addState ( toState );
128
129 if ( popPushIndex == 0 )
130 res.addReturnTransition ( fromState, std::get < 1 > ( transition.first ), pop, toState );
131 else
132 res.addReturnTransition ( fromState, pop, toState );
133
134 popPushIndex++;
135 }
136 for ( const InputSymbolType & push : ext::make_reverse ( to.second ) ) {
137 ext::variant < StateType, std::string > fromState = ( popPushIndex == 0 ) ? ext::variant < StateType, std::string > ( std::get < 0 > ( transition.first ) ) : ext::variant < StateType, std::string > ( lastUS );
138
139 if ( popPushIndex != 0 ) lastUS = common::createUnique ( us + ext::to_string ( ++i ), res.getStates ( ) );
140
141 ext::variant < StateType, std::string > toState = ( popPushIndex == popPushSymbols - 1 ) ? ext::variant < StateType, std::string > ( to.first ) : ext::variant < StateType, std::string > ( lastUS );
142
143 res.addState ( fromState );
144 res.addState ( toState );
145
146 if ( popPushIndex == 0 )
147 res.addCallTransition ( fromState, std::get < 1 > ( transition.first ), toState, push );
148 else
149 res.addCallTransition ( fromState, toState, push );
150
151 popPushIndex++;
152 }
153 }
154 }
155
156 return res;
157}
158
159template < class InputSymbolType, class PushdownStoreSymbolType, class StateType >
161 RealTimeHeightDeterministicNPDA < InputSymbolType, PushdownStoreSymbolType, ext::variant < StateType, std::string > > res ( alphabet::BottomOfTheStackSymbol::instance < DefaultSymbolType > ( ) );
162
163 res.setInputAlphabet ( pda.getInputAlphabet ( ) );
164
165 for ( const auto & state : pda.getStates ( ) )
166 res.addState ( state );
167 for ( const auto & state : pda.getFinalStates ( ) )
168 res.addFinalState ( state );
169
170 ext::set < InputSymbolType > pushdownStoreAlphabet = pda.getPushdownStoreAlphabet ( );
171 pushdownStoreAlphabet.insert ( alphabet::BottomOfTheStackSymbol::instance < InputSymbolType > ( ) );
172 res.setPushdownStoreAlphabet ( pushdownStoreAlphabet );
173
174 StateType q0 = common::createUnique ( label::InitialStateLabel::instance < StateType > ( ), res.getStates ( ) );
175 res.addState ( q0 );
176 res.addInitialState ( q0 );
177
178 res.addCallTransition ( q0, pda.getInitialState ( ), pda.getInitialSymbol ( ) );
179
180 std::string us ( "us" );
181 int i = 0;
182
183 for ( const auto & transition : pda.getTransitions ( ) )
184 if ( ( std::get < 2 > ( transition.first ).empty ( ) ) && ( transition.second.second.empty ( ) ) ) {
185 res.addLocalTransition ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ), transition.second.first );
186 } else if ( ( std::get < 2 > ( transition.first ).size ( ) == 1 ) && ( transition.second.second.empty ( ) ) ) {
187 res.addReturnTransition ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ), std::get < 2 > ( transition.first )[0], transition.second.first );
188 } else if ( ( std::get < 2 > ( transition.first ).empty ( ) ) && ( transition.second.second.size ( ) == 1 ) ) {
189 res.addCallTransition ( std::get < 0 > ( transition.first ), std::get < 1 > ( transition.first ), transition.second.first, transition.second.second[0] );
190 } else {
191 int popPushIndex = 0;
192 int popPushSymbols = std::get < 2 > ( transition.first ).size ( ) + transition.second.second.size ( );
193
194 std::string lastUS = common::createUnique ( us + ext::to_string ( i ), res.getStates ( ) );
195 std::for_each ( std::get < 2 > ( transition.first ).begin ( ), std::get < 2 > ( transition.first ).end ( ), [&] ( const InputSymbolType & pop ) {
196 ext::variant < StateType, std::string > fromState = ( popPushIndex == 0 ) ? ext::variant < StateType, std::string > ( std::get < 0 > ( transition.first ) ) : ext::variant < StateType, std::string > ( lastUS );
197
198 if ( popPushIndex != 0 ) lastUS = common::createUnique ( us + ext::to_string ( ++i ), res.getStates ( ) );
199
200 ext::variant < StateType, std::string > toState = ( popPushIndex == popPushSymbols - 1 ) ? ext::variant < StateType, std::string > ( transition.second.first ) : ext::variant < StateType, std::string > ( lastUS );
201
202 res.addState ( fromState );
203 res.addState ( toState );
204
205 if ( popPushIndex == 0 )
206 res.addReturnTransition ( fromState, std::get < 1 > ( transition.first ), pop, toState );
207 else
208 res.addReturnTransition ( fromState, pop, toState );
209
210 popPushIndex++;
211 } );
212 std::for_each ( transition.second.second.rbegin ( ), transition.second.second.rend ( ), [&] ( const InputSymbolType & push ) {
213 ext::variant < StateType, std::string > fromState = ( popPushIndex == 0 ) ? ext::variant < StateType, std::string > ( std::get < 0 > ( transition.first ) ) : ext::variant < StateType, std::string > ( lastUS );
214
215 if ( popPushIndex != 0 ) lastUS = common::createUnique ( us + ext::to_string ( ++i ), res.getStates ( ) );
216
217 ext::variant < StateType, std::string > toState = ( popPushIndex == popPushSymbols - 1 ) ? ext::variant < StateType, std::string > ( transition.second.first ) : ext::variant < StateType, std::string > ( lastUS );
218
219 res.addState ( fromState );
220 res.addState ( toState );
221
222 if ( popPushIndex == 0 )
223 res.addCallTransition ( fromState, std::get < 1 > ( transition.first ), toState, push );
224 else
225 res.addCallTransition ( fromState, toState, push );
226
227 popPushIndex++;
228 } );
229 }
230
231 return res;
232}
233
234} /* namespace transform */
235
236} /* namespace automaton */
Deterministic pushdown automaton. Accepts subset of context free languages.
Definition: DPDA.h:78
const PushdownStoreSymbolType & getInitialSymbol() const &
Definition: DPDA.h:301
const ext::set< PushdownStoreSymbolType > & getPushdownStoreAlphabet() const &
Definition: DPDA.h:243
const ext::set< InputSymbolType > & getInputAlphabet() const &
Definition: DPDA.h:330
const ext::set< StateType > & getStates() const &
Definition: DPDA.h:145
const ext::map< ext::tuple< StateType, common::symbol_or_epsilon< InputSymbolType >, ext::vector< PushdownStoreSymbolType > >, ext::pair< StateType, ext::vector< PushdownStoreSymbolType > > > & getTransitions() const &
Definition: DPDA.h:682
const ext::set< StateType > & getFinalStates() const &
Definition: DPDA.h:194
const StateType & getInitialState() const &
Definition: DPDA.h:116
Definition: NPDA.h:74
const PushdownStoreSymbolType & getInitialSymbol() const &
Definition: NPDA.h:304
const ext::set< StateType > & getFinalStates() const &
Definition: NPDA.h:197
const StateType & getInitialState() const &
Definition: NPDA.h:119
const ext::set< StateType > & getStates() const &
Definition: NPDA.h:148
const ext::multimap< ext::tuple< StateType, common::symbol_or_epsilon< InputSymbolType >, ext::vector< PushdownStoreSymbolType > >, ext::pair< StateType, ext::vector< PushdownStoreSymbolType > > > & getTransitions() const &
Definition: NPDA.h:644
const ext::set< InputSymbolType > & getInputAlphabet() const &
Definition: NPDA.h:333
const ext::set< PushdownStoreSymbolType > & getPushdownStoreAlphabet() const &
Definition: NPDA.h:246
Deterministic real time height deterministic pushdown automaton. Accepts subset of context free langu...
Definition: RealTimeHeightDeterministicDPDA.h:89
Nondeterministic real time height deterministic pushdown automaton. Accepts subset of context free la...
Definition: RealTimeHeightDeterministicNPDA.h:76
Definition: PDAToRHPDA.h:44
static automaton::RealTimeHeightDeterministicDPDA< InputSymbolType, PushdownStoreSymbolType, StateType > convert(const automaton::RealTimeHeightDeterministicDPDA< InputSymbolType, PushdownStoreSymbolType, StateType > &pda)
Definition: PDAToRHPDA.h:74
Definition: set.hpp:44
Implementation of the variant class allowing to store any type of those listed in the template parame...
Definition: variant.hpp:98
int i
Definition: AllEpsilonClosure.h:118
StateType q0
Definition: SingleInitialState.h:96
typename T::StateType StateType
Definition: Compaction.h:76
return res
Definition: AutomataConcatenation.h:102
Definition: ToGrammar.h:31
T createUnique(T object, const Alphabets &... alphabets)
Definition: createUnique.hpp:46
reverser< T > make_reverse(T &&container)
Reverese adaptor construction function.
Definition: iterator.hpp:544
ContainerType< ResType > transform(const ContainerType< InType, Ts ... > &in, Callback transform)
In container tranformation of all elements according to the tranform.
Definition: algorithm.hpp:150
std::string to_string(const T &value)
To string method designated for objects that can be casted to string.
Definition: string.hpp:131