From ca5cbd71fe14b45392dae442cc33c6d88cae054a Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Mon, 17 Sep 2018 23:01:59 +0200 Subject: [PATCH] revise templating of regular grammars --- alib2data/src/grammar/AddRawRule.h | 24 +- alib2data/src/grammar/RawRules.h | 28 +-- alib2data/src/grammar/Regular/LeftLG.h | 208 +++++++++--------- alib2data/src/grammar/Regular/RightLG.h | 207 ++++++++--------- alib2data/src/grammar/xml/Regular/LeftLG.h | 40 ++-- alib2data/src/grammar/xml/Regular/LeftRG.h | 46 ++-- alib2data/src/grammar/xml/Regular/RightLG.h | 40 ++-- .../grammar/xml/common/GrammarToXMLComposer.h | 68 +++--- alib2data/test-src/grammar/GrammarTest.cpp | 22 +- 9 files changed, 347 insertions(+), 336 deletions(-) diff --git a/alib2data/src/grammar/AddRawRule.h b/alib2data/src/grammar/AddRawRule.h index 7503109ea3..eca767dcb9 100644 --- a/alib2data/src/grammar/AddRawRule.h +++ b/alib2data/src/grammar/AddRawRule.h @@ -70,20 +70,20 @@ public: /** * \override */ - template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > - static bool addRawRule ( LeftLG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static bool addRawRule ( LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ); /** * \override */ - template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > - static bool addRawRule ( LeftRG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static bool addRawRule ( LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ); /** * \override */ - template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > - static bool addRawRule ( RightLG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static bool addRawRule ( RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ); /** * \override @@ -184,8 +184,8 @@ bool AddRawRule::addRawRule ( CFG < SymbolType > & grammar, NonterminalSymbolTyp return grammar.addRule ( std::move ( leftHandSide ), std::move ( rhs ) ); } -template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > -bool AddRawRule::addRawRule ( LeftLG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +bool AddRawRule::addRawRule ( LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ) { if ( rightHandSide.size ( ) == 0 ) return grammar.addRule ( std::move ( leftHandSide ), ext::vector < TerminalSymbolType > { } ); else if ( grammar.getNonterminalAlphabet ( ).count ( rightHandSide [ 0 ] ) ) { @@ -203,8 +203,8 @@ bool AddRawRule::addRawRule ( LeftLG < SymbolType > & grammar, NonterminalSymbol } } -template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > -bool AddRawRule::addRawRule ( LeftRG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +bool AddRawRule::addRawRule ( LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ) { if ( rightHandSide.size ( ) == 0 ) { if ( leftHandSide != grammar.getInitialSymbol ( ) ) throw GrammarException ( "Illegal left hand side of epsilon rule" ); @@ -239,8 +239,8 @@ bool AddRawRule::addRawRule ( RightRG < TerminalSymbolType, NonterminalSymbolTyp } } -template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > -bool AddRawRule::addRawRule ( RightLG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +bool AddRawRule::addRawRule ( RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ) { if ( rightHandSide.size ( ) == 0 ) return grammar.addRule ( std::move ( leftHandSide ), ext::vector < TerminalSymbolType > { } ); else if ( grammar.getNonterminalAlphabet ( ).count ( rightHandSide [ rightHandSide.size ( ) - 1 ] ) ) { diff --git a/alib2data/src/grammar/RawRules.h b/alib2data/src/grammar/RawRules.h index 5e49fea62f..8b85944c29 100644 --- a/alib2data/src/grammar/RawRules.h +++ b/alib2data/src/grammar/RawRules.h @@ -68,26 +68,26 @@ public: /** * \override */ - template < class SymbolType, class TerminalSymbolType = SymbolType, class NonterminalSymbolType = SymbolType > - static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const LeftLG < SymbolType > & grammar ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar ); /** * \override */ - template < class SymbolType, class TerminalSymbolType = SymbolType, class NonterminalSymbolType = SymbolType > - static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const LeftRG < SymbolType > & grammar ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar ); /** * \override */ - template < class SymbolType, class TerminalSymbolType = SymbolType, class NonterminalSymbolType = SymbolType > - static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const RightLG < SymbolType > & grammar ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar ); /** * \override */ template < class TerminalSymbolType, class NonterminalSymbolType > - static ext::map < TerminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const RightRG < TerminalSymbolType, NonterminalSymbolType > & grammar ); + static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const RightRG < TerminalSymbolType, NonterminalSymbolType > & grammar ); }; template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > @@ -203,8 +203,8 @@ ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < Termin return res; } -template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > -ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > RawRules::getRawRules ( const LeftLG < SymbolType > & grammar ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > RawRules::getRawRules ( const LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > res; for ( const auto & rule : grammar.getRules ( ) ) @@ -228,8 +228,8 @@ ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < Termin return res; } -template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > -ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > RawRules::getRawRules ( const LeftRG < SymbolType > & grammar ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > RawRules::getRawRules ( const LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > res; for ( const auto & rule : grammar.getRules ( ) ) @@ -251,7 +251,7 @@ ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < Termin } template < class TerminalSymbolType, class NonterminalSymbolType > -ext::map < TerminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > RawRules::getRawRules ( const RightRG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { +ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > RawRules::getRawRules ( const RightRG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { ext::map < TerminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > res; for ( const auto & rule : grammar.getRules ( ) ) @@ -272,8 +272,8 @@ ext::map < TerminalSymbolType, ext::set < ext::vector < ext::variant < TerminalS return res; } -template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > -ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > RawRules::getRawRules ( const RightLG < SymbolType > & grammar ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > RawRules::getRawRules ( const RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > res; for ( const auto & rule : grammar.getRules ( ) ) diff --git a/alib2data/src/grammar/Regular/LeftLG.h b/alib2data/src/grammar/Regular/LeftLG.h index a4656e35aa..2cd8c427f0 100644 --- a/alib2data/src/grammar/Regular/LeftLG.h +++ b/alib2data/src/grammar/Regular/LeftLG.h @@ -61,14 +61,15 @@ class InitialSymbol; * P = set of production rules of the form A -> Ba or A -> a, where A, B \in N and a \in T*, * S (InitialSymbol) = initial nonterminal symbol, * - * \tparam SymbolType used for the terminal alphabet, the nonterminal alphabet, and the initial symbol of the grammar. + * \tparam TerminalSymbolType used for the terminal alphabet of the grammar. + * \tparam NonterminalSymbolType used for the nonterminal alphabet, and the initial symbol of the grammar. */ -template < class SymbolType = DefaultSymbolType > -class LeftLG final : public GrammarBase, public core::Components < LeftLG < SymbolType >, ext::set < SymbolType >, component::Set, std::tuple < TerminalAlphabet, NonterminalAlphabet >, SymbolType, component::Value, InitialSymbol > { +template < class TerminalSymbolType = DefaultSymbolType, class NonterminalSymbolType = DefaultSymbolType > +class LeftLG final : public GrammarBase, public core::Components < LeftLG < TerminalSymbolType, NonterminalSymbolType >, ext::set < TerminalSymbolType >, component::Set, TerminalAlphabet, ext::set < NonterminalSymbolType >, component::Set, NonterminalAlphabet, NonterminalSymbolType, component::Value, InitialSymbol > { /** * Rules as mapping from nonterminal symbol on the left hand side to set of either sequence of terminal symbols or doublets of sequence of terminal symbols and nonterminal symbol. */ - ext::map < SymbolType, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > > > rules; + ext::map < NonterminalSymbolType, ext::set < ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > > > rules; public: /** @@ -76,7 +77,7 @@ public: * * \param initialSymbol the initial symbol of the grammar */ - explicit LeftLG ( SymbolType initialSymbol ); + explicit LeftLG ( NonterminalSymbolType initialSymbol ); /** * \brief Creates a new instance of Left linear grammar with a concrete nonterminal, terminal alphabet and initial symbol. @@ -85,7 +86,7 @@ public: * \param terminalSymbols the initial terminal alphabet * \param initialSymbol the initial symbol of the grammar */ - explicit LeftLG ( ext::set < SymbolType > nonTerminalSymbols, ext::set < SymbolType > terminalSymbols, SymbolType initialSymbol ); + explicit LeftLG ( ext::set < NonterminalSymbolType > nonTerminalSymbols, ext::set < TerminalSymbolType > terminalSymbols, NonterminalSymbolType initialSymbol ); /** * @copydoc grammar::GrammarBase::clone() @@ -107,7 +108,7 @@ public: * * \returns true if the rule was indeed added, false othervise */ - bool addRule ( SymbolType leftHandSide, ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > rightHandSide ); + bool addRule ( NonterminalSymbolType leftHandSide, ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > rightHandSide ); /** * \brief Add a new rule of a grammar. @@ -119,7 +120,7 @@ public: * * \returns true if the rule was indeed added, false othervise */ - bool addRule ( SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ); + bool addRule ( NonterminalSymbolType leftHandSide, ext::vector < TerminalSymbolType > rightHandSide ); /** * \brief Add a new rule of a grammar. @@ -131,7 +132,7 @@ public: * * \returns true if the rule was indeed added, false othervise */ - bool addRule ( SymbolType leftHandSide, ext::pair < SymbolType, ext::vector < SymbolType > > rightHandSide ); + bool addRule ( NonterminalSymbolType leftHandSide, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > rightHandSide ); /** * \brief Add new rules of a grammar. @@ -141,21 +142,21 @@ public: * \param leftHandSide the left hand side of the rule * \param rightHandSide a set of right hand sides of the rule */ - void addRules ( SymbolType leftHandSide, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > > rightHandSide ); + void addRules ( NonterminalSymbolType leftHandSide, ext::set < ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > > rightHandSide ); /** * Get rules of the grammar. * * \returns rules of the grammar */ - const ext::map < SymbolType, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > > > & getRules ( ) const &; + const ext::map < NonterminalSymbolType, ext::set < ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > > > & getRules ( ) const &; /** * Get rules of the grammar. * * \returns rules of the grammar */ - ext::map < SymbolType, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > > > && getRules ( ) &&; + ext::map < NonterminalSymbolType, ext::set < ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > > > && getRules ( ) &&; /** * Remove a rule of a grammar in form of A -> Ba or A -> a, where A, B \in N and a \in T*. @@ -165,7 +166,7 @@ public: * * \returns true if the rule was indeed removed, false othervise */ - bool removeRule ( const SymbolType & leftHandSide, const ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > & rightHandSide ); + bool removeRule ( const NonterminalSymbolType & leftHandSide, const ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > & rightHandSide ); /** * Remove a rule of a grammar in form of A -> a, where A \in N and a \in T*. @@ -175,7 +176,7 @@ public: * * \returns true if the rule was indeed removed, false othervise */ - bool removeRule ( const SymbolType & leftHandSide, const ext::vector < SymbolType > & rightHandSide ); + bool removeRule ( const NonterminalSymbolType & leftHandSide, const ext::vector < TerminalSymbolType > & rightHandSide ); /** * Remove a rule of a grammar in form of A -> Ba, where A, B \in N and a \in T*. @@ -185,14 +186,14 @@ public: * * \returns true if the rule was indeed removed, false othervise */ - bool removeRule ( const SymbolType & leftHandSide, const ext::pair < SymbolType, ext::vector < SymbolType > > & rightHandSide ); + bool removeRule ( const NonterminalSymbolType & leftHandSide, const ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > & rightHandSide ); /** * Getter of initial symbol. * * \returns the initial symbol of the grammar */ - const SymbolType & getInitialSymbol ( ) const & { + const NonterminalSymbolType & getInitialSymbol ( ) const & { return this->template accessComponent < InitialSymbol > ( ).get ( ); } @@ -201,7 +202,7 @@ public: * * \returns the initial symbol of the grammar */ - SymbolType && getInitialSymbol ( ) && { + NonterminalSymbolType && getInitialSymbol ( ) && { return std::move ( this->template accessComponent < InitialSymbol > ( ).get ( ) ); } @@ -212,7 +213,7 @@ public: * * \returns true if the initial symbol was indeed changed */ - bool setInitialSymbol ( SymbolType symbol ) { + bool setInitialSymbol ( NonterminalSymbolType symbol ) { return this->template accessComponent < InitialSymbol > ( ).set ( std::move ( symbol ) ); } @@ -221,7 +222,7 @@ public: * * \returns the nonterminal alphabet of the grammar */ - const ext::set < SymbolType > & getNonterminalAlphabet ( ) const & { + const ext::set < NonterminalSymbolType > & getNonterminalAlphabet ( ) const & { return this->template accessComponent < NonterminalAlphabet > ( ).get ( ); } @@ -230,7 +231,7 @@ public: * * \returns the nonterminal alphabet of the grammar */ - ext::set < SymbolType > && getNonterminalAlphabet ( ) && { + ext::set < NonterminalSymbolType > && getNonterminalAlphabet ( ) && { return std::move ( this->template accessComponent < NonterminalAlphabet > ( ).get ( ) ); } @@ -241,7 +242,7 @@ public: * * \returns true if the symbol was indeed added */ - bool addNonterminalSymbol ( SymbolType symbol ) { + bool addNonterminalSymbol ( NonterminalSymbolType symbol ) { return this->template accessComponent < NonterminalAlphabet > ( ).add ( std::move ( symbol ) ); } @@ -250,7 +251,7 @@ public: * * \param symbols completely new nonterminal alphabet */ - void setNonterminalAlphabet ( ext::set < SymbolType > symbols ) { + void setNonterminalAlphabet ( ext::set < NonterminalSymbolType > symbols ) { this->template accessComponent < NonterminalAlphabet > ( ).set ( std::move ( symbols ) ); } @@ -259,7 +260,7 @@ public: * * \returns the terminal alphabet of the grammar */ - const ext::set < SymbolType > & getTerminalAlphabet ( ) const & { + const ext::set < NonterminalSymbolType > & getTerminalAlphabet ( ) const & { return this->template accessComponent < TerminalAlphabet > ( ).get ( ); } @@ -268,7 +269,7 @@ public: * * \returns the terminal alphabet of the grammar */ - ext::set < SymbolType > && getTerminalAlphabet ( ) && { + ext::set < TerminalSymbolType > && getTerminalAlphabet ( ) && { return std::move ( this->template accessComponent < TerminalAlphabet > ( ).get ( ) ); } @@ -279,7 +280,7 @@ public: * * \returns true if the symbol was indeed added */ - bool addTerminalSymbol ( SymbolType symbol ) { + bool addTerminalSymbol ( TerminalSymbolType symbol ) { return this->template accessComponent < TerminalAlphabet > ( ).add ( std::move ( symbol ) ); } @@ -288,7 +289,7 @@ public: * * \param symbol completely new nontemrinal alphabet */ - void setTerminalAlphabet ( ext::set < SymbolType > symbols ) { + void setTerminalAlphabet ( ext::set < TerminalSymbolType > symbols ) { this->template accessComponent < TerminalAlphabet > ( ).set ( std::move ( symbols ) ); } @@ -326,35 +327,35 @@ public: virtual object::ObjectBase * inc ( ) && override; }; -template < class SymbolType > -LeftLG < SymbolType >::LeftLG ( SymbolType initialSymbol ) : LeftLG ( ext::set < SymbolType > { initialSymbol }, ext::set < SymbolType > ( ), initialSymbol ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +LeftLG < TerminalSymbolType, NonterminalSymbolType >::LeftLG ( NonterminalSymbolType initialSymbol ) : LeftLG ( ext::set < NonterminalSymbolType > { initialSymbol }, ext::set < TerminalSymbolType > ( ), initialSymbol ) { } -template < class SymbolType > -LeftLG < SymbolType >::LeftLG ( ext::set < SymbolType > nonterminalAlphabet, ext::set < SymbolType > terminalAlphabet, SymbolType initialSymbol ) : core::Components < LeftLG, ext::set < SymbolType >, component::Set, std::tuple < TerminalAlphabet, NonterminalAlphabet >, SymbolType, component::Value, InitialSymbol > ( std::move ( terminalAlphabet), std::move ( nonterminalAlphabet ), std::move ( initialSymbol ) ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +LeftLG < 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 ) ) { } -template < class SymbolType > -GrammarBase * LeftLG < SymbolType >::clone ( ) const & { +template < class TerminalSymbolType, class NonterminalSymbolType > +GrammarBase * LeftLG < TerminalSymbolType, NonterminalSymbolType >::clone ( ) const & { return new LeftLG ( * this ); } -template < class SymbolType > -GrammarBase * LeftLG < SymbolType >::clone ( ) && { +template < class TerminalSymbolType, class NonterminalSymbolType > +GrammarBase * LeftLG < TerminalSymbolType, NonterminalSymbolType >::clone ( ) && { return new LeftLG ( std::move ( * this ) ); } -template < class SymbolType > -bool LeftLG < SymbolType >::addRule ( SymbolType leftHandSide, ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > rightHandSide ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +bool LeftLG < TerminalSymbolType, NonterminalSymbolType >::addRule ( NonterminalSymbolType leftHandSide, ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > rightHandSide ) { if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) ) throw GrammarException ( "Rule must rewrite nonterminal symbol" ); - if ( rightHandSide.template is < ext::vector < SymbolType > > ( ) ) { - for ( const auto & symbol : rightHandSide.template get < ext::vector < SymbolType > > ( ) ) + if ( rightHandSide.template is < ext::vector < TerminalSymbolType > > ( ) ) { + for ( const auto & symbol : rightHandSide.template get < ext::vector < TerminalSymbolType > > ( ) ) if ( !getTerminalAlphabet ( ).count ( symbol ) ) throw GrammarException ( "Symbol " + ext::to_string ( symbol ) + " is not a terminal symbol" ); } else { - const ext::pair < SymbolType, ext::vector < SymbolType > > & rhs = rightHandSide.template get < ext::pair < SymbolType, ext::vector < SymbolType > > > ( ); + const ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > & rhs = rightHandSide.template get < ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > ( ); if ( !getNonterminalAlphabet ( ).count ( rhs.first ) ) throw GrammarException ( "Symbol " + ext::to_string ( rhs.first ) + " is not a nonterminal symbol" ); @@ -367,33 +368,33 @@ bool LeftLG < SymbolType >::addRule ( SymbolType leftHandSide, ext::variant < ex return rules [ std::move ( leftHandSide ) ].insert ( std::move ( rightHandSide ) ).second; } -template < class SymbolType > -bool LeftLG < SymbolType >::addRule ( SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ) { - ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > rhs ( std::move ( rightHandSide ) ); +template < class TerminalSymbolType, class NonterminalSymbolType > +bool LeftLG < TerminalSymbolType, NonterminalSymbolType >::addRule ( NonterminalSymbolType leftHandSide, ext::vector < TerminalSymbolType > rightHandSide ) { + ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > rhs ( std::move ( rightHandSide ) ); return addRule ( std::move ( leftHandSide ), std::move ( rhs ) ); } -template < class SymbolType > -bool LeftLG < SymbolType >::addRule ( SymbolType leftHandSide, ext::pair < SymbolType, ext::vector < SymbolType > > rightHandSide ) { - ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > rhs ( std::move ( rightHandSide ) ); +template < class TerminalSymbolType, class NonterminalSymbolType > +bool LeftLG < TerminalSymbolType, NonterminalSymbolType >::addRule ( NonterminalSymbolType leftHandSide, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > rightHandSide ) { + ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > rhs ( std::move ( rightHandSide ) ); return addRule ( std::move ( leftHandSide ), std::move ( rhs ) ); } -template < class SymbolType > -void LeftLG < SymbolType >::addRules ( SymbolType leftHandSide, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > > rightHandSide ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void LeftLG < TerminalSymbolType, NonterminalSymbolType >::addRules ( NonterminalSymbolType leftHandSide, ext::set < ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > > rightHandSide ) { if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) ) throw GrammarException ( "Rule must rewrite nonterminal symbol" ); - for ( const ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > & element : rightHandSide ) { - if ( element.template is < ext::vector < SymbolType > > ( ) ) { - for ( const auto & symbol : element.template get < ext::vector < SymbolType > > ( ) ) + for ( const ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > & element : rightHandSide ) { + if ( element.template is < ext::vector < TerminalSymbolType > > ( ) ) { + for ( const auto & symbol : element.template get < ext::vector < TerminalSymbolType > > ( ) ) if ( !getTerminalAlphabet ( ).count ( symbol ) ) throw GrammarException ( "Symbol " + ext::to_string ( symbol ) + " is not a terminal symbol" ); } else { - const ext::pair < SymbolType, ext::vector < SymbolType > > & rhs = element.template get < ext::pair < SymbolType, ext::vector < SymbolType > > > ( ); + const ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > & rhs = element.template get < ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > ( ); if ( !getNonterminalAlphabet ( ).count ( rhs.first ) ) throw GrammarException ( "Symbol " + ext::to_string ( rhs.first ) + " is not a nonterminal symbol" ); @@ -408,37 +409,37 @@ void LeftLG < SymbolType >::addRules ( SymbolType leftHandSide, ext::set < ext:: rules [ std::move ( leftHandSide ) ].insert ( ext::make_mover ( rightHandSide ).begin ( ), ext::make_mover ( rightHandSide ).end ( ) ); } -template < class SymbolType > -const ext::map < SymbolType, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > > > & LeftLG < SymbolType >::getRules ( ) const & { +template < class TerminalSymbolType, class NonterminalSymbolType > +const ext::map < NonterminalSymbolType, ext::set < ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > > > & LeftLG < TerminalSymbolType, NonterminalSymbolType >::getRules ( ) const & { return rules; } -template < class SymbolType > -ext::map < SymbolType, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > > > && LeftLG < SymbolType >::getRules ( ) && { +template < class TerminalSymbolType, class NonterminalSymbolType > +ext::map < NonterminalSymbolType, ext::set < ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > > > && LeftLG < TerminalSymbolType, NonterminalSymbolType >::getRules ( ) && { return std::move ( rules ); } -template < class SymbolType > -bool LeftLG < SymbolType >::removeRule ( const SymbolType & leftHandSide, const ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > & rightHandSide ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +bool LeftLG < TerminalSymbolType, NonterminalSymbolType >::removeRule ( const NonterminalSymbolType & leftHandSide, const ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > & rightHandSide ) { return rules[leftHandSide].erase ( rightHandSide ); } -template < class SymbolType > -bool LeftLG < SymbolType >::removeRule ( const SymbolType & leftHandSide, const ext::vector < SymbolType > & rightHandSide ) { - ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > rhs ( rightHandSide ); +template < class TerminalSymbolType, class NonterminalSymbolType > +bool LeftLG < TerminalSymbolType, NonterminalSymbolType >::removeRule ( const NonterminalSymbolType & leftHandSide, const ext::vector < TerminalSymbolType > & rightHandSide ) { + ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > rhs ( rightHandSide ); return removeRule ( leftHandSide, rhs ); } -template < class SymbolType > -bool LeftLG < SymbolType >::removeRule ( const SymbolType & leftHandSide, const ext::pair < SymbolType, ext::vector < SymbolType > > & rightHandSide ) { - ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > rhs ( rightHandSide ); +template < class TerminalSymbolType, class NonterminalSymbolType > +bool LeftLG < TerminalSymbolType, NonterminalSymbolType >::removeRule ( const NonterminalSymbolType & leftHandSide, const ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > & rightHandSide ) { + ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > rhs ( rightHandSide ); return removeRule ( leftHandSide, rhs ); } -template < class SymbolType > -int LeftLG < SymbolType >::compare ( const LeftLG & other ) const { +template < class TerminalSymbolType, class NonterminalSymbolType > +int LeftLG < TerminalSymbolType, NonterminalSymbolType >::compare ( const LeftLG & other ) const { auto first = ext::tie ( getTerminalAlphabet ( ), getNonterminalAlphabet ( ), getInitialSymbol ( ), rules ); auto second = ext::tie ( other.getTerminalAlphabet ( ), other.getNonterminalAlphabet ( ), other.getInitialSymbol ( ), other.rules ); @@ -447,8 +448,8 @@ int LeftLG < SymbolType >::compare ( const LeftLG & other ) const { return comp ( first, second ); } -template < class SymbolType > -void LeftLG < SymbolType >::operator >>( std::ostream & out ) const { +template < class TerminalSymbolType, class NonterminalSymbolType > +void LeftLG < TerminalSymbolType, NonterminalSymbolType >::operator >>( std::ostream & out ) const { out << "(LeftLG " << "nonterminalAlphabet = " << getNonterminalAlphabet ( ) << "terminalAlphabet = " << getTerminalAlphabet ( ) << "initialSymbol = " @@ -456,15 +457,15 @@ void LeftLG < SymbolType >::operator >>( std::ostream & out ) const { << rules << ")"; } -template < class SymbolType > -LeftLG < SymbolType >::operator std::string ( ) const { +template < class TerminalSymbolType, class NonterminalSymbolType > +LeftLG < TerminalSymbolType, NonterminalSymbolType >::operator std::string ( ) const { std::stringstream ss; ss << * this; return ss.str ( ); } -template < class SymbolType > -object::ObjectBase* LeftLG < SymbolType >::inc() && { +template < class TerminalSymbolType, class NonterminalSymbolType > +object::ObjectBase* LeftLG < TerminalSymbolType, NonterminalSymbolType >::inc() && { return new object::UniqueObject(object::Object(std::move(*this)), primitive::Integer(0)); } @@ -475,10 +476,11 @@ namespace core { /** * Helper class specifying constraints for the grammar's internal terminal alphabet component. * - * \tparam SymbolType used for the terminal alphabet of the grammar. + * \tparam TerminalSymbolType used for the terminal alphabet of the grammar. + * \tparam NonterminalSymbolType used for the nonterminal alphabet, and the initial symbol of the grammar. */ -template < class SymbolType > -class SetConstraint< grammar::LeftLG < SymbolType >, SymbolType, grammar::TerminalAlphabet > { +template < class TerminalSymbolType, class NonterminalSymbolType > +class SetConstraint< grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType >, TerminalSymbolType, grammar::TerminalAlphabet > { public: /** * Returns true if the terminal symbol is still used in some rule of the grammar. @@ -488,16 +490,16 @@ public: * * \returns true if the symbol is used, false othervise */ - static bool used ( const grammar::LeftLG < SymbolType > & grammar, const SymbolType & symbol ) { - for ( const std::pair < const SymbolType, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > > > & rule : grammar.getRules ( ) ) { - for ( const ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > & rhsTmp : rule.second ) - if ( rhsTmp.template is < ext::vector < SymbolType > > ( ) ) { - const ext::vector < SymbolType > & rhs = rhsTmp.template get < ext::vector < SymbolType > > ( ); + static bool used ( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar, const TerminalSymbolType & symbol ) { + for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > > > & rule : grammar.getRules ( ) ) { + for ( const ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > & rhsTmp : rule.second ) + if ( rhsTmp.template is < ext::vector < TerminalSymbolType > > ( ) ) { + const ext::vector < NonterminalSymbolType > & rhs = rhsTmp.template get < ext::vector < TerminalSymbolType > > ( ); if ( std::find ( rhs.begin ( ), rhs.end ( ), symbol ) != rhs.end ( ) ) return true; } else { - const ext::pair < SymbolType, ext::vector < SymbolType > > & rhs = rhsTmp.template get < ext::pair < SymbolType, ext::vector < SymbolType > > > ( ); + const ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > & rhs = rhsTmp.template get < ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > ( ); if ( std::find ( rhs.second.begin ( ), rhs.second.end ( ), symbol ) != rhs.second.end ( ) ) return true; @@ -516,7 +518,7 @@ public: * * \returns true */ - static bool available ( const grammar::LeftLG < SymbolType > &, const SymbolType & ) { + static bool available ( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > &, const TerminalSymbolType & ) { return true; } @@ -528,7 +530,7 @@ public: * * \throws grammar::GrammarException of the tested symbol is in nonterminal alphabet */ - static void valid ( const grammar::LeftLG < SymbolType > & grammar, const SymbolType & symbol ) { + static void valid ( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar, const TerminalSymbolType & symbol ) { if ( grammar.template accessComponent < grammar::NonterminalAlphabet > ( ).get ( ).count ( symbol ) ) throw grammar::GrammarException ( "Symbol " + ext::to_string ( symbol ) + "cannot be in terminal alphabet since it is already nonterminal alphabet" ); } @@ -537,10 +539,11 @@ public: /** * Helper class specifying constraints for the grammar's internal nonterminal alphabet component. * - * \tparam SymbolType used for the nonterminal alphabet of the grammar. + * \tparam TerminalSymbolType used for the terminal alphabet of the grammar. + * \tparam NonterminalSymbolType used for the nonterminal alphabet, and the initial symbol of the grammar. */ -template < class SymbolType > -class SetConstraint< grammar::LeftLG < SymbolType >, SymbolType, grammar::NonterminalAlphabet > { +template < class TerminalSymbolType, class NonterminalSymbolType > +class SetConstraint< grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType >, NonterminalSymbolType, grammar::NonterminalAlphabet > { public: /** * Returns true if the nonterminal symbol is still used in some rule of the grammar or if it is the initial symbol of the grammar. @@ -550,14 +553,14 @@ public: * * \returns true if the symbol is used, false othervise */ - static bool used ( const grammar::LeftLG < SymbolType > & grammar, const SymbolType & symbol ) { - for ( const std::pair < const SymbolType, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > > > & rule : grammar.getRules ( ) ) { + static bool used ( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar, const NonterminalSymbolType & symbol ) { + for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > > > & rule : grammar.getRules ( ) ) { if ( rule.first == symbol ) return true; - for ( const ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > & rhsTmp : rule.second ) - if ( rhsTmp.template is < ext::pair < SymbolType, ext::vector < SymbolType > > > ( ) ) { - const ext::pair < SymbolType, ext::vector < SymbolType > > & rhs = rhsTmp.template get < ext::pair < SymbolType, ext::vector < SymbolType > > > ( ); + for ( const ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > & rhsTmp : rule.second ) + if ( rhsTmp.template is < ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > ( ) ) { + const ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > & rhs = rhsTmp.template get < ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > ( ); if ( rhs.first == symbol ) return true; @@ -579,7 +582,7 @@ public: * * \returns true */ - static bool available ( const grammar::LeftLG < SymbolType > &, const SymbolType & ) { + static bool available ( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > &, const NonterminalSymbolType & ) { return true; } @@ -591,7 +594,7 @@ public: * * \throws grammar::GrammarException of the tested symbol is in nonterminal alphabet */ - static void valid ( const grammar::LeftLG < SymbolType > & grammar, const SymbolType & symbol ) { + static void valid ( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar, const NonterminalSymbolType & symbol ) { if ( grammar.template accessComponent < grammar::TerminalAlphabet > ( ).get ( ).count ( symbol ) ) throw grammar::GrammarException ( "Symbol " + ext::to_string ( symbol ) + "cannot be in nonterminal alphabet since it is already in terminal alphabet" ); } @@ -600,10 +603,11 @@ public: /** * Helper class specifying constraints for the grammar's internal initial symbol element. * - * \tparam SymbolType used for the initial symbol of the grammar. + * \tparam TerminalSymbolType used for the terminal alphabet of the grammar. + * \tparam NonterminalSymbolType used for the nonterminal alphabet, and the initial symbol of the grammar. */ -template < class SymbolType > -class ElementConstraint< grammar::LeftLG < SymbolType >, SymbolType, grammar::InitialSymbol > { +template < class TerminalSymbolType, class NonterminalSymbolType > +class ElementConstraint< grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType >, TerminalSymbolType, grammar::InitialSymbol > { public: /** * Returns true if the symbol requested to be initial is available in nonterminal alphabet. @@ -613,7 +617,7 @@ public: * * \returns true if the tested symbol is in nonterminal alphabet */ - static bool available ( const grammar::LeftLG < SymbolType > & grammar, const SymbolType & symbol ) { + static bool available ( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar, const NonterminalSymbolType & symbol ) { return grammar.template accessComponent < grammar::NonterminalAlphabet > ( ).get ( ).count ( symbol ); } @@ -623,7 +627,7 @@ public: * \param grammar the tested grammar * \param symbol the tested symbol */ - static void valid ( const grammar::LeftLG < SymbolType > &, const SymbolType & ) { + static void valid ( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > &, const NonterminalSymbolType & ) { } }; @@ -632,19 +636,19 @@ public: * * \returns new instance of the grammar with default template parameters or unmodified instance if the template parameters were already default ones */ -template < class SymbolType > -struct normalize < grammar::LeftLG < SymbolType > > { - static grammar::LeftLG < > eval ( grammar::LeftLG < SymbolType > && value ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +struct normalize < grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > > { + static grammar::LeftLG < > eval ( grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > && value ) { ext::set < DefaultSymbolType > nonterminals = alphabet::SymbolNormalize::normalizeAlphabet ( std::move ( value ).getNonterminalAlphabet ( ) ); ext::set < DefaultSymbolType > terminals = alphabet::SymbolNormalize::normalizeAlphabet ( std::move ( value ).getTerminalAlphabet ( ) ); DefaultSymbolType initialSymbol = alphabet::SymbolNormalize::normalizeSymbol ( std::move ( value ).getInitialSymbol ( ) ); grammar::LeftLG < > res ( std::move ( nonterminals ), std::move ( terminals ), std::move ( initialSymbol ) ); - for ( std::pair < SymbolType, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > > > && rule : ext::make_mover ( std::move ( value ).getRules ( ) ) ) { + for ( std::pair < NonterminalSymbolType, ext::set < ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > > > && rule : ext::make_mover ( std::move ( value ).getRules ( ) ) ) { ext::set < ext::variant < ext::vector < DefaultSymbolType >, ext::pair < DefaultSymbolType, ext::vector < DefaultSymbolType > > > > rhs; - for ( ext::variant < ext::vector < SymbolType >, ext::pair < SymbolType, ext::vector < SymbolType > > > && target : ext::make_mover ( rule.second ) ) + for ( ext::variant < ext::vector < NonterminalSymbolType >, ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > && target : ext::make_mover ( rule.second ) ) rhs.insert ( grammar::GrammarNormalize::normalizeRHS ( std::move ( target ) ) ); DefaultSymbolType lhs = alphabet::SymbolNormalize::normalizeSymbol ( std::move ( rule.first ) ); diff --git a/alib2data/src/grammar/Regular/RightLG.h b/alib2data/src/grammar/Regular/RightLG.h index 23a20acbe4..1dec0c2318 100644 --- a/alib2data/src/grammar/Regular/RightLG.h +++ b/alib2data/src/grammar/Regular/RightLG.h @@ -61,14 +61,14 @@ class InitialSymbol; * P = set of production rules of the form A -> aB or A -> a, where A, B \in N and a \in T*, * S (InitialSymbol) = initial nonterminal symbol, * - * \tparam SymbolType used for the terminal alphabet, the nonterminal alphabet, and the initial symbol of the grammar. + * \tparam NonterminalSymbolType used for the terminal alphabet, the nonterminal alphabet, and the initial symbol of the grammar. */ -template < class SymbolType = DefaultSymbolType > -class RightLG final : public GrammarBase, public core::Components < RightLG < SymbolType >, ext::set < SymbolType >, component::Set, std::tuple < TerminalAlphabet, NonterminalAlphabet >, SymbolType, component::Value, InitialSymbol > { +template < class TerminalSymbolType = DefaultSymbolType, class NonterminalSymbolType = DefaultSymbolType > +class RightLG final : public GrammarBase, public core::Components < RightLG < TerminalSymbolType, NonterminalSymbolType >, ext::set < TerminalSymbolType >, component::Set, TerminalAlphabet, ext::set < NonterminalSymbolType >, component::Set, NonterminalAlphabet, NonterminalSymbolType, component::Value, InitialSymbol > { /** * Rules as mapping from nonterminal symbol on the left hand side to set of either sequence of terminal symbols or doublets of sequence of nonterminal symbol and terminal symbols. */ - ext::map < SymbolType, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > > > rules; + ext::map < NonterminalSymbolType, ext::set < ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > > > rules; public: /** @@ -76,7 +76,7 @@ public: * * \param initialSymbol the initial symbol of the grammar */ - explicit RightLG ( SymbolType initialSymbol ); + explicit RightLG ( NonterminalSymbolType initialSymbol ); /** * \brief Creates a new instance of Right linear grammar with a concrete nonterminal, terminal alphabet and initial symbol. @@ -85,7 +85,7 @@ public: * \param terminalSymbols the initial terminal alphabet * \param initialSymbol the initial symbol of the grammar */ - explicit RightLG ( ext::set < SymbolType > nonTerminalSymbols, ext::set < SymbolType > terminalSymbols, SymbolType initialSymbol ); + explicit RightLG ( ext::set < NonterminalSymbolType > nonTerminalSymbols, ext::set < TerminalSymbolType > terminalSymbols, NonterminalSymbolType initialSymbol ); /** * @copydoc grammar::GrammarBase::clone() @@ -107,7 +107,7 @@ public: * * \returns true if the rule was indeed added, false othervise */ - bool addRule ( SymbolType leftHandSide, ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > rightHandSide ); + bool addRule ( NonterminalSymbolType leftHandSide, ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > rightHandSide ); /** * \brief Add a new rule of a grammar. @@ -119,7 +119,7 @@ public: * * \returns true if the rule was indeed added, false othervise */ - bool addRule ( SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ); + bool addRule ( NonterminalSymbolType leftHandSide, ext::vector < TerminalSymbolType > rightHandSide ); /** * \brief Add a new rule of a grammar. @@ -131,7 +131,7 @@ public: * * \returns true if the rule was indeed added, false othervise */ - bool addRule ( SymbolType leftHandSide, ext::pair < ext::vector < SymbolType >, SymbolType > rightHandSide ); + bool addRule ( NonterminalSymbolType leftHandSide, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > rightHandSide ); /** * \brief Add new rules of a grammar. @@ -141,21 +141,21 @@ public: * \param leftHandSide the left hand side of the rule * \param rightHandSide a set of right hand sides of the rule */ - void addRules ( SymbolType leftHandSide, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > > rightHandSide ); + void addRules ( NonterminalSymbolType leftHandSide, ext::set < ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > > rightHandSide ); /** * Get rules of the grammar. * * \returns rules of the grammar */ - const ext::map < SymbolType, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > > > & getRules ( ) const &; + const ext::map < NonterminalSymbolType, ext::set < ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > > > & getRules ( ) const &; /** * Get rules of the grammar. * * \returns rules of the grammar */ - ext::map < SymbolType, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > > > && getRules ( ) &&; + ext::map < NonterminalSymbolType, ext::set < ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > > > && getRules ( ) &&; /** * Remove a rule of a grammar in form of A -> aB or A -> a, where A, B \in N and a \in T*. @@ -165,7 +165,7 @@ public: * * \returns true if the rule was indeed removed, false othervise */ - bool removeRule ( const SymbolType & leftHandSide, const ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > & rightHandSide ); + bool removeRule ( const NonterminalSymbolType & leftHandSide, const ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > & rightHandSide ); /** * Remove a rule of a grammar in form of A -> a, where A \in N and a \in T*. @@ -175,7 +175,7 @@ public: * * \returns true if the rule was indeed removed, false othervise */ - bool removeRule ( const SymbolType & leftHandSide, const ext::vector < SymbolType > & rightHandSide ); + bool removeRule ( const NonterminalSymbolType & leftHandSide, const ext::vector < TerminalSymbolType > & rightHandSide ); /** * Remove a rule of a grammar in form of A -> aB, where A, B \in N and a \in T*. @@ -185,14 +185,14 @@ public: * * \returns true if the rule was indeed removed, false othervise */ - bool removeRule ( const SymbolType & leftHandSide, const ext::pair < ext::vector < SymbolType >, SymbolType > & rightHandSide ); + bool removeRule ( const NonterminalSymbolType & leftHandSide, const ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > & rightHandSide ); /** * Getter of initial symbol. * * \returns the initial symbol of the grammar */ - const SymbolType & getInitialSymbol ( ) const & { + const NonterminalSymbolType & getInitialSymbol ( ) const & { return this->template accessComponent < InitialSymbol > ( ).get ( ); } @@ -201,7 +201,7 @@ public: * * \returns the initial symbol of the grammar */ - SymbolType && getInitialSymbol ( ) && { + NonterminalSymbolType && getInitialSymbol ( ) && { return std::move ( this->template accessComponent < InitialSymbol > ( ).get ( ) ); } @@ -212,7 +212,7 @@ public: * * \returns true if the initial symbol was indeed changed */ - bool setInitialSymbol ( SymbolType symbol ) { + bool setInitialSymbol ( NonterminalSymbolType symbol ) { return this->template accessComponent < InitialSymbol > ( ).set ( std::move ( symbol ) ); } @@ -221,7 +221,7 @@ public: * * \returns the nonterminal alphabet of the grammar */ - const ext::set < SymbolType > & getNonterminalAlphabet ( ) const & { + const ext::set < NonterminalSymbolType > & getNonterminalAlphabet ( ) const & { return this->template accessComponent < NonterminalAlphabet > ( ).get ( ); } @@ -230,7 +230,7 @@ public: * * \returns the nonterminal alphabet of the grammar */ - ext::set < SymbolType > && getNonterminalAlphabet ( ) && { + ext::set < NonterminalSymbolType > && getNonterminalAlphabet ( ) && { return std::move ( this->template accessComponent < NonterminalAlphabet > ( ).get ( ) ); } @@ -241,7 +241,7 @@ public: * * \returns true if the symbol was indeed added */ - bool addNonterminalSymbol ( SymbolType symbol ) { + bool addNonterminalSymbol ( NonterminalSymbolType symbol ) { return this->template accessComponent < NonterminalAlphabet > ( ).add ( std::move ( symbol ) ); } @@ -250,7 +250,7 @@ public: * * \param symbols completely new nonterminal alphabet */ - void setNonterminalAlphabet ( ext::set < SymbolType > symbols ) { + void setNonterminalAlphabet ( ext::set < NonterminalSymbolType > symbols ) { this->template accessComponent < NonterminalAlphabet > ( ).set ( std::move ( symbols ) ); } @@ -259,7 +259,7 @@ public: * * \returns the terminal alphabet of the grammar */ - const ext::set < SymbolType > & getTerminalAlphabet ( ) const & { + const ext::set < TerminalSymbolType > & getTerminalAlphabet ( ) const & { return this->template accessComponent < TerminalAlphabet > ( ).get ( ); } @@ -268,7 +268,7 @@ public: * * \returns the terminal alphabet of the grammar */ - ext::set < SymbolType > && getTerminalAlphabet ( ) && { + ext::set < TerminalSymbolType > && getTerminalAlphabet ( ) && { return std::move ( this->template accessComponent < TerminalAlphabet > ( ).get ( ) ); } @@ -279,7 +279,7 @@ public: * * \returns true if the symbol was indeed added */ - bool addTerminalSymbol ( SymbolType symbol ) { + bool addTerminalSymbol ( TerminalSymbolType symbol ) { return this->template accessComponent < TerminalAlphabet > ( ).add ( std::move ( symbol ) ); } @@ -288,7 +288,7 @@ public: * * \param symbol completely new nontemrinal alphabet */ - void setTerminalAlphabet ( ext::set < SymbolType > symbols ) { + void setTerminalAlphabet ( ext::set < TerminalSymbolType > symbols ) { this->template accessComponent < TerminalAlphabet > ( ).set ( std::move ( symbols ) ); } @@ -339,35 +339,35 @@ public: virtual object::ObjectBase * inc ( ) && override; }; -template < class SymbolType > -RightLG < SymbolType >::RightLG ( SymbolType initialSymbol ) : RightLG ( ext::set < SymbolType > { initialSymbol }, ext::set < SymbolType > ( ), initialSymbol ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +RightLG < TerminalSymbolType, NonterminalSymbolType >::RightLG ( NonterminalSymbolType initialSymbol ) : RightLG ( ext::set < NonterminalSymbolType > { initialSymbol }, ext::set < TerminalSymbolType > ( ), initialSymbol ) { } -template < class SymbolType > -RightLG < SymbolType >::RightLG ( ext::set < SymbolType > nonterminalAlphabet, ext::set < SymbolType > terminalAlphabet, SymbolType initialSymbol ) : core::Components < RightLG, ext::set < SymbolType >, component::Set, std::tuple < TerminalAlphabet, NonterminalAlphabet >, SymbolType, component::Value, InitialSymbol > ( std::move ( terminalAlphabet), std::move ( nonterminalAlphabet ), std::move ( initialSymbol ) ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +RightLG < TerminalSymbolType, NonterminalSymbolType >::RightLG ( ext::set < NonterminalSymbolType > nonterminalAlphabet, ext::set < TerminalSymbolType > terminalAlphabet, NonterminalSymbolType initialSymbol ) : core::Components < RightLG, 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 ) ) { } -template < class SymbolType > -GrammarBase * RightLG < SymbolType >::clone ( ) const & { +template < class TerminalSymbolType, class NonterminalSymbolType > +GrammarBase * RightLG < TerminalSymbolType, NonterminalSymbolType >::clone ( ) const & { return new RightLG ( * this ); } -template < class SymbolType > -GrammarBase * RightLG < SymbolType >::clone ( ) && { +template < class TerminalSymbolType, class NonterminalSymbolType > +GrammarBase * RightLG < TerminalSymbolType, NonterminalSymbolType >::clone ( ) && { return new RightLG ( std::move ( * this ) ); } -template < class SymbolType > -bool RightLG < SymbolType >::addRule ( SymbolType leftHandSide, ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > rightHandSide ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +bool RightLG < TerminalSymbolType, NonterminalSymbolType >::addRule ( NonterminalSymbolType leftHandSide, ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > rightHandSide ) { if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) ) throw GrammarException ( "Rule must rewrite nonterminal symbol" ); - if ( rightHandSide.template is < ext::vector < SymbolType > > ( ) ) { - for ( const auto & symbol : rightHandSide.template get < ext::vector < SymbolType > > ( ) ) + if ( rightHandSide.template is < ext::vector < TerminalSymbolType > > ( ) ) { + for ( const auto & symbol : rightHandSide.template get < ext::vector < TerminalSymbolType > > ( ) ) if ( !getTerminalAlphabet ( ).count ( symbol ) ) throw GrammarException ( "Symbol " + ext::to_string ( symbol ) + " is not a terminal symbol" ); } else { - const ext::pair < ext::vector < SymbolType >, SymbolType > & rhs = rightHandSide.template get < ext::pair < ext::vector < SymbolType >, SymbolType > > ( ); + const ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > & rhs = rightHandSide.template get < ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > ( ); if ( !getNonterminalAlphabet ( ).count ( rhs.second ) ) throw GrammarException ( "Symbol " + ext::to_string ( rhs.second ) + " is not a nonterminal symbol" ); @@ -380,32 +380,32 @@ bool RightLG < SymbolType >::addRule ( SymbolType leftHandSide, ext::variant < e return rules [ std::move ( leftHandSide ) ].insert ( std::move ( rightHandSide ) ).second; } -template < class SymbolType > -bool RightLG < SymbolType >::addRule ( SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ) { - ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > rhs ( std::move ( rightHandSide ) ); +template < class TerminalSymbolType, class NonterminalSymbolType > +bool RightLG < TerminalSymbolType, NonterminalSymbolType >::addRule ( NonterminalSymbolType leftHandSide, ext::vector < TerminalSymbolType > rightHandSide ) { + ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > rhs ( std::move ( rightHandSide ) ); return addRule ( std::move ( leftHandSide ), std::move ( rhs ) ); } -template < class SymbolType > -bool RightLG < SymbolType >::addRule ( SymbolType leftHandSide, ext::pair < ext::vector < SymbolType >, SymbolType > rightHandSide ) { - ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > rhs ( std::move ( rightHandSide ) ); +template < class TerminalSymbolType, class NonterminalSymbolType > +bool RightLG < TerminalSymbolType, NonterminalSymbolType >::addRule ( NonterminalSymbolType leftHandSide, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > rightHandSide ) { + ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > rhs ( std::move ( rightHandSide ) ); return addRule ( std::move ( leftHandSide ), std::move ( rhs ) ); } -template < class SymbolType > -void RightLG < SymbolType >::addRules ( SymbolType leftHandSide, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > > rightHandSide ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void RightLG < TerminalSymbolType, NonterminalSymbolType >::addRules ( NonterminalSymbolType leftHandSide, ext::set < ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > > rightHandSide ) { if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) ) throw GrammarException ( "Rule must rewrite nonterminal symbol" ); - for ( const ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > & element : rightHandSide ) { - if ( element.template is < ext::vector < SymbolType > > ( ) ) { - for ( const auto & symbol : element.template get < ext::vector < SymbolType > > ( ) ) + for ( const ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > & element : rightHandSide ) { + if ( element.template is < ext::vector < TerminalSymbolType > > ( ) ) { + for ( const auto & symbol : element.template get < ext::vector < TerminalSymbolType > > ( ) ) if ( !getTerminalAlphabet ( ).count ( symbol ) ) throw GrammarException ( "Symbol " + ext::to_string ( symbol ) + " is not a terminal symbol" ); } else { - const ext::pair < ext::vector < SymbolType >, SymbolType > & rhs = element.template get < ext::pair < ext::vector < SymbolType >, SymbolType > > ( ); + const ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > & rhs = element.template get < ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > ( ); if ( !getNonterminalAlphabet ( ).count ( rhs.second ) ) throw GrammarException ( "Symbol " + ext::to_string ( rhs.second ) + " is not a nonterminal symbol" ); @@ -419,37 +419,37 @@ void RightLG < SymbolType >::addRules ( SymbolType leftHandSide, ext::set < ext: rules [ std::move ( leftHandSide ) ].insert ( ext::make_mover ( rightHandSide ).begin ( ), ext::make_mover ( rightHandSide ).end ( ) ); } -template < class SymbolType > -const ext::map < SymbolType, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > > > & RightLG < SymbolType >::getRules ( ) const & { +template < class TerminalSymbolType, class NonterminalSymbolType > +const ext::map < NonterminalSymbolType, ext::set < ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > > > & RightLG < TerminalSymbolType, NonterminalSymbolType >::getRules ( ) const & { return rules; } -template < class SymbolType > -ext::map < SymbolType, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > > > && RightLG < SymbolType >::getRules ( ) && { +template < class TerminalSymbolType, class NonterminalSymbolType > +ext::map < NonterminalSymbolType, ext::set < ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > > > && RightLG < TerminalSymbolType, NonterminalSymbolType >::getRules ( ) && { return std::move ( rules ); } -template < class SymbolType > -bool RightLG < SymbolType >::removeRule ( const SymbolType & leftHandSide, const ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > & rightHandSide ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +bool RightLG < TerminalSymbolType, NonterminalSymbolType >::removeRule ( const NonterminalSymbolType & leftHandSide, const ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > & rightHandSide ) { return rules[leftHandSide].erase ( rightHandSide ); } -template < class SymbolType > -bool RightLG < SymbolType >::removeRule ( const SymbolType & leftHandSide, const ext::vector < SymbolType > & rightHandSide ) { - ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > rhs ( rightHandSide ); +template < class TerminalSymbolType, class NonterminalSymbolType > +bool RightLG < TerminalSymbolType, NonterminalSymbolType >::removeRule ( const NonterminalSymbolType & leftHandSide, const ext::vector < TerminalSymbolType > & rightHandSide ) { + ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > rhs ( rightHandSide ); return removeRule ( leftHandSide, rhs ); } -template < class SymbolType > -bool RightLG < SymbolType >::removeRule ( const SymbolType & leftHandSide, const ext::pair < ext::vector < SymbolType >, SymbolType > & rightHandSide ) { - ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > rhs ( rightHandSide ); +template < class TerminalSymbolType, class NonterminalSymbolType > +bool RightLG < TerminalSymbolType, NonterminalSymbolType >::removeRule ( const NonterminalSymbolType & leftHandSide, const ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > & rightHandSide ) { + ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > rhs ( rightHandSide ); return removeRule ( leftHandSide, rhs ); } -template < class SymbolType > -int RightLG < SymbolType >::compare ( const RightLG & other ) const { +template < class TerminalSymbolType, class NonterminalSymbolType > +int RightLG < TerminalSymbolType, NonterminalSymbolType >::compare ( const RightLG & other ) const { auto first = ext::tie ( getTerminalAlphabet ( ), getNonterminalAlphabet ( ), getInitialSymbol ( ), rules ); auto second = ext::tie ( other.getTerminalAlphabet ( ), other.getNonterminalAlphabet ( ), other.getInitialSymbol ( ), other.rules ); @@ -458,8 +458,8 @@ int RightLG < SymbolType >::compare ( const RightLG & other ) const { return comp ( first, second ); } -template < class SymbolType > -void RightLG < SymbolType >::operator >>( std::ostream & out ) const { +template < class TerminalSymbolType, class NonterminalSymbolType > +void RightLG < TerminalSymbolType, NonterminalSymbolType >::operator >>( std::ostream & out ) const { out << "(RightLG " << "nonterminalAlphabet = " << getNonterminalAlphabet ( ) << "terminalAlphabet = " << getTerminalAlphabet ( ) << "initialSymbol = " @@ -467,15 +467,15 @@ void RightLG < SymbolType >::operator >>( std::ostream & out ) const { << rules << ")"; } -template < class SymbolType > -RightLG < SymbolType >::operator std::string ( ) const { +template < class TerminalSymbolType, class NonterminalSymbolType > +RightLG < TerminalSymbolType, NonterminalSymbolType >::operator std::string ( ) const { std::stringstream ss; ss << * this; return ss.str ( ); } -template < class SymbolType > -object::ObjectBase* RightLG < SymbolType >::inc() && { +template < class TerminalSymbolType, class NonterminalSymbolType > +object::ObjectBase* RightLG < TerminalSymbolType, NonterminalSymbolType >::inc() && { return new object::UniqueObject(object::Object(std::move(*this)), primitive::Integer(0)); } @@ -486,10 +486,11 @@ namespace core { /** * Helper class specifying constraints for the grammar's internal terminal alphabet component. * - * \tparam SymbolType used for the terminal alphabet of the grammar. + * \tparam TerminalSymbolType used for the terminal alphabet of the grammar. + * \tparam NonterminalSymbolType used for the nonterminal alphabet, and the initial symbol of the grammar. */ -template < class SymbolType > -class SetConstraint< grammar::RightLG < SymbolType >, SymbolType, grammar::TerminalAlphabet > { +template < class TerminalSymbolType, class NonterminalSymbolType > +class SetConstraint< grammar::RightLG < TerminalSymbolType, NonterminalSymbolType >, TerminalSymbolType, grammar::TerminalAlphabet > { public: /** * Returns true if the terminal symbol is still used in some rule of the grammar. @@ -499,16 +500,16 @@ public: * * \returns true if the symbol is used, false othervise */ - static bool used ( const grammar::RightLG < SymbolType > & grammar, const SymbolType & symbol ) { - for ( const std::pair < const SymbolType, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > > > & rule : grammar.getRules ( ) ) { - for ( const ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > & rhsTmp : rule.second ) - if ( rhsTmp.template is < ext::vector < SymbolType > > ( ) ) { - const ext::vector < SymbolType > & rhs = rhsTmp.template get < ext::vector < SymbolType > > ( ); + static bool used ( const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar, const TerminalSymbolType & symbol ) { + for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > > > & rule : grammar.getRules ( ) ) { + for ( const ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > & rhsTmp : rule.second ) + if ( rhsTmp.template is < ext::vector < TerminalSymbolType > > ( ) ) { + const ext::vector < TerminalSymbolType > & rhs = rhsTmp.template get < ext::vector < TerminalSymbolType > > ( ); if ( std::find ( rhs.begin ( ), rhs.end ( ), symbol ) != rhs.end ( ) ) return true; } else { - const ext::vector < SymbolType > & rhs = rhsTmp.template get < ext::pair < ext::vector < SymbolType >, SymbolType > > ( ).first; + const ext::vector < TerminalSymbolType > & rhs = rhsTmp.template get < ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > ( ).first; if ( std::find ( rhs.begin ( ), rhs.end ( ), symbol ) != rhs.end ( ) ) return true; @@ -527,7 +528,7 @@ public: * * \returns true */ - static bool available ( const grammar::RightLG < SymbolType > &, const SymbolType & ) { + static bool available ( const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > &, const TerminalSymbolType & ) { return true; } @@ -539,7 +540,7 @@ public: * * \throws grammar::GrammarException of the tested symbol is in nonterminal alphabet */ - static void valid ( const grammar::RightLG < SymbolType > & grammar, const SymbolType & symbol ) { + static void valid ( const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar, const TerminalSymbolType & symbol ) { if ( grammar.template accessComponent < grammar::NonterminalAlphabet > ( ).get ( ).count ( symbol ) ) throw grammar::GrammarException ( "Symbol " + ext::to_string ( symbol ) + "cannot be in terminal alphabet since it is already nonterminal alphabet" ); } @@ -548,10 +549,11 @@ public: /** * Helper class specifying constraints for the grammar's internal nonterminal alphabet component. * - * \tparam SymbolType used for the nonterminal alphabet of the grammar. + * \tparam TerminalSymbolType used for the terminal alphabet of the grammar. + * \tparam NonterminalSymbolType used for the nonterminal alphabet, and the initial symbol of the grammar. */ -template < class SymbolType > -class SetConstraint< grammar::RightLG < SymbolType >, SymbolType, grammar::NonterminalAlphabet > { +template < class TerminalSymbolType, class NonterminalSymbolType > +class SetConstraint< grammar::RightLG < TerminalSymbolType, NonterminalSymbolType >, NonterminalSymbolType, grammar::NonterminalAlphabet > { public: /** * Returns true if the nonterminal symbol is still used in some rule of the grammar or if it is the initial symbol of the grammar. @@ -561,14 +563,14 @@ public: * * \returns true if the symbol is used, false othervise */ - static bool used ( const grammar::RightLG < SymbolType > & grammar, const SymbolType & symbol ) { - for ( const std::pair < const SymbolType, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > > > & rule : grammar.getRules ( ) ) { + static bool used ( const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar, const NonterminalSymbolType & symbol ) { + for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > > > & rule : grammar.getRules ( ) ) { if ( rule.first == symbol ) return true; - for ( const ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > & rhsTmp : rule.second ) - if ( rhsTmp.template is < ext::pair < ext::vector < SymbolType >, SymbolType > > ( ) ) { - const ext::pair < ext::vector < SymbolType >, SymbolType > & rhs = rhsTmp.template get < ext::pair < ext::vector < SymbolType >, SymbolType > > ( ); + for ( const ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > & rhsTmp : rule.second ) + if ( rhsTmp.template is < ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > ( ) ) { + const ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > & rhs = rhsTmp.template get < ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > ( ); if ( rhs.second == symbol ) return true; @@ -590,7 +592,7 @@ public: * * \returns true */ - static bool available ( const grammar::RightLG < SymbolType > &, const SymbolType & ) { + static bool available ( const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > &, const NonterminalSymbolType & ) { return true; } @@ -602,7 +604,7 @@ public: * * \throws grammar::GrammarException of the tested symbol is in nonterminal alphabet */ - static void valid ( const grammar::RightLG < SymbolType > & grammar, const SymbolType & symbol ) { + static void valid ( const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar, const NonterminalSymbolType & symbol ) { if ( grammar.template accessComponent < grammar::TerminalAlphabet > ( ).get ( ).count ( symbol ) ) throw grammar::GrammarException ( "Symbol " + ext::to_string ( symbol ) + "cannot be in nonterminal alphabet since it is already in terminal alphabet" ); } @@ -611,10 +613,11 @@ public: /** * Helper class specifying constraints for the grammar's internal initial symbol element. * - * \tparam SymbolType used for the initial symbol of the grammar. + * \tparam TerminalSymbolType used for the terminal alphabet of the grammar. + * \tparam NonterminalSymbolType used for the nonterminal alphabet, and the initial symbol of the grammar. */ -template < class SymbolType > -class ElementConstraint< grammar::RightLG < SymbolType >, SymbolType, grammar::InitialSymbol > { +template < class TerminalSymbolType, class NonterminalSymbolType > +class ElementConstraint< grammar::RightLG < TerminalSymbolType, NonterminalSymbolType >, NonterminalSymbolType, grammar::InitialSymbol > { public: /** * Returns true if the symbol requested to be initial is available in nonterminal alphabet. @@ -624,7 +627,7 @@ public: * * \returns true if the tested symbol is in nonterminal alphabet */ - static bool available ( const grammar::RightLG < SymbolType > & grammar, const SymbolType & symbol ) { + static bool available ( const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar, const NonterminalSymbolType & symbol ) { return grammar.template accessComponent < grammar::NonterminalAlphabet > ( ).get ( ).count ( symbol ); } @@ -634,7 +637,7 @@ public: * \param grammar the tested grammar * \param symbol the tested symbol */ - static void valid ( const grammar::RightLG < SymbolType > &, const SymbolType & ) { + static void valid ( const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > &, const NonterminalSymbolType & ) { } }; @@ -643,19 +646,19 @@ public: * * \returns new instance of the grammar with default template parameters or unmodified instance if the template parameters were already default ones */ -template < class SymbolType > -struct normalize < grammar::RightLG < SymbolType > > { - static grammar::RightLG < > eval ( grammar::RightLG < SymbolType > && value ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +struct normalize < grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > > { + static grammar::RightLG < > eval ( grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > && value ) { ext::set < DefaultSymbolType > nonterminals = alphabet::SymbolNormalize::normalizeAlphabet ( std::move ( value ).getNonterminalAlphabet ( ) ); ext::set < DefaultSymbolType > terminals = alphabet::SymbolNormalize::normalizeAlphabet ( std::move ( value ).getTerminalAlphabet ( ) ); DefaultSymbolType initialSymbol = alphabet::SymbolNormalize::normalizeSymbol ( std::move ( value ).getInitialSymbol ( ) ); grammar::RightLG < > res ( std::move ( nonterminals ), std::move ( terminals ), std::move ( initialSymbol ) ); - for ( std::pair < SymbolType, ext::set < ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > > > && rule : ext::make_mover ( std::move ( value ).getRules ( ) ) ) { + for ( std::pair < NonterminalSymbolType, ext::set < ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > > > && rule : ext::make_mover ( std::move ( value ).getRules ( ) ) ) { ext::set < ext::variant < ext::vector < DefaultSymbolType >, ext::pair < ext::vector < DefaultSymbolType >, DefaultSymbolType > > > rhs; - for ( ext::variant < ext::vector < SymbolType >, ext::pair < ext::vector < SymbolType >, SymbolType > > && target : ext::make_mover ( rule.second ) ) + for ( ext::variant < ext::vector < TerminalSymbolType >, ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > && target : ext::make_mover ( rule.second ) ) rhs.insert ( grammar::GrammarNormalize::normalizeRHS ( std::move ( target ) ) ); DefaultSymbolType lhs = alphabet::SymbolNormalize::normalizeSymbol ( std::move ( rule.first ) ); diff --git a/alib2data/src/grammar/xml/Regular/LeftLG.h b/alib2data/src/grammar/xml/Regular/LeftLG.h index 39511380ec..1ff60ac715 100644 --- a/alib2data/src/grammar/xml/Regular/LeftLG.h +++ b/alib2data/src/grammar/xml/Regular/LeftLG.h @@ -34,8 +34,8 @@ namespace core { -template < class SymbolType > -struct xmlApi < grammar::LeftLG < SymbolType > > { +template < class TerminalSymbolType, class NonterminalSymbolType > +struct xmlApi < grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > > { /** * \brief The XML tag name of class. * @@ -67,7 +67,7 @@ struct xmlApi < grammar::LeftLG < SymbolType > > { * * \returns the new instance of the grammar */ - static grammar::LeftLG < SymbolType > parse ( ext::deque < sax::Token >::iterator & input ); + static grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > parse ( ext::deque < sax::Token >::iterator & input ); /** * Helper for parsing of individual rules of the grammar from a sequence of xml tokens. @@ -75,7 +75,7 @@ struct xmlApi < grammar::LeftLG < SymbolType > > { * \params input the iterator to sequence of xml tokens to parse from * \params grammar the grammar to add the rule to */ - static void parseRule ( ext::deque < sax::Token >::iterator & input, grammar::LeftLG < SymbolType > & grammar ); + static void parseRule ( ext::deque < sax::Token >::iterator & input, grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar ); /** * Composing to a sequence of xml tokens helper. @@ -83,7 +83,7 @@ struct xmlApi < grammar::LeftLG < SymbolType > > { * \param out sink for new xml tokens representing the grammar * \param grammar the grammar to compose */ - static void compose ( ext::deque < sax::Token > & out, const grammar::LeftLG < SymbolType > & grammar ); + static void compose ( ext::deque < sax::Token > & out, const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar ); /** * Helper for composing rules of the grammar to a sequence of xml tokens. @@ -91,18 +91,18 @@ struct xmlApi < grammar::LeftLG < SymbolType > > { * \param out sink for xml tokens representing the rules of the grammar * \param grammar the grammar to compose */ - static void composeRules ( ext::deque < sax::Token > & out, const grammar::LeftLG < SymbolType > & grammar ); + static void composeRules ( ext::deque < sax::Token > & out, const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar ); }; -template < class SymbolType > -grammar::LeftLG < SymbolType > xmlApi < grammar::LeftLG < SymbolType > >::parse ( ext::deque < sax::Token >::iterator & input ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > xmlApi < grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > >::parse ( ext::deque < sax::Token >::iterator & input ) { sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, xmlTagName ( ) ); - ext::set < SymbolType > nonterminalAlphabet = grammar::GrammarFromXMLParser::parseNonterminalAlphabet < SymbolType > ( input ); - ext::set < SymbolType > terminalAlphabet = grammar::GrammarFromXMLParser::parseTerminalAlphabet < SymbolType > ( input ); - SymbolType initialSymbol = grammar::GrammarFromXMLParser::parseInitialSymbol < SymbolType > ( input ); + ext::set < NonterminalSymbolType > nonterminalAlphabet = grammar::GrammarFromXMLParser::parseNonterminalAlphabet < NonterminalSymbolType > ( input ); + ext::set < TerminalSymbolType > terminalAlphabet = grammar::GrammarFromXMLParser::parseTerminalAlphabet < TerminalSymbolType > ( input ); + NonterminalSymbolType initialSymbol = grammar::GrammarFromXMLParser::parseInitialSymbol < NonterminalSymbolType > ( input ); - grammar::LeftLG < SymbolType > grammar ( std::move ( initialSymbol ) ); + grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > grammar ( std::move ( initialSymbol ) ); grammar.setNonterminalAlphabet ( std::move ( nonterminalAlphabet ) ); grammar.setTerminalAlphabet ( std::move ( terminalAlphabet ) ); @@ -113,16 +113,16 @@ grammar::LeftLG < SymbolType > xmlApi < grammar::LeftLG < SymbolType > >::parse return grammar; } -template < class SymbolType > -void xmlApi < grammar::LeftLG < SymbolType > >::parseRule ( ext::deque < sax::Token >::iterator & input, grammar::LeftLG < SymbolType > & grammar ) { - SymbolType lhs = grammar::GrammarFromXMLParser::parseRuleSingleSymbolLHS < SymbolType > ( input ); - ext::vector < ext::variant < SymbolType, SymbolType > > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < ext::variant < SymbolType, SymbolType > > ( input ); +template < class TerminalSymbolType, class NonterminalSymbolType > +void xmlApi < grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > >::parseRule ( ext::deque < sax::Token >::iterator & input, grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { + NonterminalSymbolType lhs = grammar::GrammarFromXMLParser::parseRuleSingleSymbolLHS < NonterminalSymbolType > ( input ); + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < ext::variant < TerminalSymbolType, NonterminalSymbolType > > ( input ); grammar::AddRawRule::addRawRule ( grammar, std::move ( lhs ), std::move ( rhs ) ); } -template < class SymbolType > -void xmlApi < grammar::LeftLG < SymbolType > >::compose ( ext::deque < sax::Token > & out, const grammar::LeftLG < SymbolType > & grammar ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void xmlApi < grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > >::compose ( ext::deque < sax::Token > & out, const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { out.emplace_back ( xmlTagName ( ), sax::Token::TokenType::START_ELEMENT ); grammar::GrammarToXMLComposer::composeNonterminalAlphabet ( out, grammar.getNonterminalAlphabet ( ) ); @@ -133,8 +133,8 @@ void xmlApi < grammar::LeftLG < SymbolType > >::compose ( ext::deque < sax::Toke out.emplace_back ( xmlTagName ( ), sax::Token::TokenType::END_ELEMENT ); } -template < class SymbolType > -void xmlApi < grammar::LeftLG < SymbolType > >::composeRules ( ext::deque < sax::Token > & out, const grammar::LeftLG < SymbolType > & grammar ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void xmlApi < grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > >::composeRules ( ext::deque < sax::Token > & out, const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { out.emplace_back ( "rules", sax::Token::TokenType::START_ELEMENT ); for ( const auto & rule : grammar.getRules ( ) ) diff --git a/alib2data/src/grammar/xml/Regular/LeftRG.h b/alib2data/src/grammar/xml/Regular/LeftRG.h index fed328d368..7decdca1e2 100644 --- a/alib2data/src/grammar/xml/Regular/LeftRG.h +++ b/alib2data/src/grammar/xml/Regular/LeftRG.h @@ -28,10 +28,14 @@ #include "../common/GrammarFromXMLParser.h" #include "../common/GrammarToXMLComposer.h" +#include <grammar/AddRawRule.h> + +#include <container/xml/ObjectsVariant.h> + namespace core { -template < class SymbolType > -struct xmlApi < grammar::LeftRG < SymbolType > > { +template < class TerminalSymbolType, class NonterminalSymbolType > +struct xmlApi < grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > > { /** * \brief The XML tag name of class. * @@ -63,7 +67,7 @@ struct xmlApi < grammar::LeftRG < SymbolType > > { * * \returns the new instance of the grammar */ - static grammar::LeftRG < SymbolType > parse ( ext::deque < sax::Token >::iterator & input ); + static grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > parse ( ext::deque < sax::Token >::iterator & input ); /** * Helper for parsing of individual rules of the grammar from a sequence of xml tokens. @@ -71,7 +75,7 @@ struct xmlApi < grammar::LeftRG < SymbolType > > { * \params input the iterator to sequence of xml tokens to parse from * \params grammar the grammar to add the rule to */ - static void parseRule ( ext::deque < sax::Token >::iterator & input, grammar::LeftRG < SymbolType > & grammar ); + static void parseRule ( ext::deque < sax::Token >::iterator & input, grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar ); /** * Composing to a sequence of xml tokens helper. @@ -79,7 +83,7 @@ struct xmlApi < grammar::LeftRG < SymbolType > > { * \param out sink for new xml tokens representing the grammar * \param grammar the grammar to compose */ - static void compose ( ext::deque < sax::Token > & out, const grammar::LeftRG < SymbolType > & grammar ); + static void compose ( ext::deque < sax::Token > & out, const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar ); /** * Helper for composing rules of the grammar to a sequence of xml tokens. @@ -87,18 +91,18 @@ struct xmlApi < grammar::LeftRG < SymbolType > > { * \param out sink for xml tokens representing the rules of the grammar * \param grammar the grammar to compose */ - static void composeRules ( ext::deque < sax::Token > & out, const grammar::LeftRG < SymbolType > & grammar ); + static void composeRules ( ext::deque < sax::Token > & out, const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar ); }; -template < class SymbolType > -grammar::LeftRG < SymbolType > xmlApi < grammar::LeftRG < SymbolType > >::parse ( ext::deque < sax::Token >::iterator & input ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > xmlApi < grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > >::parse ( ext::deque < sax::Token >::iterator & input ) { sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, xmlTagName ( ) ); - ext::set < SymbolType > nonterminalAlphabet = grammar::GrammarFromXMLParser::parseNonterminalAlphabet < SymbolType > ( input ); - ext::set < SymbolType > terminalAlphabet = grammar::GrammarFromXMLParser::parseTerminalAlphabet < SymbolType > ( input ); - SymbolType initialSymbol = grammar::GrammarFromXMLParser::parseInitialSymbol < SymbolType > ( input ); + ext::set < NonterminalSymbolType > nonterminalAlphabet = grammar::GrammarFromXMLParser::parseNonterminalAlphabet < NonterminalSymbolType > ( input ); + ext::set < TerminalSymbolType > terminalAlphabet = grammar::GrammarFromXMLParser::parseTerminalAlphabet < TerminalSymbolType > ( input ); + NonterminalSymbolType initialSymbol = grammar::GrammarFromXMLParser::parseInitialSymbol < NonterminalSymbolType > ( input ); - grammar::LeftRG < SymbolType > grammar ( std::move ( initialSymbol ) ); + grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > grammar ( std::move ( initialSymbol ) ); grammar.setNonterminalAlphabet ( std::move ( nonterminalAlphabet ) ); grammar.setTerminalAlphabet ( std::move ( terminalAlphabet ) ); @@ -112,16 +116,16 @@ grammar::LeftRG < SymbolType > xmlApi < grammar::LeftRG < SymbolType > >::parse return grammar; } -template < class SymbolType > -void xmlApi < grammar::LeftRG < SymbolType > >::parseRule ( ext::deque < sax::Token >::iterator & input, grammar::LeftRG < SymbolType > & grammar ) { - SymbolType lhs = grammar::GrammarFromXMLParser::parseRuleSingleSymbolLHS < SymbolType > ( input ); - ext::variant < SymbolType, ext::pair < SymbolType, SymbolType > > rhs = grammar::GrammarFromXMLParser::parseRuleOneOrTwoSymbolsRHS < SymbolType > ( input ); +template < class TerminalSymbolType, class NonterminalSymbolType > +void xmlApi < grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > >::parseRule ( ext::deque < sax::Token >::iterator & input, grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { + NonterminalSymbolType lhs = grammar::GrammarFromXMLParser::parseRuleSingleSymbolLHS < NonterminalSymbolType > ( input ); + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < ext::variant < TerminalSymbolType, NonterminalSymbolType > > ( input ); - grammar.addRule ( std::move ( lhs ), std::move ( rhs ) ); + grammar::AddRawRule::addRawRule ( grammar, std::move ( lhs ), std::move ( rhs ) ); } -template < class SymbolType > -void xmlApi < grammar::LeftRG < SymbolType > >::compose ( ext::deque < sax::Token > & out, const grammar::LeftRG < SymbolType > & grammar ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void xmlApi < grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > >::compose ( ext::deque < sax::Token > & out, const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { out.emplace_back ( xmlTagName ( ), sax::Token::TokenType::START_ELEMENT ); grammar::GrammarToXMLComposer::composeNonterminalAlphabet ( out, grammar.getNonterminalAlphabet ( ) ); @@ -133,8 +137,8 @@ void xmlApi < grammar::LeftRG < SymbolType > >::compose ( ext::deque < sax::Toke out.emplace_back ( xmlTagName ( ), sax::Token::TokenType::END_ELEMENT ); } -template < class SymbolType > -void xmlApi < grammar::LeftRG < SymbolType > >::composeRules ( ext::deque < sax::Token > & out, const grammar::LeftRG < SymbolType > & grammar ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void xmlApi < grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > >::composeRules ( ext::deque < sax::Token > & out, const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { out.emplace_back ( "rules", sax::Token::TokenType::START_ELEMENT ); for ( const auto & rule : grammar.getRules ( ) ) diff --git a/alib2data/src/grammar/xml/Regular/RightLG.h b/alib2data/src/grammar/xml/Regular/RightLG.h index 238da2f028..ca8768641a 100644 --- a/alib2data/src/grammar/xml/Regular/RightLG.h +++ b/alib2data/src/grammar/xml/Regular/RightLG.h @@ -34,8 +34,8 @@ namespace core { -template < class SymbolType > -struct xmlApi < grammar::RightLG < SymbolType > > { +template < class TerminalSymbolType, class NonterminalSymbolType > +struct xmlApi < grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > > { /** * \brief The XML tag name of class. * @@ -67,7 +67,7 @@ struct xmlApi < grammar::RightLG < SymbolType > > { * * \returns the new instance of the grammar */ - static grammar::RightLG < SymbolType > parse ( ext::deque < sax::Token >::iterator & input ); + static grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > parse ( ext::deque < sax::Token >::iterator & input ); /** * Helper for parsing of individual rules of the grammar from a sequence of xml tokens. @@ -75,7 +75,7 @@ struct xmlApi < grammar::RightLG < SymbolType > > { * \params input the iterator to sequence of xml tokens to parse from * \params grammar the grammar to add the rule to */ - static void parseRule ( ext::deque < sax::Token >::iterator & input, grammar::RightLG < SymbolType > & grammar ); + static void parseRule ( ext::deque < sax::Token >::iterator & input, grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar ); /** * Composing to a sequence of xml tokens helper. @@ -83,7 +83,7 @@ struct xmlApi < grammar::RightLG < SymbolType > > { * \param out sink for new xml tokens representing the grammar * \param grammar the grammar to compose */ - static void compose ( ext::deque < sax::Token > & out, const grammar::RightLG < SymbolType > & grammar ); + static void compose ( ext::deque < sax::Token > & out, const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar ); /** * Helper for composing rules of the grammar to a sequence of xml tokens. @@ -91,18 +91,18 @@ struct xmlApi < grammar::RightLG < SymbolType > > { * \param out sink for xml tokens representing the rules of the grammar * \param grammar the grammar to compose */ - static void composeRules ( ext::deque < sax::Token > & out, const grammar::RightLG < SymbolType > & grammar ); + static void composeRules ( ext::deque < sax::Token > & out, const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar ); }; -template < class SymbolType > -grammar::RightLG < SymbolType > xmlApi < grammar::RightLG < SymbolType > >::parse ( ext::deque < sax::Token >::iterator & input ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > xmlApi < grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > >::parse ( ext::deque < sax::Token >::iterator & input ) { sax::FromXMLParserHelper::popToken ( input, sax::Token::TokenType::START_ELEMENT, xmlTagName ( ) ); - ext::set < SymbolType > nonterminalAlphabet = grammar::GrammarFromXMLParser::parseNonterminalAlphabet < SymbolType > ( input ); - ext::set < SymbolType > terminalAlphabet = grammar::GrammarFromXMLParser::parseTerminalAlphabet < SymbolType > ( input ); - SymbolType initialSymbol = grammar::GrammarFromXMLParser::parseInitialSymbol < SymbolType > ( input ); + ext::set < NonterminalSymbolType > nonterminalAlphabet = grammar::GrammarFromXMLParser::parseNonterminalAlphabet < NonterminalSymbolType > ( input ); + ext::set < TerminalSymbolType > terminalAlphabet = grammar::GrammarFromXMLParser::parseTerminalAlphabet < TerminalSymbolType > ( input ); + NonterminalSymbolType initialSymbol = grammar::GrammarFromXMLParser::parseInitialSymbol < NonterminalSymbolType > ( input ); - grammar::RightLG < SymbolType > grammar ( std::move ( initialSymbol ) ); + grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > grammar ( std::move ( initialSymbol ) ); grammar.setNonterminalAlphabet ( std::move ( nonterminalAlphabet ) ); grammar.setTerminalAlphabet ( std::move ( terminalAlphabet ) ); @@ -113,16 +113,16 @@ grammar::RightLG < SymbolType > xmlApi < grammar::RightLG < SymbolType > >::pars return grammar; } -template < class SymbolType > -void xmlApi < grammar::RightLG < SymbolType > >::parseRule ( ext::deque < sax::Token >::iterator & input, grammar::RightLG < SymbolType > & grammar ) { - SymbolType lhs = grammar::GrammarFromXMLParser::parseRuleSingleSymbolLHS < SymbolType > ( input ); - ext::vector < ext::variant < SymbolType, SymbolType > > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < ext::variant < SymbolType, SymbolType > > ( input ); +template < class TerminalSymbolType, class NonterminalSymbolType > +void xmlApi < grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > >::parseRule ( ext::deque < sax::Token >::iterator & input, grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { + NonterminalSymbolType lhs = grammar::GrammarFromXMLParser::parseRuleSingleSymbolLHS < NonterminalSymbolType > ( input ); + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < ext::variant < TerminalSymbolType, NonterminalSymbolType > > ( input ); grammar::AddRawRule::addRawRule ( grammar, std::move ( lhs ), std::move ( rhs ) ); } -template < class SymbolType > -void xmlApi < grammar::RightLG < SymbolType > >::compose ( ext::deque < sax::Token > & out, const grammar::RightLG < SymbolType > & grammar ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void xmlApi < grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > >::compose ( ext::deque < sax::Token > & out, const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { out.emplace_back ( xmlTagName ( ), sax::Token::TokenType::START_ELEMENT ); grammar::GrammarToXMLComposer::composeNonterminalAlphabet ( out, grammar.getNonterminalAlphabet ( ) ); @@ -133,8 +133,8 @@ void xmlApi < grammar::RightLG < SymbolType > >::compose ( ext::deque < sax::Tok out.emplace_back ( xmlTagName ( ), sax::Token::TokenType::END_ELEMENT ); } -template < class SymbolType > -void xmlApi < grammar::RightLG < SymbolType > >::composeRules ( ext::deque < sax::Token > & out, const grammar::RightLG < SymbolType > & grammar ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void xmlApi < grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > >::composeRules ( ext::deque < sax::Token > & out, const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { out.emplace_back ( "rules", sax::Token::TokenType::START_ELEMENT ); for ( const auto & rule : grammar.getRules ( ) ) diff --git a/alib2data/src/grammar/xml/common/GrammarToXMLComposer.h b/alib2data/src/grammar/xml/common/GrammarToXMLComposer.h index 06e624911d..988d36be7a 100644 --- a/alib2data/src/grammar/xml/common/GrammarToXMLComposer.h +++ b/alib2data/src/grammar/xml/common/GrammarToXMLComposer.h @@ -57,12 +57,12 @@ public: static void composeRuleOneOrTwoSymbolsRHS ( ext::deque < sax::Token > & out, const ext::variant < T, ext::pair < R, S > > & symbols ); template < class SymbolType > static void composeRuleGNFRHS(ext::deque<sax::Token>& out, const ext::pair<SymbolType, ext::vector<SymbolType>>& symbols); - template < class SymbolType > - static void composeRuleLeftLGRHS(ext::deque<sax::Token>& out, const ext::variant<ext::vector<SymbolType>, ext::pair<SymbolType, ext::vector<SymbolType>>>& symbols); - template < class SymbolType > - static void composeRuleRightLGRHS(ext::deque<sax::Token>& out, const ext::variant<ext::vector<SymbolType>, ext::pair<ext::vector<SymbolType>, SymbolType>>& symbols); - template < class SymbolType > - static void composeRuleLGRHS(ext::deque<sax::Token>& out, const ext::variant<ext::vector<SymbolType>, ext::tuple<ext::vector<SymbolType>, SymbolType, ext::vector<SymbolType>>>& symbols); + template < class TerminalSymbolType, class NonterminalSymbolType > + static void composeRuleLeftLGRHS(ext::deque<sax::Token>& out, const ext::variant<ext::vector<TerminalSymbolType>, ext::pair<NonterminalSymbolType, ext::vector<TerminalSymbolType>>>& symbols); + template < class TerminalSymbolType, class NonterminalSymbolType > + static void composeRuleRightLGRHS(ext::deque<sax::Token>& out, const ext::variant<ext::vector<TerminalSymbolType>, ext::pair<ext::vector<TerminalSymbolType>, NonterminalSymbolType>>& symbols); + template < class TerminalSymbolType, class NonterminalSymbolType > + static void composeRuleLGRHS(ext::deque<sax::Token>& out, const ext::variant<ext::vector<TerminalSymbolType>, ext::tuple<ext::vector<TerminalSymbolType>, NonterminalSymbolType, ext::vector<TerminalSymbolType>>>& symbols); }; template < class SymbolType > @@ -171,70 +171,70 @@ void GrammarToXMLComposer::composeRuleGNFRHS(ext::deque<sax::Token>& out, const out.emplace_back("rhs", sax::Token::TokenType::END_ELEMENT); } -template < class SymbolType > -void GrammarToXMLComposer::composeRuleLGRHS(ext::deque<sax::Token>& out, const ext::variant<ext::vector<SymbolType>, ext::tuple<ext::vector<SymbolType>, SymbolType, ext::vector<SymbolType>>>& symbols) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void GrammarToXMLComposer::composeRuleLGRHS(ext::deque<sax::Token>& out, const ext::variant<ext::vector<TerminalSymbolType>, ext::tuple<ext::vector<TerminalSymbolType>, NonterminalSymbolType, ext::vector<TerminalSymbolType>>>& symbols) { out.emplace_back("rhs", sax::Token::TokenType::START_ELEMENT); - if(symbols.template is<ext::vector<SymbolType>>()) { - const ext::vector<SymbolType>& rhs = symbols.template get<ext::vector<SymbolType>>(); + if(symbols.template is<ext::vector<TerminalSymbolType>>()) { + const ext::vector<TerminalSymbolType>& rhs = symbols.template get<ext::vector<TerminalSymbolType>>(); if(rhs.size() == 0) { out.emplace_back("epsilon", sax::Token::TokenType::START_ELEMENT); out.emplace_back("epsilon", sax::Token::TokenType::END_ELEMENT); - } else for (const auto& symbol : symbols.template get<ext::vector<SymbolType>>()) { - core::xmlApi<SymbolType>::compose(out, symbol); + } else for (const auto& symbol : symbols.template get<ext::vector<TerminalSymbolType>>()) { + core::xmlApi<TerminalSymbolType>::compose(out, symbol); } } else { - const ext::tuple<ext::vector<SymbolType>, SymbolType, ext::vector<SymbolType>>& rhs = symbols.template get<ext::tuple<ext::vector<SymbolType>, SymbolType, ext::vector<SymbolType>>>(); + const ext::tuple<ext::vector<TerminalSymbolType>, NonterminalSymbolType, ext::vector<TerminalSymbolType>>& rhs = symbols.template get<ext::tuple<ext::vector<TerminalSymbolType>, NonterminalSymbolType, ext::vector<TerminalSymbolType>>>(); for (const auto& symbol : std::get<0>(rhs)) { - core::xmlApi<SymbolType>::compose(out, symbol); + core::xmlApi<TerminalSymbolType>::compose(out, symbol); } - core::xmlApi<SymbolType>::compose(out, std::get<1>(rhs)); + core::xmlApi<NonterminalSymbolType>::compose(out, std::get<1>(rhs)); for (const auto& symbol : std::get<2>(rhs)) { - core::xmlApi<SymbolType>::compose(out, symbol); + core::xmlApi<TerminalSymbolType>::compose(out, symbol); } } out.emplace_back("rhs", sax::Token::TokenType::END_ELEMENT); } -template < class SymbolType > -void GrammarToXMLComposer::composeRuleLeftLGRHS(ext::deque<sax::Token>& out, const ext::variant<ext::vector<SymbolType>, ext::pair<SymbolType, ext::vector<SymbolType>>>& symbols) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void GrammarToXMLComposer::composeRuleLeftLGRHS(ext::deque<sax::Token>& out, const ext::variant<ext::vector<TerminalSymbolType>, ext::pair<NonterminalSymbolType, ext::vector<TerminalSymbolType>>>& symbols) { out.emplace_back("rhs", sax::Token::TokenType::START_ELEMENT); - if(symbols.template is<ext::vector<SymbolType>>()) { - const ext::vector<SymbolType>& rhs = symbols.template get<ext::vector<SymbolType>>(); + if(symbols.template is<ext::vector<TerminalSymbolType>>()) { + const ext::vector<TerminalSymbolType>& rhs = symbols.template get<ext::vector<TerminalSymbolType>>(); if(rhs.size() == 0) { out.emplace_back("epsilon", sax::Token::TokenType::START_ELEMENT); out.emplace_back("epsilon", sax::Token::TokenType::END_ELEMENT); - } else for (const auto& symbol : symbols.template get<ext::vector<SymbolType>>()) { - core::xmlApi<SymbolType>::compose(out, symbol); + } else for (const auto& symbol : symbols.template get<ext::vector<TerminalSymbolType>>()) { + core::xmlApi<TerminalSymbolType>::compose(out, symbol); } } else { - const ext::pair<SymbolType, ext::vector<SymbolType>>& rhs = symbols.template get<ext::pair<SymbolType, ext::vector<SymbolType>>>(); - core::xmlApi<SymbolType>::compose(out, rhs.first); + const ext::pair<NonterminalSymbolType, ext::vector<TerminalSymbolType>>& rhs = symbols.template get<ext::pair<NonterminalSymbolType, ext::vector<TerminalSymbolType>>>(); + core::xmlApi<NonterminalSymbolType>::compose(out, rhs.first); for (const auto& symbol : rhs.second) { - core::xmlApi<SymbolType>::compose(out, symbol); + core::xmlApi<TerminalSymbolType>::compose(out, symbol); } } out.emplace_back("rhs", sax::Token::TokenType::END_ELEMENT); } -template < class SymbolType > -void GrammarToXMLComposer::composeRuleRightLGRHS(ext::deque<sax::Token>& out, const ext::variant<ext::vector<SymbolType>, ext::pair<ext::vector<SymbolType>, SymbolType>>& symbols) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void GrammarToXMLComposer::composeRuleRightLGRHS(ext::deque<sax::Token>& out, const ext::variant<ext::vector<TerminalSymbolType>, ext::pair<ext::vector<TerminalSymbolType>, NonterminalSymbolType>>& symbols) { out.emplace_back("rhs", sax::Token::TokenType::START_ELEMENT); - if(symbols.template is<ext::vector<SymbolType>>()) { - const ext::vector<SymbolType>& rhs = symbols.template get<ext::vector<SymbolType>>(); + if(symbols.template is<ext::vector<TerminalSymbolType>>()) { + const ext::vector<TerminalSymbolType>& rhs = symbols.template get<ext::vector<TerminalSymbolType>>(); if(rhs.size() == 0) { out.emplace_back("epsilon", sax::Token::TokenType::START_ELEMENT); out.emplace_back("epsilon", sax::Token::TokenType::END_ELEMENT); } else { - for (const auto& symbol : symbols.template get<ext::vector<SymbolType>>()) { - core::xmlApi<SymbolType>::compose(out, symbol); + for (const auto& symbol : symbols.template get<ext::vector<TerminalSymbolType>>()) { + core::xmlApi<TerminalSymbolType>::compose(out, symbol); } } } else { - const ext::pair<ext::vector<SymbolType>, SymbolType>& rhs = symbols.template get<ext::pair<ext::vector<SymbolType>, SymbolType>>(); + const ext::pair<ext::vector<TerminalSymbolType>, NonterminalSymbolType>& rhs = symbols.template get<ext::pair<ext::vector<TerminalSymbolType>, NonterminalSymbolType>>(); for (const auto& symbol : rhs.first) { - core::xmlApi<SymbolType>::compose(out, symbol); + core::xmlApi<TerminalSymbolType>::compose(out, symbol); } - core::xmlApi<SymbolType>::compose(out, rhs.second); + core::xmlApi<NonterminalSymbolType>::compose(out, rhs.second); } out.emplace_back("rhs", sax::Token::TokenType::END_ELEMENT); } diff --git a/alib2data/test-src/grammar/GrammarTest.cpp b/alib2data/test-src/grammar/GrammarTest.cpp index 4b5f5b94aa..e3d0563a06 100644 --- a/alib2data/test-src/grammar/GrammarTest.cpp +++ b/alib2data/test-src/grammar/GrammarTest.cpp @@ -125,18 +125,18 @@ void GrammarTest::testRegularParser() { } { - grammar::RightLG < > grammar(DefaultSymbolType(1)); + grammar::RightLG < std::string, int > grammar ( 1 ); - grammar.addNonterminalSymbol(DefaultSymbolType(1)); - grammar.addNonterminalSymbol(DefaultSymbolType(2)); - grammar.addNonterminalSymbol(DefaultSymbolType(3)); - grammar.addTerminalSymbol(DefaultSymbolType("a")); - grammar.addTerminalSymbol(DefaultSymbolType("b")); + grammar.addNonterminalSymbol ( 1 ); + grammar.addNonterminalSymbol ( 2 ); + grammar.addNonterminalSymbol ( 3 ); + grammar.addTerminalSymbol ( "a" ); + grammar.addTerminalSymbol ( "b" ); - grammar.addRule(DefaultSymbolType(1), ext::make_pair(ext::vector<DefaultSymbolType> {DefaultSymbolType("a")}, DefaultSymbolType(2))); - grammar.addRule(DefaultSymbolType(2), ext::make_pair(ext::vector<DefaultSymbolType> {DefaultSymbolType("b")}, DefaultSymbolType(3))); - grammar.addRule(DefaultSymbolType(1), ext::make_pair(ext::vector<DefaultSymbolType> {}, DefaultSymbolType(3))); - grammar.addRule(DefaultSymbolType(1), ext::make_pair(ext::vector<DefaultSymbolType> {DefaultSymbolType("a"), DefaultSymbolType("a"), DefaultSymbolType("a")}, DefaultSymbolType(2))); + grammar.addRule ( 1, ext::make_pair ( ext::vector < std::string > { "a" }, 2 ) ); + grammar.addRule ( 2, ext::make_pair ( ext::vector < std::string > { "b" }, 3 ) ); + grammar.addRule ( 1, ext::make_pair ( ext::vector < std::string > {}, 3 ) ); + grammar.addRule ( 1, ext::make_pair ( ext::vector < std::string > { "a", "a", "a" }, 2 ) ); CPPUNIT_ASSERT( grammar == grammar ); { @@ -146,7 +146,7 @@ void GrammarTest::testRegularParser() { std::cout << tmp << std::endl; ext::deque<sax::Token> tokens2 = sax::SaxParseInterface::parseMemory ( tmp ); - grammar::RightLG < > grammar2 = factory::XmlDataFactory::fromTokens (std::move(tokens2)); + grammar::RightLG < std::string, int > grammar2 = factory::XmlDataFactory::fromTokens (std::move(tokens2)); CPPUNIT_ASSERT( grammar == grammar2 ); } -- GitLab