From 9ccbf23ac48eaaa3e44cc20734ef01e404af64ea Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Sun, 15 Nov 2015 14:00:42 +0100 Subject: [PATCH] cleanup --- .../parsing/DeterministicLL1Grammar.cpp | 25 +++++++------------ .../parsing/HandleFirstFirstConflict.cpp | 5 ++-- .../parsing/HandleFirstFirstConflict.h | 3 +-- .../parsing/HandleFirstFollowConflict.cpp | 5 ++-- .../parsing/HandleFirstFollowConflict.h | 3 +-- 5 files changed, 15 insertions(+), 26 deletions(-) diff --git a/alib2algo/src/grammar/parsing/DeterministicLL1Grammar.cpp b/alib2algo/src/grammar/parsing/DeterministicLL1Grammar.cpp index bc8e79a045..83381e302c 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 a5f5c7bd64..d0420f6a3e 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 b9bce9ccf0..542b13fe0c 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 092bcb4d78..191baa592f 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 2a9c41b5b8..c42d267c01 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 */ -- GitLab