Skip to content
Snippets Groups Projects
Commit 9ccbf23a authored by Jan Trávníček's avatar Jan Trávníček
Browse files

cleanup

parent 2b27de55
No related branches found
No related tags found
No related merge requests found
...@@ -18,13 +18,13 @@ namespace grammar { ...@@ -18,13 +18,13 @@ namespace grammar {
   
namespace parsing { 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 ) { if ( std::any_of ( rhsds.begin ( ), rhsds.end ( ), [] ( const std::vector < alphabet::Symbol > & rhs ) {
return rhs.size ( ) == 0; return rhs.size ( ) == 0;
} ) ) } ) )
return HandleFirstFollowConflict::handleFirstFollowConflict ( grammar, terminal, nonterminal, rhsds ); HandleFirstFollowConflict::handleFirstFollowConflict ( grammar, terminal, nonterminal, rhsds );
else else
return HandleFirstFirstConflict::handleFirstFirstConflict ( grammar, terminal, nonterminal, rhsds ); HandleFirstFirstConflict::handleFirstFirstConflict ( grammar, terminal, nonterminal, rhsds );
} }
   
grammar::CFG DeterministicLL1Grammar::convert ( const grammar::CFG & param ) { grammar::CFG DeterministicLL1Grammar::convert ( const grammar::CFG & param ) {
...@@ -34,7 +34,6 @@ 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 ); 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 deterministic = true;
bool cantTransform = true;
bool errorInEpsilon = false; 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 ) 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 ) { ...@@ -44,23 +43,17 @@ grammar::CFG DeterministicLL1Grammar::convert ( const grammar::CFG & param ) {
continue; continue;
} }
   
deterministic = false; transformToLL1 ( grammar, elem.first.first.get < alphabet::Symbol > ( ), elem.first.second, elem.second );
break;
if ( transformToLL1 ( grammar, elem.first.first.get < alphabet::Symbol > ( ), elem.first.second, elem.second ) ) {
cantTransform = false;
break;
}
} }
   
if ( deterministic ) if ( deterministic )
return grammar; return grammar;
   
if ( cantTransform ) { if ( errorInEpsilon )
if ( errorInEpsilon ) throw exception::AlibException ( "Cant handle conflict in epsilon" );
throw exception::AlibException ( "Cant handle conflict in epsilon" );
else
throw exception::AlibException ( "Unable to find transformable pattern in rules of the grammar" );
}
} }
} }
   
......
...@@ -16,15 +16,14 @@ namespace grammar { ...@@ -16,15 +16,14 @@ namespace grammar {
   
namespace parsing { 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 ) for ( const std::vector < alphabet::Symbol > & rhs : rhsds )
if ( ( rhs.size ( ) > 0 ) && grammar.getNonterminalAlphabet ( ).count ( rhs[0] ) && First::first ( grammar, rhs ).count ( terminal ) ) { if ( ( rhs.size ( ) > 0 ) && grammar.getNonterminalAlphabet ( ).count ( rhs[0] ) && First::first ( grammar, rhs ).count ( terminal ) ) {
CornerSubstitution::cornerSubstitution ( grammar, terminal, nonterminal ); CornerSubstitution::cornerSubstitution ( grammar, terminal, nonterminal );
return true; return;
} }
   
LeftFactorize::leftFactorize ( grammar, terminal, nonterminal ); LeftFactorize::leftFactorize ( grammar, terminal, nonterminal );
return true;
} }
   
} /* namespace parsing */ } /* namespace parsing */
......
...@@ -18,8 +18,7 @@ namespace parsing { ...@@ -18,8 +18,7 @@ namespace parsing {
   
class HandleFirstFirstConflict { class HandleFirstFirstConflict {
public: 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 */ } /* namespace parsing */
......
...@@ -18,7 +18,7 @@ namespace grammar { ...@@ -18,7 +18,7 @@ namespace grammar {
   
namespace parsing { 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 > nullableNonterminals = properties::NullableNonterminals::getNullableNonterminals ( grammar );
   
std::set < alphabet::Symbol > symbolsEndingWithNonterminal = { nonterminal }; std::set < alphabet::Symbol > symbolsEndingWithNonterminal = { nonterminal };
...@@ -53,13 +53,12 @@ bool HandleFirstFollowConflict::handleFirstFollowConflict ( grammar::CFG & gramm ...@@ -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 ) 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 ) ) { 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 ); ExtractRightContext::extractRightContext ( grammar, terminal, symbolsEndingWithNonterminal );
return true; return;
} }
   
} }
   
AbsorbTerminalSymbol::absorbTerminalSymbol ( grammar, terminal, symbolsEndingWithNonterminal ); AbsorbTerminalSymbol::absorbTerminalSymbol ( grammar, terminal, symbolsEndingWithNonterminal );
return true;
} }
   
} /* namespace parsing */ } /* namespace parsing */
......
...@@ -19,8 +19,7 @@ namespace parsing { ...@@ -19,8 +19,7 @@ namespace parsing {
   
class HandleFirstFollowConflict { class HandleFirstFollowConflict {
public: 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 */ } /* namespace parsing */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment