Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
LeftRG.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
69template < class TerminalSymbolType = DefaultSymbolType, class NonterminalSymbolType = DefaultSymbolType >
70class LeftRG final : public core::Components < LeftRG < TerminalSymbolType, NonterminalSymbolType >, ext::set < TerminalSymbolType >, component::Set, TerminalAlphabet, ext::set < NonterminalSymbolType >, component::Set, NonterminalAlphabet, NonterminalSymbolType, component::Value, InitialSymbol > {
75
79 bool generatesEpsilon;
80
81public:
87 explicit LeftRG ( NonterminalSymbolType initialSymbol );
88
96 explicit LeftRG ( ext::set < NonterminalSymbolType > nonterminalAlphabet, ext::set < TerminalSymbolType > terminalAlphabet, NonterminalSymbolType initialSymbol );
97
108 bool addRule ( NonterminalSymbolType leftHandSide, ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, TerminalSymbolType > > rightHandSide );
109
118 void addRules ( NonterminalSymbolType leftHandSide, ext::set < ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, TerminalSymbolType > > > rightHandSide );
119
126
133
142 bool removeRule ( const NonterminalSymbolType & leftHandSide, const ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, TerminalSymbolType > > & rightHandSide );
143
152 bool removeRule ( const NonterminalSymbolType & leftHandSide, const TerminalSymbolType & rightHandSide );
153
162 bool removeRule ( const NonterminalSymbolType & leftHandSide, const ext::pair < NonterminalSymbolType, TerminalSymbolType > & rightHandSide );
163
169 const NonterminalSymbolType & getInitialSymbol ( ) const & {
170 return this->template accessComponent < InitialSymbol > ( ).get ( );
171 }
172
178 NonterminalSymbolType && getInitialSymbol ( ) && {
179 return std::move ( this->template accessComponent < InitialSymbol > ( ).get ( ) );
180 }
181
189 bool setInitialSymbol ( NonterminalSymbolType symbol ) {
190 return this->template accessComponent < InitialSymbol > ( ).set ( std::move ( symbol ) );
191 }
192
199 return this->template accessComponent < NonterminalAlphabet > ( ).get ( );
200 }
201
208 return std::move ( this->template accessComponent < NonterminalAlphabet > ( ).get ( ) );
209 }
210
218 bool addNonterminalSymbol ( NonterminalSymbolType symbol ) {
219 return this->template accessComponent < NonterminalAlphabet > ( ).add ( std::move ( symbol ) );
220 }
221
228 this->template accessComponent < NonterminalAlphabet > ( ).set ( std::move ( symbols ) );
229 }
230
237 return this->template accessComponent < TerminalAlphabet > ( ).get ( );
238 }
239
246 return std::move ( this->template accessComponent < TerminalAlphabet > ( ).get ( ) );
247 }
248
256 bool addTerminalSymbol ( TerminalSymbolType symbol ) {
257 return this->template accessComponent < TerminalAlphabet > ( ).add ( std::move ( symbol ) );
258 }
259
266 this->template accessComponent < TerminalAlphabet > ( ).set ( std::move ( symbols ) );
267 }
268
274 void setGeneratesEpsilon ( bool genEps );
275
281 bool getGeneratesEpsilon ( ) const;
282
290 auto operator <=> ( const LeftRG & other ) const {
291 return std::tie ( getTerminalAlphabet ( ), getNonterminalAlphabet ( ), getInitialSymbol ( ), rules ) <=> std::tie ( other.getTerminalAlphabet ( ), other.getNonterminalAlphabet ( ), other.getInitialSymbol ( ), other.rules );
292 }
293
301 bool operator == ( const LeftRG & other ) const {
302 return std::tie ( getTerminalAlphabet ( ), getNonterminalAlphabet ( ), getInitialSymbol ( ), rules ) == std::tie ( other.getTerminalAlphabet ( ), other.getNonterminalAlphabet ( ), other.getInitialSymbol ( ), other.rules );
303 }
304
313 friend ext::ostream & operator << ( ext::ostream & out, const LeftRG & instance ) {
314 return out << "(LeftRG"
315 << " nonterminalAlphabet = " << instance.getNonterminalAlphabet ( )
316 << " terminalAlphabet = " << instance.getTerminalAlphabet ( )
317 << " initialSymbol = " << instance.getInitialSymbol ( )
318 << " rules = " << instance.getRules ( )
319 << " generatesEpsilon = " << instance.getGeneratesEpsilon ( )
320 << ")";
321 }
322};
323
324template < class TerminalSymbolType, class NonterminalSymbolType >
326}
327
328template < class TerminalSymbolType, class NonterminalSymbolType >
329LeftRG < TerminalSymbolType, NonterminalSymbolType >::LeftRG ( ext::set < NonterminalSymbolType > nonterminalAlphabet, ext::set < TerminalSymbolType > terminalAlphabet, NonterminalSymbolType initialSymbol ) : core::Components < LeftRG, 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 ) ), generatesEpsilon ( false ) {
330}
331
332template < class TerminalSymbolType, class NonterminalSymbolType >
334 if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) )
335 throw GrammarException ( "Rule must rewrite nonterminal symbol" );
336
337 if ( rightHandSide.template is < TerminalSymbolType > ( ) ) {
338 const TerminalSymbolType & rhs = rightHandSide.template get < TerminalSymbolType > ( );
339
340 if ( !getTerminalAlphabet ( ).count ( rhs ) )
341 throw GrammarException ( "Rule must rewrite to terminal symbol" );
342 } else {
343 const ext::pair < NonterminalSymbolType, TerminalSymbolType > & rhs = rightHandSide.template get < ext::pair < NonterminalSymbolType, TerminalSymbolType > > ( );
344
345 if ( !getNonterminalAlphabet ( ).count ( rhs.first ) || !getTerminalAlphabet ( ).count ( rhs.second ) )
346 throw GrammarException ( "Rule must rewrite to terminal symbol followed by nonterminal symbol" );
347 }
348
349 return rules [ std::move ( leftHandSide ) ].insert ( std::move ( rightHandSide ) ).second;
350}
351
352template < class TerminalSymbolType, class NonterminalSymbolType >
354 if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) )
355 throw GrammarException ( "Rule must rewrite nonterminal symbol" );
356
357 for ( const ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, TerminalSymbolType > > & element : rightHandSide ) {
358 if ( element.template is < NonterminalSymbolType > ( ) ) {
359 const TerminalSymbolType & rhs = element.template get < TerminalSymbolType > ( );
360
361 if ( ! getTerminalAlphabet ( ).count ( rhs ) )
362 throw GrammarException ( "Rule must rewrite to terminal symbol" );
363
364 } else {
365 const ext::pair < NonterminalSymbolType, TerminalSymbolType > & rhs = element.template get < ext::pair < NonterminalSymbolType, TerminalSymbolType > > ( );
366
367 if ( ! getNonterminalAlphabet ( ).count ( rhs.first ) || ! getTerminalAlphabet ( ).count ( rhs.second ) )
368 throw GrammarException ( "Rule must rewrite to terminal symbol followed by nonterminal symbol" );
369 }
370 }
371
372 rules [ std::move ( leftHandSide ) ].insert ( ext::make_mover ( rightHandSide ).begin ( ), ext::make_mover ( rightHandSide ).end ( ) );
373}
374
375template < class TerminalSymbolType, class NonterminalSymbolType >
377 return rules;
378}
379
380template < class TerminalSymbolType, class NonterminalSymbolType >
382 return std::move ( rules );
383}
384
385template < class TerminalSymbolType, class NonterminalSymbolType >
387 return rules[leftHandSide].erase ( rightHandSide );
388}
389
390template < class TerminalSymbolType, class NonterminalSymbolType >
391bool LeftRG < TerminalSymbolType, NonterminalSymbolType >::removeRule ( const NonterminalSymbolType & leftHandSide, const TerminalSymbolType & rightHandSide ) {
393
394 return removeRule ( leftHandSide, rhs );
395}
396
397template < class TerminalSymbolType, class NonterminalSymbolType >
400
401 return removeRule ( leftHandSide, rhs );
402}
403
404template < class TerminalSymbolType, class NonterminalSymbolType >
406 generatesEpsilon = genEps;
407}
408
409template < class TerminalSymbolType, class NonterminalSymbolType >
411 return generatesEpsilon;
412}
413
414} /* namespace grammar */
415
416namespace core {
417
424template < class TerminalSymbolType, class NonterminalSymbolType >
425class SetConstraint< grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType >, TerminalSymbolType, grammar::TerminalAlphabet > {
426public:
435 static bool used ( const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar, const TerminalSymbolType & symbol ) {
436 for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, TerminalSymbolType > > > > & rule : grammar.getRules ( ) )
437 for ( const ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, TerminalSymbolType > > & rhs : rule.second )
438 if ( ( rhs.template is < TerminalSymbolType > ( ) && ( rhs.template get < TerminalSymbolType > ( ) == symbol ) ) || ( rhs.template get < ext::pair < NonterminalSymbolType, TerminalSymbolType > > ( ).second == symbol ) )
439 return true;
440
441 return false;
442 }
443
452 static bool available ( const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > &, const TerminalSymbolType & ) {
453 return true;
454 }
455
464 static void valid ( const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar, const TerminalSymbolType & symbol ) {
465 if ( grammar.template accessComponent < grammar::NonterminalAlphabet > ( ).get ( ).count ( ext::poly_comp ( symbol ) ) )
466 throw grammar::GrammarException ( "Symbol " + ext::to_string ( symbol ) + " cannot be in the terminal alphabet since it is already in the nonterminal alphabet." );
467 }
468};
469
476template < class TerminalSymbolType, class NonterminalSymbolType >
477class SetConstraint< grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType >, NonterminalSymbolType, grammar::NonterminalAlphabet > {
478public:
487 static bool used ( const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar, const NonterminalSymbolType & symbol ) {
488 for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, TerminalSymbolType > > > > & rule : grammar.getRules ( ) ) {
489 if ( rule.first == symbol )
490 return true;
491
492 for ( const ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, TerminalSymbolType > > & rhs : rule.second )
493 if ( rhs.template get < ext::pair < NonterminalSymbolType, TerminalSymbolType > > ( ).first == symbol )
494 return true;
495
496 }
497
498 return grammar.template accessComponent < grammar::InitialSymbol > ( ).get ( ) == symbol;
499 }
500
509 static bool available ( const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > &, const NonterminalSymbolType & ) {
510 return true;
511 }
512
521 static void valid ( const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar, const NonterminalSymbolType & symbol ) {
522 if ( grammar.template accessComponent < grammar::TerminalAlphabet > ( ).get ( ).count ( ext::poly_comp ( symbol ) ) )
523 throw grammar::GrammarException ( "Symbol " + ext::to_string ( symbol ) + " cannot be in the nonterminal alphabet since it is already in the terminal alphabet." );
524 }
525};
526
533template < class TerminalSymbolType, class NonterminalSymbolType >
534class ElementConstraint< grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType >, NonterminalSymbolType, grammar::InitialSymbol > {
535public:
544 static bool available ( const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar, const NonterminalSymbolType & symbol ) {
545 return grammar.template accessComponent < grammar::NonterminalAlphabet > ( ).get ( ).count ( symbol );
546 }
547
554 static void valid ( const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > &, const NonterminalSymbolType & ) {
555 }
556};
557
563template < class TerminalSymbolType, class NonterminalSymbolType >
564struct normalize < grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > > {
566 ext::set < DefaultSymbolType > nonterminals = alphabet::SymbolNormalize::normalizeAlphabet ( std::move ( value ).getNonterminalAlphabet ( ) );
567 ext::set < DefaultSymbolType > terminals = alphabet::SymbolNormalize::normalizeAlphabet ( std::move ( value ).getTerminalAlphabet ( ) );
568 DefaultSymbolType initialSymbol = alphabet::SymbolNormalize::normalizeSymbol ( std::move ( value ).getInitialSymbol ( ) );
569
570 grammar::LeftRG < > res ( std::move ( nonterminals ), std::move ( terminals ), std::move ( initialSymbol ) );
571
572 for ( std::pair < NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, TerminalSymbolType > > > > && rule : ext::make_mover ( std::move ( value ).getRules ( ) ) ) {
573
575 for ( ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, TerminalSymbolType > > && target : ext::make_mover ( rule.second ) )
576 rhs.insert ( grammar::GrammarNormalize::normalizeRHS ( std::move ( target ) ) );
577
578 DefaultSymbolType lhs = alphabet::SymbolNormalize::normalizeSymbol ( std::move ( rule.first ) );
579
580 res.addRules ( std::move ( lhs ), std::move ( rhs ) );
581 }
582
583 res.setGeneratesEpsilon ( value.getGeneratesEpsilon ( ) );
584
585 return res;
586 }
587};
588
589} /* namespace core */
590
591extern template class grammar::LeftRG < >;
592
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 void valid(const grammar::LeftRG< TerminalSymbolType, NonterminalSymbolType > &, const NonterminalSymbolType &)
Definition: LeftRG.h:554
static bool available(const grammar::LeftRG< TerminalSymbolType, NonterminalSymbolType > &grammar, const NonterminalSymbolType &symbol)
Definition: LeftRG.h:544
Definition: components.hpp:25
static bool used(const grammar::LeftRG< TerminalSymbolType, NonterminalSymbolType > &grammar, const NonterminalSymbolType &symbol)
Definition: LeftRG.h:487
static void valid(const grammar::LeftRG< TerminalSymbolType, NonterminalSymbolType > &grammar, const NonterminalSymbolType &symbol)
Definition: LeftRG.h:521
static bool available(const grammar::LeftRG< TerminalSymbolType, NonterminalSymbolType > &, const NonterminalSymbolType &)
Definition: LeftRG.h:509
static void valid(const grammar::LeftRG< TerminalSymbolType, NonterminalSymbolType > &grammar, const TerminalSymbolType &symbol)
Definition: LeftRG.h:464
static bool used(const grammar::LeftRG< TerminalSymbolType, NonterminalSymbolType > &grammar, const TerminalSymbolType &symbol)
Definition: LeftRG.h:435
static bool available(const grammar::LeftRG< TerminalSymbolType, NonterminalSymbolType > &, const TerminalSymbolType &)
Definition: LeftRG.h:452
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
Definition: GrammarException.h:15
static ext::pair< DefaultSymbolType, ext::vector< DefaultSymbolType > > normalizeRHS(ext::pair< FirstSymbolType, ext::vector< SecondSymbolType > > &&symbol)
Definition: GrammarNormalize.h:40
Left regular grammar in Chomsky hierarchy or type 3 in Chomsky hierarchy. Generates regular languages...
Definition: LeftRG.h:70
bool setInitialSymbol(NonterminalSymbolType symbol)
Definition: LeftRG.h:189
const ext::set< TerminalSymbolType > & getTerminalAlphabet() const &
Definition: LeftRG.h:236
ext::set< TerminalSymbolType > && getTerminalAlphabet() &&
Definition: LeftRG.h:245
void setNonterminalAlphabet(ext::set< NonterminalSymbolType > symbols)
Definition: LeftRG.h:227
friend ext::ostream & operator<<(ext::ostream &out, const LeftRG &instance)
Definition: LeftRG.h:313
ext::set< NonterminalSymbolType > && getNonterminalAlphabet() &&
Definition: LeftRG.h:207
auto operator<=>(const LeftRG &other) const
Definition: LeftRG.h:290
const ext::map< NonterminalSymbolType, ext::set< ext::variant< TerminalSymbolType, ext::pair< NonterminalSymbolType, TerminalSymbolType > > > > & getRules() const &
Definition: LeftRG.h:376
LeftRG(NonterminalSymbolType initialSymbol)
Creates a new instance of Left regular grammar with a concrete initial symbol.
Definition: LeftRG.h:325
bool getGeneratesEpsilon() const
Definition: LeftRG.h:410
NonterminalSymbolType && getInitialSymbol() &&
Definition: LeftRG.h:178
void setTerminalAlphabet(ext::set< TerminalSymbolType > symbols)
Definition: LeftRG.h:265
const NonterminalSymbolType & getInitialSymbol() const &
Definition: LeftRG.h:169
bool operator==(const LeftRG &other) const
Definition: LeftRG.h:301
bool addTerminalSymbol(TerminalSymbolType symbol)
Definition: LeftRG.h:256
bool removeRule(const NonterminalSymbolType &leftHandSide, const ext::variant< TerminalSymbolType, ext::pair< NonterminalSymbolType, TerminalSymbolType > > &rightHandSide)
Definition: LeftRG.h:386
void setGeneratesEpsilon(bool genEps)
Definition: LeftRG.h:405
bool addNonterminalSymbol(NonterminalSymbolType symbol)
Definition: LeftRG.h:218
const ext::set< NonterminalSymbolType > & getNonterminalAlphabet() const &
Definition: LeftRG.h:198
void addRules(NonterminalSymbolType leftHandSide, ext::set< ext::variant< TerminalSymbolType, ext::pair< NonterminalSymbolType, TerminalSymbolType > > > rightHandSide)
Add new rules of a grammar.
Definition: LeftRG.h:353
bool addRule(NonterminalSymbolType leftHandSide, ext::variant< TerminalSymbolType, ext::pair< NonterminalSymbolType, TerminalSymbolType > > rightHandSide)
Add a new rule of a grammar.
Definition: LeftRG.h:333
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::LeftRG< > eval(grammar::LeftRG< TerminalSymbolType, NonterminalSymbolType > &&value)
Definition: LeftRG.h:565
Definition: normalize.hpp:13