Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ToPrefixPushdownAutomaton.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#include <alphabet/BarSymbol.h>
33
34namespace automaton {
35
36namespace convert {
37
42public:
48 template < class SymbolType, class StateType >
50
56 template < class SymbolType, class StateType >
58};
59
60template < class SymbolType, class StateType >
62 char nonFinalPDAState = 'q';
63 char finalPDAState = 'r';
65 automaton.addState ( finalPDAState );
66 automaton.addFinalState ( finalPDAState );
67
68 for ( const auto & symbol : afdza.getInputAlphabet ( ) ) {
69 automaton.addInputSymbol ( symbol );
70 }
71 automaton.addInputSymbol ( alphabet::BarSymbol::instance < SymbolType > ( ) );
72
73 for ( const StateType & state : afdza.getStates ( ) ) {
74 automaton.addPushdownStoreSymbol ( state );
75 }
76
77 for (const auto & transition : afdza.getTransitions()) {
78 const StateType & to = transition.second;
79
80 if ( transition.first.template is < SymbolType > ( ) ) {
81 const SymbolType & from = transition.first.template get < SymbolType > ( );
82
85
86 automaton.addTransition ( nonFinalPDAState, from, pop, nonFinalPDAState, push );
87 automaton.addTransition ( finalPDAState, from, pop, nonFinalPDAState, push );
88 } else {
89 const ext::pair < StateType, StateType > & from = transition.first.template get < ext::pair < StateType, StateType > > ( );
90
93
94 char targetState = afdza.getFinalStates ( ).contains ( from.second ) ? finalPDAState : nonFinalPDAState;
95
96 automaton.addTransition ( nonFinalPDAState, alphabet::BarSymbol::instance < SymbolType > ( ), pop, targetState, push );
97 automaton.addTransition ( finalPDAState, alphabet::BarSymbol::instance < SymbolType > ( ), pop, targetState, push );
98 }
99 }
100
101 for ( const auto & finalState : afdza.getFinalStates ( ) ) {
104
105 automaton.addTransition ( nonFinalPDAState, alphabet::BarSymbol::instance < SymbolType > ( ), pop, finalPDAState, push );
106 automaton.addTransition ( finalPDAState, alphabet::BarSymbol::instance < SymbolType > ( ), pop, finalPDAState, push );
107 }
108
109 return automaton;
110}
111
112template < class SymbolType, class StateType >
115
116 for ( const auto & symbol : afnza.getInputAlphabet ( ) ) {
117 automaton.addInputSymbol ( symbol );
118 }
119 automaton.addInputSymbol ( alphabet::BarSymbol::instance < SymbolType > ( ) );
120
121 for ( const StateType & state : afnza.getStates ( ) ) {
122 automaton.addPushdownStoreSymbol ( state );
123 }
124
125 for (const auto & transition : afnza.getTransitions()) {
126 const StateType & to = transition.second;
127 if ( transition.first.template is < SymbolType > ( ) ) {
128 const SymbolType & from = transition.first.template get < SymbolType > ( );
131 automaton.addTransition ( automaton.getInitialState ( ), from, pop, automaton.getInitialState ( ), push );
132 } else {
133 const ext::pair < StateType, StateType > & from = transition.first.template get < ext::pair < StateType, StateType > > ( );
134
137
138 automaton.addTransition ( automaton.getInitialState ( ), alphabet::BarSymbol::instance < SymbolType > ( ), pop, automaton.getInitialState ( ), push );
139 }
140 }
141
142 auto finalPDAState = 'r';
143 automaton.addState ( finalPDAState );
144 automaton.addFinalState ( finalPDAState );
145
146 for ( const auto & finalState : afnza.getFinalStates ( ) ) {
149 automaton.addTransition ( automaton.getInitialState ( ), alphabet::BarSymbol::instance < SymbolType > ( ), pop, finalPDAState, push );
150 }
151
152 return automaton;
153}
154
155} /* namespace convert */
156
157} /* namespace automaton */
158
Represents bottom of the stack symbol used in the visibly pushdown automata.
Definition: BottomOfTheStackSymbol.h:35
Deterministic Z-Automaton in Arc-Factored Normal Form. Computation model for unranked regular tree la...
Definition: ArcFactoredDeterministicZAutomaton.h:65
const ext::set< StateType > & getFinalStates() const &
Definition: ArcFactoredDeterministicZAutomaton.h:145
const ext::map< ext::variant< SymbolType, ext::pair< StateType, StateType > >, StateType > & getTransitions() const &
Definition: ArcFactoredDeterministicZAutomaton.h:284
const ext::set< SymbolType > & getInputAlphabet() const &
Definition: ArcFactoredDeterministicZAutomaton.h:194
const ext::set< StateType > & getStates() const &
Definition: ArcFactoredDeterministicZAutomaton.h:96
Nondeterministic Z-Automaton in Arc-Factored Normal Form. Computation model for unranked regular tree...
Definition: ArcFactoredNondeterministicZAutomaton.h:67
const ext::set< SymbolType > & getInputAlphabet() const &
Definition: ArcFactoredNondeterministicZAutomaton.h:203
const ext::set< StateType > & getFinalStates() const &
Definition: ArcFactoredNondeterministicZAutomaton.h:154
const ext::multimap< ext::variant< SymbolType, ext::pair< StateType, StateType > >, StateType > & getTransitions() const &
Definition: ArcFactoredNondeterministicZAutomaton.h:293
const ext::set< StateType > & getStates() const &
Definition: ArcFactoredNondeterministicZAutomaton.h:105
Deterministic pushdown automaton. Accepts subset of context free languages.
Definition: DPDA.h:78
Definition: NPDA.h:74
Definition: ToPrefixPushdownAutomaton.h:41
static automaton::DPDA< SymbolType, ext::variant< StateType, alphabet::BottomOfTheStackSymbol >, char > convert(const automaton::ArcFactoredDeterministicZAutomaton< SymbolType, StateType > &afdza)
Definition: ToPrefixPushdownAutomaton.h:61
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
Definition: ToGrammar.h:31
Definition: converterCommon.hpp:8