Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ToPostfixPushdownAutomatonGlushkovNaive.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <global/GlobalData.h>
9
10#include <automaton/PDA/NPDA.h>
12// #include <rte/unbounded/UnboundedRegExp.h>
14#include <alphabet/EndSymbol.h>
15
16#include "../glushkov/GlushkovFollowNaive.h"
17#include "../glushkov/GlushkovIndexate.h"
18#include "../glushkov/GlushkovFirst.h"
19
20namespace rte {
21
22namespace convert {
23
30 template < class SymbolType >
32 return common::ranked_symbol < SymbolType > ( symbol.getSymbol ( ).first, symbol.getRank ( ) );
33 }
34
35public:
46 template < class SymbolType >
48};
49
50template < class SymbolType >
52
53 // step 1; index RTE
55
56 // step 2; compute:
57 // - first set
59
60 // - follow set for every element of (non-indexed) RTE alphabet element
62
63 for ( const common::ranked_symbol < ext::pair < SymbolType, unsigned > > & symbol : indexedRTE.getAlphabet ( ) )
64 followSet.insert ( std::make_pair ( symbol, rte::GlushkovFollowNaive::follow ( indexedRTE, symbol ) ) );
65
66 /* check for exceptions -> there must be NO substitution symbol in first or follow sets */
67 if ( ! ext::excludes ( firstSet.begin ( ), firstSet.end ( ), indexedRTE.getSubstitutionAlphabet ( ).begin ( ), indexedRTE.getSubstitutionAlphabet ( ).end ( ) ) )
68 throw exception::CommonException ( "GlushkovRTE: Substitution symbol appeared in the first set" );
69
70 for ( const auto & kv : followSet )
71 for ( const auto & followTuple : kv.second )
72 for ( const common::ranked_symbol < ext::pair < SymbolType, unsigned > > & followTupleElem : followTuple )
73 if ( indexedRTE.getSubstitutionAlphabet ( ).count ( followTupleElem ) )
74 throw exception::CommonException ( "GlushkovRTE: Substitution symbol appeared in a follow set" );
75
76 /* check end */
77
78 // step 3; create PDA (w/o transitions yet) and initialize input alphabet = (non-indexed) RTE alphabet and END symbol
79 char q = 'q';
80 char f = 'f';
82
83 automaton.addState ( f );
84 automaton.addFinalState ( f );
85
86 for ( const common::ranked_symbol < SymbolType > & symbol : rte.getAlphabet ( ) )
87 automaton.addInputSymbol ( symbol );
88
89 automaton.addInputSymbol ( alphabet::EndSymbol { } );
90
91 // step 4; create pushdown store alphabet; it consists of elements of indexed RTE alphabet and BotS symbol
92 for ( const common::ranked_symbol < ext::pair < SymbolType, unsigned > > & symb : indexedRTE.getAlphabet ( ) )
93 automaton.addPushdownStoreSymbol ( symb );
94
95 /* DEBUG */
97 common::Streams::err << "RTE:" << std::endl;
98
99 for ( const auto & symbol : indexedRTE.getAlphabet ( ) )
100 common::Streams::err << "\t" << symbol << std::endl;
101
102 common::Streams::err << std::endl;
103
104 common::Streams::err << "First(RTE):" << std::endl;
105
106 for ( const auto & symbol : firstSet )
107 common::Streams::err << "\t" << symbol << std::endl;
108
109 common::Streams::err << std::endl;
110
111 for ( const auto & kv : followSet ) {
112 common::Streams::err << "Follow(RTE, " << kv.first << "):" << std::endl;
113
114 if ( kv.second.empty ( ) )
115 common::Streams::err << "\t" << "{}" << std::endl;
116
117 for ( const auto & follow : kv.second ) {
118 for ( const auto & symbol : follow )
119 common::Streams::err << "\t" << symbol << std::endl;
120
121 common::Streams::err << std::endl;
122 }
123
124 common::Streams::err << std::endl;
125 }
126 }
127 /* DEBUG END */
128
129 for ( const common::ranked_symbol < ext::pair < SymbolType, unsigned > > & symb : indexedRTE.getAlphabet ( ) ) {
130 if ( symb.getRank ( ) == 0 )
131 automaton.addTransition ( q, phi ( symb ), { }, q, { symb } );
132 else
133 for ( const ext::vector < common::ranked_symbol < ext::pair < SymbolType, unsigned > > > & follow : followSet[symb] ) {
135 automaton.addTransition ( q, phi ( symb ), fstring, q, { symb } );
136 }
137
138 }
139
140 for ( const common::ranked_symbol < ext::pair < SymbolType, unsigned > > & symb : firstSet ) {
142 pop.push_back ( symb );
143 pop.push_back ( alphabet::BottomOfTheStackSymbol ( ) );
144 automaton.addTransition ( q, alphabet::EndSymbol ( ), pop, f, { } );
145 }
146
147 return automaton;
148}
149
150} /* namespace convert */
151
152} /* namespace rte */
153
Represents bottom of the stack symbol used in the visibly pushdown automata.
Definition: BottomOfTheStackSymbol.h:35
Represents end symbol used as termation symbol of a string.
Definition: EndSymbol.h:35
Definition: NPDA.h:74
static bool verbose
Verbose flag. Some algorithms print additional runtime information about internal datastructures or t...
Definition: GlobalData.h:24
static ext::reference_wrapper< ext::ostream > err
Standard error stream. Mapped to descriptor 2.
Definition: GlobalData.h:72
Definition: ranked_symbol.hpp:20
Basic exception from which all other exceptions are derived.
Definition: CommonException.h:21
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
Implementation of the variant class allowing to store any type of those listed in the template parame...
Definition: variant.hpp:98
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
Formal regular tree expression represents regular tree expression. It describes regular tree language...
Definition: FormalRTE.h:71
const ext::set< common::ranked_symbol< SymbolType > > & getSubstitutionAlphabet() const &
Definition: FormalRTE.h:170
const ext::set< common::ranked_symbol< SymbolType > > & getAlphabet() const &
Definition: FormalRTE.h:112
static ext::set< common::ranked_symbol< SymbolType > > first(const rte::FormalRTE< SymbolType > &rte)
Definition: GlushkovFirst.h:37
static ext::set< ext::vector< common::ranked_symbol< SymbolType > > > follow(const rte::FormalRTE< SymbolType > &rte, const common::ranked_symbol< SymbolType > &symbol)
Definition: GlushkovFollowNaive.h:68
static FormalRTE< ext::pair< SymbolType, unsigned > > index(const rte::FormalRTE< SymbolType > &rte)
Definition: GlushkovIndexate.h:23
Definition: ToPostfixPushdownAutomatonGlushkovNaive.h:29
static automaton::NPDA< ext::variant< common::ranked_symbol< SymbolType >, alphabet::EndSymbol >, ext::variant< common::ranked_symbol< ext::pair< SymbolType, unsigned > >, alphabet::BottomOfTheStackSymbol >, char > convert(const rte::FormalRTE< SymbolType > &rte)
Definition: ToPostfixPushdownAutomatonGlushkovNaive.h:51
q
Definition: SingleInitialStateEpsilonTransition.h:85
Definition: ToGrammar.h:31
Definition: converterCommon.hpp:8
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
Definition: ToFTAGlushkov.h:22