From e3897fbc1c015b7e2287d17b0f745a67de572c2e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Ko=C4=8Di=C4=8Dka?= <martin.kocicka@gmail.com>
Date: Wed, 11 May 2016 18:56:25 +0200
Subject: [PATCH] moved getNextStateItems() to LR0Parser since it's not
 universal

---
 alib2algo/src/grammar/parsing/LR0Parser.cpp | 23 ++++++++++++++++++++-
 alib2algo/src/grammar/parsing/LR0Parser.h   |  2 ++
 alib2algo/src/grammar/parsing/LRParser.cpp  | 23 ---------------------
 alib2algo/src/grammar/parsing/LRParser.h    |  2 --
 4 files changed, 24 insertions(+), 26 deletions(-)

diff --git a/alib2algo/src/grammar/parsing/LR0Parser.cpp b/alib2algo/src/grammar/parsing/LR0Parser.cpp
index c5c12b116a..0f8ea06326 100644
--- a/alib2algo/src/grammar/parsing/LR0Parser.cpp
+++ b/alib2algo/src/grammar/parsing/LR0Parser.cpp
@@ -48,6 +48,27 @@ LR0Items LR0Parser::getClosure ( LR0Items items, grammar::CFG originalGrammar )
 	return items;
 }
 
+LR0Items LR0Parser::getNextStateItems ( LR0Items items, alphabet::Symbol symbol, grammar::CFG originalGrammar ) {
+	LR0Items transitionItems;
+	for ( const LR0Items::value_type & symbolItems : items ) {
+		alphabet::Symbol leftHandSide = symbolItems.first;
+		for ( const std::pair < unsigned, std::vector < alphabet::Symbol > > & item : symbolItems.second) {
+			unsigned position = item.first;
+			std::vector < alphabet::Symbol > rightHandSide = item.second;
+
+			if ( position == rightHandSide.size ( ) ) {
+				continue;
+			}
+
+			if ( rightHandSide[position] == symbol ) {
+				transitionItems[leftHandSide].insert ( { position + 1, rightHandSide } );
+			}
+		}
+	}
+
+	return getClosure ( transitionItems, originalGrammar );
+}
+
 automaton::DFA LR0Parser::getAutomaton ( grammar::CFG originalGrammar ) {
 	grammar::CFG augmentedGrammar = LRParser::getAugmentedGrammar ( originalGrammar );
 	alphabet::Symbol initialSymbol = augmentedGrammar.getInitialSymbol ( );
@@ -70,7 +91,7 @@ automaton::DFA LR0Parser::getAutomaton ( grammar::CFG originalGrammar ) {
 		itemsToProcess.pop ( );
 
 		for ( const alphabet::Symbol & symbol : lr0Automaton.getInputAlphabet ( ) ) {
-			LR0Items nextStateItems = LRParser::getNextStateItems ( currentLabel.getItems ( ), symbol, augmentedGrammar );
+			LR0Items nextStateItems = getNextStateItems ( currentLabel.getItems ( ), symbol, augmentedGrammar );
 			if ( !nextStateItems.empty ( ) ) {
 				label::LR0ItemsLabel nextStateLabel ( nextStateItems );
 
diff --git a/alib2algo/src/grammar/parsing/LR0Parser.h b/alib2algo/src/grammar/parsing/LR0Parser.h
index cf06cf4b96..e98fa8a66e 100644
--- a/alib2algo/src/grammar/parsing/LR0Parser.h
+++ b/alib2algo/src/grammar/parsing/LR0Parser.h
@@ -20,6 +20,8 @@ class LR0Parser {
 public:
 	static LR0Items getClosure ( LR0Items items, grammar::CFG originalGrammar );
 
+	static LR0Items getNextStateItems ( LR0Items items, alphabet::Symbol symbol, grammar::CFG originalGrammar );
+
 	static automaton::DFA getAutomaton ( grammar::CFG originalGrammar );
 };
 
diff --git a/alib2algo/src/grammar/parsing/LRParser.cpp b/alib2algo/src/grammar/parsing/LRParser.cpp
index 2c3d42b07e..f96a5fe785 100644
--- a/alib2algo/src/grammar/parsing/LRParser.cpp
+++ b/alib2algo/src/grammar/parsing/LRParser.cpp
@@ -7,8 +7,6 @@
 
 #include "LRParser.h"
 
-#include "LR0Parser.h"
-
 #include <stack>
 
 namespace grammar {
@@ -36,27 +34,6 @@ grammar::CFG LRParser::getAugmentedGrammar ( grammar::CFG originalGrammar ) {
 	return originalGrammar;
 }
 
-LR0Items LRParser::getNextStateItems ( LR0Items items, alphabet::Symbol symbol, grammar::CFG originalGrammar ) {
-	LR0Items transitionItems;
-	for ( const LR0Items::value_type & symbolItems : items ) {
-		alphabet::Symbol leftHandSide = symbolItems.first;
-		for ( const std::pair < unsigned, std::vector < alphabet::Symbol > > & item : symbolItems.second) {
-			unsigned position = item.first;
-			std::vector < alphabet::Symbol > rightHandSide = item.second;
-
-			if ( position == rightHandSide.size ( ) ) {
-				continue;
-			}
-
-			if ( rightHandSide[position] == symbol ) {
-				transitionItems[leftHandSide].insert ( { position + 1, rightHandSide } );
-			}
-		}
-	}
-
-	return LR0Parser::getClosure ( transitionItems, originalGrammar );
-}
-
 bool LRParser::parse ( LRActionTable actionTable, LRGotoTable gotoTable, automaton::State initialState, std::vector < alphabet::Symbol > input ) {
 	std::stack < automaton::State > states;
 	states.push ( initialState );
diff --git a/alib2algo/src/grammar/parsing/LRParser.h b/alib2algo/src/grammar/parsing/LRParser.h
index 63349d60f2..43a7107004 100644
--- a/alib2algo/src/grammar/parsing/LRParser.h
+++ b/alib2algo/src/grammar/parsing/LRParser.h
@@ -24,8 +24,6 @@ public:
 
 	static grammar::CFG getAugmentedGrammar ( grammar::CFG originalGrammar );
 
-	static LR0Items getNextStateItems ( LR0Items items, alphabet::Symbol symbol, grammar::CFG originalGrammar );
-
 	static bool parse ( LRActionTable actionTable, LRGotoTable gotoTable, automaton::State initialState, std::vector < alphabet::Symbol > input );
 };
 
-- 
GitLab