diff --git a/alib2data/src/grammar/ContextFree/CNF.h b/alib2data/src/grammar/ContextFree/CNF.h index a24e260b36eb96b252304580ab689f6fef4e9233..d237a009252827ec83d2db728337eeb3c6cb4ba0 100644 --- a/alib2data/src/grammar/ContextFree/CNF.h +++ b/alib2data/src/grammar/ContextFree/CNF.h @@ -145,28 +145,23 @@ GrammarBase * CNF < SymbolType >::plunder ( ) && { template < class SymbolType > bool CNF < SymbolType >::addRule ( SymbolType leftHandSide, std::variant < SymbolType, std::pair < SymbolType, SymbolType > > rightHandSide ) { - if ( rightHandSide.template is < SymbolType > ( ) ) { - if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) ) - throw GrammarException ( "Rule must rewrite nonterminal symbol" ); + if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) ) + throw GrammarException ( "Rule must rewrite nonterminal symbol" ); + if ( rightHandSide.template is < SymbolType > ( ) ) { if ( !getTerminalAlphabet ( ).count ( rightHandSide.template get < SymbolType > ( ) ) ) throw GrammarException ( "Rule must rewrite to terminal symbol" ); - - return rules[std::move ( leftHandSide )].insert ( std::move ( rightHandSide ) ).second; } else { const std::pair < SymbolType, SymbolType > rhs = rightHandSide.template get < std::pair < SymbolType, SymbolType > > ( ); - if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) ) - throw GrammarException ( "Rule must rewrite nonterminal symbol" ); - if ( !getNonterminalAlphabet ( ).count ( rhs.first ) ) throw GrammarException ( "Symbol \"" + std::to_string ( rhs.first ) + "\" is not a nonterminal symbol" ); if ( !getNonterminalAlphabet ( ).count ( rhs.second ) ) throw GrammarException ( "Symbol \"" + std::to_string ( rhs.second ) + "\" is not a nonterminal symbol" ); - - return rules[std::move ( leftHandSide )].insert ( std::move ( rightHandSide ) ).second; } + + return rules[std::move ( leftHandSide )].insert ( std::move ( rightHandSide ) ).second; } template < class SymbolType > diff --git a/alib2data/src/grammar/ContextFree/EpsilonFreeCFG.h b/alib2data/src/grammar/ContextFree/EpsilonFreeCFG.h index 809b4fcc08afda3a82ab25bef6b7f5b21d8691da..4c136ba72583d8bcad270fe5ddc5c6eafc9a8640 100644 --- a/alib2data/src/grammar/ContextFree/EpsilonFreeCFG.h +++ b/alib2data/src/grammar/ContextFree/EpsilonFreeCFG.h @@ -140,9 +140,7 @@ GrammarBase * EpsilonFreeCFG < SymbolType >::plunder ( ) && { template < class SymbolType > bool EpsilonFreeCFG < SymbolType >::addRule ( SymbolType leftHandSide, std::vector < SymbolType > rightHandSide ) { - int rSize = rightHandSide.size ( ); - - if ( rSize == 0 ) + if ( rightHandSide.size ( ) == 0 ) throw GrammarException ( "Epsilon rule is not allowed" ); if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) ) diff --git a/alib2data/src/grammar/ContextFree/LG.h b/alib2data/src/grammar/ContextFree/LG.h index 4c81ce04f88f181ec6dcda8a1ccac687b9819fba..6fa87be0b994dd1f72301ba3ac2336e71a367450 100644 --- a/alib2data/src/grammar/ContextFree/LG.h +++ b/alib2data/src/grammar/ContextFree/LG.h @@ -148,27 +148,25 @@ bool LG < SymbolType >::addRule ( SymbolType leftHandSide, std::variant < std::v if ( rightHandSide.template is < std::vector < SymbolType > > ( ) ) { const std::vector < SymbolType > & rhs = rightHandSide.template get < std::vector < SymbolType > > ( ); - for ( const auto & symbol : rhs ) + for ( const SymbolType & symbol : rhs ) if ( !getTerminalAlphabet ( ).count ( symbol ) ) throw GrammarException ( "Symbol " + std::to_string ( symbol ) + " is not a terminal symbol" ); - - return rules[std::move ( leftHandSide )].insert ( std::move ( rightHandSide ) ).second; } else { const std::tuple < std::vector < SymbolType >, SymbolType, std::vector < SymbolType > > & rhs = rightHandSide.template get < std::tuple < std::vector < SymbolType >, SymbolType, std::vector < SymbolType > > > ( ); - for ( const auto & symbol : std::get < 0 > ( rhs ) ) + for ( const SymbolType & symbol : std::get < 0 > ( rhs ) ) if ( !getTerminalAlphabet ( ).count ( symbol ) ) throw GrammarException ( "Symbol " + std::to_string ( symbol ) + " is not a terminal symbol" ); if ( !getNonterminalAlphabet ( ).count ( std::get < 1 > ( rhs ) ) ) throw GrammarException ( "Symbol " + std::to_string ( std::get < 1 > ( rhs ) ) + " is not a nonterminal symbol" ); - for ( const auto & symbol : std::get < 2 > ( rhs ) ) + for ( const SymbolType & symbol : std::get < 2 > ( rhs ) ) if ( !getTerminalAlphabet ( ).count ( symbol ) ) throw GrammarException ( "Symbol " + std::to_string ( symbol ) + " is not a terminal symbol" ); - - return rules[std::move ( leftHandSide )].insert ( std::move ( rightHandSide ) ).second; } + + return rules[std::move ( leftHandSide )].insert ( std::move ( rightHandSide ) ).second; } template < class SymbolType > diff --git a/alib2data/src/grammar/ContextSensitive/CSG.h b/alib2data/src/grammar/ContextSensitive/CSG.h index a2418715190d7b9a4381025a938f5c9d45129ebf..13cf1f45552952963efb3494e7bee7a61de0bf28 100644 --- a/alib2data/src/grammar/ContextSensitive/CSG.h +++ b/alib2data/src/grammar/ContextSensitive/CSG.h @@ -134,28 +134,26 @@ GrammarBase * CSG < SymbolType >::plunder ( ) && { template < class SymbolType > bool CSG < SymbolType >::addRule ( std::vector < SymbolType > lContext, SymbolType leftHandSide, std::vector < SymbolType > rContext, std::vector < SymbolType > rightHandSide ) { - int rSize = rightHandSide.size ( ); + for ( const SymbolType & symbol : lContext ) + if ( !getTerminalAlphabet ( ).count ( symbol ) && !getNonterminalAlphabet ( ).count ( symbol ) ) + throw GrammarException ( "Symbol \"" + std::to_string ( symbol ) + "\" is not neither terminal nor nonterminal symbol" ); - if ( rSize == 0 ) { - throw GrammarException ( "Epsilon rule is not allowed" ); - } else { - for ( const SymbolType & symbol : lContext ) - if ( !getTerminalAlphabet ( ).count ( symbol ) && !getNonterminalAlphabet ( ).count ( symbol ) ) - throw GrammarException ( "Symbol \"" + std::to_string ( symbol ) + "\" is not neither terminal nor nonterminal symbol" ); + if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) ) + throw GrammarException ( "Rule must rewrite nonterminal symbol" ); - if ( !getNonterminalAlphabet ( ).count ( leftHandSide ) ) - throw GrammarException ( "Rule must rewrite nonterminal symbol" ); - - for ( const SymbolType & symbol : rContext ) - if ( !getTerminalAlphabet ( ).count ( symbol ) && !getNonterminalAlphabet ( ).count ( symbol ) ) - throw GrammarException ( "Symbol \"" + std::to_string ( symbol ) + "\" is not neither terminal nor nonterminal symbol" ); + for ( const SymbolType & symbol : rContext ) + if ( !getTerminalAlphabet ( ).count ( symbol ) && !getNonterminalAlphabet ( ).count ( symbol ) ) + throw GrammarException ( "Symbol \"" + std::to_string ( symbol ) + "\" is not neither terminal nor nonterminal symbol" ); + if ( rightHandSide.size ( ) == 0 ) { + throw GrammarException ( "Epsilon rule is not allowed" ); + } else { for ( const SymbolType & symbol : rightHandSide ) if ( !getTerminalAlphabet ( ).count ( symbol ) && !getNonterminalAlphabet ( ).count ( symbol ) ) throw GrammarException ( "Symbol \"" + std::to_string ( symbol ) + "\" is not neither terminal nor nonterminal symbol" ); - - return rules[make_tuple ( std::move ( lContext ), std::move ( leftHandSide ), std::move ( rContext ) )].insert ( std::move ( rightHandSide ) ).second; } + + return rules[make_tuple ( std::move ( lContext ), std::move ( leftHandSide ), std::move ( rContext ) )].insert ( std::move ( rightHandSide ) ).second; } template < class SymbolType > diff --git a/alib2data/src/grammar/ContextSensitive/NonContractingGrammar.h b/alib2data/src/grammar/ContextSensitive/NonContractingGrammar.h index de01bff186d36be6aeb443f54f13501c3b1a1627..05988372ac91a99b049e98c583bdbdb0c7776f3b 100644 --- a/alib2data/src/grammar/ContextSensitive/NonContractingGrammar.h +++ b/alib2data/src/grammar/ContextSensitive/NonContractingGrammar.h @@ -135,10 +135,6 @@ GrammarBase * NonContractingGrammar < SymbolType >::plunder ( ) && { template < class SymbolType > bool NonContractingGrammar < SymbolType >::addRule ( std::vector < SymbolType > leftHandSide, std::vector < SymbolType > rightHandSide ) { int lSize = leftHandSide.size ( ); - int rSize = rightHandSide.size ( ); - - if ( lSize > rSize ) - throw GrammarException ( "Invalid size of right hand side of a rule" ); if ( std::all_of ( leftHandSide.begin ( ), leftHandSide.end ( ), [this] ( const SymbolType symbol ) { return !getNonterminalAlphabet ( ).count ( symbol ); @@ -149,6 +145,11 @@ bool NonContractingGrammar < SymbolType >::addRule ( std::vector < SymbolType > if ( !getTerminalAlphabet ( ).count ( symbol ) && !getNonterminalAlphabet ( ).count ( symbol ) ) throw GrammarException ( "Symbol \"" + std::to_string ( symbol ) + "\" is not neither terminal nor nonterminal symbol" ); + int rSize = rightHandSide.size ( ); + + if ( lSize > rSize ) + throw GrammarException ( "Invalid size of right hand side of a rule" ); + for ( const SymbolType & symbol : rightHandSide ) if ( !getTerminalAlphabet ( ).count ( symbol ) && !getNonterminalAlphabet ( ).count ( symbol ) ) throw GrammarException ( "Symbol \"" + std::to_string ( symbol ) + "\" is not neither terminal nor nonterminal symbol" ); diff --git a/alib2data/src/grammar/Regular/LeftLG.h b/alib2data/src/grammar/Regular/LeftLG.h index 19229345d7c25a7bec896718b87b027b77054a2b..a9d357117ad0ea3c685855e139d4bf73f0572aa6 100644 --- a/alib2data/src/grammar/Regular/LeftLG.h +++ b/alib2data/src/grammar/Regular/LeftLG.h @@ -148,8 +148,6 @@ bool LeftLG < SymbolType >::addRule ( SymbolType leftHandSide, std::variant < st for ( const auto & symbol : rightHandSide.template get < std::vector < SymbolType > > ( ) ) if ( !getTerminalAlphabet ( ).count ( symbol ) ) throw GrammarException ( "Symbol " + std::to_string ( symbol ) + " is not a terminal symbol" ); - - return rules[std::move ( leftHandSide )].insert ( std::move ( rightHandSide ) ).second; } else { const std::pair < SymbolType, std::vector < SymbolType > > & rhs = rightHandSide.template get < std::pair < SymbolType, std::vector < SymbolType > > > ( ); @@ -159,9 +157,9 @@ bool LeftLG < SymbolType >::addRule ( SymbolType leftHandSide, std::variant < st for ( const auto & symbol : rhs.second ) if ( !getTerminalAlphabet ( ).count ( symbol ) ) throw GrammarException ( "Symbol " + std::to_string ( symbol ) + " is not a terminal symbol" ); - - return rules[std::move ( leftHandSide )].insert ( std::move ( rightHandSide ) ).second; } + + return rules [ std::move ( leftHandSide ) ].insert ( std::move ( rightHandSide ) ).second; } template < class SymbolType > diff --git a/alib2data/src/grammar/Regular/LeftRG.h b/alib2data/src/grammar/Regular/LeftRG.h index 7bc223fcf943d0b85f74541180d43f0240c3255d..47fc58e38581d3feb8c8bcc6b4ebf3ff81bcb368 100644 --- a/alib2data/src/grammar/Regular/LeftRG.h +++ b/alib2data/src/grammar/Regular/LeftRG.h @@ -227,16 +227,14 @@ bool LeftRG < SymbolType >::addRule ( SymbolType leftHandSide, std::variant < Sy if ( !getTerminalAlphabet ( ).count ( rhs ) ) throw GrammarException ( "Rule must rewrite to terminal symbol" ); - - return rules[std::move ( leftHandSide )].insert ( std::move ( rightHandSide ) ).second; } else { const std::pair < SymbolType, SymbolType > & rhs = rightHandSide.template get < std::pair < SymbolType, SymbolType > > ( ); if ( !getNonterminalAlphabet ( ).count ( rhs.first ) || !getTerminalAlphabet ( ).count ( rhs.second ) ) throw GrammarException ( "Rule must rewrite to terminal symbol followed by nonterminal symbol" ); - - return rules[std::move ( leftHandSide )].insert ( std::move ( rightHandSide ) ).second; } + + return rules [ std::move ( leftHandSide ) ].insert ( std::move ( rightHandSide ) ).second; } template < class SymbolType > diff --git a/alib2data/src/grammar/Regular/RightLG.h b/alib2data/src/grammar/Regular/RightLG.h index ec0c428fcd57ec1112a6761e24b4d9d00e862b3d..c3d430c30b39754e5a7d81820eeab3650e7b2e5f 100644 --- a/alib2data/src/grammar/Regular/RightLG.h +++ b/alib2data/src/grammar/Regular/RightLG.h @@ -148,8 +148,6 @@ bool RightLG < SymbolType >::addRule ( SymbolType leftHandSide, std::variant < s for ( const auto & symbol : rightHandSide.template get < std::vector < SymbolType > > ( ) ) if ( !getTerminalAlphabet ( ).count ( symbol ) ) throw GrammarException ( "Symbol " + std::to_string ( symbol ) + " is not a terminal symbol" ); - - return rules[std::move ( leftHandSide )].insert ( std::move ( rightHandSide ) ).second; } else { const std::pair < std::vector < SymbolType >, SymbolType > & rhs = rightHandSide.template get < std::pair < std::vector < SymbolType >, SymbolType > > ( ); @@ -159,9 +157,9 @@ bool RightLG < SymbolType >::addRule ( SymbolType leftHandSide, std::variant < s for ( const auto & symbol : rhs.first ) if ( !getTerminalAlphabet ( ).count ( symbol ) ) throw GrammarException ( "Symbol " + std::to_string ( symbol ) + " is not a terminal symbol" ); - - return rules[std::move ( leftHandSide )].insert ( std::move ( rightHandSide ) ).second; } + + return rules [ std::move ( leftHandSide ) ].insert ( std::move ( rightHandSide ) ).second; } template < class SymbolType > diff --git a/alib2data/src/grammar/Regular/RightRG.h b/alib2data/src/grammar/Regular/RightRG.h index 359d498efd51e67cd9d026d159cbcb6d20f72908..de42427fa2005804b7d69f3eafb80b3b0d82a848 100644 --- a/alib2data/src/grammar/Regular/RightRG.h +++ b/alib2data/src/grammar/Regular/RightRG.h @@ -170,16 +170,14 @@ bool RightRG < SymbolType >::addRule ( SymbolType leftHandSide, std::variant < S if ( !getTerminalAlphabet ( ).count ( rhs ) ) throw GrammarException ( "Rule must rewrite to terminal symbol" ); - - return rules[std::move ( leftHandSide )].insert ( std::move ( rightHandSide ) ).second; } else { const std::pair < SymbolType, SymbolType > & rhs = rightHandSide.template get < std::pair < SymbolType, SymbolType > > ( ); if ( !getTerminalAlphabet ( ).count ( rhs.first ) || !getNonterminalAlphabet ( ).count ( rhs.second ) ) throw GrammarException ( "Rule must rewrite to terminal symbol followed by nonterminal symbol" ); - - return rules[std::move ( leftHandSide )].insert ( std::move ( rightHandSide ) ).second; } + + return rules[std::move ( leftHandSide )].insert ( std::move ( rightHandSide ) ).second; } template < class SymbolType >