From 48511ee7f16eb60e39bdcddbf0dcb95c895e92af Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Pecka?= <tomaspecka@gmail.com>
Date: Thu, 16 Jan 2014 16:03:55 +0100
Subject: [PATCH] Merge alib/ from master

---
 alib/makefile                                 | 13 +++++++
 alib/src/GrammarFactory.h                     | 15 ++++----
 alib/src/RegExpFactory.cpp                    | 28 ++++++++++++++
 alib/src/RegExpFactory.h                      | 38 +++++++++++++++++++
 alib/src/alphabet/Symbol.cpp                  |  6 +++
 alib/src/alphabet/Symbol.h                    |  2 +
 alib/src/automaton/Automaton.cpp              |  7 ++++
 alib/src/automaton/Automaton.h                |  9 ++++-
 alib/src/automaton/FSM/FSM.cpp                |  4 ++
 alib/src/automaton/FSM/FSM.h                  |  2 +
 alib/src/automaton/FSM/TransitionFSM.cpp      |  8 ++++
 alib/src/automaton/FSM/TransitionFSM.h        |  2 +
 alib/src/automaton/PDA/TransitionPDA.cpp      | 27 +++++++++++++
 alib/src/automaton/PDA/TransitionPDA.h        |  2 +
 alib/src/automaton/Shift.cpp                  | 10 +++++
 alib/src/automaton/Shift.h                    |  4 ++
 alib/src/automaton/State.cpp                  |  5 +++
 alib/src/automaton/State.h                    |  2 +
 alib/src/automaton/TM/TransitionTM.cpp        | 10 +++++
 alib/src/automaton/TM/TransitionTM.h          |  2 +
 alib/src/automaton/Transition.cpp             | 10 +++++
 alib/src/automaton/Transition.h               |  4 ++
 alib/src/automaton/UnknownTransition.cpp      | 28 ++++++++++++++
 alib/src/automaton/UnknownTransition.h        |  2 +
 .../{ => ContextFree}/ContextFreeGrammar.cpp  |  0
 .../{ => ContextFree}/ContextFreeGrammar.h    |  2 +-
 .../ContextSensitiveGrammar.cpp               |  0
 .../ContextSensitiveGrammar.h                 |  2 +-
 alib/src/grammar/Grammar.cpp                  |  5 +++
 alib/src/grammar/Grammar.h                    |  7 ++++
 .../{ => Linear}/LeftLinearGrammar.cpp        |  4 +-
 .../grammar/{ => Linear}/LeftLinearGrammar.h  |  4 +-
 alib/src/grammar/Linear/LinearGrammar.cpp     | 20 ++++++++++
 alib/src/grammar/Linear/LinearGrammar.h       | 26 +++++++++++++
 .../{ => Linear}/RightLinearGrammar.cpp       |  4 +-
 .../grammar/{ => Linear}/RightLinearGrammar.h |  4 +-
 .../{ => Regular}/LeftRegularGrammar.cpp      |  0
 .../{ => Regular}/LeftRegularGrammar.h        |  0
 .../grammar/{ => Regular}/RegularGrammar.cpp  |  0
 .../grammar/{ => Regular}/RegularGrammar.h    |  5 ++-
 .../{ => Regular}/RightRegularGrammar.cpp     |  0
 .../{ => Regular}/RightRegularGrammar.h       |  0
 alib/src/grammar/Rule.cpp                     | 24 ++++++++++++
 alib/src/grammar/Rule.h                       |  2 +
 .../UnrestrictedGrammar.cpp                   |  0
 .../{ => Unrestricted}/UnrestrictedGrammar.h  |  2 +-
 alib/src/regexp/RegExp.cpp                    |  5 +++
 alib/src/regexp/RegExp.h                      |  7 ++++
 alib/src/sax/SaxInterface.cpp                 |  2 +
 49 files changed, 344 insertions(+), 21 deletions(-)
 create mode 100644 alib/makefile
 create mode 100644 alib/src/RegExpFactory.cpp
 create mode 100644 alib/src/RegExpFactory.h
 create mode 100644 alib/src/automaton/Shift.cpp
 rename alib/src/grammar/{ => ContextFree}/ContextFreeGrammar.cpp (100%)
 rename alib/src/grammar/{ => ContextFree}/ContextFreeGrammar.h (98%)
 rename alib/src/grammar/{ => ContextSensitive}/ContextSensitiveGrammar.cpp (100%)
 rename alib/src/grammar/{ => ContextSensitive}/ContextSensitiveGrammar.h (98%)
 rename alib/src/grammar/{ => Linear}/LeftLinearGrammar.cpp (94%)
 rename alib/src/grammar/{ => Linear}/LeftLinearGrammar.h (93%)
 create mode 100644 alib/src/grammar/Linear/LinearGrammar.cpp
 create mode 100644 alib/src/grammar/Linear/LinearGrammar.h
 rename alib/src/grammar/{ => Linear}/RightLinearGrammar.cpp (95%)
 rename alib/src/grammar/{ => Linear}/RightLinearGrammar.h (93%)
 rename alib/src/grammar/{ => Regular}/LeftRegularGrammar.cpp (100%)
 rename alib/src/grammar/{ => Regular}/LeftRegularGrammar.h (100%)
 rename alib/src/grammar/{ => Regular}/RegularGrammar.cpp (100%)
 rename alib/src/grammar/{ => Regular}/RegularGrammar.h (94%)
 rename alib/src/grammar/{ => Regular}/RightRegularGrammar.cpp (100%)
 rename alib/src/grammar/{ => Regular}/RightRegularGrammar.h (100%)
 rename alib/src/grammar/{ => Unrestricted}/UnrestrictedGrammar.cpp (100%)
 rename alib/src/grammar/{ => Unrestricted}/UnrestrictedGrammar.h (97%)

diff --git a/alib/makefile b/alib/makefile
new file mode 100644
index 0000000000..6d1a3f5044
--- /dev/null
+++ b/alib/makefile
@@ -0,0 +1,13 @@
+LIBRARY=libalib.so 
+CXXFLAGS= -std=c++11 -O2 -c -Wall -fPIC -I/usr/include/libxml2/ 
+LDFLAGS= -shared -lxml2
+
+
+SOURCES = src/automaton/*.cpp src/automaton/TM/*.cpp src/automaton/FSM/*.cpp src/automaton/PDA/*cpp src/automaton/exception/*.cpp src/regexp/*.cpp src/grammar/*.cpp src/grammar/Regular/*.cpp src/grammar/Linear/*.cpp src/grammar/Unrestricted/*.cpp src/grammar/ContextSensitive/*.cpp src/grammar/ContextFree/*.cpp src/sax/*.cpp src/alphabet/*.cpp src/*.cpp
+
+all:
+	g++ $(CXXFLAGS) $(SOURCES)
+	g++ $(LDFLAGS) *.o -o $(LIBRARY)
+
+clean:
+	$(RM) *.o *.d $(LIBRARY)
diff --git a/alib/src/GrammarFactory.h b/alib/src/GrammarFactory.h
index 3eb5e1e7cb..d622843f5e 100644
--- a/alib/src/GrammarFactory.h
+++ b/alib/src/GrammarFactory.h
@@ -10,14 +10,13 @@
 
 #include "sax/Token.h"
 #include "grammar/UnknownGrammar.h"
-#include "grammar/RightRegularGrammar.h"
-#include "grammar/LeftRegularGrammar.h"
-#include "grammar/RightLinearGrammar.h"
-#include "grammar/LeftLinearGrammar.h"
-#include "grammar/ContextFreeGrammar.h"
-#include "grammar/ContextSensitiveGrammar.h"
-
-#include "grammar/UnrestrictedGrammar.h"
+#include "grammar/Regular/RightRegularGrammar.h"
+#include "grammar/Regular/LeftRegularGrammar.h"
+#include "grammar/Linear/RightLinearGrammar.h"
+#include "grammar/Linear/LeftLinearGrammar.h"
+#include "grammar/ContextFree/ContextFreeGrammar.h"
+#include "grammar/ContextSensitive/ContextSensitiveGrammar.h"
+#include "grammar/Unrestricted/UnrestrictedGrammar.h"
 
 namespace grammar {
 
diff --git a/alib/src/RegExpFactory.cpp b/alib/src/RegExpFactory.cpp
new file mode 100644
index 0000000000..0d5d9c514f
--- /dev/null
+++ b/alib/src/RegExpFactory.cpp
@@ -0,0 +1,28 @@
+/*
+ * RegExpFactory.cpp
+ *
+ *  Created on: Jan 1, 2014
+ *      Author: martin
+ */
+
+#include "sax/SaxInterface.h"
+#include "RegExpFactory.h"
+#include "regexp/RegExpParser.h"
+
+namespace regexp {
+
+RegExp RegExpFactory::fromFile(const string& filename) {
+	std::list<Token> tokens;
+	SaxInterface::parseFile(filename, tokens);
+	RegExpParser parser;
+	return parser.parse(tokens);
+}
+
+RegExp RegExpFactory::fromString(const string& str) {
+	list<Token> tokens;
+	SaxInterface::parseMemory(str, tokens);
+	RegExpParser parser;
+	return parser.parse(tokens);
+}
+
+} /* namespace regexp */
diff --git a/alib/src/RegExpFactory.h b/alib/src/RegExpFactory.h
new file mode 100644
index 0000000000..e81874405c
--- /dev/null
+++ b/alib/src/RegExpFactory.h
@@ -0,0 +1,38 @@
+/*
+ * RegExpFactory.h
+ *
+ *  Created on: Jan 1, 2014
+ *      Author: martin
+ */
+
+#ifndef REGEXPFACTORY_H_
+#define REGEXPFACTORY_H_
+
+#include "regexp/RegExp.h"
+
+namespace regexp {
+
+using namespace std;
+
+/**
+ * RegExp builder.
+ */
+class RegExpFactory {
+public:
+	/**
+	 * Parses the XML in file and returns the RegExp.
+	 * @param filename path to the file
+	 * @return RegExp
+	 */
+	static RegExp fromFile(const string& filename);
+
+	/**
+	 * Parses the XML and returns the RegExp.
+	 * @param str string containing the XML
+	 * @return RegExp
+	 */
+	static RegExp fromString(const string& str);
+};
+
+} /* namespace regexp */
+#endif /* REGEXPFACTORY_H_ */
diff --git a/alib/src/alphabet/Symbol.cpp b/alib/src/alphabet/Symbol.cpp
index c81332c3e5..c03d42d206 100644
--- a/alib/src/alphabet/Symbol.cpp
+++ b/alib/src/alphabet/Symbol.cpp
@@ -29,4 +29,10 @@ bool Symbol::operator !=(const Symbol& other) const {
 	return symbol != other.symbol;
 }
 
+std::ostream& operator<<(std::ostream& out, const Symbol& symbol) {
+
+	out << "Symbol " << (symbol.symbol == "" ? "\\epsilon" : symbol.symbol);
+	return out;
+}
+
 } /* namespace language */
diff --git a/alib/src/alphabet/Symbol.h b/alib/src/alphabet/Symbol.h
index 1d2f01faeb..a49c107b60 100644
--- a/alib/src/alphabet/Symbol.h
+++ b/alib/src/alphabet/Symbol.h
@@ -34,6 +34,8 @@ public:
 	bool operator <(const Symbol& other) const;
 	bool operator ==(const Symbol& other) const;
 	bool operator !=(const Symbol& other) const;
+
+	friend std::ostream& operator<<(std::ostream&, const Symbol&);
 };
 }
 #endif /* SYMBOL_H_ */
diff --git a/alib/src/automaton/Automaton.cpp b/alib/src/automaton/Automaton.cpp
index 519b65c7c2..27235c0138 100644
--- a/alib/src/automaton/Automaton.cpp
+++ b/alib/src/automaton/Automaton.cpp
@@ -77,4 +77,11 @@ const set<State>& Automaton::getFinalStates() const {
 	return finalStates;
 }
 
+ostream& operator <<(ostream& out, const Automaton& automaton) {
+	automaton.toXML(out);
+	return out;
+}
+
 } /* namespace automaton */
+
+
diff --git a/alib/src/automaton/Automaton.h b/alib/src/automaton/Automaton.h
index bfbfd4cfb8..7686fe04e9 100644
--- a/alib/src/automaton/Automaton.h
+++ b/alib/src/automaton/Automaton.h
@@ -111,10 +111,17 @@ public:
 	const set<State>& getFinalStates() const;
 
 	/**
-	 * Prints XML representation of the automaton.
+	 * Prints XML representation of the automaton to the ostream.
 	 * @param out output stream to which print the automaton
 	 */
 	virtual void toXML(ostream& out) const = 0;
+
+	/**
+	 * Prints XML representation of the automaton to the ostream.
+	 * @param out output stream to which print the automaton
+	 * @param automaton automaton to print
+	 */
+	friend ostream& operator<<(ostream& out, const Automaton& automaton);
 };
 
 } /* namespace automaton */
diff --git a/alib/src/automaton/FSM/FSM.cpp b/alib/src/automaton/FSM/FSM.cpp
index 6a90f060c6..5d5804ecce 100644
--- a/alib/src/automaton/FSM/FSM.cpp
+++ b/alib/src/automaton/FSM/FSM.cpp
@@ -132,4 +132,8 @@ bool FSM::isDeterministic() const {
 	return true;
 }
 
+bool FSM::isTotal() const {
+	return isDeterministic() && transitions.size() == inputAlphabet.size() * states.size();
+}
+
 } /* namespace automaton */
diff --git a/alib/src/automaton/FSM/FSM.h b/alib/src/automaton/FSM/FSM.h
index 0e43b41cde..9cbafba416 100644
--- a/alib/src/automaton/FSM/FSM.h
+++ b/alib/src/automaton/FSM/FSM.h
@@ -83,6 +83,8 @@ public:
 	 */
 	bool isDeterministic() const;
 
+	bool isTotal() const;
+
 	/**
 	 * @copydoc Automaton::toXML(ostream&) const
 	 */
diff --git a/alib/src/automaton/FSM/TransitionFSM.cpp b/alib/src/automaton/FSM/TransitionFSM.cpp
index 950496a955..fff1c43a7b 100644
--- a/alib/src/automaton/FSM/TransitionFSM.cpp
+++ b/alib/src/automaton/FSM/TransitionFSM.cpp
@@ -29,4 +29,12 @@ bool TransitionFSM::operator != (const TransitionFSM& other) const {
 	return from != other.from || input != other.input || to != other.to;
 }
 
+std::ostream& TransitionFSM::operator>>(std::ostream& out) const {
+	out << "TransitionFSM from = " << this->from
+		<< " to = " << this->to
+		<< " input = " << this->input;
+
+	return out;
+}
+
 } /* namespace automaton */
diff --git a/alib/src/automaton/FSM/TransitionFSM.h b/alib/src/automaton/FSM/TransitionFSM.h
index 17dc9e52d1..2c581370ac 100644
--- a/alib/src/automaton/FSM/TransitionFSM.h
+++ b/alib/src/automaton/FSM/TransitionFSM.h
@@ -29,6 +29,8 @@ public:
 	bool operator <(const TransitionFSM& other) const;
 	bool operator ==(const TransitionFSM& other) const;
 	bool operator !=(const TransitionFSM& other) const;
+
+	std::ostream& operator>>(std::ostream& out) const;
 };
 
 } /* namespace automaton */
diff --git a/alib/src/automaton/PDA/TransitionPDA.cpp b/alib/src/automaton/PDA/TransitionPDA.cpp
index 88c067cdfd..6f14b7d00e 100644
--- a/alib/src/automaton/PDA/TransitionPDA.cpp
+++ b/alib/src/automaton/PDA/TransitionPDA.cpp
@@ -120,4 +120,31 @@ bool TransitionPDA::operator !=(const TransitionPDA& other) const {
 	return !((*this) == other);
 }
 
+std::ostream& TransitionPDA::operator>>(std::ostream& out) const {
+	bool first;
+	out << "TransitionPDA from = " << this->from
+		<< " to = " << this->to
+		<< " input = " << this->input
+		<< " pop = [";
+
+	first = true;
+	for(list<Symbol>::const_iterator iter = this->pop.begin(); iter != this->pop.end(); iter++) {
+		if(!first) out << ", ";
+		first = false;
+		out << *iter;
+	}
+
+	out << "] push = [";
+
+	first = true;
+	for(list<Symbol>::const_iterator iter = this->push.begin(); iter != this->push.end(); iter++) {
+		if(!first) out << ", ";
+		first = false;
+		out << *iter;
+	}
+	out << "]";
+
+	return out;
+}
+
 } /* namespace automaton */
diff --git a/alib/src/automaton/PDA/TransitionPDA.h b/alib/src/automaton/PDA/TransitionPDA.h
index 8f2f0b5614..9fcc2b71a8 100644
--- a/alib/src/automaton/PDA/TransitionPDA.h
+++ b/alib/src/automaton/PDA/TransitionPDA.h
@@ -32,6 +32,8 @@ public:
 	bool operator <(const TransitionPDA& other) const;
 	bool operator ==(const TransitionPDA& other) const;
 	bool operator !=(const TransitionPDA& other) const;
+
+	std::ostream& operator>>(std::ostream& out) const;
 };
 
 } /* namespace automaton */
diff --git a/alib/src/automaton/Shift.cpp b/alib/src/automaton/Shift.cpp
new file mode 100644
index 0000000000..03f2e55c21
--- /dev/null
+++ b/alib/src/automaton/Shift.cpp
@@ -0,0 +1,10 @@
+/*
+ * Shift.cpp
+ *
+ *  Created on: Dec 8, 2013
+ *      Author: honza
+ */
+
+#include "Shift.h"
+
+std::string SHIFT_NAMES[] = {"LEFT", "RIGHT", "NONE", "NOT_SET" };
diff --git a/alib/src/automaton/Shift.h b/alib/src/automaton/Shift.h
index af9632f9e4..721caff2ad 100644
--- a/alib/src/automaton/Shift.h
+++ b/alib/src/automaton/Shift.h
@@ -8,6 +8,8 @@
 #ifndef SHIFT_H_
 #define SHIFT_H_
 
+#include <string>
+
 /**
  * Represent movement of the reading head in the Turing machine.
  */
@@ -15,4 +17,6 @@ enum Shift {
 	LEFT, RIGHT, NONE, NOT_SET
 };
 
+extern std::string SHIFT_NAMES[4];
+
 #endif /* SHIFT_H_ */
diff --git a/alib/src/automaton/State.cpp b/alib/src/automaton/State.cpp
index 4c2a3fb370..d278b21fb3 100644
--- a/alib/src/automaton/State.cpp
+++ b/alib/src/automaton/State.cpp
@@ -28,4 +28,9 @@ bool State::operator != (const State& other) const{
 	return name != other.name;
 }
 
+std::ostream& operator<<(std::ostream& out, const State& state) {
+	out << "State " << state.name;
+	return out;
+}
+
 } /* namespace automaton */
diff --git a/alib/src/automaton/State.h b/alib/src/automaton/State.h
index 6ffad3c43f..1cb546d3ca 100644
--- a/alib/src/automaton/State.h
+++ b/alib/src/automaton/State.h
@@ -26,6 +26,8 @@ public:
 	bool operator < (const State& other) const;
 	bool operator == (const State& other) const;
 	bool operator != (const State& other) const;
+
+	friend std::ostream& operator<<(std::ostream&, const State&);
 };
 
 } /* namespace automaton */
diff --git a/alib/src/automaton/TM/TransitionTM.cpp b/alib/src/automaton/TM/TransitionTM.cpp
index 2c67ffdafa..ac535ee8dd 100644
--- a/alib/src/automaton/TM/TransitionTM.cpp
+++ b/alib/src/automaton/TM/TransitionTM.cpp
@@ -49,4 +49,14 @@ bool TransitionTM::operator !=(const TransitionTM& other) const {
 	return !((*this) == other);
 }
 
+std::ostream& TransitionTM::operator>>(std::ostream& out) const {
+	out << "TransitionTM from = " << this->from
+		<< " to = " << this->to
+		<< " input = " << this->input
+		<< " output = " << this->output
+		<< " shift = " << SHIFT_NAMES[this->shift];
+
+	return out;
+}
+
 } /* namespace automaton */
diff --git a/alib/src/automaton/TM/TransitionTM.h b/alib/src/automaton/TM/TransitionTM.h
index 505681a531..3ce2559b19 100644
--- a/alib/src/automaton/TM/TransitionTM.h
+++ b/alib/src/automaton/TM/TransitionTM.h
@@ -56,6 +56,8 @@ public:
 	bool operator <(const TransitionTM& other) const;
 	bool operator ==(const TransitionTM& other) const;
 	bool operator !=(const TransitionTM& other) const;
+
+	std::ostream& operator>>(std::ostream& out) const;
 };
 
 } /* namespace automaton */
diff --git a/alib/src/automaton/Transition.cpp b/alib/src/automaton/Transition.cpp
index 48166b2337..3c0aea6d29 100644
--- a/alib/src/automaton/Transition.cpp
+++ b/alib/src/automaton/Transition.cpp
@@ -42,4 +42,14 @@ bool Transition::containsState(const State& state) const {
 	return from == state || to == state;
 }
 
+std::ostream& operator<<(std::ostream& out, const Transition& transition) {
+	transition >> out;
+	return out;
+}
+
+std::ostream& Transition::operator>>(std::ostream& out) const {
+	out << "Transition from = " << this->from << " to = " << this->to << " input = " << this->input;
+	return out;
+}
+
 } /* namespace automaton */
diff --git a/alib/src/automaton/Transition.h b/alib/src/automaton/Transition.h
index 857ee473ad..c3fce1ef4d 100644
--- a/alib/src/automaton/Transition.h
+++ b/alib/src/automaton/Transition.h
@@ -62,6 +62,10 @@ public:
 	 * @return true when transition contains the state, false otherwise
 	 */
 	bool containsState(const State& state) const;
+
+	friend std::ostream& operator<<(std::ostream&, const Transition&);
+
+	virtual std::ostream& operator>>(std::ostream&) const;
 };
 
 } /* namespace automaton */
diff --git a/alib/src/automaton/UnknownTransition.cpp b/alib/src/automaton/UnknownTransition.cpp
index a452f18d9e..878aededbe 100644
--- a/alib/src/automaton/UnknownTransition.cpp
+++ b/alib/src/automaton/UnknownTransition.cpp
@@ -138,4 +138,32 @@ bool UnknownTransition::operator !=(const UnknownTransition& other) const {
 	return !((*this) == other);
 }
 
+std::ostream& UnknownTransition::operator>>(std::ostream& out) const {
+	bool first;
+	out << "UnknownTransition from = " << this->from
+		<< " to = " << this->to
+		<< " input = " << this->input
+		<< " output = " << this->output
+		<< " pop = [";
+
+	first = true;
+	for(list<Symbol>::const_iterator iter = this->pop.begin(); iter != this->pop.end(); iter++) {
+		if(!first) out << ", ";
+		first = false;
+		out << *iter;
+	}
+
+	out << "] push = [";
+
+	first = true;
+	for(list<Symbol>::const_iterator iter = this->push.begin(); iter != this->push.end(); iter++) {
+		if(!first) out << ", ";
+		first = false;
+		out << *iter;
+	}
+	out << "] shift = " << SHIFT_NAMES[this->shift];
+
+	return out;
+}
+
 } /* namespace automaton */
diff --git a/alib/src/automaton/UnknownTransition.h b/alib/src/automaton/UnknownTransition.h
index 19621d0d92..0dcf1ee45d 100644
--- a/alib/src/automaton/UnknownTransition.h
+++ b/alib/src/automaton/UnknownTransition.h
@@ -81,6 +81,8 @@ public:
 	bool operator <(const UnknownTransition& other) const;
 	bool operator ==(const UnknownTransition& other) const;
 	bool operator !=(const UnknownTransition& other) const;
+
+	virtual std::ostream& operator>>(std::ostream&) const;
 };
 
 } /* namespace automaton */
diff --git a/alib/src/grammar/ContextFreeGrammar.cpp b/alib/src/grammar/ContextFree/ContextFreeGrammar.cpp
similarity index 100%
rename from alib/src/grammar/ContextFreeGrammar.cpp
rename to alib/src/grammar/ContextFree/ContextFreeGrammar.cpp
diff --git a/alib/src/grammar/ContextFreeGrammar.h b/alib/src/grammar/ContextFree/ContextFreeGrammar.h
similarity index 98%
rename from alib/src/grammar/ContextFreeGrammar.h
rename to alib/src/grammar/ContextFree/ContextFreeGrammar.h
index febbb040c6..5c481e1b05 100644
--- a/alib/src/grammar/ContextFreeGrammar.h
+++ b/alib/src/grammar/ContextFree/ContextFreeGrammar.h
@@ -8,7 +8,7 @@
 #ifndef CONTEXTFREEGRAMMAR_H_
 #define CONTEXTFREEGRAMMAR_H_
 
-#include "Grammar.h"
+#include "../Grammar.h"
 
 namespace grammar {
 
diff --git a/alib/src/grammar/ContextSensitiveGrammar.cpp b/alib/src/grammar/ContextSensitive/ContextSensitiveGrammar.cpp
similarity index 100%
rename from alib/src/grammar/ContextSensitiveGrammar.cpp
rename to alib/src/grammar/ContextSensitive/ContextSensitiveGrammar.cpp
diff --git a/alib/src/grammar/ContextSensitiveGrammar.h b/alib/src/grammar/ContextSensitive/ContextSensitiveGrammar.h
similarity index 98%
rename from alib/src/grammar/ContextSensitiveGrammar.h
rename to alib/src/grammar/ContextSensitive/ContextSensitiveGrammar.h
index d41d68f272..4b6d8d62c4 100644
--- a/alib/src/grammar/ContextSensitiveGrammar.h
+++ b/alib/src/grammar/ContextSensitive/ContextSensitiveGrammar.h
@@ -8,7 +8,7 @@
 #ifndef CONTEXTSENSITIVEGRAMMAR_H_
 #define CONTEXTSENSITIVEGRAMMAR_H_
 
-#include "Grammar.h"
+#include "../Grammar.h"
 
 namespace grammar {
 
diff --git a/alib/src/grammar/Grammar.cpp b/alib/src/grammar/Grammar.cpp
index be42c3f2d1..c1d28daed3 100644
--- a/alib/src/grammar/Grammar.cpp
+++ b/alib/src/grammar/Grammar.cpp
@@ -114,4 +114,9 @@ void Grammar::toXML(ostream& out) const {
 	GrammarPrinter::toXML(*this, out);
 }
 
+ostream& operator <<(ostream& out, const Grammar& grammar) {
+	grammar.toXML(out);
+	return out;
+}
+
 } /* namespace grammar */
diff --git a/alib/src/grammar/Grammar.h b/alib/src/grammar/Grammar.h
index a6833a74f7..fd4c2fe856 100644
--- a/alib/src/grammar/Grammar.h
+++ b/alib/src/grammar/Grammar.h
@@ -116,6 +116,13 @@ public:
 	 * @param out output stream to print to
 	 */
 	void toXML(ostream& out) const;
+
+	/**
+	 * Prints the XML representation of grammar to the output stream.
+	 * @param out output stream to print to
+	 * @param grammar grammar to print
+	 */
+	friend ostream& operator<<(ostream& out, const Grammar& grammar);
 };
 
 } /* namespace grammar */
diff --git a/alib/src/grammar/LeftLinearGrammar.cpp b/alib/src/grammar/Linear/LeftLinearGrammar.cpp
similarity index 94%
rename from alib/src/grammar/LeftLinearGrammar.cpp
rename to alib/src/grammar/Linear/LeftLinearGrammar.cpp
index f64fd515e2..325b652b33 100644
--- a/alib/src/grammar/LeftLinearGrammar.cpp
+++ b/alib/src/grammar/Linear/LeftLinearGrammar.cpp
@@ -10,12 +10,12 @@
 namespace grammar {
 
 LeftLinearGrammar::LeftLinearGrammar() :
-		Grammar() {
+		LinearGrammar() {
 }
 
 LeftLinearGrammar::LeftLinearGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols,
 		const Symbol& startSymbol) :
-		Grammar(nonTerminalSymbols, terminalSymbols, startSymbol) {
+		LinearGrammar(nonTerminalSymbols, terminalSymbols, startSymbol) {
 }
 
 bool LeftLinearGrammar::isValidRule(const Rule& rule) const {
diff --git a/alib/src/grammar/LeftLinearGrammar.h b/alib/src/grammar/Linear/LeftLinearGrammar.h
similarity index 93%
rename from alib/src/grammar/LeftLinearGrammar.h
rename to alib/src/grammar/Linear/LeftLinearGrammar.h
index c82113d3d3..c81f09d022 100644
--- a/alib/src/grammar/LeftLinearGrammar.h
+++ b/alib/src/grammar/Linear/LeftLinearGrammar.h
@@ -8,14 +8,14 @@
 #ifndef LEFTLINEARGRAMMAR_H_
 #define LEFTLINEARGRAMMAR_H_
 
-#include "Grammar.h"
+#include "LinearGrammar.h"
 
 namespace grammar {
 
 /**
  * Left linear grammar. Produces regular language.
  */
-class LeftLinearGrammar: public Grammar {
+class LeftLinearGrammar: public LinearGrammar {
 private:
 	/**
 	 * Checks that left side of the Rule contains one symbol which is nonterminal.
diff --git a/alib/src/grammar/Linear/LinearGrammar.cpp b/alib/src/grammar/Linear/LinearGrammar.cpp
new file mode 100644
index 0000000000..21e534224e
--- /dev/null
+++ b/alib/src/grammar/Linear/LinearGrammar.cpp
@@ -0,0 +1,20 @@
+/*
+ * LinearGrammar.cpp
+ *
+ *  Created on: Jan 1, 2014
+ *      Author: martin
+ */
+
+#include "LinearGrammar.h"
+
+namespace grammar {
+
+LinearGrammar::LinearGrammar() :
+		Grammar() {
+}
+
+LinearGrammar::LinearGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols,
+		const Symbol& startSymbol) :
+		Grammar(nonTerminalSymbols, terminalSymbols, startSymbol) {
+}
+} /* namespace grammar */
diff --git a/alib/src/grammar/Linear/LinearGrammar.h b/alib/src/grammar/Linear/LinearGrammar.h
new file mode 100644
index 0000000000..9c3a95f736
--- /dev/null
+++ b/alib/src/grammar/Linear/LinearGrammar.h
@@ -0,0 +1,26 @@
+/*
+ * LinearGrammar.h
+ *
+ *  Created on: Jan 1, 2014
+ *      Author: Martin Zak
+ */
+
+#ifndef LINEARGRAMMAR_H_
+#define LINEARGRAMMAR_H_
+
+#include "../Grammar.h"
+
+namespace grammar {
+
+/**
+ * Abstract class for linear grammars.
+ */
+class LinearGrammar: public Grammar {
+public:
+	LinearGrammar();
+	LinearGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols,
+			const Symbol& startSymbol);
+};
+
+} /* namespace grammar */
+#endif /* LINEARGRAMMAR_H_ */
diff --git a/alib/src/grammar/RightLinearGrammar.cpp b/alib/src/grammar/Linear/RightLinearGrammar.cpp
similarity index 95%
rename from alib/src/grammar/RightLinearGrammar.cpp
rename to alib/src/grammar/Linear/RightLinearGrammar.cpp
index e32612d742..10d34e1fbf 100644
--- a/alib/src/grammar/RightLinearGrammar.cpp
+++ b/alib/src/grammar/Linear/RightLinearGrammar.cpp
@@ -10,12 +10,12 @@
 namespace grammar {
 
 RightLinearGrammar::RightLinearGrammar() :
-		Grammar() {
+		LinearGrammar() {
 }
 
 RightLinearGrammar::RightLinearGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols,
 		const Symbol& startSymbol) :
-		Grammar(nonTerminalSymbols, terminalSymbols, startSymbol) {
+		LinearGrammar(nonTerminalSymbols, terminalSymbols, startSymbol) {
 }
 
 bool RightLinearGrammar::isValidRule(const Rule& rule) const {
diff --git a/alib/src/grammar/RightLinearGrammar.h b/alib/src/grammar/Linear/RightLinearGrammar.h
similarity index 93%
rename from alib/src/grammar/RightLinearGrammar.h
rename to alib/src/grammar/Linear/RightLinearGrammar.h
index 7f0ec1a209..8397ece746 100644
--- a/alib/src/grammar/RightLinearGrammar.h
+++ b/alib/src/grammar/Linear/RightLinearGrammar.h
@@ -8,14 +8,14 @@
 #ifndef RIGHTLINEARGRAMMAR_H_
 #define RIGHTLINEARGRAMMAR_H_
 
-#include "Grammar.h"
+#include "LinearGrammar.h"
 
 namespace grammar {
 
 /**
  * Right regular grammar. Produces regular language.
  */
-class RightLinearGrammar: public Grammar {
+class RightLinearGrammar: public LinearGrammar {
 private:
 	/**
 	 * Checks that left side of the Rule contains one symbol which is nonterminal.
diff --git a/alib/src/grammar/LeftRegularGrammar.cpp b/alib/src/grammar/Regular/LeftRegularGrammar.cpp
similarity index 100%
rename from alib/src/grammar/LeftRegularGrammar.cpp
rename to alib/src/grammar/Regular/LeftRegularGrammar.cpp
diff --git a/alib/src/grammar/LeftRegularGrammar.h b/alib/src/grammar/Regular/LeftRegularGrammar.h
similarity index 100%
rename from alib/src/grammar/LeftRegularGrammar.h
rename to alib/src/grammar/Regular/LeftRegularGrammar.h
diff --git a/alib/src/grammar/RegularGrammar.cpp b/alib/src/grammar/Regular/RegularGrammar.cpp
similarity index 100%
rename from alib/src/grammar/RegularGrammar.cpp
rename to alib/src/grammar/Regular/RegularGrammar.cpp
diff --git a/alib/src/grammar/RegularGrammar.h b/alib/src/grammar/Regular/RegularGrammar.h
similarity index 94%
rename from alib/src/grammar/RegularGrammar.h
rename to alib/src/grammar/Regular/RegularGrammar.h
index daa090e269..639326417c 100644
--- a/alib/src/grammar/RegularGrammar.h
+++ b/alib/src/grammar/Regular/RegularGrammar.h
@@ -8,10 +8,13 @@
 #ifndef REGULARGRAMMAR_H_
 #define REGULARGRAMMAR_H_
 
-#include "Grammar.h"
+#include "../Grammar.h"
 
 namespace grammar {
 
+/**
+ * Abstract class for regular grammars.
+ */
 class RegularGrammar: public Grammar {
 protected:
 	/**
diff --git a/alib/src/grammar/RightRegularGrammar.cpp b/alib/src/grammar/Regular/RightRegularGrammar.cpp
similarity index 100%
rename from alib/src/grammar/RightRegularGrammar.cpp
rename to alib/src/grammar/Regular/RightRegularGrammar.cpp
diff --git a/alib/src/grammar/RightRegularGrammar.h b/alib/src/grammar/Regular/RightRegularGrammar.h
similarity index 100%
rename from alib/src/grammar/RightRegularGrammar.h
rename to alib/src/grammar/Regular/RightRegularGrammar.h
diff --git a/alib/src/grammar/Rule.cpp b/alib/src/grammar/Rule.cpp
index 8113b2ca37..1abe676b25 100644
--- a/alib/src/grammar/Rule.cpp
+++ b/alib/src/grammar/Rule.cpp
@@ -102,4 +102,28 @@ bool Rule::operator !=(const Rule& other) const {
 			|| !equal(rightSide.begin(), rightSide.end(), other.rightSide.begin());
 }
 
+ostream& operator<<(ostream& out, const Rule& rule) {
+	bool first;
+	out << " leftSide = [";
+
+	first = true;
+	for(list<Symbol>::const_iterator iter = rule.leftSide.begin(); iter != rule.leftSide.end(); iter++) {
+		if(!first) out << ", ";
+		first = false;
+		out << *iter;
+	}
+
+	out << "] rightSide = [";
+
+	first = true;
+	for(list<Symbol>::const_iterator iter = rule.rightSide.begin(); iter != rule.rightSide.end(); iter++) {
+		if(!first) out << ", ";
+		first = false;
+		out << *iter;
+	}
+	out << "]";
+
+	return out;
+}
+
 } /* namespace grammar */
diff --git a/alib/src/grammar/Rule.h b/alib/src/grammar/Rule.h
index 1395dc2fef..f708aa71f5 100644
--- a/alib/src/grammar/Rule.h
+++ b/alib/src/grammar/Rule.h
@@ -65,6 +65,8 @@ public:
 	bool operator <(const Rule& other) const;
 	bool operator ==(const Rule& other) const;
 	bool operator !=(const Rule& other) const;
+
+	friend ostream& operator<<(ostream&, const Rule&);
 };
 
 } /* namespace grammar */
diff --git a/alib/src/grammar/UnrestrictedGrammar.cpp b/alib/src/grammar/Unrestricted/UnrestrictedGrammar.cpp
similarity index 100%
rename from alib/src/grammar/UnrestrictedGrammar.cpp
rename to alib/src/grammar/Unrestricted/UnrestrictedGrammar.cpp
diff --git a/alib/src/grammar/UnrestrictedGrammar.h b/alib/src/grammar/Unrestricted/UnrestrictedGrammar.h
similarity index 97%
rename from alib/src/grammar/UnrestrictedGrammar.h
rename to alib/src/grammar/Unrestricted/UnrestrictedGrammar.h
index 9bd424f3c3..eb9802d07b 100644
--- a/alib/src/grammar/UnrestrictedGrammar.h
+++ b/alib/src/grammar/Unrestricted/UnrestrictedGrammar.h
@@ -8,7 +8,7 @@
 #ifndef UNRESTRICTEDGRAMMAR_H_
 #define UNRESTRICTEDGRAMMAR_H_
 
-#include "Grammar.h"
+#include "../Grammar.h"
 
 namespace grammar {
 
diff --git a/alib/src/regexp/RegExp.cpp b/alib/src/regexp/RegExp.cpp
index 92641f64ab..6cec78b363 100644
--- a/alib/src/regexp/RegExp.cpp
+++ b/alib/src/regexp/RegExp.cpp
@@ -70,4 +70,9 @@ void RegExp::toXML(ostream& out) {
 	RegExpPrinter::toXML(*this, out);
 }
 
+ostream& operator <<(ostream& out, RegExp& regexp) {
+	regexp.toXML(out);
+	return out;
+}
+
 } /* namespace regexp */
diff --git a/alib/src/regexp/RegExp.h b/alib/src/regexp/RegExp.h
index f26f94fbf7..73507c1fa4 100644
--- a/alib/src/regexp/RegExp.h
+++ b/alib/src/regexp/RegExp.h
@@ -54,6 +54,13 @@ public:
 	 * @param out output stream to which print the RegExp
 	 */
 	void toXML(ostream& out);
+
+	/**
+	 * Prints XML representation of the RegExp to the output stream.
+	 * @param out output stream to which print the RegExp
+	 * @param regexp RegExp to print
+	 */
+	friend ostream& operator<<(ostream& out, RegExp& regexp);
 };
 
 } /* namespace regexp */
diff --git a/alib/src/sax/SaxInterface.cpp b/alib/src/sax/SaxInterface.cpp
index b963e752bf..6370398af1 100644
--- a/alib/src/sax/SaxInterface.cpp
+++ b/alib/src/sax/SaxInterface.cpp
@@ -65,6 +65,8 @@ void SaxInterface::characters(void * userData, const xmlChar * ch, int len) {
 }
 
 void SaxInterface::startElement(void* userData, const xmlChar* name, const xmlChar** attrs) {
+	(void)attrs;
+
 	list<Token> &out = *((list<Token>*) userData);
 	out.push_back(Token((char*) name, Token::START_ELEMENT));
 }
-- 
GitLab