From 80717c8d788c1ae0e63d26cbbfd4b2261a407c5f Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Wed, 19 Sep 2018 15:17:37 +0200 Subject: [PATCH] continue in adaptation to grammar templating changes --- .../properties/NonterminalUnitRuleCycle.h | 18 ++--- .../src/grammar/simplify/EpsilonRemover.h | 68 +++++++++---------- .../grammar/simplify/LeftRecursionRemover.h | 56 +++++++-------- 3 files changed, 71 insertions(+), 71 deletions(-) diff --git a/alib2algo/src/grammar/properties/NonterminalUnitRuleCycle.h b/alib2algo/src/grammar/properties/NonterminalUnitRuleCycle.h index c8e0c729e3..9561f25272 100644 --- a/alib2algo/src/grammar/properties/NonterminalUnitRuleCycle.h +++ b/alib2algo/src/grammar/properties/NonterminalUnitRuleCycle.h @@ -33,30 +33,30 @@ public: * @param nonterminal nonterminal * @return set of nonterminals for which we can be derived from giveUnitRuleCyclen nonterminals in finite number of steps */ - template<class T, class SymbolType> - static ext::set<SymbolType> getNonterminalUnitRuleCycle(const T& grammar, const SymbolType& nonterminal); + template < class T, class NonterminalSymbolType > + static ext::set < NonterminalSymbolType > getNonterminalUnitRuleCycle ( const T& grammar, const NonterminalSymbolType& nonterminal); }; -template<class T, class SymbolType > -ext::set<SymbolType> NonterminalUnitRuleCycle::getNonterminalUnitRuleCycle(const T& grammar, const SymbolType& nonterminal) { +template<class T, class NonterminalSymbolType > +ext::set<NonterminalSymbolType> NonterminalUnitRuleCycle::getNonterminalUnitRuleCycle(const T& grammar, const NonterminalSymbolType& nonterminal) { if(grammar.getNonterminalAlphabet().count(nonterminal) == 0) { throw exception::CommonException("Nonterminal symbol \"" + ext::to_string ( nonterminal ) + "\" is not present in grammar."); } auto rawRules = grammar::RawRules::getRawRules ( grammar ); - ext::deque<ext::set<SymbolType>> Ni; - Ni.push_back(ext::set<SymbolType>{nonterminal}); + ext::deque<ext::set<NonterminalSymbolType>> Ni; + Ni.push_back(ext::set<NonterminalSymbolType>{nonterminal}); int i = 1; while(true) { Ni.push_back(Ni.at(i-1)); for ( const auto & rule : rawRules ) { - const SymbolType& lhs = rule.first; + const NonterminalSymbolType& lhs = rule.first; for(const auto& rhs : rule.second) { - 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 > ( ) ); + if(Ni.at(i-1).count(lhs) && rhs.size() == 1 && rhs.front ( ).template is < NonterminalSymbolType > ( ) && grammar.getNonterminalAlphabet().count(rhs.front().template get < NonterminalSymbolType > ( ) )) { + Ni.at(i).insert(rhs.front().template get < NonterminalSymbolType > ( ) ); } } } diff --git a/alib2algo/src/grammar/simplify/EpsilonRemover.h b/alib2algo/src/grammar/simplify/EpsilonRemover.h index 11d020ef20..46ae389fc9 100644 --- a/alib2algo/src/grammar/simplify/EpsilonRemover.h +++ b/alib2algo/src/grammar/simplify/EpsilonRemover.h @@ -33,33 +33,33 @@ namespace simplify { class EpsilonRemover { template < class TerminalSymbolType, class NonterminalSymbolType > - static void removeNullableNonterminals(grammar::EpsilonFreeCFG < TerminalSymbolType > & grammar, const ext::set < NonterminalSymbolType >& nullableNonterminals, const NonterminalSymbolType & lhs, const ext::vector<ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs, unsigned i, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > clear); + static void removeNullableNonterminals(grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > & grammar, const ext::set < NonterminalSymbolType >& nullableNonterminals, const NonterminalSymbolType & lhs, const ext::vector<ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs, unsigned i, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > clear); template<class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > > static grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > removeInternal( const T & origGrammar ); public: template < class TerminalSymbolType, class NonterminalSymbolType > static grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > remove( const grammar::CFG < TerminalSymbolType, NonterminalSymbolType > & grammar ); - template < class SymbolType > - static grammar::EpsilonFreeCFG < SymbolType > remove( const grammar::EpsilonFreeCFG < SymbolType > & grammar ); - template < class SymbolType > - static grammar::GNF < SymbolType > remove( const grammar::GNF < SymbolType > & grammar ); - template < class SymbolType > - static grammar::CNF < SymbolType > remove( const grammar::CNF < SymbolType > & grammar ); - template < class SymbolType > - static grammar::EpsilonFreeCFG < SymbolType > remove( const grammar::LG < SymbolType > & grammar ); - template < class SymbolType > - static grammar::EpsilonFreeCFG < SymbolType > remove( const grammar::LeftLG < SymbolType > & grammar ); - template < class SymbolType > - static grammar::LeftRG < SymbolType > remove( const grammar::LeftRG < SymbolType > & grammar ); - template < class SymbolType > - static grammar::EpsilonFreeCFG < SymbolType > remove( const grammar::RightLG < SymbolType > & grammar ); - template < class SymbolType > - static grammar::RightRG < SymbolType > remove( const grammar::RightRG < SymbolType > & grammar ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > remove( const grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > & grammar ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static grammar::GNF < TerminalSymbolType, NonterminalSymbolType > remove( const grammar::GNF < TerminalSymbolType, NonterminalSymbolType > & grammar ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static grammar::CNF < TerminalSymbolType, NonterminalSymbolType > remove( const grammar::CNF < TerminalSymbolType, NonterminalSymbolType > & grammar ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > remove( const grammar::LG < TerminalSymbolType, NonterminalSymbolType > & grammar ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > remove( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > remove( const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > remove( const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static grammar::RightRG < TerminalSymbolType, NonterminalSymbolType > remove( const grammar::RightRG < TerminalSymbolType, NonterminalSymbolType > & grammar ); }; template < class TerminalSymbolType, class NonterminalSymbolType > -void EpsilonRemover::removeNullableNonterminals(grammar::EpsilonFreeCFG < TerminalSymbolType > & grammar, const ext::set < NonterminalSymbolType > & nullableNonterminals, const NonterminalSymbolType & lhs, const ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs, unsigned i, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > clear) { +void EpsilonRemover::removeNullableNonterminals(grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > & grammar, const ext::set < NonterminalSymbolType > & nullableNonterminals, const NonterminalSymbolType & lhs, const ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > & rhs, unsigned i, ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > clear) { if(rhs.size() == i) { if(clear.size() == 0) return; grammar.addRule(lhs, clear); @@ -106,43 +106,43 @@ grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > EpsilonRem return EpsilonRemover::removeInternal(origGrammar); } -template < class SymbolType > -grammar::EpsilonFreeCFG < SymbolType > EpsilonRemover::remove(const grammar::EpsilonFreeCFG < SymbolType > & origGrammar) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > EpsilonRemover::remove(const grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > & origGrammar) { return origGrammar; } -template < class SymbolType > -grammar::CNF < SymbolType > EpsilonRemover::remove(const grammar::CNF < SymbolType > & origGrammar) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::CNF < TerminalSymbolType, NonterminalSymbolType > EpsilonRemover::remove(const grammar::CNF < TerminalSymbolType, NonterminalSymbolType > & origGrammar) { return origGrammar; } -template < class SymbolType > -grammar::GNF < SymbolType > EpsilonRemover::remove(const grammar::GNF < SymbolType > & origGrammar) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::GNF < TerminalSymbolType, NonterminalSymbolType > EpsilonRemover::remove(const grammar::GNF < TerminalSymbolType, NonterminalSymbolType > & origGrammar) { return origGrammar; } -template < class SymbolType > -grammar::EpsilonFreeCFG < SymbolType > EpsilonRemover::remove(const grammar::LG < SymbolType > & origGrammar) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > EpsilonRemover::remove(const grammar::LG < TerminalSymbolType, NonterminalSymbolType > & origGrammar) { return EpsilonRemover::removeInternal(origGrammar); } -template < class SymbolType > -grammar::EpsilonFreeCFG < SymbolType > EpsilonRemover::remove(const grammar::LeftLG < SymbolType > & origGrammar) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > EpsilonRemover::remove(const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & origGrammar) { return EpsilonRemover::removeInternal(origGrammar); } -template < class SymbolType > -grammar::LeftRG < SymbolType > EpsilonRemover::remove(const grammar::LeftRG < SymbolType > & origGrammar) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > EpsilonRemover::remove(const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & origGrammar) { return origGrammar; } -template < class SymbolType > -grammar::EpsilonFreeCFG < SymbolType > EpsilonRemover::remove(const grammar::RightLG < SymbolType > & origGrammar) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > EpsilonRemover::remove(const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > & origGrammar) { return EpsilonRemover::removeInternal(origGrammar); } -template < class SymbolType > -grammar::RightRG < SymbolType > EpsilonRemover::remove(const grammar::RightRG < SymbolType > & origGrammar) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::RightRG < TerminalSymbolType, NonterminalSymbolType > EpsilonRemover::remove(const grammar::RightRG < TerminalSymbolType, NonterminalSymbolType > & origGrammar) { return origGrammar; } diff --git a/alib2algo/src/grammar/simplify/LeftRecursionRemover.h b/alib2algo/src/grammar/simplify/LeftRecursionRemover.h index c3b4dff95a..9e9ac733a1 100644 --- a/alib2algo/src/grammar/simplify/LeftRecursionRemover.h +++ b/alib2algo/src/grammar/simplify/LeftRecursionRemover.h @@ -39,18 +39,18 @@ class LeftRecursionRemover { public: template < class TerminalSymbolType, class NonterminalSymbolType > static grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > remove( const grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > & grammar ); - template < class SymbolType > - static grammar::EpsilonFreeCFG < SymbolType > remove( const grammar::CNF < SymbolType > & grammar ); - template < class SymbolType > - static grammar::GNF < SymbolType > remove( const grammar::GNF < SymbolType > & grammar ); - template < class SymbolType > - static grammar::RightRG < SymbolType > remove( const grammar::RightRG < SymbolType > & grammar ); - template < class SymbolType > - static grammar::RightLG < SymbolType > remove( const grammar::RightLG < SymbolType > & grammar ); - template < class SymbolType > - static grammar::RightRG < SymbolType > remove( const grammar::LeftRG < SymbolType > & grammar ); - template < class SymbolType > - static grammar::RightLG < SymbolType > remove( const grammar::LeftLG < SymbolType > & grammar ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > remove( const grammar::CNF < TerminalSymbolType, NonterminalSymbolType > & grammar ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static grammar::GNF < TerminalSymbolType, NonterminalSymbolType > remove( const grammar::GNF < TerminalSymbolType, NonterminalSymbolType > & grammar ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static grammar::RightRG < TerminalSymbolType, NonterminalSymbolType > remove( const grammar::RightRG < TerminalSymbolType, NonterminalSymbolType > & grammar ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > remove( const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static grammar::RightRG < TerminalSymbolType, NonterminalSymbolType > remove( const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar ); + template < class TerminalSymbolType, class NonterminalSymbolType > + static grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > remove( const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar ); }; template < class TerminalSymbolType, class NonterminalSymbolType > @@ -166,18 +166,18 @@ grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > LeftRecurs return step; } -template < class SymbolType > -grammar::EpsilonFreeCFG < SymbolType > LeftRecursionRemover::remove(const grammar::CNF < SymbolType > & origGrammar) { - EpsilonFreeCFG < SymbolType > tmp(origGrammar.getInitialSymbol()); +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > LeftRecursionRemover::remove(const grammar::CNF < TerminalSymbolType, NonterminalSymbolType > & origGrammar) { + EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > tmp(origGrammar.getInitialSymbol()); tmp.setTerminalAlphabet(origGrammar.getTerminalAlphabet()); tmp.setNonterminalAlphabet(origGrammar.getNonterminalAlphabet()); tmp.setGeneratesEpsilon(origGrammar.getGeneratesEpsilon()); for(const auto& rule : origGrammar.getRules()) { for(const auto& rhs : rule.second) { - if(rhs.template is<SymbolType>()) { - tmp.addRule(rule.first, {rhs.template get<SymbolType>()}); + if(rhs.template is < TerminalSymbolType > ( ) ) { + tmp.addRule ( rule.first, { rhs.template get < TerminalSymbolType > ( ) } ); } else { - const auto& rhsPair = rhs.template get<ext::pair<SymbolType, SymbolType>>(); + const auto & rhsPair = rhs.template get < ext::pair < NonterminalSymbolType, NonterminalSymbolType > > ( ); tmp.addRule(rule.first, {rhsPair.first, rhsPair.second}); } } @@ -185,28 +185,28 @@ grammar::EpsilonFreeCFG < SymbolType > LeftRecursionRemover::remove(const gramma return remove(tmp); } -template < class SymbolType > -grammar::GNF < SymbolType > LeftRecursionRemover::remove(const grammar::GNF < SymbolType > & origGrammar) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::GNF < TerminalSymbolType, NonterminalSymbolType > LeftRecursionRemover::remove(const grammar::GNF < TerminalSymbolType, NonterminalSymbolType > & origGrammar) { return origGrammar; } -template < class SymbolType > -grammar::RightRG < SymbolType > LeftRecursionRemover::remove(const grammar::RightRG < SymbolType > & origGrammar) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::RightRG < TerminalSymbolType, NonterminalSymbolType > LeftRecursionRemover::remove(const grammar::RightRG < TerminalSymbolType, NonterminalSymbolType > & origGrammar) { return origGrammar; } -template < class SymbolType > -grammar::RightLG < SymbolType > LeftRecursionRemover::remove(const grammar::RightLG < SymbolType > & origGrammar) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > LeftRecursionRemover::remove(const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > & origGrammar) { return origGrammar; } -template < class SymbolType > -grammar::RightRG < SymbolType > LeftRecursionRemover::remove(const grammar::LeftRG < SymbolType > & origGrammar) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::RightRG < TerminalSymbolType, NonterminalSymbolType > LeftRecursionRemover::remove(const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & origGrammar) { return convert::ToGrammarRightRG::convert(origGrammar); } -template < class SymbolType > -grammar::RightLG < SymbolType > LeftRecursionRemover::remove(const grammar::LeftLG < SymbolType > & /* origGrammar */) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > LeftRecursionRemover::remove(const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & /* origGrammar */) { throw exception::CommonException("LeftRecursionRemover: Removing from LeftLG NYI"); // TODO } -- GitLab