Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ToFTAGlushkov.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <ext/algorithm>
9
10#include <alib/set>
11#include <alib/variant>
12
13#include <global/GlobalData.h>
14
15#include <automaton/TA/NFTA.h>
17
18#include "../glushkov/GlushkovFollow.h"
19#include "../glushkov/GlushkovIndexate.h"
20#include "../glushkov/GlushkovFirst.h"
21
22namespace rte {
23
24namespace convert {
25
32private:
33 template < class SymbolType >
35 return common::ranked_symbol < SymbolType > ( symbol.getSymbol ( ).first, symbol.getRank ( ) );
36 }
37
38public:
49 template < class SymbolType >
51};
52
53template < class SymbolType >
55
56 // step 1; index RTE
58
59 // step 2; compute:
60 // - first set
61 // - follow set for every element of (non-indexed) RTE alphabet element
64
65 /* check for exceptions -> there must be NO substitution symbol in first set */
66 if ( ! ext::excludes ( firstSet.begin ( ), firstSet.end ( ), indexedRTE.getSubstitutionAlphabet ( ).begin ( ), indexedRTE.getSubstitutionAlphabet ( ).end ( ) ) )
67 throw exception::CommonException ( "GlushkovRTE: Substitution symbol appeared in the first set" );
68 /* check end */
69
70 /* check for exceptions -> there must be NO substitution symbol in follow sets */
71 for ( const std::pair < const common::ranked_symbol < ext::pair < SymbolType, unsigned > >, TFollowTuple < SymbolType > > & kv : followSet ) {
72 const TFollowTuple < SymbolType > & followTuple = kv.second; // TFollowTuple = vector < set < ranked_symbol > >
73 for ( const ext::set < common::ranked_symbol < ext::pair < SymbolType, unsigned > > > & followTupleElem : followTuple )
74 if ( ! ext::excludes ( followTupleElem.begin ( ), followTupleElem.end ( ), indexedRTE.getSubstitutionAlphabet ( ).begin ( ), indexedRTE.getSubstitutionAlphabet ( ).end ( ) ) )
75 throw exception::CommonException ( "GlushkovRTE: Substitution symbol appeared in a follow set" );
76 }
77 /* check end */
78
79 // step 3; create PDA (w/o transitions yet) and initialize input alphabet = (non-indexed) RTE alphabet and END symbol
81
82 for ( const common::ranked_symbol < SymbolType > & symbol : rte.getAlphabet ( ) )
83 automaton.addInputSymbol ( symbol );
84
85 // step 4; create pushdown store alphabet;
86
87 // simple
88 for ( const common::ranked_symbol < ext::pair < SymbolType, unsigned > > & symb : indexedRTE.getAlphabet ( ) )
90
91 // complex
92 // any element (set) from any follow tuple is a complex state
93 for ( const std::pair < const common::ranked_symbol < ext::pair < SymbolType, unsigned > >, TFollowTuple < SymbolType > > & kv : followSet ) {
94 const TFollowTuple < SymbolType > & followTuple = kv.second; // TFollowTuple = vector < set < ranked_symbol > >
95 for ( const ext::set < common::ranked_symbol < ext::pair < SymbolType, unsigned > > > & followTupleElem : followTuple )
96 automaton.addState ( followTupleElem );
97 }
98
99 // add final states
100 for ( const auto & symbol : firstSet )
101 automaton.addFinalState ( ext::set < common::ranked_symbol < ext::pair < SymbolType, unsigned > > > ( { symbol } ) );
102
103 /* DEBUG */
105 common::Streams::err << "---------------------------------------------------------" << std::endl;
106 common::Streams::err << "RTE:" << std::endl;
107 for ( const auto & symbol : indexedRTE.getAlphabet ( ) )
108 common::Streams::err << "\t" << symbol << std::endl;
109 common::Streams::err << std::endl;
110
111 common::Streams::err << "First(RTE):" << std::endl;
112 for ( const auto & symbol : firstSet )
113 common::Streams::err << "\t" << symbol << std::endl;
114 common::Streams::err << std::endl;
115
116 for ( const std::pair < const common::ranked_symbol < ext::pair < SymbolType, unsigned > >, TFollowTuple < SymbolType > > & kv : followSet ) {
117 common::Streams::err << "Follow(RTE, " << kv.first << "):" << std::endl;
118
119 if ( kv.second.empty ( ) )
120 common::Streams::err << "\t" << "{}" << std::endl;
121
122 const TFollowTuple < SymbolType > & followTuple = kv.second; // TFollowTuple = vector < set < ranked_symbol > >
123 common::Streams::err << " \t - FollowTuple:" << std::endl;
124 for ( const ext::set < common::ranked_symbol < ext::pair < SymbolType, unsigned > > > & child : followTuple )
125 common::Streams::err << "\t\t - " << child << std::endl;
126
127 common::Streams::err << std::endl;
128 }
129
130 common::Streams::err << "---------------------------------------------------------" << std::endl;
131 common::Streams::err << "FTA:" << std::endl;
132 common::Streams::err << " states: " << automaton.getStates ( ) << std::endl;
133 common::Streams::err << "fstates: " << automaton.getFinalStates ( ) << std::endl;
134 common::Streams::err << "symbols: " << automaton.getInputAlphabet ( ) << std::endl;
135 common::Streams::err << std::endl;
136 }
137 /* DEBUG END */
138
139 /* TRANSITIONS */
140 // Pattern 3 and 2
141 for ( const common::ranked_symbol < ext::pair < SymbolType, unsigned > > & symb : indexedRTE.getAlphabet ( ) ) {
143
144 if ( symb.getRank ( ) == 0 ) {
146 common::Streams::err << "Transition 3: " << phi ( symb ) << " -> " << stateTo << std::endl;
147 }
148 automaton.addTransition ( phi ( symb ), { }, std::move ( stateTo ) );
149 } else {
150 const TFollowTuple < SymbolType > & followTuple = followSet.at ( symb ); //tuple = vector < set < symb > >
152
153 for ( const auto & e : followTuple )
154 stateFrom.push_back ( e );
155
157 common::Streams::err << "Transition 2: " << phi ( symb ) << " ( " << stateFrom << " ) -> " << stateTo << std::endl;
158 }
159 automaton.addTransition ( phi ( symb ), std::move ( stateFrom ), std::move ( stateTo ) );
160 }
161 }
162
163 // Pattern 1
164 // if a symbol is in some FollowTuple's element (lets call it X), then create transition: a4 ( Follow(a4) ) -> X
165 for ( const common::ranked_symbol < ext::pair < SymbolType, unsigned > > & symb : indexedRTE.getAlphabet ( ) ) {
166 for ( const std::pair < const common::ranked_symbol < ext::pair < SymbolType, unsigned > >, TFollowTuple < SymbolType > > & symbolFollowMapPair : followSet ) {
167 const TFollowTuple < SymbolType > & followTuple = symbolFollowMapPair.second;
168 for ( const ext::set < common::ranked_symbol < ext::pair < SymbolType, unsigned > > > & followTupleElem : followTuple ) {
169 // pokud je symb v followTupleElem....
170 if ( followTupleElem.count ( symb ) == 0 )
171 continue;
172
173 const TFollowTuple < SymbolType > & symbFollowTuple = followSet.at ( symb );
176
177 for ( const auto & e : symbFollowTuple )
178 stateFrom.push_back ( e );
179
181 common::Streams::err << "Transition 1" << ( stateFrom.empty() ? "a" : "b" ) << ": " << phi ( symb ) << " ( " << stateFrom << " ) -> " << stateTo << std::endl;
182 }
183
184 automaton.addTransition ( phi ( symb ), std::move ( stateFrom ), std::move ( stateTo ) );
185 }
186 }
187 }
188
189 /* TRANSITIONS END */
190
191 return automaton;
192}
193
194} /* namespace convert */
195
196} /* namespace rte */
197
Nondeterministic finite tree automaton without epsilon transitions. Accepts regular tree languages.
Definition: NFTA.h:72
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
R & at(const T &key, R &defaultValue)
Definition: map.hpp:163
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
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::map< common::ranked_symbol< ext::pair< SymbolType, unsigned > >, TFollowTuple< SymbolType > > follow(const rte::FormalRTE< ext::pair< SymbolType, unsigned > > &rte)
Definition: GlushkovFollow.h:63
static FormalRTE< ext::pair< SymbolType, unsigned > > index(const rte::FormalRTE< SymbolType > &rte)
Definition: GlushkovIndexate.h:23
Definition: ToFTAGlushkov.h:31
static automaton::NFTA< SymbolType, ext::set< common::ranked_symbol< ext::pair< SymbolType, unsigned > > > > convert(const rte::FormalRTE< SymbolType > &rte)
Definition: ToFTAGlushkov.h:54
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
Definition: ToFTAGlushkov.h:22