From 082b3d82071f7fdf16f17a433e3af2bf2824f6e4 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Thu, 12 Nov 2015 16:35:40 +0100
Subject: [PATCH] fix ExtractRightContext,disable cout in some tests

---
 .../grammar/parsing/ExtractRightContext.cpp   | 18 ++++--
 .../grammar/parsing/CornerSubstitution.cpp    |  4 +-
 .../grammar/parsing/ExtractRightContext.cpp   | 60 +++++++++++++++++++
 .../grammar/parsing/ExtractRightContext.h     | 20 +++++++
 .../parsing/HandleFirstFirstConflict.cpp      |  8 +--
 .../grammar/parsing/LeftFactorize.cpp         |  4 +-
 6 files changed, 100 insertions(+), 14 deletions(-)
 create mode 100644 alib2algo/test-src/grammar/parsing/ExtractRightContext.cpp
 create mode 100644 alib2algo/test-src/grammar/parsing/ExtractRightContext.h

diff --git a/alib2algo/src/grammar/parsing/ExtractRightContext.cpp b/alib2algo/src/grammar/parsing/ExtractRightContext.cpp
index 6d1ad8f9a3..835c95a582 100644
--- a/alib2algo/src/grammar/parsing/ExtractRightContext.cpp
+++ b/alib2algo/src/grammar/parsing/ExtractRightContext.cpp
@@ -23,12 +23,18 @@ void ExtractRightContext::extractRightContext ( grammar::CFG & grammar, const al
 	for ( const std::pair < alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > > & rule : grammar.getRules ( ) ) {
 		const alphabet::Symbol & lhs = rule.first;
 
-		for ( const std::vector < alphabet::Symbol > & rhs : rule.second )
-			for ( std::vector < alphabet::Symbol >::const_iterator iter = rhs.begin ( ); iter + 1 != rhs.end ( ); ++iter )
-				if ( nonterminals.count ( * iter ) && grammar.getNonterminalAlphabet ( ).count ( * ( iter + 1 ) ) && First::first ( grammar, std::vector < alphabet::Symbol > ( iter + 1, rhs.end ( ) ) ).count ( terminal ) )
-					Substitute::substitute ( grammar, res, lhs, rhs, iter );
-				else
-					res.addRule ( lhs, rhs );
+		for ( const std::vector < alphabet::Symbol > & rhs : rule.second ) {
+			bool substitued = false;
+			if(rhs.size() > 0)
+				for ( std::vector < alphabet::Symbol >::const_iterator iter = rhs.begin ( ); iter + 1 != rhs.end ( ); ++iter )
+					if ( nonterminals.count ( * iter ) && grammar.getNonterminalAlphabet ( ).count ( * ( iter + 1 ) ) && First::first ( grammar, std::vector < alphabet::Symbol > ( iter + 1, rhs.end ( ) ) ).count ( terminal ) ) {
+						Substitute::substitute ( grammar, res, lhs, rhs, iter + 1 );
+						substitued = true;
+						break;
+					}
+			if( ! substitued )
+				res.addRule ( lhs, rhs );
+		}
 
 	}
 
diff --git a/alib2algo/test-src/grammar/parsing/CornerSubstitution.cpp b/alib2algo/test-src/grammar/parsing/CornerSubstitution.cpp
index 6bd7aa9ee2..6b586609cd 100644
--- a/alib2algo/test-src/grammar/parsing/CornerSubstitution.cpp
+++ b/alib2algo/test-src/grammar/parsing/CornerSubstitution.cpp
@@ -52,7 +52,7 @@ void CornerSubstitution::testCornerSubstitution ( ) {
 	comp.addRule ( C, std::vector < alphabet::Symbol > { a, C } );
 	comp.addRule ( C, std::vector < alphabet::Symbol > { b, B } );
 
-	std::cout << res << std::endl << comp << std::endl;
+//	std::cout << res << std::endl << comp << std::endl;
 
 	CPPUNIT_ASSERT ( res == comp );
 }
@@ -99,7 +99,7 @@ void CornerSubstitution::testCornerSubstitution2 ( ) {
 	comp.addRule ( C, std::vector < alphabet::Symbol > { a, C } );
 	comp.addRule ( C, std::vector < alphabet::Symbol > { B, b } );
 
-	std::cout << res << std::endl << comp << std::endl;
+//	std::cout << res << std::endl << comp << std::endl;
 
 	CPPUNIT_ASSERT ( res == comp );
 }
diff --git a/alib2algo/test-src/grammar/parsing/ExtractRightContext.cpp b/alib2algo/test-src/grammar/parsing/ExtractRightContext.cpp
new file mode 100644
index 0000000000..69184b1a89
--- /dev/null
+++ b/alib2algo/test-src/grammar/parsing/ExtractRightContext.cpp
@@ -0,0 +1,60 @@
+#include "ExtractRightContext.h"
+
+#include "grammar/ContextFree/CFG.h"
+#include "grammar/parsing/ExtractRightContext.h"
+
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( ExtractRightContext, "grammar" );
+CPPUNIT_TEST_SUITE_REGISTRATION ( ExtractRightContext );
+
+void ExtractRightContext::setUp ( ) {
+}
+
+void ExtractRightContext::tearDown ( ) {
+}
+
+void ExtractRightContext::testExtractRightContext ( ) {
+	alphabet::Symbol S = alphabet::symbolFrom ( "S" );
+	alphabet::Symbol A = alphabet::symbolFrom ( "A" );
+	alphabet::Symbol C = alphabet::symbolFrom ( "C" );
+
+	alphabet::Symbol a = alphabet::symbolFrom ( 'a' );
+	alphabet::Symbol b = alphabet::symbolFrom ( 'b' );
+	alphabet::Symbol c = alphabet::symbolFrom ( 'c' );
+
+	grammar::CFG grammar ( S );
+
+	grammar.setTerminalAlphabet ( { a, b, c } );
+	grammar.setNonterminalAlphabet ( { S, A, C } );
+	grammar.setInitialSymbol ( S );
+
+	grammar.addRule ( S, std::vector < alphabet::Symbol > { b, c, A, C } );
+	grammar.addRule ( S, std::vector < alphabet::Symbol > { b, b } );
+	grammar.addRule ( A, std::vector < alphabet::Symbol > { } );
+	grammar.addRule ( A, std::vector < alphabet::Symbol > { a, c, A, b } );
+	grammar.addRule ( C, {a, b} );
+	grammar.addRule ( C, {c, C} );
+
+	grammar::CFG res = grammar;
+	grammar::parsing::ExtractRightContext::extractRightContext ( res, a, { A } );
+
+	grammar::CFG comp ( S );
+
+	comp.setTerminalAlphabet ( { a, b, c } );
+	comp.setNonterminalAlphabet ( { S, A, C } );
+	comp.setInitialSymbol ( S );
+
+	comp.addRule ( S, std::vector < alphabet::Symbol > { b, c, A, a, b } );
+	comp.addRule ( S, std::vector < alphabet::Symbol > { b, c, A, c, C } );
+	comp.addRule ( S, std::vector < alphabet::Symbol > { b, b } );
+	comp.addRule ( A, std::vector < alphabet::Symbol > { } );
+	comp.addRule ( A, std::vector < alphabet::Symbol > { a, c, A, b } );
+	comp.addRule ( C, std::vector < alphabet::Symbol > { a, b } );
+	comp.addRule ( C, std::vector < alphabet::Symbol > { c, C } );
+
+	std::cout << res << std::endl << comp << std::endl;
+
+	CPPUNIT_ASSERT ( res == comp );
+}
+
+void ExtractRightContext::testExtractRightContext2 ( ) {
+}
diff --git a/alib2algo/test-src/grammar/parsing/ExtractRightContext.h b/alib2algo/test-src/grammar/parsing/ExtractRightContext.h
new file mode 100644
index 0000000000..f471356bc8
--- /dev/null
+++ b/alib2algo/test-src/grammar/parsing/ExtractRightContext.h
@@ -0,0 +1,20 @@
+#ifndef EXTRACT_RIGHT_CONTEXT_TEST_H_
+#define EXTRACT_RIGHT_CONTEXT_TEST_H_
+
+#include <cppunit/extensions/HelperMacros.h>
+
+class ExtractRightContext : public CppUnit::TestFixture {
+	CPPUNIT_TEST_SUITE ( ExtractRightContext );
+	CPPUNIT_TEST ( testExtractRightContext );
+	CPPUNIT_TEST ( testExtractRightContext2 );
+	CPPUNIT_TEST_SUITE_END ( );
+
+public:
+	void setUp ( );
+	void tearDown ( );
+
+	void testExtractRightContext ( );
+	void testExtractRightContext2 ( );
+};
+
+#endif /* EXTRACT_RIGHT_CONTEXT_TEST_H_ */
diff --git a/alib2algo/test-src/grammar/parsing/HandleFirstFirstConflict.cpp b/alib2algo/test-src/grammar/parsing/HandleFirstFirstConflict.cpp
index dbbafa42df..eb002db5ad 100644
--- a/alib2algo/test-src/grammar/parsing/HandleFirstFirstConflict.cpp
+++ b/alib2algo/test-src/grammar/parsing/HandleFirstFirstConflict.cpp
@@ -53,7 +53,7 @@ void HandleFirstFirstConflict::testHandleFirstFirstConflict ( ) {
 	comp.addRule ( C, std::vector < alphabet::Symbol > { a, C } );
 	comp.addRule ( C, std::vector < alphabet::Symbol > { b, B } );
 
-	std::cout << res << std::endl << comp << std::endl;
+//	std::cout << res << std::endl << comp << std::endl;
 
 	CPPUNIT_ASSERT ( res == comp );
 
@@ -74,7 +74,7 @@ void HandleFirstFirstConflict::testHandleFirstFirstConflict ( ) {
 	comp2.addRule ( C, std::vector < alphabet::Symbol > { a, C } );
 	comp2.addRule ( C, std::vector < alphabet::Symbol > { b, B } );
 
-	std::cout << res << std::endl << comp2 << std::endl;
+//	std::cout << res << std::endl << comp2 << std::endl;
 
 	CPPUNIT_ASSERT ( res == comp2 );
 }
@@ -122,7 +122,7 @@ void HandleFirstFirstConflict::testHandleFirstFirstConflict2 ( ) {
 	comp.addRule ( C, std::vector < alphabet::Symbol > { a, C } );
 	comp.addRule ( C, std::vector < alphabet::Symbol > { B, b } );
 
-	std::cout << res << std::endl << comp << std::endl;
+//	std::cout << res << std::endl << comp << std::endl;
 
 	CPPUNIT_ASSERT ( res == comp );
 
@@ -144,7 +144,7 @@ void HandleFirstFirstConflict::testHandleFirstFirstConflict2 ( ) {
 	comp2.addRule ( C, std::vector < alphabet::Symbol > { a, C } );
 	comp2.addRule ( C, std::vector < alphabet::Symbol > { B, b } );
 
-	std::cout << res << std::endl << comp2 << std::endl;
+//	std::cout << res << std::endl << comp2 << std::endl;
 
 	CPPUNIT_ASSERT ( res == comp2 );
 }
diff --git a/alib2algo/test-src/grammar/parsing/LeftFactorize.cpp b/alib2algo/test-src/grammar/parsing/LeftFactorize.cpp
index 83b366f12b..f5c9f254ae 100644
--- a/alib2algo/test-src/grammar/parsing/LeftFactorize.cpp
+++ b/alib2algo/test-src/grammar/parsing/LeftFactorize.cpp
@@ -58,7 +58,7 @@ void LeftFactorize::testLeftFactorize ( ) {
 	comp.addRule ( Cp, std::vector < alphabet::Symbol > { C } );
 	comp.addRule ( Cp, std::vector < alphabet::Symbol > { } );
 
-	std::cout << res << std::endl << comp << std::endl;
+//	std::cout << res << std::endl << comp << std::endl;
 
 	CPPUNIT_ASSERT ( res == comp );
 }
@@ -117,7 +117,7 @@ void LeftFactorize::testLeftFactorize2 ( ) {
 	comp.addRule ( Cp, std::vector < alphabet::Symbol > { } );
 	comp.addRule ( D, std::vector < alphabet::Symbol > { d } );
 
-	std::cout << res << std::endl << comp << std::endl;
+//	std::cout << res << std::endl << comp << std::endl;
 
 	CPPUNIT_ASSERT ( res == comp );
 }
-- 
GitLab