From 2f1df5ebb3ccdbe2195e9dc566a2bb448ff77d9a Mon Sep 17 00:00:00 2001
From: Tomas Pecka <peckato1@fit.cvut.cz>
Date: Sun, 17 Apr 2016 11:23:41 +0200
Subject: [PATCH] GlushkovRTE: Follow iter

---
 alib2algo/src/rte/GlushkovTraversal.cpp       | 33 ++++++++++++++-----
 .../ToPostfixPushdownAutomatonGlushkov.cpp    |  9 ++---
 examples2/rte/rte3.xml                        | 32 ++++++++++++++++++
 3 files changed, 62 insertions(+), 12 deletions(-)
 create mode 100644 examples2/rte/rte3.xml

diff --git a/alib2algo/src/rte/GlushkovTraversal.cpp b/alib2algo/src/rte/GlushkovTraversal.cpp
index df65c24751..a9bcaf4467 100644
--- a/alib2algo/src/rte/GlushkovTraversal.cpp
+++ b/alib2algo/src/rte/GlushkovTraversal.cpp
@@ -29,8 +29,10 @@ bool GlushkovTraversal::pos ( GlushkovSymbol const & symbol, rte::FormalRTE cons
 std::set < GlushkovSymbol > GlushkovTraversal::first ( rte::FormalRTE const & re ) {
 	std::set < GlushkovSymbol > firstSet, symbolSet = getSymbols ( re );
 
-	for ( auto const & s : first ( & re.getRTE ( ) ) )
+	for ( auto const & s : first ( & re.getRTE ( ) ) ) {
+		std::cerr << "In first set: " << s->getSymbol ( ) << std::endl;
 		firstSet.insert ( findSymbol ( s, symbolSet ) );
+	}
 
 	return firstSet;
 }
@@ -109,8 +111,16 @@ std::set < rte::FormalRTESymbol const * > GlushkovTraversal::first ( rte::Formal
 	tmp = first ( & node->getLeftElement ( ) );
 	ret.insert ( tmp.begin ( ), tmp.end ( ) );
 
-	if ( ret.count ( & node->getSubstitutionSymbol ( ) ) ) {
-		ret.erase ( & node->getSubstitutionSymbol ( ) );
+	for ( const auto & r : ret )
+		std::cerr << "   In subst first: so far: " << r->getSymbol ( ) << std::endl;
+
+	auto it = std::find_if ( ret.begin ( ), ret.end ( ), [node] ( rte::FormalRTESymbol const * a ) {
+			return a->getSymbol ( ) == node->getSubstitutionSymbol ( );
+		} );
+
+	if ( it != ret.end ( ) ) {
+		std::cerr << "SubstFirst: subst symbol found in left" << std::endl;
+		ret.erase ( it );
 		tmp = first ( & node->getRightElement ( ) );
 		ret.insert ( tmp.begin ( ), tmp.end ( ) );
 	}
@@ -126,9 +136,7 @@ std::set < rte::FormalRTESymbol const * > GlushkovTraversal::first ( rte::Formal
 }
 
 std::set < rte::FormalRTESymbol const * > GlushkovTraversal::first ( rte::FormalRTESymbol const * const & node ) {
-	return {
-			   node
-	};
+	return std::set < rte::FormalRTESymbol const * > { node };
 }
 
 std::set < rte::FormalRTESymbol const * > GlushkovTraversal::first ( rte::FormalRTEEmpty const * const & /* node */ ) {
@@ -268,8 +276,17 @@ std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::fol
 	return ret;
 }
 
-std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::follow ( rte::FormalRTEIteration const * const & /* node */, rte::FormalRTESymbol const * const & /* symbolptr */, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & /* subMap */ ) {
-	return std::set < std::vector < rte::FormalRTESymbol const * > > ( );
+std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::follow ( rte::FormalRTEIteration const * const & node, rte::FormalRTESymbol const * const & symbolptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & subMap ) {
+
+	std::set < std::vector < rte::FormalRTESymbol const * > > ret;
+
+	for ( const auto & s : first ( & node->getElement ( ) ) )
+		subMap[node->getSubstitutionSymbol ( ).getSymbol ( )].insert ( s );
+
+	ret = follow ( & node->getElement ( ), symbolptr, subMap );
+
+	std::cerr << "Follow Sub: (" << symbolptr->getSymbol ( ) << "): " << ret << std::endl;
+	return ret;
 }
 
 std::set < std::vector < rte::FormalRTESymbol const * > > replaceConstants ( const std::vector < rte::FormalRTESymbol const * > & f, const std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & subMap ) {
diff --git a/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.cpp b/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.cpp
index 6be918c685..00c35fca1a 100644
--- a/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.cpp
+++ b/alib2algo/src/rte/convert/ToPostfixPushdownAutomatonGlushkov.cpp
@@ -78,10 +78,12 @@ automaton::NPDA ToPostfixPushdownAutomatonGlushkov::convert ( const rte::FormalR
 	for ( const auto & kv : followSet ) {
 		std::cerr << "\t" << kv.first.getInputSymbol ( ) << " id=" << kv.first.getId ( ) << std::endl;
 
-		for ( const auto & f : kv.second )
+		for ( const auto & f : kv.second ) {
 			for ( const auto & symbol : f )
-				std::cerr << "\t\t" << symbol.getInputSymbol ( ) << " id=" << symbol.getId ( ) << std::endl;
+				std::cerr << "\t\t" << symbol.getInputSymbol ( ) << "(id=" << symbol.getId ( ) << ") ";
 
+			std::cerr << std::endl;
+		}
 	}
 
 	/* DEBUG END */
@@ -118,8 +120,7 @@ automaton::NPDA ToPostfixPushdownAutomatonGlushkov::convert ( const rte::FormalR
 		if ( it == pos_stack_map.end ( ) )
 			throw exception::CommonException ( "GlushkovRTE: fail2" );
 
-		automaton.addTransition ( q, alphabet::Symbol { alphabet::EndSymbol::END }, { it->second, alphabet::Symbol { alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK }
-								  }, f, { } );
+		automaton.addTransition ( q, alphabet::Symbol { alphabet::EndSymbol::END }, { it->second, alphabet::Symbol { alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK } }, f, { } );
 	}
 
 	return automaton;
diff --git a/examples2/rte/rte3.xml b/examples2/rte/rte3.xml
new file mode 100644
index 0000000000..60f92a1778
--- /dev/null
+++ b/examples2/rte/rte3.xml
@@ -0,0 +1,32 @@
+<FormalRTE>
+	<alphabet>
+		<RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>a</Character></PrimitiveLabel></LabeledSymbol><Unsigned>2</Unsigned></RankedSymbol>
+		<RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>b</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol>
+	</alphabet>
+	<constantAlphabet>
+		<RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>z</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol>
+	</constantAlphabet>
+
+	<substitution>
+		<RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>z</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol>
+
+		<iteration>
+			<RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>z</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol>
+
+			<symbol>
+				<RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>a</Character></PrimitiveLabel></LabeledSymbol><Unsigned>2</Unsigned></RankedSymbol>
+				<symbol>
+					<RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>z</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol>
+				</symbol>
+				<symbol>
+					<RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>z</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol>
+				</symbol>
+			</symbol>
+		</iteration>
+
+		<symbol>
+			<RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>b</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol>
+		</symbol>
+	</substitution>
+</FormalRTE>
+
-- 
GitLab