27#include <ext/algorithm>
45class TerminalAlphabet;
46class NonterminalAlphabet;
63template <
class SymbolType = DefaultSymbolType >
64class CSG final :
public core::Components < CSG < SymbolType >, ext::set < SymbolType >, component::Set, std::tuple < TerminalAlphabet, NonterminalAlphabet >, SymbolType, component::Value, InitialSymbol > {
73 bool generatesEpsilon;
150 return this->
template accessComponent < InitialSymbol > ( ).get ( );
159 return std::move ( this->
template accessComponent < InitialSymbol > ( ).
get ( ) );
170 return this->
template accessComponent < InitialSymbol > ( ).set ( std::move ( symbol ) );
179 return this->
template accessComponent < NonterminalAlphabet > ( ).get ( );
188 return std::move ( this->
template accessComponent < NonterminalAlphabet > ( ).
get ( ) );
199 return this->
template accessComponent < NonterminalAlphabet > ( ).add ( std::move ( symbol ) );
208 this->
template accessComponent < NonterminalAlphabet > ( ).set ( std::move ( symbols ) );
217 return this->
template accessComponent < TerminalAlphabet > ( ).get ( );
226 return std::move ( this->
template accessComponent < TerminalAlphabet > ( ).
get ( ) );
237 return this->
template accessComponent < TerminalAlphabet > ( ).add ( std::move ( symbol ) );
246 this->
template accessComponent < TerminalAlphabet > ( ).set ( std::move ( symbols ) );
298 <<
" rules = " << instance.
getRules ( )
304template <
class SymbolType >
308template <
class SymbolType >
309CSG < SymbolType >::CSG (
ext::set < SymbolType > nonterminalAlphabet,
ext::set < SymbolType > terminalAlphabet,
SymbolType initialSymbol ) :
core::Components <
CSG,
ext::set < SymbolType >, component::Set, std::tuple < TerminalAlphabet, NonterminalAlphabet >,
SymbolType, component::Value, InitialSymbol > ( std::move ( terminalAlphabet), std::move ( nonterminalAlphabet ), std::move ( initialSymbol ) ), generatesEpsilon (
false ) {
312template <
class SymbolType >
315 if ( !getTerminalAlphabet ( ).count ( symbol ) && !getNonterminalAlphabet ( ).count ( symbol ) )
318 if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) )
322 if ( !getTerminalAlphabet ( ).count ( symbol ) && !getNonterminalAlphabet ( ).count ( symbol ) )
325 if ( rightHandSide.empty ( ) ) {
328 for (
const SymbolType & symbol : rightHandSide )
329 if ( !getTerminalAlphabet ( ).count ( symbol ) && !getNonterminalAlphabet ( ).count ( symbol ) )
333 return rules [
ext::make_tuple ( std::move ( lContext ), std::move ( leftHandSide ), std::move ( rContext ) ) ].insert ( std::move ( rightHandSide ) ).second;
336template <
class SymbolType >
339 if ( !getTerminalAlphabet ( ).count ( symbol ) && !getNonterminalAlphabet ( ).count ( symbol ) )
342 if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) )
346 if ( !getTerminalAlphabet ( ).count ( symbol ) && !getNonterminalAlphabet ( ).count ( symbol ) )
350 if ( rhs.empty ( ) ) {
354 if ( !getTerminalAlphabet ( ).count ( symbol ) && !getNonterminalAlphabet ( ).count ( symbol ) )
362template <
class SymbolType >
367template <
class SymbolType >
369 return std::move ( rules );
372template <
class SymbolType >
374 return rules [
ext::make_tuple ( lContext, leftHandSide, rContext ) ].erase ( rightHandSide );
377template <
class SymbolType >
379 generatesEpsilon = genEps;
382template <
class SymbolType >
384 return generatesEpsilon;
396template <
class SymbolType >
409 for (
const SymbolType & lCont : std::get < 0 > ( rule.first ) )
410 if ( lCont == symbol )
413 for (
const SymbolType & rCont : std::get < 2 > ( rule.first ) )
414 if ( rCont == symbol )
418 if ( std::find ( rhs.
begin ( ), rhs.
end ( ), symbol ) != rhs.
end ( ) )
447 if (
grammar.template accessComponent < grammar::NonterminalAlphabet > ( ).get ( ).count ( symbol ) )
457template <
class SymbolType >
470 for (
const SymbolType & lCont : std::get < 0 > ( rule.first ) )
471 if ( lCont == symbol )
474 if ( std::get < 1 > ( rule.first ) == symbol )
477 for (
const SymbolType & rCont : std::get < 2 > ( rule.first ) )
478 if ( rCont == symbol )
482 if ( std::find ( rhs.
begin ( ), rhs.
end ( ), symbol ) != rhs.
end ( ) )
487 return grammar.template accessComponent < grammar::InitialSymbol > ( ).get ( ) == symbol;
511 if (
grammar.template accessComponent < grammar::TerminalAlphabet > ( ).get ( ).count ( symbol ) )
521template <
class SymbolType >
533 return grammar.template accessComponent < grammar::NonterminalAlphabet > ( ).get ( ).count ( symbol );
551template <
class SymbolType >
558 grammar::CSG < > res ( std::move ( nonterminals ), std::move ( terminals ), std::move ( initialSymbol ) );
570 res.addRules ( std::move ( lContext ), std::move ( lhs ), std::move ( rContext ), std::move ( rhs ) );
573 res.setGeneratesEpsilon ( value.getGeneratesEpsilon ( ) );
static ext::set< DefaultSymbolType > normalizeAlphabet(ext::set< SymbolType > &&symbols)
Definition: SymbolNormalize.h:50
static ext::vector< DefaultSymbolType > normalizeSymbols(ext::vector< SymbolType > &&symbols)
Definition: SymbolNormalize.h:86
static DefaultSymbolType normalizeSymbol(SymbolType &&symbol)
Definition: SymbolNormalize.h:68
Definition: components.hpp:181
static bool available(const grammar::CSG< SymbolType > &grammar, const SymbolType &symbol)
Definition: CSG.h:532
static void valid(const grammar::CSG< SymbolType > &, const SymbolType &)
Definition: CSG.h:542
Definition: components.hpp:25
static bool available(const grammar::CSG< SymbolType > &, const SymbolType &)
Definition: CSG.h:498
static bool used(const grammar::CSG< SymbolType > &grammar, const SymbolType &symbol)
Definition: CSG.h:468
static void valid(const grammar::CSG< SymbolType > &grammar, const SymbolType &symbol)
Definition: CSG.h:510
static void valid(const grammar::CSG< SymbolType > &grammar, const SymbolType &symbol)
Definition: CSG.h:446
static bool available(const grammar::CSG< SymbolType > &, const SymbolType &)
Definition: CSG.h:434
static bool used(const grammar::CSG< SymbolType > &grammar, const SymbolType &symbol)
Definition: CSG.h:407
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
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
Context sensitive grammar in Chomsky hierarchy or type 1 in Chomsky hierarchy. Generates context sens...
Definition: CSG.h:64
const ext::set< SymbolType > & getNonterminalAlphabet() const &
Definition: CSG.h:178
const ext::set< SymbolType > & getTerminalAlphabet() const &
Definition: CSG.h:216
CSG(SymbolType initialSymbol)
Creates a new instance of the grammar with a concrete initial symbol.
Definition: CSG.h:305
void addRules(ext::vector< SymbolType > lContext, SymbolType leftHandSide, ext::vector< SymbolType > rContext, ext::set< ext::vector< SymbolType > > rightHandSide)
Add new rules of a grammar.
Definition: CSG.h:337
bool operator==(const CSG &other) const
Definition: CSG.h:281
bool addRule(ext::vector< SymbolType > lContext, SymbolType leftHandSide, ext::vector< SymbolType > rContext, ext::vector< SymbolType > rightHandSide)
Add a new rule of a grammar.
Definition: CSG.h:313
void setTerminalAlphabet(ext::set< SymbolType > symbols)
Definition: CSG.h:245
bool addTerminalSymbol(SymbolType symbol)
Definition: CSG.h:236
void setGeneratesEpsilon(bool genEps)
Definition: CSG.h:378
bool removeRule(const ext::vector< SymbolType > &lContext, const SymbolType &leftHandSide, const ext::vector< SymbolType > &rContext, const ext::vector< SymbolType > &rightHandSide)
Definition: CSG.h:373
bool setInitialSymbol(SymbolType symbol)
Definition: CSG.h:169
ext::set< SymbolType > && getNonterminalAlphabet() &&
Definition: CSG.h:187
SymbolType && getInitialSymbol() &&
Definition: CSG.h:158
ext::set< SymbolType > && getTerminalAlphabet() &&
Definition: CSG.h:225
bool addNonterminalSymbol(SymbolType symbol)
Definition: CSG.h:198
auto operator<=>(const CSG &other) const
Definition: CSG.h:270
void setNonterminalAlphabet(ext::set< SymbolType > symbols)
Definition: CSG.h:207
const SymbolType & getInitialSymbol() const &
Definition: CSG.h:149
friend ext::ostream & operator<<(ext::ostream &out, const CSG &instance)
Definition: CSG.h:293
bool getGeneratesEpsilon() const
Definition: CSG.h:383
const ext::map< ext::tuple< ext::vector< SymbolType >, SymbolType, ext::vector< SymbolType > >, ext::set< ext::vector< SymbolType > > > & getRules() const &
Definition: CSG.h:363
Definition: GrammarException.h:15
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
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
constexpr auto make_tuple(Elements &&... args)
Helper of extended tuple construction. The tuple is constructed from values pack, types are deduced.
Definition: tuple.hpp:203
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::CSG< > eval(grammar::CSG< SymbolType > &&value)
Definition: CSG.h:553
Definition: normalize.hpp:13