From b1c33b52430a2835243c488d5c85c4c70ed0d628 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Mon, 17 Sep 2018 14:18:21 +0200 Subject: [PATCH] prepare rawrules api for type different terminals and nonterminals in grammars --- .../src/grammar/convert/ToAutomatonBottomUp.h | 2 +- .../src/grammar/generate/GenerateUpToLength.h | 40 ++-- .../grammar/properties/NullableNonterminals.h | 18 +- .../properties/ProductiveNonterminals.h | 32 +-- .../grammar/properties/RecursiveNonterminal.h | 26 +-- .../grammar/properties/UnreachableSymbols.cpp | 18 +- .../grammar/properties/UnreachableSymbols.h | 14 +- .../src/grammar/simplify/EpsilonRemover.h | 16 +- .../src/grammar/simplify/SimpleRulesRemover.h | 6 +- .../simplify/UnproductiveSymbolsRemover.h | 20 +- .../simplify/UnreachableSymbolsRemover.h | 16 +- .../grammar/parsing/CornerSubstitution.cpp | 2 +- .../parsing/DeterministicLL1Grammar.cpp | 16 +- .../grammar/parsing/ExtractRightContext.cpp | 2 +- .../src/grammar/parsing/First.cpp | 38 ++-- .../src/grammar/parsing/First.h | 68 +++--- .../src/grammar/parsing/Follow.h | 54 ++--- .../parsing/HandleFirstFirstConflict.cpp | 4 +- .../parsing/HandleFirstFirstConflict.h | 2 +- .../parsing/HandleFirstFollowConflict.cpp | 4 +- .../parsing/HandleFirstFollowConflict.h | 2 +- .../src/grammar/parsing/LL1ParseTable.cpp | 18 +- .../src/grammar/parsing/LL1ParseTable.h | 28 +-- .../test-src/grammar/parsing/FirstTest.cpp | 52 ++--- .../grammar/parsing/LL1ParseTable.cpp | 20 +- alib2data/src/grammar/AddRawRule.cpp | 18 +- alib2data/src/grammar/AddRawRule.h | 158 +++++++------- alib2data/src/grammar/Grammar.h | 5 +- alib2data/src/grammar/RawRules.cpp | 18 +- alib2data/src/grammar/RawRules.h | 196 +++++++++--------- alib2data/src/grammar/xml/ContextFree/LG.h | 2 +- alib2data/src/grammar/xml/Regular/LeftLG.h | 2 +- alib2data/src/grammar/xml/Regular/RightLG.h | 2 +- .../common/GrammarFromStringParserCommon.h | 39 ++-- .../common/GrammarToStringComposerCommon.h | 18 +- 35 files changed, 488 insertions(+), 488 deletions(-) diff --git a/alib2algo/src/grammar/convert/ToAutomatonBottomUp.h b/alib2algo/src/grammar/convert/ToAutomatonBottomUp.h index 03be378593..bcccf27e72 100644 --- a/alib2algo/src/grammar/convert/ToAutomatonBottomUp.h +++ b/alib2algo/src/grammar/convert/ToAutomatonBottomUp.h @@ -29,7 +29,7 @@ public: /** * Performs conversion. */ - template <class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T >, class EpsilonType = DefaultEpsilonType, class StateType = DefaultStateType > + template <class T, class SymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class EpsilonType = DefaultEpsilonType, class StateType = DefaultStateType > static automaton::NPDA < SymbolType, EpsilonType, SymbolType, StateType > convert(const T& grammar); }; diff --git a/alib2algo/src/grammar/generate/GenerateUpToLength.h b/alib2algo/src/grammar/generate/GenerateUpToLength.h index 22e8db4604..45e0487520 100644 --- a/alib2algo/src/grammar/generate/GenerateUpToLength.h +++ b/alib2algo/src/grammar/generate/GenerateUpToLength.h @@ -30,49 +30,49 @@ namespace generate { */ class GenerateUpToLength { public: - template < class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > - static ext::set < string::LinearString < SymbolType > > generate ( const T & grammar, unsigned length ); + template < class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NontermimnalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > > + static ext::set < string::LinearString < TerminalSymbolType > > generate ( const T & grammar, unsigned length ); }; -template < class T, class SymbolType > -ext::set < string::LinearString < SymbolType > > GenerateUpToLength::generate ( const T & grammar, unsigned length ) { - ext::set < string::LinearString < SymbolType > > res; +template < class T, class TerminalSymbolType, class NonterminalSymbolType > +ext::set < string::LinearString < TerminalSymbolType > > GenerateUpToLength::generate ( const T & grammar, unsigned length ) { + ext::set < string::LinearString < TerminalSymbolType > > res; - ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > rules = grammar::RawRules::getRawRules ( grammar ); + ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > rules = grammar::RawRules::getRawRules ( grammar ); if ( grammar.getGeneratesEpsilon ( ) ) { - res.insert ( string::LinearString < SymbolType > { } ); - rules [ grammar.getInitialSymbol ( ) ].erase ( ext::vector < ext::variant < SymbolType > > { } ); + res.insert ( string::LinearString < TerminalSymbolType > { } ); + rules [ grammar.getInitialSymbol ( ) ].erase ( ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > { } ); } - ext::deque < std::pair < ext::vector < SymbolType >, ext::vector < ext::variant < SymbolType > > > > data; - data.push_back ( std::make_pair ( ext::vector < SymbolType > { }, ext::vector < ext::variant < SymbolType > > { ext::variant < SymbolType > ( grammar.getInitialSymbol ( ) ) } ) ); + ext::deque < std::pair < ext::vector < TerminalSymbolType >, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > data; + data.push_back ( std::make_pair ( ext::vector < TerminalSymbolType > { }, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > { ext::variant < TerminalSymbolType, NonterminalSymbolType > ( grammar.getInitialSymbol ( ) ) } ) ); while ( ! data.empty ( ) ) { - std::pair < ext::vector < SymbolType >, ext::vector < ext::variant < SymbolType > > > item = std::move ( data.back ( ) ); + std::pair < ext::vector < TerminalSymbolType >, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > item = std::move ( data.back ( ) ); data.pop_back ( ); - ext::variant < SymbolType > nonterminal = std::move ( item.second.back ( ) ); + ext::variant < TerminalSymbolType, NonterminalSymbolType > top = std::move ( item.second.back ( ) ); item.second.pop_back ( ); - if ( ! nonterminal.template is < SymbolType > ( ) ) + if ( ! top.template is < NonterminalSymbolType > ( ) ) // maybe not needed continue; - auto rule = rules.find ( nonterminal.template get < SymbolType > ( ) ); + auto rule = rules.find ( top.template get < NonterminalSymbolType > ( ) ); if ( rule == rules.end ( ) ) continue; - for ( const ext::vector < ext::variant < SymbolType > > & rhs : rule->second ) { + for ( const ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs : rule->second ) { if ( item.first.size ( ) + item.second.size ( ) + rhs.size ( ) > length ) continue; - ext::vector < ext::variant < SymbolType > > newStack ( item.second ); - ext::vector < SymbolType > newString ( item.first ); + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > newStack ( item.second ); + ext::vector < TerminalSymbolType > newString ( item.first ); newStack.insert ( newStack.end ( ), rhs.rbegin ( ), rhs.rend ( ) ); - while ( ! newStack.empty ( ) && newStack.back ( ).template is < SymbolType > ( ) && grammar.getTerminalAlphabet ( ).count ( newStack.back ( ).template get < SymbolType > ( ) ) ) { - newString.push_back ( std::move ( newStack.back ( ).template get < SymbolType > ( ) ) ); + while ( ! newStack.empty ( ) && grammar.getTerminalAlphabet ( ).count ( newStack.back ( ).template get < TerminalSymbolType > ( ) ) ) { + newString.push_back ( std::move ( newStack.back ( ).template get < TerminalSymbolType > ( ) ) ); newStack.pop_back ( ); } if ( newStack.empty ( ) ) { - res.insert ( string::LinearString < SymbolType > ( newString ) ); + res.insert ( string::LinearString < TerminalSymbolType > ( newString ) ); } else { data.push_back ( std::make_pair ( std::move ( newString ), std::move ( newStack ) ) ); } diff --git a/alib2algo/src/grammar/properties/NullableNonterminals.h b/alib2algo/src/grammar/properties/NullableNonterminals.h index 53fe41847f..3e04716650 100644 --- a/alib2algo/src/grammar/properties/NullableNonterminals.h +++ b/alib2algo/src/grammar/properties/NullableNonterminals.h @@ -34,25 +34,25 @@ public: * @param grammar grammar * @return set of nullable nonterminals from grammar */ - template<class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > - static ext::set<SymbolType> getNullableNonterminals(const T& grammar); + template<class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > > + static ext::set < NonterminalSymbolType > getNullableNonterminals ( const T & grammar ); }; -template<class T, class SymbolType > -ext::set<SymbolType> NullableNonterminals::getNullableNonterminals(const T& grammar) { +template < class T, class TerminalSymbolType, class NonterminalSymbolType > +ext::set < NonterminalSymbolType > NullableNonterminals::getNullableNonterminals ( const T & grammar ) { auto rawRules = grammar::RawRules::getRawRules ( grammar ); - ext::deque < ext::set < SymbolType > > Ni; + ext::deque < ext::set < NonterminalSymbolType > > Ni; - Ni.push_back ( ext::set < SymbolType > { } ); + Ni.push_back ( ext::set < NonterminalSymbolType > { } ); int i = 1; - auto testCallback = [ & ] ( const ext::variant < SymbolType > & symb ) { - return Ni.at ( i - 1 ).count ( symb.template get < SymbolType > ( ) ); + auto testCallback = [ & ] ( const ext::variant < TerminalSymbolType, NonterminalSymbolType > & symb ) { + return Ni.at ( i - 1 ).count ( symb ); }; while ( true ) { - Ni.push_back ( ext::set < SymbolType > { } ); + Ni.push_back ( ext::set < NonterminalSymbolType > { } ); for ( const auto & rule : rawRules ) { for ( const auto & rhs : rule.second ) { if ( rhs.size ( ) == 0 || std::all_of ( rhs.begin ( ), rhs.end ( ), testCallback ) ) { diff --git a/alib2algo/src/grammar/properties/ProductiveNonterminals.h b/alib2algo/src/grammar/properties/ProductiveNonterminals.h index 6cbf0c4840..d088685006 100644 --- a/alib2algo/src/grammar/properties/ProductiveNonterminals.h +++ b/alib2algo/src/grammar/properties/ProductiveNonterminals.h @@ -28,43 +28,43 @@ public: /** * Implements steps 1 through 3 in Melichar 3.6 */ - template<class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > - static ext::set<SymbolType> getProductiveNonterminals( const T & grammar ); + template<class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > > + static ext::set < NonterminalSymbolType > getProductiveNonterminals ( const T & grammar ); }; -template<class T, class SymbolType > -ext::set<SymbolType> ProductiveNonterminals::getProductiveNonterminals( const T & grammar ) { +template < class T, class TerminalSymbolType, class NonterminalSymbolType > +ext::set < NonterminalSymbolType > ProductiveNonterminals::getProductiveNonterminals ( const T & grammar ) { auto rawRules = grammar::RawRules::getRawRules ( grammar ); // 1. - ext::deque<ext::set<SymbolType>> Ni; - Ni.push_back( ext::set<SymbolType>( ) ); + ext::deque < ext::set < NonterminalSymbolType > > Ni; + Ni.push_back ( ext::set < NonterminalSymbolType > ( ) ); int i = 1; - auto testCallback = [ & ]( const ext::variant < SymbolType > & symbol ) -> bool { - return ( symbol.template is < SymbolType > ( ) && Ni.at( i - 1 ) . count ( symbol.template get < SymbolType > ( ) ) ) - || ( symbol.template is < SymbolType > ( ) && grammar.getTerminalAlphabet( ). count ( symbol.template get < SymbolType > ( ) ) ); + auto testCallback = [ & ]( const ext::variant < TerminalSymbolType, NonterminalSymbolType > & symbol ) -> bool { + return ( symbol.template is < NonterminalSymbolType > ( ) && Ni.at( i - 1 ) . count ( symbol.template get < NonterminalSymbolType > ( ) ) ) + || ( symbol.template is < TerminalSymbolType > ( ) && grammar.getTerminalAlphabet( ). count ( symbol.template get < TerminalSymbolType > ( ) ) ); }; // 2. while( true ) { - Ni.push_back( Ni.at( i - 1 ) ); + Ni.push_back ( Ni.at( i - 1 ) ); - for( const auto & rule : rawRules ) { - for( const auto & rhs : rule.second ) { - if( std::all_of( rhs.begin( ), rhs.end( ), testCallback ) ) - Ni.at( i ).insert( rule.first ); + for ( const auto & rule : rawRules ) { + for ( const auto & rhs : rule.second ) { + if ( std::all_of ( rhs.begin ( ), rhs.end ( ), testCallback ) ) + Ni.at ( i ).insert ( rule.first ); } } - if( Ni.at( i ) == Ni.at( i - 1 ) ) + if( Ni.at ( i ) == Ni.at ( i - 1 ) ) break; i = i + 1; } // 3. - return Ni.at( i ); + return Ni.at ( i ); } } /* namespace properties */ diff --git a/alib2algo/src/grammar/properties/RecursiveNonterminal.h b/alib2algo/src/grammar/properties/RecursiveNonterminal.h index d0225eb95a..3c3ac531e5 100644 --- a/alib2algo/src/grammar/properties/RecursiveNonterminal.h +++ b/alib2algo/src/grammar/properties/RecursiveNonterminal.h @@ -33,36 +33,36 @@ public: * @param nonterminal nonterminal * @return bool which denote whether the nonterminal is recursive in the grammar */ - template < class T, class SymbolType > - static bool isNonterminalRecursive ( const T & grammar, const SymbolType & nonterminal ); + template < class T, class NonterminalSymbolType, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T > > + static bool isNonterminalRecursive ( const T & grammar, const NonterminalSymbolType & nonterminal ); }; -template < class T, class SymbolType > -bool RecursiveNonterminal::isNonterminalRecursive ( const T & grammar, const SymbolType & nonterminal ) { +template < class T, class NonterminalSymbolType, class TerminalSymbolType > +bool RecursiveNonterminal::isNonterminalRecursive ( const T & grammar, const NonterminalSymbolType & nonterminal ) { if ( grammar.getNonterminalAlphabet ( ).count ( nonterminal ) == 0 ) throw exception::CommonException ( "Nonterminal symbol \"" + ( std::string ) nonterminal + "\" is not present in grammar." ); - ext::deque < ext::set < SymbolType > > Ni; - Ni.push_back ( ext::set < SymbolType > { nonterminal } ); + ext::deque < ext::set < NonterminalSymbolType > > Ni; + Ni.push_back ( ext::set < NonterminalSymbolType > { nonterminal } ); unsigned i = 1; auto rawRules = grammar::RawRules::getRawRules ( grammar ); auto nullable = grammar::properties::NullableNonterminals::getNullableNonterminals ( grammar ); while ( i <= grammar.getNonterminalAlphabet ( ).size ( ) ) { - Ni.push_back ( ext::set < SymbolType > { } ); + Ni.push_back ( ext::set < NonterminalSymbolType > { } ); - for ( const SymbolType & lhs : Ni.at ( i - 1 ) ) + for ( const NonterminalSymbolType & lhs : Ni.at ( i - 1 ) ) if ( rawRules.find ( lhs ) != rawRules.end ( ) ) - for ( const ext::vector < ext::variant < SymbolType > > & rhs : rawRules.find ( lhs )->second ) - for ( const ext::variant < SymbolType > & rhsSymbol : rhs ) { - if ( rhsSymbol.template is < SymbolType > ( ) && grammar.getTerminalAlphabet ( ).count ( rhsSymbol.template get < SymbolType > ( ) ) ) + for ( const ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs : rawRules.find ( lhs )->second ) + for ( const ext::variant < TerminalSymbolType, NonterminalSymbolType > & rhsSymbol : rhs ) { + if ( grammar.getTerminalAlphabet ( ).count ( rhsSymbol ) ) break; - Ni.at ( i ).insert ( rhsSymbol.template get < SymbolType > ( ) ); + Ni.at ( i ).insert ( rhsSymbol.template get < NonterminalSymbolType > ( ) ); - if ( ! nullable.count ( rhsSymbol.template get < SymbolType > ( ) ) ) + if ( ! nullable.count ( rhsSymbol.template get < NonterminalSymbolType > ( ) ) ) break; } diff --git a/alib2algo/src/grammar/properties/UnreachableSymbols.cpp b/alib2algo/src/grammar/properties/UnreachableSymbols.cpp index 6c4fc7fc94..9415c5b6e8 100644 --- a/alib2algo/src/grammar/properties/UnreachableSymbols.cpp +++ b/alib2algo/src/grammar/properties/UnreachableSymbols.cpp @@ -22,15 +22,15 @@ namespace grammar { namespace properties { -auto UnreachableSymbolsCFG = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType > >, const grammar::CFG < > & > ( UnreachableSymbols::getUnreachableSymbols ); -auto UnreachableSymbolsEpsilonFreeCFG = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType > >, const grammar::EpsilonFreeCFG < > & > ( UnreachableSymbols::getUnreachableSymbols ); -auto UnreachableSymbolsGNF = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType > >, const grammar::GNF < > & > ( UnreachableSymbols::getUnreachableSymbols ); -auto UnreachableSymbolsCNF = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType > >, const grammar::CNF < > & > ( UnreachableSymbols::getUnreachableSymbols ); -auto UnreachableSymbolsLG = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType > >, const grammar::LG < > & > ( UnreachableSymbols::getUnreachableSymbols ); -auto UnreachableSymbolsLeftLG = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType > >, const grammar::LeftLG < > & > ( UnreachableSymbols::getUnreachableSymbols ); -auto UnreachableSymbolsLeftRG = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType > >, const grammar::LeftRG < > & > ( UnreachableSymbols::getUnreachableSymbols ); -auto UnreachableSymbolsRightLG = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType > >, const grammar::RightLG < > & > ( UnreachableSymbols::getUnreachableSymbols ); -auto UnreachableSymbolsRightRG = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType > >, const grammar::RightRG < > & > ( UnreachableSymbols::getUnreachableSymbols ); +auto UnreachableSymbolsCFG = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType, DefaultSymbolType > >, const grammar::CFG < > & > ( UnreachableSymbols::getUnreachableSymbols ); +auto UnreachableSymbolsEpsilonFreeCFG = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType, DefaultSymbolType > >, const grammar::EpsilonFreeCFG < > & > ( UnreachableSymbols::getUnreachableSymbols ); +auto UnreachableSymbolsGNF = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType, DefaultSymbolType > >, const grammar::GNF < > & > ( UnreachableSymbols::getUnreachableSymbols ); +auto UnreachableSymbolsCNF = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType, DefaultSymbolType > >, const grammar::CNF < > & > ( UnreachableSymbols::getUnreachableSymbols ); +auto UnreachableSymbolsLG = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType, DefaultSymbolType > >, const grammar::LG < > & > ( UnreachableSymbols::getUnreachableSymbols ); +auto UnreachableSymbolsLeftLG = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType, DefaultSymbolType > >, const grammar::LeftLG < > & > ( UnreachableSymbols::getUnreachableSymbols ); +auto UnreachableSymbolsLeftRG = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType, DefaultSymbolType > >, const grammar::LeftRG < > & > ( UnreachableSymbols::getUnreachableSymbols ); +auto UnreachableSymbolsRightLG = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType, DefaultSymbolType > >, const grammar::RightLG < > & > ( UnreachableSymbols::getUnreachableSymbols ); +auto UnreachableSymbolsRightRG = registration::AbstractRegister < UnreachableSymbols, ext::set < ext::variant < DefaultSymbolType, DefaultSymbolType > >, const grammar::RightRG < > & > ( UnreachableSymbols::getUnreachableSymbols ); } /* namespace properties */ diff --git a/alib2algo/src/grammar/properties/UnreachableSymbols.h b/alib2algo/src/grammar/properties/UnreachableSymbols.h index 5bd8fd8011..3c0713c946 100644 --- a/alib2algo/src/grammar/properties/UnreachableSymbols.h +++ b/alib2algo/src/grammar/properties/UnreachableSymbols.h @@ -28,17 +28,17 @@ public: /** * Implements */ - template<class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > - static ext::set < ext::variant < SymbolType > > getUnreachableSymbols ( const T & grammar ); + template<class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > > + static ext::set < ext::variant < TerminalSymbolType, NonterminalSymbolType > > getUnreachableSymbols ( const T & grammar ); }; -template<class T, class SymbolType > -ext::set < ext::variant < SymbolType > > UnreachableSymbols::getUnreachableSymbols( const T & grammar ) { +template<class T, class TerminalSymbolType, class NonterminalSymbolType > +ext::set < ext::variant < TerminalSymbolType, NonterminalSymbolType > > UnreachableSymbols::getUnreachableSymbols( const T & grammar ) { auto rawRules = grammar::RawRules::getRawRules ( grammar ); // 1 - ext::deque < ext::set < ext::variant < SymbolType > > > Vi; - Vi.push_back ( ext::set < ext::variant < SymbolType > > ( ) ); + ext::deque < ext::set < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > Vi; + Vi.push_back ( ext::set < ext::variant < TerminalSymbolType, NonterminalSymbolType > > ( ) ); Vi.at ( 0 ).insert ( grammar.getInitialSymbol ( ) ); int i = 1; @@ -48,7 +48,7 @@ ext::set < ext::variant < SymbolType > > UnreachableSymbols::getUnreachableSymbo Vi.push_back ( Vi.at ( i - 1 ) ); for ( const auto & rule : rawRules ) { - if ( Vi.at( i - 1 ).count ( ext::variant < SymbolType > ( rule.first ) ) ) { + if ( Vi.at( i - 1 ).count ( rule.first ) ) { for ( const auto & rhs : rule.second ) { Vi.at ( i ).insert( rhs.begin( ), rhs.end( ) ); } diff --git a/alib2algo/src/grammar/simplify/EpsilonRemover.h b/alib2algo/src/grammar/simplify/EpsilonRemover.h index 2ff6355534..b91a62ed3e 100644 --- a/alib2algo/src/grammar/simplify/EpsilonRemover.h +++ b/alib2algo/src/grammar/simplify/EpsilonRemover.h @@ -32,10 +32,10 @@ namespace grammar { namespace simplify { class EpsilonRemover { - template < class SymbolType > - static void removeNullableNonterminals(grammar::EpsilonFreeCFG < SymbolType > & grammar, const ext::set<SymbolType>& nullableNonterminals, const SymbolType& lhs, const ext::vector<ext::variant < SymbolType > > & rhs, unsigned i, ext::vector<SymbolType> clear); + template < class TerminalSymbolType, class NonterminalSymbolType > + static void removeNullableNonterminals(grammar::EpsilonFreeCFG < TerminalSymbolType > & grammar, const ext::set < NonterminalSymbolType >& nullableNonterminals, const NonterminalSymbolType & lhs, const ext::vector<ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs, unsigned i, ext::vector < TerminalSymbolType > clear); - template<class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > + template<class T, class SymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T > > static grammar::EpsilonFreeCFG < SymbolType > removeInternal( const T & origGrammar ); public: template < class SymbolType > @@ -58,18 +58,18 @@ public: static grammar::RightRG < SymbolType > remove( const grammar::RightRG < SymbolType > & grammar ); }; -template < class SymbolType > -void EpsilonRemover::removeNullableNonterminals(grammar::EpsilonFreeCFG < SymbolType > & grammar, const ext::set<SymbolType>& nullableNonterminals, const SymbolType& lhs, const ext::vector<ext::variant < SymbolType > >& rhs, unsigned i, ext::vector < SymbolType > clear) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void EpsilonRemover::removeNullableNonterminals(grammar::EpsilonFreeCFG < TerminalSymbolType > & grammar, const ext::set < NonterminalSymbolType > & nullableNonterminals, const NonterminalSymbolType & lhs, const ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs, unsigned i, ext::vector < TerminalSymbolType > clear) { if(rhs.size() == i) { if(clear.size() == 0) return; grammar.addRule(lhs, clear); - } else if(rhs[i].template is < SymbolType > ( ) && nullableNonterminals.find ( rhs [ i ].template get < SymbolType > ( ) ) != nullableNonterminals.end()) { + } else if(rhs[i].template is < NonterminalSymbolType > ( ) && nullableNonterminals.find ( rhs [ i ].template get < NonterminalSymbolType > ( ) ) != nullableNonterminals.end()) { removeNullableNonterminals(grammar, nullableNonterminals, lhs, rhs, i+1, clear); - clear.push_back(rhs[i].template get < SymbolType > ( ) ); + clear.push_back(rhs[i].template get < NonterminalSymbolType > ( ) ); removeNullableNonterminals(grammar, nullableNonterminals, lhs, rhs, i+1, clear); } else { - clear.push_back(rhs[i].template get < SymbolType > ( ) ); + clear.push_back(rhs[i].template get < TerminalSymbolType > ( ) ); removeNullableNonterminals(grammar, nullableNonterminals, lhs, rhs, i+1, clear); } } diff --git a/alib2algo/src/grammar/simplify/SimpleRulesRemover.h b/alib2algo/src/grammar/simplify/SimpleRulesRemover.h index b5fbd9c303..63c31922c3 100644 --- a/alib2algo/src/grammar/simplify/SimpleRulesRemover.h +++ b/alib2algo/src/grammar/simplify/SimpleRulesRemover.h @@ -32,7 +32,7 @@ namespace grammar { namespace simplify { class SimpleRulesRemover { - template<class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > + template<class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > > static T removeNonEpsilonFree( const T & origGrammar ); public: @@ -56,7 +56,7 @@ public: static grammar::RightRG < SymbolType > remove( const grammar::RightRG < SymbolType > & grammar ); }; -template < class T, class SymbolType > +template < class T, class TerminalSymbolType, class NonterminalSymbolType > T SimpleRulesRemover::removeNonEpsilonFree( const T & origGrammar ) { T grammar(origGrammar.getInitialSymbol()); @@ -69,7 +69,7 @@ T SimpleRulesRemover::removeNonEpsilonFree( const T & origGrammar ) { auto rawRules = grammar::RawRules::getRawRules ( origGrammar ); for( const auto & symbol : origGrammar.getNonterminalAlphabet() ) { - ext::set<SymbolType> simpleRulesClosure = grammar::properties::NonterminalUnitRuleCycle::getNonterminalUnitRuleCycle(origGrammar, symbol); + ext::set < NonterminalSymbolType > simpleRulesClosure = grammar::properties::NonterminalUnitRuleCycle::getNonterminalUnitRuleCycle(origGrammar, symbol); for( const auto & closureSymbol : simpleRulesClosure ) { auto rules = rawRules.find(closureSymbol); if(rules != rawRules.end()) for( const auto& rawRule : rules->second ) { diff --git a/alib2algo/src/grammar/simplify/UnproductiveSymbolsRemover.h b/alib2algo/src/grammar/simplify/UnproductiveSymbolsRemover.h index 16f6eaadc4..46627dcb9e 100644 --- a/alib2algo/src/grammar/simplify/UnproductiveSymbolsRemover.h +++ b/alib2algo/src/grammar/simplify/UnproductiveSymbolsRemover.h @@ -39,29 +39,29 @@ public: /** * Removes unproductive (or useless - terminology) symbols - Melichar 3.12 */ - template < class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > + template < class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > > static T remove( const T & grammar ); }; -template < class T, class SymbolType > +template < class T, class TerminalSymbolType, class NonterminalSymbolType > T UnproductiveSymbolsRemover::remove( const T & grammar ) { // 1. - ext::set<SymbolType> Nt = grammar::properties::ProductiveNonterminals::getProductiveNonterminals( grammar ); + ext::set < NonterminalSymbolType > Nt = grammar::properties::ProductiveNonterminals::getProductiveNonterminals ( grammar ); - T ret(grammar.getInitialSymbol( ) ); + T ret ( grammar.getInitialSymbol ( ) ); for( const auto & symbol : Nt ) - ret.addNonterminalSymbol( symbol ); + ret.addNonterminalSymbol ( symbol ); for( const auto & symbol : grammar.getTerminalAlphabet( ) ) - ret.addTerminalSymbol( symbol ); + ret.addTerminalSymbol ( symbol ); auto rawRules = grammar::RawRules::getRawRules ( grammar ); - const ext::set<SymbolType> & terminals = ret.getTerminalAlphabet( ); + const ext::set < TerminalSymbolType > & terminals = ret.getTerminalAlphabet( ); - auto testCallback = [ & ] ( const ext::variant < SymbolType > & symbol ) { - return ( symbol.template is < SymbolType > ( ) && Nt.count ( symbol.template get < SymbolType > ( ) ) ) - || ( symbol.template is < SymbolType > ( ) && terminals.count ( symbol.template get < SymbolType > ( ) ) ); + auto testCallback = [ & ] ( const ext::variant < TerminalSymbolType, NonterminalSymbolType > & symbol ) { + return ( symbol.template is < NonterminalSymbolType > ( ) && Nt.count ( symbol.template get < NonterminalSymbolType > ( ) ) ) + || ( symbol.template is < TerminalSymbolType > ( ) && terminals.count ( symbol.template get < TerminalSymbolType > ( ) ) ); }; for( const auto & rule : rawRules ) { diff --git a/alib2algo/src/grammar/simplify/UnreachableSymbolsRemover.h b/alib2algo/src/grammar/simplify/UnreachableSymbolsRemover.h index aebfb246cd..f82a61244b 100644 --- a/alib2algo/src/grammar/simplify/UnreachableSymbolsRemover.h +++ b/alib2algo/src/grammar/simplify/UnreachableSymbolsRemover.h @@ -38,30 +38,30 @@ public: /* * Removes unreachable symbols - Melichar 3.9 */ - template < class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > + template < class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > > static T remove( const T & grammar ); }; -template < class T, class SymbolType > +template < class T, class TerminalSymbolType, class NonterminalSymbolType > T UnreachableSymbolsRemover::remove( const T & grammar) { // 1. - ext::set < ext::variant < SymbolType > > Vt = grammar::properties::UnreachableSymbols::getUnreachableSymbols( grammar ); + ext::set < ext::variant < TerminalSymbolType, NonterminalSymbolType > > Vt = grammar::properties::UnreachableSymbols::getUnreachableSymbols( grammar ); - T ret(grammar.getInitialSymbol( ) ); + T ret ( grammar.getInitialSymbol ( ) ); - ext::set < ext::variant < SymbolType > > newNonTerminals, newTerminals; + ext::set < ext::variant < TerminalSymbolType, NonterminalSymbolType > > newNonTerminals, newTerminals; set_intersection( Vt.begin( ), Vt.end( ), grammar.getNonterminalAlphabet( ).begin( ), grammar.getNonterminalAlphabet( ).end( ), std::inserter( newNonTerminals, newNonTerminals.begin( ) ) ); for( const auto & symbol : newNonTerminals ) - ret.addNonterminalSymbol( symbol.template get < SymbolType > ( ) ); + ret.addNonterminalSymbol( symbol.template get < NonterminalSymbolType > ( ) ); set_intersection( Vt.begin( ), Vt.end( ), grammar.getTerminalAlphabet( ).begin( ), grammar.getTerminalAlphabet( ).end( ), std::inserter( newTerminals, newTerminals.begin( ) ) ); for( const auto & symbol : newTerminals ) - ret.addTerminalSymbol( symbol.template get < SymbolType > ( ) ); + ret.addTerminalSymbol( symbol.template get < TerminalSymbolType > ( ) ); auto rawRules = grammar::RawRules::getRawRules ( grammar ); - auto testCallback = [ & ] ( const ext::variant < SymbolType > & symb ) -> bool { + auto testCallback = [ & ] ( const ext::variant < TerminalSymbolType, NonterminalSymbolType > & symb ) -> bool { return Vt.count( symb ); }; diff --git a/alib2algo_experimental/src/grammar/parsing/CornerSubstitution.cpp b/alib2algo_experimental/src/grammar/parsing/CornerSubstitution.cpp index 7bbe1a5ddd..19c550037c 100644 --- a/alib2algo_experimental/src/grammar/parsing/CornerSubstitution.cpp +++ b/alib2algo_experimental/src/grammar/parsing/CornerSubstitution.cpp @@ -26,7 +26,7 @@ void CornerSubstitution::cornerSubstitution ( grammar::CFG < > & grammar, const const DefaultSymbolType & lhs = rule.first; for ( const ext::vector < DefaultSymbolType > & rhs : rule.second ) { - ext::vector < ext::variant < DefaultSymbolType > > rawRhs ( rhs.begin ( ), rhs.end ( ) ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rawRhs ( rhs.begin ( ), rhs.end ( ) ); if ( ( lhs == nonterminal ) && ( rhs.size ( ) > 0 ) && ( grammar.getNonterminalAlphabet ( ).count ( rhs[0] ) ) && First::first ( grammar, rawRhs ).count ( ext::variant < DefaultSymbolType, DefaultEpsilonType > ( terminal ) ) ) Substitute::substitute ( grammar, res, lhs, rhs, rhs.begin ( ) ); else diff --git a/alib2algo_experimental/src/grammar/parsing/DeterministicLL1Grammar.cpp b/alib2algo_experimental/src/grammar/parsing/DeterministicLL1Grammar.cpp index 1f823db964..dfb44b6647 100644 --- a/alib2algo_experimental/src/grammar/parsing/DeterministicLL1Grammar.cpp +++ b/alib2algo_experimental/src/grammar/parsing/DeterministicLL1Grammar.cpp @@ -31,23 +31,23 @@ grammar::CFG < > DeterministicLL1Grammar::convert ( const grammar::CFG < > & par grammar::CFG < > grammar = param; while ( true ) { - ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > > parseTable = LL1ParseTable::parseTable ( grammar ); + ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > > parseTable = LL1ParseTable::parseTable ( grammar ); bool deterministic = true; - for ( const std::pair < const ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > > & elem : parseTable ) + for ( const std::pair < const ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > > & elem : parseTable ) if ( elem.second.size ( ) > 1 ) if ( elem.first.first.is < string::Epsilon < > > ( ) ) throw exception::CommonException ( "Cant handle conflict in epsilon" ); - for ( const std::pair < const ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > > & elem : parseTable ) { + for ( const std::pair < const ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > > & elem : parseTable ) { if ( elem.first.first.is < string::Epsilon < > > ( ) ) continue; const DefaultSymbolType & terminal = elem.first.first.get < DefaultSymbolType > ( ); const DefaultSymbolType & nonterminal = elem.first.second; - const ext::set < ext::vector < ext::variant < DefaultSymbolType > > > & rhsds = elem.second; + const ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > & rhsds = elem.second; - if ( ( elem.second.size ( ) > 1 ) && std::all_of ( rhsds.begin ( ), rhsds.end ( ), [] ( const ext::vector < ext::variant < DefaultSymbolType > > & rhs ) { + if ( ( elem.second.size ( ) > 1 ) && std::all_of ( rhsds.begin ( ), rhsds.end ( ), [] ( const ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > & rhs ) { return rhs.size ( ) > 0; } ) ) { HandleFirstFirstConflict::handleFirstFirstConflict ( grammar, terminal, nonterminal, rhsds ); @@ -58,14 +58,14 @@ grammar::CFG < > DeterministicLL1Grammar::convert ( const grammar::CFG < > & par if ( !deterministic ) continue; - for ( const std::pair < const ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > > & elem : parseTable ) { + for ( const std::pair < const ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > > & elem : parseTable ) { if ( elem.first.first.is < string::Epsilon < > > ( ) ) continue; const DefaultSymbolType & terminal = elem.first.first.get < DefaultSymbolType > ( ); const DefaultSymbolType & nonterminal = elem.first.second; - const ext::set < ext::vector < ext::variant < DefaultSymbolType > > > & rhsds = elem.second; + const ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > & rhsds = elem.second; - if ( ( elem.second.size ( ) > 1 ) && std::any_of ( rhsds.begin ( ), rhsds.end ( ), [] ( const ext::vector < ext::variant < DefaultSymbolType > > & rhs ) { + if ( ( elem.second.size ( ) > 1 ) && std::any_of ( rhsds.begin ( ), rhsds.end ( ), [] ( const ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > & rhs ) { return rhs.size ( ) == 0; } ) ) { HandleFirstFollowConflict::handleFirstFollowConflict ( grammar, terminal, nonterminal, rhsds ); diff --git a/alib2algo_experimental/src/grammar/parsing/ExtractRightContext.cpp b/alib2algo_experimental/src/grammar/parsing/ExtractRightContext.cpp index 5a296bb34a..43fcdb9048 100644 --- a/alib2algo_experimental/src/grammar/parsing/ExtractRightContext.cpp +++ b/alib2algo_experimental/src/grammar/parsing/ExtractRightContext.cpp @@ -28,7 +28,7 @@ void ExtractRightContext::extractRightContext ( grammar::CFG < > & grammar, cons bool substitued = false; if(rhs.size() > 0) for ( ext::vector < DefaultSymbolType >::const_iterator iter = rhs.begin ( ); iter + 1 != rhs.end ( ); ++iter ) - if ( nonterminals.count ( * iter ) && grammar.getNonterminalAlphabet ( ).count ( * ( iter + 1 ) ) && First::first ( grammar, ext::vector < ext::variant < DefaultSymbolType > > ( iter + 1, rhs.end ( ) ) ).count ( ext::variant < DefaultSymbolType, DefaultEpsilonType > ( terminal ) ) ) { + if ( nonterminals.count ( * iter ) && grammar.getNonterminalAlphabet ( ).count ( * ( iter + 1 ) ) && First::first ( grammar, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > ( iter + 1, rhs.end ( ) ) ).count ( ext::variant < DefaultSymbolType, DefaultEpsilonType > ( terminal ) ) ) { Substitute::substitute ( grammar, res, lhs, rhs, iter + 1 ); substitued = true; break; diff --git a/alib2algo_experimental/src/grammar/parsing/First.cpp b/alib2algo_experimental/src/grammar/parsing/First.cpp index 53e7335856..67da8ec0c1 100644 --- a/alib2algo_experimental/src/grammar/parsing/First.cpp +++ b/alib2algo_experimental/src/grammar/parsing/First.cpp @@ -23,25 +23,25 @@ namespace grammar { namespace parsing { -auto FirstCFG = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::CFG < > & > ( First::first ); -auto FirstEpsilonFreeCFG = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::EpsilonFreeCFG < > & > ( First::first ); -auto FirstGNF = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::GNF < > & > ( First::first ); -auto FirstCNF = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::CNF < > & > ( First::first ); -auto FirstLG = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::LG < > & > ( First::first ); -auto FirstLeftLG = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::LeftLG < > & > ( First::first ); -auto FirstLeftRG = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::LeftRG < > & > ( First::first ); -auto FirstRightLG = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::RightLG < > & > ( First::first ); -auto FirstRightRG = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::RightRG < > & > ( First::first ); - -auto FirstCFG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::CFG < > &, const ext::vector < ext::variant < DefaultSymbolType > > & > ( First::first ); -auto FirstEpsilonFreeCFG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::EpsilonFreeCFG < > &, const ext::vector < ext::variant < DefaultSymbolType > > & > ( First::first ); -auto FirstGNF2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::GNF < > &, const ext::vector < ext::variant < DefaultSymbolType > > & > ( First::first ); -auto FirstCNF2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::CNF < > &, const ext::vector < ext::variant < DefaultSymbolType > > & > ( First::first ); -auto FirstLG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::LG < > &, const ext::vector < ext::variant < DefaultSymbolType > > & > ( First::first ); -auto FirstLeftLG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::LeftLG < > &, const ext::vector < ext::variant < DefaultSymbolType > > & > ( First::first ); -auto FirstLeftRG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::LeftRG < > &, const ext::vector < ext::variant < DefaultSymbolType > > & > ( First::first ); -auto FirstRightLG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::RightLG < > &, const ext::vector < ext::variant < DefaultSymbolType > > & > ( First::first ); -auto FirstRightRG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::RightRG < > &, const ext::vector < ext::variant < DefaultSymbolType > > & > ( First::first ); +auto FirstCFG = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::CFG < > & > ( First::first ); +auto FirstEpsilonFreeCFG = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::EpsilonFreeCFG < > & > ( First::first ); +auto FirstGNF = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::GNF < > & > ( First::first ); +auto FirstCNF = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::CNF < > & > ( First::first ); +auto FirstLG = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::LG < > & > ( First::first ); +auto FirstLeftLG = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::LeftLG < > & > ( First::first ); +auto FirstLeftRG = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::LeftRG < > & > ( First::first ); +auto FirstRightLG = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::RightLG < > & > ( First::first ); +auto FirstRightRG = registration::AbstractRegister < First, ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::RightRG < > & > ( First::first ); + +auto FirstCFG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::CFG < > &, const ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > & > ( First::first ); +auto FirstEpsilonFreeCFG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::EpsilonFreeCFG < > &, const ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > & > ( First::first ); +auto FirstGNF2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::GNF < > &, const ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > & > ( First::first ); +auto FirstCNF2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::CNF < > &, const ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > & > ( First::first ); +auto FirstLG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::LG < > &, const ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > & > ( First::first ); +auto FirstLeftLG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::LeftLG < > &, const ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > & > ( First::first ); +auto FirstLeftRG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::LeftRG < > &, const ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > & > ( First::first ); +auto FirstRightLG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::RightLG < > &, const ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > & > ( First::first ); +auto FirstRightRG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::RightRG < > &, const ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > & > ( First::first ); } /* namespace parsing */ diff --git a/alib2algo_experimental/src/grammar/parsing/First.h b/alib2algo_experimental/src/grammar/parsing/First.h index e42565eff9..a81cb197a3 100644 --- a/alib2algo_experimental/src/grammar/parsing/First.h +++ b/alib2algo_experimental/src/grammar/parsing/First.h @@ -21,43 +21,43 @@ namespace grammar { namespace parsing { class First { - template < class SymbolType > - static ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > first ( const ext::set < SymbolType > & terminals, const ext::set < SymbolType > & nonterminals, const ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > & firstOfNonterminal, const ext::vector < ext::variant < SymbolType > > & rhs ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > first ( const ext::set < TerminalSymbolType > & terminals, const ext::set < NonterminalSymbolType > & nonterminals, const ext::map < NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > & firstOfNonterminal, const ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs ); - template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > first ( const ext::set < SymbolType > & terminals, const ext::set < SymbolType > & nonterminals, const ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > & rules ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static ext::map < NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > first ( const ext::set < TerminalSymbolType > & terminals, const ext::set < NonterminalSymbolType > & nonterminals, const ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > & rules ); public: - template < class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > - static ext::map < ext::vector < ext::variant < SymbolType > >, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > first ( const T & grammar ); + template < class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > > + static ext::map < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > >, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > first ( const T & grammar ); - template < class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > - static ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > first ( const T & grammar, const ext::vector < ext::variant < SymbolType > > & rhs ); + template < class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > > + static ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > first ( const T & grammar, const ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs ); }; -template < class SymbolType > -ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > First::first ( const ext::set < SymbolType > & terminals, const ext::set < SymbolType > & nonterminals, const ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > & firstOfNonterminal, const ext::vector < ext::variant < SymbolType > > & rhs ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > First::first ( const ext::set < TerminalSymbolType > & terminals, const ext::set < NonterminalSymbolType > & nonterminals, const ext::map < NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > & firstOfNonterminal, const ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs ) { // 1. FIRST(\varepsilon) = { \varepsilon } if ( rhs.size ( ) == 0 ) { - return { string::Epsilon < SymbolType >::EPSILON }; + return { string::Epsilon < TerminalSymbolType >::EPSILON }; } // 2. FIRST(a) = { a } forall a \in T else if ( terminals.count ( rhs[0] ) ) { - return { rhs[0].template get < SymbolType > ( ) }; + return { rhs[0].template get < TerminalSymbolType > ( ) }; } // 4. FIRST(A \alpha) = first(A) if A \in N and \varepsilon \notin first(A) - else if ( nonterminals.count ( rhs[0] ) && !firstOfNonterminal.find ( rhs[0] )->second.count ( ext::variant < SymbolType, string::Epsilon < SymbolType > >::template from < string::Epsilon < SymbolType > > ( ) ) ) { + else if ( nonterminals.count ( rhs[0] ) && !firstOfNonterminal.find ( rhs[0] )->second.count ( ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > >::template from < string::Epsilon < TerminalSymbolType > > ( ) ) ) { return firstOfNonterminal.find ( rhs[0] )->second; } // 5. FIRST(A \alpha) = (first(A) - \varepsilon) \cup FIRST(\alpha) if A \in N and \varepsilon \in first(A) - else if ( nonterminals.count ( rhs[0] ) && firstOfNonterminal.find ( rhs[0] )->second.count ( ext::variant < SymbolType, string::Epsilon < SymbolType > >::template from < string::Epsilon < SymbolType > > ( ) ) ) { - ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > res = firstOfNonterminal.find ( rhs[0] )->second; - res.erase ( string::Epsilon < SymbolType >::EPSILON ); + else if ( nonterminals.count ( rhs[0] ) && firstOfNonterminal.find ( rhs[0] )->second.count ( ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > >::template from < string::Epsilon < TerminalSymbolType > > ( ) ) ) { + ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > res = firstOfNonterminal.find ( rhs[0] )->second; + res.erase ( string::Epsilon < TerminalSymbolType >::EPSILON ); - ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > next = first ( terminals, nonterminals, firstOfNonterminal, ext::vector < ext::variant < SymbolType > > ( rhs.begin ( ) + 1, rhs.end ( ) ) ); + ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > next = first ( terminals, nonterminals, firstOfNonterminal, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > ( rhs.begin ( ) + 1, rhs.end ( ) ) ); res.insert ( next.begin ( ), next.end ( ) ); return res; } else { @@ -65,8 +65,8 @@ ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > First:: } } -template < class SymbolType > -ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > First::first ( const ext::set < SymbolType > & terminals, const ext::set < SymbolType > & nonterminals, const ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > & rules ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +ext::map < NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > First::first ( const ext::set < TerminalSymbolType > & terminals, const ext::set < NonterminalSymbolType > & nonterminals, const ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > & rules ) { /* * * 1. foreach A \in N: first(A) = \emptyset @@ -75,17 +75,17 @@ ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < S * 3. repeat step 2 if at least one set first(A) has changed * */ - ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > firstOfNonterminal1; + ext::map < NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > firstOfNonterminal1; - for ( const SymbolType & nonterminal : nonterminals ) + for ( const NonterminalSymbolType & nonterminal : nonterminals ) firstOfNonterminal1[nonterminal]; - ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > firstOfNonterminal2 = firstOfNonterminal1; + ext::map < NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > firstOfNonterminal2 = firstOfNonterminal1; do { - for ( const std::pair < const SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > & rule : rules ) - for ( const ext::vector < ext::variant < SymbolType > > & rhs : rule.second ) { - ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > newFirst = first ( terminals, nonterminals, firstOfNonterminal1, rhs ); + for ( const std::pair < const NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > & rule : rules ) + for ( const ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs : rule.second ) { + ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > newFirst = first ( terminals, nonterminals, firstOfNonterminal1, rhs ); firstOfNonterminal2[rule.first].insert ( newFirst.begin ( ), newFirst.end ( ) ); } @@ -100,26 +100,26 @@ ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < S return firstOfNonterminal1; } -template < class T, class SymbolType > -ext::map < ext::vector < ext::variant < SymbolType > >, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > First::first ( const T & grammar ) { +template < class T, class TerminalSymbolType, class NonterminalSymbolType > +ext::map < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > >, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > First::first ( const T & grammar ) { auto rawRules = grammar::RawRules::getRawRules ( grammar ); - ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > firstNt = first ( grammar.getTerminalAlphabet ( ), grammar.getNonterminalAlphabet ( ), rawRules ); + ext::map < NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > firstNt = first ( grammar.getTerminalAlphabet ( ), grammar.getNonterminalAlphabet ( ), rawRules ); - ext::map < ext::vector < ext::variant < SymbolType > >, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > res; + ext::map < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > >, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > res; - for ( const std::pair < const SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > & rule : rawRules ) - for ( const ext::vector < ext::variant < SymbolType > > & rhs : rule.second ) + for ( const std::pair < const NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > & rule : rawRules ) + for ( const ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs : rule.second ) res.insert ( std::make_pair ( rhs, first ( grammar.getTerminalAlphabet ( ), grammar.getNonterminalAlphabet ( ), firstNt, rhs ) ) ); return res; } -template < class T, class SymbolType > -ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > First::first ( const T & grammar, const ext::vector < ext::variant < SymbolType > > & rhs ) { +template < class T, class TerminalSymbolType, class NonterminalSymbolType > +ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > First::first ( const T & grammar, const ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs ) { auto rawRules = grammar::RawRules::getRawRules ( grammar ); - ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > firstNt = first ( grammar.getTerminalAlphabet ( ), grammar.getNonterminalAlphabet ( ), rawRules ); + ext::map < NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > firstNt = first ( grammar.getTerminalAlphabet ( ), grammar.getNonterminalAlphabet ( ), rawRules ); return first ( grammar.getTerminalAlphabet ( ), grammar.getNonterminalAlphabet ( ), firstNt, rhs ); } diff --git a/alib2algo_experimental/src/grammar/parsing/Follow.h b/alib2algo_experimental/src/grammar/parsing/Follow.h index aa9693c2b6..42396a77f7 100644 --- a/alib2algo_experimental/src/grammar/parsing/Follow.h +++ b/alib2algo_experimental/src/grammar/parsing/Follow.h @@ -26,46 +26,46 @@ namespace grammar { namespace parsing { class Follow { - template < class T, class SymbolType > - static void follow ( const T & grammar, ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > & followSet ); + template < class T, class TerminalSymbolType, class NonterminalSymbolType > + static void follow ( const T & grammar, ext::map < NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > & followSet ); public: - template < class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > - static ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > follow ( const T & grammar ); + template < class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > > + static ext::map < NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > follow ( const T & grammar ); - template < class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > - static ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > follow ( const T & grammar, const SymbolType & nt ); + template < class T, class NonterminalSymbolType, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T > > + static ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > follow ( const T & grammar, const NonterminalSymbolType & nt ); }; -template < class T, class SymbolType > -void Follow::follow ( const T & grammar, ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > & followSet ) { +template < class T, class TerminalSymbolType, class NonterminalSymbolType > +void Follow::follow ( const T & grammar, ext::map < NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > & followSet ) { auto rawRules = grammar::RawRules::getRawRules ( grammar ); - for ( const std::pair < const SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > & rule : rawRules ) { - const SymbolType & X = rule.first; + for ( const std::pair < const NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > & rule : rawRules ) { + const NonterminalSymbolType & X = rule.first; - for ( const ext::vector < ext::variant < SymbolType > > & rhs : rule.second ) + for ( const ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs : rule.second ) // every nt in rhs is Y - for ( typename ext::vector < ext::variant < SymbolType > >::const_iterator it = rhs.begin ( ); it != rhs.end ( ); it++ ) { - const ext::variant < SymbolType > & Y = * it; + for ( typename ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > >::const_iterator it = rhs.begin ( ); it != rhs.end ( ); it++ ) { + const ext::variant < TerminalSymbolType, NonterminalSymbolType > & Y = * it; if ( ! grammar.getNonterminalAlphabet ( ).count ( Y ) ) continue; - ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > firstBeta = First::first ( grammar, ext::vector < ext::variant < SymbolType > > ( std::next ( it ), rhs.end ( ) ) ); + ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > firstBeta = First::first ( grammar, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > ( std::next ( it ), rhs.end ( ) ) ); - if ( firstBeta.count ( ext::variant < SymbolType, string::Epsilon < SymbolType > >::template from < string::Epsilon < SymbolType > > ( ) ) ) { - firstBeta.erase ( ext::variant < SymbolType, string::Epsilon < SymbolType > >::template from < string::Epsilon < SymbolType > > ( ) ); - followSet [ Y.template get < SymbolType > ( ) ].insert ( followSet[X].begin ( ), followSet[X].end ( ) ); + if ( firstBeta.count ( ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > >::template from < string::Epsilon < TerminalSymbolType > > ( ) ) ) { + firstBeta.erase ( ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > >::template from < string::Epsilon < TerminalSymbolType > > ( ) ); + followSet [ Y.template get < TerminalSymbolType > ( ) ].insert ( followSet[X].begin ( ), followSet[X].end ( ) ); } - followSet [ Y.template get < SymbolType > ( ) ].insert ( firstBeta.begin ( ), firstBeta.end ( ) ); + followSet [ Y.template get < TerminalSymbolType > ( ) ].insert ( firstBeta.begin ( ), firstBeta.end ( ) ); } } } -template < class T, class SymbolType > -ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > Follow::follow ( const T & grammar ) { +template < class T, class TerminalSymbolType, class NonterminalSymbolType > +ext::map < NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > Follow::follow ( const T & grammar ) { /* * 1. Follow(S) = { \varepsilon } * Follow(A) = {} forall A \in N, A \neq S @@ -77,14 +77,14 @@ ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < S * 3. goto 2 if any follow set was changed in prev step. */ - ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > followSet1; + ext::map < NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > followSet1; - for ( const SymbolType & symb : grammar.getNonterminalAlphabet ( ) ) + for ( const NonterminalSymbolType & symb : grammar.getNonterminalAlphabet ( ) ) followSet1[symb]; - followSet1[grammar.getInitialSymbol ( )] = { ext::variant < SymbolType, string::Epsilon < SymbolType > >::template from < string::Epsilon < SymbolType > > ( ) }; + followSet1[grammar.getInitialSymbol ( )] = { ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > >::template from < string::Epsilon < TerminalSymbolType > > ( ) }; - ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > followSet2 = followSet1; + ext::map < NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > followSet2 = followSet1; do { follow ( grammar, followSet2 ); @@ -98,9 +98,9 @@ ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < S return followSet1; } -template < class T, class SymbolType > -ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > Follow::follow ( const T & grammar, const SymbolType & nt ) { - if ( !grammar.getNonterminalAlphabet ( ).count ( nt ) ) +template < class T, class NonterminalSymbolType, class TerminalSymbolType > +ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > Follow::follow ( const T & grammar, const NonterminalSymbolType & nt ) { + if ( ! grammar.getNonterminalAlphabet ( ).count ( nt ) ) throw exception::CommonException ( "Follow: Given symbol is not nonterminal." ); return follow ( grammar )[nt]; diff --git a/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.cpp b/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.cpp index 6ef83ca990..1da410830a 100644 --- a/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.cpp +++ b/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.cpp @@ -17,8 +17,8 @@ namespace grammar { namespace parsing { -void HandleFirstFirstConflict::handleFirstFirstConflict ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal, const ext::set < ext::vector < ext::variant < DefaultSymbolType > > > & rhsds ) { - for ( const ext::vector < ext::variant < DefaultSymbolType > > & rhs : rhsds ) +void HandleFirstFirstConflict::handleFirstFirstConflict ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal, const ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > & rhsds ) { + for ( const ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > & rhs : rhsds ) if ( ( rhs.size ( ) > 0 ) && grammar.getNonterminalAlphabet ( ).count ( rhs[0] ) && First::first ( grammar, rhs ).count ( ext::variant < DefaultSymbolType, DefaultEpsilonType > ( terminal ) ) ) { CornerSubstitution::cornerSubstitution ( grammar, terminal, nonterminal ); return; diff --git a/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.h b/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.h index a58683b0a4..825774bfa0 100644 --- a/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.h +++ b/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.h @@ -18,7 +18,7 @@ namespace parsing { class HandleFirstFirstConflict { public: - static void handleFirstFirstConflict ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal, const ext::set < ext::vector < ext::variant < DefaultSymbolType > > > & rhsds ); + static void handleFirstFirstConflict ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal, const ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > & rhsds ); }; } /* namespace parsing */ diff --git a/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.cpp b/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.cpp index a9593adf0e..58b8116cd1 100644 --- a/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.cpp +++ b/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.cpp @@ -19,7 +19,7 @@ namespace grammar { namespace parsing { -void HandleFirstFollowConflict::handleFirstFollowConflict ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal, const ext::set < ext::vector < ext::variant < DefaultSymbolType > > > & /* rhsds */ ) { +void HandleFirstFollowConflict::handleFirstFollowConflict ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal, const ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > & /* rhsds */ ) { ext::set < DefaultSymbolType > nullableNonterminals = properties::NullableNonterminals::getNullableNonterminals ( grammar ); ext::set < DefaultSymbolType > symbolsEndingWithNonterminal = { nonterminal }; @@ -52,7 +52,7 @@ void HandleFirstFollowConflict::handleFirstFollowConflict ( grammar::CFG < > & g for ( const ext::vector < DefaultSymbolType > & rhs : rule.second ) { if ( rhs.size ( ) > 0 ) for ( ext::vector < DefaultSymbolType >::const_iterator iter = rhs.begin ( ); iter + 1 != rhs.end ( ); ++iter ) - if ( symbolsEndingWithNonterminal.count ( * iter ) && grammar.getNonterminalAlphabet ( ).count ( * ( iter + 1 ) ) && First::first ( grammar, ext::vector < ext::variant < DefaultSymbolType > > ( iter + 1, rhs.end ( ) ) ).count ( ext::variant < DefaultSymbolType, DefaultEpsilonType > ( terminal ) ) ) { + if ( symbolsEndingWithNonterminal.count ( * iter ) && grammar.getNonterminalAlphabet ( ).count ( * ( iter + 1 ) ) && First::first ( grammar, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > ( iter + 1, rhs.end ( ) ) ).count ( ext::variant < DefaultSymbolType, DefaultEpsilonType > ( terminal ) ) ) { ExtractRightContext::extractRightContext ( grammar, terminal, symbolsEndingWithNonterminal ); return; } diff --git a/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.h b/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.h index cefc8625bf..8f315dcd1e 100644 --- a/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.h +++ b/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.h @@ -19,7 +19,7 @@ namespace parsing { class HandleFirstFollowConflict { public: - static void handleFirstFollowConflict ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal, const ext::set < ext::vector < ext::variant < DefaultSymbolType > > > & /* rhsds */ ); + static void handleFirstFollowConflict ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal, const ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > & /* rhsds */ ); }; } /* namespace parsing */ diff --git a/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.cpp b/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.cpp index c895a2dd21..b1834d71ce 100644 --- a/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.cpp +++ b/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.cpp @@ -22,15 +22,15 @@ namespace grammar { namespace parsing { -auto LL1ParseTableCFG = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::CFG < > & > ( LL1ParseTable::parseTable ); -auto LL1ParseTableEpsilonFreeCFG = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::EpsilonFreeCFG < > & > ( LL1ParseTable::parseTable ); -auto LL1ParseTableGNF = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::GNF < > & > ( LL1ParseTable::parseTable ); -auto LL1ParseTableCNF = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::CNF < > & > ( LL1ParseTable::parseTable ); -auto LL1ParseTableLG = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::LG < > & > ( LL1ParseTable::parseTable ); -auto LL1ParseTableLeftLG = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::LeftLG < > & > ( LL1ParseTable::parseTable ); -auto LL1ParseTableLeftRG = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::LeftRG < > & > ( LL1ParseTable::parseTable ); -auto LL1ParseTableRightLG = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::RightLG < > & > ( LL1ParseTable::parseTable ); -auto LL1ParseTableRightRG = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::RightRG < > & > ( LL1ParseTable::parseTable ); +auto LL1ParseTableCFG = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::CFG < > & > ( LL1ParseTable::parseTable ); +auto LL1ParseTableEpsilonFreeCFG = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::EpsilonFreeCFG < > & > ( LL1ParseTable::parseTable ); +auto LL1ParseTableGNF = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::GNF < > & > ( LL1ParseTable::parseTable ); +auto LL1ParseTableCNF = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::CNF < > & > ( LL1ParseTable::parseTable ); +auto LL1ParseTableLG = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::LG < > & > ( LL1ParseTable::parseTable ); +auto LL1ParseTableLeftLG = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::LeftLG < > & > ( LL1ParseTable::parseTable ); +auto LL1ParseTableLeftRG = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::LeftRG < > & > ( LL1ParseTable::parseTable ); +auto LL1ParseTableRightLG = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::RightLG < > & > ( LL1ParseTable::parseTable ); +auto LL1ParseTableRightRG = registration::AbstractRegister < LL1ParseTable, ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::RightRG < > & > ( LL1ParseTable::parseTable ); } /* namespace parsing */ diff --git a/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.h b/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.h index eed9eed649..a9610072c5 100644 --- a/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.h +++ b/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.h @@ -25,32 +25,32 @@ namespace parsing { class LL1ParseTable { public: - template < class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > - static ext::map < ext::pair < ext::variant < SymbolType, string::Epsilon < > >, SymbolType >, ext::set < ext::vector < ext::variant < SymbolType > > > > parseTable ( const T & grammar ); + template < class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > > + static ext::map < ext::pair < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > >, NonterminalSymbolType >, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > parseTable ( const T & grammar ); }; -template < class T, class SymbolType > -ext::map < ext::pair < ext::variant < SymbolType, string::Epsilon < > >, SymbolType >, ext::set < ext::vector < ext::variant < SymbolType > > > > LL1ParseTable::parseTable ( const T & grammar ) { - ext::map < ext::pair < ext::variant < SymbolType, string::Epsilon < > >, SymbolType >, ext::set < ext::vector < ext::variant < SymbolType > > > > res; +template < class T, class TerminalSymbolType, class NonterminalSymbolType > +ext::map < ext::pair < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > >, NonterminalSymbolType >, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > LL1ParseTable::parseTable ( const T & grammar ) { + ext::map < ext::pair < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > >, NonterminalSymbolType >, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > res; - ext::map < ext::vector < ext::variant < SymbolType > >, ext::set < ext::variant < SymbolType, string::Epsilon < > > > > first = First::first ( grammar ); - ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < > > > > follow = Follow::follow ( grammar ); + ext::map < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > >, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > first = First::first ( grammar ); + ext::map < NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > > > follow = Follow::follow ( grammar ); auto rawRules = grammar::RawRules::getRawRules ( grammar ); - for ( const std::pair < const SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > & transition : rawRules ) { - const SymbolType & lhs = transition.first; + for ( const std::pair < const NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > & transition : rawRules ) { + const NonterminalSymbolType & lhs = transition.first; - for ( const ext::vector < ext::variant < SymbolType > > & rhs : transition.second ) { - for ( const ext::variant < SymbolType, string::Epsilon < > > & firstElem : first[rhs] ) { - if ( firstElem.template is < string::Epsilon < > > ( ) ) + for ( const ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs : transition.second ) { + for ( const ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > & firstElem : first[rhs] ) { + if ( firstElem.template is < string::Epsilon < TerminalSymbolType > > ( ) ) continue; res [ ext::make_pair ( firstElem, lhs ) ].insert ( rhs ); } - if ( first[rhs].count ( ext::variant < SymbolType, string::Epsilon < SymbolType > >::template from < string::Epsilon < SymbolType > > ( ) ) ) - for ( const ext::variant < SymbolType, string::Epsilon < > > & followElem : follow[lhs] ) + if ( first[rhs].count ( ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > >::template from < string::Epsilon < TerminalSymbolType > > ( ) ) ) + for ( const ext::variant < TerminalSymbolType, string::Epsilon < TerminalSymbolType > > & followElem : follow[lhs] ) res [ ext::make_pair ( followElem, lhs ) ].insert ( rhs ); } diff --git a/alib2algo_experimental/test-src/grammar/parsing/FirstTest.cpp b/alib2algo_experimental/test-src/grammar/parsing/FirstTest.cpp index 4fb8493bfb..da5d72bbfd 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/FirstTest.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/FirstTest.cpp @@ -46,15 +46,15 @@ void FirstTest::testFirst ( ) { } // -------------------------------------------------- - ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > first; + ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > first; { - ext::vector < ext::variant < DefaultSymbolType > > rhsE1 ( { nE, tP, nT } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsE2 ( { nT } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsT1 ( { nT, tS, nF } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsT2 ( { nF } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsF1 ( { tA } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsF2 ( { tL, nE, tR } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsE1 ( { nE, tP, nT } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsE2 ( { nT } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsT1 ( { nT, tS, nF } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsT2 ( { nF } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsF1 ( { tA } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsF2 ( { tL, nE, tR } ); first[rhsE1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { tA, tL @@ -78,7 +78,7 @@ void FirstTest::testFirst ( ) { // -------------------------------------------------- - ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > firstAlgo; + ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > firstAlgo; for ( const auto & rule : grammar::RawRules::getRawRules ( grammar ) ) for ( const auto & rhs : rule.second ) @@ -139,23 +139,23 @@ void FirstTest::testFirst ( ) { } // -------------------------------------------------- - ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > first; + ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > first; { - ext::vector < ext::variant < DefaultSymbolType > > rhsS1 ( { nB, tD, nS } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsS2 ( { tD, tD, nC } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsS3 ( { tC, nA } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsA1 ( { tA, tE, nE } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsA2 ( { tB, tB, nE } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsB1 ( { tA, nF } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsB2 ( { tB, tB, nD } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsC1 ( { tA, nB, tD } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsC2 ( { tE, nA } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsD1 ( { tC, tA, nF } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsE1 ( { tC, tA, tE, nE } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsE2 ( { } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsF1 ( { tE, nD } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsF2 ( { } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsS1 ( { nB, tD, nS } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsS2 ( { tD, tD, nC } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsS3 ( { tC, nA } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsA1 ( { tA, tE, nE } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsA2 ( { tB, tB, nE } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsB1 ( { tA, nF } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsB2 ( { tB, tB, nD } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsC1 ( { tA, nB, tD } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsC2 ( { tE, nA } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsD1 ( { tC, tA, nF } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsE1 ( { tC, tA, tE, nE } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsE2 ( { } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsF1 ( { tE, nD } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsF2 ( { } ); first[rhsS1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { tA, tB @@ -202,7 +202,7 @@ void FirstTest::testFirst ( ) { } // -------------------------------------------------- - ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > firstAlgo; + ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > firstAlgo; for ( const auto & rule : grammar::RawRules::getRawRules ( grammar ) ) for ( const auto & rhs : rule.second ) @@ -226,7 +226,7 @@ void FirstTest::testFirst2 ( ) { grammar.addRule ( A, ext::vector < DefaultSymbolType > { A, c } ); grammar.addRule ( A, ext::vector < DefaultSymbolType > { d } ); - ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > res = { { { d }, { d } }, { { A, c }, { d } } }; + ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > res = { { { d }, { d } }, { { A, c }, { d } } }; CPPUNIT_ASSERT ( res == grammar::parsing::First::first ( grammar ) ); } @@ -253,7 +253,7 @@ void FirstTest::testFirst3 ( ) { grammar.addRule ( B, ext::vector < DefaultSymbolType > { f, S } ); grammar.addRule ( B, ext::vector < DefaultSymbolType > { } ); - ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > res = + ext::map < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > res = { { { A, a }, { c, f, a } }, { { b, S }, { b } }, { { c, A, d }, { c } }, { { B }, { f, string::Epsilon < >::EPSILON } }, { { f, S }, { f } }, { { }, { string::Epsilon < >::EPSILON } } }; diff --git a/alib2algo_experimental/test-src/grammar/parsing/LL1ParseTable.cpp b/alib2algo_experimental/test-src/grammar/parsing/LL1ParseTable.cpp index 8bd4c8e8e4..17484041bb 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/LL1ParseTable.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/LL1ParseTable.cpp @@ -52,17 +52,17 @@ void LL1ParseTable::testLL1Table ( ) { } // -------------------------------------------------- - ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > > parseTable; + ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > > parseTable; { - ext::vector < ext::variant < DefaultSymbolType > > rhsE1 ( { nT, nEp } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsEp1 ( { tP, nT, nEp } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsEp2 ( { } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsT1 ( { nF, nTp } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsTp1 ( { tS, nF, nTp } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsTp2 ( { } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsF1 ( { tA } ); - ext::vector < ext::variant < DefaultSymbolType > > rhsF2 ( { tL, nE, tR } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsE1 ( { nT, nEp } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsEp1 ( { tP, nT, nEp } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsEp2 ( { } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsT1 ( { nF, nTp } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsTp1 ( { tS, nF, nTp } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsTp2 ( { } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsF1 ( { tA } ); + ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > rhsF2 ( { tL, nE, tR } ); parseTable[ext::make_pair ( tA, nE )].insert ( rhsE1 ); parseTable[ext::make_pair ( tL, nE )].insert ( rhsE1 ); @@ -85,7 +85,7 @@ void LL1ParseTable::testLL1Table ( ) { // -------------------------------------------------- - ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > > parseTableAlgo = grammar::parsing::LL1ParseTable::parseTable ( grammar ); + ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > > parseTableAlgo = grammar::parsing::LL1ParseTable::parseTable ( grammar ); std::cout << parseTable << std::endl; std::cout << parseTableAlgo << std::endl; diff --git a/alib2data/src/grammar/AddRawRule.cpp b/alib2data/src/grammar/AddRawRule.cpp index d252ab35a9..3d84c57761 100644 --- a/alib2data/src/grammar/AddRawRule.cpp +++ b/alib2data/src/grammar/AddRawRule.cpp @@ -10,14 +10,14 @@ namespace grammar { -auto AddRawRuleCFG = registration::AbstractRegister < AddRawRule, bool, grammar::CFG < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType > > > ( AddRawRule::addRawRule ); -auto AddRawRuleEpsilonFreeCFG = registration::AbstractRegister < AddRawRule, bool, grammar::EpsilonFreeCFG < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType > > > ( AddRawRule::addRawRule ); -auto AddRawRuleGNF = registration::AbstractRegister < AddRawRule, bool, grammar::GNF < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType > > > ( AddRawRule::addRawRule ); -auto AddRawRuleCNF = registration::AbstractRegister < AddRawRule, bool, grammar::CNF < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType > > > ( AddRawRule::addRawRule ); -auto AddRawRuleLG = registration::AbstractRegister < AddRawRule, bool, grammar::LG < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType > > > ( AddRawRule::addRawRule ); -auto AddRawRuleLeftLG = registration::AbstractRegister < AddRawRule, bool, grammar::LeftLG < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType > > > ( AddRawRule::addRawRule ); -auto AddRawRuleLeftRG = registration::AbstractRegister < AddRawRule, bool, grammar::LeftRG < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType > > > ( AddRawRule::addRawRule ); -auto AddRawRuleRightLG = registration::AbstractRegister < AddRawRule, bool, grammar::RightLG < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType > > > ( AddRawRule::addRawRule ); -auto AddRawRuleRightRG = registration::AbstractRegister < AddRawRule, bool, grammar::RightRG < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType > > > ( AddRawRule::addRawRule ); +auto AddRawRuleCFG = registration::AbstractRegister < AddRawRule, bool, grammar::CFG < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > ( AddRawRule::addRawRule ); +auto AddRawRuleEpsilonFreeCFG = registration::AbstractRegister < AddRawRule, bool, grammar::EpsilonFreeCFG < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > ( AddRawRule::addRawRule ); +auto AddRawRuleGNF = registration::AbstractRegister < AddRawRule, bool, grammar::GNF < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > ( AddRawRule::addRawRule ); +auto AddRawRuleCNF = registration::AbstractRegister < AddRawRule, bool, grammar::CNF < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > ( AddRawRule::addRawRule ); +auto AddRawRuleLG = registration::AbstractRegister < AddRawRule, bool, grammar::LG < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > ( AddRawRule::addRawRule ); +auto AddRawRuleLeftLG = registration::AbstractRegister < AddRawRule, bool, grammar::LeftLG < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > ( AddRawRule::addRawRule ); +auto AddRawRuleLeftRG = registration::AbstractRegister < AddRawRule, bool, grammar::LeftRG < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > ( AddRawRule::addRawRule ); +auto AddRawRuleRightLG = registration::AbstractRegister < AddRawRule, bool, grammar::RightLG < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > ( AddRawRule::addRawRule ); +auto AddRawRuleRightRG = registration::AbstractRegister < AddRawRule, bool, grammar::RightRG < > &, DefaultSymbolType, ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > ( AddRawRule::addRawRule ); } /* namespace grammar */ diff --git a/alib2data/src/grammar/AddRawRule.h b/alib2data/src/grammar/AddRawRule.h index b6ec343315..bcd574323b 100644 --- a/alib2data/src/grammar/AddRawRule.h +++ b/alib2data/src/grammar/AddRawRule.h @@ -33,92 +33,94 @@ public: * Get rules in most common format of rules as mapping from leftHandSide to rightHandSides represented as vectors of symbols. * * \tparam SymbolType the type of symbols in the grammar + * \tparam TerminalSymbolType the type of terminal symbols in the grammar + * \tparam NonterminalSymbolType the type of nontermnal symbols in the grammar * * \param grammar the source grammar of rules to transform * * \returns rules of the grammar in a common representation */ - template < class SymbolType > - static bool addRawRule ( LG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); + template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > + static bool addRawRule ( LG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ); /** * \override */ - template < class SymbolType > - static bool addRawRule ( GNF < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); + template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > + static bool addRawRule ( GNF < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ); /** * \override */ - template < class SymbolType > - static bool addRawRule ( EpsilonFreeCFG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); + template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > + static bool addRawRule ( EpsilonFreeCFG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ); /** * \override */ - template < class SymbolType > - static bool addRawRule ( CNF < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); + template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > + static bool addRawRule ( CNF < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ); /** * \override */ - template < class SymbolType > - static bool addRawRule ( CFG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); + template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > + static bool addRawRule ( CFG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ); /** * \override */ - template < class SymbolType > - static bool addRawRule ( LeftLG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); + template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > + static bool addRawRule ( LeftLG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ); /** * \override */ - template < class SymbolType > - static bool addRawRule ( LeftRG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); + template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > + static bool addRawRule ( LeftRG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ); /** * \override */ - template < class SymbolType > - static bool addRawRule ( RightLG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); + template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > + static bool addRawRule ( RightLG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ); /** * \override */ - template < class SymbolType > - static bool addRawRule ( RightRG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); + template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > + static bool addRawRule ( RightRG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ); }; -template < class SymbolType > -bool AddRawRule::addRawRule ( LG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { - typename ext::vector < ext::variant < SymbolType > >::iterator nonterminalPosition = rightHandSide.begin ( ); +template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > +bool AddRawRule::addRawRule ( LG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ) { + typename ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > >::iterator nonterminalPosition = rightHandSide.begin ( ); for ( ; nonterminalPosition != rightHandSide.end ( ); ++nonterminalPosition ) - if ( grammar.getNonterminalAlphabet ( ).count ( nonterminalPosition->template get < SymbolType > ( ) ) ) break; + if ( grammar.getNonterminalAlphabet ( ).count ( * nonterminalPosition ) ) break; if ( nonterminalPosition == rightHandSide.end ( ) ) { - ext::vector < SymbolType > rhs; + ext::vector < TerminalSymbolType > rhs; - for ( ext::variant < SymbolType > & symbol : rightHandSide ) - rhs.push_back ( std::move ( symbol.template get < SymbolType > ( ) ) ); + for ( ext::variant < TerminalSymbolType, NonterminalSymbolType > & symbol : rightHandSide ) + rhs.push_back ( std::move ( symbol.template get < TerminalSymbolType > ( ) ) ); return grammar.addRule ( std::move ( leftHandSide ), std::move ( rhs ) ); } else { - ext::vector < SymbolType > rhs1; - ext::vector < SymbolType > rhs2; + ext::vector < TerminalSymbolType > rhs1; + ext::vector < TerminalSymbolType > rhs2; - for ( ext::variant < SymbolType > & symbol : ext::make_iterator_range ( rightHandSide.begin ( ), nonterminalPosition ) ) - rhs1.push_back ( std::move ( symbol.template get < SymbolType > ( ) ) ); - for ( ext::variant < SymbolType > & symbol : ext::make_iterator_range ( std::next ( nonterminalPosition ), rightHandSide.end ( ) ) ) - rhs2.push_back ( std::move ( symbol.template get < SymbolType > ( ) ) ); + for ( ext::variant < TerminalSymbolType, NonterminalSymbolType > & symbol : ext::make_iterator_range ( rightHandSide.begin ( ), nonterminalPosition ) ) + rhs1.push_back ( std::move ( symbol.template get < TerminalSymbolType > ( ) ) ); + for ( ext::variant < TerminalSymbolType, NonterminalSymbolType > & symbol : ext::make_iterator_range ( std::next ( nonterminalPosition ), rightHandSide.end ( ) ) ) + rhs2.push_back ( std::move ( symbol.template get < TerminalSymbolType > ( ) ) ); - return grammar.addRule ( leftHandSide, ext::make_tuple ( std::move ( rhs1 ), std::move ( nonterminalPosition->template get < SymbolType > ( ) ), std::move ( rhs2 ) ) ); + return grammar.addRule ( leftHandSide, ext::make_tuple ( std::move ( rhs1 ), std::move ( nonterminalPosition->template get < NonterminalSymbolType > ( ) ), std::move ( rhs2 ) ) ); } } -template < class SymbolType > -bool AddRawRule::addRawRule ( GNF < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { +template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > +bool AddRawRule::addRawRule ( GNF < SymbolType > & 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" ); @@ -127,18 +129,18 @@ bool AddRawRule::addRawRule ( GNF < SymbolType > & grammar, SymbolType leftHandS grammar.setGeneratesEpsilon ( true ); return !res; } else { - SymbolType first = std::move ( rightHandSide[0].template get < SymbolType > ( ) ); + TerminalSymbolType first = std::move ( rightHandSide[0].template get < TerminalSymbolType > ( ) ); - ext::vector < SymbolType > rest; - for ( ext::variant < SymbolType > & element : ext::make_iterator_range ( rightHandSide.begin ( ) + 1, rightHandSide.end ( ) ) ) - rest.push_back ( std::move ( element.template get < SymbolType > ( ) ) ); + ext::vector < NonterminalSymbolType > rest; + for ( ext::variant < TerminalSymbolType, NonterminalSymbolType > & element : ext::make_iterator_range ( rightHandSide.begin ( ) + 1, rightHandSide.end ( ) ) ) + rest.push_back ( std::move ( element.template get < NonterminalSymbolType > ( ) ) ); return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( first ), std::move ( rest ) ) ); } } -template < class SymbolType > -bool AddRawRule::addRawRule ( EpsilonFreeCFG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { +template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > +bool AddRawRule::addRawRule ( EpsilonFreeCFG < SymbolType > & 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" ); @@ -148,15 +150,15 @@ bool AddRawRule::addRawRule ( EpsilonFreeCFG < SymbolType > & grammar, SymbolTyp return ! res; } else { ext::vector < SymbolType > rhs; - for ( ext::variant < SymbolType > & element : ext::make_iterator_range ( rightHandSide.begin ( ), rightHandSide.end ( ) ) ) + for ( ext::variant < TerminalSymbolType, NonterminalSymbolType > & element : ext::make_iterator_range ( rightHandSide.begin ( ), rightHandSide.end ( ) ) ) rhs.push_back ( std::move ( element.template get < SymbolType > ( ) ) ); return grammar.addRule ( std::move ( leftHandSide ), std::move ( rhs ) ); } } -template < class SymbolType > -bool AddRawRule::addRawRule ( CNF < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { +template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > +bool AddRawRule::addRawRule ( CNF < SymbolType > & 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" ); @@ -165,44 +167,44 @@ bool AddRawRule::addRawRule ( CNF < SymbolType > & grammar, SymbolType leftHandS grammar.setGeneratesEpsilon ( true ); return ! res; } else if ( rightHandSide.size ( ) == 1 ) { - return grammar.addRule ( std::move ( leftHandSide ), std::move ( rightHandSide[0].template get < SymbolType > ( ) ) ); + return grammar.addRule ( std::move ( leftHandSide ), std::move ( rightHandSide [ 0 ].template get < TerminalSymbolType > ( ) ) ); } else if ( rightHandSide.size ( ) == 2 ) { - return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( rightHandSide[0].template get < SymbolType > ( ) ), std::move ( rightHandSide[1].template get < SymbolType > ( ) ) ) ); + return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( rightHandSide [ 0 ].template get < NonterminalSymbolType > ( ) ), std::move ( rightHandSide [ 1 ].template get < NonterminalSymbolType > ( ) ) ) ); } else { throw GrammarException ( "Invalid right hand side" ); } } -template < class SymbolType > -bool AddRawRule::addRawRule ( CFG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { +template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > +bool AddRawRule::addRawRule ( CFG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ) { ext::vector < SymbolType > rhs; - for ( ext::variant < SymbolType > & element : ext::make_iterator_range ( rightHandSide.begin ( ), rightHandSide.end ( ) ) ) + for ( ext::variant < TerminalSymbolType, NonterminalSymbolType > & element : ext::make_iterator_range ( rightHandSide.begin ( ), rightHandSide.end ( ) ) ) rhs.push_back ( std::move ( element.template get < SymbolType > ( ) ) ); return grammar.addRule ( std::move ( leftHandSide ), std::move ( rhs ) ); } -template < class SymbolType > -bool AddRawRule::addRawRule ( LeftLG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { +template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > +bool AddRawRule::addRawRule ( LeftLG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ) { if ( rightHandSide.size ( ) == 0 ) - return grammar.addRule ( std::move ( leftHandSide ), ext::vector < SymbolType > { } ); - else if ( grammar.getNonterminalAlphabet ( ).count ( rightHandSide[0].template get < SymbolType > ( ) ) ) { - ext::vector < SymbolType > rhs; - for ( ext::variant < SymbolType > & element : ext::make_iterator_range ( rightHandSide.begin ( ) + 1, rightHandSide.end ( ) ) ) - rhs.push_back ( std::move ( element.template get < SymbolType > ( ) ) ); + return grammar.addRule ( std::move ( leftHandSide ), ext::vector < TerminalSymbolType > { } ); + else if ( grammar.getNonterminalAlphabet ( ).count ( rightHandSide [ 0 ] ) ) { + ext::vector < TerminalSymbolType > rhs; + for ( ext::variant < TerminalSymbolType, NonterminalSymbolType > & element : ext::make_iterator_range ( rightHandSide.begin ( ) + 1, rightHandSide.end ( ) ) ) + rhs.push_back ( std::move ( element.template get < TerminalSymbolType > ( ) ) ); - return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( rightHandSide[0].template get < SymbolType > ( ) ), std::move ( rhs ) ) ); + return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( rightHandSide [ 0 ].template get < NonterminalSymbolType > ( ) ), std::move ( rhs ) ) ); } else { - ext::vector < SymbolType > rhs; - for ( ext::variant < SymbolType > & element : ext::make_iterator_range ( rightHandSide.begin ( ), rightHandSide.end ( ) ) ) - rhs.push_back ( std::move ( element.template get < SymbolType > ( ) ) ); + ext::vector < TerminalSymbolType > rhs; + for ( ext::variant < TerminalSymbolType, NonterminalSymbolType > & element : ext::make_iterator_range ( rightHandSide.begin ( ), rightHandSide.end ( ) ) ) + rhs.push_back ( std::move ( element.template get < TerminalSymbolType > ( ) ) ); return grammar.addRule ( std::move ( leftHandSide ), std::move ( rhs ) ); } } -template < class SymbolType > -bool AddRawRule::addRawRule ( LeftRG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { +template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > +bool AddRawRule::addRawRule ( LeftRG < SymbolType > & 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" ); @@ -211,16 +213,16 @@ bool AddRawRule::addRawRule ( LeftRG < SymbolType > & grammar, SymbolType leftHa grammar.setGeneratesEpsilon ( true ); return res; } else if ( rightHandSide.size ( ) == 1 ) { - return grammar.addRule ( std::move ( leftHandSide ), std::move ( rightHandSide[0].template get < SymbolType > ( ) ) ); + return grammar.addRule ( std::move ( leftHandSide ), std::move ( rightHandSide [ 0 ].template get < TerminalSymbolType > ( ) ) ); } else if ( rightHandSide.size ( ) == 2 ) { - return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( rightHandSide[0].template get < SymbolType > ( ) ), std::move ( rightHandSide[1].template get < SymbolType > ( ) ) ) ); + return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( rightHandSide [ 0 ].template get < NonterminalSymbolType > ( ) ), std::move ( rightHandSide [ 1 ].template get < TerminalSymbolType > ( ) ) ) ); } else { throw GrammarException ( "Invalid right hand side" ); } } -template < class SymbolType > -bool AddRawRule::addRawRule ( RightRG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { +template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > +bool AddRawRule::addRawRule ( RightRG < SymbolType > & 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" ); @@ -229,28 +231,28 @@ bool AddRawRule::addRawRule ( RightRG < SymbolType > & grammar, SymbolType leftH grammar.setGeneratesEpsilon ( true ); return res; } else if ( rightHandSide.size ( ) == 1 ) { - return grammar.addRule ( std::move ( leftHandSide ), std::move ( rightHandSide[0].template get < SymbolType > ( ) ) ); + return grammar.addRule ( std::move ( leftHandSide ), std::move ( rightHandSide [ 0 ].template get < TerminalSymbolType > ( ) ) ); } else if ( rightHandSide.size ( ) == 2 ) { - return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( rightHandSide[0].template get < SymbolType > ( ) ), std::move ( rightHandSide[1].template get < SymbolType > ( ) ) ) ); + return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( rightHandSide [ 0 ].template get < TerminalSymbolType > ( ) ), std::move ( rightHandSide [ 1 ].template get < NonterminalSymbolType > ( ) ) ) ); } else { throw GrammarException ( "Invalid right hand side" ); } } -template < class SymbolType > -bool AddRawRule::addRawRule ( RightLG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { +template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > +bool AddRawRule::addRawRule ( RightLG < SymbolType > & grammar, NonterminalSymbolType leftHandSide, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rightHandSide ) { if ( rightHandSide.size ( ) == 0 ) - return grammar.addRule ( std::move ( leftHandSide ), ext::vector < SymbolType > { } ); - else if ( grammar.getNonterminalAlphabet ( ).count ( rightHandSide[rightHandSide.size ( ) - 1].template get < SymbolType > ( ) ) ) { - ext::vector < SymbolType > rhs; - for ( ext::variant < SymbolType > & element : ext::make_iterator_range ( rightHandSide.begin ( ), rightHandSide.end ( ) - 1 ) ) - rhs.push_back ( std::move ( element.template get < SymbolType > ( ) ) ); + return grammar.addRule ( std::move ( leftHandSide ), ext::vector < TerminalSymbolType > { } ); + else if ( grammar.getNonterminalAlphabet ( ).count ( rightHandSide [ rightHandSide.size ( ) - 1 ] ) ) { + ext::vector < TerminalSymbolType > rhs; + for ( ext::variant < TerminalSymbolType, NonterminalSymbolType > & element : ext::make_iterator_range ( rightHandSide.begin ( ), rightHandSide.end ( ) - 1 ) ) + rhs.push_back ( std::move ( element.template get < TerminalSymbolType > ( ) ) ); - return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( rhs ), std::move ( rightHandSide[rightHandSide.size ( ) - 1].template get < SymbolType > ( ) ) ) ); + return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( rhs ), std::move ( rightHandSide [ rightHandSide.size ( ) - 1 ].template get < NonterminalSymbolType > ( ) ) ) ); } else { - ext::vector < SymbolType > rhs; - for ( ext::variant < SymbolType > & element : ext::make_iterator_range ( rightHandSide.begin ( ), rightHandSide.end ( ) ) ) - rhs.push_back ( std::move ( element.template get < SymbolType > ( ) ) ); + ext::vector < TerminalSymbolType > rhs; + for ( ext::variant < TerminalSymbolType, NonterminalSymbolType > & element : ext::make_iterator_range ( rightHandSide.begin ( ), rightHandSide.end ( ) ) ) + rhs.push_back ( std::move ( element.template get < TerminalSymbolType > ( ) ) ); return grammar.addRule ( std::move ( leftHandSide ), std::move ( rhs ) ); } diff --git a/alib2data/src/grammar/Grammar.h b/alib2data/src/grammar/Grammar.h index 621205775e..df680b48f2 100644 --- a/alib2data/src/grammar/Grammar.h +++ b/alib2data/src/grammar/Grammar.h @@ -24,7 +24,10 @@ class Grammar : public base::WrapperBase < GrammarBase > { }; template < class T > -using SymbolTypeOfGrammar = typename std::decay < decltype (std::declval<T>().getNonterminalAlphabet()) >::type::value_type; +using TerminalSymbolTypeOfGrammar = typename std::decay < decltype (std::declval<T>().getTerminalAlphabet()) >::type::value_type; + +template < class T > +using NonterminalSymbolTypeOfGrammar = typename std::decay < decltype (std::declval<T>().getNonterminalAlphabet()) >::type::value_type; } /* namespace grammar */ diff --git a/alib2data/src/grammar/RawRules.cpp b/alib2data/src/grammar/RawRules.cpp index 07f8929e01..bfeec6689f 100644 --- a/alib2data/src/grammar/RawRules.cpp +++ b/alib2data/src/grammar/RawRules.cpp @@ -10,14 +10,14 @@ namespace grammar { -auto RawRulesCFG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::CFG < > & > ( RawRules::getRawRules ); -auto RawRulesEpsilonFreeCFG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::EpsilonFreeCFG < > & > ( RawRules::getRawRules ); -auto RawRulesGNF = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::GNF < > & > ( RawRules::getRawRules ); -auto RawRulesCNF = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::CNF < > & > ( RawRules::getRawRules ); -auto RawRulesLG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::LG < > & > ( RawRules::getRawRules ); -auto RawRulesLeftLG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::LeftLG < > & > ( RawRules::getRawRules ); -auto RawRulesLeftRG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::LeftRG < > & > ( RawRules::getRawRules ); -auto RawRulesRightLG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::RightLG < > & > ( RawRules::getRawRules ); -auto RawRulesRightRG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > >, const grammar::RightRG < > & > ( RawRules::getRawRules ); +auto RawRulesCFG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::CFG < > & > ( RawRules::getRawRules ); +auto RawRulesEpsilonFreeCFG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::EpsilonFreeCFG < > & > ( RawRules::getRawRules ); +auto RawRulesGNF = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::GNF < > & > ( RawRules::getRawRules ); +auto RawRulesCNF = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::CNF < > & > ( RawRules::getRawRules ); +auto RawRulesLG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::LG < > & > ( RawRules::getRawRules ); +auto RawRulesLeftLG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::LeftLG < > & > ( RawRules::getRawRules ); +auto RawRulesLeftRG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::LeftRG < > & > ( RawRules::getRawRules ); +auto RawRulesRightLG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::RightLG < > & > ( RawRules::getRawRules ); +auto RawRulesRightRG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < ext::variant < DefaultSymbolType, DefaultSymbolType > > > >, const grammar::RightRG < > & > ( RawRules::getRawRules ); } /* namespace grammar */ diff --git a/alib2data/src/grammar/RawRules.h b/alib2data/src/grammar/RawRules.h index 122775e95d..0c662e3694 100644 --- a/alib2data/src/grammar/RawRules.h +++ b/alib2data/src/grammar/RawRules.h @@ -38,79 +38,79 @@ public: * * \returns rules of the grammar in a common representation */ - template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const LG < SymbolType > & grammar ); + template < class SymbolType, class TerminalSymbolType = SymbolType, class NonterminalSymbolType = SymbolType > + static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const LG < SymbolType > & grammar ); /** * \override */ - template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const GNF < SymbolType > & grammar ); + template < class SymbolType, class TerminalSymbolType = SymbolType, class NonterminalSymbolType = SymbolType > + static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const GNF < SymbolType > & grammar ); /** * \override */ - template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const EpsilonFreeCFG < SymbolType > & grammar ); + template < class SymbolType, class TerminalSymbolType = SymbolType, class NonterminalSymbolType = SymbolType > + static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const EpsilonFreeCFG < SymbolType > & grammar ); /** * \override */ - template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const CNF < SymbolType > & grammar ); + template < class SymbolType, class TerminalSymbolType = SymbolType, class NonterminalSymbolType = SymbolType > + static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const CNF < SymbolType > & grammar ); /** * \override */ - template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const CFG < SymbolType > & grammar ); + template < class SymbolType, class TerminalSymbolType = SymbolType, class NonterminalSymbolType = SymbolType > + static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const CFG < SymbolType > & grammar ); /** * \override */ - template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const LeftLG < SymbolType > & grammar ); + 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 ); /** * \override */ - template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const LeftRG < SymbolType > & grammar ); + 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 ); /** * \override */ - template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const RightLG < SymbolType > & grammar ); + 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 ); /** * \override */ - template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const RightRG < SymbolType > & grammar ); + template < class SymbolType, class TerminalSymbolType = SymbolType, class NonterminalSymbolType = SymbolType > + static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > getRawRules ( const RightRG < SymbolType > & grammar ); }; -template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > RawRules::getRawRules ( const LG < SymbolType > & grammar ) { - ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > res; +template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > +ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > RawRules::getRawRules ( const LG < SymbolType > & grammar ) { + ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > res; for ( const auto & rule : grammar.getRules ( ) ) for ( const auto & rhs : rule.second ) { - ext::vector < ext::variant < SymbolType > > tmp; + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > tmp; - if ( rhs.template is < ext::vector < SymbolType > > ( ) ) { - for ( const SymbolType & symbol : rhs.template get < ext::vector < SymbolType > > ( ) ) - tmp.push_back ( ext::variant < SymbolType > ( symbol ) ); + if ( rhs.template is < ext::vector < TerminalSymbolType > > ( ) ) { + for ( const TerminalSymbolType & symbol : rhs.template get < ext::vector < TerminalSymbolType > > ( ) ) + tmp.push_back ( ext::variant < TerminalSymbolType, NonterminalSymbolType > ( symbol ) ); } else { - const auto & rhsTuple = rhs.template get < ext::tuple < ext::vector < SymbolType >, SymbolType, ext::vector < SymbolType > > > ( ); + const auto & rhsTuple = rhs.template get < ext::tuple < ext::vector < TerminalSymbolType >, NonterminalSymbolType, ext::vector < TerminalSymbolType > > > ( ); - for ( const SymbolType & symbol : ext::make_iterator_range ( std::get < 0 > ( rhsTuple ).begin ( ), std::get < 0 > ( rhsTuple ).end ( ) ) ) - tmp.push_back ( ext::variant < SymbolType > ( symbol ) ); + for ( const TerminalSymbolType & symbol : ext::make_iterator_range ( std::get < 0 > ( rhsTuple ).begin ( ), std::get < 0 > ( rhsTuple ).end ( ) ) ) + tmp.push_back ( ext::variant < TerminalSymbolType, NonterminalSymbolType > ( symbol ) ); - tmp.push_back ( ext::variant < SymbolType > ( std::get < 1 > ( rhsTuple ) ) ); + tmp.push_back ( ext::variant < TerminalSymbolType, NonterminalSymbolType > ( std::get < 1 > ( rhsTuple ) ) ); - for ( const SymbolType & symbol : ext::make_iterator_range ( std::get < 2 > ( rhsTuple ).begin ( ), std::get < 2 > ( rhsTuple ).end ( ) ) ) - tmp.push_back ( ext::variant < SymbolType > ( symbol ) ); + for ( const TerminalSymbolType & symbol : ext::make_iterator_range ( std::get < 2 > ( rhsTuple ).begin ( ), std::get < 2 > ( rhsTuple ).end ( ) ) ) + tmp.push_back ( ext::variant < TerminalSymbolType, NonterminalSymbolType > ( symbol ) ); } res[rule.first].insert ( std::move ( tmp ) ); @@ -119,37 +119,37 @@ ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > return res; } -template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > RawRules::getRawRules ( const GNF < SymbolType > & grammar ) { - ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > res; +template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > +ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > RawRules::getRawRules ( const GNF < SymbolType > & grammar ) { + ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > res; for ( const auto & rule : grammar.getRules ( ) ) for ( const auto & rhs : rule.second ) { - ext::vector < ext::variant < SymbolType > > tmp; - tmp.push_back ( ext::variant < SymbolType > ( rhs.first ) ); + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > tmp; + tmp.push_back ( ext::variant < TerminalSymbolType, NonterminalSymbolType > ( rhs.first ) ); - for ( const SymbolType & symbol : ext::make_iterator_range ( rhs.second.begin ( ), rhs.second.end ( ) ) ) - tmp.push_back ( ext::variant < SymbolType > ( symbol ) ); + for ( const NonterminalSymbolType & symbol : ext::make_iterator_range ( rhs.second.begin ( ), rhs.second.end ( ) ) ) + tmp.push_back ( ext::variant < TerminalSymbolType, NonterminalSymbolType > ( symbol ) ); res[rule.first].insert ( std::move ( tmp ) ); } if ( grammar.getGeneratesEpsilon ( ) ) - res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < SymbolType > > { } ); + res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > { } ); return res; } -template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > RawRules::getRawRules ( const EpsilonFreeCFG < SymbolType > & grammar ) { - ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > res; +template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > +ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > RawRules::getRawRules ( const EpsilonFreeCFG < SymbolType > & grammar ) { + ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > res; for ( const std::pair < SymbolType, ext::set < ext::vector < SymbolType > > > & rule : grammar.getRules ( ) ) { - ext::set < ext::vector < ext::variant < SymbolType > > > resRHSSet; + ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > resRHSSet; for ( const ext::vector < SymbolType > & rhs : rule.second ) { - ext::vector < ext::variant < SymbolType > > resRHS; + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > resRHS; for ( const SymbolType & symbol : rhs ) { - resRHS.push_back ( ext::variant < SymbolType > ( symbol ) ); + resRHS.push_back ( ext::variant < TerminalSymbolType, NonterminalSymbolType > ( symbol ) ); } resRHSSet.insert ( std::move ( resRHS ) ); } @@ -157,43 +157,43 @@ ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > } if ( grammar.getGeneratesEpsilon ( ) ) - res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < SymbolType > > { } ); + res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > { } ); return res; } -template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > RawRules::getRawRules ( const CNF < SymbolType > & grammar ) { - ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > res; +template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > +ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > RawRules::getRawRules ( const CNF < SymbolType > & grammar ) { + ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > res; for ( const auto & rule : grammar.getRules ( ) ) for ( const auto & rhs : rule.second ) { - if ( rhs.template is < SymbolType > ( ) ) { - ext::vector < ext::variant < SymbolType > > tmp { ext::variant < SymbolType > ( rhs.template get < SymbolType > ( ) ) }; + if ( rhs.template is < TerminalSymbolType > ( ) ) { + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > tmp { ext::variant < TerminalSymbolType, NonterminalSymbolType > ( rhs.template get < SymbolType > ( ) ) }; res[rule.first].insert ( std::move ( tmp ) ); } else { - const auto & realRHS = rhs.template get < ext::pair < SymbolType, SymbolType > > ( ); - ext::vector < ext::variant < SymbolType > > tmp { ext::variant < SymbolType > ( realRHS.first ), ext::variant < SymbolType > ( realRHS.second ) }; + const auto & realRHS = rhs.template get < ext::pair < NonterminalSymbolType, NonterminalSymbolType > > ( ); + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > tmp { ext::variant < TerminalSymbolType, NonterminalSymbolType > ( realRHS.first ), ext::variant < TerminalSymbolType, NonterminalSymbolType > ( realRHS.second ) }; res[rule.first].insert ( std::move ( tmp ) ); } } if ( grammar.getGeneratesEpsilon ( ) ) - res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < SymbolType > > { } ); + res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > { } ); return res; } -template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > RawRules::getRawRules ( const CFG < SymbolType > & grammar ) { - ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > res; +template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > +ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > RawRules::getRawRules ( const CFG < SymbolType > & grammar ) { + ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > res; for ( const std::pair < SymbolType, ext::set < ext::vector < SymbolType > > > & rule : grammar.getRules ( ) ) { - ext::set < ext::vector < ext::variant < SymbolType > > > resRHSSet; + ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > resRHSSet; for ( const ext::vector < SymbolType > & rhs : rule.second ) { - ext::vector < ext::variant < SymbolType > > resRHS; + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > resRHS; for ( const SymbolType & symbol : rhs ) { - resRHS.push_back ( ext::variant < SymbolType > ( symbol ) ); + resRHS.push_back ( ext::variant < TerminalSymbolType, NonterminalSymbolType > ( symbol ) ); } resRHSSet.insert ( std::move ( resRHS ) ); } @@ -203,23 +203,23 @@ ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > return res; } -template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > RawRules::getRawRules ( const LeftLG < SymbolType > & grammar ) { - ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > 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 ) { + ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > res; for ( const auto & rule : grammar.getRules ( ) ) for ( const auto & rhs : rule.second ) { - ext::vector < ext::variant < SymbolType > > tmp; + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > tmp; - if ( rhs.template is < ext::vector < SymbolType > > ( ) ) { - for ( const SymbolType & symbol : rhs.template get < ext::vector < SymbolType > > ( ) ) - tmp.push_back ( ext::variant < SymbolType > ( symbol ) ); + if ( rhs.template is < ext::vector < TerminalSymbolType > > ( ) ) { + for ( const TerminalSymbolType & symbol : rhs.template get < ext::vector < TerminalSymbolType > > ( ) ) + tmp.push_back ( ext::variant < TerminalSymbolType, NonterminalSymbolType > ( symbol ) ); } else { - const auto & rhsTuple = rhs.template get < ext::pair < SymbolType, ext::vector < SymbolType > > > ( ); + const auto & rhsTuple = rhs.template get < ext::pair < NonterminalSymbolType, ext::vector < TerminalSymbolType > > > ( ); - tmp.push_back ( ext::variant < SymbolType > ( rhsTuple.first ) ); - for ( const SymbolType & symbol : ext::make_iterator_range ( rhsTuple.second.begin ( ), rhsTuple.second.end ( ) ) ) - tmp.push_back ( ext::variant < SymbolType > ( symbol ) ); + tmp.push_back ( ext::variant < TerminalSymbolType, NonterminalSymbolType > ( rhsTuple.first ) ); + for ( const TerminalSymbolType & symbol : ext::make_iterator_range ( rhsTuple.second.begin ( ), rhsTuple.second.end ( ) ) ) + tmp.push_back ( ext::variant < TerminalSymbolType, NonterminalSymbolType > ( symbol ) ); } res[rule.first].insert ( std::move ( tmp ) ); @@ -228,67 +228,67 @@ ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > return res; } -template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > RawRules::getRawRules ( const LeftRG < SymbolType > & grammar ) { - ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > 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 ) { + ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > res; for ( const auto & rule : grammar.getRules ( ) ) for ( const auto & rhs : rule.second ) { - if ( rhs.template is < SymbolType > ( ) ) { - ext::vector < ext::variant < SymbolType > > tmp { ext::variant < SymbolType > ( rhs.template get < SymbolType > ( ) ) }; + if ( rhs.template is < TerminalSymbolType > ( ) ) { + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > tmp { ext::variant < TerminalSymbolType, NonterminalSymbolType > ( rhs.template get < TerminalSymbolType > ( ) ) }; res[rule.first].insert ( std::move ( tmp ) ); } else { - const auto & rhsPair = rhs.template get < ext::pair < SymbolType, SymbolType > > ( ); - ext::vector < ext::variant < SymbolType > > tmp { ext::variant < SymbolType > ( rhsPair.first ), ext::variant < SymbolType > ( rhsPair.second ) }; + const auto & rhsPair = rhs.template get < ext::pair < NonterminalSymbolType, TerminalSymbolType > > ( ); + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > tmp { ext::variant < TerminalSymbolType, NonterminalSymbolType > ( rhsPair.first ), ext::variant < TerminalSymbolType, NonterminalSymbolType > ( rhsPair.second ) }; res[rule.first].insert ( std::move ( tmp ) ); } } if ( grammar.getGeneratesEpsilon ( ) ) - res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < SymbolType > > { } ); + res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > { } ); return res; } -template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > RawRules::getRawRules ( const RightRG < SymbolType > & grammar ) { - ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > res; +template < class SymbolType, class TerminalSymbolType, class NonterminalSymbolType > +ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > RawRules::getRawRules ( const RightRG < SymbolType > & grammar ) { + ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > res; for ( const auto & rule : grammar.getRules ( ) ) for ( const auto & rhs : rule.second ) { - if ( rhs.template is < SymbolType > ( ) ) { - ext::vector < ext::variant < SymbolType > > tmp { ext::variant < SymbolType > ( rhs.template get < SymbolType > ( ) ) }; + if ( rhs.template is < TerminalSymbolType > ( ) ) { + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > tmp { ext::variant < TerminalSymbolType, NonterminalSymbolType > ( rhs.template get < TerminalSymbolType > ( ) ) }; res[rule.first].insert ( std::move ( tmp ) ); } else { - const auto & rhsPair = rhs.template get < ext::pair < SymbolType, SymbolType > > ( ); - ext::vector < ext::variant < SymbolType > > tmp { ext::variant < SymbolType > ( rhsPair.first ), ext::variant < SymbolType > ( rhsPair.second ) }; + const auto & rhsPair = rhs.template get < ext::pair < TerminalSymbolType, NonterminalSymbolType > > ( ); + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > tmp { ext::variant < TerminalSymbolType, NonterminalSymbolType > ( rhsPair.first ), ext::variant < TerminalSymbolType, NonterminalSymbolType > ( rhsPair.second ) }; res[rule.first].insert ( std::move ( tmp ) ); } } if ( grammar.getGeneratesEpsilon ( ) ) - res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < SymbolType > > { } ); + res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > { } ); return res; } -template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > RawRules::getRawRules ( const RightLG < SymbolType > & grammar ) { - ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > 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 ) { + ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > res; for ( const auto & rule : grammar.getRules ( ) ) for ( const auto & rhs : rule.second ) { - ext::vector < ext::variant < SymbolType > > tmp; + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > tmp; - if ( rhs.template is < ext::vector < SymbolType > > ( ) ) { - for ( const SymbolType & symbol : rhs.template get < ext::vector < SymbolType > > ( ) ) - tmp.push_back ( ext::variant < SymbolType > ( symbol ) ); + if ( rhs.template is < ext::vector < TerminalSymbolType > > ( ) ) { + for ( const TerminalSymbolType & symbol : rhs.template get < ext::vector < TerminalSymbolType > > ( ) ) + tmp.push_back ( ext::variant < TerminalSymbolType, NonterminalSymbolType > ( symbol ) ); } else { - const auto & rhsTuple = rhs.template get < ext::pair < ext::vector < SymbolType >, SymbolType > > ( ); + const auto & rhsTuple = rhs.template get < ext::pair < ext::vector < TerminalSymbolType >, NonterminalSymbolType > > ( ); - for ( const SymbolType & symbol : ext::make_iterator_range ( rhsTuple.first.begin ( ), rhsTuple.first.end ( ) ) ) - tmp.push_back ( ext::variant < SymbolType > ( symbol ) ); - tmp.push_back ( ext::variant < SymbolType > ( rhsTuple.second ) ); + for ( const TerminalSymbolType & symbol : ext::make_iterator_range ( rhsTuple.first.begin ( ), rhsTuple.first.end ( ) ) ) + tmp.push_back ( ext::variant < TerminalSymbolType, NonterminalSymbolType > ( symbol ) ); + tmp.push_back ( ext::variant < TerminalSymbolType, NonterminalSymbolType > ( rhsTuple.second ) ); } res[rule.first].insert ( std::move ( tmp ) ); diff --git a/alib2data/src/grammar/xml/ContextFree/LG.h b/alib2data/src/grammar/xml/ContextFree/LG.h index f6f89acb9f..f7be1babaa 100644 --- a/alib2data/src/grammar/xml/ContextFree/LG.h +++ b/alib2data/src/grammar/xml/ContextFree/LG.h @@ -115,7 +115,7 @@ grammar::LG < SymbolType > xmlApi < grammar::LG < SymbolType > >::parse ( ext::d template < class SymbolType > void xmlApi < grammar::LG < SymbolType > >::parseRule ( ext::deque < sax::Token >::iterator & input, grammar::LG < SymbolType > & grammar ) { SymbolType lhs = grammar::GrammarFromXMLParser::parseRuleSingleSymbolLHS < SymbolType > ( input ); - ext::vector < ext::variant < SymbolType > > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < ext::variant < SymbolType > > ( input ); + ext::vector < ext::variant < SymbolType, SymbolType > > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < ext::variant < SymbolType, SymbolType > > ( input ); grammar::AddRawRule::addRawRule ( grammar, std::move ( lhs ), std::move ( rhs ) ); } diff --git a/alib2data/src/grammar/xml/Regular/LeftLG.h b/alib2data/src/grammar/xml/Regular/LeftLG.h index 966f4ec9f6..39511380ec 100644 --- a/alib2data/src/grammar/xml/Regular/LeftLG.h +++ b/alib2data/src/grammar/xml/Regular/LeftLG.h @@ -116,7 +116,7 @@ grammar::LeftLG < SymbolType > xmlApi < grammar::LeftLG < SymbolType > >::parse 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 > > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < ext::variant < SymbolType > > ( input ); + ext::vector < ext::variant < SymbolType, SymbolType > > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < ext::variant < SymbolType, SymbolType > > ( input ); grammar::AddRawRule::addRawRule ( grammar, std::move ( lhs ), std::move ( rhs ) ); } diff --git a/alib2data/src/grammar/xml/Regular/RightLG.h b/alib2data/src/grammar/xml/Regular/RightLG.h index d2ce1bddb4..238da2f028 100644 --- a/alib2data/src/grammar/xml/Regular/RightLG.h +++ b/alib2data/src/grammar/xml/Regular/RightLG.h @@ -116,7 +116,7 @@ grammar::RightLG < SymbolType > xmlApi < grammar::RightLG < SymbolType > >::pars 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 > > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < ext::variant < SymbolType > > ( input ); + ext::vector < ext::variant < SymbolType, SymbolType > > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < ext::variant < SymbolType, SymbolType > > ( input ); grammar::AddRawRule::addRawRule ( grammar, std::move ( lhs ), std::move ( rhs ) ); } diff --git a/alib2str/src/grammar/string/common/GrammarFromStringParserCommon.h b/alib2str/src/grammar/string/common/GrammarFromStringParserCommon.h index 2d50711678..9094f0ebd2 100644 --- a/alib2str/src/grammar/string/common/GrammarFromStringParserCommon.h +++ b/alib2str/src/grammar/string/common/GrammarFromStringParserCommon.h @@ -27,8 +27,8 @@ class GrammarFromStringParserCommon { template < class SymbolType > static ext::set < SymbolType > parseSet ( std::istream & input ); - template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > parseCFLikeRules ( std::istream & input ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > parseCFLikeRules ( std::istream & input ); template < class SymbolType > static ext::map < ext::vector < SymbolType >, ext::set < ext::vector < SymbolType > > > parseCSLikeRules ( std::istream & input ); @@ -38,13 +38,13 @@ class GrammarFromStringParserCommon { public: - template < class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > + template < class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > > static T parseCFLikeGrammar ( std::istream & input ); - template < class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > + template < class T, class SymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T > > static T parseCSLikeGrammar ( std::istream & input ); - template < class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > + template < class T, class SymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T > > static T parsePreservingCSLikeGrammar ( std::istream & input ); }; @@ -81,9 +81,9 @@ ext::set<SymbolType> GrammarFromStringParserCommon::parseSet (std::istream& inpu return res; } -template < class SymbolType > -ext::map<SymbolType, ext::set<ext::vector<SymbolType>>> GrammarFromStringParserCommon::parseCFLikeRules (std::istream& input) { - ext::map<SymbolType, ext::set<ext::vector<SymbolType>>> result; +template < class TerminalSymbolType, class NonterminalSymbolType > +ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > GrammarFromStringParserCommon::parseCFLikeRules ( std::istream & input ) { + ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > result; grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next(input); if(token.type != grammar::GrammarFromStringLexer::TokenType::SET_BEGIN) { @@ -94,7 +94,7 @@ ext::map<SymbolType, ext::set<ext::vector<SymbolType>>> GrammarFromStringParserC if(token.type != grammar::GrammarFromStringLexer::TokenType::SET_END) { grammar::GrammarFromStringLexer::putback(input, token); while(true) { - SymbolType lhs = core::stringApi<SymbolType>::parse(input); + NonterminalSymbolType lhs = core::stringApi < NonterminalSymbolType >::parse ( input ); token = grammar::GrammarFromStringLexer::next(input); if(token.type != grammar::GrammarFromStringLexer::TokenType::MAPS_TO) { @@ -102,7 +102,7 @@ ext::map<SymbolType, ext::set<ext::vector<SymbolType>>> GrammarFromStringParserC } while(true) { - ext::vector<SymbolType> rhs; + ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rhs; token = grammar::GrammarFromStringLexer::next(input); if ( token.type != grammar::GrammarFromStringLexer::TokenType::COMMA @@ -113,7 +113,7 @@ ext::map<SymbolType, ext::set<ext::vector<SymbolType>>> GrammarFromStringParserC } else while(true) { grammar::GrammarFromStringLexer::putback(input, token); - rhs.push_back(core::stringApi<SymbolType>::parse(input)); + rhs.push_back ( core::stringApi < ext::variant < TerminalSymbolType, NonterminalSymbolType > >::parse ( input ) ); token = grammar::GrammarFromStringLexer::next(input); if ( token.type == grammar::GrammarFromStringLexer::TokenType::SEPARATOR || token.type == grammar::GrammarFromStringLexer::TokenType::COMMA @@ -146,35 +146,35 @@ ext::map<SymbolType, ext::set<ext::vector<SymbolType>>> GrammarFromStringParserC return result; } -template< class T, class SymbolType > +template< class T, class TerminalSymbolType, class NonterminalSymbolType > T GrammarFromStringParserCommon::parseCFLikeGrammar(std::istream& input) { grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next(input); if(token.type != grammar::GrammarFromStringLexer::TokenType::TUPLE_BEGIN) { throw exception::CommonException("Unrecognised Tuple begin token."); } - ext::set<SymbolType> nonterminals = parseSet < SymbolType > (input); + ext::set < NonterminalSymbolType > nonterminals = parseSet < NonterminalSymbolType > (input); token = grammar::GrammarFromStringLexer::next(input); if(token.type != grammar::GrammarFromStringLexer::TokenType::COMMA) { throw exception::CommonException("Unrecognised Comma token."); } - ext::set<SymbolType> terminals = parseSet < SymbolType > (input); + ext::set < TerminalSymbolType > terminals = parseSet < TerminalSymbolType > (input); token = grammar::GrammarFromStringLexer::next(input); if(token.type != grammar::GrammarFromStringLexer::TokenType::COMMA) { throw exception::CommonException("Unrecognised Comma token."); } - ext::map<SymbolType, ext::set<ext::vector<SymbolType>>> rules = parseCFLikeRules < SymbolType > (input); + ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > rules = parseCFLikeRules < TerminalSymbolType, NonterminalSymbolType > ( input ); token = grammar::GrammarFromStringLexer::next(input); if(token.type != grammar::GrammarFromStringLexer::TokenType::COMMA) { throw exception::CommonException("Unrecognised Comma token."); } - SymbolType initialSymbol = core::stringApi<SymbolType>::parse(input); + NonterminalSymbolType initialSymbol = core::stringApi < NonterminalSymbolType >::parse ( input ); token = grammar::GrammarFromStringLexer::next(input); if(token.type != grammar::GrammarFromStringLexer::TokenType::TUPLE_END) { @@ -183,12 +183,7 @@ T GrammarFromStringParserCommon::parseCFLikeGrammar(std::istream& input) { T grammar(nonterminals, terminals, initialSymbol); for(const auto& rule : rules) { - for(const auto& ruleRHS : rule.second) { - ext::vector < ext::variant < SymbolType > > rhs; - - for ( const SymbolType & symbol : ruleRHS ) - rhs.push_back ( symbol ); - + for(const ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs : rule.second) { grammar::AddRawRule::addRawRule ( grammar, rule.first, rhs ); } } diff --git a/alib2str/src/grammar/string/common/GrammarToStringComposerCommon.h b/alib2str/src/grammar/string/common/GrammarToStringComposerCommon.h index 21cfaaa392..c80419cc93 100644 --- a/alib2str/src/grammar/string/common/GrammarToStringComposerCommon.h +++ b/alib2str/src/grammar/string/common/GrammarToStringComposerCommon.h @@ -19,15 +19,15 @@ namespace grammar { class GrammarToStringComposerCommon { public: - template < class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > + template < class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > > static void composeCFLikeGrammar(std::ostream& output, const T& grammar); - template < class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > + template < class T, class SymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T > > static void composeCSLikeGrammar(std::ostream& output, const T& grammar); - template < class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > + template < class T, class SymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T > > static void composePreservingCSLikeGrammar(std::ostream& output, const T& grammar); }; -template < class T, class SymbolType > +template < class T, class TerminalSymbolType, class NonterminalSymbolType > void GrammarToStringComposerCommon::composeCFLikeGrammar(std::ostream& output, const T& grammar) { bool first; @@ -40,7 +40,7 @@ void GrammarToStringComposerCommon::composeCFLikeGrammar(std::ostream& output, c output << ", "; else first = true; - core::stringApi < SymbolType >::compose(output, symbol); + core::stringApi < NonterminalSymbolType >::compose(output, symbol); } output << "}," << std::endl; output << "{"; @@ -50,7 +50,7 @@ void GrammarToStringComposerCommon::composeCFLikeGrammar(std::ostream& output, c output << ", "; else first = true; - core::stringApi < SymbolType >::compose(output, symbol); + core::stringApi < TerminalSymbolType >::compose(output, symbol); } output << "}," << std::endl; output << "{ "; @@ -61,7 +61,7 @@ void GrammarToStringComposerCommon::composeCFLikeGrammar(std::ostream& output, c first = false; else output << "," << std::endl << " "; - core::stringApi < SymbolType >::compose(output, rule.first); + core::stringApi < NonterminalSymbolType >::compose(output, rule.first); output << " ->"; bool innerFirst = true; for(const auto& rhs : rule.second) { @@ -71,12 +71,12 @@ void GrammarToStringComposerCommon::composeCFLikeGrammar(std::ostream& output, c output << " |"; for(const auto& symbol : rhs) { output << " "; - core::stringApi < ext::variant < SymbolType > >::compose(output, symbol); + core::stringApi < ext::variant < TerminalSymbolType, NonterminalSymbolType > >::compose(output, symbol); } } } output << "}," << std::endl; - core::stringApi < SymbolType >::compose(output, grammar.getInitialSymbol()); + core::stringApi < NonterminalSymbolType >::compose(output, grammar.getInitialSymbol()); output << ")" << std::endl; } -- GitLab