27#include <ext/algorithm>
33#include <alib/variant>
47class TerminalAlphabet;
48class NonterminalAlphabet;
66template <
class TerminalSymbolType = DefaultSymbolType,
class NonterminalSymbolType = DefaultSymbolType >
67class LG final :
public core::Components < LG < TerminalSymbolType, NonterminalSymbolType >, ext::set < TerminalSymbolType >, component::Set, TerminalAlphabet, ext::set < NonterminalSymbolType >, component::Set, NonterminalAlphabet, NonterminalSymbolType, component::Value, InitialSymbol > {
79 explicit LG ( NonterminalSymbolType initialSymbol );
162 return this->
template accessComponent < InitialSymbol > ( ).get ( );
171 return std::move ( this->
template accessComponent < InitialSymbol > ( ).
get ( ) );
182 return this->
template accessComponent < InitialSymbol > ( ).set ( std::move ( symbol ) );
191 return this->
template accessComponent < NonterminalAlphabet > ( ).get ( );
200 return std::move ( this->
template accessComponent < NonterminalAlphabet > ( ).
get ( ) );
211 return this->
template accessComponent < NonterminalAlphabet > ( ).add ( std::move ( symbol ) );
220 this->
template accessComponent < NonterminalAlphabet > ( ).set ( std::move ( symbols ) );
229 return this->
template accessComponent < TerminalAlphabet > ( ).get ( );
238 return std::move ( this->
template accessComponent < TerminalAlphabet > ( ).
get ( ) );
249 return this->
template accessComponent < TerminalAlphabet > ( ).add ( std::move ( symbol ) );
258 this->
template accessComponent < TerminalAlphabet > ( ).set ( std::move ( symbols ) );
296 <<
" rules = " << instance.
getRules ( )
301template <
class TerminalSymbolType,
class NonterminalSymbolType >
305template <
class TerminalSymbolType,
class NonterminalSymbolType >
306LG < TerminalSymbolType, NonterminalSymbolType >::LG (
ext::set < NonterminalSymbolType > nonterminalAlphabet,
ext::set < TerminalSymbolType > terminalAlphabet, NonterminalSymbolType initialSymbol ) :
core::Components <
LG,
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 ) ) {
309template <
class TerminalSymbolType,
class NonterminalSymbolType >
311 if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) )
317 for (
const TerminalSymbolType & symbol : rhs )
318 if ( !getTerminalAlphabet ( ).count ( symbol ) )
323 for (
const TerminalSymbolType & symbol : std::get < 0 > ( rhs ) )
324 if ( !getTerminalAlphabet ( ).count ( symbol ) )
327 if ( !getNonterminalAlphabet ( ).count ( std::get < 1 > ( rhs ) ) )
330 for (
const TerminalSymbolType & symbol : std::get < 2 > ( rhs ) )
331 if ( !getTerminalAlphabet ( ).count ( symbol ) )
335 return rules[std::move ( leftHandSide )].insert ( std::move ( rightHandSide ) ).second;
338template <
class TerminalSymbolType,
class NonterminalSymbolType >
340 if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) )
347 for (
const TerminalSymbolType & symbol : rhs )
348 if ( !getTerminalAlphabet ( ).count ( symbol ) )
353 for (
const TerminalSymbolType & symbol : std::get < 0 > ( rhs ) )
354 if ( !getTerminalAlphabet ( ).count ( symbol ) )
357 if ( !getNonterminalAlphabet ( ).count ( std::get < 1 > ( rhs ) ) )
360 for (
const TerminalSymbolType & symbol : std::get < 2 > ( rhs ) )
361 if ( !getTerminalAlphabet ( ).count ( symbol ) )
369template <
class TerminalSymbolType,
class NonterminalSymbolType >
374template <
class TerminalSymbolType,
class NonterminalSymbolType >
376 return std::move ( rules );
379template <
class TerminalSymbolType,
class NonterminalSymbolType >
381 return rules[leftHandSide].erase ( rightHandSide );
384template <
class TerminalSymbolType,
class NonterminalSymbolType >
388 return removeRule ( leftHandSide, rhs );
391template <
class TerminalSymbolType,
class NonterminalSymbolType >
395 return removeRule ( leftHandSide, rhs );
408template <
class TerminalSymbolType,
class NonterminalSymbolType >
409class SetConstraint<
grammar::LG < TerminalSymbolType, NonterminalSymbolType >, TerminalSymbolType, grammar::TerminalAlphabet > {
425 if ( std::find ( rhs.
begin ( ), rhs.
end ( ), symbol ) != rhs.
end ( ) )
432 if ( std::find ( lPart.begin ( ), lPart.end ( ), symbol ) != lPart.end ( ) )
437 if ( std::find ( rPart.
begin ( ), rPart.
end ( ), symbol ) != rPart.
end ( ) )
467 if (
grammar.template accessComponent < grammar::NonterminalAlphabet > ( ).get ( ).count (
ext::poly_comp ( symbol ) ) )
478template <
class TerminalSymbolType,
class NonterminalSymbolType >
479class SetConstraint<
grammar::LG < TerminalSymbolType, NonterminalSymbolType >, NonterminalSymbolType, grammar::NonterminalAlphabet > {
491 if ( rule.first == symbol )
498 if ( std::get < 1 > ( rhs ) == symbol )
504 return grammar.template accessComponent < grammar::InitialSymbol > ( ).get ( ) == symbol;
528 if (
grammar.template accessComponent < grammar::TerminalAlphabet > ( ).get ( ).count (
ext::poly_comp ( symbol ) ) )
539template <
class TerminalSymbolType,
class NonterminalSymbolType >
540class ElementConstraint<
grammar::LG < TerminalSymbolType, NonterminalSymbolType >, NonterminalSymbolType, grammar::InitialSymbol > {
551 return grammar.template accessComponent < grammar::NonterminalAlphabet > ( ).get ( ).count ( symbol );
569template <
class TerminalSymbolType,
class NonterminalSymbolType >
576 grammar::LG < > res ( std::move ( nonterminals ), std::move ( terminals ), std::move ( initialSymbol ) );
586 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::LG< TerminalSymbolType, NonterminalSymbolType > &grammar, const NonterminalSymbolType &symbol)
Definition: LG.h:550
static void valid(const grammar::LG< TerminalSymbolType, NonterminalSymbolType > &, const NonterminalSymbolType &)
Definition: LG.h:560
Definition: components.hpp:25
static void valid(const grammar::LG< TerminalSymbolType, NonterminalSymbolType > &grammar, const TerminalSymbolType &symbol)
Definition: LG.h:466
static bool available(const grammar::LG< TerminalSymbolType, NonterminalSymbolType > &, const TerminalSymbolType &)
Definition: LG.h:454
static bool used(const grammar::LG< TerminalSymbolType, NonterminalSymbolType > &grammar, const TerminalSymbolType &symbol)
Definition: LG.h:419
static bool available(const grammar::LG< TerminalSymbolType, NonterminalSymbolType > &, const NonterminalSymbolType &)
Definition: LG.h:515
static void valid(const grammar::LG< TerminalSymbolType, NonterminalSymbolType > &grammar, const NonterminalSymbolType &symbol)
Definition: LG.h:527
static bool used(const grammar::LG< TerminalSymbolType, NonterminalSymbolType > &grammar, const NonterminalSymbolType &symbol)
Definition: LG.h:489
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 tuple class from the standard library. Original reason is to allow printing of th...
Definition: tuple.hpp:42
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
Context free grammar in Chomsky hierarchy or type 2 in Chomsky hierarchy. Generates context free lang...
Definition: LG.h:67
const NonterminalSymbolType & getInitialSymbol() const &
Definition: LG.h:161
void addRules(NonterminalSymbolType leftHandSide, ext::set< ext::variant< ext::vector< TerminalSymbolType >, ext::tuple< ext::vector< TerminalSymbolType >, NonterminalSymbolType, ext::vector< TerminalSymbolType > > > > rightHandSide)
Add new rules of a grammar.
Definition: LG.h:339
bool addRule(NonterminalSymbolType leftHandSide, ext::variant< ext::vector< TerminalSymbolType >, ext::tuple< ext::vector< TerminalSymbolType >, NonterminalSymbolType, ext::vector< TerminalSymbolType > > > rightHandSide)
Add a new rule of a grammar.
Definition: LG.h:310
bool addNonterminalSymbol(NonterminalSymbolType symbol)
Definition: LG.h:210
auto operator<=>(const LG &other) const
Definition: LG.h:268
const ext::set< NonterminalSymbolType > & getNonterminalAlphabet() const &
Definition: LG.h:190
bool setInitialSymbol(NonterminalSymbolType symbol)
Definition: LG.h:181
bool addTerminalSymbol(TerminalSymbolType symbol)
Definition: LG.h:248
bool operator==(const LG &other) const
Definition: LG.h:279
ext::set< TerminalSymbolType > && getTerminalAlphabet() &&
Definition: LG.h:237
const ext::map< NonterminalSymbolType, ext::set< ext::variant< ext::vector< TerminalSymbolType >, ext::tuple< ext::vector< TerminalSymbolType >, NonterminalSymbolType, ext::vector< TerminalSymbolType > > > > > & getRules() const &
Definition: LG.h:370
ext::set< NonterminalSymbolType > && getNonterminalAlphabet() &&
Definition: LG.h:199
NonterminalSymbolType && getInitialSymbol() &&
Definition: LG.h:170
LG(NonterminalSymbolType initialSymbol)
Creates a new instance of the grammar with a concrete initial symbol.
Definition: LG.h:302
const ext::set< TerminalSymbolType > & getTerminalAlphabet() const &
Definition: LG.h:228
bool removeRule(const NonterminalSymbolType &leftHandSide, const ext::variant< ext::vector< TerminalSymbolType >, ext::tuple< ext::vector< TerminalSymbolType >, NonterminalSymbolType, ext::vector< TerminalSymbolType > > > &rightHandSide)
Definition: LG.h:380
friend ext::ostream & operator<<(ext::ostream &out, const LG &instance)
Definition: LG.h:291
void setTerminalAlphabet(ext::set< TerminalSymbolType > symbols)
Definition: LG.h:257
void setNonterminalAlphabet(ext::set< NonterminalSymbolType > symbols)
Definition: LG.h:219
object::Object DefaultSymbolType
Definition: DefaultSymbolType.h:10
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::LG< > eval(grammar::LG< TerminalSymbolType, NonterminalSymbolType > &&value)
Definition: LG.h:571
Definition: normalize.hpp:13