Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
LeftLG.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
26
27#include <ext/algorithm>
28
29#include <alib/map>
30#include <alib/set>
31#include <alib/vector>
32#include <alib/variant>
33
34#include <core/components.hpp>
35
37
39
40#include <core/normalize.hpp>
43
44namespace grammar {
45
46class TerminalAlphabet;
47class NonterminalAlphabet;
48class InitialSymbol;
49
65template < class TerminalSymbolType = DefaultSymbolType, class NonterminalSymbolType = DefaultSymbolType >
66class LeftLG final : public core::Components < LeftLG < TerminalSymbolType, NonterminalSymbolType >, ext::set < TerminalSymbolType >, component::Set, TerminalAlphabet, ext::set < NonterminalSymbolType >, component::Set, NonterminalAlphabet, NonterminalSymbolType, component::Value, InitialSymbol > {
71
72public:
78 explicit LeftLG ( NonterminalSymbolType initialSymbol );
79
87 explicit LeftLG ( ext::set < NonterminalSymbolType > nonterminalAlphabet, ext::set < TerminalSymbolType > terminalAlphabet, NonterminalSymbolType initialSymbol );
88
99 bool addRule ( NonterminalSymbolType leftHandSide, ext::variant < ext::vector < TerminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > rightHandSide );
100
109 void addRules ( NonterminalSymbolType leftHandSide, ext::set < ext::variant < ext::vector < TerminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > > rightHandSide );
110
117
124
133 bool removeRule ( const NonterminalSymbolType & leftHandSide, const ext::variant < ext::vector < TerminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > & rightHandSide );
134
143 bool removeRule ( const NonterminalSymbolType & leftHandSide, const ext::vector < TerminalSymbolType > & rightHandSide );
144
153 bool removeRule ( const NonterminalSymbolType & leftHandSide, const ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > & rightHandSide );
154
160 const NonterminalSymbolType & getInitialSymbol ( ) const & {
161 return this->template accessComponent < InitialSymbol > ( ).get ( );
162 }
163
169 NonterminalSymbolType && getInitialSymbol ( ) && {
170 return std::move ( this->template accessComponent < InitialSymbol > ( ).get ( ) );
171 }
172
180 bool setInitialSymbol ( NonterminalSymbolType symbol ) {
181 return this->template accessComponent < InitialSymbol > ( ).set ( std::move ( symbol ) );
182 }
183
190 return this->template accessComponent < NonterminalAlphabet > ( ).get ( );
191 }
192
199 return std::move ( this->template accessComponent < NonterminalAlphabet > ( ).get ( ) );
200 }
201
209 bool addNonterminalSymbol ( NonterminalSymbolType symbol ) {
210 return this->template accessComponent < NonterminalAlphabet > ( ).add ( std::move ( symbol ) );
211 }
212
219 this->template accessComponent < NonterminalAlphabet > ( ).set ( std::move ( symbols ) );
220 }
221
228 return this->template accessComponent < TerminalAlphabet > ( ).get ( );
229 }
230
237 return std::move ( this->template accessComponent < TerminalAlphabet > ( ).get ( ) );
238 }
239
247 bool addTerminalSymbol ( TerminalSymbolType symbol ) {
248 return this->template accessComponent < TerminalAlphabet > ( ).add ( std::move ( symbol ) );
249 }
250
257 this->template accessComponent < TerminalAlphabet > ( ).set ( std::move ( symbols ) );
258 }
259
267 auto operator <=> ( const LeftLG & other ) const {
268 return std::tie ( getTerminalAlphabet ( ), getNonterminalAlphabet ( ), getInitialSymbol ( ), rules ) <=> std::tie ( other.getTerminalAlphabet ( ), other.getNonterminalAlphabet ( ), other.getInitialSymbol ( ), other.rules );
269 }
270
278 bool operator == ( const LeftLG & other ) const {
279 return std::tie ( getTerminalAlphabet ( ), getNonterminalAlphabet ( ), getInitialSymbol ( ), rules ) == std::tie ( other.getTerminalAlphabet ( ), other.getNonterminalAlphabet ( ), other.getInitialSymbol ( ), other.rules );
280 }
281
290 friend ext::ostream & operator << ( ext::ostream & out, const LeftLG & instance ) {
291 return out << "(LeftLG"
292 << " nonterminalAlphabet = " << instance.getNonterminalAlphabet ( )
293 << " terminalAlphabet = " << instance.getTerminalAlphabet ( )
294 << " initialSymbol = " << instance.getInitialSymbol ( )
295 << " rules = " << instance.getRules ( )
296 << ")";
297 }
298};
299
300template < class TerminalSymbolType, class NonterminalSymbolType >
302}
303
304template < class TerminalSymbolType, class NonterminalSymbolType >
305LeftLG < TerminalSymbolType, NonterminalSymbolType >::LeftLG ( ext::set < NonterminalSymbolType > nonterminalAlphabet, ext::set < TerminalSymbolType > terminalAlphabet, NonterminalSymbolType initialSymbol ) : core::Components < LeftLG, ext::set < TerminalSymbolType >, component::Set, TerminalAlphabet, ext::set < NonterminalSymbolType >, component::Set, NonterminalAlphabet, NonterminalSymbolType, component::Value, InitialSymbol > ( std::move ( terminalAlphabet), std::move ( nonterminalAlphabet ), std::move ( initialSymbol ) ) {
306}
307
308template < class TerminalSymbolType, class NonterminalSymbolType >
310 if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) )
311 throw GrammarException ( "Rule must rewrite nonterminal symbol" );
312
313 if ( rightHandSide.template is < ext::vector < TerminalSymbolType > > ( ) ) {
314 for ( const auto & symbol : rightHandSide.template get < ext::vector < TerminalSymbolType > > ( ) )
315 if ( !getTerminalAlphabet ( ).count ( symbol ) )
316 throw GrammarException ( "Symbol " + ext::to_string ( symbol ) + " is not a terminal symbol" );
317 } else {
318 const ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > & rhs = rightHandSide.template get < ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > ( );
319
320 if ( !getNonterminalAlphabet ( ).count ( rhs.first ) )
321 throw GrammarException ( "Symbol " + ext::to_string ( rhs.first ) + " is not a nonterminal symbol" );
322
323 for ( const auto & symbol : rhs.second )
324 if ( !getTerminalAlphabet ( ).count ( symbol ) )
325 throw GrammarException ( "Symbol " + ext::to_string ( symbol ) + " is not a terminal symbol" );
326 }
327
328 return rules [ std::move ( leftHandSide ) ].insert ( std::move ( rightHandSide ) ).second;
329}
330
331template < class TerminalSymbolType, class NonterminalSymbolType >
333 if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) )
334 throw GrammarException ( "Rule must rewrite nonterminal symbol" );
335
336 for ( const ext::variant < ext::vector < TerminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > & element : rightHandSide ) {
337 if ( element.template is < ext::vector < TerminalSymbolType > > ( ) ) {
338 for ( const auto & symbol : element.template get < ext::vector < TerminalSymbolType > > ( ) )
339 if ( !getTerminalAlphabet ( ).count ( symbol ) )
340 throw GrammarException ( "Symbol " + ext::to_string ( symbol ) + " is not a terminal symbol" );
341
342 } else {
343 const ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > & rhs = element.template get < ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > ( );
344
345 if ( !getNonterminalAlphabet ( ).count ( rhs.first ) )
346 throw GrammarException ( "Symbol " + ext::to_string ( rhs.first ) + " is not a nonterminal symbol" );
347
348 for ( const auto & symbol : rhs.second )
349 if ( !getTerminalAlphabet ( ).count ( symbol ) )
350 throw GrammarException ( "Symbol " + ext::to_string ( symbol ) + " is not a terminal symbol" );
351
352 }
353 }
354
355 rules [ std::move ( leftHandSide ) ].insert ( ext::make_mover ( rightHandSide ).begin ( ), ext::make_mover ( rightHandSide ).end ( ) );
356}
357
358template < class TerminalSymbolType, class NonterminalSymbolType >
360 return rules;
361}
362
363template < class TerminalSymbolType, class NonterminalSymbolType >
365 return std::move ( rules );
366}
367
368template < class TerminalSymbolType, class NonterminalSymbolType >
370 return rules[leftHandSide].erase ( rightHandSide );
371}
372
373template < class TerminalSymbolType, class NonterminalSymbolType >
374bool LeftLG < TerminalSymbolType, NonterminalSymbolType >::removeRule ( const NonterminalSymbolType & leftHandSide, const ext::vector < TerminalSymbolType > & rightHandSide ) {
376
377 return removeRule ( leftHandSide, rhs );
378}
379
380template < class TerminalSymbolType, class NonterminalSymbolType >
383
384 return removeRule ( leftHandSide, rhs );
385}
386
387} /* namespace grammar */
388
389namespace core {
390
397template < class TerminalSymbolType, class NonterminalSymbolType >
398class SetConstraint< grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType >, TerminalSymbolType, grammar::TerminalAlphabet > {
399public:
408 static bool used ( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar, const TerminalSymbolType & symbol ) {
409 for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < ext::vector < TerminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > > > & rule : grammar.getRules ( ) ) {
410 for ( const ext::variant < ext::vector < TerminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > & rhsTmp : rule.second )
411 if ( rhsTmp.template is < ext::vector < TerminalSymbolType > > ( ) ) {
412 const ext::vector < TerminalSymbolType > & rhs = rhsTmp.template get < ext::vector < TerminalSymbolType > > ( );
413
414 if ( std::find ( rhs.begin ( ), rhs.end ( ), symbol ) != rhs.end ( ) )
415 return true;
416 } else {
417 const ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > & rhs = rhsTmp.template get < ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > ( );
418
419 if ( std::find ( rhs.second.begin ( ), rhs.second.end ( ), symbol ) != rhs.second.end ( ) )
420 return true;
421 }
422
423 }
424
425 return false;
426 }
427
436 static bool available ( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > &, const TerminalSymbolType & ) {
437 return true;
438 }
439
448 static void valid ( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar, const TerminalSymbolType & symbol ) {
449 if ( grammar.template accessComponent < grammar::NonterminalAlphabet > ( ).get ( ).count ( ext::poly_comp ( symbol ) ) )
450 throw grammar::GrammarException ( "Symbol " + ext::to_string ( symbol ) + " cannot be in the terminal alphabet since it is already in the nonterminal alphabet." );
451 }
452};
453
460template < class TerminalSymbolType, class NonterminalSymbolType >
461class SetConstraint< grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType >, NonterminalSymbolType, grammar::NonterminalAlphabet > {
462public:
471 static bool used ( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar, const NonterminalSymbolType & symbol ) {
472 for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < ext::vector < TerminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > > > & rule : grammar.getRules ( ) ) {
473 if ( rule.first == symbol )
474 return true;
475
476 for ( const ext::variant < ext::vector < TerminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > & rhsTmp : rule.second )
477 if ( rhsTmp.template is < ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > ( ) ) {
478 const ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > & rhs = rhsTmp.template get < ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > ( );
479
480 if ( rhs.first == symbol )
481 return true;
482 }
483
484 }
485
486 return grammar.template accessComponent < grammar::InitialSymbol > ( ).get ( ) == symbol;
487 }
488
497 static bool available ( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > &, const NonterminalSymbolType & ) {
498 return true;
499 }
500
509 static void valid ( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar, const NonterminalSymbolType & symbol ) {
510 if ( grammar.template accessComponent < grammar::TerminalAlphabet > ( ).get ( ).count ( ext::poly_comp ( symbol ) ) )
511 throw grammar::GrammarException ( "Symbol " + ext::to_string ( symbol ) + " cannot be in the nonterminal alphabet since it is already in the terminal alphabet." );
512 }
513};
514
521template < class TerminalSymbolType, class NonterminalSymbolType >
522class ElementConstraint< grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType >, NonterminalSymbolType, grammar::InitialSymbol > {
523public:
532 static bool available ( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar, const NonterminalSymbolType & symbol ) {
533 return grammar.template accessComponent < grammar::NonterminalAlphabet > ( ).get ( ).count ( symbol );
534 }
535
542 static void valid ( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > &, const NonterminalSymbolType & ) {
543 }
544};
545
551template < class TerminalSymbolType, class NonterminalSymbolType >
552struct normalize < grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > > {
554 ext::set < DefaultSymbolType > nonterminals = alphabet::SymbolNormalize::normalizeAlphabet ( std::move ( value ).getNonterminalAlphabet ( ) );
555 ext::set < DefaultSymbolType > terminals = alphabet::SymbolNormalize::normalizeAlphabet ( std::move ( value ).getTerminalAlphabet ( ) );
556 DefaultSymbolType initialSymbol = alphabet::SymbolNormalize::normalizeSymbol ( std::move ( value ).getInitialSymbol ( ) );
557
558 grammar::LeftLG < > res ( std::move ( nonterminals ), std::move ( terminals ), std::move ( initialSymbol ) );
559
560 for ( std::pair < NonterminalSymbolType, ext::set < ext::variant < ext::vector < TerminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > > > && rule : ext::make_mover ( std::move ( value ).getRules ( ) ) ) {
561
563 for ( ext::variant < ext::vector < TerminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > && target : ext::make_mover ( rule.second ) )
564 rhs.insert ( grammar::GrammarNormalize::normalizeRHS ( std::move ( target ) ) );
565
566 DefaultSymbolType lhs = alphabet::SymbolNormalize::normalizeSymbol ( std::move ( rule.first ) );
567
568 res.addRules ( std::move ( lhs ), std::move ( rhs ) );
569 }
570
571 return res;
572 }
573};
574
575} /* namespace core */
576
577extern template class grammar::LeftLG < >;
578
static ext::set< DefaultSymbolType > normalizeAlphabet(ext::set< SymbolType > &&symbols)
Definition: SymbolNormalize.h:50
static DefaultSymbolType normalizeSymbol(SymbolType &&symbol)
Definition: SymbolNormalize.h:68
Definition: components.hpp:181
static bool available(const grammar::LeftLG< TerminalSymbolType, NonterminalSymbolType > &grammar, const NonterminalSymbolType &symbol)
Definition: LeftLG.h:532
static void valid(const grammar::LeftLG< TerminalSymbolType, NonterminalSymbolType > &, const NonterminalSymbolType &)
Definition: LeftLG.h:542
Definition: components.hpp:25
static bool used(const grammar::LeftLG< TerminalSymbolType, NonterminalSymbolType > &grammar, const NonterminalSymbolType &symbol)
Definition: LeftLG.h:471
static bool available(const grammar::LeftLG< TerminalSymbolType, NonterminalSymbolType > &, const NonterminalSymbolType &)
Definition: LeftLG.h:497
static void valid(const grammar::LeftLG< TerminalSymbolType, NonterminalSymbolType > &grammar, const NonterminalSymbolType &symbol)
Definition: LeftLG.h:509
static void valid(const grammar::LeftLG< TerminalSymbolType, NonterminalSymbolType > &grammar, const TerminalSymbolType &symbol)
Definition: LeftLG.h:448
static bool used(const grammar::LeftLG< TerminalSymbolType, NonterminalSymbolType > &grammar, const TerminalSymbolType &symbol)
Definition: LeftLG.h:408
static bool available(const grammar::LeftLG< TerminalSymbolType, NonterminalSymbolType > &, const TerminalSymbolType &)
Definition: LeftLG.h:436
Definition: setComponents.hpp:26
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
Definition: ostream.h:14
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
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
auto end() &
Inherited behavior of end for non-const instance.
Definition: vector.hpp:155
Definition: GrammarException.h:15
static ext::pair< DefaultSymbolType, ext::vector< DefaultSymbolType > > normalizeRHS(ext::pair< FirstSymbolType, ext::vector< SecondSymbolType > > &&symbol)
Definition: GrammarNormalize.h:40
Left linear grammar in Chomsky hierarchy or type 3 in Chomsky hierarchy. Generates regular languages.
Definition: LeftLG.h:66
bool removeRule(const NonterminalSymbolType &leftHandSide, const ext::variant< ext::vector< TerminalSymbolType >, ext::pair< NonterminalSymbolType, ext::vector< TerminalSymbolType > > > &rightHandSide)
Definition: LeftLG.h:369
void setNonterminalAlphabet(ext::set< NonterminalSymbolType > symbols)
Definition: LeftLG.h:218
bool setInitialSymbol(NonterminalSymbolType symbol)
Definition: LeftLG.h:180
NonterminalSymbolType && getInitialSymbol() &&
Definition: LeftLG.h:169
bool addNonterminalSymbol(NonterminalSymbolType symbol)
Definition: LeftLG.h:209
void addRules(NonterminalSymbolType leftHandSide, ext::set< ext::variant< ext::vector< TerminalSymbolType >, ext::pair< NonterminalSymbolType, ext::vector< TerminalSymbolType > > > > rightHandSide)
Add new rules of a grammar.
Definition: LeftLG.h:332
bool addTerminalSymbol(TerminalSymbolType symbol)
Definition: LeftLG.h:247
const NonterminalSymbolType & getInitialSymbol() const &
Definition: LeftLG.h:160
const ext::set< NonterminalSymbolType > & getNonterminalAlphabet() const &
Definition: LeftLG.h:189
ext::set< NonterminalSymbolType > && getNonterminalAlphabet() &&
Definition: LeftLG.h:198
bool addRule(NonterminalSymbolType leftHandSide, ext::variant< ext::vector< TerminalSymbolType >, ext::pair< NonterminalSymbolType, ext::vector< TerminalSymbolType > > > rightHandSide)
Add a new rule of a grammar.
Definition: LeftLG.h:309
friend ext::ostream & operator<<(ext::ostream &out, const LeftLG &instance)
Definition: LeftLG.h:290
auto operator<=>(const LeftLG &other) const
Definition: LeftLG.h:267
const ext::set< TerminalSymbolType > & getTerminalAlphabet() const &
Definition: LeftLG.h:227
LeftLG(NonterminalSymbolType initialSymbol)
Creates a new instance of Left linear grammar with a concrete initial symbol.
Definition: LeftLG.h:301
ext::set< TerminalSymbolType > && getTerminalAlphabet() &&
Definition: LeftLG.h:236
const ext::map< NonterminalSymbolType, ext::set< ext::variant< ext::vector< TerminalSymbolType >, ext::pair< NonterminalSymbolType, ext::vector< TerminalSymbolType > > > > > & getRules() const &
Definition: LeftLG.h:359
bool operator==(const LeftLG &other) const
Definition: LeftLG.h:278
void setTerminalAlphabet(ext::set< TerminalSymbolType > symbols)
Definition: LeftLG.h:256
Definition: Object.h:16
return res
Definition: MinimizeByPartitioning.h:145
Definition: normalize.hpp:10
constexpr tuple< Elements &... > tie(Elements &... args) noexcept
Helper of extended tuple of references construction. The tuple is constructed to reffer to values in ...
Definition: tuple.hpp:218
reference_mover< T > make_mover(T &param)
Move adaptor construction function specialized to lvalue reference parameter.
Definition: iterator.hpp:468
std::string to_string(const T &value)
To string method designated for objects that can be casted to string.
Definition: string.hpp:131
PolyComp< T > poly_comp(const T &inst)
Definition: functional.hpp:60
auto begin(Container &&cont) -> decltype(std::forward(cont).begin())
Definition: iterator.hpp:900
Definition: ToAutomaton.h:24
void end()
Definition: measurements.cpp:19
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693
static grammar::LeftLG< > eval(grammar::LeftLG< TerminalSymbolType, NonterminalSymbolType > &&value)
Definition: LeftLG.h:553
Definition: normalize.hpp:13