27#include <ext/algorithm>
32#include <alib/variant>
46class TerminalAlphabet;
47class NonterminalAlphabet;
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 > {
78 explicit LeftLG ( NonterminalSymbolType initialSymbol );
161 return this->
template accessComponent < InitialSymbol > ( ).get ( );
170 return std::move ( this->
template accessComponent < InitialSymbol > ( ).
get ( ) );
181 return this->
template accessComponent < InitialSymbol > ( ).set ( std::move ( symbol ) );
190 return this->
template accessComponent < NonterminalAlphabet > ( ).get ( );
199 return std::move ( this->
template accessComponent < NonterminalAlphabet > ( ).
get ( ) );
210 return this->
template accessComponent < NonterminalAlphabet > ( ).add ( std::move ( symbol ) );
219 this->
template accessComponent < NonterminalAlphabet > ( ).set ( std::move ( symbols ) );
228 return this->
template accessComponent < TerminalAlphabet > ( ).get ( );
237 return std::move ( this->
template accessComponent < TerminalAlphabet > ( ).
get ( ) );
248 return this->
template accessComponent < TerminalAlphabet > ( ).add ( std::move ( symbol ) );
257 this->
template accessComponent < TerminalAlphabet > ( ).set ( std::move ( symbols ) );
291 return out <<
"(LeftLG"
295 <<
" rules = " << instance.
getRules ( )
300template <
class TerminalSymbolType,
class NonterminalSymbolType >
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 ) ) {
308template <
class TerminalSymbolType,
class NonterminalSymbolType >
310 if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) )
315 if ( !getTerminalAlphabet ( ).count ( symbol ) )
320 if ( !getNonterminalAlphabet ( ).count ( rhs.first ) )
323 for (
const auto & symbol : rhs.second )
324 if ( !getTerminalAlphabet ( ).count ( symbol ) )
328 return rules [ std::move ( leftHandSide ) ].insert ( std::move ( rightHandSide ) ).second;
331template <
class TerminalSymbolType,
class NonterminalSymbolType >
333 if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) )
339 if ( !getTerminalAlphabet ( ).count ( symbol ) )
345 if ( !getNonterminalAlphabet ( ).count ( rhs.first ) )
348 for (
const auto & symbol : rhs.second )
349 if ( !getTerminalAlphabet ( ).count ( symbol ) )
358template <
class TerminalSymbolType,
class NonterminalSymbolType >
363template <
class TerminalSymbolType,
class NonterminalSymbolType >
365 return std::move ( rules );
368template <
class TerminalSymbolType,
class NonterminalSymbolType >
370 return rules[leftHandSide].erase ( rightHandSide );
373template <
class TerminalSymbolType,
class NonterminalSymbolType >
377 return removeRule ( leftHandSide, rhs );
380template <
class TerminalSymbolType,
class NonterminalSymbolType >
384 return removeRule ( leftHandSide, rhs );
397template <
class TerminalSymbolType,
class NonterminalSymbolType >
398class SetConstraint<
grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType >, TerminalSymbolType, grammar::TerminalAlphabet > {
414 if ( std::find ( rhs.
begin ( ), rhs.
end ( ), symbol ) != rhs.
end ( ) )
419 if ( std::find ( rhs.second.begin ( ), rhs.second.end ( ), symbol ) != rhs.second.end ( ) )
449 if (
grammar.template accessComponent < grammar::NonterminalAlphabet > ( ).get ( ).count (
ext::poly_comp ( symbol ) ) )
460template <
class TerminalSymbolType,
class NonterminalSymbolType >
461class SetConstraint<
grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType >, NonterminalSymbolType, grammar::NonterminalAlphabet > {
473 if ( rule.first == symbol )
480 if ( rhs.first == symbol )
486 return grammar.template accessComponent < grammar::InitialSymbol > ( ).get ( ) == symbol;
510 if (
grammar.template accessComponent < grammar::TerminalAlphabet > ( ).get ( ).count (
ext::poly_comp ( symbol ) ) )
521template <
class TerminalSymbolType,
class NonterminalSymbolType >
522class ElementConstraint<
grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType >, NonterminalSymbolType, grammar::InitialSymbol > {
533 return grammar.template accessComponent < grammar::NonterminalAlphabet > ( ).get ( ).count ( symbol );
551template <
class TerminalSymbolType,
class NonterminalSymbolType >
558 grammar::LeftLG < > res ( std::move ( nonterminals ), std::move ( terminals ), std::move ( initialSymbol ) );
568 res.addRules ( std::move ( lhs ), std::move ( rhs ) );
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
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
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
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 ¶m)
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