diff --git a/alib2algo/src/grammar/parsing/DeterministicLL1Grammar.cpp b/alib2algo/src/grammar/parsing/DeterministicLL1Grammar.cpp index bc8e79a0455cd86fa5be6f0369ff69039a7eed9d..83381e302cd764a4f7dadcb0d56b904d37357691 100644 --- a/alib2algo/src/grammar/parsing/DeterministicLL1Grammar.cpp +++ b/alib2algo/src/grammar/parsing/DeterministicLL1Grammar.cpp @@ -18,13 +18,13 @@ namespace grammar { namespace parsing { -bool transformToLL1 ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal, const std::set < std::vector < alphabet::Symbol > > & rhsds ) { +void transformToLL1 ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal, const std::set < std::vector < alphabet::Symbol > > & rhsds ) { if ( std::any_of ( rhsds.begin ( ), rhsds.end ( ), [] ( const std::vector < alphabet::Symbol > & rhs ) { return rhs.size ( ) == 0; } ) ) - return HandleFirstFollowConflict::handleFirstFollowConflict ( grammar, terminal, nonterminal, rhsds ); + HandleFirstFollowConflict::handleFirstFollowConflict ( grammar, terminal, nonterminal, rhsds ); else - return HandleFirstFirstConflict::handleFirstFirstConflict ( grammar, terminal, nonterminal, rhsds ); + HandleFirstFirstConflict::handleFirstFirstConflict ( grammar, terminal, nonterminal, rhsds ); } grammar::CFG DeterministicLL1Grammar::convert ( const grammar::CFG & param ) { @@ -34,7 +34,6 @@ grammar::CFG DeterministicLL1Grammar::convert ( const grammar::CFG & param ) { std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > > parseTable = LL1ParseTable::parseTable ( grammar ); bool deterministic = true; - bool cantTransform = true; bool errorInEpsilon = false; for ( const std::pair < std::pair < std::variant < alphabet::Symbol, string::Epsilon >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > > & elem : parseTable ) @@ -44,23 +43,17 @@ grammar::CFG DeterministicLL1Grammar::convert ( const grammar::CFG & param ) { continue; } - deterministic = false; - - if ( transformToLL1 ( grammar, elem.first.first.get < alphabet::Symbol > ( ), elem.first.second, elem.second ) ) { - cantTransform = false; - break; - } + transformToLL1 ( grammar, elem.first.first.get < alphabet::Symbol > ( ), elem.first.second, elem.second ); + break; } + + if ( deterministic ) return grammar; - if ( cantTransform ) { - if ( errorInEpsilon ) - throw exception::AlibException ( "Cant handle conflict in epsilon" ); - else - throw exception::AlibException ( "Unable to find transformable pattern in rules of the grammar" ); - } + if ( errorInEpsilon ) + throw exception::AlibException ( "Cant handle conflict in epsilon" ); } } diff --git a/alib2algo/src/grammar/parsing/HandleFirstFirstConflict.cpp b/alib2algo/src/grammar/parsing/HandleFirstFirstConflict.cpp index a5f5c7bd64f470571200fc12b738b236af16ceee..d0420f6a3e35dccc12c3ce4ded31e89a37d93769 100644 --- a/alib2algo/src/grammar/parsing/HandleFirstFirstConflict.cpp +++ b/alib2algo/src/grammar/parsing/HandleFirstFirstConflict.cpp @@ -16,15 +16,14 @@ namespace grammar { namespace parsing { -bool HandleFirstFirstConflict::handleFirstFirstConflict ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal, const std::set < std::vector < alphabet::Symbol > > & rhsds ) { +void HandleFirstFirstConflict::handleFirstFirstConflict ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal, const std::set < std::vector < alphabet::Symbol > > & rhsds ) { for ( const std::vector < alphabet::Symbol > & rhs : rhsds ) if ( ( rhs.size ( ) > 0 ) && grammar.getNonterminalAlphabet ( ).count ( rhs[0] ) && First::first ( grammar, rhs ).count ( terminal ) ) { CornerSubstitution::cornerSubstitution ( grammar, terminal, nonterminal ); - return true; + return; } LeftFactorize::leftFactorize ( grammar, terminal, nonterminal ); - return true; } } /* namespace parsing */ diff --git a/alib2algo/src/grammar/parsing/HandleFirstFirstConflict.h b/alib2algo/src/grammar/parsing/HandleFirstFirstConflict.h index b9bce9ccf042d6c07ee71b360fc21df8d0065831..542b13fe0c3ff11df318cbfc4214afede682575b 100644 --- a/alib2algo/src/grammar/parsing/HandleFirstFirstConflict.h +++ b/alib2algo/src/grammar/parsing/HandleFirstFirstConflict.h @@ -18,8 +18,7 @@ namespace parsing { class HandleFirstFirstConflict { public: - static bool handleFirstFirstConflict ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal, const std::set < std::vector < alphabet::Symbol > > & rhsds ); - + static void handleFirstFirstConflict ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal, const std::set < std::vector < alphabet::Symbol > > & rhsds ); }; } /* namespace parsing */ diff --git a/alib2algo/src/grammar/parsing/HandleFirstFollowConflict.cpp b/alib2algo/src/grammar/parsing/HandleFirstFollowConflict.cpp index 092bcb4d780ac72b38fa3742ed770e71a1385e7d..191baa592f2a050e9b716491388e03a32aac909f 100644 --- a/alib2algo/src/grammar/parsing/HandleFirstFollowConflict.cpp +++ b/alib2algo/src/grammar/parsing/HandleFirstFollowConflict.cpp @@ -18,7 +18,7 @@ namespace grammar { namespace parsing { -bool HandleFirstFollowConflict::handleFirstFollowConflict ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal, const std::set < std::vector < alphabet::Symbol > > & /* rhsds */ ) { +void HandleFirstFollowConflict::handleFirstFollowConflict ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal, const std::set < std::vector < alphabet::Symbol > > & /* rhsds */ ) { std::set < alphabet::Symbol > nullableNonterminals = properties::NullableNonterminals::getNullableNonterminals ( grammar ); std::set < alphabet::Symbol > symbolsEndingWithNonterminal = { nonterminal }; @@ -53,13 +53,12 @@ bool HandleFirstFollowConflict::handleFirstFollowConflict ( grammar::CFG & gramm for ( std::vector < alphabet::Symbol >::const_iterator iter = rhs.begin ( ); iter + 1 != rhs.end ( ); ++iter ) if ( symbolsEndingWithNonterminal.count ( * iter ) && grammar.getNonterminalAlphabet ( ).count ( * ( iter + 1 ) ) && First::first ( grammar, std::vector < alphabet::Symbol > ( iter + 1, rhs.end ( ) ) ).count ( terminal ) ) { ExtractRightContext::extractRightContext ( grammar, terminal, symbolsEndingWithNonterminal ); - return true; + return; } } AbsorbTerminalSymbol::absorbTerminalSymbol ( grammar, terminal, symbolsEndingWithNonterminal ); - return true; } } /* namespace parsing */ diff --git a/alib2algo/src/grammar/parsing/HandleFirstFollowConflict.h b/alib2algo/src/grammar/parsing/HandleFirstFollowConflict.h index 2a9c41b5b84a7ac36c80e919bbc4ee1a84cc4504..c42d267c0141b92554d50fce184f04afd6bec54f 100644 --- a/alib2algo/src/grammar/parsing/HandleFirstFollowConflict.h +++ b/alib2algo/src/grammar/parsing/HandleFirstFollowConflict.h @@ -19,8 +19,7 @@ namespace parsing { class HandleFirstFollowConflict { public: - static bool handleFirstFollowConflict ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal, const std::set < std::vector < alphabet::Symbol > > & /* rhsds */ ); - + static void handleFirstFollowConflict ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal, const std::set < std::vector < alphabet::Symbol > > & /* rhsds */ ); }; } /* namespace parsing */