From 986338e982d0a85a7c3ef681f80d502eb314ca6c Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Sun, 10 Aug 2014 20:47:11 +0200
Subject: [PATCH] Conversions Formal<->Unbounded

---
 .../src/regexp/RegExpFromStringParser.cpp     |  2 +-
 alib2data/src/regexp/RegExpFromStringParser.h |  1 +
 alib2data/src/regexp/formal/FormalRegExp.cpp  |  7 +++++
 alib2data/src/regexp/formal/FormalRegExp.h    |  1 +
 .../regexp/formal/FormalRegExpAlternation.cpp | 15 +++++++++++
 .../regexp/formal/FormalRegExpAlternation.h   |  5 ++++
 .../formal/FormalRegExpConcatenation.cpp      | 15 +++++++++++
 .../regexp/formal/FormalRegExpConcatenation.h |  5 ++++
 .../src/regexp/formal/FormalRegExpElement.h   |  8 ++++++
 .../src/regexp/formal/FormalRegExpEmpty.cpp   |  5 ++++
 .../src/regexp/formal/FormalRegExpEmpty.h     |  5 ++++
 .../src/regexp/formal/FormalRegExpEpsilon.cpp |  5 ++++
 .../src/regexp/formal/FormalRegExpEpsilon.h   |  5 ++++
 .../regexp/formal/FormalRegExpIteration.cpp   |  9 +++++++
 .../src/regexp/formal/FormalRegExpIteration.h |  5 ++++
 .../src/regexp/formal/FormalRegExpSymbol.cpp  |  5 ++++
 .../src/regexp/formal/FormalRegExpSymbol.h    |  5 ++++
 .../src/regexp/unbounded/UnboundedRegExp.cpp  |  7 +++++
 .../src/regexp/unbounded/UnboundedRegExp.h    |  1 +
 .../unbounded/UnboundedRegExpAlternation.cpp  | 26 +++++++++++++++++++
 .../unbounded/UnboundedRegExpAlternation.h    |  5 ++++
 .../UnboundedRegExpConcatenation.cpp          | 26 +++++++++++++++++++
 .../unbounded/UnboundedRegExpConcatenation.h  |  7 ++++-
 .../regexp/unbounded/UnboundedRegExpElement.h | 10 ++++++-
 .../regexp/unbounded/UnboundedRegExpEmpty.cpp |  5 ++++
 .../regexp/unbounded/UnboundedRegExpEmpty.h   |  5 ++++
 .../unbounded/UnboundedRegExpEpsilon.cpp      |  7 ++++-
 .../regexp/unbounded/UnboundedRegExpEpsilon.h |  5 ++++
 .../unbounded/UnboundedRegExpIteration.cpp    |  9 +++++++
 .../unbounded/UnboundedRegExpIteration.h      |  5 ++++
 .../unbounded/UnboundedRegExpSymbol.cpp       |  5 ++++
 .../regexp/unbounded/UnboundedRegExpSymbol.h  |  5 ++++
 32 files changed, 227 insertions(+), 4 deletions(-)

diff --git a/alib2data/src/regexp/RegExpFromStringParser.cpp b/alib2data/src/regexp/RegExpFromStringParser.cpp
index 6ca6c60f53..ddf3bab87b 100644
--- a/alib2data/src/regexp/RegExpFromStringParser.cpp
+++ b/alib2data/src/regexp/RegExpFromStringParser.cpp
@@ -19,7 +19,7 @@ RegExp RegExpFromStringParser::parse(const std::set<FEATURES>& features) {
 	delete element;
 	if(features.count(FEATURES::UNBOUNDED)) return RegExp{regexp};
 
-//	if(features.count(FEATURES::FORMAL)) return RegExp{FormalRegExp{regexp}}; TODO
+	if(features.count(FEATURES::FORMAL)) return RegExp{FormalRegExp{regexp}};
 
 	throw exception::AlibException();
 }
diff --git a/alib2data/src/regexp/RegExpFromStringParser.h b/alib2data/src/regexp/RegExpFromStringParser.h
index 96c488031d..341ef44b30 100644
--- a/alib2data/src/regexp/RegExpFromStringParser.h
+++ b/alib2data/src/regexp/RegExpFromStringParser.h
@@ -10,6 +10,7 @@
 
 #include "RegExp.h"
 #include "unbounded/UnboundedRegExpElements.h"
+#include "formal/FormalRegExpElements.h"
 
 #include "RegExpFromStringLexer.h"
 #include "../alphabet/SymbolFromStringParser.h"
diff --git a/alib2data/src/regexp/formal/FormalRegExp.cpp b/alib2data/src/regexp/formal/FormalRegExp.cpp
index 351f9005c2..9b171d128f 100644
--- a/alib2data/src/regexp/formal/FormalRegExp.cpp
+++ b/alib2data/src/regexp/formal/FormalRegExp.cpp
@@ -23,6 +23,13 @@ FormalRegExp::FormalRegExp() {
 	this->regExp = new FormalRegExpEmpty();
 }
 
+FormalRegExp::FormalRegExp(const UnboundedRegExp& other) : alphabet(other.getAlphabet()) {
+	this->regExp = NULL;
+	FormalRegExpElement* element = other.getRegExp().cloneAsFormal();
+	setRegExp(std::move(*element));
+	delete element;
+}
+
 FormalRegExp::FormalRegExp(const std::set<alphabet::Symbol>& alphabet, const FormalRegExpElement& regExp) : alphabet(alphabet) {
 	this->regExp = NULL;
 	setRegExp(regExp);
diff --git a/alib2data/src/regexp/formal/FormalRegExp.h b/alib2data/src/regexp/formal/FormalRegExp.h
index 639d8e07b0..bba5ca7e4f 100644
--- a/alib2data/src/regexp/formal/FormalRegExp.h
+++ b/alib2data/src/regexp/formal/FormalRegExp.h
@@ -42,6 +42,7 @@ protected:
 
 public:
 	FormalRegExp();
+	explicit FormalRegExp(const UnboundedRegExp& other);
 	FormalRegExp(const std::set<alphabet::Symbol>& alphabet, const FormalRegExpElement& regExp);
 	FormalRegExp(std::set<alphabet::Symbol>&& alphabet, FormalRegExpElement&& regExp);
 	explicit FormalRegExp(const FormalRegExpElement& regExp);
diff --git a/alib2data/src/regexp/formal/FormalRegExpAlternation.cpp b/alib2data/src/regexp/formal/FormalRegExpAlternation.cpp
index bc772ca388..9ef0dd8253 100644
--- a/alib2data/src/regexp/formal/FormalRegExpAlternation.cpp
+++ b/alib2data/src/regexp/formal/FormalRegExpAlternation.cpp
@@ -7,6 +7,7 @@
 
 #include "FormalRegExpAlternation.h"
 #include "../../exception/AlibException.h"
+#include "../unbounded/UnboundedRegExpAlternation.h"
 
 namespace regexp {
 
@@ -112,6 +113,20 @@ FormalRegExpElement* FormalRegExpAlternation::plunder() && {
 	return new FormalRegExpAlternation(std::move(*this));
 }
 
+UnboundedRegExpElement* FormalRegExpAlternation::cloneAsUnbounded() const {
+	UnboundedRegExpAlternation* res = new UnboundedRegExpAlternation();
+	
+	UnboundedRegExpElement* leftTmp = left->cloneAsUnbounded();
+	res->appendElement(std::move(*leftTmp));
+	delete leftTmp;
+	
+	UnboundedRegExpElement* rightTmp = right->cloneAsUnbounded();
+	res->appendElement(std::move(*rightTmp));
+	delete rightTmp;
+
+	return res;
+}
+
 bool FormalRegExpAlternation::operator<(const FormalRegExpElement& other) const {
 	return other > *this;
 }
diff --git a/alib2data/src/regexp/formal/FormalRegExpAlternation.h b/alib2data/src/regexp/formal/FormalRegExpAlternation.h
index 4f317b8de7..200a00f571 100644
--- a/alib2data/src/regexp/formal/FormalRegExpAlternation.h
+++ b/alib2data/src/regexp/formal/FormalRegExpAlternation.h
@@ -29,6 +29,11 @@ protected:
 	 */
 	virtual FormalRegExpElement* plunder() &&;
 
+	/**
+	 * @copydoc FormalRegExpElement::clone() const
+	 */
+	virtual UnboundedRegExpElement* cloneAsUnbounded() const;
+	
 	FormalRegExpElement* left;
 	FormalRegExpElement* right;
 
diff --git a/alib2data/src/regexp/formal/FormalRegExpConcatenation.cpp b/alib2data/src/regexp/formal/FormalRegExpConcatenation.cpp
index 9cd3cc01ea..dab550bb6a 100644
--- a/alib2data/src/regexp/formal/FormalRegExpConcatenation.cpp
+++ b/alib2data/src/regexp/formal/FormalRegExpConcatenation.cpp
@@ -7,6 +7,7 @@
 
 #include "FormalRegExpConcatenation.h"
 #include "../../exception/AlibException.h"
+#include "../unbounded/UnboundedRegExpConcatenation.h"
 
 namespace regexp {
 
@@ -108,6 +109,20 @@ FormalRegExpElement* FormalRegExpConcatenation::clone() const {
 	return new FormalRegExpConcatenation(*this);
 }
 
+UnboundedRegExpElement* FormalRegExpConcatenation::cloneAsUnbounded() const {
+	UnboundedRegExpConcatenation* res = new UnboundedRegExpConcatenation();
+	
+	UnboundedRegExpElement* leftTmp = left->cloneAsUnbounded();
+	res->appendElement(std::move(*leftTmp));
+	delete leftTmp;
+	
+	UnboundedRegExpElement* rightTmp = right->cloneAsUnbounded();
+	res->appendElement(std::move(*rightTmp));
+	delete rightTmp;
+
+	return res;
+}
+
 FormalRegExpElement* FormalRegExpConcatenation::plunder() && {
 	return new FormalRegExpConcatenation(std::move(*this));
 }
diff --git a/alib2data/src/regexp/formal/FormalRegExpConcatenation.h b/alib2data/src/regexp/formal/FormalRegExpConcatenation.h
index c47a20a8c8..c5cf985af0 100644
--- a/alib2data/src/regexp/formal/FormalRegExpConcatenation.h
+++ b/alib2data/src/regexp/formal/FormalRegExpConcatenation.h
@@ -26,6 +26,11 @@ protected:
 	
 	virtual FormalRegExpElement* plunder() &&;
 
+	/**
+	 * @copydoc FormalRegExpElement::clone() const
+	 */
+	virtual UnboundedRegExpElement* cloneAsUnbounded() const;
+	
 	FormalRegExpElement* left;
 	FormalRegExpElement* right;
 	
diff --git a/alib2data/src/regexp/formal/FormalRegExpElement.h b/alib2data/src/regexp/formal/FormalRegExpElement.h
index ee6fb0dea1..a5d6240fff 100644
--- a/alib2data/src/regexp/formal/FormalRegExpElement.h
+++ b/alib2data/src/regexp/formal/FormalRegExpElement.h
@@ -24,6 +24,8 @@ class FormalRegExpSymbol;
 class FormalRegExpEmpty;
 class FormalRegExpEpsilon;
 
+class UnboundedRegExpElement;
+
 /**
  * Abstract class representing element in the regular expression. Can be operator or symbol.
  */
@@ -68,6 +70,12 @@ public:
 	
 	virtual FormalRegExpElement* plunder() && = 0;
 
+	/**
+	 * Creates copy of the element.
+	 * @return copy of the element
+	 */
+	virtual UnboundedRegExpElement* cloneAsUnbounded() const = 0;
+
 	virtual ~FormalRegExpElement() noexcept;
 
 	// RegExpEmpty < RegExpEpsilon < RegExpSymbol < RegExpIteration < RegExpAlternation < RegExpConcatenation
diff --git a/alib2data/src/regexp/formal/FormalRegExpEmpty.cpp b/alib2data/src/regexp/formal/FormalRegExpEmpty.cpp
index f9ffa8e78e..17f69d97b8 100644
--- a/alib2data/src/regexp/formal/FormalRegExpEmpty.cpp
+++ b/alib2data/src/regexp/formal/FormalRegExpEmpty.cpp
@@ -6,6 +6,7 @@
  */
 
 #include "FormalRegExpEmpty.h"
+#include "../unbounded/UnboundedRegExpEmpty.h"
 
 namespace regexp {
 
@@ -39,6 +40,10 @@ FormalRegExpElement* FormalRegExpEmpty::plunder() && {
 	return new FormalRegExpEmpty(std::move(*this));
 }
 
+UnboundedRegExpElement* FormalRegExpEmpty::cloneAsUnbounded() const {
+	return new UnboundedRegExpEmpty();
+}
+
 bool FormalRegExpEmpty::operator<(const FormalRegExpElement& other) const {
 	return other > *this;
 }
diff --git a/alib2data/src/regexp/formal/FormalRegExpEmpty.h b/alib2data/src/regexp/formal/FormalRegExpEmpty.h
index a34a9823e1..2416ac97b9 100644
--- a/alib2data/src/regexp/formal/FormalRegExpEmpty.h
+++ b/alib2data/src/regexp/formal/FormalRegExpEmpty.h
@@ -24,6 +24,11 @@ protected:
 	
 	virtual FormalRegExpElement* plunder() &&;
 
+	/**
+	 * @copydoc FormalRegExpElement::clone() const
+	 */
+	virtual UnboundedRegExpElement* cloneAsUnbounded() const;
+	
 	/**
 	 * @copydoc FormalRegExpElement::testSymbol() const
 	 */
diff --git a/alib2data/src/regexp/formal/FormalRegExpEpsilon.cpp b/alib2data/src/regexp/formal/FormalRegExpEpsilon.cpp
index dcbabffcb3..b434618e3a 100644
--- a/alib2data/src/regexp/formal/FormalRegExpEpsilon.cpp
+++ b/alib2data/src/regexp/formal/FormalRegExpEpsilon.cpp
@@ -6,6 +6,7 @@
  */
 
 #include "FormalRegExpEpsilon.h"
+#include "../unbounded/UnboundedRegExpEpsilon.h"
 
 namespace regexp {
 
@@ -39,6 +40,10 @@ FormalRegExpElement* FormalRegExpEpsilon::plunder() && {
 	return new FormalRegExpEpsilon(std::move(*this));
 }
 
+UnboundedRegExpElement* FormalRegExpEpsilon::cloneAsUnbounded() const {
+	return new UnboundedRegExpEpsilon();
+}
+
 bool FormalRegExpEpsilon::operator<(const FormalRegExpElement& other) const {
 	return other > *this;
 }
diff --git a/alib2data/src/regexp/formal/FormalRegExpEpsilon.h b/alib2data/src/regexp/formal/FormalRegExpEpsilon.h
index 44b6721496..9c510aaf58 100644
--- a/alib2data/src/regexp/formal/FormalRegExpEpsilon.h
+++ b/alib2data/src/regexp/formal/FormalRegExpEpsilon.h
@@ -24,6 +24,11 @@ protected:
 	
 	virtual FormalRegExpElement* plunder() &&;
 	
+	/**
+	 * @copydoc FormalRegExpElement::clone() const
+	 */
+	virtual UnboundedRegExpElement* cloneAsUnbounded() const;
+	
 	/**
 	 * @copydoc FormalRegExpElement::testSymbol() const
 	 */
diff --git a/alib2data/src/regexp/formal/FormalRegExpIteration.cpp b/alib2data/src/regexp/formal/FormalRegExpIteration.cpp
index 6198410e3d..6f851535e6 100644
--- a/alib2data/src/regexp/formal/FormalRegExpIteration.cpp
+++ b/alib2data/src/regexp/formal/FormalRegExpIteration.cpp
@@ -8,6 +8,8 @@
 #include "FormalRegExpIteration.h"
 #include "../../exception/AlibException.h"
 
+#include "../unbounded/UnboundedRegExpIteration.h"
+
 namespace regexp {
 
 FormalRegExpIteration::FormalRegExpIteration(FormalRegExpElement&& element) : element( NULL ) {
@@ -83,6 +85,13 @@ FormalRegExpElement* FormalRegExpIteration::plunder() && {
 	return new FormalRegExpIteration(std::move(*this));
 }
 
+UnboundedRegExpElement* FormalRegExpIteration::cloneAsUnbounded() const {
+	UnboundedRegExpElement* tmp = this->element->cloneAsUnbounded();
+	UnboundedRegExpElement* res = new UnboundedRegExpIteration(std::move(*tmp));
+	delete tmp;
+	return res;
+}
+
 bool FormalRegExpIteration::operator<(const FormalRegExpElement& other) const {
 	return other > *this;
 }
diff --git a/alib2data/src/regexp/formal/FormalRegExpIteration.h b/alib2data/src/regexp/formal/FormalRegExpIteration.h
index db404a7ee1..1d3a4110f2 100644
--- a/alib2data/src/regexp/formal/FormalRegExpIteration.h
+++ b/alib2data/src/regexp/formal/FormalRegExpIteration.h
@@ -28,6 +28,11 @@ protected:
 	
 	virtual FormalRegExpElement* plunder() &&;
 
+	/**
+	 * @copydoc FormalRegExpElement::clone() const
+	 */
+	virtual UnboundedRegExpElement* cloneAsUnbounded() const;
+	
 	/**
 	 * @copydoc FormalRegExpElement::testSymbol() const
 	 */
diff --git a/alib2data/src/regexp/formal/FormalRegExpSymbol.cpp b/alib2data/src/regexp/formal/FormalRegExpSymbol.cpp
index 7885ca4e73..3f3ee36666 100644
--- a/alib2data/src/regexp/formal/FormalRegExpSymbol.cpp
+++ b/alib2data/src/regexp/formal/FormalRegExpSymbol.cpp
@@ -6,6 +6,7 @@
  */
 
 #include "FormalRegExpSymbol.h"
+#include "../unbounded/UnboundedRegExpSymbol.h"
 
 namespace regexp {
 
@@ -52,6 +53,10 @@ FormalRegExpElement* FormalRegExpSymbol::plunder() && {
 	return new FormalRegExpSymbol(std::move(*this));
 }
 
+UnboundedRegExpElement* FormalRegExpSymbol::cloneAsUnbounded() const {
+	return new UnboundedRegExpSymbol(this->symbol);
+}
+
 bool FormalRegExpSymbol::operator==(const alphabet::Symbol& other) const {
 	return this->symbol == other;
 }
diff --git a/alib2data/src/regexp/formal/FormalRegExpSymbol.h b/alib2data/src/regexp/formal/FormalRegExpSymbol.h
index 3edb3aed80..f7c4615e99 100644
--- a/alib2data/src/regexp/formal/FormalRegExpSymbol.h
+++ b/alib2data/src/regexp/formal/FormalRegExpSymbol.h
@@ -28,6 +28,11 @@ protected:
 	
 	virtual FormalRegExpElement* plunder() &&;
 
+	/**
+	 * @copydoc FormalRegExpElement::clone() const
+	 */
+	virtual UnboundedRegExpElement* cloneAsUnbounded() const;
+	
 	/**
 	 * @copydoc FormalRegExpElement::testSymbol() const
 	 */
diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExp.cpp b/alib2data/src/regexp/unbounded/UnboundedRegExp.cpp
index 3d9f2a0102..77ba7a25d1 100644
--- a/alib2data/src/regexp/unbounded/UnboundedRegExp.cpp
+++ b/alib2data/src/regexp/unbounded/UnboundedRegExp.cpp
@@ -23,6 +23,13 @@ UnboundedRegExp::UnboundedRegExp() {
 	this->regExp = new UnboundedRegExpEmpty();
 }
 
+UnboundedRegExp::UnboundedRegExp(const FormalRegExp& other) : alphabet(other.getAlphabet()) {
+	this->regExp = NULL;
+	UnboundedRegExpElement* element = other.getRegExp().cloneAsUnbounded();
+	setRegExp(std::move(*element));
+	delete element;
+}
+
 UnboundedRegExp::UnboundedRegExp(const std::set<alphabet::Symbol>& alphabet, const UnboundedRegExpElement& regExp) : alphabet(alphabet) {
 	this->regExp = NULL;
 	setRegExp(regExp);
diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExp.h b/alib2data/src/regexp/unbounded/UnboundedRegExp.h
index 2812e23334..ef282ae9f2 100644
--- a/alib2data/src/regexp/unbounded/UnboundedRegExp.h
+++ b/alib2data/src/regexp/unbounded/UnboundedRegExp.h
@@ -42,6 +42,7 @@ protected:
 
 public:
 	UnboundedRegExp();
+	explicit UnboundedRegExp(const FormalRegExp& other);
 	UnboundedRegExp(const std::set<alphabet::Symbol>& alphabet, const UnboundedRegExpElement& regExp);
 	UnboundedRegExp(std::set<alphabet::Symbol>&& alphabet, UnboundedRegExpElement&& regExp);
 	explicit UnboundedRegExp(const UnboundedRegExpElement& regExp);
diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpAlternation.cpp b/alib2data/src/regexp/unbounded/UnboundedRegExpAlternation.cpp
index 1d3cfd9eaf..dad8b817cc 100644
--- a/alib2data/src/regexp/unbounded/UnboundedRegExpAlternation.cpp
+++ b/alib2data/src/regexp/unbounded/UnboundedRegExpAlternation.cpp
@@ -7,6 +7,8 @@
 
 #include "UnboundedRegExpAlternation.h"
 #include "../../exception/AlibException.h"
+#include "../formal/FormalRegExpAlternation.h"
+#include "../formal/FormalRegExpEmpty.h"
 
 namespace regexp {
 
@@ -81,6 +83,30 @@ UnboundedRegExpElement* UnboundedRegExpAlternation::plunder() && {
 	return new UnboundedRegExpAlternation(std::move(*this));
 }
 
+FormalRegExpElement* UnboundedRegExpAlternation::cloneAsFormal() const {
+	if(elements.size() == 0) return new FormalRegExpEmpty();
+	if(elements.size() == 1) return elements[0]->cloneAsFormal();
+
+	int size = elements.size();
+	FormalRegExpElement* rightTmp = elements[--size]->cloneAsFormal();
+	FormalRegExpElement* leftTmp = elements[--size]->cloneAsFormal();
+
+	FormalRegExpAlternation* res = new FormalRegExpAlternation(std::move(*leftTmp), std::move(*rightTmp));
+	delete rightTmp;
+	delete leftTmp;
+
+	while(size) {
+		FormalRegExpElement* tmp = res;
+		FormalRegExpElement* newLeft = elements[--size]->cloneAsFormal();
+
+		res = new FormalRegExpAlternation(std::move(*newLeft), std::move(*tmp));
+		delete tmp;
+		delete newLeft;
+	}
+
+	return res;
+}
+
 bool UnboundedRegExpAlternation::operator<(const UnboundedRegExpElement& other) const {
 	return other > *this;
 }
diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpAlternation.h b/alib2data/src/regexp/unbounded/UnboundedRegExpAlternation.h
index 1e51660957..e6a2fb21a3 100644
--- a/alib2data/src/regexp/unbounded/UnboundedRegExpAlternation.h
+++ b/alib2data/src/regexp/unbounded/UnboundedRegExpAlternation.h
@@ -24,6 +24,11 @@ protected:
 	 */
 	virtual UnboundedRegExpElement* clone() const;
 
+	/**
+	 * @copydoc UnboundedRegExpElement::cloneAsFormal() const
+	 */
+	virtual FormalRegExpElement* cloneAsFormal() const;
+
 	/**
 	 * @copydoc UnboundedRegExpElement::plunder() const
 	 */
diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpConcatenation.cpp b/alib2data/src/regexp/unbounded/UnboundedRegExpConcatenation.cpp
index bcf2c13742..bcbe39ad3f 100644
--- a/alib2data/src/regexp/unbounded/UnboundedRegExpConcatenation.cpp
+++ b/alib2data/src/regexp/unbounded/UnboundedRegExpConcatenation.cpp
@@ -7,6 +7,8 @@
 
 #include "UnboundedRegExpConcatenation.h"
 #include "../../exception/AlibException.h"
+#include "../formal/FormalRegExpConcatenation.h"
+#include "../formal/FormalRegExpEpsilon.h"
 
 namespace regexp {
 
@@ -80,6 +82,30 @@ UnboundedRegExpElement* UnboundedRegExpConcatenation::plunder() && {
 	return new UnboundedRegExpConcatenation(std::move(*this));
 }
 
+FormalRegExpElement* UnboundedRegExpConcatenation::cloneAsFormal() const {
+	if(elements.size() == 0) return new FormalRegExpEpsilon();
+	if(elements.size() == 1) return elements[0]->cloneAsFormal();
+
+	int size = elements.size();
+	FormalRegExpElement* rightTmp = elements[--size]->cloneAsFormal();
+	FormalRegExpElement* leftTmp = elements[--size]->cloneAsFormal();
+
+	FormalRegExpConcatenation* res = new FormalRegExpConcatenation(std::move(*leftTmp), std::move(*rightTmp));
+	delete rightTmp;
+	delete leftTmp;
+
+	while(size) {
+		FormalRegExpElement* tmp = res;
+		FormalRegExpElement* newLeft = elements[--size]->cloneAsFormal();
+
+		res = new FormalRegExpConcatenation(std::move(*newLeft), std::move(*tmp));
+		delete tmp;
+		delete newLeft;
+	}
+
+	return res;
+}
+
 bool UnboundedRegExpConcatenation::operator<(const UnboundedRegExpElement& other) const {
 	return other > *this;
 }
diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpConcatenation.h b/alib2data/src/regexp/unbounded/UnboundedRegExpConcatenation.h
index bf31844256..aee2fe4d21 100644
--- a/alib2data/src/regexp/unbounded/UnboundedRegExpConcatenation.h
+++ b/alib2data/src/regexp/unbounded/UnboundedRegExpConcatenation.h
@@ -23,7 +23,12 @@ protected:
 	 * @copydoc UnboundedRegExpElement::clone() const
 	 */
 	virtual UnboundedRegExpElement* clone() const;
-	
+
+	/**
+	 * @copydoc UnboundedRegExpElement::cloneAsFormal() const
+	 */
+	virtual FormalRegExpElement* cloneAsFormal() const;
+
 	virtual UnboundedRegExpElement* plunder() &&;
 
 	std::vector<UnboundedRegExpElement*> elements;
diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpElement.h b/alib2data/src/regexp/unbounded/UnboundedRegExpElement.h
index 00ee179a21..160f3ca5cc 100644
--- a/alib2data/src/regexp/unbounded/UnboundedRegExpElement.h
+++ b/alib2data/src/regexp/unbounded/UnboundedRegExpElement.h
@@ -24,12 +24,14 @@ class UnboundedRegExpSymbol;
 class UnboundedRegExpEmpty;
 class UnboundedRegExpEpsilon;
 
+class FormalRegExpElement;
+
 /**
  * Abstract class representing element in the regular expression. Can be operator or symbol.
  */
 class UnboundedRegExpElement : public std::elementBase<UnboundedRegExpAlternation, UnboundedRegExpConcatenation, UnboundedRegExpIteration, UnboundedRegExpSymbol, UnboundedRegExpEmpty, UnboundedRegExpEpsilon> {
 protected:
-	/* 
+	/**
 	 * Parent regexp contanining this instance of RegExpElement
 	 */
 	const UnboundedRegExp * parentRegExp;
@@ -68,6 +70,12 @@ public:
 	
 	virtual UnboundedRegExpElement* plunder() && = 0;
 
+	/**
+	 * Creates copy of the element.
+	 * @return copy of the element
+	 */
+	virtual FormalRegExpElement* cloneAsFormal() const = 0;
+
 	virtual ~UnboundedRegExpElement() noexcept;
 
 	// RegExpEmpty < RegExpEpsilon < RegExpSymbol < RegExpIteration < RegExpAlternation < RegExpConcatenation
diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpEmpty.cpp b/alib2data/src/regexp/unbounded/UnboundedRegExpEmpty.cpp
index 23bbd11300..d5d2c73f6f 100644
--- a/alib2data/src/regexp/unbounded/UnboundedRegExpEmpty.cpp
+++ b/alib2data/src/regexp/unbounded/UnboundedRegExpEmpty.cpp
@@ -6,6 +6,7 @@
  */
 
 #include "UnboundedRegExpEmpty.h"
+#include "../formal/FormalRegExpEmpty.h"
 
 namespace regexp {
 
@@ -39,6 +40,10 @@ UnboundedRegExpElement* UnboundedRegExpEmpty::plunder() && {
 	return new UnboundedRegExpEmpty(std::move(*this));
 }
 
+FormalRegExpElement* UnboundedRegExpEmpty::cloneAsFormal() const {
+	return new FormalRegExpEmpty();
+}
+
 bool UnboundedRegExpEmpty::operator<(const UnboundedRegExpElement& other) const {
 	return other > *this;
 }
diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpEmpty.h b/alib2data/src/regexp/unbounded/UnboundedRegExpEmpty.h
index 598d4a56ef..e5466db10d 100644
--- a/alib2data/src/regexp/unbounded/UnboundedRegExpEmpty.h
+++ b/alib2data/src/regexp/unbounded/UnboundedRegExpEmpty.h
@@ -24,6 +24,11 @@ protected:
 	
 	virtual UnboundedRegExpElement* plunder() &&;
 
+	/**
+	 * @copydoc UnboundedRegExpElement::cloneAsFormal() const
+	 */
+	virtual FormalRegExpElement* cloneAsFormal() const;
+
 	/**
 	 * @copydoc UnboundedRegExpElement::testSymbol() const
 	 */
diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpEpsilon.cpp b/alib2data/src/regexp/unbounded/UnboundedRegExpEpsilon.cpp
index 1054408192..706b4c455a 100644
--- a/alib2data/src/regexp/unbounded/UnboundedRegExpEpsilon.cpp
+++ b/alib2data/src/regexp/unbounded/UnboundedRegExpEpsilon.cpp
@@ -6,6 +6,7 @@
  */
 
 #include "UnboundedRegExpEpsilon.h"
+#include "../formal/FormalRegExpEpsilon.h"
 
 namespace regexp {
 
@@ -30,7 +31,7 @@ UnboundedRegExpEpsilon& UnboundedRegExpEpsilon::operator =(UnboundedRegExpEpsilo
   //this is actually different than default implementation
   return *this;
 }
-  
+
 UnboundedRegExpElement* UnboundedRegExpEpsilon::clone() const {
 	return new UnboundedRegExpEpsilon(*this);
 }
@@ -39,6 +40,10 @@ UnboundedRegExpElement* UnboundedRegExpEpsilon::plunder() && {
 	return new UnboundedRegExpEpsilon(std::move(*this));
 }
 
+FormalRegExpElement* UnboundedRegExpEpsilon::cloneAsFormal() const {
+	return new FormalRegExpEpsilon();
+}
+
 bool UnboundedRegExpEpsilon::operator<(const UnboundedRegExpElement& other) const {
 	return other > *this;
 }
diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpEpsilon.h b/alib2data/src/regexp/unbounded/UnboundedRegExpEpsilon.h
index d69f3f6701..61f6f5ef77 100644
--- a/alib2data/src/regexp/unbounded/UnboundedRegExpEpsilon.h
+++ b/alib2data/src/regexp/unbounded/UnboundedRegExpEpsilon.h
@@ -24,6 +24,11 @@ protected:
 	
 	virtual UnboundedRegExpElement* plunder() &&;
 	
+	/**
+	 * @copydoc UnboundedRegExpElement::cloneAsFormal() const
+	 */
+	virtual FormalRegExpElement* cloneAsFormal() const;
+
 	/**
 	 * @copydoc UnboundedRegExpElement::testSymbol() const
 	 */
diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpIteration.cpp b/alib2data/src/regexp/unbounded/UnboundedRegExpIteration.cpp
index 9e2322a0cf..7788d970ed 100644
--- a/alib2data/src/regexp/unbounded/UnboundedRegExpIteration.cpp
+++ b/alib2data/src/regexp/unbounded/UnboundedRegExpIteration.cpp
@@ -8,6 +8,8 @@
 #include "UnboundedRegExpIteration.h"
 #include "../../exception/AlibException.h"
 
+#include "../formal/FormalRegExpIteration.h"
+
 namespace regexp {
 
 UnboundedRegExpIteration::UnboundedRegExpIteration(UnboundedRegExpElement&& element) : element( NULL ) {
@@ -83,6 +85,13 @@ UnboundedRegExpElement* UnboundedRegExpIteration::plunder() && {
 	return new UnboundedRegExpIteration(std::move(*this));
 }
 
+FormalRegExpElement* UnboundedRegExpIteration::cloneAsFormal() const {
+	FormalRegExpElement* tmp = this->element->cloneAsFormal();
+	FormalRegExpElement* res = new FormalRegExpIteration(std::move(*tmp));
+	delete tmp;
+	return res;
+}
+
 bool UnboundedRegExpIteration::operator<(const UnboundedRegExpElement& other) const {
 	return other > *this;
 }
diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpIteration.h b/alib2data/src/regexp/unbounded/UnboundedRegExpIteration.h
index 7a8fe4e3ac..b16ddbbc22 100644
--- a/alib2data/src/regexp/unbounded/UnboundedRegExpIteration.h
+++ b/alib2data/src/regexp/unbounded/UnboundedRegExpIteration.h
@@ -28,6 +28,11 @@ protected:
 	
 	virtual UnboundedRegExpElement* plunder() &&;
 
+	/**
+	 * @copydoc UnboundedRegExpElement::cloneAsFormal() const
+	 */
+	virtual FormalRegExpElement* cloneAsFormal() const;
+
 	/**
 	 * @copydoc UnboundedRegExpElement::testSymbol() const
 	 */
diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpSymbol.cpp b/alib2data/src/regexp/unbounded/UnboundedRegExpSymbol.cpp
index 268abb7585..312bc642db 100644
--- a/alib2data/src/regexp/unbounded/UnboundedRegExpSymbol.cpp
+++ b/alib2data/src/regexp/unbounded/UnboundedRegExpSymbol.cpp
@@ -6,6 +6,7 @@
  */
 
 #include "UnboundedRegExpSymbol.h"
+#include "../formal/FormalRegExpSymbol.h"
 
 namespace regexp {
 
@@ -52,6 +53,10 @@ UnboundedRegExpElement* UnboundedRegExpSymbol::plunder() && {
 	return new UnboundedRegExpSymbol(std::move(*this));
 }
 
+FormalRegExpElement* UnboundedRegExpSymbol::cloneAsFormal() const {
+	return new FormalRegExpSymbol(this->symbol);
+}
+
 bool UnboundedRegExpSymbol::operator==(const alphabet::Symbol& other) const {
 	return this->symbol == other;
 }
diff --git a/alib2data/src/regexp/unbounded/UnboundedRegExpSymbol.h b/alib2data/src/regexp/unbounded/UnboundedRegExpSymbol.h
index f282d312c6..fd30fd2447 100644
--- a/alib2data/src/regexp/unbounded/UnboundedRegExpSymbol.h
+++ b/alib2data/src/regexp/unbounded/UnboundedRegExpSymbol.h
@@ -28,6 +28,11 @@ protected:
 	
 	virtual UnboundedRegExpElement* plunder() &&;
 
+	/**
+	 * @copydoc UnboundedRegExpElement::cloneAsFormal() const
+	 */
+	virtual FormalRegExpElement* cloneAsFormal() const;
+
 	/**
 	 * @copydoc UnboundedRegExpElement::testSymbol() const
 	 */
-- 
GitLab