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

concrete implementation of SipleRulesRemover for EpsilonFreeCFG

parent a0b71e57
No related branches found
No related tags found
No related merge requests found
......@@ -29,9 +29,6 @@ namespace grammar {
namespace simplify {
 
class SimpleRulesRemover {
template<class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > >
static T removeEpsilonFree( const T & origGrammar );
template<class T, class SymbolType = typename grammar::SymbolTypeOfGrammar < T > >
static T removeNonEpsilonFree( const T & origGrammar );
 
......@@ -57,7 +54,7 @@ public:
};
 
template < class T, class SymbolType >
T SimpleRulesRemover::removeEpsilonFree( const T & origGrammar ) {
T SimpleRulesRemover::removeNonEpsilonFree( const T & origGrammar ) {
T grammar(origGrammar.getInitialSymbol());
 
for( const auto & symbol : origGrammar.getNonterminalAlphabet() )
......@@ -79,14 +76,17 @@ T SimpleRulesRemover::removeEpsilonFree( const T & origGrammar ) {
}
}
 
grammar.setGeneratesEpsilon(origGrammar.getGeneratesEpsilon());
return grammar;
}
 
template < class T, class SymbolType >
T SimpleRulesRemover::removeNonEpsilonFree( const T & origGrammar ) {
T grammar(origGrammar.getInitialSymbol());
template < class SymbolType >
grammar::CFG < SymbolType > SimpleRulesRemover::remove(const grammar::CFG < SymbolType > & origGrammar) {
return removeNonEpsilonFree(origGrammar);
}
template < class SymbolType >
grammar::EpsilonFreeCFG < SymbolType > SimpleRulesRemover::remove(const grammar::EpsilonFreeCFG < SymbolType > & origGrammar) {
grammar::EpsilonFreeCFG < SymbolType > grammar(origGrammar.getInitialSymbol());
 
for( const auto & symbol : origGrammar.getNonterminalAlphabet() )
grammar.addNonterminalSymbol( symbol );
......@@ -94,30 +94,20 @@ T SimpleRulesRemover::removeNonEpsilonFree( const T & origGrammar ) {
for( const auto & symbol : origGrammar.getTerminalAlphabet() )
grammar.addTerminalSymbol( symbol );
 
auto origRules = origGrammar.getRawRules();
for( const auto & symbol : origGrammar.getNonterminalAlphabet() ) {
ext::set<SymbolType> simpleRulesClosure = grammar::properties::NonterminalUnitRuleCycle::getNonterminalUnitRuleCycle(origGrammar, symbol);
for( const auto & closureSymbol : simpleRulesClosure ) {
auto rules = origRules.find(closureSymbol);
if(rules != origRules.end()) for( const auto& rawRule : rules->second ) {
auto rules = origGrammar.getRules().find(closureSymbol);
if(rules != origGrammar.getRules().end()) for( const auto& rawRule : rules->second ) {
if(rawRule.size() == 0 || (rawRule.size() == 1 && !origGrammar.getNonterminalAlphabet().count(rawRule[0])) || rawRule.size() >= 2)
grammar.addRawRule(symbol, rawRule);
grammar.addRule(symbol, rawRule);
}
}
}
 
return grammar;
}
template < class SymbolType >
grammar::CFG < SymbolType > SimpleRulesRemover::remove(const grammar::CFG < SymbolType > & origGrammar) {
return removeNonEpsilonFree(origGrammar);
}
grammar.setGeneratesEpsilon(origGrammar.getGeneratesEpsilon());
 
template < class SymbolType >
grammar::EpsilonFreeCFG < SymbolType > SimpleRulesRemover::remove(const grammar::EpsilonFreeCFG < SymbolType > & origGrammar) {
return removeEpsilonFree(origGrammar);
return grammar;
}
 
template < class SymbolType >
......
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