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

add follow of all symbols

parent 875c7917
No related branches found
No related tags found
No related merge requests found
...@@ -52,10 +52,7 @@ void Follow::follow ( const T & grammar, std::map < alphabet::Symbol, std::set < ...@@ -52,10 +52,7 @@ void Follow::follow ( const T & grammar, std::map < alphabet::Symbol, std::set <
} }
   
template < class T > template < class T >
std::set < std::variant < alphabet::Symbol, string::Epsilon > > Follow::follow ( const T & grammar, const alphabet::Symbol & nt ) { std::map< alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon > > > Follow::follow ( const T & grammar ) {
if ( !grammar.getNonterminalAlphabet ( ).count ( nt ) )
throw exception::AlibException ( "Follow: Given symbol is not nonterminal." );
/* /*
* 1. Follow(S) = { \varepsilon } * 1. Follow(S) = { \varepsilon }
* Follow(A) = {} forall A \in N, A \neq S * Follow(A) = {} forall A \in N, A \neq S
...@@ -84,21 +81,43 @@ std::set < std::variant < alphabet::Symbol, string::Epsilon > > Follow::follow ( ...@@ -84,21 +81,43 @@ std::set < std::variant < alphabet::Symbol, string::Epsilon > > Follow::follow (
followSet1 = followSet2; followSet1 = followSet2;
} while ( true ); } while ( true );
   
return followSet1[nt]; return followSet1;
}
template < class T >
std::set < std::variant < alphabet::Symbol, string::Epsilon > > Follow::follow ( const T & grammar, const alphabet::Symbol & nt ) {
if ( !grammar.getNonterminalAlphabet ( ).count ( nt ) )
throw exception::AlibException ( "Follow: Given symbol is not nonterminal." );
return follow(grammar)[nt];
}
auto FollowCFG = FollowBase1::RegistratorWrapper < FollowResult1, grammar::CFG > ( Follow::getInstance1 ( ), Follow::follow );
auto FollowEpsilonFreeCFG = FollowBase1::RegistratorWrapper < FollowResult1, grammar::EpsilonFreeCFG > ( Follow::getInstance1 ( ), Follow::follow );
auto FollowGNF = FollowBase1::RegistratorWrapper < FollowResult1, grammar::GNF > ( Follow::getInstance1 ( ), Follow::follow );
auto FollowCNF = FollowBase1::RegistratorWrapper < FollowResult1, grammar::CNF > ( Follow::getInstance1 ( ), Follow::follow );
auto FollowLG = FollowBase1::RegistratorWrapper < FollowResult1, grammar::LG > ( Follow::getInstance1 ( ), Follow::follow );
auto FollowLeftLG = FollowBase1::RegistratorWrapper < FollowResult1, grammar::LeftLG > ( Follow::getInstance1 ( ), Follow::follow );
auto FollowLeftRG = FollowBase1::RegistratorWrapper < FollowResult1, grammar::LeftRG > ( Follow::getInstance1 ( ), Follow::follow );
auto FollowRightLG = FollowBase1::RegistratorWrapper < FollowResult1, grammar::RightLG > ( Follow::getInstance1 ( ), Follow::follow );
auto FollowRightRG = FollowBase1::RegistratorWrapper < FollowResult1, grammar::RightRG > ( Follow::getInstance1 ( ), Follow::follow );
std::map< alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon > > > Follow::follow ( const grammar::Grammar & grammar ) {
return getInstance1 ( ).dispatch ( grammar.getData ( ) );
} }
   
auto FollowCFG = Follow::RegistratorWrapper < std::set < std::variant < alphabet::Symbol, string::Epsilon > >, grammar::CFG > ( Follow::getInstance ( ), Follow::follow ); auto FollowCFG2 = FollowBase2::RegistratorWrapper < FollowResult2, grammar::CFG > ( Follow::getInstance2 ( ), Follow::follow );
auto FollowEpsilonFreeCFG = Follow::RegistratorWrapper < std::set < std::variant < alphabet::Symbol, string::Epsilon > >, grammar::EpsilonFreeCFG > ( Follow::getInstance ( ), Follow::follow ); auto FollowEpsilonFreeCFG2 = FollowBase2::RegistratorWrapper < FollowResult2, grammar::EpsilonFreeCFG > ( Follow::getInstance2 ( ), Follow::follow );
auto FollowGNF = Follow::RegistratorWrapper < std::set < std::variant < alphabet::Symbol, string::Epsilon > >, grammar::GNF > ( Follow::getInstance ( ), Follow::follow ); auto FollowGNF2 = FollowBase2::RegistratorWrapper < FollowResult2, grammar::GNF > ( Follow::getInstance2 ( ), Follow::follow );
auto FollowCNF = Follow::RegistratorWrapper < std::set < std::variant < alphabet::Symbol, string::Epsilon > >, grammar::CNF > ( Follow::getInstance ( ), Follow::follow ); auto FollowCNF2 = FollowBase2::RegistratorWrapper < FollowResult2, grammar::CNF > ( Follow::getInstance2 ( ), Follow::follow );
auto FollowLG = Follow::RegistratorWrapper < std::set < std::variant < alphabet::Symbol, string::Epsilon > >, grammar::LG > ( Follow::getInstance ( ), Follow::follow ); auto FollowLG2 = FollowBase2::RegistratorWrapper < FollowResult2, grammar::LG > ( Follow::getInstance2 ( ), Follow::follow );
auto FollowLeftLG = Follow::RegistratorWrapper < std::set < std::variant < alphabet::Symbol, string::Epsilon > >, grammar::LeftLG > ( Follow::getInstance ( ), Follow::follow ); auto FollowLeftLG2 = FollowBase2::RegistratorWrapper < FollowResult2, grammar::LeftLG > ( Follow::getInstance2 ( ), Follow::follow );
auto FollowLeftRG = Follow::RegistratorWrapper < std::set < std::variant < alphabet::Symbol, string::Epsilon > >, grammar::LeftRG > ( Follow::getInstance ( ), Follow::follow ); auto FollowLeftRG2 = FollowBase2::RegistratorWrapper < FollowResult2, grammar::LeftRG > ( Follow::getInstance2 ( ), Follow::follow );
auto FollowRightLG = Follow::RegistratorWrapper < std::set < std::variant < alphabet::Symbol, string::Epsilon > >, grammar::RightLG > ( Follow::getInstance ( ), Follow::follow ); auto FollowRightLG2 = FollowBase2::RegistratorWrapper < FollowResult2, grammar::RightLG > ( Follow::getInstance2 ( ), Follow::follow );
auto FollowRightRG = Follow::RegistratorWrapper < std::set < std::variant < alphabet::Symbol, string::Epsilon > >, grammar::RightRG > ( Follow::getInstance ( ), Follow::follow ); auto FollowRightRG2 = FollowBase2::RegistratorWrapper < FollowResult2, grammar::RightRG > ( Follow::getInstance2 ( ), Follow::follow );
   
std::set < std::variant < alphabet::Symbol, string::Epsilon > > Follow::follow ( const grammar::Grammar & grammar, const alphabet::Symbol & nt ) { std::set < std::variant < alphabet::Symbol, string::Epsilon > > Follow::follow ( const grammar::Grammar & grammar, const alphabet::Symbol & nt ) {
return getInstance ( ).dispatch ( grammar.getData ( ), nt ); return getInstance2 ( ).dispatch ( grammar.getData ( ), nt );
} }
   
} /* namespace parsing */ } /* namespace parsing */
......
...@@ -20,15 +20,15 @@ namespace grammar { ...@@ -20,15 +20,15 @@ namespace grammar {
   
namespace parsing { namespace parsing {
   
class Follow : public std::SingleDispatchLastStaticParam < std::set < std::variant < alphabet::Symbol, string::Epsilon > >, grammar::GrammarBase, const alphabet::Symbol & > { typedef std::map< alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon > > > FollowResult1;
template < class T > typedef std::SingleDispatch < FollowResult1, grammar::GrammarBase > FollowBase1;
static void follow ( const T & grammar, std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon > > > & followSet );
   
public: typedef std::set < std::variant < alphabet::Symbol, string::Epsilon > > FollowResult2;
static std::set < std::variant < alphabet::Symbol, string::Epsilon > > follow ( const grammar::Grammar & grammar, const alphabet::Symbol & nt ); typedef std::SingleDispatchLastStaticParam < FollowResult2, grammar::GrammarBase, const alphabet::Symbol & > FollowBase2;
   
class Follow : public FollowBase1, public FollowBase2 {
template < class T > template < class T >
static std::set < std::variant < alphabet::Symbol, string::Epsilon > > follow ( const T & grammar, const alphabet::Symbol & nt ); static void follow ( const T & grammar, std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon > > > & followSet );
   
static Follow & getInstance ( ) { static Follow & getInstance ( ) {
static Follow res; static Follow res;
...@@ -36,6 +36,25 @@ public: ...@@ -36,6 +36,25 @@ public:
return res; return res;
} }
   
public:
static FollowResult1 follow ( const grammar::Grammar & grammar );
static FollowResult2 follow ( const grammar::Grammar & grammar, const alphabet::Symbol & nt );
template < class T >
static FollowResult1 follow ( const T & grammar );
template < class T >
static FollowResult2 follow ( const T & grammar, const alphabet::Symbol & nt );
static FollowBase1 & getInstance1 ( ) {
return getInstance ( );
}
static FollowBase2 & getInstance2 ( ) {
return getInstance ( );
}
}; };
   
} /* 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