From ffe4e18ddca03cf2333ee5e1373586bb23b8a116 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Fri, 28 Nov 2014 14:54:54 +0100
Subject: [PATCH] add conversion from LG to EpsilonFreeCFG

---
 .../src/grammar/simplify/EpsilonRemover.cpp   | 85 +++++++++++--------
 .../src/grammar/simplify/EpsilonRemover.h     |  2 +
 2 files changed, 50 insertions(+), 37 deletions(-)

diff --git a/alib2algo/src/grammar/simplify/EpsilonRemover.cpp b/alib2algo/src/grammar/simplify/EpsilonRemover.cpp
index c68bde0290..16962d4965 100644
--- a/alib2algo/src/grammar/simplify/EpsilonRemover.cpp
+++ b/alib2algo/src/grammar/simplify/EpsilonRemover.cpp
@@ -13,41 +13,6 @@ namespace grammar {
 
 namespace simplify {
 
-grammar::EpsilonFreeCFG EpsilonRemover::remove(const grammar::EpsilonFreeCFG& origGrammar)
-{
-	return origGrammar;
-}
-
-grammar::CNF EpsilonRemover::remove(const grammar::CNF& origGrammar)
-{
-	return origGrammar;
-}
-
-grammar::GNF EpsilonRemover::remove(const grammar::GNF& origGrammar)
-{
-	return origGrammar;
-}
-
-grammar::LeftLG EpsilonRemover::remove(const grammar::LeftLG& origGrammar)
-{
-	return origGrammar;
-}
-
-grammar::LeftRG EpsilonRemover::remove(const grammar::LeftRG& origGrammar)
-{
-	return origGrammar;
-}
-
-grammar::RightLG EpsilonRemover::remove(const grammar::RightLG& origGrammar)
-{
-	return origGrammar;
-}
-
-grammar::RightRG EpsilonRemover::remove(const grammar::RightRG& origGrammar)
-{
-	return origGrammar;
-}
-
 void removeNullableNonterminals(grammar::EpsilonFreeCFG& grammar, const std::set<alphabet::Symbol>& nullableNonterminals, const alphabet::Symbol& lhs, const std::vector<alphabet::Symbol>& rhs, unsigned i, std::vector<alphabet::Symbol> clear) {
 	if(rhs.size() == i) {
 		if(clear.size() == 0) return;
@@ -62,7 +27,8 @@ void removeNullableNonterminals(grammar::EpsilonFreeCFG& grammar, const std::set
 	}
 }
 
-grammar::EpsilonFreeCFG EpsilonRemover::remove( const grammar::CFG & origGrammar ) {
+template<class T>
+grammar::EpsilonFreeCFG removeInternal( const T & origGrammar ) {
 	grammar::EpsilonFreeCFG grammar(origGrammar.getInitialSymbol());
 
 	for( const auto & symbol : origGrammar.getNonterminalAlphabet() )
@@ -73,7 +39,7 @@ grammar::EpsilonFreeCFG EpsilonRemover::remove( const grammar::CFG & origGrammar
 
 	std::set<alphabet::Symbol> nullableNonterminals = grammar::properties::NullableNonterminals::getNullableNonterminals(origGrammar);
 
-	for( const auto & rule : origGrammar.getRules( ) )
+	for( const auto & rule : origGrammar.getRawRules( ) )
 		for(const auto & rhs : rule.second) {
 			if(rhs.size() == 0) continue;
 
@@ -86,6 +52,51 @@ grammar::EpsilonFreeCFG EpsilonRemover::remove( const grammar::CFG & origGrammar
 	return grammar;
 }
 
+grammar::EpsilonFreeCFG EpsilonRemover::remove(const grammar::CFG& origGrammar)
+{
+	return removeInternal(origGrammar);
+}
+
+grammar::EpsilonFreeCFG EpsilonRemover::remove(const grammar::EpsilonFreeCFG& origGrammar)
+{
+	return origGrammar;
+}
+
+grammar::CNF EpsilonRemover::remove(const grammar::CNF& origGrammar)
+{
+	return origGrammar;
+}
+
+grammar::GNF EpsilonRemover::remove(const grammar::GNF& origGrammar)
+{
+	return origGrammar;
+}
+
+grammar::EpsilonFreeCFG EpsilonRemover::remove(const grammar::LG& origGrammar)
+{
+	return removeInternal(origGrammar);
+}
+
+grammar::LeftLG EpsilonRemover::remove(const grammar::LeftLG& origGrammar)
+{
+	return origGrammar;
+}
+
+grammar::LeftRG EpsilonRemover::remove(const grammar::LeftRG& origGrammar)
+{
+	return origGrammar;
+}
+
+grammar::RightLG EpsilonRemover::remove(const grammar::RightLG& origGrammar)
+{
+	return origGrammar;
+}
+
+grammar::RightRG EpsilonRemover::remove(const grammar::RightRG& origGrammar)
+{
+	return origGrammar;
+}
+
 grammar::Grammar EpsilonRemover::remove(const grammar::Grammar& grammar) {
 	grammar::Grammar* out = NULL;
 	grammar.getData().Accept((void*) &out, EpsilonRemover::EPSILON_REMOVER);
diff --git a/alib2algo/src/grammar/simplify/EpsilonRemover.h b/alib2algo/src/grammar/simplify/EpsilonRemover.h
index e38f4cd7ce..d3931245ac 100644
--- a/alib2algo/src/grammar/simplify/EpsilonRemover.h
+++ b/alib2algo/src/grammar/simplify/EpsilonRemover.h
@@ -17,6 +17,7 @@
 #include <grammar/ContextFree/EpsilonFreeCFG.h>
 #include <grammar/ContextFree/CNF.h>
 #include <grammar/ContextFree/GNF.h>
+#include <grammar/ContextFree/LG.h>
 #include <grammar/Regular/LeftLG.h>
 #include <grammar/Regular/LeftRG.h>
 #include <grammar/Regular/RightLG.h>
@@ -35,6 +36,7 @@ public:
 	static grammar::EpsilonFreeCFG remove( const grammar::EpsilonFreeCFG & grammar );
 	static grammar::GNF remove( const grammar::GNF & grammar );
 	static grammar::CNF remove( const grammar::CNF & grammar );
+	static grammar::EpsilonFreeCFG remove( const grammar::LG & grammar );
 	static grammar::LeftLG remove( const grammar::LeftLG & grammar );
 	static grammar::LeftRG remove( const grammar::LeftRG & grammar );
 	static grammar::RightLG remove( const grammar::RightLG & grammar );
-- 
GitLab