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

randomize CFG

parent b5aad9d9
No related branches found
No related tags found
No related merge requests found
...@@ -16,6 +16,7 @@ auto RandomizeGrammarLeftRG = RandomizeGrammar::RegistratorWrapper < grammar::Le ...@@ -16,6 +16,7 @@ auto RandomizeGrammarLeftRG = RandomizeGrammar::RegistratorWrapper < grammar::Le
auto RandomizeGrammarLeftLG = RandomizeGrammar::RegistratorWrapper < grammar::LeftLG < >, grammar::LeftLG < > > ( RandomizeGrammar::randomize ); auto RandomizeGrammarLeftLG = RandomizeGrammar::RegistratorWrapper < grammar::LeftLG < >, grammar::LeftLG < > > ( RandomizeGrammar::randomize );
auto RandomizeGrammarRightRG = RandomizeGrammar::RegistratorWrapper < grammar::RightRG < >, grammar::RightRG < > > ( RandomizeGrammar::randomize ); auto RandomizeGrammarRightRG = RandomizeGrammar::RegistratorWrapper < grammar::RightRG < >, grammar::RightRG < > > ( RandomizeGrammar::randomize );
auto RandomizeGrammarRightLG = RandomizeGrammar::RegistratorWrapper < grammar::RightLG < >, grammar::RightLG < > > ( RandomizeGrammar::randomize ); auto RandomizeGrammarRightLG = RandomizeGrammar::RegistratorWrapper < grammar::RightLG < >, grammar::RightLG < > > ( RandomizeGrammar::randomize );
auto RandomizeGrammarCFG = RandomizeGrammar::RegistratorWrapper < grammar::CFG < >, grammar::CFG < > > ( RandomizeGrammar::randomize );
   
grammar::Grammar RandomizeGrammar::randomize ( const grammar::Grammar & grammar ) { grammar::Grammar RandomizeGrammar::randomize ( const grammar::Grammar & grammar ) {
return dispatch ( grammar.getData ( ) ); return dispatch ( grammar.getData ( ) );
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include <grammar/Regular/LeftRG.h> #include <grammar/Regular/LeftRG.h>
#include <grammar/Regular/RightLG.h> #include <grammar/Regular/RightLG.h>
#include <grammar/Regular/RightRG.h> #include <grammar/Regular/RightRG.h>
#include <grammar/ContextFree/CFG.h>
   
#include <common/Permutation.hpp> #include <common/Permutation.hpp>
   
...@@ -34,6 +35,9 @@ public: ...@@ -34,6 +35,9 @@ public:
static grammar::RightRG < SymbolType > randomize ( const grammar::RightRG < SymbolType > & gram ); static grammar::RightRG < SymbolType > randomize ( const grammar::RightRG < SymbolType > & gram );
template < class SymbolType > template < class SymbolType >
static grammar::RightLG < SymbolType > randomize ( const grammar::RightLG < SymbolType > & gram ); static grammar::RightLG < SymbolType > randomize ( const grammar::RightLG < SymbolType > & gram );
template < class SymbolType >
static grammar::CFG < SymbolType > randomize ( const grammar::CFG < SymbolType > & gram );
}; };
   
template < class SymbolType > template < class SymbolType >
...@@ -112,6 +116,27 @@ grammar::RightLG < SymbolType > RandomizeGrammar::randomize ( const grammar::Rig ...@@ -112,6 +116,27 @@ grammar::RightLG < SymbolType > RandomizeGrammar::randomize ( const grammar::Rig
return res; return res;
} }
   
template < class SymbolType >
grammar::CFG < SymbolType > RandomizeGrammar::randomize ( const grammar::CFG < SymbolType > & gram ) {
std::map < SymbolType, SymbolType > symbolPermutationMap = common::Permutation::permutationMap ( gram.getNonterminalAlphabet ( ) );
grammar::CFG < SymbolType > res ( symbolPermutationMap.find ( gram.getInitialSymbol ( ) )->second );
res.setNonterminalAlphabet ( gram.getNonterminalAlphabet ( ) );
res.setTerminalAlphabet ( gram.getTerminalAlphabet ( ) );
for ( const std::pair < SymbolType, std::set < std::vector < SymbolType > > > & rule : gram.getRules ( ) )
for ( const std::vector < SymbolType > & rhs : rule.second )
res.addRule ( symbolPermutationMap.find ( rule.first )->second, std::transform < SymbolType > ( rhs, [ & ] ( const SymbolType & elem ) {
if ( gram.getNonterminalAlphabet ( ).count ( elem ) )
return symbolPermutationMap.find ( elem )->second;
else
return elem;
} ) );
return res;
}
} /* namespace generate */ } /* namespace generate */
   
} /* namespace grammar */ } /* namespace grammar */
......
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