From 239b71142267bd6040bcbaed0f072e515582a664 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Sun, 13 Aug 2017 19:37:08 +0200 Subject: [PATCH] split compare grammars to compare and diff --- acompare2/src/acompare.cpp | 4 +- alib2aux/src/compare/GrammarCompare.cpp | 30 +- alib2aux/src/compare/GrammarCompare.h | 512 ++---------------------- alib2aux/src/compare/GrammarDiff.cpp | 34 ++ alib2aux/src/compare/GrammarDiff.h | 506 +++++++++++++++++++++++ 5 files changed, 586 insertions(+), 500 deletions(-) create mode 100644 alib2aux/src/compare/GrammarDiff.cpp create mode 100644 alib2aux/src/compare/GrammarDiff.h diff --git a/acompare2/src/acompare.cpp b/acompare2/src/acompare.cpp index 9c5fcd1af0..99de76702e 100644 --- a/acompare2/src/acompare.cpp +++ b/acompare2/src/acompare.cpp @@ -14,7 +14,7 @@ #include "factory/XmlDataFactory.hpp" #include <compare/AutomatonDiff.h> -#include <compare/GrammarCompare.h> +#include <compare/GrammarDiff.h> #include <compare/StringCompare.h> int main ( int argc, char * * argv ) { @@ -67,7 +67,7 @@ int main ( int argc, char * * argv ) { measurements::end ( ); measurements::start ( "Compare", measurements::Type::MAIN ); - res = compare::GrammarCompare::compare ( grammar1, grammar2, std::cout ); + res = compare::GrammarDiff::diff ( grammar1, grammar2, std::cout ); } else if ( alib::XmlDataFactory::first < string::String > ( tokens1 ) && alib::XmlDataFactory::first < string::String > ( tokens2 ) ) { string::String string1 = alib::XmlDataFactory::fromTokens ( std::move ( tokens1 ) ); string::String string2 = alib::XmlDataFactory::fromTokens ( std::move ( tokens2 ) ); diff --git a/alib2aux/src/compare/GrammarCompare.cpp b/alib2aux/src/compare/GrammarCompare.cpp index c19a7ab88a..3daa30201f 100644 --- a/alib2aux/src/compare/GrammarCompare.cpp +++ b/alib2aux/src/compare/GrammarCompare.cpp @@ -10,25 +10,25 @@ namespace compare { -auto GrammarCompareLeftLG = registration::OverloadRegister < GrammarCompare, int, grammar::LeftLG < >, grammar::LeftLG < > > ( GrammarCompare::compare ); -auto GrammarCompareLeftRG = registration::OverloadRegister < GrammarCompare, int, grammar::LeftRG < >, grammar::LeftRG < > > ( GrammarCompare::compare ); -auto GrammarCompareRightLG = registration::OverloadRegister < GrammarCompare, int, grammar::RightLG < >, grammar::RightLG < > > ( GrammarCompare::compare ); -auto GrammarCompareRightRG = registration::OverloadRegister < GrammarCompare, int, grammar::RightRG < >, grammar::RightRG < > > ( GrammarCompare::compare ); +auto GrammarCompareLeftLG = registration::OverloadRegister < GrammarCompare, bool, grammar::LeftLG < >, grammar::LeftLG < > > ( GrammarCompare::compare ); +auto GrammarCompareLeftRG = registration::OverloadRegister < GrammarCompare, bool, grammar::LeftRG < >, grammar::LeftRG < > > ( GrammarCompare::compare ); +auto GrammarCompareRightLG = registration::OverloadRegister < GrammarCompare, bool, grammar::RightLG < >, grammar::RightLG < > > ( GrammarCompare::compare ); +auto GrammarCompareRightRG = registration::OverloadRegister < GrammarCompare, bool, grammar::RightRG < >, grammar::RightRG < > > ( GrammarCompare::compare ); -auto GrammarCompareLG = registration::OverloadRegister < GrammarCompare, int, grammar::LG < >, grammar::LG < > > ( GrammarCompare::compare ); -auto GrammarCompareCFG = registration::OverloadRegister < GrammarCompare, int, grammar::CFG < >, grammar::CFG < > > ( GrammarCompare::compare ); -auto GrammarCompareEpsilonFreeCFG = registration::OverloadRegister < GrammarCompare, int, grammar::EpsilonFreeCFG < >, grammar::EpsilonFreeCFG < > > ( GrammarCompare::compare ); -auto GrammarCompareCNF = registration::OverloadRegister < GrammarCompare, int, grammar::CNF < >, grammar::CNF < > > ( GrammarCompare::compare ); -auto GrammarCompareGNF = registration::OverloadRegister < GrammarCompare, int, grammar::GNF < >, grammar::GNF < > > ( GrammarCompare::compare ); -auto GrammarCompareCSG = registration::OverloadRegister < GrammarCompare, int, grammar::CSG < >, grammar::CSG < > > ( GrammarCompare::compare ); +auto GrammarCompareLG = registration::OverloadRegister < GrammarCompare, bool, grammar::LG < >, grammar::LG < > > ( GrammarCompare::compare ); +auto GrammarCompareCFG = registration::OverloadRegister < GrammarCompare, bool, grammar::CFG < >, grammar::CFG < > > ( GrammarCompare::compare ); +auto GrammarCompareEpsilonFreeCFG = registration::OverloadRegister < GrammarCompare, bool, grammar::EpsilonFreeCFG < >, grammar::EpsilonFreeCFG < > > ( GrammarCompare::compare ); +auto GrammarCompareCNF = registration::OverloadRegister < GrammarCompare, bool, grammar::CNF < >, grammar::CNF < > > ( GrammarCompare::compare ); +auto GrammarCompareGNF = registration::OverloadRegister < GrammarCompare, bool, grammar::GNF < >, grammar::GNF < > > ( GrammarCompare::compare ); +auto GrammarCompareCSG = registration::OverloadRegister < GrammarCompare, bool, grammar::CSG < >, grammar::CSG < > > ( GrammarCompare::compare ); -auto GrammarCompareNonContractingGrammar = registration::OverloadRegister < GrammarCompare, int, grammar::NonContractingGrammar < >, grammar::NonContractingGrammar < > > ( GrammarCompare::compare ); -auto GrammarCompareContextPreservingUnrestrictedGrammar = registration::OverloadRegister < GrammarCompare, int, grammar::ContextPreservingUnrestrictedGrammar < >, grammar::ContextPreservingUnrestrictedGrammar < > > ( GrammarCompare::compare ); +auto GrammarCompareNonContractingGrammar = registration::OverloadRegister < GrammarCompare, bool, grammar::NonContractingGrammar < >, grammar::NonContractingGrammar < > > ( GrammarCompare::compare ); +auto GrammarCompareContextPreservingUnrestrictedGrammar = registration::OverloadRegister < GrammarCompare, bool, grammar::ContextPreservingUnrestrictedGrammar < >, grammar::ContextPreservingUnrestrictedGrammar < > > ( GrammarCompare::compare ); -auto GrammarCompareUnrestrictedGrammar = registration::OverloadRegister < GrammarCompare, int, grammar::UnrestrictedGrammar < >, grammar::UnrestrictedGrammar < > > ( GrammarCompare::compare ); +auto GrammarCompareUnrestrictedGrammar = registration::OverloadRegister < GrammarCompare, bool, grammar::UnrestrictedGrammar < >, grammar::UnrestrictedGrammar < > > ( GrammarCompare::compare ); -int GrammarCompare::compare(const grammar::Grammar& a, const grammar::Grammar& b, std::ostream & out ) { - return dispatch(a.getData(), b.getData(), out); +bool GrammarCompare::compare(const grammar::Grammar& a, const grammar::Grammar& b ) { + return dispatch(a.getData(), b.getData() ); } } /* namespace compare */ diff --git a/alib2aux/src/compare/GrammarCompare.h b/alib2aux/src/compare/GrammarCompare.h index 9f125637f5..81a8f6cce3 100644 --- a/alib2aux/src/compare/GrammarCompare.h +++ b/alib2aux/src/compare/GrammarCompare.h @@ -10,20 +10,8 @@ #include <core/multipleDispatch.hpp> -#include <compare/DiffAux.h> - #include <grammar/Grammar.h> #include <grammar/GrammarFeatures.h> -#include <set> -#include <map> -#include <list> -#include <utility> -#include <vector> -#include <typeinfo> -#include <iostream> -#include <ostream> -#include <algorithm> - #include "grammar/Regular/LeftLG.h" #include "grammar/Regular/LeftRG.h" #include "grammar/Regular/RightLG.h" @@ -40,81 +28,52 @@ namespace compare { -class GrammarCompare : public alib::MultipleDispatch < GrammarCompare, int, ext::tuple < >, ext::tuple < const grammar::GrammarBase &, const grammar::GrammarBase & >, ext::tuple < std::ostream & > > { -private: - template < class SymbolType > - static bool testDiff(const grammar::LeftLG < SymbolType > & a, const grammar::LeftLG < SymbolType > & b); +class GrammarCompare : public alib::MultipleDispatch < GrammarCompare, bool, ext::tuple < >, ext::tuple < const grammar::GrammarBase &, const grammar::GrammarBase & >, ext::tuple < > > { +public: template < class SymbolType > - static void printDiff(const grammar::LeftLG < SymbolType > & a, const grammar::LeftLG < SymbolType > & b, std::ostream & out ); + static bool compare(const grammar::LeftLG < SymbolType > & a, const grammar::LeftLG < SymbolType > & b); template < class SymbolType > - static bool testDiff(const grammar::LeftRG < SymbolType > & a, const grammar::LeftRG < SymbolType > & b); - template < class SymbolType > - static void printDiff(const grammar::LeftRG < SymbolType > & a, const grammar::LeftRG < SymbolType > & b, std::ostream & out ); + static bool compare(const grammar::LeftRG < SymbolType > & a, const grammar::LeftRG < SymbolType > & b); template < class SymbolType > - static bool testDiff(const grammar::RightLG < SymbolType > & a, const grammar::RightLG < SymbolType > & b); - template < class SymbolType > - static void printDiff(const grammar::RightLG < SymbolType > & a, const grammar::RightLG < SymbolType > & b, std::ostream & out ); + static bool compare(const grammar::RightLG < SymbolType > & a, const grammar::RightLG < SymbolType > & b); template < class SymbolType > - static bool testDiff(const grammar::RightRG < SymbolType > & a, const grammar::RightRG < SymbolType > & b); - template < class SymbolType > - static void printDiff(const grammar::RightRG < SymbolType > & a, const grammar::RightRG < SymbolType > & b, std::ostream & out ); + static bool compare(const grammar::RightRG < SymbolType > & a, const grammar::RightRG < SymbolType > & b); template < class SymbolType > - static bool testDiff(const grammar::LG < SymbolType > & a, const grammar::LG < SymbolType > & b); - template < class SymbolType > - static void printDiff(const grammar::LG < SymbolType > & a, const grammar::LG < SymbolType > & b, std::ostream & out ); + static bool compare(const grammar::LG < SymbolType > & a, const grammar::LG < SymbolType > & b); template < class SymbolType > - static bool testDiff(const grammar::CFG < SymbolType > & a, const grammar::CFG < SymbolType > & b); - template < class SymbolType > - static void printDiff(const grammar::CFG < SymbolType > & a, const grammar::CFG < SymbolType > & b, std::ostream & out ); + static bool compare(const grammar::CFG < SymbolType > & a, const grammar::CFG < SymbolType > & b); template < class SymbolType > - static bool testDiff(const grammar::EpsilonFreeCFG < SymbolType > & a, const grammar::EpsilonFreeCFG < SymbolType > & b); - template < class SymbolType > - static void printDiff(const grammar::EpsilonFreeCFG < SymbolType > & a, const grammar::EpsilonFreeCFG < SymbolType > & b, std::ostream & out ); + static bool compare(const grammar::EpsilonFreeCFG < SymbolType > & a, const grammar::EpsilonFreeCFG < SymbolType > & b); template < class SymbolType > - static bool testDiff(const grammar::CNF < SymbolType > & a, const grammar::CNF < SymbolType > & b); - template < class SymbolType > - static void printDiff(const grammar::CNF < SymbolType > & a, const grammar::CNF < SymbolType > & b, std::ostream & out ); + static bool compare(const grammar::CNF < SymbolType > & a, const grammar::CNF < SymbolType > & b); template < class SymbolType > - static bool testDiff(const grammar::GNF < SymbolType > & a, const grammar::GNF < SymbolType > & b); - template < class SymbolType > - static void printDiff(const grammar::GNF < SymbolType > & a, const grammar::GNF < SymbolType > & b, std::ostream & out ); + static bool compare(const grammar::GNF < SymbolType > & a, const grammar::GNF < SymbolType > & b); template < class SymbolType > - static bool testDiff(const grammar::CSG < SymbolType > & a, const grammar::CSG < SymbolType > & b); - template < class SymbolType > - static void printDiff(const grammar::CSG < SymbolType > & a, const grammar::CSG < SymbolType > & b, std::ostream & out ); + static bool compare(const grammar::CSG < SymbolType > & a, const grammar::CSG < SymbolType > & b); template < class SymbolType > - static bool testDiff(const grammar::NonContractingGrammar < SymbolType > & a, const grammar::NonContractingGrammar < SymbolType > & b); - template < class SymbolType > - static void printDiff(const grammar::NonContractingGrammar < SymbolType > & a, const grammar::NonContractingGrammar < SymbolType > & b, std::ostream & out ); + static bool compare(const grammar::NonContractingGrammar < SymbolType > & a, const grammar::NonContractingGrammar < SymbolType > & b); template < class SymbolType > - static bool testDiff(const grammar::ContextPreservingUnrestrictedGrammar < SymbolType > & a, const grammar::ContextPreservingUnrestrictedGrammar < SymbolType > & b); - template < class SymbolType > - static void printDiff(const grammar::ContextPreservingUnrestrictedGrammar < SymbolType > & a, const grammar::ContextPreservingUnrestrictedGrammar < SymbolType > & b, std::ostream & out ); + static bool compare(const grammar::ContextPreservingUnrestrictedGrammar < SymbolType > & a, const grammar::ContextPreservingUnrestrictedGrammar < SymbolType > & b); template < class SymbolType > - static bool testDiff(const grammar::UnrestrictedGrammar < SymbolType > & a, const grammar::UnrestrictedGrammar < SymbolType > & b); - template < class SymbolType > - static void printDiff(const grammar::UnrestrictedGrammar < SymbolType > & a, const grammar::UnrestrictedGrammar < SymbolType > & b, std::ostream & out ); -public: - template < class T > - static int compare(const T & a, const T & b, std::ostream & out ); + static bool compare(const grammar::UnrestrictedGrammar < SymbolType > & a, const grammar::UnrestrictedGrammar < SymbolType > & b); - static int compare(const grammar::Grammar& a, const grammar::Grammar& b, std::ostream & out ); + static bool compare(const grammar::Grammar& a, const grammar::Grammar& b ); }; template < class SymbolType > -bool GrammarCompare::testDiff(const grammar::LeftLG < SymbolType > & a, const grammar::LeftLG < SymbolType > & b) { +bool GrammarCompare::compare(const grammar::LeftLG < SymbolType > & a, const grammar::LeftLG < SymbolType > & b) { return a.getNonterminalAlphabet() == b.getNonterminalAlphabet() && a.getRules() == b.getRules() && // a.getTerminalAlphabet() == b.getTerminalAlphabet() && @@ -122,7 +81,7 @@ bool GrammarCompare::testDiff(const grammar::LeftLG < SymbolType > & a, const gr } template < class SymbolType > -bool GrammarCompare::testDiff(const grammar::LeftRG < SymbolType > & a, const grammar::LeftRG < SymbolType > & b) { +bool GrammarCompare::compare(const grammar::LeftRG < SymbolType > & a, const grammar::LeftRG < SymbolType > & b) { return a.getNonterminalAlphabet() == b.getNonterminalAlphabet() && a.getRules() == b.getRules() && // a.getTerminalAlphabet() == b.getTerminalAlphabet() && @@ -130,7 +89,7 @@ bool GrammarCompare::testDiff(const grammar::LeftRG < SymbolType > & a, const gr } template < class SymbolType > -bool GrammarCompare::testDiff(const grammar::RightLG < SymbolType > & a, const grammar::RightLG < SymbolType > & b) { +bool GrammarCompare::compare(const grammar::RightLG < SymbolType > & a, const grammar::RightLG < SymbolType > & b) { return a.getNonterminalAlphabet() == b.getNonterminalAlphabet() && a.getRules() == b.getRules() && // a.getTerminalAlphabet() == b.getTerminalAlphabet() && @@ -138,7 +97,7 @@ bool GrammarCompare::testDiff(const grammar::RightLG < SymbolType > & a, const g } template < class SymbolType > -bool GrammarCompare::testDiff(const grammar::RightRG < SymbolType > & a, const grammar::RightRG < SymbolType > & b) { +bool GrammarCompare::compare(const grammar::RightRG < SymbolType > & a, const grammar::RightRG < SymbolType > & b) { return a.getNonterminalAlphabet() == b.getNonterminalAlphabet() && a.getRules() == b.getRules() && // a.getTerminalAlphabet() == b.getTerminalAlphabet() && @@ -146,7 +105,7 @@ bool GrammarCompare::testDiff(const grammar::RightRG < SymbolType > & a, const g } template < class SymbolType > -bool GrammarCompare::testDiff(const grammar::LG < SymbolType > & a, const grammar::LG < SymbolType > & b) { +bool GrammarCompare::compare(const grammar::LG < SymbolType > & a, const grammar::LG < SymbolType > & b) { return a.getNonterminalAlphabet() == b.getNonterminalAlphabet() && a.getRules() == b.getRules() && // a.getTerminalAlphabet() == b.getTerminalAlphabet() && @@ -154,7 +113,7 @@ bool GrammarCompare::testDiff(const grammar::LG < SymbolType > & a, const gramma } template < class SymbolType > -bool GrammarCompare::testDiff(const grammar::CFG < SymbolType > & a, const grammar::CFG < SymbolType > & b) { +bool GrammarCompare::compare(const grammar::CFG < SymbolType > & a, const grammar::CFG < SymbolType > & b) { return a.getNonterminalAlphabet() == b.getNonterminalAlphabet() && a.getRules() == b.getRules() && // a.getTerminalAlphabet() == b.getTerminalAlphabet() && @@ -162,7 +121,7 @@ bool GrammarCompare::testDiff(const grammar::CFG < SymbolType > & a, const gramm } template < class SymbolType > -bool GrammarCompare::testDiff(const grammar::EpsilonFreeCFG < SymbolType > & a, const grammar::EpsilonFreeCFG < SymbolType > & b) { +bool GrammarCompare::compare(const grammar::EpsilonFreeCFG < SymbolType > & a, const grammar::EpsilonFreeCFG < SymbolType > & b) { return a.getNonterminalAlphabet() == b.getNonterminalAlphabet() && a.getRules() == b.getRules() && // a.getTerminalAlphabet() == b.getTerminalAlphabet() && @@ -170,7 +129,7 @@ bool GrammarCompare::testDiff(const grammar::EpsilonFreeCFG < SymbolType > & a, } template < class SymbolType > -bool GrammarCompare::testDiff(const grammar::CNF < SymbolType > & a, const grammar::CNF < SymbolType > & b) { +bool GrammarCompare::compare(const grammar::CNF < SymbolType > & a, const grammar::CNF < SymbolType > & b) { return a.getNonterminalAlphabet() == b.getNonterminalAlphabet() && a.getRules() == b.getRules() && // a.getTerminalAlphabet() == b.getTerminalAlphabet() && @@ -178,7 +137,7 @@ bool GrammarCompare::testDiff(const grammar::CNF < SymbolType > & a, const gramm } template < class SymbolType > -bool GrammarCompare::testDiff(const grammar::GNF < SymbolType > & a, const grammar::GNF < SymbolType > & b) { +bool GrammarCompare::compare(const grammar::GNF < SymbolType > & a, const grammar::GNF < SymbolType > & b) { return a.getNonterminalAlphabet() == b.getNonterminalAlphabet() && a.getRules() == b.getRules() && // a.getTerminalAlphabet() == b.getTerminalAlphabet() && @@ -186,7 +145,7 @@ bool GrammarCompare::testDiff(const grammar::GNF < SymbolType > & a, const gramm } template < class SymbolType > -bool GrammarCompare::testDiff(const grammar::CSG < SymbolType > & a, const grammar::CSG < SymbolType > & b) { +bool GrammarCompare::compare(const grammar::CSG < SymbolType > & a, const grammar::CSG < SymbolType > & b) { return a.getNonterminalAlphabet() == b.getNonterminalAlphabet() && a.getRules() == b.getRules() && // a.getTerminalAlphabet() == b.getTerminalAlphabet() && @@ -194,7 +153,7 @@ bool GrammarCompare::testDiff(const grammar::CSG < SymbolType > & a, const gramm } template < class SymbolType > -bool GrammarCompare::testDiff(const grammar::NonContractingGrammar < SymbolType > & a, const grammar::NonContractingGrammar < SymbolType > & b) { +bool GrammarCompare::compare(const grammar::NonContractingGrammar < SymbolType > & a, const grammar::NonContractingGrammar < SymbolType > & b) { return a.getNonterminalAlphabet() == b.getNonterminalAlphabet() && a.getRules() == b.getRules() && // a.getTerminalAlphabet() == b.getTerminalAlphabet() && @@ -202,7 +161,7 @@ bool GrammarCompare::testDiff(const grammar::NonContractingGrammar < SymbolType } template < class SymbolType > -bool GrammarCompare::testDiff(const grammar::ContextPreservingUnrestrictedGrammar < SymbolType > & a, const grammar::ContextPreservingUnrestrictedGrammar < SymbolType > & b) { +bool GrammarCompare::compare(const grammar::ContextPreservingUnrestrictedGrammar < SymbolType > & a, const grammar::ContextPreservingUnrestrictedGrammar < SymbolType > & b) { return a.getNonterminalAlphabet() == b.getNonterminalAlphabet() && a.getRules() == b.getRules() && // a.getTerminalAlphabet() == b.getTerminalAlphabet() && @@ -210,426 +169,13 @@ bool GrammarCompare::testDiff(const grammar::ContextPreservingUnrestrictedGramma } template < class SymbolType > -bool GrammarCompare::testDiff(const grammar::UnrestrictedGrammar < SymbolType > & a, const grammar::UnrestrictedGrammar < SymbolType > & b) { +bool GrammarCompare::compare(const grammar::UnrestrictedGrammar < SymbolType > & a, const grammar::UnrestrictedGrammar < SymbolType > & b) { return a.getNonterminalAlphabet() == b.getNonterminalAlphabet() && a.getRules() == b.getRules() && // a.getTerminalAlphabet() == b.getTerminalAlphabet() && a.getInitialSymbol() == b.getInitialSymbol() ; } -template < class SymbolType > -void GrammarCompare::printDiff(const grammar::LeftLG < SymbolType > & a, const grammar::LeftLG < SymbolType > & b, std::ostream & out ) { - out << "GrammarsComparer" << std::endl; - - if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { - out << "Nonterminal alphabet" << std::endl; - - DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); - } - - if(a.getRules() != b.getRules()){ - out << "Rules" << std::endl; - - DiffAux::mapDiff(a.getRules(), b.getRules()); - } - - if(a.getInitialSymbol() != b.getInitialSymbol()){ - out << "Initial symbol" << std::endl; - - out << "< " << a.getInitialSymbol() << std::endl; - out << "---" << std::endl; - out << "> " << b.getInitialSymbol() << std::endl; - } - - if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { - out << "Terminal alphabet" << std::endl; - - DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); - } -} - -template < class SymbolType > -void GrammarCompare::printDiff(const grammar::LeftRG < SymbolType > & a, const grammar::LeftRG < SymbolType > & b, std::ostream & out ) { - out << "GrammarsComparer" << std::endl; - - if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { - out << "Nonterminal alphabet" << std::endl; - - DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); - } - - if(a.getRules() != b.getRules()){ - out << "Rules" << std::endl; - - DiffAux::mapDiff(a.getRules(), b.getRules()); - } - - if(a.getInitialSymbol() != b.getInitialSymbol()){ - out << "Initial symbol" << std::endl; - - out << "< " << a.getInitialSymbol() << std::endl; - out << "---" << std::endl; - out << "> " << b.getInitialSymbol() << std::endl; - } - - if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { - out << "Terminal alphabet" << std::endl; - - DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); - } -} - -template < class SymbolType > -void GrammarCompare::printDiff(const grammar::RightLG < SymbolType > & a, const grammar::RightLG < SymbolType > & b, std::ostream & out ) { - out << "GrammarsComparer" << std::endl; - - if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { - out << "Nonterminal alphabet" << std::endl; - - DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); - } - - if(a.getRules() != b.getRules()){ - out << "Rules" << std::endl; - - DiffAux::mapDiff(a.getRules(), b.getRules()); - } - - if(a.getInitialSymbol() != b.getInitialSymbol()){ - out << "Initial symbol" << std::endl; - - out << "< " << a.getInitialSymbol() << std::endl; - out << "---" << std::endl; - out << "> " << b.getInitialSymbol() << std::endl; - } - - if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { - out << "Terminal alphabet" << std::endl; - - DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); - } -} - -template < class SymbolType > -void GrammarCompare::printDiff(const grammar::RightRG < SymbolType > & a, const grammar::RightRG < SymbolType > & b, std::ostream & out ) { - out << "GrammarsComparer" << std::endl; - - if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { - out << "Nonterminal alphabet" << std::endl; - - DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); - } - - if(a.getRules() != b.getRules()){ - out << "Rules" << std::endl; - - DiffAux::mapDiff(a.getRules(), b.getRules()); - } - - if(a.getInitialSymbol() != b.getInitialSymbol()){ - out << "Initial symbol" << std::endl; - - out << "< " << a.getInitialSymbol() << std::endl; - out << "---" << std::endl; - out << "> " << b.getInitialSymbol() << std::endl; - } - - if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { - out << "Terminal alphabet" << std::endl; - - DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); - } -} - -template < class SymbolType > -void GrammarCompare::printDiff(const grammar::LG < SymbolType > & a, const grammar::LG < SymbolType > & b, std::ostream & out ) { - out << "GrammarsComparer" << std::endl; - - if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { - out << "Nonterminal alphabet" << std::endl; - - DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); - } - - if(a.getRules() != b.getRules()){ - out << "Rules" << std::endl; - - DiffAux::mapDiff(a.getRules(), b.getRules()); - } - - if(a.getInitialSymbol() != b.getInitialSymbol()){ - out << "Initial symbol" << std::endl; - - out << "< " << a.getInitialSymbol() << std::endl; - out << "---" << std::endl; - out << "> " << b.getInitialSymbol() << std::endl; - } - - if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { - out << "Terminal alphabet" << std::endl; - - DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); - } -} - -template < class SymbolType > -void GrammarCompare::printDiff(const grammar::CFG < SymbolType > & a, const grammar::CFG < SymbolType > & b, std::ostream & out ) { - out << "GrammarsComparer" << std::endl; - - if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { - out << "Nonterminal alphabet" << std::endl; - - DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); - } - - if(a.getRules() != b.getRules()){ - out << "Rules" << std::endl; - - DiffAux::mapDiff(a.getRules(), b.getRules()); - } - - if(a.getInitialSymbol() != b.getInitialSymbol()){ - out << "Initial symbol" << std::endl; - - out << "< " << a.getInitialSymbol() << std::endl; - out << "---" << std::endl; - out << "> " << b.getInitialSymbol() << std::endl; - } - - if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { - out << "Terminal alphabet" << std::endl; - - DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); - } -} - -template < class SymbolType > -void GrammarCompare::printDiff(const grammar::EpsilonFreeCFG < SymbolType > & a, const grammar::EpsilonFreeCFG < SymbolType > & b, std::ostream & out ) { - out << "GrammarsComparer" << std::endl; - - if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { - out << "Nonterminal alphabet" << std::endl; - - DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); - } - - if(a.getRules() != b.getRules()){ - out << "Rules" << std::endl; - - DiffAux::mapDiff(a.getRules(), b.getRules()); - } - - if(a.getInitialSymbol() != b.getInitialSymbol()){ - out << "Initial symbol" << std::endl; - - out << "< " << a.getInitialSymbol() << std::endl; - out << "---" << std::endl; - out << "> " << b.getInitialSymbol() << std::endl; - } - - if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { - out << "Terminal alphabet" << std::endl; - - DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); - } -} - -template < class SymbolType > -void GrammarCompare::printDiff(const grammar::CNF < SymbolType > & a, const grammar::CNF < SymbolType > & b, std::ostream & out ) { - out << "GrammarsComparer" << std::endl; - - if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { - out << "Nonterminal alphabet" << std::endl; - - DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); - } - - if(a.getRules() != b.getRules()){ - out << "Rules" << std::endl; - - DiffAux::mapDiff(a.getRules(), b.getRules()); - } - - if(a.getInitialSymbol() != b.getInitialSymbol()){ - out << "Initial symbol" << std::endl; - - out << "< " << a.getInitialSymbol() << std::endl; - out << "---" << std::endl; - out << "> " << b.getInitialSymbol() << std::endl; - } - - if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { - out << "Terminal alphabet" << std::endl; - - DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); - } -} - -template < class SymbolType > -void GrammarCompare::printDiff(const grammar::GNF < SymbolType > & a, const grammar::GNF < SymbolType > & b, std::ostream & out ) { - out << "GrammarsComparer" << std::endl; - - if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { - out << "Nonterminal alphabet" << std::endl; - - DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); - } - - if(a.getRules() != b.getRules()){ - out << "Rules" << std::endl; - - DiffAux::mapDiff(a.getRules(), b.getRules()); - } - - if(a.getInitialSymbol() != b.getInitialSymbol()){ - out << "Initial symbol" << std::endl; - - out << "< " << a.getInitialSymbol() << std::endl; - out << "---" << std::endl; - out << "> " << b.getInitialSymbol() << std::endl; - } - - if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { - out << "Terminal alphabet" << std::endl; - - DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); - } -} - -template < class SymbolType > -void GrammarCompare::printDiff(const grammar::CSG < SymbolType > & a, const grammar::CSG < SymbolType > & b, std::ostream & out ) { - out << "GrammarsComparer" << std::endl; - - if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { - out << "Nonterminal alphabet" << std::endl; - - DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); - } - - if(a.getRules() != b.getRules()){ - out << "Rules" << std::endl; - - DiffAux::mapDiff(a.getRules(), b.getRules()); - } - - if(a.getInitialSymbol() != b.getInitialSymbol()){ - out << "Initial symbol" << std::endl; - - out << "< " << a.getInitialSymbol() << std::endl; - out << "---" << std::endl; - out << "> " << b.getInitialSymbol() << std::endl; - } - - if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { - out << "Terminal alphabet" << std::endl; - - DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); - } -} - -template < class SymbolType > -void GrammarCompare::printDiff(const grammar::NonContractingGrammar < SymbolType > & a, const grammar::NonContractingGrammar < SymbolType > & b, std::ostream & out ) { - out << "GrammarsComparer" << std::endl; - - if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { - out << "Nonterminal alphabet" << std::endl; - - DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); - } - - if(a.getRules() != b.getRules()){ - out << "Rules" << std::endl; - - DiffAux::mapDiff(a.getRules(), b.getRules()); - } - - if(a.getInitialSymbol() != b.getInitialSymbol()){ - out << "Initial symbol" << std::endl; - - out << "< " << a.getInitialSymbol() << std::endl; - out << "---" << std::endl; - out << "> " << b.getInitialSymbol() << std::endl; - } - - if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { - out << "Terminal alphabet" << std::endl; - - DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); - } -} - -template < class SymbolType > -void GrammarCompare::printDiff(const grammar::ContextPreservingUnrestrictedGrammar < SymbolType > & a, const grammar::ContextPreservingUnrestrictedGrammar < SymbolType > & b, std::ostream & out ) { - out << "GrammarsComparer" << std::endl; - - if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { - out << "Nonterminal alphabet" << std::endl; - - DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); - } - - if(a.getRules() != b.getRules()){ - out << "Rules" << std::endl; - - DiffAux::mapDiff(a.getRules(), b.getRules()); - } - - if(a.getInitialSymbol() != b.getInitialSymbol()){ - out << "Initial symbol" << std::endl; - - out << "< " << a.getInitialSymbol() << std::endl; - out << "---" << std::endl; - out << "> " << b.getInitialSymbol() << std::endl; - } - - if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { - out << "Terminal alphabet" << std::endl; - - DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); - } -} - -template < class SymbolType > -void GrammarCompare::printDiff(const grammar::UnrestrictedGrammar < SymbolType > & a, const grammar::UnrestrictedGrammar < SymbolType > & b, std::ostream & out ) { - out << "GrammarsComparer" << std::endl; - - if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { - out << "Nonterminal alphabet" << std::endl; - - DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); - } - - if(a.getRules() != b.getRules()){ - out << "Rules" << std::endl; - - DiffAux::mapDiff(a.getRules(), b.getRules()); - } - - if(a.getInitialSymbol() != b.getInitialSymbol()){ - out << "Initial symbol" << std::endl; - - out << "< " << a.getInitialSymbol() << std::endl; - out << "---" << std::endl; - out << "> " << b.getInitialSymbol() << std::endl; - } - - if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { - out << "Terminal alphabet" << std::endl; - - DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); - } -} - -template < class T > -int GrammarCompare::compare(const T & a, const T & b, std::ostream & out ) { - if(!GrammarCompare::testDiff(a, b)) { - GrammarCompare::printDiff ( a, b, out ); - return 1; - } else { - return 0; - } -} - } /* namespace compare */ #endif /* GRAMMAR_COMPARE_H_ */ diff --git a/alib2aux/src/compare/GrammarDiff.cpp b/alib2aux/src/compare/GrammarDiff.cpp new file mode 100644 index 0000000000..da6b6736e2 --- /dev/null +++ b/alib2aux/src/compare/GrammarDiff.cpp @@ -0,0 +1,34 @@ +/* + * GrammarDiff.cpp + * + * Created on: Apr 1, 2013 + * Author: honza + */ + +#include "GrammarDiff.h" +#include <registration/AlgoRegistration.hpp> + +namespace compare { + +auto GrammarDiffLeftLG = registration::OverloadRegister < GrammarDiff, int, grammar::LeftLG < >, grammar::LeftLG < > > ( GrammarDiff::diff ); +auto GrammarDiffLeftRG = registration::OverloadRegister < GrammarDiff, int, grammar::LeftRG < >, grammar::LeftRG < > > ( GrammarDiff::diff ); +auto GrammarDiffRightLG = registration::OverloadRegister < GrammarDiff, int, grammar::RightLG < >, grammar::RightLG < > > ( GrammarDiff::diff ); +auto GrammarDiffRightRG = registration::OverloadRegister < GrammarDiff, int, grammar::RightRG < >, grammar::RightRG < > > ( GrammarDiff::diff ); + +auto GrammarDiffLG = registration::OverloadRegister < GrammarDiff, int, grammar::LG < >, grammar::LG < > > ( GrammarDiff::diff ); +auto GrammarDiffCFG = registration::OverloadRegister < GrammarDiff, int, grammar::CFG < >, grammar::CFG < > > ( GrammarDiff::diff ); +auto GrammarDiffEpsilonFreeCFG = registration::OverloadRegister < GrammarDiff, int, grammar::EpsilonFreeCFG < >, grammar::EpsilonFreeCFG < > > ( GrammarDiff::diff ); +auto GrammarDiffCNF = registration::OverloadRegister < GrammarDiff, int, grammar::CNF < >, grammar::CNF < > > ( GrammarDiff::diff ); +auto GrammarDiffGNF = registration::OverloadRegister < GrammarDiff, int, grammar::GNF < >, grammar::GNF < > > ( GrammarDiff::diff ); +auto GrammarDiffCSG = registration::OverloadRegister < GrammarDiff, int, grammar::CSG < >, grammar::CSG < > > ( GrammarDiff::diff ); + +auto GrammarDiffNonContractingGrammar = registration::OverloadRegister < GrammarDiff, int, grammar::NonContractingGrammar < >, grammar::NonContractingGrammar < > > ( GrammarDiff::diff ); +auto GrammarDiffContextPreservingUnrestrictedGrammar = registration::OverloadRegister < GrammarDiff, int, grammar::ContextPreservingUnrestrictedGrammar < >, grammar::ContextPreservingUnrestrictedGrammar < > > ( GrammarDiff::diff ); + +auto GrammarDiffUnrestrictedGrammar = registration::OverloadRegister < GrammarDiff, int, grammar::UnrestrictedGrammar < >, grammar::UnrestrictedGrammar < > > ( GrammarDiff::diff ); + +int GrammarDiff::diff(const grammar::Grammar& a, const grammar::Grammar& b, std::ostream & out ) { + return dispatch(a.getData(), b.getData(), out); +} + +} /* namespace compare */ diff --git a/alib2aux/src/compare/GrammarDiff.h b/alib2aux/src/compare/GrammarDiff.h new file mode 100644 index 0000000000..c50a78f740 --- /dev/null +++ b/alib2aux/src/compare/GrammarDiff.h @@ -0,0 +1,506 @@ +/* + * GrammarDiff.h + * + * Created on: Apr 1, 2013 + * Author: honza + */ + +#ifndef GRAMMAR_DIFF_H_ +#define GRAMMAR_DIFF_H_ + +#include <core/multipleDispatch.hpp> + +#include <compare/DiffAux.h> +#include <compare/GrammarCompare.h> + +#include <grammar/Grammar.h> +#include <grammar/GrammarFeatures.h> +#include <set> +#include <map> +#include <list> +#include <utility> +#include <vector> +#include <typeinfo> +#include <iostream> +#include <ostream> +#include <algorithm> + +#include "grammar/Regular/LeftLG.h" +#include "grammar/Regular/LeftRG.h" +#include "grammar/Regular/RightLG.h" +#include "grammar/Regular/RightRG.h" +#include "grammar/ContextFree/LG.h" +#include "grammar/ContextFree/CFG.h" +#include "grammar/ContextFree/EpsilonFreeCFG.h" +#include "grammar/ContextFree/CNF.h" +#include "grammar/ContextFree/GNF.h" +#include "grammar/ContextSensitive/CSG.h" +#include "grammar/ContextSensitive/NonContractingGrammar.h" +#include "grammar/Unrestricted/ContextPreservingUnrestrictedGrammar.h" +#include "grammar/Unrestricted/UnrestrictedGrammar.h" + +namespace compare { + +class GrammarDiff : public alib::MultipleDispatch < GrammarDiff, int, ext::tuple < >, ext::tuple < const grammar::GrammarBase &, const grammar::GrammarBase & >, ext::tuple < std::ostream & > > { +private: + template < class SymbolType > + static void printDiff(const grammar::LeftLG < SymbolType > & a, const grammar::LeftLG < SymbolType > & b, std::ostream & out ); + + template < class SymbolType > + static void printDiff(const grammar::LeftRG < SymbolType > & a, const grammar::LeftRG < SymbolType > & b, std::ostream & out ); + + template < class SymbolType > + static void printDiff(const grammar::RightLG < SymbolType > & a, const grammar::RightLG < SymbolType > & b, std::ostream & out ); + + template < class SymbolType > + static void printDiff(const grammar::RightRG < SymbolType > & a, const grammar::RightRG < SymbolType > & b, std::ostream & out ); + + template < class SymbolType > + static void printDiff(const grammar::LG < SymbolType > & a, const grammar::LG < SymbolType > & b, std::ostream & out ); + + template < class SymbolType > + static void printDiff(const grammar::CFG < SymbolType > & a, const grammar::CFG < SymbolType > & b, std::ostream & out ); + + template < class SymbolType > + static void printDiff(const grammar::EpsilonFreeCFG < SymbolType > & a, const grammar::EpsilonFreeCFG < SymbolType > & b, std::ostream & out ); + + template < class SymbolType > + static void printDiff(const grammar::CNF < SymbolType > & a, const grammar::CNF < SymbolType > & b, std::ostream & out ); + + template < class SymbolType > + static void printDiff(const grammar::GNF < SymbolType > & a, const grammar::GNF < SymbolType > & b, std::ostream & out ); + + template < class SymbolType > + static void printDiff(const grammar::CSG < SymbolType > & a, const grammar::CSG < SymbolType > & b, std::ostream & out ); + + template < class SymbolType > + static void printDiff(const grammar::NonContractingGrammar < SymbolType > & a, const grammar::NonContractingGrammar < SymbolType > & b, std::ostream & out ); + + template < class SymbolType > + static void printDiff(const grammar::ContextPreservingUnrestrictedGrammar < SymbolType > & a, const grammar::ContextPreservingUnrestrictedGrammar < SymbolType > & b, std::ostream & out ); + + template < class SymbolType > + static void printDiff(const grammar::UnrestrictedGrammar < SymbolType > & a, const grammar::UnrestrictedGrammar < SymbolType > & b, std::ostream & out ); +public: + template < class T > + static int diff(const T & a, const T & b, std::ostream & out ); + + static int diff(const grammar::Grammar& a, const grammar::Grammar& b, std::ostream & out ); +}; + +template < class SymbolType > +void GrammarDiff::printDiff(const grammar::LeftLG < SymbolType > & a, const grammar::LeftLG < SymbolType > & b, std::ostream & out ) { + out << "GrammarsComparer" << std::endl; + + if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { + out << "Nonterminal alphabet" << std::endl; + + DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); + } + + if(a.getRules() != b.getRules()){ + out << "Rules" << std::endl; + + DiffAux::mapDiff(a.getRules(), b.getRules()); + } + + if(a.getInitialSymbol() != b.getInitialSymbol()){ + out << "Initial symbol" << std::endl; + + out << "< " << a.getInitialSymbol() << std::endl; + out << "---" << std::endl; + out << "> " << b.getInitialSymbol() << std::endl; + } + + if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { + out << "Terminal alphabet" << std::endl; + + DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); + } +} + +template < class SymbolType > +void GrammarDiff::printDiff(const grammar::LeftRG < SymbolType > & a, const grammar::LeftRG < SymbolType > & b, std::ostream & out ) { + out << "GrammarsComparer" << std::endl; + + if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { + out << "Nonterminal alphabet" << std::endl; + + DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); + } + + if(a.getRules() != b.getRules()){ + out << "Rules" << std::endl; + + DiffAux::mapDiff(a.getRules(), b.getRules()); + } + + if(a.getInitialSymbol() != b.getInitialSymbol()){ + out << "Initial symbol" << std::endl; + + out << "< " << a.getInitialSymbol() << std::endl; + out << "---" << std::endl; + out << "> " << b.getInitialSymbol() << std::endl; + } + + if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { + out << "Terminal alphabet" << std::endl; + + DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); + } +} + +template < class SymbolType > +void GrammarDiff::printDiff(const grammar::RightLG < SymbolType > & a, const grammar::RightLG < SymbolType > & b, std::ostream & out ) { + out << "GrammarsComparer" << std::endl; + + if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { + out << "Nonterminal alphabet" << std::endl; + + DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); + } + + if(a.getRules() != b.getRules()){ + out << "Rules" << std::endl; + + DiffAux::mapDiff(a.getRules(), b.getRules()); + } + + if(a.getInitialSymbol() != b.getInitialSymbol()){ + out << "Initial symbol" << std::endl; + + out << "< " << a.getInitialSymbol() << std::endl; + out << "---" << std::endl; + out << "> " << b.getInitialSymbol() << std::endl; + } + + if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { + out << "Terminal alphabet" << std::endl; + + DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); + } +} + +template < class SymbolType > +void GrammarDiff::printDiff(const grammar::RightRG < SymbolType > & a, const grammar::RightRG < SymbolType > & b, std::ostream & out ) { + out << "GrammarsComparer" << std::endl; + + if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { + out << "Nonterminal alphabet" << std::endl; + + DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); + } + + if(a.getRules() != b.getRules()){ + out << "Rules" << std::endl; + + DiffAux::mapDiff(a.getRules(), b.getRules()); + } + + if(a.getInitialSymbol() != b.getInitialSymbol()){ + out << "Initial symbol" << std::endl; + + out << "< " << a.getInitialSymbol() << std::endl; + out << "---" << std::endl; + out << "> " << b.getInitialSymbol() << std::endl; + } + + if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { + out << "Terminal alphabet" << std::endl; + + DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); + } +} + +template < class SymbolType > +void GrammarDiff::printDiff(const grammar::LG < SymbolType > & a, const grammar::LG < SymbolType > & b, std::ostream & out ) { + out << "GrammarsComparer" << std::endl; + + if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { + out << "Nonterminal alphabet" << std::endl; + + DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); + } + + if(a.getRules() != b.getRules()){ + out << "Rules" << std::endl; + + DiffAux::mapDiff(a.getRules(), b.getRules()); + } + + if(a.getInitialSymbol() != b.getInitialSymbol()){ + out << "Initial symbol" << std::endl; + + out << "< " << a.getInitialSymbol() << std::endl; + out << "---" << std::endl; + out << "> " << b.getInitialSymbol() << std::endl; + } + + if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { + out << "Terminal alphabet" << std::endl; + + DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); + } +} + +template < class SymbolType > +void GrammarDiff::printDiff(const grammar::CFG < SymbolType > & a, const grammar::CFG < SymbolType > & b, std::ostream & out ) { + out << "GrammarsComparer" << std::endl; + + if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { + out << "Nonterminal alphabet" << std::endl; + + DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); + } + + if(a.getRules() != b.getRules()){ + out << "Rules" << std::endl; + + DiffAux::mapDiff(a.getRules(), b.getRules()); + } + + if(a.getInitialSymbol() != b.getInitialSymbol()){ + out << "Initial symbol" << std::endl; + + out << "< " << a.getInitialSymbol() << std::endl; + out << "---" << std::endl; + out << "> " << b.getInitialSymbol() << std::endl; + } + + if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { + out << "Terminal alphabet" << std::endl; + + DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); + } +} + +template < class SymbolType > +void GrammarDiff::printDiff(const grammar::EpsilonFreeCFG < SymbolType > & a, const grammar::EpsilonFreeCFG < SymbolType > & b, std::ostream & out ) { + out << "GrammarsComparer" << std::endl; + + if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { + out << "Nonterminal alphabet" << std::endl; + + DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); + } + + if(a.getRules() != b.getRules()){ + out << "Rules" << std::endl; + + DiffAux::mapDiff(a.getRules(), b.getRules()); + } + + if(a.getInitialSymbol() != b.getInitialSymbol()){ + out << "Initial symbol" << std::endl; + + out << "< " << a.getInitialSymbol() << std::endl; + out << "---" << std::endl; + out << "> " << b.getInitialSymbol() << std::endl; + } + + if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { + out << "Terminal alphabet" << std::endl; + + DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); + } +} + +template < class SymbolType > +void GrammarDiff::printDiff(const grammar::CNF < SymbolType > & a, const grammar::CNF < SymbolType > & b, std::ostream & out ) { + out << "GrammarsComparer" << std::endl; + + if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { + out << "Nonterminal alphabet" << std::endl; + + DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); + } + + if(a.getRules() != b.getRules()){ + out << "Rules" << std::endl; + + DiffAux::mapDiff(a.getRules(), b.getRules()); + } + + if(a.getInitialSymbol() != b.getInitialSymbol()){ + out << "Initial symbol" << std::endl; + + out << "< " << a.getInitialSymbol() << std::endl; + out << "---" << std::endl; + out << "> " << b.getInitialSymbol() << std::endl; + } + + if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { + out << "Terminal alphabet" << std::endl; + + DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); + } +} + +template < class SymbolType > +void GrammarDiff::printDiff(const grammar::GNF < SymbolType > & a, const grammar::GNF < SymbolType > & b, std::ostream & out ) { + out << "GrammarsComparer" << std::endl; + + if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { + out << "Nonterminal alphabet" << std::endl; + + DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); + } + + if(a.getRules() != b.getRules()){ + out << "Rules" << std::endl; + + DiffAux::mapDiff(a.getRules(), b.getRules()); + } + + if(a.getInitialSymbol() != b.getInitialSymbol()){ + out << "Initial symbol" << std::endl; + + out << "< " << a.getInitialSymbol() << std::endl; + out << "---" << std::endl; + out << "> " << b.getInitialSymbol() << std::endl; + } + + if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { + out << "Terminal alphabet" << std::endl; + + DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); + } +} + +template < class SymbolType > +void GrammarDiff::printDiff(const grammar::CSG < SymbolType > & a, const grammar::CSG < SymbolType > & b, std::ostream & out ) { + out << "GrammarsComparer" << std::endl; + + if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { + out << "Nonterminal alphabet" << std::endl; + + DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); + } + + if(a.getRules() != b.getRules()){ + out << "Rules" << std::endl; + + DiffAux::mapDiff(a.getRules(), b.getRules()); + } + + if(a.getInitialSymbol() != b.getInitialSymbol()){ + out << "Initial symbol" << std::endl; + + out << "< " << a.getInitialSymbol() << std::endl; + out << "---" << std::endl; + out << "> " << b.getInitialSymbol() << std::endl; + } + + if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { + out << "Terminal alphabet" << std::endl; + + DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); + } +} + +template < class SymbolType > +void GrammarDiff::printDiff(const grammar::NonContractingGrammar < SymbolType > & a, const grammar::NonContractingGrammar < SymbolType > & b, std::ostream & out ) { + out << "GrammarsComparer" << std::endl; + + if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { + out << "Nonterminal alphabet" << std::endl; + + DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); + } + + if(a.getRules() != b.getRules()){ + out << "Rules" << std::endl; + + DiffAux::mapDiff(a.getRules(), b.getRules()); + } + + if(a.getInitialSymbol() != b.getInitialSymbol()){ + out << "Initial symbol" << std::endl; + + out << "< " << a.getInitialSymbol() << std::endl; + out << "---" << std::endl; + out << "> " << b.getInitialSymbol() << std::endl; + } + + if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { + out << "Terminal alphabet" << std::endl; + + DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); + } +} + +template < class SymbolType > +void GrammarDiff::printDiff(const grammar::ContextPreservingUnrestrictedGrammar < SymbolType > & a, const grammar::ContextPreservingUnrestrictedGrammar < SymbolType > & b, std::ostream & out ) { + out << "GrammarsComparer" << std::endl; + + if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { + out << "Nonterminal alphabet" << std::endl; + + DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); + } + + if(a.getRules() != b.getRules()){ + out << "Rules" << std::endl; + + DiffAux::mapDiff(a.getRules(), b.getRules()); + } + + if(a.getInitialSymbol() != b.getInitialSymbol()){ + out << "Initial symbol" << std::endl; + + out << "< " << a.getInitialSymbol() << std::endl; + out << "---" << std::endl; + out << "> " << b.getInitialSymbol() << std::endl; + } + + if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { + out << "Terminal alphabet" << std::endl; + + DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); + } +} + +template < class SymbolType > +void GrammarDiff::printDiff(const grammar::UnrestrictedGrammar < SymbolType > & a, const grammar::UnrestrictedGrammar < SymbolType > & b, std::ostream & out ) { + out << "GrammarsComparer" << std::endl; + + if(a.getNonterminalAlphabet() != b.getNonterminalAlphabet()) { + out << "Nonterminal alphabet" << std::endl; + + DiffAux::setDiff(a.getNonterminalAlphabet(), b.getNonterminalAlphabet()); + } + + if(a.getRules() != b.getRules()){ + out << "Rules" << std::endl; + + DiffAux::mapDiff(a.getRules(), b.getRules()); + } + + if(a.getInitialSymbol() != b.getInitialSymbol()){ + out << "Initial symbol" << std::endl; + + out << "< " << a.getInitialSymbol() << std::endl; + out << "---" << std::endl; + out << "> " << b.getInitialSymbol() << std::endl; + } + + if(a.getTerminalAlphabet() != b.getTerminalAlphabet()) { + out << "Terminal alphabet" << std::endl; + + DiffAux::setDiff(a.getTerminalAlphabet(), b.getTerminalAlphabet()); + } +} + +template < class T > +int GrammarDiff::diff(const T & a, const T & b, std::ostream & out ) { + if(!GrammarCompare::compare(a, b)) { + GrammarDiff::printDiff ( a, b, out ); + return 1; + } else { + return 0; + } +} + +} /* namespace compare */ + +#endif /* GRAMMAR_DIFF_H_ */ -- GitLab