From ab2818587a10a65c8e59b3c459ceb9d8968363ce Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Thu, 13 Sep 2018 22:43:40 +0200 Subject: [PATCH] preparation for terminal and nonterminal types in grammars --- .../src/grammar/generate/GenerateUpToLength.h | 26 +- .../properties/NonterminalUnitRuleCycle.h | 4 +- .../grammar/properties/NullableNonterminals.h | 16 +- .../properties/ProductiveNonterminals.h | 6 +- .../grammar/properties/RecursiveNonterminal.h | 15 +- .../grammar/properties/UnreachableSymbols.cpp | 18 +- .../grammar/properties/UnreachableSymbols.h | 22 +- .../src/grammar/simplify/EpsilonRemover.h | 11 +- .../simplify/UnproductiveSymbolsRemover.h | 10 +- .../simplify/UnreachableSymbolsRemover.h | 10 +- .../grammar/parsing/CornerSubstitution.cpp | 3 +- .../parsing/DeterministicLL1Grammar.cpp | 16 +- .../grammar/parsing/ExtractRightContext.cpp | 2 +- .../src/grammar/parsing/First.cpp | 38 +-- .../src/grammar/parsing/First.h | 30 +-- .../src/grammar/parsing/Follow.h | 16 +- .../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 | 12 +- .../test-src/grammar/parsing/FirstTest.cpp | 251 ++++++++++-------- .../grammar/parsing/LL1ParseTable.cpp | 77 +++--- alib2data/src/grammar/AddRawRule.cpp | 18 +- alib2data/src/grammar/AddRawRule.h | 132 ++++++--- alib2data/src/grammar/RawRules.cpp | 18 +- alib2data/src/grammar/RawRules.h | 157 +++++++---- alib2data/src/grammar/xml/ContextFree/LG.h | 4 +- alib2data/src/grammar/xml/Regular/LeftLG.h | 4 +- alib2data/src/grammar/xml/Regular/RightLG.h | 4 +- .../common/GrammarFromStringParserCommon.h | 7 +- .../common/GrammarToStringComposerCommon.h | 3 +- 33 files changed, 559 insertions(+), 401 deletions(-) diff --git a/alib2algo/src/grammar/generate/GenerateUpToLength.h b/alib2algo/src/grammar/generate/GenerateUpToLength.h index ab6cdd066b..22e8db4604 100644 --- a/alib2algo/src/grammar/generate/GenerateUpToLength.h +++ b/alib2algo/src/grammar/generate/GenerateUpToLength.h @@ -38,32 +38,36 @@ template < class T, class SymbolType > ext::set < string::LinearString < SymbolType > > GenerateUpToLength::generate ( const T & grammar, unsigned length ) { ext::set < string::LinearString < SymbolType > > res; - ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > rules = grammar::RawRules::getRawRules ( grammar ); + ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > rules = grammar::RawRules::getRawRules ( grammar ); if ( grammar.getGeneratesEpsilon ( ) ) { res.insert ( string::LinearString < SymbolType > { } ); - rules [ grammar.getInitialSymbol ( ) ].erase ( ext::vector < SymbolType > { } ); + rules [ grammar.getInitialSymbol ( ) ].erase ( ext::vector < ext::variant < SymbolType > > { } ); } - ext::deque < std::pair < ext::vector < SymbolType >, ext::vector < SymbolType > > > data; - data.push_back ( std::make_pair ( ext::vector < SymbolType > { }, ext::vector < SymbolType > { grammar.getInitialSymbol ( ) } ) ); + 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 ( ) ) } ) ); while ( ! data.empty ( ) ) { - std::pair < ext::vector < SymbolType >, ext::vector < SymbolType > > item = std::move ( data.back ( ) ); + std::pair < ext::vector < SymbolType >, ext::vector < ext::variant < SymbolType > > > item = std::move ( data.back ( ) ); data.pop_back ( ); - SymbolType nonterminal = std::move ( item.second.back ( ) ); + ext::variant < SymbolType > nonterminal = std::move ( item.second.back ( ) ); item.second.pop_back ( ); - if ( rules.find ( nonterminal ) == rules.end ( ) ) continue; + if ( ! nonterminal.template is < SymbolType > ( ) ) + continue; + auto rule = rules.find ( nonterminal.template get < SymbolType > ( ) ); + if ( rule == rules.end ( ) ) + continue; - for ( const ext::vector < SymbolType > & rhs : rules.find ( nonterminal )->second ) { + for ( const ext::vector < ext::variant < SymbolType > > & rhs : rule->second ) { if ( item.first.size ( ) + item.second.size ( ) + rhs.size ( ) > length ) continue; - ext::vector < SymbolType > newStack ( item.second ); + ext::vector < ext::variant < SymbolType > > newStack ( item.second ); ext::vector < SymbolType > newString ( item.first ); newStack.insert ( newStack.end ( ), rhs.rbegin ( ), rhs.rend ( ) ); - while ( ! newStack.empty ( ) && grammar.getTerminalAlphabet ( ).count ( newStack.back ( ) ) ) { - newString.push_back ( std::move(newStack.back ( ) ) ); + 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 > ( ) ) ); newStack.pop_back ( ); } diff --git a/alib2algo/src/grammar/properties/NonterminalUnitRuleCycle.h b/alib2algo/src/grammar/properties/NonterminalUnitRuleCycle.h index 35040e65fb..c8e0c729e3 100644 --- a/alib2algo/src/grammar/properties/NonterminalUnitRuleCycle.h +++ b/alib2algo/src/grammar/properties/NonterminalUnitRuleCycle.h @@ -55,8 +55,8 @@ ext::set<SymbolType> NonterminalUnitRuleCycle::getNonterminalUnitRuleCycle(const const SymbolType& lhs = rule.first; for(const auto& rhs : rule.second) { - if(Ni.at(i-1).count(lhs) && rhs.size() == 1 && grammar.getNonterminalAlphabet().count(rhs.front())) { - Ni.at(i).insert(rhs.front()); + if(Ni.at(i-1).count(lhs) && rhs.size() == 1 && rhs.front ( ).template is < SymbolType > ( ) && grammar.getNonterminalAlphabet().count(rhs.front().template get < SymbolType > ( ) )) { + Ni.at(i).insert(rhs.front().template get < SymbolType > ( ) ); } } } diff --git a/alib2algo/src/grammar/properties/NullableNonterminals.h b/alib2algo/src/grammar/properties/NullableNonterminals.h index 5c6472de99..53fe41847f 100644 --- a/alib2algo/src/grammar/properties/NullableNonterminals.h +++ b/alib2algo/src/grammar/properties/NullableNonterminals.h @@ -42,21 +42,21 @@ template<class T, class SymbolType > ext::set<SymbolType> NullableNonterminals::getNullableNonterminals(const T& grammar) { auto rawRules = grammar::RawRules::getRawRules ( grammar ); - ext::deque<ext::set<SymbolType>> Ni; + ext::deque < ext::set < SymbolType > > Ni; - Ni.push_back(ext::set<SymbolType>{ }); + Ni.push_back ( ext::set < SymbolType > { } ); int i = 1; - auto testCallback = [ & ] ( const SymbolType & symb ) { - return Ni.at ( i - 1 ).count ( symb ); + auto testCallback = [ & ] ( const ext::variant < SymbolType > & symb ) { + return Ni.at ( i - 1 ).count ( symb.template get < SymbolType > ( ) ); }; - while(true) { - Ni.push_back(ext::set<SymbolType>{ }); + while ( true ) { + Ni.push_back ( ext::set < SymbolType > { } ); for ( const auto & rule : rawRules ) { - for(const auto& rhs : rule.second) { + for ( const auto & rhs : rule.second ) { if ( rhs.size ( ) == 0 || std::all_of ( rhs.begin ( ), rhs.end ( ), testCallback ) ) { - Ni.at(i).insert(rule.first); + Ni.at ( i ).insert ( rule.first ); } } } diff --git a/alib2algo/src/grammar/properties/ProductiveNonterminals.h b/alib2algo/src/grammar/properties/ProductiveNonterminals.h index f7df0a21b5..6cbf0c4840 100644 --- a/alib2algo/src/grammar/properties/ProductiveNonterminals.h +++ b/alib2algo/src/grammar/properties/ProductiveNonterminals.h @@ -41,9 +41,9 @@ ext::set<SymbolType> ProductiveNonterminals::getProductiveNonterminals( const T Ni.push_back( ext::set<SymbolType>( ) ); int i = 1; - - auto testCallback = [ & ] ( const SymbolType & symbol ) { - return Ni.at ( i - 1 ).count ( symbol ) || grammar.getTerminalAlphabet ( ).count ( symbol ); + 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 > ( ) ) ); }; // 2. diff --git a/alib2algo/src/grammar/properties/RecursiveNonterminal.h b/alib2algo/src/grammar/properties/RecursiveNonterminal.h index 4add4074b4..d0225eb95a 100644 --- a/alib2algo/src/grammar/properties/RecursiveNonterminal.h +++ b/alib2algo/src/grammar/properties/RecursiveNonterminal.h @@ -55,18 +55,21 @@ bool RecursiveNonterminal::isNonterminalRecursive ( const T & grammar, const Sym for ( const SymbolType & lhs : Ni.at ( i - 1 ) ) if ( rawRules.find ( lhs ) != rawRules.end ( ) ) - for ( const ext::vector < SymbolType > & rhs : rawRules.find ( lhs )->second ) - for ( const SymbolType & rhsSymbol : rhs ) { - if ( grammar.getTerminalAlphabet ( ).count ( rhsSymbol ) ) break; + 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 > ( ) ) ) + break; - Ni.at ( i ).insert ( rhsSymbol ); + Ni.at ( i ).insert ( rhsSymbol.template get < SymbolType > ( ) ); - if ( !nullable.count ( rhsSymbol ) ) break; + if ( ! nullable.count ( rhsSymbol.template get < SymbolType > ( ) ) ) + break; } - if ( Ni.at ( i ).count ( nonterminal ) ) return true; + if ( Ni.at ( i ).count ( nonterminal ) ) + return true; i += 1; } diff --git a/alib2algo/src/grammar/properties/UnreachableSymbols.cpp b/alib2algo/src/grammar/properties/UnreachableSymbols.cpp index aaaec8ca88..6c4fc7fc94 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 < DefaultSymbolType >, const grammar::CFG < > & > ( UnreachableSymbols::getUnreachableSymbols ); -auto UnreachableSymbolsEpsilonFreeCFG = registration::AbstractRegister < UnreachableSymbols, ext::set < DefaultSymbolType >, const grammar::EpsilonFreeCFG < > & > ( UnreachableSymbols::getUnreachableSymbols ); -auto UnreachableSymbolsGNF = registration::AbstractRegister < UnreachableSymbols, ext::set < DefaultSymbolType >, const grammar::GNF < > & > ( UnreachableSymbols::getUnreachableSymbols ); -auto UnreachableSymbolsCNF = registration::AbstractRegister < UnreachableSymbols, ext::set < DefaultSymbolType >, const grammar::CNF < > & > ( UnreachableSymbols::getUnreachableSymbols ); -auto UnreachableSymbolsLG = registration::AbstractRegister < UnreachableSymbols, ext::set < DefaultSymbolType >, const grammar::LG < > & > ( UnreachableSymbols::getUnreachableSymbols ); -auto UnreachableSymbolsLeftLG = registration::AbstractRegister < UnreachableSymbols, ext::set < DefaultSymbolType >, const grammar::LeftLG < > & > ( UnreachableSymbols::getUnreachableSymbols ); -auto UnreachableSymbolsLeftRG = registration::AbstractRegister < UnreachableSymbols, ext::set < DefaultSymbolType >, const grammar::LeftRG < > & > ( UnreachableSymbols::getUnreachableSymbols ); -auto UnreachableSymbolsRightLG = registration::AbstractRegister < UnreachableSymbols, ext::set < DefaultSymbolType >, const grammar::RightLG < > & > ( UnreachableSymbols::getUnreachableSymbols ); -auto UnreachableSymbolsRightRG = registration::AbstractRegister < UnreachableSymbols, ext::set < DefaultSymbolType >, const grammar::RightRG < > & > ( UnreachableSymbols::getUnreachableSymbols ); +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 ); } /* namespace properties */ diff --git a/alib2algo/src/grammar/properties/UnreachableSymbols.h b/alib2algo/src/grammar/properties/UnreachableSymbols.h index 8cf54ffd3d..5bd8fd8011 100644 --- a/alib2algo/src/grammar/properties/UnreachableSymbols.h +++ b/alib2algo/src/grammar/properties/UnreachableSymbols.h @@ -29,28 +29,28 @@ public: * Implements */ template<class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > - static ext::set<DefaultSymbolType> getUnreachableSymbols( const T & grammar ); + static ext::set < ext::variant < SymbolType > > getUnreachableSymbols ( const T & grammar ); }; template<class T, class SymbolType > -ext::set<DefaultSymbolType> UnreachableSymbols::getUnreachableSymbols( const T & grammar ) { +ext::set < ext::variant < SymbolType > > UnreachableSymbols::getUnreachableSymbols( const T & grammar ) { auto rawRules = grammar::RawRules::getRawRules ( grammar ); // 1 - ext::deque<ext::set<DefaultSymbolType>> Vi; - Vi.push_back( ext::set<DefaultSymbolType>( ) ); - Vi.at( 0 ).insert( grammar.getInitialSymbol( ) ); + ext::deque < ext::set < ext::variant < SymbolType > > > Vi; + Vi.push_back ( ext::set < ext::variant < SymbolType > > ( ) ); + Vi.at ( 0 ).insert ( grammar.getInitialSymbol ( ) ); int i = 1; // 2. - while( true ) { - Vi.push_back( Vi.at( i - 1 ) ); + while ( true ) { + Vi.push_back ( Vi.at ( i - 1 ) ); - for( const auto & rule : rawRules ) { - if( Vi.at( i - 1 ).count( rule.first ) ) { - for( const auto & rhs : rule.second ) { - Vi.at( i ).insert( rhs.begin( ), rhs.end( ) ); + for ( const auto & rule : rawRules ) { + if ( Vi.at( i - 1 ).count ( ext::variant < SymbolType > ( 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 d01d2611bc..2ff6355534 100644 --- a/alib2algo/src/grammar/simplify/EpsilonRemover.h +++ b/alib2algo/src/grammar/simplify/EpsilonRemover.h @@ -33,7 +33,7 @@ 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<SymbolType>& rhs, unsigned i, ext::vector<SymbolType> clear); + 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 T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > static grammar::EpsilonFreeCFG < SymbolType > removeInternal( const T & origGrammar ); @@ -59,16 +59,17 @@ public: }; template < class SymbolType > -void EpsilonRemover::removeNullableNonterminals(grammar::EpsilonFreeCFG < SymbolType > & grammar, const ext::set<SymbolType>& nullableNonterminals, const SymbolType& lhs, const ext::vector<SymbolType>& rhs, unsigned i, ext::vector<SymbolType> clear) { +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) { if(rhs.size() == i) { if(clear.size() == 0) return; grammar.addRule(lhs, clear); - } else if(nullableNonterminals.find(rhs[i]) != nullableNonterminals.end()) { + } else if(rhs[i].template is < SymbolType > ( ) && nullableNonterminals.find ( rhs [ i ].template get < SymbolType > ( ) ) != nullableNonterminals.end()) { removeNullableNonterminals(grammar, nullableNonterminals, lhs, rhs, i+1, clear); - clear.push_back(rhs[i]); + + clear.push_back(rhs[i].template get < SymbolType > ( ) ); removeNullableNonterminals(grammar, nullableNonterminals, lhs, rhs, i+1, clear); } else { - clear.push_back(rhs[i]); + clear.push_back(rhs[i].template get < SymbolType > ( ) ); removeNullableNonterminals(grammar, nullableNonterminals, lhs, rhs, i+1, clear); } } diff --git a/alib2algo/src/grammar/simplify/UnproductiveSymbolsRemover.h b/alib2algo/src/grammar/simplify/UnproductiveSymbolsRemover.h index 68697b0ddf..16f6eaadc4 100644 --- a/alib2algo/src/grammar/simplify/UnproductiveSymbolsRemover.h +++ b/alib2algo/src/grammar/simplify/UnproductiveSymbolsRemover.h @@ -57,18 +57,18 @@ T UnproductiveSymbolsRemover::remove( const T & grammar ) { ret.addTerminalSymbol( symbol ); auto rawRules = grammar::RawRules::getRawRules ( grammar ); - const ext::set<SymbolType> & terminals = ret.getTerminalAlphabet( ); - auto testCallback = [ & ]( const SymbolType & symbol ) { - return Nt.count( symbol ) || terminals.count( symbol ); + 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 > ( ) ) ); }; for( const auto & rule : rawRules ) { if( Nt.count( rule.first ) ) { for( const auto & rhs : rule.second ) { - if( all_of( rhs.begin( ), rhs.end( ), testCallback) ) - grammar::AddRawRule::addRawRule ( ret, rule.first, rhs ); + if( all_of( rhs.begin( ), rhs.end( ), testCallback ) ) + grammar::AddRawRule::addRawRule ( ret, rule.first, rhs ); } } } diff --git a/alib2algo/src/grammar/simplify/UnreachableSymbolsRemover.h b/alib2algo/src/grammar/simplify/UnreachableSymbolsRemover.h index 24c07b71d3..aebfb246cd 100644 --- a/alib2algo/src/grammar/simplify/UnreachableSymbolsRemover.h +++ b/alib2algo/src/grammar/simplify/UnreachableSymbolsRemover.h @@ -45,23 +45,23 @@ public: template < class T, class SymbolType > T UnreachableSymbolsRemover::remove( const T & grammar) { // 1. - ext::set<SymbolType> Vt = grammar::properties::UnreachableSymbols::getUnreachableSymbols( grammar ); + ext::set < ext::variant < SymbolType > > Vt = grammar::properties::UnreachableSymbols::getUnreachableSymbols( grammar ); T ret(grammar.getInitialSymbol( ) ); - ext::set<SymbolType> newNonTerminals, newTerminals; + ext::set < ext::variant < SymbolType > > 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 ); + ret.addNonterminalSymbol( symbol.template get < SymbolType > ( ) ); 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 ); + ret.addTerminalSymbol( symbol.template get < SymbolType > ( ) ); auto rawRules = grammar::RawRules::getRawRules ( grammar ); - auto testCallback = [ & ]( const SymbolType & symb ) -> bool { + auto testCallback = [ & ] ( const ext::variant < SymbolType > & 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 beb7bb338e..7bbe1a5ddd 100644 --- a/alib2algo_experimental/src/grammar/parsing/CornerSubstitution.cpp +++ b/alib2algo_experimental/src/grammar/parsing/CornerSubstitution.cpp @@ -26,7 +26,8 @@ void CornerSubstitution::cornerSubstitution ( grammar::CFG < > & grammar, const const DefaultSymbolType & lhs = rule.first; for ( const ext::vector < DefaultSymbolType > & rhs : rule.second ) { - if ( ( lhs == nonterminal ) && ( rhs.size ( ) > 0 ) && ( grammar.getNonterminalAlphabet ( ).count ( rhs[0] ) ) && First::first ( grammar, rhs ).count ( ext::variant < DefaultSymbolType, DefaultEpsilonType > ( terminal ) ) ) + ext::vector < ext::variant < 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 res.addRule ( lhs, rhs ); diff --git a/alib2algo_experimental/src/grammar/parsing/DeterministicLL1Grammar.cpp b/alib2algo_experimental/src/grammar/parsing/DeterministicLL1Grammar.cpp index 1ced2810c9..1f823db964 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 < DefaultSymbolType > > > parseTable = LL1ParseTable::parseTable ( grammar ); + ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < 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 < DefaultSymbolType > > > & elem : parseTable ) + for ( const std::pair < const ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < 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 < DefaultSymbolType > > > & elem : parseTable ) { + for ( const std::pair < const ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < 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 < DefaultSymbolType > > & rhsds = elem.second; + const ext::set < ext::vector < ext::variant < DefaultSymbolType > > > & rhsds = elem.second; - if ( ( elem.second.size ( ) > 1 ) && std::all_of ( rhsds.begin ( ), rhsds.end ( ), [] ( const ext::vector < DefaultSymbolType > & rhs ) { + if ( ( elem.second.size ( ) > 1 ) && std::all_of ( rhsds.begin ( ), rhsds.end ( ), [] ( const ext::vector < ext::variant < 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 < DefaultSymbolType > > > & elem : parseTable ) { + for ( const std::pair < const ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < 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 < DefaultSymbolType > > & rhsds = elem.second; + const ext::set < ext::vector < ext::variant < DefaultSymbolType > > > & rhsds = elem.second; - if ( ( elem.second.size ( ) > 1 ) && std::any_of ( rhsds.begin ( ), rhsds.end ( ), [] ( const ext::vector < DefaultSymbolType > & rhs ) { + if ( ( elem.second.size ( ) > 1 ) && std::any_of ( rhsds.begin ( ), rhsds.end ( ), [] ( const ext::vector < ext::variant < 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 5cfb2364b5..5a296bb34a 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 < 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 > > ( 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 b182a19226..53e7335856 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 < DefaultSymbolType >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::CFG < > & > ( First::first ); -auto FirstEpsilonFreeCFG = registration::AbstractRegister < First, ext::map < ext::vector < DefaultSymbolType >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::EpsilonFreeCFG < > & > ( First::first ); -auto FirstGNF = registration::AbstractRegister < First, ext::map < ext::vector < DefaultSymbolType >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::GNF < > & > ( First::first ); -auto FirstCNF = registration::AbstractRegister < First, ext::map < ext::vector < DefaultSymbolType >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::CNF < > & > ( First::first ); -auto FirstLG = registration::AbstractRegister < First, ext::map < ext::vector < DefaultSymbolType >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::LG < > & > ( First::first ); -auto FirstLeftLG = registration::AbstractRegister < First, ext::map < ext::vector < DefaultSymbolType >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::LeftLG < > & > ( First::first ); -auto FirstLeftRG = registration::AbstractRegister < First, ext::map < ext::vector < DefaultSymbolType >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::LeftRG < > & > ( First::first ); -auto FirstRightLG = registration::AbstractRegister < First, ext::map < ext::vector < DefaultSymbolType >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > >, const grammar::RightLG < > & > ( First::first ); -auto FirstRightRG = registration::AbstractRegister < First, ext::map < ext::vector < 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 < DefaultSymbolType > & > ( First::first ); -auto FirstEpsilonFreeCFG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::EpsilonFreeCFG < > &, const ext::vector < DefaultSymbolType > & > ( First::first ); -auto FirstGNF2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::GNF < > &, const ext::vector < DefaultSymbolType > & > ( First::first ); -auto FirstCNF2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::CNF < > &, const ext::vector < DefaultSymbolType > & > ( First::first ); -auto FirstLG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::LG < > &, const ext::vector < DefaultSymbolType > & > ( First::first ); -auto FirstLeftLG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::LeftLG < > &, const ext::vector < DefaultSymbolType > & > ( First::first ); -auto FirstLeftRG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::LeftRG < > &, const ext::vector < DefaultSymbolType > & > ( First::first ); -auto FirstRightLG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::RightLG < > &, const ext::vector < DefaultSymbolType > & > ( First::first ); -auto FirstRightRG2 = registration::AbstractRegister < First, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > >, const grammar::RightRG < > &, const ext::vector < DefaultSymbolType > & > ( First::first ); +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 ); } /* namespace parsing */ diff --git a/alib2algo_experimental/src/grammar/parsing/First.h b/alib2algo_experimental/src/grammar/parsing/First.h index 21a53135f9..e42565eff9 100644 --- a/alib2algo_experimental/src/grammar/parsing/First.h +++ b/alib2algo_experimental/src/grammar/parsing/First.h @@ -22,21 +22,21 @@ 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 < SymbolType > & rhs ); + 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 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 < SymbolType > > > & rules ); + 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 ); public: template < class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > > - static ext::map < ext::vector < SymbolType >, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > first ( const T & grammar ); + static ext::map < ext::vector < ext::variant < SymbolType > >, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > 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 < SymbolType > & rhs ); + static ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > first ( const T & grammar, const ext::vector < ext::variant < SymbolType > > & 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 < SymbolType > & rhs ) { +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 ) { // 1. FIRST(\varepsilon) = { \varepsilon } if ( rhs.size ( ) == 0 ) { return { string::Epsilon < SymbolType >::EPSILON }; @@ -44,7 +44,7 @@ ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > First:: // 2. FIRST(a) = { a } forall a \in T else if ( terminals.count ( rhs[0] ) ) { - return { rhs[0] }; + return { rhs[0].template get < SymbolType > ( ) }; } // 4. FIRST(A \alpha) = first(A) if A \in N and \varepsilon \notin first(A) @@ -57,7 +57,7 @@ ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > First:: ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > res = firstOfNonterminal.find ( rhs[0] )->second; res.erase ( string::Epsilon < SymbolType >::EPSILON ); - ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > next = first ( terminals, nonterminals, firstOfNonterminal, ext::vector < SymbolType > ( rhs.begin ( ) + 1, rhs.end ( ) ) ); + ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > next = first ( terminals, nonterminals, firstOfNonterminal, ext::vector < ext::variant < SymbolType > > ( rhs.begin ( ) + 1, rhs.end ( ) ) ); res.insert ( next.begin ( ), next.end ( ) ); return res; } else { @@ -66,7 +66,7 @@ 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 < SymbolType > > > & rules ) { +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 ) { /* * * 1. foreach A \in N: first(A) = \emptyset @@ -83,8 +83,8 @@ ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < S ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > firstOfNonterminal2 = firstOfNonterminal1; do { - for ( const std::pair < const SymbolType, ext::set < ext::vector < SymbolType > > > & rule : rules ) - for ( const ext::vector < SymbolType > & rhs : rule.second ) { + 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 ); firstOfNonterminal2[rule.first].insert ( newFirst.begin ( ), newFirst.end ( ) ); } @@ -101,22 +101,22 @@ ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < S } template < class T, class SymbolType > -ext::map < ext::vector < SymbolType >, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > First::first ( const T & grammar ) { +ext::map < ext::vector < ext::variant < SymbolType > >, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > 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 < ext::vector < SymbolType >, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > res; + ext::map < ext::vector < ext::variant < SymbolType > >, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > res; - for ( const std::pair < const SymbolType, ext::set < ext::vector < SymbolType > > > & rule : rawRules ) - for ( const ext::vector < SymbolType > & rhs : rule.second ) + 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 ) 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 < SymbolType > & rhs ) { +ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > First::first ( const T & grammar, const ext::vector < ext::variant < SymbolType > > & 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 ); diff --git a/alib2algo_experimental/src/grammar/parsing/Follow.h b/alib2algo_experimental/src/grammar/parsing/Follow.h index b23686f8be..aa9693c2b6 100644 --- a/alib2algo_experimental/src/grammar/parsing/Follow.h +++ b/alib2algo_experimental/src/grammar/parsing/Follow.h @@ -40,25 +40,25 @@ public: template < class T, class SymbolType > void Follow::follow ( const T & grammar, ext::map < SymbolType, ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > > & followSet ) { auto rawRules = grammar::RawRules::getRawRules ( grammar ); - for ( const std::pair < const SymbolType, ext::set < ext::vector < SymbolType > > > & rule : rawRules ) { + for ( const std::pair < const SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > & rule : rawRules ) { const SymbolType & X = rule.first; - for ( const ext::vector < SymbolType > & rhs : rule.second ) + for ( const ext::vector < ext::variant < SymbolType > > & rhs : rule.second ) // every nt in rhs is Y - for ( typename ext::vector < SymbolType >::const_iterator it = rhs.begin ( ); it != rhs.end ( ); it++ ) { - const SymbolType & Y = * it; + for ( typename ext::vector < ext::variant < SymbolType > >::const_iterator it = rhs.begin ( ); it != rhs.end ( ); it++ ) { + const ext::variant < SymbolType > & Y = * it; - if ( !grammar.getNonterminalAlphabet ( ).count ( Y ) ) + if ( ! grammar.getNonterminalAlphabet ( ).count ( Y ) ) continue; - ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > firstBeta = First::first ( grammar, ext::vector < SymbolType > ( std::next ( it ), rhs.end ( ) ) ); + ext::set < ext::variant < SymbolType, string::Epsilon < SymbolType > > > firstBeta = First::first ( grammar, ext::vector < ext::variant < SymbolType > > ( 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].insert ( followSet[X].begin ( ), followSet[X].end ( ) ); + followSet [ Y.template get < SymbolType > ( ) ].insert ( followSet[X].begin ( ), followSet[X].end ( ) ); } - followSet[Y].insert ( firstBeta.begin ( ), firstBeta.end ( ) ); + followSet [ Y.template get < SymbolType > ( ) ].insert ( firstBeta.begin ( ), firstBeta.end ( ) ); } } diff --git a/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.cpp b/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.cpp index a40b4d2b25..6ef83ca990 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 < DefaultSymbolType > > & rhsds ) { - for ( const ext::vector < DefaultSymbolType > & rhs : rhsds ) +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 ) 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 25158338a0..a58683b0a4 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 < DefaultSymbolType > > & rhsds ); + static void handleFirstFirstConflict ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal, const ext::set < ext::vector < ext::variant < DefaultSymbolType > > > & rhsds ); }; } /* namespace parsing */ diff --git a/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.cpp b/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.cpp index 30e0d5d64d..a9593adf0e 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 < DefaultSymbolType > > & /* rhsds */ ) { +void HandleFirstFollowConflict::handleFirstFollowConflict ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal, const ext::set < ext::vector < ext::variant < 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 < 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 > > ( 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 18bb291c08..cefc8625bf 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 < DefaultSymbolType > > & /* rhsds */ ); + static void handleFirstFollowConflict ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal, const ext::set < ext::vector < ext::variant < DefaultSymbolType > > > & /* rhsds */ ); }; } /* namespace parsing */ diff --git a/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.cpp b/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.cpp index e8e061dad5..c895a2dd21 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 < 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 < 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 < 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 < 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 < 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 < 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 < 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 < 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 < 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 > > > >, 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 ); } /* namespace parsing */ diff --git a/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.h b/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.h index 0d2708390e..eed9eed649 100644 --- a/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.h +++ b/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.h @@ -26,22 +26,22 @@ 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 < SymbolType > > > parseTable ( const T & grammar ); + 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 SymbolType > -ext::map < ext::pair < ext::variant < SymbolType, string::Epsilon < > >, SymbolType >, ext::set < ext::vector < SymbolType > > > LL1ParseTable::parseTable ( const T & grammar ) { - ext::map < ext::pair < ext::variant < SymbolType, string::Epsilon < > >, SymbolType >, ext::set < ext::vector < SymbolType > > > res; +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; - ext::map < ext::vector < SymbolType >, ext::set < ext::variant < SymbolType, string::Epsilon < > > > > first = First::first ( grammar ); + 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 ); auto rawRules = grammar::RawRules::getRawRules ( grammar ); - for ( const std::pair < const SymbolType, ext::set < ext::vector < SymbolType > > > & transition : rawRules ) { + for ( const std::pair < const SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > & transition : rawRules ) { const SymbolType & lhs = transition.first; - for ( const ext::vector < SymbolType > & rhs : transition.second ) { + 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 < > > ( ) ) continue; diff --git a/alib2algo_experimental/test-src/grammar/parsing/FirstTest.cpp b/alib2algo_experimental/test-src/grammar/parsing/FirstTest.cpp index 3c448587f4..4fb8493bfb 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/FirstTest.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/FirstTest.cpp @@ -29,45 +29,56 @@ void FirstTest::testFirst ( ) { grammar.setTerminalAlphabet ( ext::set < DefaultSymbolType > { tP, tS, tL, tR, tA } ); grammar.setNonterminalAlphabet ( ext::set < DefaultSymbolType > { nE, nT, nF } ); - ext::vector < DefaultSymbolType > rhsE1 ( { nE, tP, nT } ); - ext::vector < DefaultSymbolType > rhsE2 ( { nT } ); - ext::vector < DefaultSymbolType > rhsT1 ( { nT, tS, nF } ); - ext::vector < DefaultSymbolType > rhsT2 ( { nF } ); - ext::vector < DefaultSymbolType > rhsF1 ( { tA } ); - ext::vector < DefaultSymbolType > rhsF2 ( { tL, nE, tR } ); - - grammar.addRule ( nE, rhsE1 ); - grammar.addRule ( nE, rhsE2 ); - grammar.addRule ( nT, rhsT1 ); - grammar.addRule ( nT, rhsT2 ); - grammar.addRule ( nF, rhsF1 ); - grammar.addRule ( nF, rhsF2 ); + { + ext::vector < DefaultSymbolType > rhsE1 ( { nE, tP, nT } ); + ext::vector < DefaultSymbolType > rhsE2 ( { nT } ); + ext::vector < DefaultSymbolType > rhsT1 ( { nT, tS, nF } ); + ext::vector < DefaultSymbolType > rhsT2 ( { nF } ); + ext::vector < DefaultSymbolType > rhsF1 ( { tA } ); + ext::vector < DefaultSymbolType > rhsF2 ( { tL, nE, tR } ); + + grammar.addRule ( nE, rhsE1 ); + grammar.addRule ( nE, rhsE2 ); + grammar.addRule ( nT, rhsT1 ); + grammar.addRule ( nT, rhsT2 ); + grammar.addRule ( nF, rhsF1 ); + grammar.addRule ( nF, rhsF2 ); + } // -------------------------------------------------- - ext::map < ext::vector < DefaultSymbolType >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > first; - - first[rhsE1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tA, tL - }; - first[rhsE2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tA, tL - }; - first[rhsT1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tA, tL - }; - first[rhsT2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tA, tL - }; - first[rhsF1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tA - }; - first[rhsF2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tL - }; + ext::map < ext::vector < ext::variant < 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 } ); + + first[rhsE1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tA, tL + }; + first[rhsE2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tA, tL + }; + first[rhsT1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tA, tL + }; + first[rhsT2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tA, tL + }; + first[rhsF1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tA + }; + first[rhsF2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tL + }; + } // -------------------------------------------------- - ext::map < ext::vector < DefaultSymbolType >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > firstAlgo; + ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > firstAlgo; for ( const auto & rule : grammar::RawRules::getRawRules ( grammar ) ) for ( const auto & rhs : rule.second ) @@ -95,85 +106,103 @@ void FirstTest::testFirst ( ) { grammar.setTerminalAlphabet ( ext::set < DefaultSymbolType > { tA, tB, tC, tD, tE } ); grammar.setNonterminalAlphabet ( ext::set < DefaultSymbolType > { nS, nA, nB, nC, nD, nE, nF } ); - ext::vector < DefaultSymbolType > rhsS1 ( { nB, tD, nS } ); - ext::vector < DefaultSymbolType > rhsS2 ( { tD, tD, nC } ); - ext::vector < DefaultSymbolType > rhsS3 ( { tC, nA } ); - ext::vector < DefaultSymbolType > rhsA1 ( { tA, tE, nE } ); - ext::vector < DefaultSymbolType > rhsA2 ( { tB, tB, nE } ); - ext::vector < DefaultSymbolType > rhsB1 ( { tA, nF } ); - ext::vector < DefaultSymbolType > rhsB2 ( { tB, tB, nD } ); - ext::vector < DefaultSymbolType > rhsC1 ( { tA, nB, tD } ); - ext::vector < DefaultSymbolType > rhsC2 ( { tE, nA } ); - ext::vector < DefaultSymbolType > rhsD1 ( { tC, tA, nF } ); - ext::vector < DefaultSymbolType > rhsE1 ( { tC, tA, tE, nE } ); - ext::vector < DefaultSymbolType > rhsE2 ( { } ); - ext::vector < DefaultSymbolType > rhsF1 ( { tE, nD } ); - ext::vector < DefaultSymbolType > rhsF2 ( { } ); - - grammar.addRule ( nS, rhsS1 ); - grammar.addRule ( nS, rhsS2 ); - grammar.addRule ( nS, rhsS3 ); - grammar.addRule ( nA, rhsA1 ); - grammar.addRule ( nA, rhsA2 ); - grammar.addRule ( nB, rhsB1 ); - grammar.addRule ( nB, rhsB2 ); - grammar.addRule ( nC, rhsC1 ); - grammar.addRule ( nC, rhsC2 ); - grammar.addRule ( nD, rhsD1 ); - grammar.addRule ( nE, rhsE1 ); - grammar.addRule ( nE, rhsE2 ); - grammar.addRule ( nF, rhsF1 ); - grammar.addRule ( nF, rhsF2 ); + { + ext::vector < DefaultSymbolType > rhsS1 ( { nB, tD, nS } ); + ext::vector < DefaultSymbolType > rhsS2 ( { tD, tD, nC } ); + ext::vector < DefaultSymbolType > rhsS3 ( { tC, nA } ); + ext::vector < DefaultSymbolType > rhsA1 ( { tA, tE, nE } ); + ext::vector < DefaultSymbolType > rhsA2 ( { tB, tB, nE } ); + ext::vector < DefaultSymbolType > rhsB1 ( { tA, nF } ); + ext::vector < DefaultSymbolType > rhsB2 ( { tB, tB, nD } ); + ext::vector < DefaultSymbolType > rhsC1 ( { tA, nB, tD } ); + ext::vector < DefaultSymbolType > rhsC2 ( { tE, nA } ); + ext::vector < DefaultSymbolType > rhsD1 ( { tC, tA, nF } ); + ext::vector < DefaultSymbolType > rhsE1 ( { tC, tA, tE, nE } ); + ext::vector < DefaultSymbolType > rhsE2 ( { } ); + ext::vector < DefaultSymbolType > rhsF1 ( { tE, nD } ); + ext::vector < DefaultSymbolType > rhsF2 ( { } ); + + grammar.addRule ( nS, rhsS1 ); + grammar.addRule ( nS, rhsS2 ); + grammar.addRule ( nS, rhsS3 ); + grammar.addRule ( nA, rhsA1 ); + grammar.addRule ( nA, rhsA2 ); + grammar.addRule ( nB, rhsB1 ); + grammar.addRule ( nB, rhsB2 ); + grammar.addRule ( nC, rhsC1 ); + grammar.addRule ( nC, rhsC2 ); + grammar.addRule ( nD, rhsD1 ); + grammar.addRule ( nE, rhsE1 ); + grammar.addRule ( nE, rhsE2 ); + grammar.addRule ( nF, rhsF1 ); + grammar.addRule ( nF, rhsF2 ); + } // -------------------------------------------------- - ext::map < ext::vector < DefaultSymbolType >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > first; - - first[rhsS1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tA, tB - }; - first[rhsS2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tD - }; - first[rhsS3] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tC - }; - first[rhsA1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tA - }; - first[rhsA2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tB - }; - first[rhsB1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tA - }; - first[rhsB2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tB - }; - first[rhsC1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tA - }; - first[rhsC2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tE - }; - first[rhsD1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tC - }; - first[rhsE1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tC - }; - first[rhsE2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - string::Epsilon < >::EPSILON - }; - first[rhsF1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - tE - }; - first[rhsF2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { - string::Epsilon < >::EPSILON - }; - + ext::map < ext::vector < ext::variant < 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 ( { } ); + + first[rhsS1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tA, tB + }; + first[rhsS2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tD + }; + first[rhsS3] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tC + }; + first[rhsA1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tA + }; + first[rhsA2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tB + }; + first[rhsB1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tA + }; + first[rhsB2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tB + }; + first[rhsC1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tA + }; + first[rhsC2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tE + }; + first[rhsD1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tC + }; + first[rhsE1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tC + }; + first[rhsE2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + string::Epsilon < >::EPSILON + }; + first[rhsF1] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + tE + }; + first[rhsF2] = ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > { + string::Epsilon < >::EPSILON + }; + } // -------------------------------------------------- - ext::map < ext::vector < DefaultSymbolType >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > firstAlgo; + ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > firstAlgo; for ( const auto & rule : grammar::RawRules::getRawRules ( grammar ) ) for ( const auto & rhs : rule.second ) @@ -197,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 < DefaultSymbolType >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > res = { { { d }, { d } }, { { A, c }, { d } } }; + ext::map < ext::vector < ext::variant < DefaultSymbolType > >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > res = { { { d }, { d } }, { { A, c }, { d } } }; CPPUNIT_ASSERT ( res == grammar::parsing::First::first ( grammar ) ); } @@ -224,7 +253,7 @@ void FirstTest::testFirst3 ( ) { grammar.addRule ( B, ext::vector < DefaultSymbolType > { f, S } ); grammar.addRule ( B, ext::vector < DefaultSymbolType > { } ); - ext::map < ext::vector < DefaultSymbolType >, ext::set < ext::variant < DefaultSymbolType, string::Epsilon < > > > > res = + ext::map < ext::vector < ext::variant < 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 8806d31612..8bd4c8e8e4 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/LL1ParseTable.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/LL1ParseTable.cpp @@ -31,48 +31,61 @@ void LL1ParseTable::testLL1Table ( ) { grammar.setTerminalAlphabet ( ext::set < DefaultSymbolType > { tP, tS, tL, tR, tA } ); grammar.setNonterminalAlphabet ( ext::set < DefaultSymbolType > { nE, nEp, nT, nTp, nF } ); - ext::vector < DefaultSymbolType > rhsE1 ( { nT, nEp } ); - ext::vector < DefaultSymbolType > rhsEp1 ( { tP, nT, nEp } ); - ext::vector < DefaultSymbolType > rhsEp2 ( { } ); - ext::vector < DefaultSymbolType > rhsT1 ( { nF, nTp } ); - ext::vector < DefaultSymbolType > rhsTp1 ( { tS, nF, nTp } ); - ext::vector < DefaultSymbolType > rhsTp2 ( { } ); - ext::vector < DefaultSymbolType > rhsF1 ( { tA } ); - ext::vector < DefaultSymbolType > rhsF2 ( { tL, nE, tR } ); - - grammar.addRule ( nE, rhsE1 ); - grammar.addRule ( nEp, rhsEp1 ); - grammar.addRule ( nEp, rhsEp2 ); - grammar.addRule ( nT, rhsT1 ); - grammar.addRule ( nTp, rhsTp1 ); - grammar.addRule ( nTp, rhsTp2 ); - grammar.addRule ( nF, rhsF1 ); - grammar.addRule ( nF, rhsF2 ); + { + ext::vector < DefaultSymbolType > rhsE1 ( { nT, nEp } ); + ext::vector < DefaultSymbolType > rhsEp1 ( { tP, nT, nEp } ); + ext::vector < DefaultSymbolType > rhsEp2 ( { } ); + ext::vector < DefaultSymbolType > rhsT1 ( { nF, nTp } ); + ext::vector < DefaultSymbolType > rhsTp1 ( { tS, nF, nTp } ); + ext::vector < DefaultSymbolType > rhsTp2 ( { } ); + ext::vector < DefaultSymbolType > rhsF1 ( { tA } ); + ext::vector < DefaultSymbolType > rhsF2 ( { tL, nE, tR } ); + + grammar.addRule ( nE, rhsE1 ); + grammar.addRule ( nEp, rhsEp1 ); + grammar.addRule ( nEp, rhsEp2 ); + grammar.addRule ( nT, rhsT1 ); + grammar.addRule ( nTp, rhsTp1 ); + grammar.addRule ( nTp, rhsTp2 ); + grammar.addRule ( nF, rhsF1 ); + grammar.addRule ( nF, rhsF2 ); + } // -------------------------------------------------- - ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < DefaultSymbolType > > > parseTable; + ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < DefaultSymbolType > > > > parseTable; - parseTable[ext::make_pair ( tA, nE )].insert ( rhsE1 ); - parseTable[ext::make_pair ( tL, nE )].insert ( rhsE1 ); + { + 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 } ); - parseTable[ext::make_pair ( tP, nEp )].insert ( rhsEp1 ); - parseTable[ext::make_pair ( tR, nEp )].insert ( rhsEp2 ); - parseTable[ext::make_pair ( string::Epsilon < >::EPSILON, nEp )].insert ( rhsEp2 ); + parseTable[ext::make_pair ( tA, nE )].insert ( rhsE1 ); + parseTable[ext::make_pair ( tL, nE )].insert ( rhsE1 ); - parseTable[ext::make_pair ( tA, nT )].insert ( rhsT1 ); - parseTable[ext::make_pair ( tL, nT )].insert ( rhsT1 ); + parseTable[ext::make_pair ( tP, nEp )].insert ( rhsEp1 ); + parseTable[ext::make_pair ( tR, nEp )].insert ( rhsEp2 ); + parseTable[ext::make_pair ( string::Epsilon < >::EPSILON, nEp )].insert ( rhsEp2 ); - parseTable[ext::make_pair ( tS, nTp )].insert ( rhsTp1 ); - parseTable[ext::make_pair ( tP, nTp )].insert ( rhsTp2 ); - parseTable[ext::make_pair ( tR, nTp )].insert ( rhsTp2 ); - parseTable[ext::make_pair ( string::Epsilon < >::EPSILON, nTp )].insert ( rhsTp2 ); + parseTable[ext::make_pair ( tA, nT )].insert ( rhsT1 ); + parseTable[ext::make_pair ( tL, nT )].insert ( rhsT1 ); - parseTable[ext::make_pair ( tA, nF )].insert ( rhsF1 ); - parseTable[ext::make_pair ( tL, nF )].insert ( rhsF2 ); + parseTable[ext::make_pair ( tS, nTp )].insert ( rhsTp1 ); + parseTable[ext::make_pair ( tP, nTp )].insert ( rhsTp2 ); + parseTable[ext::make_pair ( tR, nTp )].insert ( rhsTp2 ); + parseTable[ext::make_pair ( string::Epsilon < >::EPSILON, nTp )].insert ( rhsTp2 ); + + parseTable[ext::make_pair ( tA, nF )].insert ( rhsF1 ); + parseTable[ext::make_pair ( tL, nF )].insert ( rhsF2 ); + } // -------------------------------------------------- - ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < DefaultSymbolType > > > parseTableAlgo = grammar::parsing::LL1ParseTable::parseTable ( grammar ); + ext::map < ext::pair < ext::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, ext::set < ext::vector < ext::variant < 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 5c0bef8808..d252ab35a9 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 < DefaultSymbolType > > ( AddRawRule::addRawRule ); -auto AddRawRuleEpsilonFreeCFG = registration::AbstractRegister < AddRawRule, bool, grammar::EpsilonFreeCFG < > &, DefaultSymbolType, ext::vector < DefaultSymbolType > > ( AddRawRule::addRawRule ); -auto AddRawRuleGNF = registration::AbstractRegister < AddRawRule, bool, grammar::GNF < > &, DefaultSymbolType, ext::vector < DefaultSymbolType > > ( AddRawRule::addRawRule ); -auto AddRawRuleCNF = registration::AbstractRegister < AddRawRule, bool, grammar::CNF < > &, DefaultSymbolType, ext::vector < DefaultSymbolType > > ( AddRawRule::addRawRule ); -auto AddRawRuleLG = registration::AbstractRegister < AddRawRule, bool, grammar::LG < > &, DefaultSymbolType, ext::vector < DefaultSymbolType > > ( AddRawRule::addRawRule ); -auto AddRawRuleLeftLG = registration::AbstractRegister < AddRawRule, bool, grammar::LeftLG < > &, DefaultSymbolType, ext::vector < DefaultSymbolType > > ( AddRawRule::addRawRule ); -auto AddRawRuleLeftRG = registration::AbstractRegister < AddRawRule, bool, grammar::LeftRG < > &, DefaultSymbolType, ext::vector < DefaultSymbolType > > ( AddRawRule::addRawRule ); -auto AddRawRuleRightLG = registration::AbstractRegister < AddRawRule, bool, grammar::RightLG < > &, DefaultSymbolType, ext::vector < DefaultSymbolType > > ( AddRawRule::addRawRule ); -auto AddRawRuleRightRG = registration::AbstractRegister < AddRawRule, bool, grammar::RightRG < > &, DefaultSymbolType, ext::vector < DefaultSymbolType > > ( AddRawRule::addRawRule ); +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 ); } /* namespace grammar */ diff --git a/alib2data/src/grammar/AddRawRule.h b/alib2data/src/grammar/AddRawRule.h index 30d110caf6..b6ec343315 100644 --- a/alib2data/src/grammar/AddRawRule.h +++ b/alib2data/src/grammar/AddRawRule.h @@ -39,72 +39,86 @@ public: * \returns rules of the grammar in a common representation */ template < class SymbolType > - static bool addRawRule ( LG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ); + static bool addRawRule ( LG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); /** * \override */ template < class SymbolType > - static bool addRawRule ( GNF < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ); + static bool addRawRule ( GNF < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); /** * \override */ template < class SymbolType > - static bool addRawRule ( EpsilonFreeCFG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ); + static bool addRawRule ( EpsilonFreeCFG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); /** * \override */ template < class SymbolType > - static bool addRawRule ( CNF < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ); + static bool addRawRule ( CNF < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); /** * \override */ template < class SymbolType > - static bool addRawRule ( CFG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ); + static bool addRawRule ( CFG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); /** * \override */ template < class SymbolType > - static bool addRawRule ( LeftLG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ); + static bool addRawRule ( LeftLG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); /** * \override */ template < class SymbolType > - static bool addRawRule ( LeftRG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ); + static bool addRawRule ( LeftRG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); /** * \override */ template < class SymbolType > - static bool addRawRule ( RightLG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ); + static bool addRawRule ( RightLG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); /** * \override */ template < class SymbolType > - static bool addRawRule ( RightRG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ); + static bool addRawRule ( RightRG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ); }; template < class SymbolType > -bool AddRawRule::addRawRule ( LG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ) { - typename ext::vector < SymbolType >::iterator nonterminalPosition = rightHandSide.begin ( ); +bool AddRawRule::addRawRule ( LG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { + typename ext::vector < ext::variant < SymbolType > >::iterator nonterminalPosition = rightHandSide.begin ( ); for ( ; nonterminalPosition != rightHandSide.end ( ); ++nonterminalPosition ) - if ( grammar.getNonterminalAlphabet ( ).count ( * nonterminalPosition ) ) break; + if ( grammar.getNonterminalAlphabet ( ).count ( nonterminalPosition->template get < SymbolType > ( ) ) ) break; - if ( nonterminalPosition == rightHandSide.end ( ) ) - return grammar.addRule ( leftHandSide, rightHandSide ); - else - return grammar.addRule ( leftHandSide, ext::make_tuple ( ext::vector < SymbolType > ( std::make_move_iterator ( rightHandSide.begin ( ) ), std::make_move_iterator ( nonterminalPosition ) ), std::move ( * nonterminalPosition ), ext::vector < SymbolType > ( std::make_move_iterator ( nonterminalPosition ) + 1, std::make_move_iterator ( rightHandSide.end ( ) ) ) ) ); + if ( nonterminalPosition == rightHandSide.end ( ) ) { + ext::vector < SymbolType > rhs; + + for ( ext::variant < SymbolType > & symbol : rightHandSide ) + rhs.push_back ( std::move ( symbol.template get < SymbolType > ( ) ) ); + + return grammar.addRule ( std::move ( leftHandSide ), std::move ( rhs ) ); + } else { + ext::vector < SymbolType > rhs1; + ext::vector < SymbolType > 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 > ( ) ) ); + + return grammar.addRule ( leftHandSide, ext::make_tuple ( std::move ( rhs1 ), std::move ( nonterminalPosition->template get < SymbolType > ( ) ), std::move ( rhs2 ) ) ); + } } template < class SymbolType > -bool AddRawRule::addRawRule ( GNF < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ) { +bool AddRawRule::addRawRule ( GNF < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { if ( rightHandSide.size ( ) == 0 ) { if ( leftHandSide != grammar.getInitialSymbol ( ) ) throw GrammarException ( "Illegal left hand side of epsilon rule" ); @@ -113,14 +127,18 @@ bool AddRawRule::addRawRule ( GNF < SymbolType > & grammar, SymbolType leftHandS grammar.setGeneratesEpsilon ( true ); return !res; } else { - SymbolType first = std::move ( rightHandSide[0] ); - ext::vector < SymbolType > rest ( std::make_move_iterator ( rightHandSide.begin ( ) ) + 1, std::make_move_iterator ( rightHandSide.end ( ) ) ); + SymbolType first = std::move ( rightHandSide[0].template get < SymbolType > ( ) ); + + 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 > ( ) ) ); + 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 < SymbolType > rightHandSide ) { +bool AddRawRule::addRawRule ( EpsilonFreeCFG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { if ( rightHandSide.size ( ) == 0 ) { if ( leftHandSide != grammar.getInitialSymbol ( ) ) throw GrammarException ( "Illegal left hand side of epsilon rule" ); @@ -129,12 +147,16 @@ bool AddRawRule::addRawRule ( EpsilonFreeCFG < SymbolType > & grammar, SymbolTyp grammar.setGeneratesEpsilon ( true ); return ! res; } else { - return grammar.addRule ( std::move ( leftHandSide ), std::move ( rightHandSide ) ); + 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 > ( ) ) ); + + return grammar.addRule ( std::move ( leftHandSide ), std::move ( rhs ) ); } } template < class SymbolType > -bool AddRawRule::addRawRule ( CNF < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ) { +bool AddRawRule::addRawRule ( CNF < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { if ( rightHandSide.size ( ) == 0 ) { if ( leftHandSide != grammar.getInitialSymbol ( ) ) throw GrammarException ( "Illegal left hand side of epsilon rule" ); @@ -143,31 +165,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] ) ); + return grammar.addRule ( std::move ( leftHandSide ), std::move ( rightHandSide[0].template get < SymbolType > ( ) ) ); } else if ( rightHandSide.size ( ) == 2 ) { - return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( rightHandSide[0] ), std::move ( rightHandSide[1] ) ) ); + return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( rightHandSide[0].template get < SymbolType > ( ) ), std::move ( rightHandSide[1].template get < SymbolType > ( ) ) ) ); } else { throw GrammarException ( "Invalid right hand side" ); } } template < class SymbolType > -bool AddRawRule::addRawRule ( CFG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ) { - return grammar.addRule ( std::move ( leftHandSide ), std::move ( rightHandSide ) ); +bool AddRawRule::addRawRule ( CFG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { + 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 > ( ) ) ); + + return grammar.addRule ( std::move ( leftHandSide ), std::move ( rhs ) ); } template < class SymbolType > -bool AddRawRule::addRawRule ( LeftLG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ) { +bool AddRawRule::addRawRule ( LeftLG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { if ( rightHandSide.size ( ) == 0 ) - return grammar.addRule ( std::move ( leftHandSide ), std::move ( rightHandSide ) ); - else if ( grammar.getNonterminalAlphabet ( ).count ( rightHandSide[0] ) ) - return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( rightHandSide[0] ), ext::vector < SymbolType > ( std::make_move_iterator ( rightHandSide.begin ( ) ), std::make_move_iterator ( rightHandSide.end ( ) ) ) ) ); - else - return grammar.addRule ( std::move ( leftHandSide ), std::move ( rightHandSide ) ); + 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::make_pair ( std::move ( rightHandSide[0].template get < SymbolType > ( ) ), 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 > ( ) ) ); + + return grammar.addRule ( std::move ( leftHandSide ), std::move ( rhs ) ); + } } template < class SymbolType > -bool AddRawRule::addRawRule ( LeftRG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ) { +bool AddRawRule::addRawRule ( LeftRG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { if ( rightHandSide.size ( ) == 0 ) { if ( leftHandSide != grammar.getInitialSymbol ( ) ) throw GrammarException ( "Illegal left hand side of epsilon rule" ); @@ -176,16 +211,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] ) ); + return grammar.addRule ( std::move ( leftHandSide ), std::move ( rightHandSide[0].template get < SymbolType > ( ) ) ); } else if ( rightHandSide.size ( ) == 2 ) { - return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( rightHandSide[0] ), std::move ( rightHandSide[1] ) ) ); + return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( rightHandSide[0].template get < SymbolType > ( ) ), std::move ( rightHandSide[1].template get < SymbolType > ( ) ) ) ); } else { throw GrammarException ( "Invalid right hand side" ); } } template < class SymbolType > -bool AddRawRule::addRawRule ( RightRG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ) { +bool AddRawRule::addRawRule ( RightRG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { if ( rightHandSide.size ( ) == 0 ) { if ( leftHandSide != grammar.getInitialSymbol ( ) ) throw GrammarException ( "Illegal left hand side of epsilon rule" ); @@ -194,22 +229,31 @@ 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] ) ); + return grammar.addRule ( std::move ( leftHandSide ), std::move ( rightHandSide[0].template get < SymbolType > ( ) ) ); } else if ( rightHandSide.size ( ) == 2 ) { - return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( rightHandSide[0] ), std::move ( rightHandSide[1] ) ) ); + return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( std::move ( rightHandSide[0].template get < SymbolType > ( ) ), std::move ( rightHandSide[1].template get < SymbolType > ( ) ) ) ); } else { throw GrammarException ( "Invalid right hand side" ); } } template < class SymbolType > -bool AddRawRule::addRawRule ( RightLG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < SymbolType > rightHandSide ) { +bool AddRawRule::addRawRule ( RightLG < SymbolType > & grammar, SymbolType leftHandSide, ext::vector < ext::variant < SymbolType > > rightHandSide ) { if ( rightHandSide.size ( ) == 0 ) - return grammar.addRule ( std::move ( leftHandSide ), std::move ( rightHandSide ) ); - else if ( grammar.getNonterminalAlphabet ( ).count ( rightHandSide[rightHandSide.size ( ) - 1] ) ) - return grammar.addRule ( std::move ( leftHandSide ), ext::make_pair ( ext::vector < SymbolType > ( std::make_move_iterator ( rightHandSide.begin ( ) ), std::make_move_iterator ( rightHandSide.end ( ) ) - 1 ), std::move ( rightHandSide[rightHandSide.size ( ) - 1] ) ) ); - else - return grammar.addRule ( std::move ( leftHandSide ), std::move ( rightHandSide ) ); + 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::make_pair ( std::move ( rhs ), std::move ( rightHandSide[rightHandSide.size ( ) - 1].template get < SymbolType > ( ) ) ) ); + } 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 > ( ) ) ); + + return grammar.addRule ( std::move ( leftHandSide ), std::move ( rhs ) ); + } } } /* namespace grammar */ diff --git a/alib2data/src/grammar/RawRules.cpp b/alib2data/src/grammar/RawRules.cpp index 55f86cb52c..07f8929e01 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 < DefaultSymbolType > > >, const grammar::CFG < > & > ( RawRules::getRawRules ); -auto RawRulesEpsilonFreeCFG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < DefaultSymbolType > > >, const grammar::EpsilonFreeCFG < > & > ( RawRules::getRawRules ); -auto RawRulesGNF = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < DefaultSymbolType > > >, const grammar::GNF < > & > ( RawRules::getRawRules ); -auto RawRulesCNF = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < DefaultSymbolType > > >, const grammar::CNF < > & > ( RawRules::getRawRules ); -auto RawRulesLG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < DefaultSymbolType > > >, const grammar::LG < > & > ( RawRules::getRawRules ); -auto RawRulesLeftLG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < DefaultSymbolType > > >, const grammar::LeftLG < > & > ( RawRules::getRawRules ); -auto RawRulesLeftRG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < DefaultSymbolType > > >, const grammar::LeftRG < > & > ( RawRules::getRawRules ); -auto RawRulesRightLG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < DefaultSymbolType > > >, const grammar::RightLG < > & > ( RawRules::getRawRules ); -auto RawRulesRightRG = registration::AbstractRegister < RawRules, ext::map < DefaultSymbolType, ext::set < ext::vector < DefaultSymbolType > > >, const grammar::RightRG < > & > ( RawRules::getRawRules ); +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 ); } /* namespace grammar */ diff --git a/alib2data/src/grammar/RawRules.h b/alib2data/src/grammar/RawRules.h index 9cb29cbd88..122775e95d 100644 --- a/alib2data/src/grammar/RawRules.h +++ b/alib2data/src/grammar/RawRules.h @@ -39,208 +39,259 @@ public: * \returns rules of the grammar in a common representation */ template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > getRawRules ( const LG < SymbolType > & grammar ); + static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const LG < SymbolType > & grammar ); /** * \override */ template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > getRawRules ( const GNF < SymbolType > & grammar ); + static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const GNF < SymbolType > & grammar ); /** * \override */ template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > getRawRules ( const EpsilonFreeCFG < SymbolType > & grammar ); + static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const EpsilonFreeCFG < SymbolType > & grammar ); /** * \override */ template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > getRawRules ( const CNF < SymbolType > & grammar ); + static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const CNF < SymbolType > & grammar ); /** * \override */ template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > getRawRules ( const CFG < SymbolType > & grammar ); + static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const CFG < SymbolType > & grammar ); /** * \override */ template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > getRawRules ( const LeftLG < SymbolType > & grammar ); + static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const LeftLG < SymbolType > & grammar ); /** * \override */ template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > getRawRules ( const LeftRG < SymbolType > & grammar ); + static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const LeftRG < SymbolType > & grammar ); /** * \override */ template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > getRawRules ( const RightLG < SymbolType > & grammar ); + static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const RightLG < SymbolType > & grammar ); /** * \override */ template < class SymbolType > - static ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > getRawRules ( const RightRG < SymbolType > & grammar ); + static ext::map < SymbolType, ext::set < ext::vector < ext::variant < SymbolType > > > > getRawRules ( const RightRG < SymbolType > & grammar ); }; template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > RawRules::getRawRules ( const LG < SymbolType > & grammar ) { - ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > res; +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; for ( const auto & rule : grammar.getRules ( ) ) for ( const auto & rhs : rule.second ) { + ext::vector < ext::variant < SymbolType > > tmp; + if ( rhs.template is < ext::vector < SymbolType > > ( ) ) { - res[rule.first].insert ( rhs.template get < ext::vector < SymbolType > > ( ) ); + for ( const SymbolType & symbol : rhs.template get < ext::vector < SymbolType > > ( ) ) + tmp.push_back ( ext::variant < SymbolType > ( symbol ) ); } else { const auto & rhsTuple = rhs.template get < ext::tuple < ext::vector < SymbolType >, SymbolType, ext::vector < SymbolType > > > ( ); - ext::vector < SymbolType > tmp { std::get < 0 > ( rhsTuple ).begin ( ), std::get < 0 > ( rhsTuple ).end ( ) }; - tmp.push_back ( std::get < 1 > ( rhsTuple ) ); - tmp.insert ( tmp.end ( ), std::get < 2 > ( rhsTuple ).begin ( ), std::get < 2 > ( rhsTuple ).end ( ) ); - res[rule.first].insert ( std::move ( tmp ) ); + + 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 ) ); + + tmp.push_back ( ext::variant < SymbolType > ( 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 ) ); } + + res[rule.first].insert ( std::move ( tmp ) ); } return res; } template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > RawRules::getRawRules ( const GNF < SymbolType > & grammar ) { - ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > res; +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; for ( const auto & rule : grammar.getRules ( ) ) for ( const auto & rhs : rule.second ) { - ext::vector < SymbolType > tmp { rhs.first }; - tmp.insert ( tmp.end ( ), rhs.second.begin ( ), rhs.second.end ( ) ); + ext::vector < ext::variant < SymbolType > > tmp; + tmp.push_back ( ext::variant < SymbolType > ( rhs.first ) ); + + for ( const SymbolType & symbol : ext::make_iterator_range ( rhs.second.begin ( ), rhs.second.end ( ) ) ) + tmp.push_back ( ext::variant < SymbolType > ( symbol ) ); + res[rule.first].insert ( std::move ( tmp ) ); } if ( grammar.getGeneratesEpsilon ( ) ) - res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < SymbolType > { } ); + res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < SymbolType > > { } ); return res; } template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > RawRules::getRawRules ( const EpsilonFreeCFG < SymbolType > & grammar ) { - ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > res ( grammar.getRules ( ) ); +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; + + for ( const std::pair < SymbolType, ext::set < ext::vector < SymbolType > > > & rule : grammar.getRules ( ) ) { + ext::set < ext::vector < ext::variant < SymbolType > > > resRHSSet; + for ( const ext::vector < SymbolType > & rhs : rule.second ) { + ext::vector < ext::variant < SymbolType > > resRHS; + for ( const SymbolType & symbol : rhs ) { + resRHS.push_back ( ext::variant < SymbolType > ( symbol ) ); + } + resRHSSet.insert ( std::move ( resRHS ) ); + } + res.insert ( std::make_pair ( rule.first, std::move ( resRHSSet ) ) ); + } if ( grammar.getGeneratesEpsilon ( ) ) - res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < SymbolType > { } ); + res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < SymbolType > > { } ); return res; } template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > RawRules::getRawRules ( const CNF < SymbolType > & grammar ) { - ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > res; +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; for ( const auto & rule : grammar.getRules ( ) ) for ( const auto & rhs : rule.second ) { if ( rhs.template is < SymbolType > ( ) ) { - ext::vector < SymbolType > tmp { rhs.template get < SymbolType > ( ) }; + ext::vector < ext::variant < SymbolType > > tmp { ext::variant < SymbolType > ( rhs.template get < SymbolType > ( ) ) }; res[rule.first].insert ( std::move ( tmp ) ); } else { const auto & realRHS = rhs.template get < ext::pair < SymbolType, SymbolType > > ( ); - ext::vector < SymbolType > tmp { realRHS.first, realRHS.second }; + ext::vector < ext::variant < SymbolType > > tmp { ext::variant < SymbolType > ( realRHS.first ), ext::variant < SymbolType > ( realRHS.second ) }; res[rule.first].insert ( std::move ( tmp ) ); } } if ( grammar.getGeneratesEpsilon ( ) ) - res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < SymbolType > { } ); + res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < SymbolType > > { } ); return res; } template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > RawRules::getRawRules ( const CFG < SymbolType > & grammar ) { - return grammar.getRules ( ); +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; + + for ( const std::pair < SymbolType, ext::set < ext::vector < SymbolType > > > & rule : grammar.getRules ( ) ) { + ext::set < ext::vector < ext::variant < SymbolType > > > resRHSSet; + for ( const ext::vector < SymbolType > & rhs : rule.second ) { + ext::vector < ext::variant < SymbolType > > resRHS; + for ( const SymbolType & symbol : rhs ) { + resRHS.push_back ( ext::variant < SymbolType > ( symbol ) ); + } + resRHSSet.insert ( std::move ( resRHS ) ); + } + res.insert ( std::make_pair ( rule.first, std::move ( resRHSSet ) ) ); + } + + return res; } template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > RawRules::getRawRules ( const LeftLG < SymbolType > & grammar ) { - ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > res; +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; for ( const auto & rule : grammar.getRules ( ) ) for ( const auto & rhs : rule.second ) { + ext::vector < ext::variant < SymbolType > > tmp; + if ( rhs.template is < ext::vector < SymbolType > > ( ) ) { - res[rule.first].insert ( rhs.template get < ext::vector < SymbolType > > ( ) ); + for ( const SymbolType & symbol : rhs.template get < ext::vector < SymbolType > > ( ) ) + tmp.push_back ( ext::variant < SymbolType > ( symbol ) ); } else { const auto & rhsTuple = rhs.template get < ext::pair < SymbolType, ext::vector < SymbolType > > > ( ); - ext::vector < SymbolType > tmp { rhsTuple.first }; - tmp.insert ( tmp.end ( ), rhsTuple.second.begin ( ), rhsTuple.second.end ( ) ); - res[rule.first].insert ( std::move ( tmp ) ); + + 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 ) ); } + + res[rule.first].insert ( std::move ( tmp ) ); } return res; } template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > RawRules::getRawRules ( const LeftRG < SymbolType > & grammar ) { - ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > res; +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; for ( const auto & rule : grammar.getRules ( ) ) for ( const auto & rhs : rule.second ) { if ( rhs.template is < SymbolType > ( ) ) { - ext::vector < SymbolType > tmp { rhs.template get < SymbolType > ( ) }; + ext::vector < ext::variant < SymbolType > > tmp { ext::variant < SymbolType > ( rhs.template get < SymbolType > ( ) ) }; res[rule.first].insert ( std::move ( tmp ) ); } else { const auto & rhsPair = rhs.template get < ext::pair < SymbolType, SymbolType > > ( ); - ext::vector < SymbolType > tmp { rhsPair.first, rhsPair.second }; + ext::vector < ext::variant < SymbolType > > tmp { ext::variant < SymbolType > ( rhsPair.first ), ext::variant < SymbolType > ( rhsPair.second ) }; res[rule.first].insert ( std::move ( tmp ) ); } } if ( grammar.getGeneratesEpsilon ( ) ) - res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < SymbolType > { } ); + res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < SymbolType > > { } ); return res; } template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > RawRules::getRawRules ( const RightRG < SymbolType > & grammar ) { - ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > res; +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; for ( const auto & rule : grammar.getRules ( ) ) for ( const auto & rhs : rule.second ) { if ( rhs.template is < SymbolType > ( ) ) { - ext::vector < SymbolType > tmp { rhs.template get < SymbolType > ( ) }; + ext::vector < ext::variant < SymbolType > > tmp { ext::variant < SymbolType > ( rhs.template get < SymbolType > ( ) ) }; res[rule.first].insert ( std::move ( tmp ) ); } else { const auto & rhsPair = rhs.template get < ext::pair < SymbolType, SymbolType > > ( ); - ext::vector < SymbolType > tmp { rhsPair.first, rhsPair.second }; + ext::vector < ext::variant < SymbolType > > tmp { ext::variant < SymbolType > ( rhsPair.first ), ext::variant < SymbolType > ( rhsPair.second ) }; res[rule.first].insert ( std::move ( tmp ) ); } } if ( grammar.getGeneratesEpsilon ( ) ) - res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < SymbolType > { } ); + res [ grammar.getInitialSymbol ( ) ].insert ( ext::vector < ext::variant < SymbolType > > { } ); return res; } template < class SymbolType > -ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > RawRules::getRawRules ( const RightLG < SymbolType > & grammar ) { - ext::map < SymbolType, ext::set < ext::vector < SymbolType > > > res; +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; for ( const auto & rule : grammar.getRules ( ) ) for ( const auto & rhs : rule.second ) { + ext::vector < ext::variant < SymbolType > > tmp; + if ( rhs.template is < ext::vector < SymbolType > > ( ) ) { - res[rule.first].insert ( rhs.template get < ext::vector < SymbolType > > ( ) ); + for ( const SymbolType & symbol : rhs.template get < ext::vector < SymbolType > > ( ) ) + tmp.push_back ( ext::variant < SymbolType > ( symbol ) ); } else { const auto & rhsTuple = rhs.template get < ext::pair < ext::vector < SymbolType >, SymbolType > > ( ); - ext::vector < SymbolType > tmp { rhsTuple.first.begin ( ), rhsTuple.first.end ( ) }; - tmp.push_back ( rhsTuple.second ); - res[rule.first].insert ( std::move ( tmp ) ); + + 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 ) ); } + + res[rule.first].insert ( std::move ( tmp ) ); } return res; diff --git a/alib2data/src/grammar/xml/ContextFree/LG.h b/alib2data/src/grammar/xml/ContextFree/LG.h index 08be7de726..f6f89acb9f 100644 --- a/alib2data/src/grammar/xml/ContextFree/LG.h +++ b/alib2data/src/grammar/xml/ContextFree/LG.h @@ -29,6 +29,8 @@ #include "../common/GrammarToXMLComposer.h" #include <grammar/AddRawRule.h> +#include <container/xml/ObjectsVariant.h> + namespace core { template < class SymbolType > @@ -113,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 < SymbolType > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < SymbolType > ( input ); + ext::vector < ext::variant < SymbolType > > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < ext::variant < 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 87a3628ae7..966f4ec9f6 100644 --- a/alib2data/src/grammar/xml/Regular/LeftLG.h +++ b/alib2data/src/grammar/xml/Regular/LeftLG.h @@ -30,6 +30,8 @@ #include <grammar/AddRawRule.h> +#include <container/xml/ObjectsVariant.h> + namespace core { template < class SymbolType > @@ -114,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 < SymbolType > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < SymbolType > ( input ); + ext::vector < ext::variant < SymbolType > > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < ext::variant < 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 347ab204aa..d2ce1bddb4 100644 --- a/alib2data/src/grammar/xml/Regular/RightLG.h +++ b/alib2data/src/grammar/xml/Regular/RightLG.h @@ -30,6 +30,8 @@ #include <grammar/AddRawRule.h> +#include <container/xml/ObjectsVariant.h> + namespace core { template < class SymbolType > @@ -114,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 < SymbolType > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < SymbolType > ( input ); + ext::vector < ext::variant < SymbolType > > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < ext::variant < 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 bc40f61ead..2d50711678 100644 --- a/alib2str/src/grammar/string/common/GrammarFromStringParserCommon.h +++ b/alib2str/src/grammar/string/common/GrammarFromStringParserCommon.h @@ -184,7 +184,12 @@ T GrammarFromStringParserCommon::parseCFLikeGrammar(std::istream& input) { T grammar(nonterminals, terminals, initialSymbol); for(const auto& rule : rules) { for(const auto& ruleRHS : rule.second) { - grammar::AddRawRule::addRawRule(grammar,rule.first, ruleRHS); + ext::vector < ext::variant < SymbolType > > rhs; + + for ( const SymbolType & symbol : ruleRHS ) + rhs.push_back ( symbol ); + + grammar::AddRawRule::addRawRule ( grammar, rule.first, rhs ); } } return grammar; diff --git a/alib2str/src/grammar/string/common/GrammarToStringComposerCommon.h b/alib2str/src/grammar/string/common/GrammarToStringComposerCommon.h index 2a836af142..21cfaaa392 100644 --- a/alib2str/src/grammar/string/common/GrammarToStringComposerCommon.h +++ b/alib2str/src/grammar/string/common/GrammarToStringComposerCommon.h @@ -13,6 +13,7 @@ #include <core/stringApi.hpp> #include <grammar/RawRules.h> +#include <container/string/ObjectsVariant.h> namespace grammar { @@ -70,7 +71,7 @@ void GrammarToStringComposerCommon::composeCFLikeGrammar(std::ostream& output, c output << " |"; for(const auto& symbol : rhs) { output << " "; - core::stringApi < SymbolType >::compose(output, symbol); + core::stringApi < ext::variant < SymbolType > >::compose(output, symbol); } } } -- GitLab