Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
RawRules.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <alib/set>
9#include <alib/map>
19
20#include <grammar/Grammar.h>
21
22namespace grammar {
23
27class RawRules {
28public:
39 template < class TerminalSymbolType, class NonterminalSymbolType >
41
45 template < class TerminalSymbolType, class NonterminalSymbolType >
47
51 template < class TerminalSymbolType, class NonterminalSymbolType >
53
57 template < class TerminalSymbolType, class NonterminalSymbolType >
59
63 template < class TerminalSymbolType, class NonterminalSymbolType >
65
69 template < class TerminalSymbolType, class NonterminalSymbolType >
71
75 template < class TerminalSymbolType, class NonterminalSymbolType >
77
81 template < class TerminalSymbolType, class NonterminalSymbolType >
83
87 template < class TerminalSymbolType, class NonterminalSymbolType >
89};
90
91template < class TerminalSymbolType, class NonterminalSymbolType >
94
95 for ( const auto & rule : grammar.getRules ( ) )
96 for ( const auto & rhs : rule.second ) {
98
99 if ( rhs.template is < ext::vector < TerminalSymbolType > > ( ) ) {
100 tmp.insert ( tmp.end ( ), rhs.template get < ext::vector < TerminalSymbolType > > ( ).begin ( ), rhs.template get < ext::vector < TerminalSymbolType > > ( ).end ( ) );
101 } else {
102 const auto & rhsTuple = rhs.template get < ext::tuple < ext::vector < TerminalSymbolType >, NonterminalSymbolType, ext::vector < TerminalSymbolType > > > ( );
103
104 tmp.insert ( tmp.end ( ), std::get < 0 > ( rhsTuple ).begin ( ), std::get < 0 > ( rhsTuple ).end ( ) );
105
106 tmp.push_back ( std::get < 1 > ( rhsTuple ) );
107
108 tmp.insert ( tmp.end ( ), std::get < 2 > ( rhsTuple ).begin ( ), std::get < 2 > ( rhsTuple ).end ( ) );
109 }
110
111 res[rule.first].insert ( std::move ( tmp ) );
112 }
113
114 return res;
115}
116
117template < class TerminalSymbolType, class NonterminalSymbolType >
120
121 for ( const auto & rule : grammar.getRules ( ) )
122 for ( const auto & rhs : rule.second ) {
124 tmp.push_back ( rhs.first );
125
126 tmp.insert ( tmp.end ( ), rhs.second.begin ( ), rhs.second.end ( ) );
127
128 res[rule.first].insert ( std::move ( tmp ) );
129 }
130
131 if ( grammar.getGeneratesEpsilon ( ) )
132 res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > { } );
133
134 return res;
135}
136
137template < class TerminalSymbolType, class NonterminalSymbolType >
140
141 if ( grammar.getGeneratesEpsilon ( ) )
142 res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > { } );
143
144 return res;
145}
146
147template < class TerminalSymbolType, class NonterminalSymbolType >
150
151 for ( const auto & rule : grammar.getRules ( ) )
152 for ( const auto & rhs : rule.second ) {
153 if ( rhs.template is < TerminalSymbolType > ( ) ) {
155 res[rule.first].insert ( std::move ( tmp ) );
156 } else {
157 const auto & realRHS = rhs.template get < ext::pair < NonterminalSymbolType, NonterminalSymbolType > > ( );
159 res[rule.first].insert ( std::move ( tmp ) );
160 }
161 }
162
163 if ( grammar.getGeneratesEpsilon ( ) )
164 res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > { } );
165
166 return res;
167}
168
169template < class TerminalSymbolType, class NonterminalSymbolType >
171 return grammar.getRules ( );
172}
173
174template < class TerminalSymbolType, class NonterminalSymbolType >
177
178 for ( const auto & rule : grammar.getRules ( ) )
179 for ( const auto & rhs : rule.second ) {
181
182 if ( rhs.template is < ext::vector < TerminalSymbolType > > ( ) ) {
183 tmp.insert ( tmp.end ( ), rhs.template get < ext::vector < TerminalSymbolType > > ( ).begin ( ), rhs.template get < ext::vector < TerminalSymbolType > > ( ).end ( ) );
184 } else {
185 const auto & rhsTuple = rhs.template get < ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > ( );
186
187 tmp.push_back ( rhsTuple.first );
188 tmp.insert ( tmp.end ( ), rhsTuple.second.begin ( ), rhsTuple.second.end ( ) );
189 }
190
191 res[rule.first].insert ( std::move ( tmp ) );
192 }
193
194 return res;
195}
196
197template < class TerminalSymbolType, class NonterminalSymbolType >
200
201 for ( const auto & rule : grammar.getRules ( ) )
202 for ( const auto & rhs : rule.second ) {
203 if ( rhs.template is < TerminalSymbolType > ( ) ) {
205 res[rule.first].insert ( std::move ( tmp ) );
206 } else {
207 const auto & rhsPair = rhs.template get < ext::pair < NonterminalSymbolType, TerminalSymbolType > > ( );
209 res[rule.first].insert ( std::move ( tmp ) );
210 }
211 }
212
213 if ( grammar.getGeneratesEpsilon ( ) )
214 res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > { } );
215
216 return res;
217}
218
219template < class TerminalSymbolType, class NonterminalSymbolType >
222
223 for ( const auto & rule : grammar.getRules ( ) )
224 for ( const auto & rhs : rule.second ) {
225 if ( rhs.template is < TerminalSymbolType > ( ) ) {
227 res[rule.first].insert ( std::move ( tmp ) );
228 } else {
229 const auto & rhsPair = rhs.template get < ext::pair < TerminalSymbolType, NonterminalSymbolType > > ( );
231 res[rule.first].insert ( std::move ( tmp ) );
232 }
233 }
234
235 if ( grammar.getGeneratesEpsilon ( ) )
236 res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > { } );
237
238 return res;
239}
240
241template < class TerminalSymbolType, class NonterminalSymbolType >
244
245 for ( const auto & rule : grammar.getRules ( ) )
246 for ( const auto & rhs : rule.second ) {
248
249 if ( rhs.template is < ext::vector < TerminalSymbolType > > ( ) ) {
250 tmp.insert ( tmp.end ( ), rhs.template get < ext::vector < TerminalSymbolType > > ( ).begin ( ), rhs.template get < ext::vector < TerminalSymbolType > > ( ).end ( ) );
251 } else {
252 const auto & rhsTuple = rhs.template get < ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > ( );
253
254 tmp.insert ( tmp.end ( ), rhsTuple.first.begin ( ), rhsTuple.first.end ( ) );
255 tmp.push_back ( rhsTuple.second );
256 }
257
258 res[rule.first].insert ( std::move ( tmp ) );
259 }
260
261 return res;
262}
263
264} /* namespace grammar */
265
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
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
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: vector.hpp:125
reverse_iterator insert(const_reverse_iterator pos, const T &value)
Inserts the value on position given by iterator pos.
Definition: vector.hpp:229
auto end() &
Inherited behavior of end for non-const instance.
Definition: vector.hpp:155
Context free grammar in Chomsky hierarchy or type 2 in Chomsky hierarchy. Generates context free lang...
Definition: CFG.h:67
Chomsky normal form of a context free grammar in Chomsky hierarchy or type 2 in Chomsky hierarchy....
Definition: CNF.h:66
Context free grammar without epsilon rules in Chomsky hierarchy or type 2 in Chomsky hierarchy....
Definition: EpsilonFreeCFG.h:65
Greibach normal form of a context free grammar in Chomsky hierarchy or type 2 in Chomsky hierarchy....
Definition: GNF.h:65
Context free grammar in Chomsky hierarchy or type 2 in Chomsky hierarchy. Generates context free lang...
Definition: LG.h:67
Left linear grammar in Chomsky hierarchy or type 3 in Chomsky hierarchy. Generates regular languages.
Definition: LeftLG.h:66
Left regular grammar in Chomsky hierarchy or type 3 in Chomsky hierarchy. Generates regular languages...
Definition: LeftRG.h:70
Definition: RawRules.h:27
static ext::map< NonterminalSymbolType, ext::set< ext::vector< ext::variant< TerminalSymbolType, NonterminalSymbolType > > > > getRawRules(const LG< TerminalSymbolType, NonterminalSymbolType > &grammar)
Definition: RawRules.h:92
Right linear grammar in Chomsky hierarchy or type 3 in Chomsky hierarchy. Generates regular languages...
Definition: RightLG.h:65
Right regular grammar in Chomsky hierarchy or type 3 in Chomsky hierarchy. Generates regular language...
Definition: RightRG.h:70
return res
Definition: MinimizeByPartitioning.h:145
Definition: ToAutomaton.h:24
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693