diff --git a/alib/makefile b/alib/makefile deleted file mode 100644 index 588666118e0af0cb1b4ed5e090314199f99bb4d8..0000000000000000000000000000000000000000 --- a/alib/makefile +++ /dev/null @@ -1,20 +0,0 @@ -CC=g++ -LIBRARY=libalib.so -CCFLAGS= -std=c++11 -O2 -g -c -Wall -fPIC -I/usr/include/libxml2/ -LDFLAGS= -shared -lxml2 - -SOURCES=$(shell find src/ -name *cpp) -OBJECTS=$(patsubst src/%.cpp, obj/%.o, $(SOURCES)) - -all: $(SOURCES) lib/$(LIBRARY) - -lib/$(LIBRARY): $(OBJECTS) - mkdir -p lib - $(CC) $(OBJECTS) -o $@ $(LDFLAGS) - -obj/%.o: src/%.cpp - mkdir -p $(dir $@) - $(CC) $(CCFLAGS) $< -o $@ - -clean: - $(RM) -r *.o *.d lib obj diff --git a/alib/src/AlibException.cpp b/alib/src/AlibException.cpp deleted file mode 100644 index 3e6e35b633621a9b11a7ea5f19f9f849314b5716..0000000000000000000000000000000000000000 --- a/alib/src/AlibException.cpp +++ /dev/null @@ -1,36 +0,0 @@ -/* - * AlibException.cpp - * - * Created on: Apr 1, 2013 - * Author: martin - */ - -#include "AlibException.h" - -namespace alib { - -using namespace std; - -AlibException::AlibException() { - -} - -AlibException::AlibException(const string& cause) { - this->cause = cause; -} - -AlibException::~AlibException() throw (){ - -} - -const char* AlibException::what() const throw() { - return cause.c_str(); -} - -const std::string& AlibException::getCause() const { - return cause; -} - -} /* namespace alib */ - - diff --git a/alib/src/AlibException.h b/alib/src/AlibException.h deleted file mode 100644 index a22efddc47d6b85abef031cc2dac31ce03bc21ef..0000000000000000000000000000000000000000 --- a/alib/src/AlibException.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * AlibException.h - * - * Created on: Apr 1, 2013 - * Author: martin - */ - -#ifndef ALIBEXCEPTION_H_ -#define ALIBEXCEPTION_H_ - -#include <exception> -#include <string> - -namespace alib { - -using namespace std; - -/** - * Basic exception from which are derived all other exceptions. - * Contains reason why the exception occured. - */ -class AlibException: public exception { -protected: - string cause; -public: - AlibException(); - AlibException(const string& cause); - virtual ~AlibException() throw (); - - /** - * @return reason why the exception occured - */ - const char* what() const throw (); - - /** - * @return reason why the exception occured - */ - const std::string& getCause() const; -}; - -} /* namespace alib */ -#endif /* ALIBEXCEPTION_H_ */ diff --git a/alib/src/AutomatonFactory.cpp b/alib/src/AutomatonFactory.cpp deleted file mode 100644 index 9fe20e2ea0e50396167fc125d7cc1c836f2475ff..0000000000000000000000000000000000000000 --- a/alib/src/AutomatonFactory.cpp +++ /dev/null @@ -1,231 +0,0 @@ -/* - * AutomatonFactory.cpp - * - * Created on: Apr 23, 2013 - * Author: martin - */ - -#include "AutomatonFactory.h" - -#include <algorithm> -#include <iostream> - -#include "sax/Token.h" -#include "sax/SaxInterface.h" -#include "automaton/AutomatonParser.h" -#include "automaton/exception/AutomatonException.h" - -namespace automaton { - -using namespace sax; - -UnknownAutomaton AutomatonFactory::fromFile(const string& filename) { - list<Token> tokens; - SaxInterface::parseFile(filename, tokens); - return parse(tokens); -} - -UnknownAutomaton AutomatonFactory::fromString(const string& str) { - list<Token> tokens; - SaxInterface::parseMemory(str, tokens); - return parse(tokens); -} - -UnknownAutomaton AutomatonFactory::fromStdin() { - return AutomatonFactory::fromStream(cin); -} - -UnknownAutomaton AutomatonFactory::fromStream(std::istream& in) { - string input(istreambuf_iterator<char>(in), (istreambuf_iterator<char>())); - return AutomatonFactory::fromString(input); -} - -UnknownAutomaton AutomatonFactory::parse(list<Token> tokens) { - AutomatonParser parser; - return parser.parse(tokens); -} - -Automaton* AutomatonFactory::buildAutomaton(const UnknownAutomaton& automaton) { - if (isFSM(automaton)) { - return new FSM(buildFSM(automaton)); - } else if (isPDA(automaton)) { - return new PDA(buildPDA(automaton)); - } else if (isTM(automaton)) { - return new TM(buildTM(automaton)); - } else { - throw AutomatonException("Cannot determine automaton type."); - } -} - -void AutomatonFactory::buildCommon(Automaton& automaton,const UnknownAutomaton& unknownAutomaton) { - set<State>::const_iterator state; - set<Symbol>::const_iterator symbol; - - const set<State>& states = unknownAutomaton.getStates(); - state = states.begin(); - while (state != states.end()) { - automaton.addState(*state); - state++; - } - - const set<Symbol>& inputAlphabet = unknownAutomaton.getInputAlphabet(); - symbol = inputAlphabet.begin(); - while (symbol != inputAlphabet.end()) { - automaton.addInputSymbol(*symbol); - symbol++; - } - - const set<State>& initialStates = unknownAutomaton.getInitialStates(); - state = initialStates.begin(); - while (state != initialStates.end()) { - automaton.addInitialState(*state); - state++; - } - - const set<State>& finalStates = unknownAutomaton.getFinalStates(); - state = finalStates.begin(); - while (state != finalStates.end()) { - automaton.addFinalState(*state); - state++; - } -} - -bool AutomatonFactory::isFSM(const UnknownAutomaton& automaton) { - if (automaton.getStackAlphabet().size() > 0 || automaton.getStartSymbols().size() > 0 - || automaton.getTapeAlphabet().size() > 0 || automaton.getBlankSymbol() != Symbol("")) { - return false; - } - - const set<UnknownTransition> transitions = automaton.getTransitions(); - set<UnknownTransition>::const_iterator it = transitions.begin(); - while (it != transitions.end()) { - if (!isFSMTransition(*it)) { - return false; - } - it++; - } - - return true; -} - -bool AutomatonFactory::isFSMTransition(const UnknownTransition& transition) { - return !(transition.getPop().size() > 0 || transition.getPush().size() > 0 || transition.getOutput() != Symbol("") - || transition.getShift() != NOT_SET); -} - -FSM AutomatonFactory::buildFSM(const UnknownAutomaton& automaton) { - FSM fsm; - - buildCommon(fsm, automaton); - - const set<UnknownTransition> transitions = automaton.getTransitions(); - set<UnknownTransition>::const_iterator transition = transitions.begin(); - while (transition != transitions.end()) { - fsm.addTransition(transition->getFrom(), transition->getInput(), transition->getTo()); - transition++; - } - - return fsm; -} - -bool AutomatonFactory::isPDA(const UnknownAutomaton& automaton) { - if (automaton.getTapeAlphabet().size() > 0 || automaton.getBlankSymbol() != Symbol("")) { - return false; - } - - const set<UnknownTransition> transitions = automaton.getTransitions(); - set<UnknownTransition>::const_iterator transition = transitions.begin(); - while (transition != transitions.end()) { - if (!isPDATransition(*transition)) { - return false; - } - transition++; - } - - return true; -} - -bool AutomatonFactory::isPDATransition(const UnknownTransition& transition) { - return !(transition.getOutput() != Symbol("") || transition.getShift() != NOT_SET); -} - -PDA AutomatonFactory::buildPDA(const UnknownAutomaton& automaton) { - PDA pda; - buildCommon(pda, automaton); - - const set<Symbol>& stackAlphabet = automaton.getStackAlphabet(); - set<Symbol>::iterator stackSymbol = stackAlphabet.begin(); - while (stackSymbol != stackAlphabet.end()) { - pda.addStackSymbol(*stackSymbol); - stackSymbol++; - } - - pda.setStartSymbols(automaton.getStartSymbols()); - - const set<UnknownTransition>& transitions = automaton.getTransitions(); - - set<UnknownTransition>::const_iterator transition = transitions.begin(); - while (transition != transitions.end()) { - TransitionPDA tr(transition->getFrom(), transition->getInput(), transition->getTo(), transition->getPop(), - transition->getPush()); - pda.addTransition(tr); - transition++; - } - - return pda; -} - -bool AutomatonFactory::isTM(const UnknownAutomaton& automaton) { - if (automaton.getStackAlphabet().size() > 0 || automaton.getStartSymbols().size() > 0) { - return false; - } - - const set<UnknownTransition>& transitions = automaton.getTransitions(); - - set<UnknownTransition>::const_iterator transition = transitions.begin(); - while (transition != transitions.end()) { - if (!isTMTransition(*transition)) { - return false; - } - transition++; - } - - return true; - -} - -bool AutomatonFactory::isTMTransition(const UnknownTransition& transition) { - return !(transition.getPop().size() > 0 || transition.getPush().size() > 0); - -} - -TM AutomatonFactory::buildTM(const UnknownAutomaton& automaton) { - TM tm; - - const set<Symbol>& tapeAlphabet = automaton.getTapeAlphabet(); - - set<Symbol>::const_iterator tapeSymbol = tapeAlphabet.begin(); - while (tapeSymbol != tapeAlphabet.end()) { - tm.addTapeSymbol(*tapeSymbol); - tapeSymbol++; - } - - buildCommon(tm, automaton); - - tm.setBlankSymbol(automaton.getBlankSymbol()); - - const set<UnknownTransition>& transitions = automaton.getTransitions(); - - set<UnknownTransition>::const_iterator transition = transitions.begin(); - while (transition != transitions.end()) { - TransitionTM tr(transition->getFrom(), transition->getInput(), transition->getTo(), transition->getOutput(), - transition->getShift()); - tm.addTransition(tr); - transition++; - } - - return tm; - -} - -} /* namespace automaton */ diff --git a/alib/src/AutomatonFactory.h b/alib/src/AutomatonFactory.h deleted file mode 100644 index 8a987ca94cac44cde0ecb18b34d3f67fc504d445..0000000000000000000000000000000000000000 --- a/alib/src/AutomatonFactory.h +++ /dev/null @@ -1,151 +0,0 @@ -/* - * AutomatonFactory.h - * - * Created on: Apr 23, 2013 - * Author: martin - */ - -#ifndef AUTOMATONFACTORY_H_ -#define AUTOMATONFACTORY_H_ - -#include <string> -#include "sax/Token.h" -#include "automaton/UnknownAutomaton.h" -#include "automaton/FSM/FSM.h" -#include "automaton/PDA/PDA.h" -#include "automaton/TM/TM.h" - -namespace automaton { - -using namespace std; - -/** - * Builds specific automatons from UnknownAutomaton. - */ -class AutomatonFactory { -public: - /** - * Parses the XML in file and returns UnknownAutomaton. - * @param filename path to the file - * @return UnknownAutomaton - */ - static UnknownAutomaton fromFile(const string& filename); - - /** - * Parses the XML and returns UnknownAutomaton. - * @param str string containing the XML - * @return UnknownAutomaton - */ - static UnknownAutomaton fromString(const string& str); - - /** - * Parses the XML from stdin and returns UnknownAutomaton. - * @return UnknownAutomaton - */ - static UnknownAutomaton fromStdin(); - - /** - * Parses the XML from stream and returns UnknownAutomaton. - * @return UnknownAutomaton - */ - static UnknownAutomaton fromStream(std::istream& in); - - /** - * Tries to create specific automaton from UnknownAutomaton. - * @param automaton source UnknownAutomaton - * @return pointer to new automaton - * @throws AutomatonException when specific automaton cannot - * be created form UnknownAutomaton (e.g. automaton type is not recognized) - */ - static Automaton* buildAutomaton(const UnknownAutomaton& automaton); - - /** - * Checks that FSM can be build from UnknownAutomaton. That means UnknownAutomaton - * doesn't contain elements that are not present in the FSM. - * @param automaton source UnknownAutomaton - * @return true when FSM can be build from UnknownAutomaton, false otherwise - */ - static bool isFSM(const UnknownAutomaton& automaton); - - /** - * Tries to build FSM from the UnknownAutomaton. Ignores elements that - * are not required for the FSM. - * @param automaton source UnknownAutomaton - * @return FSM - */ - static FSM buildFSM(const UnknownAutomaton& automaton); - - /** - * Checks that PDA can be build from UnknownAutomaton. That means UnknownAutomaton - * doesn't contain elements that are not present in the PDA. - * @param automaton source UnknownAutomaton - * @return true when PDA can be build from UnknownAutomaton, false otherwise - */ - static bool isPDA(const UnknownAutomaton& automaton); - - /** - * Tries to build PDA from the UnknownAutomaton. Ignores elements that - * are not required for the PDA. - * @param automaton source UnknownAutomaton - * @return PDA - */ - static PDA buildPDA(const UnknownAutomaton& automaton); - - /** - * Checks that TM can be build from UnknownAutomaton. That means UnknownAutomaton - * doesn't contain elements that are not present in the TM. - * @param automaton source UnknownAutomaton - * @return true when TM can be build from UnknownAutomaton, false otherwise - */ - static bool isTM(const UnknownAutomaton& automaton); - - /** - * Tries to build TM from the UnknownAutomaton. Ignores elements that - * are not required for the TM. - * @param automaton source UnknownAutomaton - * @return TM - */ - static TM buildTM(const UnknownAutomaton& automaton); - -protected: - - /** - * Parses the UnknownAutomaton from the list of tokens. - * @param tokens XML represented as list of tokens - * @return parsed UnknownAutomaton - */ - static UnknownAutomaton parse(list<sax::Token> tokens); - - /** - * Build common part (states, input alphabet, initial states, final states) - * of the automaton from UnknownAutomaton. - * @param automaton automaton to build (destination) - * @param unknownAutomaton source automaton - */ - static void buildCommon(Automaton& automaton, const UnknownAutomaton& unknownAutomaton); - - /** - * Checks that transition contains only elements that are valid for FSM. - * @param transition UnknownTransition to check - * @return true when transition is FSM transition - */ - static bool isFSMTransition(const UnknownTransition& transition); - - /** - * Checks that transition contains only elements that are valid for PDA. - * @param transition UnknownTransition to check - * @return true when transition is PDA transition - */ - static bool isPDATransition(const UnknownTransition& transition); - - /** - * Checks that transition contains only elements that are valid for TM. - * @param transition UnknownTransition to check - * @return true when transition is TM transition - */ - static bool isTMTransition(const UnknownTransition& transition); -}; - -} /* namespace automaton */ -#endif /* AUTOMATONFACTORY_H_ */ - diff --git a/alib/src/GrammarFactory.cpp b/alib/src/GrammarFactory.cpp deleted file mode 100644 index 40cf81b8179702f2083f70cfdd70492e8c5b9973..0000000000000000000000000000000000000000 --- a/alib/src/GrammarFactory.cpp +++ /dev/null @@ -1,122 +0,0 @@ -/* - 0 * GrammarFactory.cpp - * - * Created on: Nov 5, 2013 - * Author: martin - */ - -#include <iostream> - -#include "GrammarFactory.h" - -#include "sax/SaxInterface.h" -#include "grammar/GrammarParser.h" - -namespace grammar { - -UnknownGrammar GrammarFactory::fromFile(const string& filename) { - std::list<Token> tokens; - SaxInterface::parseFile(filename, tokens); - return parse(tokens); -} - -UnknownGrammar GrammarFactory::fromString(const string& str) { - list<Token> tokens; - SaxInterface::parseMemory(str, tokens); - return parse(tokens); -} - -UnknownGrammar GrammarFactory::fromStdin() { - return GrammarFactory::fromStream(cin); -} - -UnknownGrammar GrammarFactory::fromStream(std::istream& in) { - string input(istreambuf_iterator<char>(in), (istreambuf_iterator<char>())); - return GrammarFactory::fromString(input); -} - - -UnknownGrammar GrammarFactory::parse(list<sax::Token> tokens) { - GrammarParser parser; - return parser.parse(tokens); -} - -RightRegularGrammar GrammarFactory::buildRightRegularGrammar(const Grammar& grammar) { - RightRegularGrammar regularGrammar(grammar.getNonTerminalSymbols(), grammar.getTerminalSymbols(), - grammar.getStartSymbol()); - - for (auto rule : grammar.getRules()) { - regularGrammar.addRule(rule); - } - - return regularGrammar; -} - -LeftRegularGrammar GrammarFactory::buildLeftRegularGrammar(const Grammar& grammar) { - LeftRegularGrammar regularGrammar(grammar.getNonTerminalSymbols(), grammar.getTerminalSymbols(), - grammar.getStartSymbol()); - - for (auto rule : grammar.getRules()) { - regularGrammar.addRule(rule); - } - - return regularGrammar; -} - -LeftLinearGrammar GrammarFactory::buildLeftLinearGrammar(const Grammar& grammar) { - LeftLinearGrammar linearGrammar(grammar.getNonTerminalSymbols(), grammar.getTerminalSymbols(), - grammar.getStartSymbol()); - - for (auto rule : grammar.getRules()) { - linearGrammar.addRule(rule); - } - - return linearGrammar; -} - -RightLinearGrammar GrammarFactory::buildRightLinearGrammar(const Grammar& grammar) { - RightLinearGrammar linearGrammar(grammar.getNonTerminalSymbols(), grammar.getTerminalSymbols(), - grammar.getStartSymbol()); - - for (auto rule : grammar.getRules()) { - linearGrammar.addRule(rule); - } - - return linearGrammar; -} - -ContextFreeGrammar GrammarFactory::buildContextFreeGrammar(const Grammar& grammar) { - ContextFreeGrammar contextFreeGrammar(grammar.getNonTerminalSymbols(), grammar.getTerminalSymbols(), - grammar.getStartSymbol()); - - for (auto rule : grammar.getRules()) { - contextFreeGrammar.addRule(rule); - } - - return contextFreeGrammar; -} - -ContextSensitiveGrammar GrammarFactory::buildContextSensitiveGrammar(const Grammar& grammar) { - ContextSensitiveGrammar contextSensitiveGrammar(grammar.getNonTerminalSymbols(), grammar.getTerminalSymbols(), - grammar.getStartSymbol()); - - for (auto rule : grammar.getRules()) { - contextSensitiveGrammar.addRule(rule); - } - - return contextSensitiveGrammar; -} - -UnrestrictedGrammar GrammarFactory::buildUnrestrictedGrammar(const Grammar& grammar) { - UnrestrictedGrammar unrestrictedGrammar(grammar.getNonTerminalSymbols(), grammar.getTerminalSymbols(), - grammar.getStartSymbol()); - - for (auto rule : grammar.getRules()) { - unrestrictedGrammar.addRule(rule); - } - - return unrestrictedGrammar; -} - -} /* namespace grammar */ - diff --git a/alib/src/GrammarFactory.h b/alib/src/GrammarFactory.h deleted file mode 100644 index 440a70c8c6075b25ac37832d91f0c117daa09217..0000000000000000000000000000000000000000 --- a/alib/src/GrammarFactory.h +++ /dev/null @@ -1,122 +0,0 @@ -/* - * GrammarFactory.h - * - * Created on: Nov 5, 2013 - * Author: Martin Zak - */ - -#ifndef GRAMMARFACTORY_H_ -#define GRAMMARFACTORY_H_ - -#include "sax/Token.h" -#include "grammar/UnknownGrammar.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 { - -using namespace std; - -/** - * Builds specific grammars from UnknownGrammar. - */ -class GrammarFactory { -public: - /** - * Parses the XML in file and returns UnknownGrammar. - * @param filename path to the file - * @return UnknownGrammar - */ - static UnknownGrammar fromFile(const string& filename); - - /** - * Parses the XML and returns UnknownGrammar. - * @param str string containing the XML - * @return UnknownGrammar - */ - static UnknownGrammar fromString(const string& str); - - /** - * Parses the XML from stdin and returns UnknownGrammar. - * @return UnknownGrammar - */ - static UnknownGrammar fromStdin(); - - /** - * Parses the XML from stream and returns the UnknownGrammar. - * @return RegExp - */ - static UnknownGrammar fromStream(std::istream& in); - - /** - * Tries to build RightRegularGrammar from any grammar. - * @param grammar source grammar - * @return grammar created from source grammar - * @throws GrammarException when RightRegularGrammar cannot be build from source grammar - */ - static RightRegularGrammar buildRightRegularGrammar(const Grammar& grammar); - - /** - * Tries to build LeftRegularGrammar from any grammar. - * @param grammar source grammar - * @return grammar created from source grammar - * @throws GrammarException when LeftRegularGrammar cannot be build from source grammar - */ - static LeftRegularGrammar buildLeftRegularGrammar(const Grammar& grammar); - - /** - * Tries to build LeftLinearGrammar from any grammar. - * @param grammar source grammar - * @return grammar created from source grammar - * @throws GrammarException when LeftLinearGrammar cannot be build from source grammar - */ - static LeftLinearGrammar buildLeftLinearGrammar(const Grammar& grammar); - - /** - * Tries to build RightLinearGrammar from any grammar. - * @param grammar source grammar - * @return grammar created from source grammar - * @throws GrammarException when RightLinearGrammar cannot be build from source grammar - */ - static RightLinearGrammar buildRightLinearGrammar(const Grammar& grammar); - - /** - * Tries to build ContextFreeGrammar from any grammar. - * @param grammar source grammar - * @return grammar created from source grammar - * @throws GrammarException when ContextFreeGrammar cannot be build from source grammar - */ - static ContextFreeGrammar buildContextFreeGrammar(const Grammar& grammar); - - /** - * Tries to build ContextSensitiveGrammar from any grammar. - * @param grammar source grammar - * @return grammar created from source grammar - * @throws GrammarException when ContextSensitiveGrammar cannot be build from source grammar - */ - static ContextSensitiveGrammar buildContextSensitiveGrammar(const Grammar& grammar); - - /** - * Tries to build UnrestrictedGrammar from any grammar. - * @param grammar source grammar - * @return grammar created from source grammar - * @throws GrammarException when UnrestrictedGrammar cannot be build from source grammar - */ - static UnrestrictedGrammar buildUnrestrictedGrammar(const Grammar& grammar); - -protected: - /** - * Parses the UnknownGrammar from list of tokens. - * @param tokens XML represented as list of tokens - * @return parsed UnknownGrammar - */ - static UnknownGrammar parse(list<sax::Token> tokens); -}; - -} /* namespace grammar */ -#endif /* GRAMMARFACTORY_H_ */ diff --git a/alib/src/RegExpFactory.cpp b/alib/src/RegExpFactory.cpp deleted file mode 100644 index e8d54eaf5f8ce8abbb7976f7639d93e3e55d14fe..0000000000000000000000000000000000000000 --- a/alib/src/RegExpFactory.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/* - * RegExpFactory.cpp - * - * Created on: Jan 1, 2014 - * Author: martin - */ - -#include <iostream> - -#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); - return parse(tokens); -} - -RegExp RegExpFactory::fromString(const string& str) { - list<Token> tokens; - SaxInterface::parseMemory(str, tokens); - return parse(tokens); -} - -RegExp RegExpFactory::fromStdin() { - return RegExpFactory::fromStream(cin); -} - -RegExp RegExpFactory::fromStream(std::istream& in) { - string input(istreambuf_iterator<char>(in), (istreambuf_iterator<char>())); - return RegExpFactory::fromString(input); -} - -RegExp RegExpFactory::parse(list<sax::Token> tokens) { - RegExpParser parser; - return parser.parse(tokens); -} - -} /* namespace regexp */ diff --git a/alib/src/RegExpFactory.h b/alib/src/RegExpFactory.h deleted file mode 100644 index 510ddddc1d867800f3824fc8c9cf52ae5a28ef34..0000000000000000000000000000000000000000 --- a/alib/src/RegExpFactory.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * RegExpFactory.h - * - * Created on: Jan 1, 2014 - * Author: martin - */ - -#ifndef REGEXPFACTORY_H_ -#define REGEXPFACTORY_H_ - -#include "sax/Token.h" -#include "regexp/RegExp.h" - -namespace regexp { - -using namespace std; -using namespace sax; - -/** - * 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); - - /** - * Parses the XML from stdin and returns the RegExp. - * @return RegExp - */ - static RegExp fromStdin(); - - /** - * Parses the XML from stream and returns the RegExp. - * @return RegExp - */ - static RegExp fromStream(std::istream& in); - -protected: - /** - * Parses the RegExp from list of tokens. - * @param tokens XML represented as list of tokens - * @return parsed RegExp - */ - static RegExp parse(list<Token> tokens); -}; - -} /* namespace regexp */ -#endif /* REGEXPFACTORY_H_ */ diff --git a/alib/src/StringFactory.cpp b/alib/src/StringFactory.cpp deleted file mode 100644 index f3845f0e417b53458e5468dee4fbab2c0387ddf8..0000000000000000000000000000000000000000 --- a/alib/src/StringFactory.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/* - * StringFactory.cpp - * - * Created on: Jan 1, 2014 - * Author: Jan Travnicek - */ - -#include <iostream> - -#include "sax/SaxInterface.h" -#include "StringFactory.h" -#include "string/StringParser.h" - -namespace notions { -namespace string { - -String StringFactory::fromFile(const std::string& filename) { - std::list<sax::Token> tokens; - sax::SaxInterface::parseFile(filename, tokens); - return parse(tokens); -} - -String StringFactory::fromString(const std::string& str) { - std::list<sax::Token> tokens; - sax::SaxInterface::parseMemory(str, tokens); - return parse(tokens); -} - -String StringFactory::fromStdin() { - return StringFactory::fromStream(std::cin); -} - -String StringFactory::fromStream(std::istream& in) { - std::string input(std::istreambuf_iterator<char>(in), (std::istreambuf_iterator<char>())); - return StringFactory::fromString(input); -} - -String StringFactory::parse(std::list<sax::Token> tokens) { - StringParser parser; - return parser.parse(tokens); -} - -} /* namespace string */ -} /* namespace notions */ diff --git a/alib/src/StringFactory.h b/alib/src/StringFactory.h deleted file mode 100644 index 610c2a6e5984990db89613e97440f20d36b7e412..0000000000000000000000000000000000000000 --- a/alib/src/StringFactory.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * StringFactory.h - * - * Created on: Jan 1, 2014 - * Author: Jan Travnicek - */ - -#ifndef STRING_FACTORY_H_ -#define STRING_FACTORY_H_ - -#include <list> - -#include "sax/Token.h" -#include "string/String.h" - -namespace notions { -namespace string { - -/** - * String builder. - */ -class StringFactory { -public: - /** - * Parses the XML in file and returns the String. - * @param filename path to the file - * @return String - */ - static String fromFile(const std::string& filename); - - /** - * Parses the XML and returns the String. - * @param str string containing the XML - * @return String - */ - static String fromString(const std::string& str); - - /** - * Parses the XML from stdin and returns the String. - * @return String - */ - static String fromStdin(); - - /** - * Parses the XML from stream and returns the String. - * @return String - */ - static String fromStream(std::istream& in); - -protected: - /** - * Parses the String from list of tokens. - * @param tokens XML represented as list of tokens - * @return parsed String - */ - static String parse(std::list<sax::Token> tokens); -}; - -} /* namespace string */ -} /* namespace notions */ - -#endif /* STRING_FACTORY_H_ */ diff --git a/alib/src/alphabet/Epsilon.cpp b/alib/src/alphabet/Epsilon.cpp deleted file mode 100644 index 22f884f5979f7bee164ca7fab4039c40c0dc774e..0000000000000000000000000000000000000000 --- a/alib/src/alphabet/Epsilon.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Epsilon.cpp - * - * Created on: Jan 30, 2014 - * Author: honza - */ - -#include "Epsilon.h" - -namespace alphabet { - -Epsilon::Epsilon() { - -} - -bool Epsilon::operator <(const Epsilon& other) const { - return false; -} - -bool Epsilon::operator ==(const Epsilon& other) const { - return true; -} - -bool Epsilon::operator !=(const Epsilon& other) const { - return false; -} - -std::ostream& operator<<(std::ostream& out, const Epsilon& symbol) { - - out << "Epsilon"; - return out; -} - -} /* namespace language */ diff --git a/alib/src/alphabet/Epsilon.h b/alib/src/alphabet/Epsilon.h deleted file mode 100644 index d36616d7acec9bb3127dc389edbc3cb2cad8e3e4..0000000000000000000000000000000000000000 --- a/alib/src/alphabet/Epsilon.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Epsilon.h - * - * Created on: Jan 30, 2014 - * Author: honza - */ - -#ifndef EPSILON_H_ -#define EPSILON_H_ - -#include <ostream> - -namespace alphabet { - -/** - * Represents empty word. - */ -class Epsilon { -public: - /** - * Creates new empty string. - */ - Epsilon(); - - bool operator <(const Epsilon& other) const; - bool operator ==(const Epsilon& other) const; - bool operator !=(const Epsilon& other) const; - - friend std::ostream& operator<<(std::ostream&, const Epsilon&); -}; -} -#endif /* EPSILON_H_ */ diff --git a/alib/src/alphabet/Symbol.cpp b/alib/src/alphabet/Symbol.cpp deleted file mode 100644 index b16010e82870a00571823f18cc478b699397e20b..0000000000000000000000000000000000000000 --- a/alib/src/alphabet/Symbol.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Symbol.cpp - * - * Created on: Mar 26, 2013 - * Author: Martin Zak - */ - -#include "Symbol.h" - -namespace alphabet { - -Symbol::Symbol(const std::string& symbol) { - this->symbol = symbol; -} - -const std::string& Symbol::getSymbol() const { - return symbol; -} - -bool Symbol::operator <(const Symbol& other) const { - return symbol < other.symbol; -} - -bool Symbol::operator ==(const Symbol& other) const { - return symbol == other.symbol; -} - -bool Symbol::operator >(const Symbol& other) const { - return symbol > other.symbol; -} - -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 deleted file mode 100644 index 9b82e08a355cdef5843cdcedb027776e28b0a95c..0000000000000000000000000000000000000000 --- a/alib/src/alphabet/Symbol.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Symbol.h - * - * Created on: Mar 26, 2013 - * Author: Martin Zak - */ - -#ifndef SYMBOL_H_ -#define SYMBOL_H_ - -#include <string> -#include <ostream> - -namespace alphabet { - -/** - * Represents symbol in an alphabet. - */ -class Symbol { -protected: - std::string symbol; -public: - /** - * Creates new symbol with given name. - * @param symbol name of the symbol - */ - Symbol(const std::string& symbol); - - /** - * @return name of the symbol - */ - const std::string& getSymbol() const; - - bool operator <(const Symbol& other) const; - 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 deleted file mode 100644 index dcf865a23651aa6f7dcf59481b098db1d412b915..0000000000000000000000000000000000000000 --- a/alib/src/automaton/Automaton.cpp +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Automaton.cpp - * - * Created on: Apr 16, 2013 - * Author: martin - */ - -#include "Automaton.h" - -#include <set> -#include "exception/AutomatonException.h" - -namespace automaton { - -Automaton::~Automaton() { - -} - -void Automaton::addState(const State& state) { - pair<set<State>::iterator, bool> ret = states.insert(state); - if (!ret.second) - throw AutomatonException("State \"" + state.getName() + "\" already exists."); -} - -const set<State>& Automaton::getStates() const { - return states; -} - -void Automaton::addInputSymbol(const Symbol& symbol) { - pair<set<Symbol>::iterator, bool> ret = inputAlphabet.insert(symbol); - if (!ret.second) - throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" already exists."); -} - -const set<Symbol>& Automaton::getInputAlphabet() const { - return inputAlphabet; -} - -void Automaton::addInitialState(const State& state) { - pair<set<State>::iterator, bool> ret = initialStates.insert(state); - if (!ret.second) - throw AutomatonException("State \"" + state.getName() + "\" is already initial state."); -} - -void Automaton::removeInitialState(const State& state) { - if (states.find(state) == states.end()) { - throw AutomatonException("State cannot be set as initial state. It is not present in the automaton."); - } - - int removed = initialStates.erase(state); - if (!removed) - throw AutomatonException("State \"" + state.getName() + "\" isn't initial state."); - -} - -const set<State>& Automaton::getInitialStates() const { - return initialStates; -} - -void Automaton::addFinalState(const State& state) { - if (states.find(state) == states.end()) { - throw AutomatonException("State cannot be set as final state. It is not present in the automaton."); - } - - pair<set<State>::iterator, bool> ret = finalStates.insert(state); - if (!ret.second) - throw AutomatonException("State \"" + state.getName() + "\" is already final state."); -} - -void Automaton::removeFinalState(const State& state) { - int removed = finalStates.erase(state); - if (!removed) - throw AutomatonException("State \"" + state.getName() + "\" isn't final state."); -} - -const set<State>& Automaton::getFinalStates() const { - return finalStates; -} - -const State& Automaton::createUniqueState(const string& name, bool integerSuffix){ - try{ - State uniqueState(name); - addState(uniqueState); - return * states.find(uniqueState); - } - catch(AutomatonException& e){ - } - - int i = 0; - string str = name; - - while(i < INT_MAX) - { - if(integerSuffix) - str = name + std::to_string(i++); - else - str += '\''; - - State uniqueState(str); - try{ - addState(uniqueState); - } - catch(AutomatonException& e){ - continue; - } - - return * states.find(uniqueState); - } - - throw AutomatonException("Could not create unique state with name " + name + "." ); -} - -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 deleted file mode 100644 index b7a41b9ce0211b972be1d74333c08482f50dc931..0000000000000000000000000000000000000000 --- a/alib/src/automaton/Automaton.h +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Automaton.h - * - * Created on: Apr 10, 2013 - * Author: martin - */ - -#ifndef AUTOMATON_H_ -#define AUTOMATON_H_ - -#include <climits> -#include <ostream> -#include <set> -#include <string> -#include "State.h" -#include "../alphabet/Symbol.h" - - -namespace automaton { - -using namespace std; -using namespace alphabet; - -/** - * Abstract base class for all automatons. Contains common elements of automatons. - */ -class Automaton { -protected: - set<State> states; - set<Symbol> inputAlphabet; - set<State> initialStates; - set<State> finalStates; -public: - virtual ~Automaton(); - - /** - * Adds new state to the automaton. - * @param state State to add - * @throws AutomatonException when state already exist in the automaton - */ - void addState(const State& state); - - /** - * Removes the state from the automaton. - * @param state State to remove - * @throws AutomatonException when state is not part of the automaton, - * is set as initial or final or when it's used in a transition - */ - virtual void removeState(const State& state) = 0; - - /** - * @return list of states - */ - const set<State>& getStates() const; - - /** - * Adds input symbol to input alphabet. - * @param symbol Symbol to add - * @throws AutomatonException when symbol already exists - */ - virtual void addInputSymbol(const Symbol& symbol); - - /** - * Removes input symbol from the input alphabet. - * @param symbol Symbol to remove - * @throws AutomatonException when symbol is not present in input alphabet - * or when symbol is part of the transition - */ - virtual void removeInputSymbol(const Symbol& symbol) = 0; - - /** - * @return the input alphabet - */ - const set<Symbol>& getInputAlphabet() const; - - /** - * Adds the State to the initial states. - * @param state State to add - * @throws AutomatonException when state is not present - * in the automaton or when it's already initial state - */ - void addInitialState(const State& state); - - /** - * Removes the state from the initial states. - * @param state State to remove - * @throws AutomatonException when State is not initial state - */ - virtual void removeInitialState(const State& state); - - /** - * @return initial states - */ - const set<State>& getInitialStates() const; - - /** - * Adds the State to the final states. - * @param state State to add - * @throws AutomatonException when state is not present - * in the automaton or when it's already final state - */ - void addFinalState(const State& state); - - /** - * Removes the state from the final states. - * @param state State to remove - * @throws AutomatonException when State is not final state - */ - virtual void removeFinalState(const State& state); - - /** - * @return final states - */ - const set<State>& getFinalStates() const; - - /** - * Creates and adds unique state to automaton. If given state name is - * already used, appends apostrophe or integer suffix - * @param name name of the state - * @throws AutomatonException if state could not be created - * @return created state - */ - const State& createUniqueState(const std::string& name, bool integerSuffix = false); - - /** - * 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 */ -#endif /* AUTOMATON_H_ */ diff --git a/alib/src/automaton/AutomatonParser.cpp b/alib/src/automaton/AutomatonParser.cpp deleted file mode 100644 index 425621665ad09a93d2a6c3caf967f96b1c74cac8..0000000000000000000000000000000000000000 --- a/alib/src/automaton/AutomatonParser.cpp +++ /dev/null @@ -1,244 +0,0 @@ -/* - * AutomatonParser.cpp - * - * Created on: Oct 12, 2013 - * Author: Martin Zak - */ - -#include "AutomatonParser.h" - -#include "../sax/ParserException.h" -#include "Shift.h" - -namespace automaton { - -using namespace std; -using namespace sax; - -UnknownAutomaton AutomatonParser::parse(list<Token> &input) { - UnknownAutomaton automaton; - - popToken(input, Token::START_ELEMENT, "automaton"); - - while (true) { - if (isToken(input, Token::END_ELEMENT, "automaton")) { - break; - } else if (isToken(input, Token::START_ELEMENT, "states")) { - parseStates(input, automaton); - } else if (isToken(input, Token::START_ELEMENT, "inputAlphabet")) { - parseInputAlphabet(input, automaton); - } else if (isToken(input, Token::START_ELEMENT, "initialStates")) { - parseInitialStates(input, automaton); - } else if (isToken(input, Token::START_ELEMENT, "finalStates")) { - parseFinalStates(input, automaton); - } else if (isToken(input, Token::START_ELEMENT, "stackAlphabet")) { - parseStackAlphabet(input, automaton); - } else if (isToken(input, Token::START_ELEMENT, "startSymbols")) { - parseStartSymbols(input, automaton); - } else if (isToken(input, Token::START_ELEMENT, "tapeAlphabet")) { - parseTapeAlphabet(input, automaton); - } else if (isToken(input, Token::START_ELEMENT, "blankSymbol")) { - parseBlankSymbol(input, automaton); - } else if (isToken(input, Token::START_ELEMENT, "transitions")) { - parseTransitions(input, automaton); - - } else { - throw ParserException(Token("automaton", Token::END_ELEMENT), input.front()); - } - } - - popToken(input, Token::END_ELEMENT, "automaton"); - return automaton; -} - -void AutomatonParser::parseStates(list<Token> &input, UnknownAutomaton& automaton) { - popToken(input, Token::START_ELEMENT, "states"); - while (isToken(input, Token::START_ELEMENT, "state")) { - automaton.addState(parseState(input)); - } - popToken(input, Token::END_ELEMENT, "states"); -} - -void AutomatonParser::parseInputAlphabet(list<Token> &input, UnknownAutomaton& automaton) { - popToken(input, Token::START_ELEMENT, "inputAlphabet"); - while (isToken(input, Token::START_ELEMENT, "symbol")) { - automaton.addInputSymbol(parseSymbol(input)); - } - popToken(input, Token::END_ELEMENT, "inputAlphabet"); -} - -void AutomatonParser::parseInitialStates(list<Token> &input, UnknownAutomaton& automaton) { - popToken(input, Token::START_ELEMENT, "initialStates"); - while (isToken(input, Token::START_ELEMENT, "state")) { - automaton.addInitialState(parseState(input)); - } - popToken(input, Token::END_ELEMENT, "initialStates"); -} - -void AutomatonParser::parseFinalStates(list<Token> &input, UnknownAutomaton& automaton) { - popToken(input, Token::START_ELEMENT, "finalStates"); - while (isToken(input, Token::START_ELEMENT, "state")) { - automaton.addFinalState(parseState(input)); - } - popToken(input, Token::END_ELEMENT, "finalStates"); -} - -void AutomatonParser::parseStackAlphabet(list<Token> &input, UnknownAutomaton& automaton) { - popToken(input, Token::START_ELEMENT, "stackAlphabet"); - while (isToken(input, Token::START_ELEMENT, "symbol")) { - automaton.addStackSymbol(parseSymbol(input)); - } - popToken(input, Token::END_ELEMENT, "stackAlphabet"); -} - -void AutomatonParser::parseStartSymbols(list<Token> &input, UnknownAutomaton& automaton) { - popToken(input, Token::START_ELEMENT, "startSymbols"); - list<Symbol> startSymbols; - while (isToken(input, Token::START_ELEMENT, "symbol")) { - startSymbols.push_back(parseSymbol(input)); - } - automaton.setStartSymbols(startSymbols); - popToken(input, Token::END_ELEMENT, "startSymbols"); -} - -void AutomatonParser::parseTapeAlphabet(list<Token>& input, UnknownAutomaton& automaton) { - popToken(input, Token::START_ELEMENT, "tapeAlphabet"); - while (isToken(input, Token::START_ELEMENT, "symbol")) { - automaton.addTapeSymbol(parseSymbol(input)); - } - popToken(input, Token::END_ELEMENT, "tapeAlphabet"); -} - -void AutomatonParser::parseBlankSymbol(list<Token>& input, UnknownAutomaton& automaton) { - popToken(input, Token::START_ELEMENT, "blankSymbol"); - if (input.front().getType() == Token::CHARACTER) { - automaton.setBlankSymbol(Symbol(input.front().getData())); - input.pop_front(); - } else { - throw ParserException(Token("", Token::CHARACTER), input.front()); - } - popToken(input, Token::END_ELEMENT, "blankSymbol"); -} - -void AutomatonParser::parseTransitions(list<Token> &input, UnknownAutomaton& automaton) { - popToken(input, Token::START_ELEMENT, "transitions"); - while (isToken(input, Token::START_ELEMENT, "transition")) { - automaton.addTransition(parseTransition(input)); - } - popToken(input, Token::END_ELEMENT, "transitions"); -} - -void AutomatonParser::parsePop(list<Token>& input, UnknownTransition* transition) { - popToken(input, Token::START_ELEMENT, "pop"); - while (isToken(input, Token::START_ELEMENT, "symbol")) { - transition->addPop(parseSymbol(input)); - } - popToken(input, Token::END_ELEMENT, "pop"); -} - -void AutomatonParser::parsePush(list<Token>& input, UnknownTransition* transition) { - popToken(input, Token::START_ELEMENT, "push"); - while (isToken(input, Token::START_ELEMENT, "symbol")) { - transition->addPush(parseSymbol(input)); - } - popToken(input, Token::END_ELEMENT, "push"); -} - -State AutomatonParser::parseState(list<Token> &input, string tagName) { - popToken(input, Token::START_ELEMENT, tagName); - - if (input.front().getType() == Token::CHARACTER) { - State state(input.front().getData()); - input.pop_front(); - popToken(input, Token::END_ELEMENT, tagName); - return state; - } else { - throw ParserException(Token("", Token::CHARACTER), input.front()); - } -} - -Symbol AutomatonParser::parseSymbol(list<Token>& input, string tagName) { - popToken(input, Token::START_ELEMENT, tagName); - - if (input.front().getType() == Token::CHARACTER) { - Symbol symbol(input.front().getData()); - input.pop_front(); - popToken(input, Token::END_ELEMENT, tagName); - return symbol; - } else if(isToken(input,Token::END_ELEMENT, tagName)){ - input.pop_front(); - return Symbol(""); - } else if(isToken(input,Token::START_ELEMENT, "eps")) { - input.pop_front(); - popToken(input, Token::END_ELEMENT,"eps"); - popToken(input, Token::END_ELEMENT, tagName); - return Symbol(""); - } else { - throw ParserException(Token("", Token::CHARACTER), input.front()); - } -} - -Shift AutomatonParser::parseShift(list<Token>& input) { - Shift shift; - - popToken(input, Token::START_ELEMENT, "shift"); - if (input.front().getType() == Token::CHARACTER) { - if (input.front().getData() == "left") - shift = LEFT; - if (input.front().getData() == "right") - shift = RIGHT; - if (input.front().getData() == "none") - shift = NONE; - input.pop_front(); - } else { - throw sax::ParserException(sax::Token("", sax::Token::CHARACTER), input.front()); - } - popToken(input, Token::END_ELEMENT, "shift"); - - return shift; - -} - -UnknownTransition AutomatonParser::parseTransition(list<Token>& input) { - UnknownTransition transition; - - popToken(input, Token::START_ELEMENT, "transition"); - while (true) { - if (isToken(input, Token::END_ELEMENT, "transition")) { - break; - } else if (isToken(input, Token::START_ELEMENT, "from")) { - transition.setFrom(parseState(input, "from")); - } else if (isToken(input, Token::START_ELEMENT, "input")) { - transition.setInput(parseSymbol(input, "input")); - } else if (isToken(input, Token::START_ELEMENT, "to")) { - transition.setTo(parseState(input, "to")); - } else if (isToken(input, Token::START_ELEMENT, "pop")) { - parsePop(input, &transition); - } else if (isToken(input, Token::START_ELEMENT, "push")) { - parsePush(input, &transition); - } else if (isToken(input, Token::START_ELEMENT, "output")) { - transition.setOutput(parseSymbol(input, "output")); - } else if (isToken(input, Token::START_ELEMENT, "shift")) { - transition.setShift(parseShift(input)); - } else { - throw ParserException(Token("transitions", Token::END_ELEMENT), input.front()); - } - } - popToken(input, Token::END_ELEMENT, "transition"); - - return transition; -} - -bool AutomatonParser::isToken(list<Token>& input, Token::TokenType type, string data) { - return input.front().getType() == type && input.front().getData() == data; -} - -void AutomatonParser::popToken(list<Token>& input, Token::TokenType type, string data) { - if (isToken(input, type, data)) { - input.pop_front(); - } else { - throw ParserException(Token(data, type), input.front()); - } -} - -} /* namespace automaton */ diff --git a/alib/src/automaton/AutomatonParser.h b/alib/src/automaton/AutomatonParser.h deleted file mode 100644 index e0ba16477108371befe7a22a584019e82c172727..0000000000000000000000000000000000000000 --- a/alib/src/automaton/AutomatonParser.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * AutomatonParser.h - * - * Created on: Oct 12, 2013 - * Author: martin - */ - -#ifndef AUTOMATONPARSER_H_ -#define AUTOMATONPARSER_H_ - -#include "UnknownAutomaton.h" - -#include <list> -#include <set> -#include "../sax/Token.h" - -namespace automaton { - -using namespace std; -using namespace sax; - -/** - * Parser used to get UnknownAutomaton from XML parsed into list of Tokens. - */ -class AutomatonParser { -protected: - static void parseStates(list<Token> &input, UnknownAutomaton& automaton); - static void parseInputAlphabet(list<Token> &input, UnknownAutomaton& automaton); - static void parseInitialStates(list<Token> &input, UnknownAutomaton& automaton); - static void parseFinalStates(list<Token> &input, UnknownAutomaton& automaton); - static void parseStackAlphabet(list<Token> &input, UnknownAutomaton& automaton); - static void parseStartSymbols(list<Token> &input, UnknownAutomaton& automaton); - static void parseTapeAlphabet(list<Token> &input, UnknownAutomaton& automaton); - static void parseBlankSymbol(list<Token> &input, UnknownAutomaton& automaton); - - static void parseTransitions(list<Token> &input, UnknownAutomaton& automaton); - static UnknownTransition parseTransition(list<Token>& input); - static void parsePop(list<Token>& input, UnknownTransition* transition); - static void parsePush(list<Token>& input, UnknownTransition* transition); - - static State parseState(list<Token> &input, string tagName = "state"); - static Symbol parseSymbol(list<Token> &input, string tagName = "symbol"); - static Shift parseShift(list<Token> &input); - - static bool isToken(list<Token> &input, Token::TokenType type, string data); - static void popToken(list<Token> &input, Token::TokenType type, string data); - -public: - /** - * Parses the xml and returns the automaton. The input is destroyed in the process. - * @param input XML represented as list of Tokens - * @return UnknownAutomaton - * @throws ParserException when an error occurs - */ - static UnknownAutomaton parse(list<Token> &input); -}; - -} /* namespace automaton */ -#endif /* AUTOMATONPARSER_H_ */ diff --git a/alib/src/automaton/AutomatonPrinter.cpp b/alib/src/automaton/AutomatonPrinter.cpp deleted file mode 100644 index 6515cffdbaa51df852c9103e2ea5e71f7c70bc5f..0000000000000000000000000000000000000000 --- a/alib/src/automaton/AutomatonPrinter.cpp +++ /dev/null @@ -1,207 +0,0 @@ -/* - * AutomatonPrinter.cpp - * - * Created on: Nov 11, 2013 - * Author: martin - */ - -#include "AutomatonPrinter.h" - -namespace automaton { - -const string AutomatonPrinter::INDENTATION = "\t"; - -void AutomatonPrinter::printStates(const set<State>& states, ostream& out, string tagName, string prefix) { - out << prefix << "<" << tagName << ">\n"; - for (auto state : states) { - printState(state, out, "state", prefix + INDENTATION); - } - out << prefix << "</" << tagName << ">\n"; -} - -void AutomatonPrinter::printAlphabet(const set<Symbol>& alphabet, ostream& out, string tagName, string prefix) { - out << prefix << "<" << tagName << ">\n"; - for (auto symbol : alphabet) { - printSymbol(symbol, out, "symbol", prefix + INDENTATION); - } - out << prefix << "</" << tagName << ">\n"; -} - -void AutomatonPrinter::printUnknownTransitions(const set<UnknownTransition>& transitions, ostream& out, string prefix) { - string contentPrefix = prefix + INDENTATION + INDENTATION; - - out << prefix << "<transitions>\n"; - for (auto transition : transitions) { - out << prefix << INDENTATION << "<transition>\n"; - printState(transition.getFrom(), out, "from", contentPrefix); - printSymbol(transition.getInput(), out, "input", contentPrefix); - printState(transition.getTo(), out, "to", contentPrefix); - - if (transition.getPop().size() > 0) { - printSymbolList(transition.getPop(), out, "pop", contentPrefix); - } - if (transition.getPush().size() > 0) { - printSymbolList(transition.getPush(), out, "push", contentPrefix); - } - - if (transition.getOutput().getSymbol() != "") { - printSymbol(transition.getOutput(), out, "output", contentPrefix); - } - - if (transition.getShift() != Shift::NOT_SET) { - printShift(transition.getShift(), out, "shift", contentPrefix); - } - - out << prefix << INDENTATION << "</transition>\n"; - } - - out << prefix << "</transitions>\n"; -} - -void AutomatonPrinter::printTransitionsFSM(const set<TransitionFSM>& transitions, ostream& out, string prefix) { - string contentPrefix = prefix + INDENTATION + INDENTATION; - - out << prefix << "<transitions>\n"; - for (auto transition : transitions) { - out << prefix << INDENTATION << "<transition>\n"; - printState(transition.getFrom(), out, "from", contentPrefix); - printSymbol(transition.getInput(), out, "input", contentPrefix); - printState(transition.getTo(), out, "to", contentPrefix); - out << prefix << INDENTATION << "</transition>\n"; - } - out << prefix << "</transitions>\n"; -} - -void AutomatonPrinter::printTransitionsPDA(const set<TransitionPDA>& transitions, ostream& out, string prefix) { - string contentPrefix = prefix + INDENTATION + INDENTATION; - - out << prefix << "<transitions>\n"; - for (auto transition : transitions) { - out << prefix << INDENTATION << "<transition>\n"; - printState(transition.getFrom(), out, "from", contentPrefix); - printSymbol(transition.getInput(), out, "input", contentPrefix); - printState(transition.getTo(), out, "to", contentPrefix); - printSymbolList(transition.getPop(), out, "pop", contentPrefix); - printSymbolList(transition.getPush(), out, "push", contentPrefix); - out << prefix << INDENTATION << "</transition>\n"; - } - out << prefix << "</transitions>\n"; -} - -void AutomatonPrinter::printTransitionsTM(const set<TransitionTM>& transitions, ostream& out, string prefix) { - string contentPrefix = prefix + INDENTATION + INDENTATION; - - out << prefix << "<transitions>\n"; - for (auto transition : transitions) { - out << prefix << INDENTATION << "<transition>\n"; - printState(transition.getFrom(), out, "from", contentPrefix); - printSymbol(transition.getInput(), out, "input", contentPrefix); - printState(transition.getTo(), out, "to", contentPrefix); - printSymbol(transition.getOutput(), out, "output", contentPrefix); - printShift(transition.getShift(), out, "shift", contentPrefix); - out << prefix << INDENTATION << "</transition>\n"; - } - out << prefix << "</transitions>\n"; -} - -void AutomatonPrinter::printState(const State& state, ostream& out, string tagName, string prefix) { - out << prefix << "<" << tagName << ">"; - out << state.getName(); - out << "</" << tagName << ">\n"; -} - -void AutomatonPrinter::printSymbol(const Symbol& symbol, ostream& out, string tagName, string prefix) { - out << prefix << "<" << tagName << ">"; - out << symbol.getSymbol(); - out << "</" << tagName << ">\n"; -} - -void AutomatonPrinter::printSymbolList(const list<Symbol>& symbols, ostream& out, string tagName, string prefix) { - out << prefix << "<" << tagName << ">\n"; - for (auto symbol : symbols) { - printSymbol(symbol, out, "symbol", prefix + INDENTATION); - } - out << prefix << "</" << tagName << ">\n"; -} - -void AutomatonPrinter::printShift(const Shift& shift, ostream& out, string tagName, string prefix) { - out << prefix << "<" << tagName << ">"; - out << (std::string[] ) { "left", "right", "none" } [shift]; - out << "</" << tagName << ">\n"; -} - -void AutomatonPrinter::toXML(const UnknownAutomaton& automaton, ostream& out) { - out << "<automaton>\n"; - - if (automaton.getStates().size() > 0) { - printStates(automaton.getStates(), out, "states", INDENTATION); - } - - if (automaton.getInputAlphabet().size() > 0) { - printAlphabet(automaton.getInputAlphabet(), out, "inputAlphabet", INDENTATION); - } - if (automaton.getTapeAlphabet().size() > 0) { - printAlphabet(automaton.getTapeAlphabet(), out, "tapeAlphabet", INDENTATION); - } - if (automaton.getStackAlphabet().size() > 0) { - printAlphabet(automaton.getStackAlphabet(), out, "stackAlphabet", INDENTATION); - } - - if (automaton.getTransitions().size() > 0) { - printUnknownTransitions(automaton.getTransitions(), out, INDENTATION); - } - - if (automaton.getBlankSymbol().getSymbol() != "") { - printSymbol(automaton.getBlankSymbol(), out, "blankSymbol", INDENTATION); - } - if (automaton.getStartSymbols().size() > 0) { - printSymbolList(automaton.getStartSymbols(), out, "startSymbols", INDENTATION); - } - - if (automaton.getInitialStates().size() > 0) { - printStates(automaton.getInitialStates(), out, "initialStates", INDENTATION); - } - if (automaton.getFinalStates().size() > 0) { - printStates(automaton.getFinalStates(), out, "finalStates", INDENTATION); - } - - out << "</automaton>\n"; - -} - -void AutomatonPrinter::toXML(const FSM& automaton, ostream& out) { - out << "<automaton>\n"; - printStates(automaton.getStates(), out, "states", INDENTATION); - printAlphabet(automaton.getInputAlphabet(), out, "inputAlphabet", INDENTATION); - printTransitionsFSM(automaton.getTransitions(), out, INDENTATION); - printStates(automaton.getInitialStates(), out, "initialStates", INDENTATION); - printStates(automaton.getFinalStates(), out, "finalStates", INDENTATION); - out << "</automaton>\n"; -} - -void AutomatonPrinter::toXML(const PDA& automaton, ostream& out) { - out << "<automaton>\n"; - printStates(automaton.getStates(), out, "states", INDENTATION); - printAlphabet(automaton.getInputAlphabet(), out, "inputAlphabet", INDENTATION); - printAlphabet(automaton.getStackAlphabet(), out, "stackAlphabet", INDENTATION); - printTransitionsPDA(automaton.getTransitions(), out, INDENTATION); - printSymbolList(automaton.getStartSymbols(), out, "startSymbols", INDENTATION); - printStates(automaton.getInitialStates(), out, "initialStates", INDENTATION); - printStates(automaton.getFinalStates(), out, "finalStates", INDENTATION); - out << "</automaton>\n"; -} - -void AutomatonPrinter::toXML(const TM& automaton, ostream& out) { - out << "<automaton>\n"; - printStates(automaton.getStates(), out, "states", INDENTATION); - printAlphabet(automaton.getTapeAlphabet(), out, "tapeAlphabet", INDENTATION); - printAlphabet(automaton.getInputAlphabet(), out, "inputAlphabet", INDENTATION); - printTransitionsTM(automaton.getTransitions(), out, INDENTATION); - printSymbol(automaton.getBlankSymbol(), out, "blankSymbol", INDENTATION); - printStates(automaton.getInitialStates(), out, "initialStates", INDENTATION); - printStates(automaton.getFinalStates(), out, "finalStates", INDENTATION); - out << "</automaton>\n"; -} - -} /* namespace automaton */ - diff --git a/alib/src/automaton/AutomatonPrinter.h b/alib/src/automaton/AutomatonPrinter.h deleted file mode 100644 index 1ba95dd112c81ac4e358ac1bee08909fbcd943ff..0000000000000000000000000000000000000000 --- a/alib/src/automaton/AutomatonPrinter.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * AutomatonPrinter.h - * - * Created on: Nov 11, 2013 - * Author: martin - */ - -#ifndef AUTOMATONPRINTER_H_ -#define AUTOMATONPRINTER_H_ - -#include <ostream> -#include "UnknownAutomaton.h" -#include "FSM/FSM.h" -#include "PDA/PDA.h" -#include "TM/TM.h" - -namespace automaton { - -using namespace std; - -/** - * This class contains methods to print XML representation of automata to the output stream. - */ -class AutomatonPrinter { -protected: - static const string INDENTATION; - - static void printStates(const set<State>& states, ostream& out, string tagName, string prefix); - static void printAlphabet(const set<Symbol>& alphabet, ostream& out, string tagName, string prefix); - - static void printUnknownTransitions(const set<UnknownTransition>& transitions, ostream& out, string prefix); - static void printTransitionsFSM(const set<TransitionFSM>& transitions, ostream& out, string prefix); - static void printTransitionsPDA(const set<TransitionPDA>& transitions, ostream& out, string prefix); - static void printTransitionsTM(const set<TransitionTM>& transitions, ostream& out, string prefix); - - - static void printState(const State& state, ostream& out, string tagName, string prefix); - static void printSymbol(const Symbol& symbol, ostream& out, string tagName, string prefix); - static void printSymbolList(const list<Symbol>& symbols, ostream& out, string tagName, string prefix); - static void printShift(const Shift& shift, ostream& out, string tagName, string prefix); - -public: - /** - * Prints XML representation of UnknownAutomaton to the output stream. - * @param automaton automaton to print - * @param out output stream to which print the automaton - */ - static void toXML(const UnknownAutomaton& automaton, ostream& out); - - /** - * Prints XML representation of FSM to the output stream. - * @param automaton automaton to print - * @param out output stream to which print the automaton - */ - static void toXML(const FSM& automaton, ostream& out); - - /** - * Prints XML representation of PDA to the output stream. - * @param automaton automaton to print - * @param out output stream to which print the automaton - */ - static void toXML(const PDA& automaton, ostream& out); - - /** - * Prints XML representation of TM to the output stream. - * @param automaton automaton to print - * @param out output stream to which print the automaton - */ - static void toXML(const TM& automaton, ostream& out); -}; - -} /* namespace automaton */ -#endif /* AUTOMATONPRINTER_H_ */ diff --git a/alib/src/automaton/FSM/FSM.cpp b/alib/src/automaton/FSM/FSM.cpp deleted file mode 100644 index 645ed9a833ef8f01c05e00eae36fec948c23b10d..0000000000000000000000000000000000000000 --- a/alib/src/automaton/FSM/FSM.cpp +++ /dev/null @@ -1,167 +0,0 @@ -/* - * FSM.cpp - * - * Created on: Mar 25, 2013 - * Author: martin - */ - -#include "FSM.h" -#include "../AutomatonPrinter.h" -#include "../exception/AutomatonException.h" -#include <ostream> - -namespace automaton { - -using namespace std; -using namespace alphabet; - -FSM::~FSM() { - -} - -void FSM::removeState(const State& state) { - for (set<State>::const_iterator initialState = initialStates.begin(); initialState != initialStates.end(); - initialState++) { - if (*initialState == state) { - throw AutomatonException("State \"" + state.getName() + "\" is initial state."); - } - } - - for (set<State>::const_iterator finalState = finalStates.begin(); finalState != finalStates.end(); finalState++) { - if (*finalState == state) { - throw AutomatonException("State \"" + state.getName() + "\" is final state."); - } - } - - for (set<TransitionFSM>::const_iterator t = transitions.begin(); t != transitions.end(); t++) { - if (state == t->getFrom() || state == t->getTo()) - throw AutomatonException("State \"" + state.getName() + "\" is used in transition."); - } - - int removed = states.erase(state); - if (!removed) - throw AutomatonException("State \"" + state.getName() + "\" doesn't exist."); -} - -void FSM::removeInputSymbol(const Symbol& symbol) { - for (set<TransitionFSM>::const_iterator transition = transitions.begin(); transition != transitions.end(); - transition++) { - if (symbol == transition->getInput()) - throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" is used."); - } - - int removed = inputAlphabet.erase(symbol); - if (!removed) - throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" doesn't exist."); - -} - -void FSM::addTransition(const State& from, const Symbol& input, const State& to) { - addTransition(TransitionFSM(from, input, to)); -} - -void FSM::addTransition(const TransitionFSM& transition) { - const State& from = transition.getFrom(); - const Symbol& input = transition.getInput(); - const State& to = transition.getTo(); - - if (states.find(from) == states.end()) - throw AutomatonException("State \"" + from.getName() + "\" doesn't exist."); - - if (input.getSymbol() != "") { - if (inputAlphabet.find(input) == inputAlphabet.end()) - throw AutomatonException("Input symbol \"" + input.getSymbol() + "\" doesn't exist."); - } - - if (states.find(to) == states.end()) - throw AutomatonException("State \"" + to.getName() + "\" doesn't exist."); - - pair<set<TransitionFSM>::iterator, bool> ret = transitions.insert(transition); - if (!ret.second) - throw AutomatonException( - "Transition (\"" + from.getName() + "\", \"" + input.getSymbol() + "\") -> \"" + to.getName() - + "\" already exists."); -} - -void FSM::removeTransition(const TransitionFSM& transition) { - int removed = transitions.erase(transition); - if (!removed) - throw AutomatonException( - "Transition (\"" + transition.getFrom().getName() + "\", \"" + transition.getInput().getSymbol() - + "\") -> \"" + transition.getTo().getName() + "\" doesn't exist."); - -} - -const set<TransitionFSM>& FSM::getTransitions() const { - return transitions; -} - -const set<TransitionFSM> FSM::getTransitionsFromState(const State& from) const { - if( states.find(from) == states.end()) - throw AutomatonException("State \"" + from.getName() + "\" doesn't exist"); - - set<TransitionFSM> transitionsFromState; - for(auto const& transition : transitions){ - if (transition.getFrom() == from){ - transitionsFromState.insert(transition); - } - } - - return transitionsFromState; -} - -const set<TransitionFSM> FSM::getTransitionsToState(const State& to) const { - if( states.find(to) == states.end()) - throw AutomatonException("State \"" + to.getName() + "\" doesn't exist"); - - set<TransitionFSM> transitionsToState; - for(auto const& transition : transitions){ - if (transition.getTo() == to){ - transitionsToState.insert(transition); - } - } - - return transitionsToState; -} - -void FSM::toXML(ostream& out) const { - AutomatonPrinter::toXML(*this, out); -} - -bool FSM::isEpsilonFree() const { - for (auto const& transition : transitions) { - if (transition.getInput() == Symbol("")) { - return false; - } - } - return true; -} - -bool FSM::isDeterministic() const { - if (initialStates.size() != 1) { - return false; - } - - if (!isEpsilonFree()) { - return false; - } - - for (auto transition = transitions.begin(); transition != transitions.end(); transition++) { - auto subTransition = transition; - subTransition++; - for (; subTransition != transitions.end(); subTransition++) { - if (subTransition->getFrom() == transition->getFrom() - && subTransition->getInput() == transition->getInput()) { - return false; - } - } - } - - 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 deleted file mode 100644 index d874df11a6f78c840f0c9074fc1fb3860012a178..0000000000000000000000000000000000000000 --- a/alib/src/automaton/FSM/FSM.h +++ /dev/null @@ -1,105 +0,0 @@ -/* - * FSM.h - * - * Created on: Mar 25, 2013 - * Author: martin - */ - -#ifndef FSM_H_ -#define FSM_H_ - -#include <set> -#include "../Automaton.h" -#include "../State.h" -#include "../../alphabet/Symbol.h" -#include "TransitionFSM.h" - -namespace automaton { - -using namespace std; -using namespace alphabet; - -/** - * Represents Finite State Machine. - * Can store nondeterministic finite state machines with epsilon transitions. - */ -class FSM: public Automaton { -protected: - set<TransitionFSM> transitions; -public: - ~FSM(); - - /** - * @copydoc Automaton::removeState(const State&) - */ - virtual void removeState(const State& state); - - /** - * @copydoc Automaton::removeInputSymbol(const Symbol&) - */ - virtual void removeInputSymbol(const Symbol& symbol); - - /** - * Adds transition defined by parameters to the automaton. - * @param current current state - * @param input input symbol - * @param next next state - * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton - */ - void addTransition(const State& current, const Symbol& input, const State& next); - - /** - * Adds transition to the automaton. - * @param transition transition to add - * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton - */ - void addTransition(const TransitionFSM& transition); - - /** - * Removes transition from the automaton. - * @param transition transition to remove - * @throws AutomatonException when transition doesn't exists. - */ - void removeTransition(const TransitionFSM& transition); - - /** - * @return automaton transitions - */ - const set<TransitionFSM>& getTransitions() const; - - /** - * @return automaton transitions from state - */ - const set<TransitionFSM> getTransitionsFromState(const State& from) const; - - /** - * @return automaton transitions to state - */ - const set<TransitionFSM> getTransitionsToState(const State& to) const; - - /** - * Determines whether FSM contains epsilon transitions. - * @return true when automaton doesn't contain epsilon transitions, false otherwise - */ - bool isEpsilonFree() const; - - /** - * Determines whether FSM is deterministic. - * FSM is deterministic when: - * \li \c contains only 1 initial state. - * \li \c doesn't contain epsilon transitions. - * \li doesn't contain more than one transition with combination (from state, input symbol) - * @return true when automaton is deterministic, false otherwise - */ - bool isDeterministic() const; - - bool isTotal() const; - - /** - * @copydoc Automaton::toXML(ostream&) const - */ - virtual void toXML(ostream& out) const; -}; - -} /* namespace automaton */ -#endif /* FSM_H_ */ diff --git a/alib/src/automaton/FSM/TransitionFSM.cpp b/alib/src/automaton/FSM/TransitionFSM.cpp deleted file mode 100644 index fff1c43a7b9908184e9ab29dfcd494d3a6a8b371..0000000000000000000000000000000000000000 --- a/alib/src/automaton/FSM/TransitionFSM.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* - * TransitionFSM.cpp - * - * Created on: Mar 26, 2013 - * Author: martin - */ - -#include "TransitionFSM.h" - -namespace automaton { -TransitionFSM::TransitionFSM(const State& from,const Symbol& input,const State& to) : Transition(from, input, to) { -} - -bool TransitionFSM::operator < (const TransitionFSM& other) const { - if(from != other.from) { - return from < other.from; - } else if(input != other.input) { - return input < other.input; - } else { - return to < other.to; - } -} - -bool TransitionFSM::operator == (const TransitionFSM& other) const { - return from == other.from && input == other.input && to == other.to; -} - -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 deleted file mode 100644 index 2c581370acebc160aacdca6d8af13143c53be934..0000000000000000000000000000000000000000 --- a/alib/src/automaton/FSM/TransitionFSM.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * TransitionFSM.h - * - * Created on: Mar 26, 2013 - * Author: martin - */ - -#ifndef TRANSITIONFSM_H_ -#define TRANSITIONFSM_H_ - -#include "../Transition.h" -#include <ostream> - -namespace automaton { - -/** - * Represents transition rule for FSM. - */ -class TransitionFSM : public Transition { -public: - /** - * Creates new transition with given parameters. - * @param from from state - * @param input input symbol - * @param to to state - */ - TransitionFSM(const State& from,const Symbol& input,const State& to); - - 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 */ -#endif /* TRANSITIONFSM_H_ */ diff --git a/alib/src/automaton/PDA/PDA.cpp b/alib/src/automaton/PDA/PDA.cpp deleted file mode 100644 index 7866cd1cfb1226cce6da46d2ebb832fbb661f0c9..0000000000000000000000000000000000000000 --- a/alib/src/automaton/PDA/PDA.cpp +++ /dev/null @@ -1,158 +0,0 @@ -/* - * PDA.cpp - * - * Created on: Apr 10, 2013 - * Author: martin - */ - -#include "PDA.h" -#include "../AutomatonPrinter.h" -#include "../exception/AutomatonException.h" -#include <algorithm> - -namespace automaton { - -using namespace std; - -PDA::PDA() { - -} - -void PDA::removeState(const State& state) { - for (set<State>::const_iterator initialState = initialStates.begin(); initialState != initialStates.end(); - initialState++) { - if (*initialState == state) { - throw AutomatonException("State \"" + state.getName() + "\" is initial state."); - } - } - - for (set<State>::const_iterator finalState = finalStates.begin(); finalState != finalStates.end(); finalState++) { - if (*finalState == state) { - throw AutomatonException("State \"" + state.getName() + "\" is final state."); - } - } - - for (set<TransitionPDA>::const_iterator t = transitions.begin(); t != transitions.end(); t++) { - if (state == t->getFrom() || state == t->getTo()) - throw AutomatonException("State \"" + state.getName() + "\" is used in transition."); - } - - int removed = states.erase(state); - if (!removed) - throw AutomatonException("State \"" + state.getName() + "\" doesn't exist."); -} - -void PDA::removeInputSymbol(const Symbol& symbol) { - for (set<TransitionPDA>::const_iterator t = transitions.begin(); t != transitions.end(); t++) { - if (symbol == t->getInput()) - throw AutomatonException("Symbol \"" + symbol.getSymbol() + "\" is used in transition."); - } - - int removed = inputAlphabet.erase(symbol); - if (!removed) - throw AutomatonException("Symbol \"" + symbol.getSymbol() + "\" doesn't exist."); -} - -void PDA::addStackSymbol(const Symbol& symbol) { - pair<set<Symbol>::iterator, bool> ret = stackAlphabet.insert(symbol); - if (!ret.second) - throw AutomatonException("Stack symbol \"" + symbol.getSymbol() + "\" already exists."); -} - -void PDA::removeStackSymbol(const Symbol& symbol) { - for (set<TransitionPDA>::const_iterator t = transitions.begin(); t != transitions.end(); t++) { - for (list<Symbol>::const_iterator popSymbol = t->getPop().begin(); popSymbol != t->getPop().end(); - popSymbol++) { - if (symbol == (*popSymbol)) - throw AutomatonException("Stack symbol \"" + symbol.getSymbol() + "\" is used in transition."); - } - for (list<Symbol>::const_iterator pushSymbol = t->getPush().begin(); pushSymbol != t->getPush().end(); - pushSymbol++) { - if (symbol == (*pushSymbol)) - throw AutomatonException("Stack symbol \"" + symbol.getSymbol() + "\" is used in transition."); - } - } - - for(list<Symbol>::const_iterator startSymbol = startSymbols.begin(); startSymbol != startSymbols.end(); startSymbol++){ - if(*startSymbol == symbol) { - throw AutomatonException("Stack symbol \"" + symbol.getSymbol() + "\" is start symbol."); - } - } - - int removed = stackAlphabet.erase(symbol); - if (!removed) - throw AutomatonException("Stack symbol \"" + symbol.getSymbol() + "\" doesn't exist."); - -} - -const set<Symbol>& PDA::getStackAlphabet() const { - return stackAlphabet; -} - -void PDA::addTransition(const TransitionPDA& transition) { - if (states.find(transition.getFrom()) == states.end()) { - throw AutomatonException("State \"" + transition.getFrom().getName() + "\" doesn't exist."); - } - - if (transition.getInput().getSymbol().compare("") != 0) { - if (inputAlphabet.find(transition.getInput()) == inputAlphabet.end()) { - throw AutomatonException("Input symbol \"" + transition.getInput().getSymbol() + "\" doesn't exist."); - } - } - - if (states.find(transition.getTo()) == states.end()) { - throw AutomatonException("State \"" + transition.getFrom().getName() + "\" doesn't exist."); - } - - list<Symbol>::const_iterator popSymbol = transition.getPop().begin(); - while (popSymbol != transition.getPop().end()) { - if (stackAlphabet.find(*popSymbol) == stackAlphabet.end()) { - throw AutomatonException("Stack symbol \"" + popSymbol->getSymbol() + "\" doesn't exist."); - } - popSymbol++; - } - - list<Symbol>::const_iterator pushSymbol = transition.getPush().begin(); - while (pushSymbol != transition.getPush().end()) { - if (stackAlphabet.find(*pushSymbol) == stackAlphabet.end()) { - throw AutomatonException("Stack symbol \"" + pushSymbol->getSymbol() + "\" doesn't exist."); - } - pushSymbol++; - } - - pair<set<TransitionPDA>::iterator, bool> ret = transitions.insert(transition); - if (!ret.second) { - throw AutomatonException("Transition already exists."); - } -} - -void PDA::removeTransition(const TransitionPDA& transition) { - int removed = transitions.erase(transition); - if (!removed) { - throw AutomatonException("Transition doesn't exist."); - } -} - -const set<TransitionPDA>& PDA::getTransitions() const { - return transitions; -} - -void PDA::setStartSymbols(const list<Symbol>& start) { - for (list<Symbol>::const_iterator symbol = start.begin(); symbol != start.end(); symbol++) { - if (stackAlphabet.find(*symbol) == stackAlphabet.end()) { - throw AutomatonException("Stack symbol \"" + symbol->getSymbol() + "\" doesn't exist."); - } - } - - this->startSymbols = start; -} - -const list<Symbol>& PDA::getStartSymbols() const { - return startSymbols; -} - -void PDA::toXML(ostream& out) const { - AutomatonPrinter::toXML(*this, out); -} - -} /* namespace automaton */ diff --git a/alib/src/automaton/PDA/PDA.h b/alib/src/automaton/PDA/PDA.h deleted file mode 100644 index 0aa2bb8e872d7821e3f692d9730b24b2b4c2d0f0..0000000000000000000000000000000000000000 --- a/alib/src/automaton/PDA/PDA.h +++ /dev/null @@ -1,105 +0,0 @@ -/* - * PDA.h - * - * Created on: Apr 10, 2013 - * Author: martin - */ - -#ifndef PDA_H_ -#define PDA_H_ - -#include <set> -#include <list> -#include "../Automaton.h" -#include "../State.h" -#include "../../alphabet/Symbol.h" -#include "TransitionPDA.h" - -namespace automaton { - -using namespace std; -using namespace alphabet; - -/** - * Push Down Automaton - */ -class PDA: public Automaton { -protected: - set<Symbol> stackAlphabet; - set<TransitionPDA> transitions; - list<Symbol> startSymbols; -public: - PDA(); - - /** - * @copydoc Automaton::removeState(const State&) - */ - virtual void removeState(const State& state); - - /** - * @copydoc Automaton::removeInputSymbol(const Symbol&) - */ - virtual void removeInputSymbol(const Symbol& symbol); - - /** - * Adds symbol to the stack alphabet. - * @param symbol Symbol to add - * @throws AutomatonException when symbol is already present in stack alphabet - */ - void addStackSymbol(const Symbol& symbol); - - /** - * Removes symbol from the stack alphabet. - * @param symbol Symbol to remove - * @throws AutomatonException when symbol is used in transition - * (popped from or pushed to the stack), when it is a start symbol - * or when it is not present in the stack alphabet - */ - void removeStackSymbol(const Symbol& symbol); - - /** - * @return the stack alphabet - */ - const set<Symbol>& getStackAlphabet() const; - - /** - * Adds transition to the PDA. - * @param transition transition to add - * @throws AutomatonException when some part of the transition is not present - * in the PDA (state, input symbol, stack symbol) or when transition already exists - */ - void addTransition(const TransitionPDA& transition); - - /** - * Removes the transition from the PDA. - * @param transition transition to remove - * @throws AutomatonException when transition is not present in the PDA - */ - void removeTransition(const TransitionPDA& transition); - - /** - * @return PDA transitions - */ - const set<TransitionPDA>& getTransitions() const; - - /** - * Sets the list of start symbols. Start symbols are symbols that are pushed - * to the stack when PDA is created. - * @param start symbols that are pushed to the stack - * @throws AutomatonException when any of the symbols is not present in the stack alphabet - */ - void setStartSymbols(const list<Symbol>& start); - - /** - * @return list of start symbols - */ - const list<Symbol>& getStartSymbols() const; - - /** - * @copydoc Automaton::toXML(ostream&) const - */ - void toXML(ostream& out) const; -}; - -} /* namespace automaton */ -#endif /* PDA_H_ */ diff --git a/alib/src/automaton/PDA/TransitionPDA.cpp b/alib/src/automaton/PDA/TransitionPDA.cpp deleted file mode 100644 index 6f14b7d00e8c69eeb14c6f4c2ab9d2e82776277f..0000000000000000000000000000000000000000 --- a/alib/src/automaton/PDA/TransitionPDA.cpp +++ /dev/null @@ -1,150 +0,0 @@ -/* - * TransitionPDA.cpp - * - * Created on: Apr 10, 2013 - * Author: martin - */ - -#include "TransitionPDA.h" - -namespace automaton { - -TransitionPDA::TransitionPDA(const State& from, const Symbol& input, const State& to) : Transition(from, input, to){ - -} - -TransitionPDA::TransitionPDA(const State& from, const Symbol& input, const State& to, const list<Symbol>& pop, - const list<Symbol>& push) : - Transition(from, input, to), pop(pop), push(push) { - -} - -void TransitionPDA::setPop(const list<Symbol>& pop) { - this->pop = pop; -} - -const list<Symbol>& TransitionPDA::getPop() const { - return pop; -} - -void TransitionPDA::setPush(const list<Symbol>& push) { - this->push = push; -} - -const list<Symbol>& TransitionPDA::getPush() const { - return push; -} - -bool TransitionPDA::operator <(const TransitionPDA& other) const { - if (from != other.from) { - return from < other.from; - } else if (input != other.input) { - return input < other.input; - } else if (to != other.to) { - return to < other.to; - } - - list<Symbol>::const_iterator it; - list<Symbol>::const_iterator it2; - - //compare pop size - if (pop.size() != other.pop.size()) - return pop.size() < other.pop.size(); - - //compare pop content - it = pop.begin(); - it2 = other.pop.begin(); - while (it != pop.end()) { - if ((*it) != (*it2)) - return (*it) < (*it2); - it++; - it2++; - } - - //compare push size - if (push.size() != other.push.size()) - return push.size() < other.push.size(); - - //compare push content - it = push.begin(); - it2 = other.push.begin(); - while (it != push.end()) { - if ((*it) != (*it2)) - return (*it) < (*it2); - it++; - it2++; - } - - return false; -} - -bool TransitionPDA::operator ==(const TransitionPDA& other) const { - if (from == other.from && input == other.input && to == other.to) { - - //compare pop size - if (pop.size() != other.pop.size()) - return false; - //compare push size - if (push.size() != other.push.size()) - return false; - - list<Symbol>::const_iterator it; - list<Symbol>::const_iterator it2; - - //compare pop content - it = pop.begin(); - it2 = other.pop.begin(); - while (it != pop.end()) { - if ((*it) != (*it2)) - return false; - it++; - it2++; - } - - //compare push content - it = push.begin(); - it2 = other.push.begin(); - while (it != push.end()) { - if ((*it) != (*it2)) - return false; - it++; - it2++; - } - return true; - } else { - return false; - } -} - -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 deleted file mode 100644 index 9fcc2b71a87139a087c3681bdd0ccf429bb9185a..0000000000000000000000000000000000000000 --- a/alib/src/automaton/PDA/TransitionPDA.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * TransitionPDA.h - * - * Created on: Apr 10, 2013 - * Author: martin - */ - -#ifndef TRANSITIONPDA_H_ -#define TRANSITIONPDA_H_ - -#include "../Transition.h" -#include <list> -namespace automaton { - -using namespace std; - -class TransitionPDA: public Transition { -protected: - list<Symbol> pop; - list<Symbol> push; - -public: - TransitionPDA(const State& from, const Symbol& input, const State& to); - TransitionPDA(const State& from, const Symbol& input, const State& to, const list<Symbol>& pop, - const list<Symbol>& push); - - void setPop(const list<Symbol>& pop); - const list<Symbol>& getPop() const; - void setPush(const list<Symbol>& push); - const list<Symbol>& getPush() const; - - 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 */ -#endif /* TRANSITIONPDA_H_ */ diff --git a/alib/src/automaton/Shift.cpp b/alib/src/automaton/Shift.cpp deleted file mode 100644 index 03f2e55c21ad5cb07091c953f7ea6c2334bae539..0000000000000000000000000000000000000000 --- a/alib/src/automaton/Shift.cpp +++ /dev/null @@ -1,10 +0,0 @@ -/* - * 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 deleted file mode 100644 index 721caff2ad9be5795e5612e118c8e51a85572a85..0000000000000000000000000000000000000000 --- a/alib/src/automaton/Shift.h +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Shift.h - * - * Created on: Oct 13, 2013 - * Author: martin - */ - -#ifndef SHIFT_H_ -#define SHIFT_H_ - -#include <string> - -/** - * Represent movement of the reading head in the Turing machine. - */ -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 deleted file mode 100644 index d278b21fb39be9c9f29b76062e075b60c72982f2..0000000000000000000000000000000000000000 --- a/alib/src/automaton/State.cpp +++ /dev/null @@ -1,36 +0,0 @@ -/* - * State.cpp - * - * Created on: Mar 26, 2013 - * Author: martin - */ - -#include "State.h" - -namespace automaton { - -State::State(const std::string& name) { - this->name = name; -} - -const std::string& State::getName() const { - return name; -} - -bool State::operator < (const State& other) const { - return name < other.name; -} - -bool State::operator == (const State& other) const { - return name == other.name; -} -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 deleted file mode 100644 index 1cb546d3cafaf3896ca73aaa006962f65916c64c..0000000000000000000000000000000000000000 --- a/alib/src/automaton/State.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * StateFSM.h - * - * Created on: Mar 26, 2013 - * Author: martin - */ - -#ifndef STATEFSM_H_ -#define STATEFSM_H_ - -#include <string> -#include <ostream> - -namespace automaton { - -/** - * Class representing state in an automaton. - */ -class State { -private: - std::string name; -public: - State(const std::string& name); - const std::string& getName() const; - - 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 */ -#endif /* STATEFSM_H_ */ diff --git a/alib/src/automaton/TM/TM.cpp b/alib/src/automaton/TM/TM.cpp deleted file mode 100644 index edb73c66b5e7d312ac29d2aabe6ba192abca4270..0000000000000000000000000000000000000000 --- a/alib/src/automaton/TM/TM.cpp +++ /dev/null @@ -1,151 +0,0 @@ -/* - * TM.cpp - * - * Created on: Apr 24, 2013 - * Author: martin - */ - -#include "TM.h" -#include "../AutomatonPrinter.h" -#include "../exception/AutomatonException.h" - -namespace automaton { - -using namespace std; -using namespace alphabet; - -TM::TM() : - blankSymbol(Symbol("")) { -} - -void TM::addInputSymbol(const Symbol& symbol) { - if (tapeAlphabet.find(symbol) == tapeAlphabet.end()) { - throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" is not in tape alphabet."); - } - - if (symbol == blankSymbol) { - throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" cannot be blank symbol."); - } - - std::pair<std::set<Symbol>::iterator, bool> ret = inputAlphabet.insert(symbol); - if (!ret.second) - throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" already exists."); -} - -void TM::removeState(const State& state) { - for (set<State>::const_iterator initialState = initialStates.begin(); initialState != initialStates.end(); - initialState++) { - if (*initialState == state) { - throw AutomatonException("State \"" + state.getName() + "\" is initial state."); - } - } - - for (set<State>::const_iterator finalState = finalStates.begin(); finalState != finalStates.end(); finalState++) { - if (*finalState == state) { - throw AutomatonException("State \"" + state.getName() + "\" is final state."); - } - } - - for (set<TransitionTM>::const_iterator t = transitions.begin(); t != transitions.end(); t++) { - if (state == t->getFrom() || state == t->getTo()) - throw AutomatonException("State \"" + state.getName() + "\" is used."); - } - - int removed = states.erase(state); - if (!removed) - throw AutomatonException("State \"" + state.getName() + "\" doesn't exist."); -} - -void TM::removeInputSymbol(const Symbol& symbol) { - int removed = inputAlphabet.erase(symbol); - if (!removed) - throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" doesn't exist."); -} - -void TM::addTapeSymbol(const Symbol& symbol) { - pair<set<Symbol>::iterator, bool> ret = tapeAlphabet.insert(symbol); - if (!ret.second) - throw AutomatonException("Tape symbol \"" + symbol.getSymbol() + "\" already exists."); -} - -void TM::removeTapeSymbol(const Symbol& symbol) { - for (set<Symbol>::const_iterator it = inputAlphabet.begin(); it != inputAlphabet.end(); it++) { - if (symbol == (*it)) { - throw AutomatonException("Tape symbol \"" + symbol.getSymbol() + "\" is in input alphabet."); - } - } - - for (set<TransitionTM>::const_iterator t = transitions.begin(); t != transitions.end(); t++) { - if (symbol == t->getInput() || symbol == t->getOutput()) - throw AutomatonException("Tape symbol \"" + symbol.getSymbol() + "\" is used in transition."); - } - - int removed = inputAlphabet.erase(symbol); - if (!removed) - throw AutomatonException("Tape symbol \"" + symbol.getSymbol() + "\" doesn't exist."); - -} - -const std::set<Symbol>& TM::getTapeAlphabet() const { - return tapeAlphabet; -} - -void TM::addTransition(const TransitionTM& transition) { - if (states.find(transition.getFrom()) == states.end()) { - throw AutomatonException("State \"" + transition.getFrom().getName() + "\" doesn't exist."); - } - - if (transition.getInput().getSymbol() != "") { - if (tapeAlphabet.find(transition.getInput()) == tapeAlphabet.end()) { - throw AutomatonException("Tape symbol \"" + transition.getInput().getSymbol() + "\" doesn't exist."); - } - } - - if (states.find(transition.getTo()) == states.end()) { - throw AutomatonException("State \"" + transition.getTo().getName() + "\" doesn't exist."); - } - - if (tapeAlphabet.find(transition.getOutput()) == tapeAlphabet.end()) { - throw AutomatonException("Tape symbol \"" + transition.getOutput().getSymbol() + "\" doesn't exist."); - } - - pair<set<TransitionTM>::iterator, bool> ret = transitions.insert(transition); - if (!ret.second) { - throw AutomatonException("Transition already exists."); - } - -} - -void TM::removeTransition(const TransitionTM& transition) { - int removed = transitions.erase(transition); - if (!removed) { - throw AutomatonException("Transition doesn't exist."); - } -} - -const std::set<TransitionTM>& TM::getTransitions() const { - return transitions; -} - -void TM::setBlankSymbol(const Symbol& symbol) { - for (set<Symbol>::const_iterator it = inputAlphabet.begin(); it != inputAlphabet.end(); it++) { - if (symbol == (*it)) { - throw AutomatonException("Blank symbol \"" + symbol.getSymbol() + "\" is in input alphabet."); - } - } - - if (tapeAlphabet.find(symbol) == tapeAlphabet.end()) - throw AutomatonException("Blank symbol \"" + symbol.getSymbol() + "\" is not in tape alphabet."); - - blankSymbol = symbol; - -} -const Symbol& TM::getBlankSymbol() const { - return blankSymbol; -} - -void TM::toXML(std::ostream& out) const { - AutomatonPrinter::toXML(*this, out); -} - -} /* namespace automaton */ diff --git a/alib/src/automaton/TM/TM.h b/alib/src/automaton/TM/TM.h deleted file mode 100644 index 550e7404d1d0c611fb33dd9a254625714ffe1f2e..0000000000000000000000000000000000000000 --- a/alib/src/automaton/TM/TM.h +++ /dev/null @@ -1,109 +0,0 @@ -/* - * TM.h - * - * Created on: Apr 24, 2013 - * Author: martin - */ - -#ifndef TM_H_ -#define TM_H_ - -#include "../Automaton.h" -#include "TransitionTM.h" - -namespace automaton { - -using namespace std; -using namespace alphabet; - -/** - * Turing machine - */ -class TM: public automaton::Automaton { -protected: - set<Symbol> tapeAlphabet; - set<TransitionTM> transitions; - Symbol blankSymbol; - -public: - TM(); - - /** - * Adds symbol to the input alphabet. - * @param symbol Symbol to add - * @throws AutomatonException when symbol is not present in tape alphabet, - * when it's blank symbol or when it is already present in input alphabet - */ - virtual void addInputSymbol(const Symbol& symbol); - - /** - * @copydoc Automaton::removeState(const State&) - */ - virtual void removeState(const State& state); - - /** - * Removes input symbol from the input alphabet. - * @param symbol Symbol to remove - * @throws AutomatonException when symbol is part of the transition - */ - virtual void removeInputSymbol(const Symbol& symbol); - - /** - * Adds symbol to the tape alphabet. - * @param symbol Symbol to add - * @throw AutomatonException when Symbol is already present in tape alphabet - */ - void addTapeSymbol(const Symbol& symbol); - - /** - * Removes symbol from the tape alphabet. - * @param symbol Symbol to remove - * @throw AutomatonException when Symbol is not present in the tape alphabet, - * when it is used in transition or when it is present in input alphabet - */ - void removeTapeSymbol(const Symbol& symbol); - - /** - * @return tape alphabet - */ - const set<Symbol>& getTapeAlphabet() const; - - /** - * Adds transition to the automaton. - * @param transition transition to add - * @throws AutomatonException when some part of the transition is not present - * in the TM (state, tape symbol) or when transition already exists - */ - void addTransition(const TransitionTM& transition); - - /** - * Removes the transition from the TM. - * @param transition transition to remove - * @throws AutomatonException when transition is not present in the TM - */ - void removeTransition(const TransitionTM& transition); - - /** - * @return TM transitions - */ - const set<TransitionTM>& getTransitions() const; - - /** - * Sets the blank symbol = symbol representing empty cell of the tape - * @param symbol Symbol to set - */ - void setBlankSymbol(const Symbol& symbol); - - /** - * @return symbol representing empty cell of the tape - */ - const Symbol& getBlankSymbol() const; - - /** - * @copydoc Automaton::toXML(ostream&) const - */ - virtual void toXML(ostream& out) const; -}; - -} /* namespace automaton */ -#endif /* TM_H_ */ diff --git a/alib/src/automaton/TM/TransitionTM.cpp b/alib/src/automaton/TM/TransitionTM.cpp deleted file mode 100644 index ac535ee8dd366362eb1b2c1edf5ec9495b9ca8e1..0000000000000000000000000000000000000000 --- a/alib/src/automaton/TM/TransitionTM.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/* - * TransitionTM.cpp - * - * Created on: Apr 24, 2013 - * Author: martin - */ - -#include "TransitionTM.h" - -namespace automaton { - -TransitionTM::TransitionTM(const State& from, const Symbol& input, const State& to, const Symbol& output, - Shift shift) : - Transition(from, input, to), output(output), shift(shift) { -} - -const Symbol& TransitionTM::getOutput() const { - return output; -} -void TransitionTM::setOutput(const Symbol& output) { - this->output = output; -} -Shift TransitionTM::getShift() const { - return shift; -} -void TransitionTM::setShift(Shift shift) { - this->shift = shift; -} - -bool TransitionTM::operator <(const TransitionTM& other) const { - if (from != other.from) { - return from < other.from; - } else if (input != other.input) { - return input < other.input; - } else if (to != to) { - return to < other.to; - } else if (output != output) { - return output < other.output; - } else { - return shift < other.shift; - } - -} -bool TransitionTM::operator ==(const TransitionTM& other) const { - return from == other.from && input == other.input && to == other.to && output == other.output - && shift == other.shift; -} -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 deleted file mode 100644 index 3ce2559b199762bea700e532d7f9397f47341c78..0000000000000000000000000000000000000000 --- a/alib/src/automaton/TM/TransitionTM.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * TransitionTM.h - * - * Created on: Apr 24, 2013 - * Author: martin - */ - -#ifndef TRANSITIONTM_H_ -#define TRANSITIONTM_H_ - -#include "../Transition.h" -#include "../Shift.h" - -namespace automaton { - -using namespace std; - -class TransitionTM: public Transition { -protected: - Symbol output; - Shift shift; -public: - - /** - * Creates new transition with given parameters. - * @param from from state - * @param input input symbol - * @param to to state - * @param output output symbol - * @param shift direction of movement of the reading head - */ - TransitionTM(const State& from, const Symbol& input, const State& to, const Symbol& output, Shift shift); - - /** - * @return output Symbol - */ - const Symbol& getOutput() const; - - /** - * Sets the output symbol. - * @param output Symbol - */ - void setOutput(const Symbol& output); - - /** - * @return direction of movement of the reading head - */ - Shift getShift() const; - - /** - * Sets the direction of movement of the reading head - * @param shift Shift to set - */ - void setShift(Shift shift); - - 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 */ -#endif /* TRANSITIONTM_H_ */ diff --git a/alib/src/automaton/Transition.cpp b/alib/src/automaton/Transition.cpp deleted file mode 100644 index 3c0aea6d294c59e38bbb0a1054fd8376e92af701..0000000000000000000000000000000000000000 --- a/alib/src/automaton/Transition.cpp +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Transition.cpp - * - * Created on: Apr 16, 2013 - * Author: martin - */ - -#include "Transition.h" - -namespace automaton { - -Transition::Transition(const State& current, const Symbol& input, const State& next) : - from(current), input(input), to(next) { - -} - -Transition::~Transition() { - -} -void Transition::setFrom(const State& state) { - from = state; -} -const State& Transition::getFrom() const { - return from; -} -void Transition::setInput(const Symbol& symbol) { - input = symbol; -} - -const Symbol& Transition::getInput() const { - return input; -} -void Transition::setTo(const State& state) { - to = state; -} - -const State& Transition::getTo() const { - return to; -} - -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 deleted file mode 100644 index c3fce1ef4d2f415f2d10cedd124b5a763dca806e..0000000000000000000000000000000000000000 --- a/alib/src/automaton/Transition.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Transition.h - * - * Created on: Apr 16, 2013 - * Author: martin - */ - -#ifndef TRANSITION_H_ -#define TRANSITION_H_ - -#include "State.h" -#include "../alphabet/Symbol.h" - -namespace automaton { - -using namespace alphabet; - -/** - * Abstract base class for all transitions. Contains common elements of transitions. - */ -class Transition { -protected: - State from; - Symbol input; - State to; -public: - Transition(const State& current, const Symbol& input, const State& next); - virtual ~Transition(); - - /** - * @param state State from which the transition goes - */ - void setFrom(const State& state); - - /** - * @return State form which the transition goes - */ - const State& getFrom() const; - - /** - * @param symbol input symbol for this transition - */ - void setInput(const Symbol& symbol); - - /** - * @return input symbol for this transition - */ - const Symbol& getInput() const; - - /** - * @param state State to which the transition goes - */ - void setTo(const State& state); - - /** - * @return State to which the transition goes - */ - const State& getTo() const; - - /** - * Determines whether State is used in this transition either as from state or to state. - * @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 */ -#endif /* TRANSITION_H_ */ diff --git a/alib/src/automaton/UnknownAutomaton.cpp b/alib/src/automaton/UnknownAutomaton.cpp deleted file mode 100644 index 225802e24b83bd77face09d7504da9d4f8ddfe1e..0000000000000000000000000000000000000000 --- a/alib/src/automaton/UnknownAutomaton.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/* - * UnknownAutomaton.cpp - * - * Created on: Oct 12, 2013 - * Author: martin - */ - -#include "UnknownAutomaton.h" - -#include "AutomatonPrinter.h" -#include "exception/AutomatonException.h" - -namespace automaton { - -UnknownAutomaton::UnknownAutomaton() : - blankSymbol("") { - -} - -UnknownAutomaton::~UnknownAutomaton() { -} - -void UnknownAutomaton::removeState(const State& state) { - if (!states.erase(state)) - throw AutomatonException("State \"" + state.getName() + "\" doesn't exist."); -} - -void UnknownAutomaton::removeInputSymbol(const Symbol& symbol) { - if (!inputAlphabet.erase(symbol)) - throw AutomatonException("Input symbol \"" + symbol.getSymbol() + "\" doesn't exist."); -} - -const set<Symbol>& UnknownAutomaton::getStackAlphabet() const { - return stackAlphabet; -} - -void UnknownAutomaton::addStackSymbol(const Symbol& symbol) { - if (!stackAlphabet.insert(symbol).second) { - throw AutomatonException("Stack symbol \"" + symbol.getSymbol() + "\" already exists."); - } -} - -void UnknownAutomaton::removeStackSymbol(const Symbol& symbol) { - if (!stackAlphabet.erase(symbol)) - throw AutomatonException("Stack symbol \"" + symbol.getSymbol() + "\" doesn't exist."); -} - -const list<Symbol>& UnknownAutomaton::getStartSymbols() const { - return startSymbols; -} - -void UnknownAutomaton::setStartSymbols(const list<Symbol>& newSymbols) { - this->startSymbols = newSymbols; -} - -const set<Symbol>& UnknownAutomaton::getTapeAlphabet() const { - return tapeAlphabet; -} - -void UnknownAutomaton::addTapeSymbol(const Symbol& symbol) { - if (!tapeAlphabet.insert(symbol).second) { - throw AutomatonException("Tape symbol \"" + symbol.getSymbol() + "\" already exists."); - } -} - -void UnknownAutomaton::removeTapeSymbol(const Symbol& symbol) { - int removed = tapeAlphabet.erase(symbol); - if (!removed) { - throw AutomatonException("Tape symbol \"" + symbol.getSymbol() + "\" doesn't exist."); - } -} - -const Symbol& UnknownAutomaton::getBlankSymbol() const { - return blankSymbol; -} - -void UnknownAutomaton::setBlankSymbol(const Symbol& symbol) { - blankSymbol = symbol; -} - -const set<UnknownTransition>& UnknownAutomaton::getTransitions() const { - return transitions; -} - -void UnknownAutomaton::addTransition(const UnknownTransition& transition) { - if (!transitions.insert(transition).second) { - throw AutomatonException("Transition already exists."); - } -} - -void UnknownAutomaton::removeTransition(const UnknownTransition& transition) { - if (!transitions.erase(transition)) { - throw AutomatonException("Transition doesn't exist."); - } -} - -void UnknownAutomaton::toXML(ostream& out) const { - AutomatonPrinter::toXML(*this, out); -} - -} /* namespace automaton */ diff --git a/alib/src/automaton/UnknownAutomaton.h b/alib/src/automaton/UnknownAutomaton.h deleted file mode 100644 index 3adf1b62f34dd5621580c7414aa15424697e4e00..0000000000000000000000000000000000000000 --- a/alib/src/automaton/UnknownAutomaton.h +++ /dev/null @@ -1,139 +0,0 @@ -/* - * UnknownAutomaton.h - * - * Created on: Oct 12, 2013 - * Author: Martin Zak - */ - -#ifndef UNKNOWNAUTOMATON_H_ -#define UNKNOWNAUTOMATON_H_ - -#include <set> -#include <list> - -#include "Automaton.h" -#include "State.h" -#include "../alphabet/Symbol.h" -#include "UnknownTransition.h" - -namespace automaton { - -using namespace std; -using namespace alphabet; - -/** - * Class representing unknown automaton parsed from XML. - */ -class UnknownAutomaton: public Automaton { -protected: - set<Symbol> stackAlphabet; - list<Symbol> startSymbols; - set<Symbol> tapeAlphabet; - Symbol blankSymbol; - - set<UnknownTransition> transitions; -public: - UnknownAutomaton(); - virtual ~UnknownAutomaton(); - - /** - * Removes the state from the automaton. - * @param state State to remove - * @throws AutomatonException when state is not present in the automaton - */ - void removeState(const State& state); - - /** - * Removes input symbol from the input alphabet. - * @param symbol Symbol to remove - * @throws AutomatonException when symbol is not present in input alphabet - */ - void removeInputSymbol(const Symbol& symbol); - - /** - * @return the stack alphabet - */ - const set<Symbol>& getStackAlphabet() const; - - /** - * Adds symbol to the stack alphabet. - * @param symbol Symbol to add - * @throw AutomatonException when Symbol is already present in stack alphabet - */ - void addStackSymbol(const Symbol& symbol); - - /** - * Removes symbol from the stack alphabet. - * @param symbol Symbol to remove - * @throw AutomatonException when Symbol is not present in input alphabet - */ - void removeStackSymbol(const Symbol& symbol); - - /** - * @return list of start symbols - */ - const list<Symbol>& getStartSymbols() const; - - /** - * Set the start symbol list. - * @param newSymbols symbols to set - */ - void setStartSymbols(const list<Symbol>& newSymbols); - - /** - * @return the tape alphabet - */ - const set<Symbol>& getTapeAlphabet() const; - - /** - * Adds symbol to the tape alphabet. - * @param symbol Symbol to add - * @throw AutomatonException when Symbol is already present in tape alphabet - */ - void addTapeSymbol(const Symbol& symbol); - - /** - * Removes symbol from the tape alphabet. - * @param symbol Symbol to remove - * @throw AutomatonException when Symbol is not present in tape alphabet - */ - void removeTapeSymbol(const Symbol& symbol); - - /** - * @return the blank symbol - */ - const Symbol& getBlankSymbol() const; - - /** - * Sets the blank symbol. - * @param symbol the Symbol to set - */ - void setBlankSymbol(const Symbol& symbol); - - /** - * @return transitions of the automaton - */ - const set<UnknownTransition>& getTransitions() const; - - /** - * Adds new transition to the automaton. - * @param transition UnknownTransition to add - * @throws AutomatonException when transition is already present in the automaton - */ - void addTransition(const UnknownTransition& transition); - - /** - * Removes the transition from the automaton. - * @param transition UnknownTransition to remove - * @throws AutomatonException when transition is not present in the automaton - */ - void removeTransition(const UnknownTransition& transition); - - /** - * @copydoc Automaton::toXML(ostream&) const - */ - void toXML(std::ostream& out) const; -}; - -} /* namespace automaton */ -#endif /* UNKNOWNAUTOMATON_H_ */ diff --git a/alib/src/automaton/UnknownTransition.cpp b/alib/src/automaton/UnknownTransition.cpp deleted file mode 100644 index 878aededbef8d9fd28b765b8c602797eda42e5ab..0000000000000000000000000000000000000000 --- a/alib/src/automaton/UnknownTransition.cpp +++ /dev/null @@ -1,169 +0,0 @@ -/* - * UnknownTransition.cpp - * - * Created on: Oct 13, 2013 - * Author: martin - */ - -#include "UnknownTransition.h" - -namespace automaton { - -using namespace std; - -UnknownTransition::UnknownTransition() : - Transition(State(""), Symbol(""), State("")), output("") { - shift = NOT_SET; -} - -const list<Symbol>& UnknownTransition::getPop() const { - return pop; -} - -void UnknownTransition::addPop(const Symbol& symbol) { - pop.push_back(symbol); -} - -const list<Symbol>& UnknownTransition::getPush() const { - return push; -} - -void UnknownTransition::addPush(const Symbol& symbol) { - push.push_back(symbol); -} - -const Symbol& UnknownTransition::getOutput() const { - return output; -} - -void UnknownTransition::setOutput(const Symbol& symbol) { - output = symbol; -} - -const Shift& UnknownTransition::getShift() const { - return shift; -} - -void UnknownTransition::setShift(const Shift& shift) { - this->shift = shift; -} - -bool UnknownTransition::operator <(const UnknownTransition& other) const { - if (from != other.from) { - return from < other.from; - } else if (input != other.input) { - return input < other.input; - } else if (to != other.to) { - return to < other.to; - } else if (output != other.output) { - return output < other.output; - } else if (shift != other.shift) { - return shift < other.shift; - } - - std::list<Symbol>::const_iterator it; - std::list<Symbol>::const_iterator it2; - - //compare pop size - if (pop.size() != other.pop.size()) - return pop.size() < other.pop.size(); - - //compare pop content - it = pop.begin(); - it2 = other.pop.begin(); - while (it != pop.end()) { - if ((*it) != (*it2)) - return (*it) < (*it2); - it++; - it2++; - } - - //compare push size - if (push.size() != other.push.size()) - return push.size() < other.push.size(); - - //compare push content - it = push.begin(); - it2 = other.push.begin(); - while (it != push.end()) { - if ((*it) != (*it2)) - return (*it) < (*it2); - it++; - it2++; - } - - return false; -} - -bool UnknownTransition::operator ==(const UnknownTransition& other) const { - if (from == other.from && input == other.input && to == other.to && output == other.output - && shift == other.shift) { - - //compare pop size - if (pop.size() != other.pop.size()) - return false; - //compare push size - if (push.size() != other.push.size()) - return false; - - std::list<Symbol>::const_iterator it; - std::list<Symbol>::const_iterator it2; - - //compare pop content - it = pop.begin(); - it2 = other.pop.begin(); - while (it != pop.end()) { - if ((*it) != (*it2)) - return false; - it++; - it2++; - } - - //compare push content - it = push.begin(); - it2 = other.push.begin(); - while (it != push.end()) { - if ((*it) != (*it2)) - return false; - it++; - it2++; - } - return true; - } else { - return false; - } -} - -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 deleted file mode 100644 index 0dcf1ee45d9a0f9f7e62f8658c3a7a21d6a21476..0000000000000000000000000000000000000000 --- a/alib/src/automaton/UnknownTransition.h +++ /dev/null @@ -1,89 +0,0 @@ -/* - * UnknownTransition.h - * - * Created on: Oct 13, 2013 - * Author: Martin Zak - */ - -#ifndef UNKNOWNTRANSITION_H_ -#define UNKNOWNTRANSITION_H_ - -#include <list> - -#include "Transition.h" -#include "State.h" -#include "../alphabet/Symbol.h" -#include "Shift.h" - -namespace automaton { - -using namespace std; -using namespace alphabet; - -/** - * Class representing unknown transition parsed from XML. Part of UnknownAutomaton. - */ -class UnknownTransition: public Transition { -protected: - list<Symbol> pop; - list<Symbol> push; - - Symbol output; - Shift shift; - -public: - UnknownTransition(); - - /** - * @return list of symbols that are popped from the stack - */ - const list<Symbol>& getPop() const; - - /** - * Add symbol to the end of the list of symbols that are popped from the stack. - * @param symbol Symbol to add - */ - void addPop(const Symbol& symbol); - - /** - * @return list of symbols that are pushed to the stack - */ - const list<Symbol>& getPush() const; - - /** - * Add symbol to the end of the list of symbols that are pushed to the stack. - * @param symbol Symbol to add - */ - void addPush(const Symbol& symbol); - - /** - * @return the output symbol of the transition - */ - const Symbol& getOutput() const; - - /** - * Sets the output Symbol of the transition. - * @param symbol Symbol to set - */ - void setOutput(const Symbol& symbol); - - /** - * @return direction of movement of the reading head - */ - const Shift& getShift() const; - - /** - * Sets the direction of movement of reading head. - * @param shift the direction - */ - void setShift(const Shift& shift); - - 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 */ -#endif /* UNKNOWNTRANSITION_H_ */ diff --git a/alib/src/automaton/exception/AutomatonException.cpp b/alib/src/automaton/exception/AutomatonException.cpp deleted file mode 100644 index f432776b2daa7b49e68dd94ff779b14d6bb0c656..0000000000000000000000000000000000000000 --- a/alib/src/automaton/exception/AutomatonException.cpp +++ /dev/null @@ -1,23 +0,0 @@ -/* - * AutomatonException.cpp - * - * Created on: Apr 1, 2013 - * Author: Martin Zak - */ - -#include "AutomatonException.h" - -namespace automaton { - -AutomatonException::AutomatonException() { -} - -AutomatonException::AutomatonException(const string& cause) : - AlibException(cause) { -} - -AutomatonException::~AutomatonException() throw () { - -} - -} /* namespace automaton */ diff --git a/alib/src/automaton/exception/AutomatonException.h b/alib/src/automaton/exception/AutomatonException.h deleted file mode 100644 index fe0fffe1babd18f8dd10f20e4e43d52593780fa6..0000000000000000000000000000000000000000 --- a/alib/src/automaton/exception/AutomatonException.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * AutomatonException.h - * - * Created on: Apr 1, 2013 - * Author: Martin Zak - */ - -#ifndef AUTOMATONEXCEPTION_H_ -#define AUTOMATONEXCEPTION_H_ - -#include "../../AlibException.h" - -namespace automaton { - -using namespace std; - -/** - * Exception thrown by an automaton, automaton parser or automaton printer. - */ -class AutomatonException: public alib::AlibException { -public: - AutomatonException(); - AutomatonException(const string& cause); - virtual ~AutomatonException() throw (); -}; - -} /* namespace automaton */ -#endif /* AUTOMATONEXCEPTION_H_ */ diff --git a/alib/src/grammar/ContextFree/ContextFreeGrammar.cpp b/alib/src/grammar/ContextFree/ContextFreeGrammar.cpp deleted file mode 100644 index 194f2c09a1ec53a4b5dade5021245090eb0e1dfb..0000000000000000000000000000000000000000 --- a/alib/src/grammar/ContextFree/ContextFreeGrammar.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/* - * ContextFreeGrammar.cpp - * - * Created on: Nov 17, 2013 - * Author: martin - */ - -#include "ContextFreeGrammar.h" - -namespace grammar { - -ContextFreeGrammar::ContextFreeGrammar() : - Grammar() { -} - -ContextFreeGrammar::ContextFreeGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols, - const Symbol& startSymbol) : - Grammar(nonTerminalSymbols, terminalSymbols, startSymbol) { -} - -bool ContextFreeGrammar::isValidRule(const Rule& rule) const { - return checkLeftSide(rule.getLeftSide()) && checkRightSide(rule.getRightSide()); -} - -bool ContextFreeGrammar::checkLeftSide(const list<Symbol>& leftSide) const { - if (leftSide.size() != 1) { - return false; - } - - if (nonTerminalSymbols.find(leftSide.front()) == nonTerminalSymbols.end()) { - return false; - } - - return true; -} - -bool ContextFreeGrammar::checkRightSide(const list<Symbol>& rightSide) const { - - //check that all symbols are terminal or nonterminal - for (auto const& symbol : rightSide) { - if (terminalSymbols.find(symbol) == terminalSymbols.end() - && nonTerminalSymbols.find(symbol) == nonTerminalSymbols.end()) { - return false; - } - } - - return true; -} - -} /* namespace grammar */ diff --git a/alib/src/grammar/ContextFree/ContextFreeGrammar.h b/alib/src/grammar/ContextFree/ContextFreeGrammar.h deleted file mode 100644 index 5c481e1b055199b5c99358832cfca8cdd1c46cc9..0000000000000000000000000000000000000000 --- a/alib/src/grammar/ContextFree/ContextFreeGrammar.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * ContextFreeGrammar.h - * - * Created on: Nov 17, 2013 - * Author: martin - */ - -#ifndef CONTEXTFREEGRAMMAR_H_ -#define CONTEXTFREEGRAMMAR_H_ - -#include "../Grammar.h" - -namespace grammar { - -/** - * Context-free grammar. Type 2 in Chomsky hierarchy. - */ -class ContextFreeGrammar: public Grammar { -private: - /** - * Checks that left side of the rule contains one symbol which is nonterminal. - * @param leftSide left side of the rule - * @return true when left side of the rule contains one nonterminal symbol, false otherwise - */ - bool checkLeftSide(const list<Symbol>& leftSide) const; - - /** - * Checks that symbols on the right side of the rule are all terminal or nonterminal. - * @param rightSide right side of the rule - * @return true when right side of the rule contains only terminal and nonterminal symbols, false otherwise - */ - bool checkRightSide(const list<Symbol>& rightSide) const; -protected: - /** - * @copydoc Grammar::isValidRule(const Rule&) const - */ - bool isValidRule(const Rule& rule) const; -public: - ContextFreeGrammar(); - ContextFreeGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols, - const Symbol& startSymbol); -}; - -} /* namespace grammar */ -#endif /* CONTEXTFREEGRAMMAR_H_ */ diff --git a/alib/src/grammar/ContextSensitive/ContextSensitiveGrammar.cpp b/alib/src/grammar/ContextSensitive/ContextSensitiveGrammar.cpp deleted file mode 100644 index bf139641618b21e2f2667d765a55d656eb1e9d2e..0000000000000000000000000000000000000000 --- a/alib/src/grammar/ContextSensitive/ContextSensitiveGrammar.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* - * ContextSensitiveGrammar.cpp - * - * Created on: Nov 17, 2013 - * Author: martin - */ - -#include "ContextSensitiveGrammar.h" - -namespace grammar { - -ContextSensitiveGrammar::ContextSensitiveGrammar() : - Grammar() { -} - -ContextSensitiveGrammar::ContextSensitiveGrammar(const set<Symbol>& nonTerminalSymbols, - const set<Symbol>& terminalSymbols, const Symbol& startSymbol) : - Grammar(nonTerminalSymbols, terminalSymbols, startSymbol) { -} - -bool ContextSensitiveGrammar::isValidRule(const Rule& rule) const { - - //left side cannot be longer than right side - if (rule.getLeftSide().size() > rule.getRightSide().size()) { - return false; - } - - /* - * Finds nonterminal symbol in the left side of the rule and tries to match the symbols before nonterminal (prefix) to the beginning - * of the right side of the rule and symbols after the nonterminal (suffix) to the end of the right side of the rule. - */ - for (list<Symbol>::const_iterator symbol = rule.getLeftSide().begin(); symbol != rule.getLeftSide().end(); - symbol++) { - if (nonTerminalSymbols.find(*symbol) != nonTerminalSymbols.end()) { - if (startsWith(rule.getLeftSide().begin(), symbol, rule.getRightSide().begin()) - && endsWith(symbol, rule.getLeftSide().end(), rule.getRightSide().end())) { - return true; - } - } - } - - return false; -} - -bool ContextSensitiveGrammar::startsWith(list<Symbol>::const_iterator prefixStart, - list<Symbol>::const_iterator prefixEnd, list<Symbol>::const_iterator rightSideStart) const { - while (prefixStart != prefixEnd) { - if (*prefixStart != *rightSideStart) { - return false; - } - prefixStart++; - rightSideStart++; - } - return true; -} - -bool ContextSensitiveGrammar::endsWith(list<Symbol>::const_iterator suffixStart, list<Symbol>::const_iterator suffixEnd, - list<Symbol>::const_iterator rightSideEnd) const { - suffixEnd--; - rightSideEnd--; - - while (suffixStart != suffixEnd) { - - if (*suffixEnd != *rightSideEnd) { - return false; - } - suffixEnd--; - rightSideEnd--; - } - return true; -} - -} /* namespace grammar */ - diff --git a/alib/src/grammar/ContextSensitive/ContextSensitiveGrammar.h b/alib/src/grammar/ContextSensitive/ContextSensitiveGrammar.h deleted file mode 100644 index 4b6d8d62c48707fd6fcfd38b86e429fc2ca20494..0000000000000000000000000000000000000000 --- a/alib/src/grammar/ContextSensitive/ContextSensitiveGrammar.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * ContextSensitiveGrammar.h - * - * Created on: Nov 17, 2013 - * Author: martin - */ - -#ifndef CONTEXTSENSITIVEGRAMMAR_H_ -#define CONTEXTSENSITIVEGRAMMAR_H_ - -#include "../Grammar.h" - -namespace grammar { - -/** - * Context-sensitive grammar. Type 1 in Chomsky hierarchy. - */ -class ContextSensitiveGrammar: public Grammar { -private: - /** - * Checks that list of symbols specified by prefixStart and prefixEnd on the left side - * is also present on the start of right side of the Rule. - * @param prefixStart iterator pointing to start of the left side prefix - * @param prefixEnd iterator pointing after last symbol of the prefix on the left side - * @param rightSide iterator pointing to the first symbol on the right side of the rule - * @return true when start of the left side of the rule matches start on the right side of the rule - */ - bool startsWith(list<Symbol>::const_iterator prefixStart, list<Symbol>::const_iterator prefixEnd, list<Symbol>::const_iterator rightSide ) const; - - /** - * Checks that list of symbols specified by suffixStart and suffixEnd on the left side - * is also present at the end of right side of the Rule. - * @param suffixStart iterator pointing to the start of left side suffix - * @param suffixEnd iterator pointing after the last symbol of the left side of the rule - * @param rightSide iterator pointing after the last symbol of the rigt side of the rule - */ - bool endsWith(list<Symbol>::const_iterator suffixStart, list<Symbol>::const_iterator suffixEnd, list<Symbol>::const_iterator rightSideEnd ) const; -protected: - /** - * @copydoc Grammar::isValidRule(const Rule&) const - */ - bool isValidRule(const Rule& rule) const; -public: - ContextSensitiveGrammar(); - ContextSensitiveGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols, const Symbol& startSymbol); -}; - -} /* namespace grammar */ -#endif /* CONTEXTSENSITIVEGRAMMAR_H_ */ diff --git a/alib/src/grammar/Grammar.cpp b/alib/src/grammar/Grammar.cpp deleted file mode 100644 index 04f87c9e48af2842ef63c25df24dcd1a305b8816..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Grammar.cpp +++ /dev/null @@ -1,171 +0,0 @@ -/* - * Grammar.cpp - * - * Created on: Nov 2, 2013 - * Author: Martin Zak - */ - -#include "Grammar.h" -#include "GrammarException.h" -#include "GrammarPrinter.h" - -namespace grammar { - -using namespace std; -using namespace alphabet; - -Grammar::Grammar() : startSymbol(""){ - -} - -Grammar::Grammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols, const Symbol& startSymbol) : startSymbol(startSymbol) { - this->nonTerminalSymbols = nonTerminalSymbols; - this->terminalSymbols = terminalSymbols; -} - -Grammar::~Grammar() { -} - -void Grammar::addTerminalSymbol(const Symbol& symbol) { - if(nonTerminalSymbols.find(symbol) != nonTerminalSymbols.end()){ - throw GrammarException("Symbol \"" + symbol.getSymbol() + "\" is nonterminal symbol."); - } - - if(!terminalSymbols.insert(symbol).second) { - throw GrammarException("Terminal symbol \"" + symbol.getSymbol() + "\" already exists."); - } -} - -void Grammar::removeTerminalSymbol(const Symbol& symbol) { - for(set<Rule>::iterator it = rules.begin(); it != rules.end(); it++){ - if(it->containsSymbol(symbol)) { - throw GrammarException("Symbol \"" + symbol.getSymbol() + "\" is used in rule."); - } - } - - if(!terminalSymbols.erase(symbol)) { - throw GrammarException("Symbol \"" + symbol.getSymbol() + "\" isn't terminal symbol."); - } -} - -const set<Symbol>& Grammar::getTerminalSymbols() const { - return terminalSymbols; -} - -void Grammar::addNonTerminalSymbol(const Symbol& symbol) { - if(terminalSymbols.find(symbol) != terminalSymbols.end()){ - throw GrammarException("Symbol \"" + symbol.getSymbol() + "\" is terminal symbol."); - } - - if(!nonTerminalSymbols.insert(symbol).second) { - throw GrammarException("Nonterminal symbol \"" + symbol.getSymbol() + "\" already exists."); - } -} - -void Grammar::removeNonTerminalSymbol(const Symbol& symbol) { - for(set<Rule>::iterator it = rules.begin(); it != rules.end(); it++){ - if(it->containsSymbol(symbol)) { - throw GrammarException("Symbol \"" + symbol.getSymbol() + "\" is used in rule."); - } - } - - if(!nonTerminalSymbols.erase(symbol)) { - throw GrammarException("Symbol \"" + symbol.getSymbol() + "\" isn't nonterminal symbol."); - } -} - -const set<Symbol>& Grammar::getNonTerminalSymbols() const { - return nonTerminalSymbols; -} - -void Grammar::addRule(const Rule& rule) { - if (!isValidRule(rule)) { - throw GrammarException("Rule is not valid for this grammar. " + rule.toString()); - } - - if (!rules.insert(rule).second) { - throw GrammarException("Rule already exists. " + rule.toString()); - } -} - -void Grammar::removeRule(const Rule& rule) { - if(!rules.erase(rule)) { - throw GrammarException("Rule doesn't exists. " + rule.toString()); - } -} - -const set<Rule>& Grammar::getRules() const { - return rules; -} - -const Symbol& Grammar::getStartSymbol() const{ - return startSymbol; -} - -void Grammar::setStartSymbol(const Symbol& symbol) { - if(nonTerminalSymbols.find(symbol) == nonTerminalSymbols.end()) { - throw GrammarException("Symbol \"" + symbol.getSymbol() + "\" isn't nonterminal symbol."); - } - - startSymbol = symbol; -} - -const Symbol& Grammar::createUniqueNonTerminalSymbol(const string& name, bool integerSuffix){ - try{ - Symbol uniqueSymbol(name); - addNonTerminalSymbol(uniqueSymbol); - return *nonTerminalSymbols.find(uniqueSymbol); - } - catch(GrammarException& e){ - } - - int i = 0; - string str = name; - - while(i < INT_MAX) - { - if(integerSuffix) - str = name + std::to_string(i++); - else - str += '\''; - - Symbol uniqueSymbol(str); - try{ - addNonTerminalSymbol(uniqueSymbol); - } - catch(GrammarException& e){ - continue; - } - - return *nonTerminalSymbols.find(uniqueSymbol); - } - - throw GrammarException("Could not create unique state with name " + name + "." ); -} - -bool Grammar::isNonTerminalOnRightSideOfAnyRule(const Symbol& symbol) const -{ - if(nonTerminalSymbols.find(symbol) == nonTerminalSymbols.end()) { - throw GrammarException("Symbol \"" + symbol.getSymbol() + "\" isn't nonterminal symbol."); - } - - for(const auto & rule : rules ) - { - const auto & rs = rule.getRightSide(); - if(find(rs.begin(), rs.end(), symbol) != rs.end()) - return true; - } - - return false; -} - -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 deleted file mode 100644 index a8fb680c29a2a7ee770c75baef2d6b0add313e01..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Grammar.h +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Grammar.h - * - * Created on: Nov 2, 2013 - * Author: Martin Zak - */ - -#ifndef GRAMMAR_H_ -#define GRAMMAR_H_ - -#include <climits> -#include <set> -#include <list> -#include <algorithm> - -#include "../alphabet/Symbol.h" -#include "Rule.h" - -namespace grammar { - -using namespace std; -using namespace alphabet; - -/** - * Abstract class representing grammar. Implements methods for manipulation - * with terminal and nonterminal symbols. Only method, which is not implemented - * is grammar specific isValidRule. - */ -class Grammar { -protected: - set<Symbol> terminalSymbols; - set<Symbol> nonTerminalSymbols; - set<Rule> rules; - Symbol startSymbol; - - /** - * Checks that rule has valid form for this grammar. - * @param rule Rule to check - * @return true when rule is valid, false when not - */ - virtual bool isValidRule(const Rule& rule) const = 0; -public: - Grammar(); - Grammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols, const Symbol& startSymbol); - virtual ~Grammar(); - - /** - * Adds terminal symbol to the grammar. - * @param symbol Symbol to add - * @throws GrammarException when symbol is already terminal or nonterminal symbol - */ - void addTerminalSymbol(const Symbol& symbol); - - /** - * Removes terminal symbol from the grammar. - * @param symbol Symbol to remove - * @throws GrammarException when symbol is used in rule or isn't terminal symbol - */ - void removeTerminalSymbol(const Symbol& symbol); - - /** - * @return set of terminal symbols - */ - const set<Symbol>& getTerminalSymbols() const; - - /** - * Adds nonterminal symbol to the grammar. - * @param symbol Symbol to add - * @throws GrammarException when symbol is already terminal or nonterminal symbol - */ - void addNonTerminalSymbol(const Symbol& symbol); - - /** - * Removes nonterminal symbol from the grammar. - * @param symbol Symbol to remove - * @throws GrammarException when symbol is used in rule or isn't nonterminal symbol - */ - void removeNonTerminalSymbol(const Symbol& symbol); - - /** - * @return set of nonterminal symbols - */ - const set<Symbol>& getNonTerminalSymbols() const; - - /** - * Adds Rule to the grammar. Perform checks that rule is valid for this grammar. - * @param rule Rule to add - * @throws GrammarException when Rule is not valid for this grammar or when rule already exists - */ - void addRule(const Rule& rule); - - /** - * Removes Rule from the grammar. - * @param rule Rule to remove - * @throws GrammarException when Rule doesn't exist - */ - void removeRule(const Rule& rule); - - /** - * @return set of Rules - */ - const set<Rule>& getRules() const; - - /** - * @return start symbol - */ - const Symbol& getStartSymbol() const; - - /** - * Sets the start symbol. Checks that symbol is nonterminal symbol. - * @param symbol nonterminal Symbol - * @throws GrammarException when symbol isn't nonterminal symbol - */ - void setStartSymbol(const Symbol& symbol); - - /** - * Creates and adds unique state to grammar. If given state name is - * already used, appends apostrophe or integer suffix - * @param name name of the state - * @throws AutomatonException if symbol could not be created - * @return created symbol - */ - const Symbol& createUniqueNonTerminalSymbol(const std::string& name, bool integerSuffix = false); - - /** - * @param symbol nonterminal - * @return true if given nonterminal is on right side of any rule - * @throws GrammarException if symbol is not nonterminal - */ - bool isNonTerminalOnRightSideOfAnyRule( const Symbol& symbol ) const; - /** - * Prints the XML representation of grammar to the given output stream. - * @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 */ -#endif /* GRAMMAR_H_ */ diff --git a/alib/src/grammar/GrammarException.cpp b/alib/src/grammar/GrammarException.cpp deleted file mode 100644 index ad6a5f107b909d11acbe44a404158c35a6af58c6..0000000000000000000000000000000000000000 --- a/alib/src/grammar/GrammarException.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/* - * GrammarException.cpp - * - * Created on: Nov 3, 2013 - * Author: martin - */ - -#include "GrammarException.h" - -namespace grammar { - -using namespace std; - -GrammarException::GrammarException() { -} - -GrammarException::GrammarException(const string& cause) : - AlibException(cause) { -} - -GrammarException::~GrammarException() throw () { - -} - -} /* namespace grammar */ diff --git a/alib/src/grammar/GrammarException.h b/alib/src/grammar/GrammarException.h deleted file mode 100644 index c4188024a83467bd4c582ef546791806d33b972e..0000000000000000000000000000000000000000 --- a/alib/src/grammar/GrammarException.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * GrammarException.h - * - * Created on: Nov 3, 2013 - * Author: martin - */ - -#ifndef GRAMMAREXCEPTION_H_ -#define GRAMMAREXCEPTION_H_ - -#include "../AlibException.h" - -namespace grammar { - -using namespace std; - -/** - * Exception thrown by grammar types when something goes wrong. - */ -class GrammarException: public alib::AlibException { -public: - GrammarException(); - GrammarException(const string& cause); - virtual ~GrammarException() throw (); -}; - -} /* namespace grammar */ -#endif /* GRAMMAREXCEPTION_H_ */ diff --git a/alib/src/grammar/GrammarParser.cpp b/alib/src/grammar/GrammarParser.cpp deleted file mode 100644 index 2067dea78f1b358bae1e70a62d43c0d687846986..0000000000000000000000000000000000000000 --- a/alib/src/grammar/GrammarParser.cpp +++ /dev/null @@ -1,121 +0,0 @@ -/* - * GrammarParser.cpp - * - * Created on: Nov 3, 2013 - * Author: martin - */ - -#include "GrammarParser.h" - -#include "Grammar.h" -#include "../sax/ParserException.h" - -namespace grammar { - -UnknownGrammar GrammarParser::parse(list<Token>& input) { - UnknownGrammar grammar; - popToken(input, Token::START_ELEMENT, "grammar"); - - while (true) { - if (isToken(input, Token::END_ELEMENT, "grammar")) { - break; - } else if (isToken(input, Token::START_ELEMENT, "nonTerminalSymbols")) { - parseNonTerminalSymbols(input, grammar); - } else if (isToken(input, Token::START_ELEMENT, "terminalSymbols")) { - parseTerminalSymbols(input, grammar); - } else if (isToken(input, Token::START_ELEMENT, "rules")) { - parseRules(input, grammar); - } else if (isToken(input, Token::START_ELEMENT, "startSymbol")) { - parseStartSymbol(input, grammar); - } else { - throw ParserException(Token("grammar", Token::END_ELEMENT), input.front()); - } - } - - popToken(input, Token::END_ELEMENT, "grammar"); - - return grammar; -} - -void GrammarParser::parseNonTerminalSymbols(list<Token>& input, Grammar& grammar) { - popToken(input, Token::START_ELEMENT, "nonTerminalSymbols"); - - while (isToken(input, Token::START_ELEMENT, "symbol")) { - grammar.addNonTerminalSymbol(parseSymbol(input)); - } - - popToken(input, Token::END_ELEMENT, "nonTerminalSymbols"); -} - -void GrammarParser::parseTerminalSymbols(list<Token>& input, Grammar& grammar) { - popToken(input, Token::START_ELEMENT, "terminalSymbols"); - - while (isToken(input, Token::START_ELEMENT, "symbol")) { - grammar.addTerminalSymbol(parseSymbol(input)); - } - - popToken(input, Token::END_ELEMENT, "terminalSymbols"); -} - -void GrammarParser::parseRules(list<Token>& input, Grammar& grammar) { - popToken(input, Token::START_ELEMENT, "rules"); - while (isToken(input, Token::START_ELEMENT, "rule")) { - grammar.addRule(parseRule(input)); - } - popToken(input, Token::END_ELEMENT, "rules"); -} - -Rule GrammarParser::parseRule(list<Token>& input) { - popToken(input, Token::START_ELEMENT, "rule"); - - Rule rule; - - rule.setLeftSide(parseSymbolList(input, "leftSide")); - rule.setRightSide(parseSymbolList(input, "rightSide")); - - popToken(input, Token::END_ELEMENT, "rule"); - return rule; -} - -void GrammarParser::parseStartSymbol(list<Token>& input, Grammar& grammar) { - grammar.setStartSymbol(parseSymbol(input, "startSymbol")); -} - -Symbol GrammarParser::parseSymbol(list<Token>& input, string tagName) { - popToken(input, Token::START_ELEMENT, tagName); - - if (input.front().getType() == Token::CHARACTER) { - Symbol symbol(input.front().getData()); - input.pop_front(); - popToken(input, Token::END_ELEMENT, tagName); - return symbol; - } else { - throw ParserException(Token("", Token::CHARACTER), input.front()); - } -} - -list<Symbol> GrammarParser::parseSymbolList(list<Token>& input, string tagName) { - popToken(input,Token::START_ELEMENT, tagName); - - list<Symbol> symbols; - while(isToken(input, Token::START_ELEMENT, "symbol")) { - symbols.push_back(parseSymbol(input)); - } - - popToken(input,Token::END_ELEMENT, tagName); - return symbols; -} - -bool GrammarParser::isToken(list<Token>& input, Token::TokenType type, string data) { - return input.front().getType() == type && input.front().getData() == data; -} - -void GrammarParser::popToken(list<Token>& input, Token::TokenType type, string data) { - if (isToken(input, type, data)) { - input.pop_front(); - } else { - throw ParserException(Token(data, type), input.front()); - } -} - -} /* namespace grammar */ diff --git a/alib/src/grammar/GrammarParser.h b/alib/src/grammar/GrammarParser.h deleted file mode 100644 index 4d60a2ceb45c6baa0ffea7fff4bdba92d5ae23c1..0000000000000000000000000000000000000000 --- a/alib/src/grammar/GrammarParser.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * GrammarParser.h - * - * Created on: Nov 3, 2013 - * Author: martin - */ - -#ifndef GRAMMARPARSER_H_ -#define GRAMMARPARSER_H_ - -#include <list> -#include <string> -#include "UnknownGrammar.h" -#include "Rule.h" -#include "../sax/Token.h" - -namespace grammar { - -using namespace std; -using namespace sax; - -/** - * Parser used to get UnknownGrammar from XML parsed into list of Tokens. - */ -class GrammarParser { -protected: - static void parseNonTerminalSymbols(list<Token>& input, Grammar& grammar); - static void parseTerminalSymbols(list<Token>& input, Grammar& grammar); - static void parseRules(list<Token>& input, Grammar& grammar); - static Rule parseRule(list<Token>& input); - - static void parseStartSymbol(list<Token>& input, Grammar& grammar); - - static Symbol parseSymbol(list<Token> &input, string tagName = "symbol"); - static list<Symbol> parseSymbolList(list<Token>& input, string tagName); - - static bool isToken(list<Token> &input, Token::TokenType type, string data); - static void popToken(list<Token> &input, Token::TokenType type, string data); -public: - /** - * Parses the Token list and returns the grammar. The input is destroyed in the process. - * @param input XML represented as list of Tokens - * @return UnknownGrammar - * @throws ParserException when an error occurs - */ - static UnknownGrammar parse(list<Token>& input); -}; - -} /* namespace grammar */ -#endif /* GRAMMARPARSER_H_ */ diff --git a/alib/src/grammar/GrammarPrinter.cpp b/alib/src/grammar/GrammarPrinter.cpp deleted file mode 100644 index 0dea544f060dea4cbc3dfe61d9b60ea5176d3cc2..0000000000000000000000000000000000000000 --- a/alib/src/grammar/GrammarPrinter.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - * GrammarPrinter.cpp - * - * Created on: Nov 5, 2013 - * Author: martin - */ - -#include "GrammarPrinter.h" - -namespace grammar { - -const string GrammarPrinter::INDENTATION = "\t"; - -void GrammarPrinter::toXML(const Grammar& grammar, ostream& out) { - out << "<grammar>\n"; - printNonTerminalSymbols(grammar, out, INDENTATION); - printTerminalSymbols(grammar, out, INDENTATION); - printRules(grammar, out, INDENTATION); - printSymbol(grammar.getStartSymbol(), out, "startSymbol", INDENTATION); - out << "</grammar>\n"; -} - -void GrammarPrinter::printNonTerminalSymbols(const Grammar& grammar, ostream& out, string prefix) { - out << prefix << "<nonTerminalSymbols>\n"; - for (auto &symbol : grammar.getNonTerminalSymbols()) { - printSymbol(symbol, out, "symbol", prefix + INDENTATION); - } - out << prefix << "</nonTerminalSymbols>\n"; -} - -void GrammarPrinter::printTerminalSymbols(const Grammar& grammar, ostream& out, string prefix) { - out << prefix << "<terminalSymbols>\n"; - for (auto& symbol : grammar.getTerminalSymbols()) { - printSymbol(symbol, out, "symbol", prefix + INDENTATION); - } - out << prefix << "</terminalSymbols>\n"; -} - -void GrammarPrinter::printRules(const Grammar& grammar, ostream& out, string prefix) { - out << prefix << "<rules>\n"; - for (auto& rule : grammar.getRules()) { - printRule(rule, out, prefix + INDENTATION); - } - out << prefix << "</rules>\n"; -} - -void GrammarPrinter::printRule(const Rule& rule, ostream& out, string prefix) { - out << prefix << "<rule>\n"; - out << prefix << INDENTATION << "<leftSide>\n"; - for (auto& symbol : rule.getLeftSide()) { - printSymbol(symbol, out, "symbol", prefix + INDENTATION + INDENTATION); - } - out << prefix << INDENTATION << "</leftSide>\n"; - out << prefix << INDENTATION << "<rightSide>\n"; - for (auto& symbol : rule.getRightSide()) { - printSymbol(symbol, out, "symbol", prefix + INDENTATION + INDENTATION); - } - out << prefix << INDENTATION << "</rightSide>\n"; - out << prefix << "</rule>\n"; -} - -void GrammarPrinter::printSymbol(const Symbol& symbol, ostream& out, string tagName, string prefix) { - out << prefix << "<" << tagName << ">"; - out << symbol.getSymbol(); - out << "</" << tagName << ">\n"; -} - -} /* namespace grammar */ diff --git a/alib/src/grammar/GrammarPrinter.h b/alib/src/grammar/GrammarPrinter.h deleted file mode 100644 index ead6c6d373ca6b9a73340b05e9d358e93b750279..0000000000000000000000000000000000000000 --- a/alib/src/grammar/GrammarPrinter.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * GrammarPrinter.h - * - * Created on: Nov 5, 2013 - * Author: martin - */ - -#ifndef GRAMMARPRINTER_H_ -#define GRAMMARPRINTER_H_ - -#include <ostream> -#include "Grammar.h" - -namespace grammar { - -using namespace std; - -/** - * This class contains methods to print XML representation of grammars to the output stream. - */ -class GrammarPrinter { -protected: - static const string INDENTATION; - - static void printNonTerminalSymbols(const Grammar& grammar, ostream& out, string prefix); - static void printTerminalSymbols(const Grammar& grammar, ostream& out, string prefix); - static void printRules(const Grammar& grammar, ostream& out, string prefix); - static void printRule(const Rule& rule, ostream& out, string prefix); - static void printSymbol(const Symbol& symbol, ostream& out, string tagName, string prefix); -public: - /** - * Prints XML representation of Grammar to the output stream. - * @param grammar Grammar to print - * @param out output stream to which print the Grammar - */ - static void toXML(const Grammar& grammar, ostream& out); -}; - -} /* namespace grammar */ -#endif /* GRAMMARPRINTER_H_ */ diff --git a/alib/src/grammar/Linear/LeftLinearGrammar.cpp b/alib/src/grammar/Linear/LeftLinearGrammar.cpp deleted file mode 100644 index 325b652b339bcb832871d6a8424b6b8db4a08c79..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Linear/LeftLinearGrammar.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* - * LeftLinearGrammar.cpp - * - * Created on: Nov 23, 2013 - * Author: Martin Zak - */ - -#include "LeftLinearGrammar.h" - -namespace grammar { - -LeftLinearGrammar::LeftLinearGrammar() : - LinearGrammar() { -} - -LeftLinearGrammar::LeftLinearGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols, - const Symbol& startSymbol) : - LinearGrammar(nonTerminalSymbols, terminalSymbols, startSymbol) { -} - -bool LeftLinearGrammar::isValidRule(const Rule& rule) const { - return checkLeftSide(rule.getLeftSide()) && checkRightSide(rule.getRightSide()); -} - -bool LeftLinearGrammar::checkLeftSide(const list<Symbol>& leftSide) const { - if (leftSide.size() != 1) { - return false; - } - - if (nonTerminalSymbols.find(leftSide.front()) == nonTerminalSymbols.end()) { - return false; - } - - return true; -} - -bool LeftLinearGrammar::checkRightSide(const list<Symbol>& rightSide) const { - if (rightSide.size() == 0) { - return true; - } else if (rightSide.size() == 1) { - const Symbol& symbol = rightSide.front(); - - //check epsilon - if (symbol.getSymbol() == "") { - return true; - } - - //check that symbol exists - return ((terminalSymbols.find(symbol) != terminalSymbols.end()) - || (nonTerminalSymbols.find(symbol) != nonTerminalSymbols.end())); - } else { - //check if first symbol is nonterminal - list<Symbol>::const_iterator symbol = rightSide.begin(); - if (nonTerminalSymbols.find(*symbol) == nonTerminalSymbols.end()) { - return false; - } - - symbol++; - - //check if following symbols are terminal - for (; symbol != rightSide.end(); symbol++) { - if (terminalSymbols.find(*symbol) == terminalSymbols.end()) { - return false; - } - } - - return true; - } -} - -} /* namespace grammar */ diff --git a/alib/src/grammar/Linear/LeftLinearGrammar.h b/alib/src/grammar/Linear/LeftLinearGrammar.h deleted file mode 100644 index c81f09d022102b2ae1d1cd9dcd78d4f44f6c4c5a..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Linear/LeftLinearGrammar.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * LeftLinearGrammar.h - * - * Created on: Nov 23, 2013 - * Author: Martin Zak - */ - -#ifndef LEFTLINEARGRAMMAR_H_ -#define LEFTLINEARGRAMMAR_H_ - -#include "LinearGrammar.h" - -namespace grammar { - -/** - * Left linear grammar. Produces regular language. - */ -class LeftLinearGrammar: public LinearGrammar { -private: - /** - * Checks that left side of the Rule contains one symbol which is nonterminal. - * @param leftSide left side of the Rule - * @return true when left side contains one nonterminal symbol, false otherwise - */ - bool checkLeftSide(const list<Symbol>& leftSide) const; - - /** - * Checks that right side of the Rule contains no symbols, one terminal symbol - * or nonterminal symbol followed by zero or more terminal symbols. - * @param rightSide of the Rule - * @return true when right side is valid, false otherwise - */ - bool checkRightSide(const list<Symbol>& rightSide) const; -protected: - /** - * @copydoc Grammar::isValidRule(const Rule&) const - */ - bool isValidRule(const Rule& rule) const; -public: - LeftLinearGrammar(); - LeftLinearGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols, - const Symbol& startSymbol); -}; - -} /* namespace grammar */ -#endif /* LEFTLINEARGRAMMAR_H_ */ diff --git a/alib/src/grammar/Linear/LinearGrammar.cpp b/alib/src/grammar/Linear/LinearGrammar.cpp deleted file mode 100644 index 21e534224ed168c1df1991cbb3615e2896bb28db..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Linear/LinearGrammar.cpp +++ /dev/null @@ -1,20 +0,0 @@ -/* - * 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 deleted file mode 100644 index 9c3a95f736a680a7a80ae74b666ea0791dff08e8..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Linear/LinearGrammar.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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/Linear/RightLinearGrammar.cpp b/alib/src/grammar/Linear/RightLinearGrammar.cpp deleted file mode 100644 index 10d34e1fbf4a28d6288df618723c1d183e7f68b3..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Linear/RightLinearGrammar.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/* - * RightLinearGrammar.cpp - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#include "RightLinearGrammar.h" - -namespace grammar { - -RightLinearGrammar::RightLinearGrammar() : - LinearGrammar() { -} - -RightLinearGrammar::RightLinearGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols, - const Symbol& startSymbol) : - LinearGrammar(nonTerminalSymbols, terminalSymbols, startSymbol) { -} - -bool RightLinearGrammar::isValidRule(const Rule& rule) const { - return checkLeftSide(rule.getLeftSide()) && checkRightSide(rule.getRightSide()); -} - -bool RightLinearGrammar::checkLeftSide(const list<Symbol>& leftSide) const { - if (leftSide.size() != 1) { - return false; - } - - if (nonTerminalSymbols.find(leftSide.front()) == nonTerminalSymbols.end()) { - return false; - } - - return true; -} - -bool RightLinearGrammar::checkRightSide(const list<Symbol>& rightSide) const { - if (rightSide.size() == 0) { - return true; - } else if (rightSide.size() == 1) { - const Symbol& symbol = rightSide.front(); - - //check epsilon - if (symbol.getSymbol() == "") { - return true; - } - - //check that symbol exists - return ((terminalSymbols.find(symbol) != terminalSymbols.end()) - || (nonTerminalSymbols.find(symbol) != nonTerminalSymbols.end())); - - } else { - //check if all symbols except the last one are terminal symbols - for (list<Symbol>::const_iterator symbol = rightSide.begin(); symbol != --rightSide.end(); symbol++) { - if (terminalSymbols.find(*symbol) == terminalSymbols.end()) { - return false; - } - } - - //check if last symbol is nonterminal - const Symbol& lastSymbol = *(--rightSide.end()); - if (nonTerminalSymbols.find(lastSymbol) == nonTerminalSymbols.end()) { - return false; - } - - return true; - } -} - -} /* namespace grammar */ diff --git a/alib/src/grammar/Linear/RightLinearGrammar.h b/alib/src/grammar/Linear/RightLinearGrammar.h deleted file mode 100644 index 8397ece746b750102ad70d73c1a5b7f6f75f4969..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Linear/RightLinearGrammar.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * RightLinearGrammar.h - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#ifndef RIGHTLINEARGRAMMAR_H_ -#define RIGHTLINEARGRAMMAR_H_ - -#include "LinearGrammar.h" - -namespace grammar { - -/** - * Right regular grammar. Produces regular language. - */ -class RightLinearGrammar: public LinearGrammar { -private: - /** - * Checks that left side of the Rule contains one symbol which is nonterminal. - * @param leftSide left side of the Rule - * @return true when left side contains one nonterminal symbol, false otherwise - */ - bool checkLeftSide(const list<Symbol>& leftSide) const; - - /** - * Checks that right side of the Rule contains no symbols, one terminal symbol - * or zero or more terminal symbols followed by one nonterminal symbol. - * @param rightSide of the Rule - * @return true when right side is valid, false otherwise - */ - bool checkRightSide(const list<Symbol>& rightSide) const; -protected: - /** - * @copydoc Grammar::isValidRule(const Rule&) const - */ - bool isValidRule(const Rule& rule) const; -public: - RightLinearGrammar(); - RightLinearGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols, - const Symbol& startSymbol); -}; - -} /* namespace grammar */ -#endif /* RIGHTLINEARGRAMMAR_H_ */ diff --git a/alib/src/grammar/Regular/LeftRegularGrammar.cpp b/alib/src/grammar/Regular/LeftRegularGrammar.cpp deleted file mode 100644 index 17a6d471a51675ca7a00b10ec69618db8cd31e79..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Regular/LeftRegularGrammar.cpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - * LeftRegularGrammar.cpp - * - * Created on: Nov 17, 2013 - * Author: martin - */ - -#include "LeftRegularGrammar.h" - -namespace grammar { - -LeftRegularGrammar::LeftRegularGrammar() : - RegularGrammar() { -} - -LeftRegularGrammar::LeftRegularGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols, - const Symbol& startSymbol) : - RegularGrammar(nonTerminalSymbols, terminalSymbols, startSymbol) { -} - -bool LeftRegularGrammar::isValidRule(const Rule& rule) const { - if (isEpsilonRule(rule)) { - return checkEpsilonRule(rule.getLeftSide(), rule.getRightSide()); - } else { - return checkLeftSide(rule.getLeftSide()) && checkRightSide(rule.getRightSide()); - } -} - -bool LeftRegularGrammar::checkLeftSide(const list<Symbol>& leftSide) const { - if (leftSide.size() != 1) { - return false; - } - - if (nonTerminalSymbols.find(leftSide.front()) == nonTerminalSymbols.end()) { - return false; - } - - return true; -} - -bool LeftRegularGrammar::checkRightSide(const list<Symbol>& rightSide) const { - if (rightSide.size() == 1) { - return (terminalSymbols.find(rightSide.front()) != terminalSymbols.end()); - } else if (rightSide.size() == 2) { - - //cannot add rule that contains start symbol on the right side when grammar already contains epsilon rule - if (rightSide.front() == startSymbol && containsEpsilonRule()) { - return false; - } - - //check if first symbol is nonterminal - if (nonTerminalSymbols.find(rightSide.front()) == nonTerminalSymbols.end()) { - return false; - } - - //check if second symbol is terminal - if (terminalSymbols.find(rightSide.back()) == terminalSymbols.end()) { - return false; - } - return true; - - } else { - return false; - } -} - -} /* namespace grammar */ diff --git a/alib/src/grammar/Regular/LeftRegularGrammar.h b/alib/src/grammar/Regular/LeftRegularGrammar.h deleted file mode 100644 index 0f3ba4d99895319c9d776e3b2689c6b9c2b0c69c..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Regular/LeftRegularGrammar.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * LeftRegularGrammar.h - * - * Created on: Nov 17, 2013 - * Author: martin - */ - -#ifndef LEFTREGULARGRAMMAR_H_ -#define LEFTREGULARGRAMMAR_H_ - -#include "RegularGrammar.h" - -namespace grammar { - -/** - * Left regular grammar. Produces regular language. - */ -class LeftRegularGrammar: public RegularGrammar { -private: - /** - * Checks that left side contains one symbol which is nonterminal. - * @param leftSide left side of the Rule - * @return true when left side contains one symbol which is nonterminal, false otherwise - */ - bool checkLeftSide(const list<Symbol>& leftSide) const; - - /** - * Checks that right side of the rule contains one terminal symbol - * or nonterminal symbol followed by terminal symbol. Epsilon rules are checked - * in RegularGrammar class. - * @param rightSide right side of the Rule - * @return true when right side of the rule is valid, false otherwise - */ - bool checkRightSide(const list<Symbol>& rightSide) const; -protected: - /** - * @copydoc Grammar::isValidRule(const Rule&) const - */ - bool isValidRule(const Rule& rule) const; -public: - LeftRegularGrammar(); - LeftRegularGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols, - const Symbol& startSymbol); -}; - -} /* namespace grammar */ -#endif /* LEFTREGULARGRAMMAR_H_ */ diff --git a/alib/src/grammar/Regular/RegularGrammar.cpp b/alib/src/grammar/Regular/RegularGrammar.cpp deleted file mode 100644 index cfaccd4e06350f78c9a2180a8c8336489afcc8f3..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Regular/RegularGrammar.cpp +++ /dev/null @@ -1,52 +0,0 @@ -/* - * RegularGrammar.cpp - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#include "RegularGrammar.h" - -namespace grammar { - -RegularGrammar::RegularGrammar() : - Grammar() { -} - -RegularGrammar::RegularGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols, - const Symbol& startSymbol) : - Grammar(nonTerminalSymbols, terminalSymbols, startSymbol) { -} - -bool RegularGrammar::checkEpsilonRule(const list<Symbol>& leftSide, const list<Symbol>& rightSide) const { - if (leftSide.size() != 1 || leftSide.front() != startSymbol) { - return false; - } - - //startSymbol cannot be present on the right side of any rule - for (const auto& rule : rules) { - for (const auto& symbol : rule.getRightSide()) { - if (symbol == startSymbol) { - return false; - } - } - } - return true; -} - -bool RegularGrammar::isEpsilonRule(const Rule& rule) const { - return rule.getRightSide().size() == 0; -} - -bool RegularGrammar::containsEpsilonRule() const { - list<Symbol> leftSide; - leftSide.push_back(startSymbol); - list<Symbol> rightSide; - - Rule epsilonRule(leftSide, rightSide); - - return rules.find(epsilonRule) != rules.end(); -} - -} /* namespace grammar */ - diff --git a/alib/src/grammar/Regular/RegularGrammar.h b/alib/src/grammar/Regular/RegularGrammar.h deleted file mode 100644 index 21dc89f2afc8199529070b49a4a838c1243f40ce..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Regular/RegularGrammar.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * RegularGrammar.h - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#ifndef REGULARGRAMMAR_H_ -#define REGULARGRAMMAR_H_ - -#include "../Grammar.h" - -namespace grammar { - -/** - * Abstract class for regular grammars. - */ -class RegularGrammar: public Grammar { -public: - /** - * Checks if the Rule is epsilon rule (has empty right side). - * @param rule Rule to check - * @return true when Rule is epsilon rule, false otherwise - */ - bool isEpsilonRule(const Rule& rule) const; - -protected: - /** - * Checks that epsilon rule (rule that has empty right side) has start symbol - * on the left side and that start symbol is not present on the right side of any other rule. - * @param leftSide left side of the Rule - * @param rightSide right side of the Rule - * @return true when Rule is valid epsilon rule, false otherwise - */ - bool checkEpsilonRule(const list<Symbol>& leftSide, const list<Symbol>& rightSide) const; - - - /** - * Checks if Grammar contains epsilon rule. Used when adding - * rule that has start symbol on the right side of the rule. - * @return true when grammar is epsilon free (doesn't contain epsilon rules), false otherwise - */ - bool containsEpsilonRule() const; -public: - RegularGrammar(); - RegularGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols, - const Symbol& startSymbol); - -}; - -} /* namespace grammar */ -#endif /* REGULARGRAMMAR_H_ */ diff --git a/alib/src/grammar/Regular/RightRegularGrammar.cpp b/alib/src/grammar/Regular/RightRegularGrammar.cpp deleted file mode 100644 index 6d6d11d294e23b8b332302a1169717bde10cc845..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Regular/RightRegularGrammar.cpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - * RightRegularGrammar.cpp - * - * Created on: Nov 10, 2013 - * Author: martin - */ - -#include "RightRegularGrammar.h" - -namespace grammar { - -RightRegularGrammar::RightRegularGrammar() : - RegularGrammar() { -} - -RightRegularGrammar::RightRegularGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols, - const Symbol& startSymbol) : - RegularGrammar(nonTerminalSymbols, terminalSymbols, startSymbol) { -} - -bool RightRegularGrammar::isValidRule(const Rule& rule) const { - if (isEpsilonRule(rule)) { - return checkEpsilonRule(rule.getLeftSide(), rule.getRightSide()); - } else { - return checkLeftSide(rule.getLeftSide()) && checkRightSide(rule.getRightSide()); - } -} - -bool RightRegularGrammar::checkLeftSide(const list<Symbol>& leftSide) const { - if (leftSide.size() != 1) { - return false; - } - - if (nonTerminalSymbols.find(leftSide.front()) == nonTerminalSymbols.end()) { - return false; - } - - return true; -} - -bool RightRegularGrammar::checkRightSide(const list<Symbol>& rightSide) const { - if (rightSide.size() == 1) { - return terminalSymbols.find(rightSide.front()) != terminalSymbols.end(); - } else if (rightSide.size() == 2) { - - //cannot add rule that contains start symbol on the right side when grammar already contains epsilon rule - if (rightSide.back() == startSymbol && containsEpsilonRule()) { - return false; - } - - //check if first symbol is terminal - if (terminalSymbols.find(rightSide.front()) == terminalSymbols.end()) { - return false; - } - - //check if second symbol is nonterminal - if (nonTerminalSymbols.find(rightSide.back()) == nonTerminalSymbols.end()) { - return false; - } - return true; - - } else { - return false; - } -} - -} /* namespace grammar */ diff --git a/alib/src/grammar/Regular/RightRegularGrammar.h b/alib/src/grammar/Regular/RightRegularGrammar.h deleted file mode 100644 index 5e81cade7074ac7b8a8306651b827f4fa2f62d5d..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Regular/RightRegularGrammar.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * RightRegularGrammar.h - * - * Created on: Nov 10, 2013 - * Author: martin - */ - -#ifndef RIGHTREGULARGRAMMAR_H_ -#define RIGHTREGULARGRAMMAR_H_ - -#include "RegularGrammar.h" - -namespace grammar { - -/** - * Right regular grammar. Produces regular language. - */ -class RightRegularGrammar: public RegularGrammar { -private: - /** - * Checks that left side contains one symbol which is nonterminal. - * @param leftSide left side of the Rule - * @return true when left side contains one symbol which is nonterminal, false otherwise - */ - bool checkLeftSide(const list<Symbol>& leftSide) const; - - /** - * Checks that right side of the rule contains one terminal symbol - * or terminal symbol followed by nonterminal symbol. Epsilon rules are checked - * in RegularGrammar class. - * @param rightSide right side of the Rule - * @return true when right side of the rule is valid, false otherwise - */ - bool checkRightSide(const list<Symbol>& rightSide) const; -protected: - /** - * @copydoc Grammar::isValidRule(const Rule&) const - */ - bool isValidRule(const Rule& rule) const; -public: - RightRegularGrammar(); - RightRegularGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols, - const Symbol& startSymbol); -}; - -} /* namespace grammar */ -#endif /* RIGHTREGULARGRAMMAR_H_ */ diff --git a/alib/src/grammar/Rule.cpp b/alib/src/grammar/Rule.cpp deleted file mode 100644 index 1abe676b2552ec48a0a4ff49c270bd68eab990e7..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Rule.cpp +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Rule.cpp - * - * Created on: Nov 2, 2013 - * Author: Martin Zak - */ - -#include "Rule.h" -#include <algorithm> - -namespace grammar { - -Rule::Rule() { -} - -Rule::Rule(const list<Symbol>& leftSide, const list<Symbol>& rightSide) : - leftSide(leftSide), rightSide(rightSide) { -} - -void grammar::Rule::setLeftSide(const list<Symbol>& leftSide) { - this->leftSide = leftSide; -} - -void grammar::Rule::setRightSide(const list<Symbol>& rightSide) { - this->rightSide = rightSide; -} - -const list<Symbol>& grammar::Rule::getLeftSide() const { - return leftSide; -} - -const list<Symbol>& grammar::Rule::getRightSide() const { - return rightSide; -} - -bool Rule::containsSymbol(const Symbol& symbol) const { - if (find(leftSide.begin(), leftSide.end(), symbol) != leftSide.end()) { - return true; - } - - if (find(rightSide.begin(), rightSide.end(), symbol) != rightSide.end()) { - return true; - } - - return false; -} - -string Rule::toString() const { - string output; - output += "["; - for(auto const& symbol : leftSide) { - output += " " + symbol.getSymbol(); - } - output += " -> "; - for(auto const& symbol : rightSide) { - output += " " + symbol.getSymbol(); - } - - output += "]"; - return output; -} - -bool Rule::operator <(const Rule& other) const { - if (leftSide.size() != other.leftSide.size()) { - return leftSide.size() < other.leftSide.size(); - } - - if (rightSide.size() != other.rightSide.size()) { - return rightSide.size() < other.rightSide.size(); - } - - list<Symbol>::const_iterator left = leftSide.begin(); - list<Symbol>::const_iterator otherLeft = other.leftSide.begin(); - while (left != leftSide.end()) { - if (*left != *otherLeft) { - return *left < *otherLeft; - } - left++; - otherLeft++; - } - - list<Symbol>::const_iterator right = rightSide.begin(); - list<Symbol>::const_iterator otherRight = other.rightSide.begin(); - while (right != rightSide.end()) { - if (*right != *otherRight) { - return *right < *otherRight; - } - right++; - otherRight++; - } - - return false; -} - -bool Rule::operator ==(const Rule& other) const { - return equal(leftSide.begin(), leftSide.end(), other.leftSide.begin()) - && equal(rightSide.begin(), rightSide.end(), other.rightSide.begin()); -} - -bool Rule::operator !=(const Rule& other) const { - return !equal(leftSide.begin(), leftSide.end(), other.leftSide.begin()) - || !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 deleted file mode 100644 index f708aa71f5ee53a4bea1765754947858431dd08b..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Rule.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Rule.h - * - * Created on: Nov 2, 2013 - * Author: Martin Zak - */ - -#ifndef RULE_H_ -#define RULE_H_ - -#include <list> -#include "../alphabet/Symbol.h" - -namespace grammar { - -using namespace std; -using namespace alphabet; - -/** - * Represents rewrite rule (production) of a Grammar. - */ -class Rule { -private: - list<Symbol> leftSide; - list<Symbol> rightSide; -public: - Rule(); - Rule(const list<Symbol>& leftSide, const list<Symbol>& rightSide); - - /** - * Sets left side of the rule. - * @param leftSide list of symbols to set - */ - void setLeftSide(const list<Symbol>& leftSide); - - /** - * Sets right side of the rule. - * @param rightSide list of symbols to set - */ - void setRightSide(const list<Symbol>& rightSide); - - /** - * @return list of symbols on the left side of the Rule - */ - const list<Symbol>& getLeftSide() const; - - /** - * @return list of symbols on the right side of the Rule - */ - const list<Symbol>& getRightSide() const; - - /** - * Checks that Rule contains given symbol. - * @param symbol Symbol to find - * @return true when Rule contains the symbol, false otherwise - */ - bool containsSymbol(const Symbol& symbol) const; - - /** - * Converts rule into human readable string. - * @return string in format [ leftSide -> rightSide ] - */ - string toString() const; - - 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 */ -#endif /* RULE_H_ */ diff --git a/alib/src/grammar/UnknownGrammar.cpp b/alib/src/grammar/UnknownGrammar.cpp deleted file mode 100644 index ae89e343dc30143c1b638495cb01aedccca6a489..0000000000000000000000000000000000000000 --- a/alib/src/grammar/UnknownGrammar.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * UnknownGrammar.cpp - * - * Created on: Nov 3, 2013 - * Author: martin - */ - -#include "UnknownGrammar.h" -#include "GrammarException.h" - -namespace grammar { - -bool UnknownGrammar::isValidRule(const Rule& rule) const { - for (auto const & symbol : rule.getLeftSide()) { - if (terminalSymbols.find(symbol) == terminalSymbols.end() - && nonTerminalSymbols.find(symbol) == nonTerminalSymbols.end()) { - return false; - } - } - - for (auto const & right : rule.getRightSide()) { - if (terminalSymbols.find(right) == terminalSymbols.end() - && nonTerminalSymbols.find(right) == nonTerminalSymbols.end()) { - return false; - } - } - - return true; -} - -} /* namespace grammar */ - diff --git a/alib/src/grammar/UnknownGrammar.h b/alib/src/grammar/UnknownGrammar.h deleted file mode 100644 index 9bebca1c7e4d644e85740e12991e90dfcfc21da4..0000000000000000000000000000000000000000 --- a/alib/src/grammar/UnknownGrammar.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * UnknownGrammar.h - * - * Created on: Nov 3, 2013 - * Author: martin - */ - -#ifndef UNKNOWNGRAMMAR_H_ -#define UNKNOWNGRAMMAR_H_ - -#include "Grammar.h" - -namespace grammar { - -/** - * Class representing unknown grammar which was parsed from the XML. - */ -class UnknownGrammar: public grammar::Grammar { -protected: - /** - * Checks that symbols in the rule are terminal - * or nonterminal symbols of the grammar. - * @param rule Rule to check - * @return true when rule is valid, false otherwise - */ - bool isValidRule(const Rule& rule) const; -}; - -} /* namespace grammar */ -#endif /* UNKNOWNGRAMMAR_H_ */ diff --git a/alib/src/grammar/Unrestricted/UnrestrictedGrammar.cpp b/alib/src/grammar/Unrestricted/UnrestrictedGrammar.cpp deleted file mode 100644 index 1921df6c39ce3a267d63b34ba09fc3a34189c114..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Unrestricted/UnrestrictedGrammar.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * UnrestrictedGrammar.cpp - * - * Created on: Nov 17, 2013 - * Author: martin - */ - -#include "UnrestrictedGrammar.h" - -namespace grammar { - -UnrestrictedGrammar::UnrestrictedGrammar() : - Grammar() { -} - -UnrestrictedGrammar::UnrestrictedGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols, - const Symbol& startSymbol) : - Grammar(nonTerminalSymbols, terminalSymbols, startSymbol) { -} - -bool UnrestrictedGrammar::isValidRule(const Rule& rule) const { - if (rule.getLeftSide().size() == 0) { - return false; - } - - for (auto const & symbol : rule.getLeftSide()) { - if (terminalSymbols.find(symbol) == terminalSymbols.end() - && nonTerminalSymbols.find(symbol) == nonTerminalSymbols.end()) { - return false; - } - } - - for (auto const & right : rule.getRightSide()) { - if (terminalSymbols.find(right) == terminalSymbols.end() - && nonTerminalSymbols.find(right) == nonTerminalSymbols.end()) { - return false; - } - } - - return true; -} - -} /* namespace grammar */ diff --git a/alib/src/grammar/Unrestricted/UnrestrictedGrammar.h b/alib/src/grammar/Unrestricted/UnrestrictedGrammar.h deleted file mode 100644 index eb9802d07bfa8b6e6aa5b649f3cabe65c8f5dcd9..0000000000000000000000000000000000000000 --- a/alib/src/grammar/Unrestricted/UnrestrictedGrammar.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * UnrestrictedGrammar.h - * - * Created on: Nov 17, 2013 - * Author: martin - */ - -#ifndef UNRESTRICTEDGRAMMAR_H_ -#define UNRESTRICTEDGRAMMAR_H_ - -#include "../Grammar.h" - -namespace grammar { - -/** - * Unrestricted grammar. Type 0 in Chomsky hierarchy. Produces recursively enumerable language. - */ -class UnrestrictedGrammar: public Grammar { -protected: - - /** - * Checks that left side of the rule is not empty and that both sides of the rule contain only - * symbols that are terminal or nonterminal in this grammar. - * @param rule Rule to check - * @return true when rule is valid, false otherwise - */ - bool isValidRule(const Rule& rule) const; -public: - UnrestrictedGrammar(); - UnrestrictedGrammar(const set<Symbol>& nonTerminalSymbols, const set<Symbol>& terminalSymbols, - const Symbol& startSymbol); -}; - -} /* namespace grammar */ -#endif /* UNRESTRICTEDGRAMMAR_H_ */ diff --git a/alib/src/regexp/Alternation.cpp b/alib/src/regexp/Alternation.cpp deleted file mode 100644 index 7a92bf6f651b3e2026cf2af601ae3732ecf10b51..0000000000000000000000000000000000000000 --- a/alib/src/regexp/Alternation.cpp +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Alternation.cpp - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#include "Alternation.h" - -namespace regexp { - -Alternation::Alternation() { -} - -Alternation::Alternation(const Alternation& other) { - for (auto element : other.elements) { - elements.push_back(element->clone()); - } -} - -Alternation& Alternation::operator =(const Alternation& other) { - if (this == &other) { - return *this; - } - - for (auto element : elements) { - delete element; - } - elements.clear(); - - for (auto element : other.elements) { - elements.push_back(element->clone()); - } - - return *this; -} - -Alternation::~Alternation() { - for (auto element : elements) { - delete element; - } - elements.clear(); -} - -list<RegExpElement*>& Alternation::getElements() { - return elements; -} - -const list<RegExpElement*>& Alternation::getElements() const { - return elements; -} - -RegExpElement* Alternation::clone() const { - return new Alternation(*this); -} - -bool Alternation::operator<(const RegExpElement& other) const { - return other > *this; -} - -bool Alternation::operator==(const RegExpElement& other) const { - return other == *this; -} - -bool Alternation::operator>(const RegExpElement& other) const { - return other < *this; -} - - -bool Alternation::operator<(const Concatenation&) const { - return true; -} - -bool Alternation::operator<(const Alternation& other) const { - int thisSize = this->elements.size(); - int otherSize = other.elements.size(); - if(thisSize < otherSize) return true; - if(thisSize > otherSize) return false; - - auto thisIter = this->elements.begin(); - auto otherIter = other.elements.begin(); - for(; thisIter != this->elements.end(); thisIter++, otherIter++) { - if(**thisIter != **otherIter) break; - } - if(thisIter == this->elements.end()) return false; - - return **thisIter < **otherIter; -} - -bool Alternation::operator==(const Alternation& other) const { - if(this->elements.size() != other.elements.size()) return false; - - auto thisIter = this->elements.begin(); - auto otherIter = other.elements.begin(); - for(; thisIter != this->elements.end(); thisIter++, otherIter++) { - if(**thisIter != **otherIter) return false; - } - - return true; -} - -void Alternation::getAlphabet( std::set<alphabet::Symbol> & alphabet ) const { - for(const auto& child : this->elements) - child->getAlphabet(alphabet); -} - -bool Alternation::containsEmptyString() const { - for(const auto& e : getElements()) - if(e->containsEmptyString()) - return true; - - return false; // alternation of zero regexps is empty -} - -bool Alternation::isEmpty() const { - for(const auto& e : getElements()) - if(!e->isEmpty()) - return false; - - return true; // alternation of zero regexps is empty -} - -} /* namespace regexp */ - diff --git a/alib/src/regexp/Alternation.h b/alib/src/regexp/Alternation.h deleted file mode 100644 index 320c7fa6da1ab9d9a54e92aa9353db0d93115f5d..0000000000000000000000000000000000000000 --- a/alib/src/regexp/Alternation.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Alternation.h - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#ifndef ALTERNATION_H_ -#define ALTERNATION_H_ - -#include <list> -#include "RegExpElement.h" - -namespace regexp { - -using namespace std; - -/** - * Represents alternation operator in the regular expression. Contains list of RegExpElement - * as operands of the operator. - */ -class Alternation: public RegExpElement { -private: - list<RegExpElement*> elements; -public: - Alternation(); - Alternation(const Alternation& other); - Alternation& operator =(const Alternation& other); - ~Alternation(); - - /** - * @return list of operands - */ - list<RegExpElement*>& getElements(); - - /** - * @return list of operands - */ - const list<RegExpElement*>& getElements() const; - - /** - * @copydoc RegExpElement::clone() const - */ - RegExpElement* clone() const; - - virtual bool operator<(const RegExpElement&) const; - virtual bool operator==(const RegExpElement&) const; - virtual bool operator>(const RegExpElement&) const; - - virtual bool operator<(const Concatenation&) const; - virtual bool operator<(const Alternation&) const; - virtual bool operator==(const Alternation&) const; - - /** - * @copydoc RegExpElement::getAlphabet() - */ - virtual void getAlphabet( std::set<alphabet::Symbol> & alphabet ) const; - - /** - * @copydoc RegExpElement::containsEmptyString() const - */ - virtual bool containsEmptyString() const; - - /** - * @copydoc RegExpElement::isEmpty() const - */ - virtual bool isEmpty() const; -}; - -} /* namespace regexp */ -#endif /* ALTERNATION_H_ */ diff --git a/alib/src/regexp/Concatenation.cpp b/alib/src/regexp/Concatenation.cpp deleted file mode 100644 index b678958ac903898e5d8e5dabdab98066f8ed7cb5..0000000000000000000000000000000000000000 --- a/alib/src/regexp/Concatenation.cpp +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Concatenation.cpp - * - * Created on: Nov 27, 2013 - * Author: martin - */ - -#include "Concatenation.h" - -namespace regexp { - -Concatenation::Concatenation() { -} - -Concatenation::Concatenation(const Concatenation& other) { - for (auto element : other.elements) { - elements.push_back(element->clone()); - } -} - -Concatenation& Concatenation::operator =(const Concatenation& other) { - if(this == &other) { - return *this; - } - - for (auto element : elements) { - delete element; - } - elements.clear(); - - for (auto element : other.elements) { - elements.push_back(element->clone()); - } - - return *this; -} - -Concatenation::~Concatenation() { - for (auto element : elements) { - delete element; - } - elements.clear(); -} - -list<RegExpElement*>& Concatenation::getElements() { - return elements; -} - -const list<RegExpElement*>& Concatenation::getElements() const { - return elements; -} - -RegExpElement* Concatenation::clone() const { - return new Concatenation(*this); -} - -bool Concatenation::operator<(const RegExpElement& other) const { - return other > *this; -} - -bool Concatenation::operator==(const RegExpElement& other) const { - return other == *this; -} - -bool Concatenation::operator>(const RegExpElement& other) const { - return other < *this; -} - - -bool Concatenation::operator<(const Concatenation& other) const { - int thisSize = this->elements.size(); - int otherSize = other.elements.size(); - if(thisSize < otherSize) return true; - if(thisSize > otherSize) return false; - - auto thisIter = this->elements.begin(); - auto otherIter = other.elements.begin(); - for(; thisIter != this->elements.end(); thisIter++, otherIter++) { - if(**thisIter != **otherIter) break; - } - if(thisIter == this->elements.end()) return false; - - return **thisIter < **otherIter; -} - -bool Concatenation::operator==(const Concatenation& other) const { - if(this->elements.size() != other.elements.size()) return false; - - auto thisIter = this->elements.begin(); - auto otherIter = other.elements.begin(); - for(; thisIter != this->elements.end(); thisIter++, otherIter++) { - if(**thisIter != **otherIter) return false; - } - - return true; -} - -void Concatenation::getAlphabet( std::set<alphabet::Symbol> & alphabet ) const { - for(const auto& child : this->elements) - child->getAlphabet(alphabet); -} - -bool Concatenation::containsEmptyString() const { - for(const auto& e : getElements()) - if( ! e->containsEmptyString()) - return false; - - return true; // concatenation of zero regexps is epsilon -} - -bool Concatenation::isEmpty() const { - for(const auto& e : getElements()) - if(e->isEmpty()) - return true; - - return false; // concatenation of zero regexps is epsilon -} - -} /* namespace regexp */ diff --git a/alib/src/regexp/Concatenation.h b/alib/src/regexp/Concatenation.h deleted file mode 100644 index f195f3e3cba21540413a4517c256f57d46e5d8ae..0000000000000000000000000000000000000000 --- a/alib/src/regexp/Concatenation.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Concatenation.h - * - * Created on: Nov 27, 2013 - * Author: martin - */ - -#ifndef CONCATENATION_H_ -#define CONCATENATION_H_ - -#include <list> -#include "RegExpElement.h" - -namespace regexp { - -using namespace std; - -/** - * Represents concatenation operator in the regular expression. Contains list of RegExpElement - * as operands of the operator. - */ -class Concatenation: public RegExpElement { -private: - list<RegExpElement*> elements; -public: - Concatenation(); - Concatenation(const Concatenation& other); - Concatenation& operator =(const Concatenation& other); - ~Concatenation(); - - /** - * @return list of operands - */ - list<RegExpElement*>& getElements(); - - /** - * @return list of operands - */ - const list<RegExpElement*>& getElements() const; - - /** - * @copydoc RegExpElement::clone() const - */ - RegExpElement* clone() const; - - virtual bool operator<(const RegExpElement&) const; - virtual bool operator==(const RegExpElement&) const; - virtual bool operator>(const RegExpElement&) const; - - virtual bool operator<(const Concatenation&) const; - virtual bool operator==(const Concatenation&) const; - - /** - * @copydoc RegExpElement::getAlphabet() - */ - virtual void getAlphabet( std::set<alphabet::Symbol> & alphabet ) const; - - /** - * @copydoc RegExpElement::containsEmptyString() const - */ - virtual bool containsEmptyString() const; - - /** - * @copydoc RegExpElement::isEmpty() const - */ - virtual bool isEmpty() const; -}; - -} /* namespace regexp */ -#endif /* CONCATENATION_H_ */ diff --git a/alib/src/regexp/Iteration.cpp b/alib/src/regexp/Iteration.cpp deleted file mode 100644 index 888985fa098e84a3fd3232911854a5a01d26f23b..0000000000000000000000000000000000000000 --- a/alib/src/regexp/Iteration.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Iteration.cpp - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#include "Iteration.h" -#include <cstdio> - -namespace regexp { - -Iteration::Iteration() { - element = NULL; -} - -Iteration::Iteration(const Iteration& other) { - if (other.element != NULL) { - element = other.element->clone(); - } else { - element = NULL; - } - -} - -Iteration& Iteration::operator=(const Iteration& other) { - if (this == &other) { - return *this; - } - - if (element != NULL) { - delete element; - } - - if (other.element != NULL) { - element = other.element->clone(); - } else { - element = NULL; - } - - return *this; -} - -regexp::Iteration::~Iteration() { - if (element != NULL) { - delete element; - } - - element = NULL; -} - -RegExpElement* Iteration::getElement() { - return element; -} - -const RegExpElement* Iteration::getElement() const { - return element; -} - -void Iteration::setElement(RegExpElement* element) { - this->element = element; -} - -RegExpElement* Iteration::clone() const { - return new Iteration(*this); -} - -bool Iteration::operator<(const RegExpElement& other) const { - return other > *this; -} - -bool Iteration::operator==(const RegExpElement& other) const { - return other == *this; -} - -bool Iteration::operator>(const RegExpElement& other) const { - return other < *this; -} - -bool Iteration::operator<(const Concatenation&) const { - return true; -} - -bool Iteration::operator<(const Alternation&) const { - return true; -} - -bool Iteration::operator<(const Iteration& other) const { - return *(this->element) < *(other.element); -} - -bool Iteration::operator==(const Iteration& other) const { - return *(this->element) == *(other.element); -} - -void Iteration::getAlphabet( std::set<alphabet::Symbol> & alphabet ) const { - element->getAlphabet( alphabet ); -} - -bool Iteration::containsEmptyString() const { - return true; -} - -bool Iteration::isEmpty() const { - return false; -} - -} /* namespace regexp */ - diff --git a/alib/src/regexp/Iteration.h b/alib/src/regexp/Iteration.h deleted file mode 100644 index 9b3e8c50727e6320de921393565917172033e720..0000000000000000000000000000000000000000 --- a/alib/src/regexp/Iteration.h +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Iteration.h - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#ifndef ITERATION_H_ -#define ITERATION_H_ - -#include <list> -#include "RegExpElement.h" - -namespace regexp { - -using namespace std; - -/** - * Represents iteration operator in the regular expression. Contains one RegExpElement - * as operand. - */ -class Iteration: public RegExpElement { -private: - RegExpElement* element; -public: - Iteration(); - Iteration(const Iteration& other); - Iteration& operator =(const Iteration& other); - ~Iteration(); - - /** - * @return element to iterate - */ - RegExpElement* getElement(); - - /** - * @return element to iterate - */ - const RegExpElement* getElement() const; - - /** - * Sets the element to iterate. Doesn't perform the copy, just stores the pointer! - * @param element RegExpElement to set - */ - void setElement(RegExpElement* element); - - /** - * @copydoc RegExpElement::clone() const - */ - RegExpElement* clone() const; - - virtual bool operator<(const RegExpElement&) const; - virtual bool operator==(const RegExpElement&) const; - virtual bool operator>(const RegExpElement&) const; - - virtual bool operator<(const Concatenation&) const; - virtual bool operator<(const Alternation&) const; - virtual bool operator<(const Iteration&) const; - virtual bool operator==(const Iteration&) const; - - /** - * @copydoc RegExpElement::getAlphabet() - */ - virtual void getAlphabet( std::set<alphabet::Symbol> & alphabet ) const; - - /** - * @copydoc RegExpElement::containsEmptyString() const - */ - virtual bool containsEmptyString() const; - - /** - * @copydoc RegExpElement::isEmpty() const - */ - virtual bool isEmpty() const; -}; - -} /* namespace regexp */ -#endif /* ITERATION_H_ */ diff --git a/alib/src/regexp/RegExp.cpp b/alib/src/regexp/RegExp.cpp deleted file mode 100644 index 5f7571ae1fa06a9f6119408856f541ebdf46d86c..0000000000000000000000000000000000000000 --- a/alib/src/regexp/RegExp.cpp +++ /dev/null @@ -1,126 +0,0 @@ -/* - * RegExp.cpp - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#include "RegExp.h" -#include "RegExpPrinter.h" - -#include <iostream> - -namespace regexp { - -RegExp::RegExp() { - regExp = NULL; -} - -RegExp::RegExp(const RegExp& other) { - if (other.regExp != NULL) { - regExp = other.regExp->clone(); - } else { - regExp = NULL; - } -} - -RegExp::RegExp(const RegExpElement* regExp) { - if (regExp != NULL) { - this->regExp = regExp->clone(); - } else { - this->regExp = NULL; - } -} - -RegExp& RegExp::operator =(const RegExp& other) { - if (this == &other) { - return *this; - } - - if (regExp != NULL) { - delete regExp; - } - - if (other.regExp != NULL) { - regExp = other.regExp->clone(); - } else { - regExp = NULL; - } - - return *this; -} - -RegExp::~RegExp() { - if (regExp != NULL) { - delete regExp; - } - - regExp = NULL; -} - -RegExpElement* RegExp::getRegExp() { - return regExp; -} - -const RegExpElement* RegExp::getRegExp() const { - return regExp; -} - -void RegExp::setRegExp(RegExpElement* regExp) { - this->regExp = regExp; -} - -set<alphabet::Symbol> RegExp::getAlphabet(void) const{ - set<alphabet::Symbol> alphabet; - - if(regExp) - regExp->getAlphabet( alphabet ); - - return alphabet; -} - -bool RegExp::isEmpty() const { - return regExp == NULL || regExp->isEmpty(); -} - -bool RegExp::containsEmptyString() const { - if(regExp) - return regExp->containsEmptyString(); - - return false; -} - -void RegExp::toXML(ostream& out) const { - RegExpPrinter::toXML(*this, out); -} - -ostream& operator <<(ostream& out, const RegExp& regexp) { - regexp.toXML(out); - return out; -} - -bool RegExp::operator<(const RegExp& other) const { - return *(this->regExp) < *(other.regExp); -} - -bool RegExp::operator<=(const RegExp& other) const { - return *(this->regExp) <= *(other.regExp); -} - -bool RegExp::operator==(const RegExp& other) const { - return *(this->regExp) == *(other.regExp); -} - -bool RegExp::operator!=(const RegExp& other) const { - return *(this->regExp) != *(other.regExp); -} - -bool RegExp::operator>(const RegExp& other) const { - return *(this->regExp) > *(other.regExp); -} - -bool RegExp::operator>=(const RegExp& other) const { - return *(this->regExp) >= *(other.regExp); -} - -} /* namespace regexp */ diff --git a/alib/src/regexp/RegExp.h b/alib/src/regexp/RegExp.h deleted file mode 100644 index 528cfb5024f57cf12efd24881224c62766641d6a..0000000000000000000000000000000000000000 --- a/alib/src/regexp/RegExp.h +++ /dev/null @@ -1,98 +0,0 @@ - /* - * RegExp.h - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#ifndef REGEXP_H_ -#define REGEXP_H_ - -#include <vector> -#include <list> -#include <string> -#include "RegExpElement.h" -#include "RegExpEmpty.h" - -#include "../alphabet/Symbol.h" - -namespace regexp { - -using namespace std; - -/** - * Represents regular expression parsed from the XML. Regular expression is stored - * as a tree of RegExpElement. - */ -class RegExp { -private: - RegExpElement* regExp; - -public: - RegExp(); - RegExp(const RegExpElement* regExp); - - /** - * Copy constructor. - * @param other RegExp to copy - */ - RegExp(const RegExp& other); - RegExp& operator =(const RegExp& other); - ~RegExp(); - - /** - * @return Root node of the regular expression tree - */ - RegExpElement* getRegExp(); - - /** - * @return Root node of the regular expression tree - */ - const RegExpElement* getRegExp() const; - - /** - * Sets the root node of the regular expression tree. Doesn't perform copy of the regExp param, - * just stores it! - * @param regExp root node to set - */ - void setRegExp(RegExpElement* regExp); - - /** - * Gets alphabet symbols used in RegExp. - * @return set of alphabet symbols used in regexp. - */ - std::set<alphabet::Symbol> getAlphabet(void) const; - - /** - * @return true if regexp represents empty language - */ - bool isEmpty() const; - - /** - * @return true if regexp matches empty string (epsilon) - */ - bool containsEmptyString() const; - - /** - * Prints XML representation of the RegExp to the output stream. - * @param out output stream to which print the RegExp - */ - void toXML(ostream& out) const; - - /** - * 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, const RegExp& regexp); - - bool operator<(const RegExp&) const; - bool operator<=(const RegExp&) const; - bool operator==(const RegExp&) const; - bool operator!=(const RegExp&) const; - bool operator>(const RegExp&) const; - bool operator>=(const RegExp&) const; -}; - -} /* namespace regexp */ -#endif /* REGEXP_H_ */ diff --git a/alib/src/regexp/RegExpElement.cpp b/alib/src/regexp/RegExpElement.cpp deleted file mode 100644 index 7dddc07ea2a911d2c2cf3076dce0eed3f31ab35a..0000000000000000000000000000000000000000 --- a/alib/src/regexp/RegExpElement.cpp +++ /dev/null @@ -1,82 +0,0 @@ -/* - * RegExpElement.cpp - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#include "RegExpElement.h" - -namespace regexp { - -RegExpElement::~RegExpElement() { - -} - -bool RegExpElement::operator>=(const RegExpElement& other) const { - return !(*this < other); -} - -bool RegExpElement::operator<=(const RegExpElement& other) const { - return !(*this > other); -} - -bool RegExpElement::operator!=(const RegExpElement& other) const { - return !(*this == other); -} - - -bool RegExpElement::operator<(const Concatenation& other) const { - return false; -} - -bool RegExpElement::operator<(const Alternation& other) const { - return false; -} - -bool RegExpElement::operator<(const Iteration& other) const { - return false; -} - -bool RegExpElement::operator<(const RegExpSymbol& other) const { - return false; -} - -bool RegExpElement::operator<(const RegExpEpsilon& other) const { - return false; -} - -bool RegExpElement::operator<(const RegExpEmpty& other) const { - return false; -} - - -bool RegExpElement::operator==(const Concatenation& other) const { - return false; -} - -bool RegExpElement::operator==(const Alternation& other) const { - return false; -} - -bool RegExpElement::operator==(const Iteration& other) const { - return false; -} - -bool RegExpElement::operator==(const RegExpSymbol& other) const { - return false; -} - -bool RegExpElement::operator==(const RegExpEpsilon& other) const { - return false; -} - -bool RegExpElement::operator==(const RegExpEmpty& other) const { - return false; -} - -void RegExpElement::getAlphabet( std::set<alphabet::Symbol> & alphabet ) const { - // default: do nothing. -} - -} /* namespace regexp */ diff --git a/alib/src/regexp/RegExpElement.h b/alib/src/regexp/RegExpElement.h deleted file mode 100644 index 2337635f61546c4bd0f17336dd74e105a62aeee6..0000000000000000000000000000000000000000 --- a/alib/src/regexp/RegExpElement.h +++ /dev/null @@ -1,81 +0,0 @@ -/* - * RegExpElement.h - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#ifndef REGEXPELEMENT_H_ -#define REGEXPELEMENT_H_ - -#include <set> -#include "../alphabet/Symbol.h" - -namespace regexp { - -using namespace std; - - -class Alternation; -class Concatenation; -class Iteration; -class RegExpSymbol; -class RegExpEmpty; -class RegExpEpsilon; - -/** - * Abstract class representing element in the regular expression. Can be operator or symbol. - */ -class RegExpElement { -public: - virtual ~RegExpElement(); - - /** - * Creates copy of the element. - * @return copy of the element - */ - virtual RegExpElement* clone() const = 0; - - // RegExpEmpty < RegExpEpsilon < RegExpSymbol < RegExpIteration < RegExpAlternation < RegExpConcatenation - virtual bool operator<(const RegExpElement&) const = 0; - virtual bool operator==(const RegExpElement&) const = 0; - virtual bool operator>(const RegExpElement&) const = 0; - - virtual bool operator>=(const RegExpElement&) const; - virtual bool operator<=(const RegExpElement&) const; - virtual bool operator!=(const RegExpElement&) const; - - virtual bool operator<(const Concatenation&) const; - virtual bool operator<(const Alternation&) const; - virtual bool operator<(const Iteration&) const; - virtual bool operator<(const RegExpSymbol&) const; - virtual bool operator<(const RegExpEpsilon&) const; - virtual bool operator<(const RegExpEmpty&) const; - - virtual bool operator==(const Concatenation&) const; - virtual bool operator==(const Alternation&) const; - virtual bool operator==(const Iteration&) const; - virtual bool operator==(const RegExpSymbol&) const; - virtual bool operator==(const RegExpEpsilon&) const; - virtual bool operator==(const RegExpEmpty&) const; - - /** - * @return true if this subtree of regexp matches empty string (epsilon) - */ - virtual bool containsEmptyString() const = 0; - - /** - * @return true if this subtree describes empty language - */ - virtual bool isEmpty() const = 0; - - /** - * Traverses the regexp tree to get alphabet symbols used. - * - * @param alphabet All alphabet symbols encountered are added into this set - */ - virtual void getAlphabet( std::set<alphabet::Symbol> & alphabet ) const; -}; - -} /* namespace regexp */ -#endif /* REGEXPELEMENT_H_ */ diff --git a/alib/src/regexp/RegExpElements.h b/alib/src/regexp/RegExpElements.h deleted file mode 100644 index b0f3401aec912ee4fc073d0c903f17183d00b5b0..0000000000000000000000000000000000000000 --- a/alib/src/regexp/RegExpElements.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * RegExpElements.h - * - * Created on: 14. 3. 2014 - * Author: tomas - */ - -#ifndef REGEXPELEMENTS_H_ -#define REGEXPELEMENTS_H_ - - -#include "Alternation.h" -#include "Concatenation.h" -#include "Iteration.h" -#include "RegExpElement.h" -#include "RegExpEpsilon.h" -#include "RegExpEmpty.h" -#include "RegExpSymbol.h" - - -#endif /* REGEXPELEMENTS_H_ */ diff --git a/alib/src/regexp/RegExpEmpty.cpp b/alib/src/regexp/RegExpEmpty.cpp deleted file mode 100644 index 553cfd650f50ed8d2b73f1c34247ab9d07b72285..0000000000000000000000000000000000000000 --- a/alib/src/regexp/RegExpEmpty.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/* - * RegExpEmpty.cpp - * - * Created on: Jan 30, 2014 - * Author: honza - */ - -#include "RegExpEmpty.h" - -namespace regexp { - -RegExpEmpty::RegExpEmpty() { -} - -RegExpElement* RegExpEmpty::clone() const { - return new RegExpEmpty(); -} - -bool RegExpEmpty::operator<(const RegExpElement& other) const { - return other > *this; -} - -bool RegExpEmpty::operator==(const RegExpElement& other) const { - return other == *this; -} - -bool RegExpEmpty::operator>(const RegExpElement& other) const { - return other < *this; -} - - -bool RegExpEmpty::operator<(const Concatenation&) const { - return true; -} - -bool RegExpEmpty::operator<(const Alternation&) const { - return true; -} - -bool RegExpEmpty::operator<(const Iteration&) const { - return true; -} - -bool RegExpEmpty::operator<(const RegExpSymbol&) const { - return true; -} - -bool RegExpEmpty::operator<(const RegExpEpsilon&) const { - return true; -} - -bool RegExpEmpty::operator==(const RegExpEmpty&) const { - return true; -} - -bool RegExpEmpty::containsEmptyString() const { - return false; -} - -bool RegExpEmpty::isEmpty() const { - return true; -} - -} /* namespace regexp */ - diff --git a/alib/src/regexp/RegExpEmpty.h b/alib/src/regexp/RegExpEmpty.h deleted file mode 100644 index f56089d6c88360e9affffadd74136c57181d23fd..0000000000000000000000000000000000000000 --- a/alib/src/regexp/RegExpEmpty.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * RegExpEmpty.h - * - * Created on: Jan 30, 2014 - * Author: honza - */ - -#ifndef REGEXPEMPTY_H_ -#define REGEXPEMPTY_H_ - -#include "RegExpElement.h" - -namespace regexp { - -using namespace std; - -/** - * Represents empty regular expression in the regular expression. - */ -class RegExpEmpty: public RegExpElement { -public: - RegExpEmpty(); - - /** - * @copydoc RegExpElement::clone() const - */ - RegExpElement* clone() const; - - virtual bool operator<(const RegExpElement&) const; - virtual bool operator==(const RegExpElement&) const; - virtual bool operator>(const RegExpElement&) const; - - virtual bool operator<(const Concatenation&) const; - virtual bool operator<(const Alternation&) const; - virtual bool operator<(const Iteration&) const; - virtual bool operator<(const RegExpSymbol&) const; - virtual bool operator<(const RegExpEpsilon&) const; - virtual bool operator==(const RegExpEmpty&) const; - - /** - * @copydoc RegExpElement::containsEmptyString() const - */ - virtual bool containsEmptyString() const; - - /** - * @copydoc RegExpElement::isEmpty() const - */ - virtual bool isEmpty() const; -}; - -} /* namespace regexp */ -#endif /* REGEXPEMPTY_H_ */ diff --git a/alib/src/regexp/RegExpEpsilon.cpp b/alib/src/regexp/RegExpEpsilon.cpp deleted file mode 100644 index e102539f5fe874fd45961b58c222f1c6a41bca35..0000000000000000000000000000000000000000 --- a/alib/src/regexp/RegExpEpsilon.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/* - * RegExpEpsilon.cpp - * - * Created on: Jan 30, 2014 - * Author: honza - */ - -#include "RegExpEpsilon.h" - -namespace regexp { - -RegExpEpsilon::RegExpEpsilon() { -} - -RegExpElement* RegExpEpsilon::clone() const { - return new RegExpEpsilon(); -} - -bool RegExpEpsilon::operator<(const RegExpElement& other) const { - return other > *this; -} - -bool RegExpEpsilon::operator==(const RegExpElement& other) const { - return other == *this; -} - -bool RegExpEpsilon::operator>(const RegExpElement& other) const { - return other < *this; -} - - -bool RegExpEpsilon::operator<(const Alternation&) const { - return true; -} - -bool RegExpEpsilon::operator<(const Concatenation&) const { - return true; -} - -bool RegExpEpsilon::operator<(const Iteration&) const { - return true; -} - -bool RegExpEpsilon::operator<(const RegExpSymbol&) const { - return true; -} - -bool RegExpEpsilon::operator==(const RegExpEpsilon&) const { - return true; -} - -bool RegExpEpsilon::containsEmptyString() const { - return true; -} - -bool RegExpEpsilon::isEmpty() const { - return false; -} - -} /* namespace regexp */ - diff --git a/alib/src/regexp/RegExpEpsilon.h b/alib/src/regexp/RegExpEpsilon.h deleted file mode 100644 index a3dc873544d4497f5147fbccdcadf15db61f8f89..0000000000000000000000000000000000000000 --- a/alib/src/regexp/RegExpEpsilon.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * RegExpEpsilon.h - * - * Created on: Jan 30, 2014 - * Author: honza - */ - -#ifndef REGEXPEPSILON_H_ -#define REGEXPEPSILON_H_ - -#include "RegExpElement.h" -#include "../alphabet/Epsilon.h" - -namespace regexp { - -using namespace std; -using namespace alphabet; - -/** - * Represents epsilon in the regular expression. - */ -class RegExpEpsilon: public RegExpElement, public Epsilon { -public: - RegExpEpsilon(); - - /** - * @copydoc RegExpElement::clone() const - */ - RegExpElement* clone() const; - - virtual bool operator<(const RegExpElement&) const; - virtual bool operator==(const RegExpElement&) const; - virtual bool operator>(const RegExpElement&) const; - - virtual bool operator<(const Alternation&) const; - virtual bool operator<(const Concatenation&) const; - virtual bool operator<(const Iteration&) const; - virtual bool operator<(const RegExpSymbol&) const; - virtual bool operator==(const RegExpEpsilon&) const; - - /** - * @copydoc RegExpElement::containsEmptyString() const - */ - virtual bool containsEmptyString() const; - - /** - * @copydoc RegExpElement::isEmpty() const - */ - virtual bool isEmpty() const; -}; - -} /* namespace regexp */ -#endif /* REGEXPEPSILON_H_ */ diff --git a/alib/src/regexp/RegExpParser.cpp b/alib/src/regexp/RegExpParser.cpp deleted file mode 100644 index 5bcad371450a35c23584ebbfc6d5e26ca3c80e05..0000000000000000000000000000000000000000 --- a/alib/src/regexp/RegExpParser.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/* - * RegExpParser.cpp - * - * Created on: Nov 23, 2013 - * Author: Martin Zak - */ - -#include "RegExpParser.h" -#include "../sax/ParserException.h" - -namespace regexp { - -RegExp RegExpParser::parse(list<Token>& input) { - popToken(input, Token::START_ELEMENT, "regexp"); - RegExp regexp; - regexp.setRegExp(parseElement(input)); - popToken(input, Token::END_ELEMENT, "regexp"); - - return regexp; -} - -RegExpElement* RegExpParser::parseElement(list<Token>& input) { - if (isToken(input, Token::START_ELEMENT, "symbol")) { - return parseSymbol(input); - } else if (isToken(input, Token::START_ELEMENT, "empty")) { - return parseEmpty(input); - } else if (isToken(input, Token::START_ELEMENT, "epsilon")) { - return parseEpsilon(input); - } else if (isToken(input, Token::START_ELEMENT, "iteration")) { - return parseIteration(input); - } else if (isToken(input, Token::START_ELEMENT, "alternation")) { - return parseAlternation(input); - } else if (isToken(input, Token::START_ELEMENT, "concatenation")) { - return parseConcatenation(input); - } else { - return NULL; - } -} - -Alternation* RegExpParser::parseAlternation(list<Token>& input) { - popToken(input, Token::START_ELEMENT, "alternation"); - - Alternation* alternation = new Alternation; - parseContent(input, alternation->getElements()); - - popToken(input, Token::END_ELEMENT, "alternation"); - return alternation; -} - -Concatenation* RegExpParser::parseConcatenation(list<Token>& input) { - popToken(input, Token::START_ELEMENT, "concatenation"); - - Concatenation* concatenation = new Concatenation(); - parseContent(input, concatenation->getElements()); - - popToken(input, Token::END_ELEMENT, "concatenation"); - return concatenation; - -} - -Iteration* RegExpParser::parseIteration(list<Token>& input) { - popToken(input, Token::START_ELEMENT, "iteration"); - - Iteration* iteration = new Iteration(); - iteration->setElement(parseElement(input)); - - popToken(input, Token::END_ELEMENT, "iteration"); - return iteration; -} - -void RegExpParser::parseContent(list<Token>& input, list<RegExpElement*>& elements) { - while (true) { - RegExpElement* element = parseElement(input); - if(!element) return; - elements.push_back(element); - } -} - -RegExpEpsilon* RegExpParser::parseEpsilon(list<Token>& input) { - popToken(input, Token::START_ELEMENT, "epsilon"); - - RegExpEpsilon* epsilon = new RegExpEpsilon(); - - popToken(input, Token::END_ELEMENT, "epsilon"); - return epsilon; -} - -RegExpEmpty* RegExpParser::parseEmpty(list<Token>& input) { - popToken(input, Token::START_ELEMENT, "empty"); - - RegExpEmpty* empty = new RegExpEmpty(); - - popToken(input, Token::END_ELEMENT, "empty"); - return empty; -} - -RegExpSymbol* RegExpParser::parseSymbol(list<Token>& input) { - popToken(input, Token::START_ELEMENT, "symbol"); - - if (input.front().getType() == Token::CHARACTER) { - RegExpSymbol* symbol = new RegExpSymbol(input.front().getData()); - input.pop_front(); - popToken(input, Token::END_ELEMENT, "symbol"); - return symbol; - } else { - throw ParserException(Token("", Token::CHARACTER), input.front()); - } -} - -bool RegExpParser::isToken(list<Token>& input, Token::TokenType type, string data) { - return input.front().getType() == type && input.front().getData() == data; -} - -void RegExpParser::popToken(list<Token>& input, Token::TokenType type, string data) { - if (isToken(input, type, data)) { - input.pop_front(); - } else { - throw ParserException(Token(data, type), input.front()); - } -} - -} /* namespace regexp */ - diff --git a/alib/src/regexp/RegExpParser.h b/alib/src/regexp/RegExpParser.h deleted file mode 100644 index 654d3fe719c1f1a533f1cf929b27d879356c12a9..0000000000000000000000000000000000000000 --- a/alib/src/regexp/RegExpParser.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * RegExpParser.h - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#ifndef REGEXPPARSER_H_ -#define REGEXPPARSER_H_ - -#include "RegExp.h" -#include "RegExpElements.h" -#include "../sax/Token.h" - - -namespace regexp { - -using namespace sax; - -/** - * Parser used to get RegExp from XML parsed into list of tokens. - */ -class RegExpParser { -protected: - static void parseContent(list<Token>& input, list<RegExpElement*>& elements); - static RegExpElement* parseElement(list<Token>& input); - - static RegExpEpsilon* parseEpsilon(list<Token>& input); - static RegExpEmpty* parseEmpty(list<Token>& input); - static RegExpSymbol* parseSymbol(list<Token> &input); - static Iteration* parseIteration(list<Token> &input); - static Alternation* parseAlternation(list<Token> &input); - static Concatenation* parseConcatenation(list<Token> &input); - static bool isToken(list<Token> &input, Token::TokenType type, string data); - static void popToken(list<Token> &input, Token::TokenType type, string data); - -public: - /** - * Parses the XML and returns regular expression. The input is destroyed in the process. - * @param input XML represented as list of tokens - * @return RegExp - * @throws ParserException when an error occurs - */ - static RegExp parse(list<Token>& input); -}; - -} /* namespace regexp */ -#endif /* REGEXPPARSER_H_ */ diff --git a/alib/src/regexp/RegExpPrinter.cpp b/alib/src/regexp/RegExpPrinter.cpp deleted file mode 100644 index 5c9afdc77fe63f34aa2e43e6ee3377648facf1a1..0000000000000000000000000000000000000000 --- a/alib/src/regexp/RegExpPrinter.cpp +++ /dev/null @@ -1,101 +0,0 @@ -/* - * RegExpPrinter.cpp - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#include "RegExpPrinter.h" - -namespace regexp { - -const string RegExpPrinter::INDENTATION = "\t"; - -void RegExpPrinter::toXML(const RegExp& regexp, ostream& out) { - out << "<regexp>\n"; - printElement(regexp.getRegExp(), out, INDENTATION); - out << "</regexp>\n"; -} - -void RegExpPrinter::printElement(const RegExpElement* element, ostream& out, const string & prefix) { - const Alternation* alternation = dynamic_cast<const Alternation*>(element); - if (alternation) { - printAlternation(alternation, out, prefix); - return; - } - - const Concatenation* concatenation = dynamic_cast<const Concatenation*>(element); - if(concatenation) { - printConcatenation(concatenation,out, prefix); - return; - } - - const Iteration* iteration = dynamic_cast<const Iteration*>(element); - if (iteration) { - printIteration(iteration, out, prefix); - return; - } - - const RegExpEpsilon* epsilon = dynamic_cast<const RegExpEpsilon*>(element); - if (epsilon) { - printEpsilon(epsilon, out, prefix); - return; - } - - const RegExpEmpty* empty = dynamic_cast<const RegExpEmpty*>(element); - if (empty) { - printEmpty(empty, out, prefix); - return; - } - - const RegExpSymbol* symbol = dynamic_cast<const RegExpSymbol*>(element); - if (symbol) { - printSymbol(symbol, out, prefix); - return; - } - -} - -void RegExpPrinter::printContent(const list<RegExpElement*>& content, ostream& out, const string & prefix) { - for (auto element : content) { - printElement(element, out, prefix); - } -} - -void RegExpPrinter::printAlternation(const Alternation* alternation, ostream& out, const string & prefix) { - out << prefix << "<alternation>\n"; - printContent(alternation->getElements(), out, prefix + INDENTATION); - out << prefix << "</alternation>\n"; -} - -void RegExpPrinter::printConcatenation(const Concatenation* concatenation, ostream& out, const string & prefix) { - out << prefix <<"<concatenation>\n"; - printContent(concatenation->getElements(), out, prefix + INDENTATION); - out << prefix <<"</concatenation>\n"; - -} - -void RegExpPrinter::printIteration(const Iteration* iteration, ostream& out, const string & prefix) { - out << prefix << "<iteration>\n"; - printElement(iteration->getElement(), out, prefix + INDENTATION); - out << prefix << "</iteration>\n"; -} - -void RegExpPrinter::printSymbol(const RegExpSymbol* symbol, ostream& out, const string & prefix) { - out << prefix << "<symbol>"; - out << symbol->getSymbol(); - out << "</symbol>\n"; -} - -void RegExpPrinter::printEpsilon(const RegExpEpsilon* symbol, ostream& out, const string & prefix) { - out << prefix << "<epsilon>"; - out << "</epsilon>\n"; -} - -void RegExpPrinter::printEmpty(const RegExpEmpty* symbol, ostream& out, const string & prefix) { - out << prefix << "<empty>"; - out << "</empty>\n"; -} - -} /* namespace regexp */ - diff --git a/alib/src/regexp/RegExpPrinter.h b/alib/src/regexp/RegExpPrinter.h deleted file mode 100644 index 76eacc5beaba0ffad6c74d8b10657e0b411e05a4..0000000000000000000000000000000000000000 --- a/alib/src/regexp/RegExpPrinter.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * RegExpPrinter.h - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#ifndef REGEXPPRINTER_H_ -#define REGEXPPRINTER_H_ - -#include <ostream> -#include "RegExp.h" -#include "RegExpElements.h" - -namespace regexp { - -using namespace std; - -/** - * This class contains methods to print XML representation of regular expression to the output stream. - */ -class RegExpPrinter { -protected: - static const string INDENTATION; - static void printElement(const RegExpElement* element, ostream& out, const string & prefix); - static void printContent(const list<RegExpElement*>& content, ostream& out, const string & prefix); - static void printAlternation(const Alternation* alternation, ostream& out, const string & prefix); - static void printConcatenation(const Concatenation* concatenation, ostream& out, const string & prefix); - - static void printIteration(const Iteration* iteration, ostream& out, const string & prefix); - static void printSymbol(const RegExpSymbol* symbol, ostream& out, const string & prefix); - static void printEpsilon(const RegExpEpsilon* symbol, ostream& out, const string & prefix); - static void printEmpty(const RegExpEmpty* symbol, ostream& out, const string & prefix); - -public: - /** - * Prints XML representation of RegExp to the output stream. - * @param regexp RegExp to print - * @param out output stream to which print the RegExp - */ - static void toXML(const RegExp& regexp, ostream& out); -}; - -} /* namespace regexp */ -#endif /* REGEXPPRINTER_H_ */ diff --git a/alib/src/regexp/RegExpSymbol.cpp b/alib/src/regexp/RegExpSymbol.cpp deleted file mode 100644 index 140eee9f5210f0e097601e6a24c1fae478a85ff6..0000000000000000000000000000000000000000 --- a/alib/src/regexp/RegExpSymbol.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* - * RegExpSymbol.cpp - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#include "RegExpSymbol.h" - -namespace regexp { - -RegExpSymbol::RegExpSymbol() : - symbol("") { -} - -RegExpSymbol::RegExpSymbol(const string& symbol) : - symbol(symbol) { -} - -RegExpElement* RegExpSymbol::clone() const { - return new RegExpSymbol(this->symbol); -} - -bool RegExpSymbol::operator<(const RegExpElement& other) const { - return other > *this; -} - -bool RegExpSymbol::operator==(const RegExpElement& other) const { - return other == *this; -} - -bool RegExpSymbol::operator>(const RegExpElement& other) const { - return other < *this; -} - - -bool RegExpSymbol::operator<(const Concatenation&) const { - return true; -} - -bool RegExpSymbol::operator<(const Alternation&) const { - return true; -} - -bool RegExpSymbol::operator<(const Iteration&) const { - return true; -} - -bool RegExpSymbol::operator<(const RegExpSymbol& other) const { - return this->symbol < other.symbol; -} - -bool RegExpSymbol::operator==(const RegExpSymbol& other) const { - return this->symbol == other.symbol; -} - -bool RegExpSymbol::containsEmptyString() const { - return false; -} - -bool RegExpSymbol::isEmpty() const { - return false; -} - -void RegExpSymbol::getAlphabet( std::set<alphabet::Symbol> & alphabet ) const { - alphabet.insert( Symbol( this->getSymbol( ) ) ); -} - -const string& RegExpSymbol::getSymbol() const { - return this->symbol; -} - -} /* namespace regexp */ - diff --git a/alib/src/regexp/RegExpSymbol.h b/alib/src/regexp/RegExpSymbol.h deleted file mode 100644 index 167cc5f1da52029f256ce8b4e668410024713e73..0000000000000000000000000000000000000000 --- a/alib/src/regexp/RegExpSymbol.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * RegExpSymbol.h - * - * Created on: Nov 23, 2013 - * Author: martin - */ - -#ifndef REGEXPSYMBOL_H_ -#define REGEXPSYMBOL_H_ - -#include <string> -#include "RegExpElement.h" -#include "../alphabet/Symbol.h" - -namespace regexp { - -using namespace std; -using namespace alphabet; - -/** - * Represents symbol in the regular expression. Contains name of the symbol. - */ -class RegExpSymbol: public RegExpElement { - string symbol; -public: - RegExpSymbol(); - RegExpSymbol(const string& symbol); - - /** - * @copydoc RegExpElement::clone() const - */ - RegExpElement* clone() const; - - virtual bool operator<(const RegExpElement&) const; - virtual bool operator==(const RegExpElement&) const; - virtual bool operator>(const RegExpElement&) const; - - virtual bool operator<(const Concatenation&) const; - virtual bool operator<(const Alternation&) const; - virtual bool operator<(const Iteration&) const; - virtual bool operator<(const RegExpSymbol&) const; - virtual bool operator==(const RegExpSymbol&) const; - - /** - * @copydoc RegExpElement::getAlphabet() - */ - virtual void getAlphabet( std::set<alphabet::Symbol> & alphabet ) const; - - /** - * @return Returns string representation of symbol. - */ - const string& getSymbol() const; - - /** - * @copydoc RegExpElement::containsEmptyString() const - */ - virtual bool containsEmptyString() const; - - /** - * @copydoc RegExpElement::isEmpty() const - */ - virtual bool isEmpty() const; -}; - -} /* namespace regexp */ -#endif /* REGEXPSYMBOL_H_ */ diff --git a/alib/src/sax/ParserException.cpp b/alib/src/sax/ParserException.cpp deleted file mode 100644 index c40a1b2d59647d5507edd99198a3eaec02cd428c..0000000000000000000000000000000000000000 --- a/alib/src/sax/ParserException.cpp +++ /dev/null @@ -1,21 +0,0 @@ -/* - * ParserException.cpp - * - * Created on: Apr 16, 2013 - * Author: martin - */ - -#include "ParserException.h" - -namespace sax { - -ParserException::ParserException(const Token& expected, const Token& read) : - expected(expected), read(read) { - - cause = "Parser Exception: Expected: " + expected.getData() + " Read: " + read.getData(); -} -ParserException::~ParserException() throw () { - -} - -} /* namespace sax */ diff --git a/alib/src/sax/ParserException.h b/alib/src/sax/ParserException.h deleted file mode 100644 index c2a8881ad1b9b6c5e29b0d93623c865e494bfa99..0000000000000000000000000000000000000000 --- a/alib/src/sax/ParserException.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * ParserException.h - * - * Created on: Apr 16, 2013 - * Author: martin - */ - -#ifndef PARSEREXCEPTION_H_ -#define PARSEREXCEPTION_H_ - -#include "../AlibException.h" -#include "Token.h" -#include <string.h> -namespace sax { - -/** - * Exception thrown by XML parser when is expected different tag than the one which is read. - */ -class ParserException: public alib::AlibException { -protected: - Token expected; - Token read; -public: - ParserException(const Token& expected, const Token& read); - ~ParserException() throw (); -}; - -} /* namespace sax */ -#endif /* PARSEREXCEPTION_H_ */ diff --git a/alib/src/sax/SaxInterface.cpp b/alib/src/sax/SaxInterface.cpp deleted file mode 100644 index 6370398af127c5ebb731b33412a1b32e18feede4..0000000000000000000000000000000000000000 --- a/alib/src/sax/SaxInterface.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/* - * SaxInterface.cpp - * - * Created on: 8.8.2012 - * Author: martin - */ - -#include "SaxInterface.h" - -#include <string> -#include <cstring> -#include <cstdlib> -#include "../AlibException.h" - -namespace sax { - -using namespace std; -using namespace alib; - -void SaxInterface::initSAXHandler(xmlSAXHandler& handler) { - memset(&handler, 0, sizeof(handler)); - handler.initialized = XML_SAX2_MAGIC; - - handler.startElement = &sax::SaxInterface::startElement; - handler.characters = &sax::SaxInterface::characters; - handler.endElement = &sax::SaxInterface::endElement; -} - -void SaxInterface::parseMemory(string xmlIn, list<Token>& out) { - xmlSAXHandler handler; - initSAXHandler(handler); - - int result = xmlSAXUserParseMemory(&handler, (void*) &out, xmlIn.c_str(), xmlIn.length()); - xmlCleanupParser(); - - if (result != 0) { - throw AlibException("Cannot parse the XML string."); - } -} - -void SaxInterface::parseFile(string filename, list<Token>& out) { - xmlSAXHandler handler; - initSAXHandler(handler); - - int result = xmlSAXUserParseFile(&handler, (void*) &out, filename.c_str()); - xmlCleanupParser(); - - if (result != 0) { - throw AlibException("Cannot parse the XML file " + filename); - } - -} - -void SaxInterface::characters(void * userData, const xmlChar * ch, int len) { - list<Token> &out = *((list<Token>*) userData); - string tmp((char*) ch, len); - - for (unsigned int i = 0; i < tmp.length(); i++) { - if (!isspace(tmp[i])) { - Token token(tmp, Token::CHARACTER); - out.push_back(token); - break; - } - } -} - -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)); -} - -void SaxInterface::endElement(void * userData, const xmlChar * name) { - list<Token> &out = *((list<Token>*) userData); - out.push_back(Token((char*) name, Token::END_ELEMENT)); -} -} /* namespace sax */ diff --git a/alib/src/sax/SaxInterface.h b/alib/src/sax/SaxInterface.h deleted file mode 100644 index d181b1e1dcfb0c4dbcad8a797e34eddf9084b294..0000000000000000000000000000000000000000 --- a/alib/src/sax/SaxInterface.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * SaxInterface.h - * - * Created on: 8.8.2012 - * Author: martin - */ - -#ifndef SAXINTERFACE_H_ -#define SAXINTERFACE_H_ - -#include <libxml/parser.h> -#include <list> -#include "Token.h" - -namespace sax { - -using namespace std; - -/** - * This class performs parsing of file or string containing XML. Contains callback - * methods for libxml SAX parser. - */ -class SaxInterface { - -protected: - /** - * Initializes the SAX parser. - */ - static void initSAXHandler(xmlSAXHandler&); - - /** - * Callback method called when charactes (between tags) are read. - * @param userData contains list of parsed tokens - * @param ch array of parsed characters - * @param len length of the array - */ - static void characters(void * userData, const xmlChar * ch, int len); - - /** - * Callback method called when start of the tag is read. - * @param userData contains list of parsed tokens - * @param name array of characters containing name of the tag - * @param attrs array containing attributes (arrays of characters) of the tag - */ - static void startElement(void *userData, const xmlChar *name, const xmlChar **attrs); - - /** - * Callback method called when end of the tag is read. - * @param userData contains list of parsed tokens - * @param name array of characters containing name of the tag - */ - static void endElement(void * userData, const xmlChar * name); - -public: - /** - * Parses the string containing XML. - * @param xmlIn input XML - * @param out list of tokens that are returned - * @throws AlibException when an error occurs (e.g. XML is not valid) - */ - static void parseMemory(string xmlIn, list<Token>& out); - - /** - * Parses the file containing XML. - * @param filename input XML - * @param out list of tokens that are returned - * @throws AlibException when an error occurs (e.g. file doesn't exist, XML is not valid) - */ - static void parseFile(string filename, list<Token>& out); -}; - -} /* namespace sax */ -#endif /* SAXINTERFACE_H_ */ diff --git a/alib/src/sax/Token.cpp b/alib/src/sax/Token.cpp deleted file mode 100644 index a0880c8a3e348e3c981c95e0194e2b690a436980..0000000000000000000000000000000000000000 --- a/alib/src/sax/Token.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/* - * ParseToken.cpp - * - * Created on: 9.8.2012 - * Author: martin - */ - -#include "Token.h" - -namespace sax { - -Token::Token(const std::string data, const Token::TokenType type) : - data(data), type(type) { - -} - -Token::~Token() { - -} - -std::string Token::getData() const { - return data; -} - -Token::TokenType Token::getType() const { - return type; -} - -} /* namespace sax */ diff --git a/alib/src/sax/Token.h b/alib/src/sax/Token.h deleted file mode 100644 index 3f544dabb47ad32c140d53594b19bac4de66e334..0000000000000000000000000000000000000000 --- a/alib/src/sax/Token.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * ParseToken.h - * - * Created on: 9.8.2012 - * Author: martin - */ - -#ifndef PARSETOKEN_H_ -#define PARSETOKEN_H_ - -#include <string> - -namespace sax { - -using namespace std; - -/** - * Represents part of parsed XML. Can be start of tag, end of tag, - * tag attribute or array of characters. - */ -class Token { -public: - - enum TokenType { - START_ELEMENT, END_ELEMENT, ATTRIBUTE, CHARACTER - }; - -private: - string data; - TokenType type; - -public: - Token(const string, const TokenType); - virtual ~Token(); - - /** - * @return name of the tag or characters read - */ - string getData() const; - - /** - * @return type of the token - star of the tag, end of the tag, attribute - * of the tag or characters - */ - TokenType getType() const; -}; - -} /* namespace sax */ - -#endif /* PARSETOKEN_H_ */ diff --git a/alib/src/std/istream.cpp b/alib/src/std/istream.cpp deleted file mode 100644 index f12a3dceb8d454ea662f3a13b59140ba5661a09d..0000000000000000000000000000000000000000 --- a/alib/src/std/istream.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "istream.h" - -std::istream& oprr(std::istream& in, const std::string& str, bool start) { - if(str.size() == 0) return in; - char c_str = str[0]; - char c_in = in.peek(); - in.get(); - if(in.good()) { - if(start && (c_in == ' ' || c_in == '\n' || c_in == '\t')) { - oprr(in, str, start); - } else if(c_str == c_in) { - oprr(in, str.substr(1), false); - } else { - in.clear(std::ios::failbit); - } - } - if(in.fail()) { - in.clear(); - in.putback(c_in); - in.clear(std::ios::failbit); - } - return in; -} - -std::istream& operator>>(std::istream& in, const std::string& str) { - return oprr(in, str, true); -} \ No newline at end of file diff --git a/alib/src/std/istream.h b/alib/src/std/istream.h deleted file mode 100644 index 3d5167191f0463ca0ca6b4760e7915b0b166f1f1..0000000000000000000000000000000000000000 --- a/alib/src/std/istream.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef ISTREAM_H_ -#define ISTREAM_H_ - -#include <iostream> -#include <string> - -std::istream& operator>>(std::istream& in, const std::string& str); - -#endif \ No newline at end of file diff --git a/alib/src/std/type_traits.hpp b/alib/src/std/type_traits.hpp deleted file mode 100644 index 1ee68e2268877a8cfcc1fe23624cfc840211dca7..0000000000000000000000000000000000000000 --- a/alib/src/std/type_traits.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef TYPE_TRAITS_HPP_ -#define TYPE_TRAITS_HPP_ - -#include <type_traits> - -namespace std { - template <typename T> - struct is_base_of<T, T> { - static const bool value = true; - }; - - template <typename T, typename... Ts> - struct is_base_of_any; - - template <typename T, typename F> - struct is_base_of_any<T, F> - { - static const bool value = is_base_of<T, F>::value; - }; - - template <typename T, typename F, typename... Ts> - struct is_base_of_any<T, F, Ts...> - { - static const bool value = is_base_of<T, F>::value || is_base_of_any<T, Ts...>::value; - }; -} - -#endif diff --git a/alib/src/std/variant.hpp b/alib/src/std/variant.hpp deleted file mode 100644 index 3771d4c477ba9c520d15381f71e35b0c460812c6..0000000000000000000000000000000000000000 --- a/alib/src/std/variant.hpp +++ /dev/null @@ -1,195 +0,0 @@ -/** - * https://gist.github.com/tibordp/6909880 - * Created on: Feb 28, 2014 - * Author: Tibor Djurica Potpara - * Modified: Jan Travnicek - */ - -#ifndef VATIANT_HPP_ -#define VATIANT_HPP_ - -#include <iostream> -#include <utility> -#include <typeinfo> -#include "type_traits.hpp" -#include <string> - -template <size_t arg1, size_t ... others> -struct static_max; - -template <size_t arg> -struct static_max<arg> -{ - static const size_t value = arg; -}; - -template <size_t arg1, size_t arg2, size_t ... others> -struct static_max<arg1, arg2, others...> -{ - static const size_t value = arg1 >= arg2 ? static_max<arg1, others...>::value : - static_max<arg2, others...>::value; -}; - -template<typename... Ts> -struct variant_helper; - -template<typename F, typename... Ts> -struct variant_helper<F, Ts...> { - inline static void destroy(size_t id, void * data) - { - if (id == typeid(F).hash_code()) - reinterpret_cast<F*>(data)->~F(); - else - variant_helper<Ts...>::destroy(id, data); - } - - inline static bool move(size_t old_t, void * old_v, void * new_v) - { - if (old_t == typeid(F).hash_code()) { - new (new_v) F(std::move(*reinterpret_cast<F*>(old_v))); - return true; - } else - return variant_helper<Ts...>::move(old_t, old_v, new_v); - } - - inline static void copy(size_t old_t, const void * old_v, void * new_v) - { - if (old_t == typeid(F).hash_code()) - new (new_v) F(*reinterpret_cast<const F*>(old_v)); - else - return variant_helper<Ts...>::copy(old_t, old_v, new_v); - } - - inline static bool compareEq(size_t this_t, const void * this_v, size_t other_t, const void * other_v) - { - if (this_t == typeid(F).hash_code()) - return this_t == other_t && *(reinterpret_cast<const F*>(this_v)) == *(reinterpret_cast<const F*>(other_v)); - else - return variant_helper<Ts...>::compareEq(this_t, this_v, other_t, other_v); - } - - inline static bool compareLess(size_t this_t, const void * this_v, size_t other_t, const void * other_v) - { - if (this_t == typeid(F).hash_code() && other_t != typeid(F).hash_code()) return true; - if (this_t != typeid(F).hash_code() && other_t == typeid(F).hash_code()) return false; - - if (this_t == typeid(F).hash_code() && other_t == typeid(F).hash_code()) - return *(reinterpret_cast<const F*>(this_v)) < *(reinterpret_cast<const F*>(other_v)); - else - return variant_helper<Ts...>::compareLess(this_t, this_v, other_t, other_v); - } - -}; - -template<> struct variant_helper<> { -inline static void destroy(size_t id, void * data) { } -inline static bool move(size_t old_t, void * old_v, void * new_v) { return false; } -inline static void copy(size_t old_t, const void * old_v, void * new_v) { } -inline static bool compareEq(size_t this_t, const void * this_v, size_t other_t, const void * other_v) { return true; } -inline static bool compareLess(size_t this_t, const void * this_v, size_t other_t, const void * other_v) { return false; } -}; - -template<typename F, typename... Ts> -struct variant { -private: - static const size_t data_size = static_max<sizeof(F), sizeof(Ts)...>::value; - static const size_t data_align = static_max<alignof(F), alignof(Ts)...>::value; - - using data_t = typename std::aligned_storage<data_size, data_align>::type; - - using helper_t = variant_helper<F, Ts...>; - - size_t type_id; - data_t data; -public: - //default constructor - variant() : type_id(typeid(F).hash_code()) { new (&data) F(); } - - //copy consructor - variant(const variant<F, Ts...>& old) : type_id(old.type_id) - { - helper_t::copy(old.type_id, &old.data, &data); - } - - //move constructor - variant(variant<F, Ts...>&& old) : type_id(old.type_id) - { - helper_t::move(old.type_id, &old.data, &data); - - new (&old.data) F(); - old.type_id = typeid(F).hash_code(); - } - - //assignment operator - variant<F, Ts...>& operator= (variant<F, Ts...> old) - { - std::swap(type_id, old.type_id); - std::swap(data, old.data); - - return *this; - } - - - bool operator== (const variant<F, Ts...>& other) const - { - return helper_t::compareEq(type_id, &data, other.type_id, &other.data); - } - - bool operator< (const variant<F, Ts...>& other) const - { - return helper_t::compareLess(type_id, &data, other.type_id, &other.data); - } - - template<typename T> - bool is() const { - return (type_id == typeid(T).hash_code()); - } - - template<typename T> - void set(T&& value) - { - if(std::is_base_of_any<T, F, Ts...>::value) { - helper_t::destroy(type_id, &data); - new (&data) T(value); - type_id = typeid(T).hash_code(); - } else - throw std::bad_cast(); - } - - template<typename T> - void set(const T& value) - { - if(std::is_base_of_any<T, F, Ts...>::value) { - helper_t::destroy(type_id, &data); - new (&data) T(value); - type_id = typeid(T).hash_code(); - } else - throw std::bad_cast(); - } - - template<typename T> - T& get() - { - // It is a dynamic_cast-like behaviour - if (type_id == typeid(T).hash_code()) - return *reinterpret_cast<T*>(&data); - else - throw std::bad_cast(); - } - - template<typename T> - const T& get() const - { - // It is a dynamic_cast-like behaviour - if (type_id == typeid(T).hash_code()) - return *reinterpret_cast<const T*>(&data); - else - throw std::bad_cast(); - } - - ~variant() { - helper_t::destroy(type_id, &data); - } -}; - -#endif diff --git a/alib/src/string/String.cpp b/alib/src/string/String.cpp deleted file mode 100644 index db3a74b2f45acba3d5b88a6082d6e7a8ade938d8..0000000000000000000000000000000000000000 --- a/alib/src/string/String.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/* - * String.cpp - * - * Created on: Nov 23, 2013 - * Author: Jan Travnicek - */ - -#include <algorithm> - -#include "String.h" -#include "StringPrinter.h" -#include "../AlibException.h" - -namespace notions { -namespace string { - -String::String() { - -} - -void String::addAlphabetSymbol(const alphabet::Symbol& symbol) { - std::pair<std::set<alphabet::Symbol>::iterator, bool> ret = this->alphabet.insert(symbol); - if (!ret.second) - throw alib::AlibException("Alphabet symbol \"" + symbol.getSymbol() + "\" already exists."); -} - -void String::removeAlphabetSymbol(const alphabet::Symbol& symbol) { - auto it = this->alphabet.find( symbol ); - if( it == this->alphabet.end()) - throw alib::AlibException("Symbol \"" + symbol.getSymbol() + "\" is not in alphabet."); - - auto it2 = std::find( this->symbols.begin( ), this->symbols.end( ), symbol ); - if(it2 != this->symbols.end()) - throw alib::AlibException("Symbol \"" + symbol.getSymbol() + "\" is used in string."); - - this->alphabet.erase(symbol); -} - -const std::set<alphabet::Symbol>& String::getAlphabet() const { - return this->alphabet; -} - -std::list<alphabet::Symbol>& String::getContent() { - return this->symbols; -} - -const std::list<alphabet::Symbol>& String::getContent() const { - return this->symbols; -} - -bool String::isEmpty() const { - return this->symbols.size() == 0; -} - -void String::toXML(std::ostream& out) const { - StringPrinter::toXML(*this, out); -} - -std::ostream& operator <<(std::ostream& out, const String& string) { - string.toXML(out); - return out; -} - -} /* namespace string */ -} /* namespace notions */ diff --git a/alib/src/string/String.h b/alib/src/string/String.h deleted file mode 100644 index dc4f603fa024fa85b25b8f0b6df29f50e3980a22..0000000000000000000000000000000000000000 --- a/alib/src/string/String.h +++ /dev/null @@ -1,84 +0,0 @@ -/* - * String.h - * - * Created on: Nov 23, 2013 - * Author: Jan Travnicek - */ - -#ifndef STRING_H_ -#define STRING_H_ - -#include <iostream> -#include <set> -#include <list> - -#include "../alphabet/Symbol.h" - -namespace notions { -namespace string { - -/** - * Represents regular expression parsed from the XML. Regular expression is stored - * as a tree of StringElement. - */ -class String { -private: - std::set<alphabet::Symbol> alphabet; - std::list<alphabet::Symbol> symbols; - -public: - String(); - - /** - * Adds input symbol to the alphabet. - * @param symbol Symbol to add - * @throws AlibException if symbol already exists - */ - void addAlphabetSymbol(const alphabet::Symbol& symbol); - - /** - * Removes input symbol from the the alphabet. - * @param symbol Symbol to remove - * @throws AlibException if symbol is not present in input alphabet - * or when symbol is present in content of the string. - */ - void removeAlphabetSymbol(const alphabet::Symbol& symbol); - - /** - * @return the input alphabet - */ - const std::set<alphabet::Symbol>& getAlphabet() const; - - /** - * @return List of symbols forming string. - */ - std::list<alphabet::Symbol>& getContent(); - - /** - * @return List of symbols forming string (const version). - */ - const std::list<alphabet::Symbol>& getContent() const; - - /** - * @return true if string is an empty word (its length is 0) - */ - bool isEmpty() const; - - /** - * Prints XML representation of the String to the output stream. - * @param out output stream to which print the String - */ - void toXML(std::ostream& out) const; - - /** - * Prints XML representation of the String to the output stream. - * @param out output stream to which print the String - * @param string String to print - */ - friend std::ostream& operator<<(std::ostream& out, const String& string); - -}; - -} /* namespace string */ -} /* namespace notions */ -#endif /* STRING_H_ */ diff --git a/alib/src/string/StringParser.cpp b/alib/src/string/StringParser.cpp deleted file mode 100644 index f894eb47d072a0915ac0600db3cc9988b5091cc2..0000000000000000000000000000000000000000 --- a/alib/src/string/StringParser.cpp +++ /dev/null @@ -1,77 +0,0 @@ -/* - * RegExpParser.cpp - * - * Created on: Nov 23, 2013 - * Author: Martin Zak - */ - -#include "StringParser.h" -#include "../sax/ParserException.h" - -namespace notions { -namespace string { - -String StringParser::parse(std::list<sax::Token>& input) { - popToken(input, sax::Token::START_ELEMENT, "string"); - String string; - parseAlphabet(input, string); - parseContent(input, string); - popToken(input, sax::Token::END_ELEMENT, "string"); - - return string; -} - -void StringParser::parseContent(std::list<sax::Token>& input, String& string) { - popToken(input, sax::Token::START_ELEMENT, "content"); - - while (isToken(input, sax::Token::START_ELEMENT, "symbol")) { - popToken(input, sax::Token::START_ELEMENT, "symbol"); - - if (input.front().getType() == sax::Token::CHARACTER) { - alphabet::Symbol symbol(input.front().getData()); - input.pop_front(); - string.getContent().push_back(symbol); - } else { - throw sax::ParserException(sax::Token("", sax::Token::CHARACTER), input.front()); - } - - popToken(input, sax::Token::END_ELEMENT, "symbol"); - } - - popToken(input, sax::Token::END_ELEMENT, "content"); -} - -void StringParser::parseAlphabet(std::list<sax::Token>& input, String& string) { - popToken(input, sax::Token::START_ELEMENT, "alphabet"); - - while (isToken(input, sax::Token::START_ELEMENT, "symbol")) { - popToken(input, sax::Token::START_ELEMENT, "symbol"); - - if (input.front().getType() == sax::Token::CHARACTER) { - alphabet::Symbol symbol(input.front().getData()); - input.pop_front(); - string.addAlphabetSymbol(symbol); - } else { - throw sax::ParserException(sax::Token("", sax::Token::CHARACTER), input.front()); - } - - popToken(input, sax::Token::END_ELEMENT, "symbol"); - } - - popToken(input, sax::Token::END_ELEMENT, "alphabet"); -} - -bool StringParser::isToken(std::list<sax::Token>& input, sax::Token::TokenType type, std::string data) { - return input.front().getType() == type && input.front().getData() == data; -} - -void StringParser::popToken(std::list<sax::Token>& input, sax::Token::TokenType type, std::string data) { - if (isToken(input, type, data)) { - input.pop_front(); - } else { - throw sax::ParserException(sax::Token(data, type), input.front()); - } -} - -} /* namespace regexp */ -} /* namespace notions */ diff --git a/alib/src/string/StringParser.h b/alib/src/string/StringParser.h deleted file mode 100644 index c45ab84ea8c774304daba1b63d1d3e18d922e6d8..0000000000000000000000000000000000000000 --- a/alib/src/string/StringParser.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * StringParser.h - * - * Created on: Nov 23, 2013 - * Author: Jan Travnicek - */ - -#ifndef STRING_PARSER_H_ -#define STRING_PARSER_H_ - -#include <list> - -#include "String.h" -#include "../sax/Token.h" - -namespace notions { -namespace string { - -/** - * Parser used to get String from XML parsed into list of tokens. - */ -class StringParser { -protected: - static void parseContent(std::list<sax::Token>& input, String& content); - static void parseAlphabet(std::list<sax::Token>& input, String& alphabet); - - static bool isToken(std::list<sax::Token> &input, sax::Token::TokenType type, std::string data); - static void popToken(std::list<sax::Token> &input, sax::Token::TokenType type, std::string data); - -public: - /** - * Parses the XML and returns regular expression. The input is destroyed in the process. - * @param input XML represented as list of tokens - * @return String - * @throws ParserException when an error occurs - */ - static String parse(std::list<sax::Token>& input); -}; - -} /* namespace string */ -} /* namespace notions */ -#endif /* STRING_PARSER_H_ */ diff --git a/alib/src/string/StringPrinter.cpp b/alib/src/string/StringPrinter.cpp deleted file mode 100644 index 3746e5f6a438ad379107c7f9cfa6a3e2ef9ce2f0..0000000000000000000000000000000000000000 --- a/alib/src/string/StringPrinter.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * StringPrinter.cpp - * - * Created on: Nov 23, 2013 - * Author: Jan Travnicek - */ - -#include "StringPrinter.h" - -namespace notions { -namespace string { - -const std::string StringPrinter::INDENTATION = "\t"; - -void StringPrinter::toXML(const String& string, std::ostream& out) { - out << "<string>\n"; - printAlphabet(string, out, INDENTATION); - printContent(string, out, INDENTATION); - out << "</string>\n"; -} - -void StringPrinter::printAlphabet(const String& string, std::ostream& out, const std::string & prefix) { - out << prefix << "<alphabet>\n"; - for (auto element : string.getAlphabet()) { - printSymbol(element, out, prefix + INDENTATION); - } - out << prefix << "</alphabet>\n"; -} - -void StringPrinter::printContent(const String& string, std::ostream& out, const std::string & prefix) { - out << prefix << "<content>\n"; - for (auto element : string.getContent()) { - printSymbol(element, out, prefix + INDENTATION); - } - out << prefix << "</content>\n"; -} - -void StringPrinter::printSymbol(const alphabet::Symbol& element, std::ostream& out, const std::string & prefix) { - out << prefix << "<symbol>" << element.getSymbol() << "</symbol>\n"; -} - -} /* namespace string */ -} /* namespace notions */ diff --git a/alib/src/string/StringPrinter.h b/alib/src/string/StringPrinter.h deleted file mode 100644 index 192fa9fa1988f1eeb45859260ede00b345196400..0000000000000000000000000000000000000000 --- a/alib/src/string/StringPrinter.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * StringPrinter.h - * - * Created on: Nov 23, 2013 - * Author: Jan Travnciek - */ - -#ifndef STRING_PRINTER_H_ -#define STRING_PRINTER_H_ - -#include <ostream> -#include "String.h" - -namespace notions { -namespace string { - -/** - * This class contains methods to print XML representation of string to the output stream. - */ -class StringPrinter { -protected: - static const std::string INDENTATION; - - static void printContent(const String& element, std::ostream& out, const std::string & prefix); - static void printAlphabet(const String& content, std::ostream& out, const std::string & prefix); - static void printSymbol(const alphabet::Symbol& symbol, std::ostream& out, const std::string & prefix); -public: - /** - * Prints XML representation of String to the output stream. - * @param string String to print - * @param out output stream to which print the String - */ - static void toXML(const notions::string::String& regexp, std::ostream& out); -}; - -} /* namespace string */ -} /* namespace notions */ -#endif /* STRING_PRINTER_H_ */