diff --git a/alib2/src/automaton/AutomatonFromXMLParser.cpp b/alib2/src/automaton/AutomatonFromXMLParser.cpp index 7cd16f866fc65567d4fdbff1fc95aec4dadbd3c4..c92624e0577698c4497f840028b42ab19883c2d2 100644 --- a/alib2/src/automaton/AutomatonFromXMLParser.cpp +++ b/alib2/src/automaton/AutomatonFromXMLParser.cpp @@ -13,8 +13,23 @@ #include "../label/Label.h" #include "../alphabet/LabeledSymbol.h" +#include "../FromXMLParsers.h" + namespace automaton { +Automaton AutomatonFromXMLParser::parse(std::list<sax::Token> &input) const { + if(isToken(input, sax::Token::TokenType::START_ELEMENT, "automaton")) + return Automaton(parseUnknownAutomaton(input)); + else if(isToken(input, sax::Token::TokenType::START_ELEMENT, "EpsilonNFA")) + return Automaton(parseEpsilonNFA(input)); + else if(isToken(input, sax::Token::TokenType::START_ELEMENT, "NFA")) + return Automaton(parseNFA(input)); + else if(isToken(input, sax::Token::TokenType::START_ELEMENT, "DFA")) + return Automaton(parseDFA(input)); + else + throw sax::ParserException(sax::Token("EpsilonNFA / NFA / DFA", sax::Token::TokenType::START_ELEMENT), input.front()); +} + Automaton AutomatonFromXMLParser::parseValue(std::list<sax::Token>& input) const { Automaton res = parse(input); @@ -33,19 +48,6 @@ Automaton* AutomatonFromXMLParser::parsePointer(std::list<sax::Token>& input) co } } -Automaton AutomatonFromXMLParser::parse(std::list<sax::Token> &input) const { - if(isToken(input, sax::Token::TokenType::START_ELEMENT, "automaton")) - return Automaton(parseUnknownAutomaton(input)); - else if(isToken(input, sax::Token::TokenType::START_ELEMENT, "EpsilonNFA")) - return Automaton(parseEpsilonNFA(input)); - else if(isToken(input, sax::Token::TokenType::START_ELEMENT, "NFA")) - return Automaton(parseNFA(input)); - else if(isToken(input, sax::Token::TokenType::START_ELEMENT, "DFA")) - return Automaton(parseDFA(input)); - else - throw sax::ParserException(sax::Token("EpsilonNFA / NFA / DFA", sax::Token::TokenType::START_ELEMENT), input.front()); -} - UnknownAutomaton AutomatonFromXMLParser::parseUnknownAutomaton(std::list<sax::Token> &input) const { UnknownAutomaton automaton; @@ -62,15 +64,14 @@ UnknownAutomaton AutomatonFromXMLParser::parseUnknownAutomaton(std::list<sax::To automaton.setFinalStates(parseFinalStates(input)); } else if (isToken(input, sax::Token::TokenType::START_ELEMENT, "stackAlphabet")) { automaton.setStackSymbols(parseStackAlphabet(input)); - } else if (isToken(input, sax::Token::TokenType::START_ELEMENT, "startSymbols")) { - automaton.setInitialSymbols(parseInitialSymbols(input)); + } else if (isToken(input, sax::Token::TokenType::START_ELEMENT, "initialStackSymbols")) { + automaton.setInitialSymbols(parseInitialStackSymbols(input)); } else if (isToken(input, sax::Token::TokenType::START_ELEMENT, "tapeAlphabet")) { automaton.setTapeSymbols(parseTapeAlphabet(input)); } else if (isToken(input, sax::Token::TokenType::START_ELEMENT, "blankSymbol")) { automaton.setBlankSymbol(parseBlankSymbol(input)); } else if (isToken(input, sax::Token::TokenType::START_ELEMENT, "transitions")) { parseTransitions(input, automaton); - } else { throw sax::ParserException(sax::Token("automaton", sax::Token::TokenType::END_ELEMENT), input.front()); } @@ -139,17 +140,11 @@ EpsilonNFA AutomatonFromXMLParser::parseEpsilonNFA(std::list<sax::Token>& input) return automaton; } -std::string AutomatonFromXMLParser::parseLabel(std::list<sax::Token>& input) const { - return popTokenData(input, sax::Token::TokenType::CHARACTER); -} - std::set<State> AutomatonFromXMLParser::parseStates(std::list<sax::Token> &input) const { std::set<State> states; popToken(input, sax::Token::TokenType::START_ELEMENT, "states"); while (isTokenType(input, sax::Token::TokenType::START_ELEMENT)) { - popToken(input, sax::Token::TokenType::START_ELEMENT, "state"); - states.insert(State(parseLabel(input))); - popToken(input, sax::Token::TokenType::END_ELEMENT, "state"); + states.insert(State(alib::FromXMLParsers::labelParser.parse(input))); } popToken(input, sax::Token::TokenType::END_ELEMENT, "states"); return states; @@ -159,9 +154,7 @@ std::set<alphabet::Symbol> AutomatonFromXMLParser::parseInputAlphabet(std::list< std::set<alphabet::Symbol> inputSymbols; popToken(input, sax::Token::TokenType::START_ELEMENT, "inputAlphabet"); while (isTokenType(input, sax::Token::TokenType::START_ELEMENT)) { - popToken(input, sax::Token::TokenType::START_ELEMENT, "symbol"); - inputSymbols.insert(alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel(parseLabel(input)))))); - popToken(input, sax::Token::TokenType::END_ELEMENT, "symbol"); + inputSymbols.insert(alib::FromXMLParsers::symbolParser.parse(input)); } popToken(input, sax::Token::TokenType::END_ELEMENT, "inputAlphabet"); return inputSymbols; @@ -169,7 +162,7 @@ std::set<alphabet::Symbol> AutomatonFromXMLParser::parseInputAlphabet(std::list< State AutomatonFromXMLParser::parseInitialState(std::list<sax::Token> &input) const { popToken(input, sax::Token::TokenType::START_ELEMENT, "initialState"); - State state = State(parseLabel(input)); + State state(alib::FromXMLParsers::labelParser.parse(input)); popToken(input, sax::Token::TokenType::END_ELEMENT, "initialState"); return state; } @@ -178,9 +171,7 @@ std::set<State> AutomatonFromXMLParser::parseInitialStates(std::list<sax::Token> std::set<State> initialStates; popToken(input, sax::Token::TokenType::START_ELEMENT, "initialStates"); while (isTokenType(input, sax::Token::TokenType::START_ELEMENT)) { - popToken(input, sax::Token::TokenType::START_ELEMENT, "state"); - initialStates.insert(State(parseLabel(input))); - popToken(input, sax::Token::TokenType::END_ELEMENT, "state"); + initialStates.insert(State(alib::FromXMLParsers::labelParser.parse(input))); } popToken(input, sax::Token::TokenType::END_ELEMENT, "initialStates"); return initialStates; @@ -190,9 +181,7 @@ std::set<State> AutomatonFromXMLParser::parseFinalStates(std::list<sax::Token> & std::set<State> finalStates; popToken(input, sax::Token::TokenType::START_ELEMENT, "finalStates"); while (isTokenType(input, sax::Token::TokenType::START_ELEMENT)) { - popToken(input, sax::Token::TokenType::START_ELEMENT, "state"); - finalStates.insert(State(parseLabel(input))); - popToken(input, sax::Token::TokenType::END_ELEMENT, "state"); + finalStates.insert(State(alib::FromXMLParsers::labelParser.parse(input))); } popToken(input, sax::Token::TokenType::END_ELEMENT, "finalStates"); return finalStates; @@ -202,33 +191,34 @@ std::set<alphabet::Symbol> AutomatonFromXMLParser::parseStackAlphabet(std::list< std::set<alphabet::Symbol> stackSymbols; popToken(input, sax::Token::TokenType::START_ELEMENT, "stackAlphabet"); while (isTokenType(input, sax::Token::TokenType::START_ELEMENT)) { - popToken(input, sax::Token::TokenType::START_ELEMENT, "symbol"); - stackSymbols.insert(alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel(parseLabel(input)))))); - popToken(input, sax::Token::TokenType::END_ELEMENT, "symbol"); + stackSymbols.insert(alib::FromXMLParsers::symbolParser.parse(input)); } popToken(input, sax::Token::TokenType::END_ELEMENT, "stackAlphabet"); return stackSymbols; } -std::set<alphabet::Symbol> AutomatonFromXMLParser::parseInitialSymbols(std::list<sax::Token> &input) const { +std::set<alphabet::Symbol> AutomatonFromXMLParser::parseInitialStackSymbols(std::list<sax::Token> &input) const { std::set<alphabet::Symbol> initialSymbols; - popToken(input, sax::Token::TokenType::START_ELEMENT, "initialAlphabet"); + popToken(input, sax::Token::TokenType::START_ELEMENT, "initialStackSymbols"); while (isTokenType(input, sax::Token::TokenType::START_ELEMENT)) { - popToken(input, sax::Token::TokenType::START_ELEMENT, "symbol"); - initialSymbols.insert(alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel(parseLabel(input)))))); - popToken(input, sax::Token::TokenType::END_ELEMENT, "symbol"); + initialSymbols.insert(alib::FromXMLParsers::symbolParser.parse(input)); } - popToken(input, sax::Token::TokenType::END_ELEMENT, "initialAlphabet"); + popToken(input, sax::Token::TokenType::END_ELEMENT, "initialStackSymbols"); return initialSymbols; } +alphabet::Symbol AutomatonFromXMLParser::parseInitialStackSymbol(std::list<sax::Token> &input) const { + popToken(input, sax::Token::TokenType::START_ELEMENT, "initialStackSymbol"); + alphabet::Symbol initialSymbol(alib::FromXMLParsers::symbolParser.parse(input)); + popToken(input, sax::Token::TokenType::END_ELEMENT, "initialStackSymbol"); + return initialSymbol; +} + std::set<alphabet::Symbol> AutomatonFromXMLParser::parseTapeAlphabet(std::list<sax::Token> &input) const { std::set<alphabet::Symbol> tapeSymbols; popToken(input, sax::Token::TokenType::START_ELEMENT, "tapeAlphabet"); while (isTokenType(input, sax::Token::TokenType::START_ELEMENT)) { - popToken(input, sax::Token::TokenType::START_ELEMENT, "symbol"); - tapeSymbols.insert(alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel(parseLabel(input)))))); - popToken(input, sax::Token::TokenType::END_ELEMENT, "symbol"); + tapeSymbols.insert(alib::FromXMLParsers::symbolParser.parse(input)); } popToken(input, sax::Token::TokenType::END_ELEMENT, "tapeAlphabet"); return tapeSymbols; @@ -236,18 +226,9 @@ std::set<alphabet::Symbol> AutomatonFromXMLParser::parseTapeAlphabet(std::list<s alphabet::Symbol AutomatonFromXMLParser::parseBlankSymbol(std::list<sax::Token>& input) const { popToken(input, sax::Token::TokenType::START_ELEMENT, "blankSymbol"); - - alphabet::Symbol result = alphabet::Symbol(alphabet::BlankSymbol()); - if (isToken(input, sax::Token::TokenType::START_ELEMENT, "blank")) { - input.pop_front(); - popToken(input, sax::Token::TokenType::END_ELEMENT,"blank"); - } else { - result = alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel(parseLabel(input))))); - } - + alphabet::Symbol blank(alib::FromXMLParsers::symbolParser.parse(input)); popToken(input, sax::Token::TokenType::END_ELEMENT, "blankSymbol"); - - return result; + return blank; } template<class T> @@ -292,7 +273,7 @@ void AutomatonFromXMLParser::parseTransition(std::list<sax::Token>& input, Unkno if (isToken(input, sax::Token::TokenType::START_ELEMENT, "from")) { transition.setFrom(parseTransitionFrom(input)); } else if (isToken(input, sax::Token::TokenType::START_ELEMENT, "input")) { - transition.setInput(parseTransitionInputBlankEpsilonSymbol(input)); + transition.setInput(parseTransitionInputEpsilonSymbol(input)); } else if (isToken(input, sax::Token::TokenType::START_ELEMENT, "to")) { transition.setTo(parseTransitionFrom(input)); } else if (isToken(input, sax::Token::TokenType::START_ELEMENT, "pop")) { @@ -300,7 +281,7 @@ void AutomatonFromXMLParser::parseTransition(std::list<sax::Token>& input, Unkno } else if (isToken(input, sax::Token::TokenType::START_ELEMENT, "push")) { transition.setPush(parseTransitionPush(input)); } else if (isToken(input, sax::Token::TokenType::START_ELEMENT, "output")) { - transition.setOutput(parseTransitionOutputBlankEpsilonSymbol(input)); + transition.setOutput(parseTransitionOutputEpsilonSymbol(input)); } else if (isToken(input, sax::Token::TokenType::START_ELEMENT, "shift")) { transition.setShift(parseTransitionShift(input)); } else { @@ -313,14 +294,14 @@ void AutomatonFromXMLParser::parseTransition(std::list<sax::Token>& input, Unkno State AutomatonFromXMLParser::parseTransitionTo(std::list<sax::Token> &input) const { popToken(input, sax::Token::TokenType::START_ELEMENT, "to"); - State state(parseLabel(input)); + State state(alib::FromXMLParsers::labelParser.parse(input)); popToken(input, sax::Token::TokenType::END_ELEMENT, "to"); return state; } State AutomatonFromXMLParser::parseTransitionFrom(std::list<sax::Token> &input) const { popToken(input, sax::Token::TokenType::START_ELEMENT, "from"); - State state(parseLabel(input)); + State state(alib::FromXMLParsers::labelParser.parse(input)); popToken(input, sax::Token::TokenType::END_ELEMENT, "from"); return state; } @@ -331,16 +312,14 @@ Shift AutomatonFromXMLParser::parseTransitionShift(std::list<sax::Token>& input) popToken(input, sax::Token::TokenType::START_ELEMENT, "shift"); if (isToken(input, sax::Token::TokenType::CHARACTER, "left")) { shift = LEFT; - input.pop_front(); } else if (isToken(input, sax::Token::TokenType::CHARACTER, "right")) { shift = RIGHT; - input.pop_front(); } else if (isToken(input, sax::Token::TokenType::CHARACTER, "none")) { shift = NONE; - input.pop_front(); } else { throw sax::ParserException(sax::Token("", sax::Token::TokenType::CHARACTER), input.front()); } + input.pop_front(); popToken(input, sax::Token::TokenType::END_ELEMENT, "shift"); return shift; @@ -350,9 +329,7 @@ std::vector<alphabet::Symbol> AutomatonFromXMLParser::parseTransitionPop(std::li std::vector<alphabet::Symbol> pops; popToken(input, sax::Token::TokenType::START_ELEMENT, "pop"); while (isTokenType(input, sax::Token::TokenType::START_ELEMENT)) { - popToken(input, sax::Token::TokenType::START_ELEMENT, "symbol"); - pops.push_back(alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel(parseLabel(input)))))); - popToken(input, sax::Token::TokenType::END_ELEMENT, "symbol"); + pops.push_back(alib::FromXMLParsers::symbolParser.parse(input)); } popToken(input, sax::Token::TokenType::END_ELEMENT, "pop"); return pops; @@ -362,63 +339,27 @@ std::vector<alphabet::Symbol> AutomatonFromXMLParser::parseTransitionPush(std::l std::vector<alphabet::Symbol> pushes; popToken(input, sax::Token::TokenType::START_ELEMENT, "push"); while (isTokenType(input, sax::Token::TokenType::START_ELEMENT)) { - popToken(input, sax::Token::TokenType::START_ELEMENT, "symbol"); - pushes.push_back(alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel(parseLabel(input)))))); - popToken(input, sax::Token::TokenType::END_ELEMENT, "symbol"); + pushes.push_back(alib::FromXMLParsers::symbolParser.parse(input)); } popToken(input, sax::Token::TokenType::END_ELEMENT, "push"); return pushes; } -alphabet::Symbol AutomatonFromXMLParser::parseTransitionOutputSymbol(std::list<sax::Token>& input) const { - popToken(input, sax::Token::TokenType::START_ELEMENT, "output"); - alphabet::Symbol result = alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel(parseLabel(input))))); - popToken(input, sax::Token::TokenType::END_ELEMENT, "output"); - return result; -} - alphabet::Symbol AutomatonFromXMLParser::parseTransitionInputSymbol(std::list<sax::Token>& input) const { popToken(input, sax::Token::TokenType::START_ELEMENT, "input"); - alphabet::Symbol result = alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel(parseLabel(input))))); - popToken(input, sax::Token::TokenType::END_ELEMENT, "input"); - return result; -} - -std::variant<string::Epsilon, alphabet::Symbol> AutomatonFromXMLParser::parseTransitionInputEpsilonSymbol(std::list<sax::Token>& input) const { - popToken(input, sax::Token::TokenType::START_ELEMENT, "input"); - - std::variant<string::Epsilon, alphabet::Symbol> result; - if(isToken(input, sax::Token::TokenType::START_ELEMENT, "epsilon")) { - result.set<string::Epsilon>(string::Epsilon()); - input.pop_front(); - popToken(input, sax::Token::TokenType::END_ELEMENT, "epsilon"); - } else { - result.set<alphabet::Symbol>(alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel(parseLabel(input)))))); - } - + alphabet::Symbol result(alib::FromXMLParsers::symbolParser.parse(input)); popToken(input, sax::Token::TokenType::END_ELEMENT, "input"); - return result; } -std::variant<string::Epsilon, alphabet::Symbol> AutomatonFromXMLParser::parseTransitionOutputEpsilonSymbol(std::list<sax::Token>& input) const { +alphabet::Symbol AutomatonFromXMLParser::parseTransitionOutputSymbol(std::list<sax::Token>& input) const { popToken(input, sax::Token::TokenType::START_ELEMENT, "output"); - - std::variant<string::Epsilon, alphabet::Symbol> result; - if(isToken(input, sax::Token::TokenType::START_ELEMENT, "epsilon")) { - result.set<string::Epsilon>(string::Epsilon()); - input.pop_front(); - popToken(input, sax::Token::TokenType::END_ELEMENT, "epsilon"); - } else { - result.set<alphabet::Symbol>(alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel(parseLabel(input)))))); - } - + alphabet::Symbol result(alib::FromXMLParsers::symbolParser.parse(input)); popToken(input, sax::Token::TokenType::END_ELEMENT, "output"); - return result; } -std::variant<string::Epsilon, alphabet::Symbol> AutomatonFromXMLParser::parseTransitionInputBlankEpsilonSymbol(std::list<sax::Token>& input) const { +std::variant<string::Epsilon, alphabet::Symbol> AutomatonFromXMLParser::parseTransitionInputEpsilonSymbol(std::list<sax::Token>& input) const { popToken(input, sax::Token::TokenType::START_ELEMENT, "input"); std::variant<string::Epsilon, alphabet::Symbol> result; @@ -426,12 +367,8 @@ std::variant<string::Epsilon, alphabet::Symbol> AutomatonFromXMLParser::parseTra result.set<string::Epsilon>(string::Epsilon()); input.pop_front(); popToken(input, sax::Token::TokenType::END_ELEMENT, "epsilon"); - } else if (isToken(input, sax::Token::TokenType::START_ELEMENT, "blank")) { - result.set<alphabet::Symbol>(alphabet::Symbol(alphabet::BlankSymbol())); - input.pop_front(); - popToken(input, sax::Token::TokenType::END_ELEMENT,"blank"); } else { - result.set<alphabet::Symbol>(alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel(parseLabel(input)))))); + result.set<alphabet::Symbol>(alib::FromXMLParsers::symbolParser.parse(input)); } popToken(input, sax::Token::TokenType::END_ELEMENT, "input"); @@ -439,7 +376,7 @@ std::variant<string::Epsilon, alphabet::Symbol> AutomatonFromXMLParser::parseTra return result; } -std::variant<string::Epsilon, alphabet::Symbol> AutomatonFromXMLParser::parseTransitionOutputBlankEpsilonSymbol(std::list<sax::Token>& input) const { +std::variant<string::Epsilon, alphabet::Symbol> AutomatonFromXMLParser::parseTransitionOutputEpsilonSymbol(std::list<sax::Token>& input) const { popToken(input, sax::Token::TokenType::START_ELEMENT, "output"); std::variant<string::Epsilon, alphabet::Symbol> result; @@ -447,12 +384,8 @@ std::variant<string::Epsilon, alphabet::Symbol> AutomatonFromXMLParser::parseTra result.set<string::Epsilon>(string::Epsilon()); input.pop_front(); popToken(input, sax::Token::TokenType::END_ELEMENT, "epsilon"); - } else if (isToken(input, sax::Token::TokenType::START_ELEMENT, "blank")) { - result.set<alphabet::Symbol>(alphabet::Symbol(alphabet::BlankSymbol())); - input.pop_front(); - popToken(input, sax::Token::TokenType::END_ELEMENT,"blank"); } else { - result.set<alphabet::Symbol>(alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel(parseLabel(input)))))); + result.set<alphabet::Symbol>(alib::FromXMLParsers::symbolParser.parse(input)); } popToken(input, sax::Token::TokenType::END_ELEMENT, "output"); diff --git a/alib2/src/automaton/AutomatonFromXMLParser.h b/alib2/src/automaton/AutomatonFromXMLParser.h index 743242e006182933922954b7c49470a83c51f632..461b662ef7740945426fe99f00f79733559d5c5a 100644 --- a/alib2/src/automaton/AutomatonFromXMLParser.h +++ b/alib2/src/automaton/AutomatonFromXMLParser.h @@ -32,7 +32,8 @@ protected: std::set<State> parseStates(std::list<sax::Token> &input) const; std::set<alphabet::Symbol> parseInputAlphabet(std::list<sax::Token> &input) const; std::set<alphabet::Symbol> parseStackAlphabet(std::list<sax::Token> &input) const; - std::set<alphabet::Symbol> parseInitialSymbols(std::list<sax::Token> &input) const; + std::set<alphabet::Symbol> parseInitialStackSymbols(std::list<sax::Token> &input) const; + alphabet::Symbol parseInitialStackSymbol(std::list<sax::Token> &input) const; std::set<alphabet::Symbol> parseTapeAlphabet(std::list<sax::Token> &input) const; State parseInitialState(std::list<sax::Token> &input) const; std::set<State> parseInitialStates(std::list<sax::Token> &input) const; diff --git a/alib2/src/automaton/AutomatonToXMLComposer.cpp b/alib2/src/automaton/AutomatonToXMLComposer.cpp index f171f3d772e1a9300fc7d059c458e06af7759c68..802e84f0004469db6c6b0a3d66dee62241161ef8 100644 --- a/alib2/src/automaton/AutomatonToXMLComposer.cpp +++ b/alib2/src/automaton/AutomatonToXMLComposer.cpp @@ -8,63 +8,109 @@ #include "AutomatonToXMLComposer.h" #include "../alphabet/BlankSymbol.h" +#include "../ToXMLComposers.h" + namespace automaton { -void AutomatonToXMLComposer::printStates(std::list<sax::Token>& out, const std::set<State>& states, std::string tagName) const { - out.push_back(sax::Token(tagName, sax::Token::TokenType::START_ELEMENT)); +void AutomatonToXMLComposer::composeStates(std::list<sax::Token>& out, const std::set<State>& states) const { + out.push_back(sax::Token("states", sax::Token::TokenType::START_ELEMENT)); + for (auto& state : states) { + out.splice(out.end(), alib::ToXMLComposers::labelComposer.compose(state.getName())); + } + out.push_back(sax::Token("states", sax::Token::TokenType::END_ELEMENT)); +} + +void AutomatonToXMLComposer::composeInputAlphabet(std::list<sax::Token>& out, const std::set<alphabet::Symbol>& symbols) const { + out.push_back(sax::Token("inputAlphabet", sax::Token::TokenType::START_ELEMENT)); + for (auto& symbol : symbols) { + out.splice(out.end(), alib::ToXMLComposers::symbolComposer.compose(symbol)); + } + out.push_back(sax::Token("inputAlphabet", sax::Token::TokenType::END_ELEMENT)); +} + +void AutomatonToXMLComposer::composeInitialStates(std::list<sax::Token>& out, const std::set<State>& states) const { + out.push_back(sax::Token("initialStates", sax::Token::TokenType::START_ELEMENT)); + for (auto& state : states) { + out.splice(out.end(), alib::ToXMLComposers::labelComposer.compose(state.getName())); + } + out.push_back(sax::Token("initialStates", sax::Token::TokenType::END_ELEMENT)); +} + +void AutomatonToXMLComposer::composeInitialState(std::list<sax::Token>& out, const State& state) const { + out.push_back(sax::Token("initialState", sax::Token::TokenType::START_ELEMENT)); + out.splice(out.end(), alib::ToXMLComposers::labelComposer.compose(state.getName())); + out.push_back(sax::Token("initialState", sax::Token::TokenType::END_ELEMENT)); +} + +void AutomatonToXMLComposer::composeFinalStates(std::list<sax::Token>& out, const std::set<State>& states) const { + out.push_back(sax::Token("finalStates", sax::Token::TokenType::START_ELEMENT)); for (auto& state : states) { - printState(out, state, "state"); + out.splice(out.end(), alib::ToXMLComposers::labelComposer.compose(state.getName())); } - out.push_back(sax::Token(tagName, sax::Token::TokenType::END_ELEMENT)); + out.push_back(sax::Token("finalStates", sax::Token::TokenType::END_ELEMENT)); } -void AutomatonToXMLComposer::printSymbols(std::list<sax::Token>& out, const std::set<alphabet::Symbol>& symbols, std::string tagName) const { - out.push_back(sax::Token(tagName, sax::Token::TokenType::START_ELEMENT)); +void AutomatonToXMLComposer::composeStackAlphabet(std::list<sax::Token>& out, const std::set<alphabet::Symbol>& symbols) const { + out.push_back(sax::Token("stackAlphabet", sax::Token::TokenType::START_ELEMENT)); for (auto& symbol : symbols) { - printSymbol(out, symbol, "symbol"); + out.splice(out.end(), alib::ToXMLComposers::symbolComposer.compose(symbol)); } - out.push_back(sax::Token(tagName, sax::Token::TokenType::END_ELEMENT)); + out.push_back(sax::Token("stackAlphabet", sax::Token::TokenType::END_ELEMENT)); } -void AutomatonToXMLComposer::printSymbols(std::list<sax::Token>& out, const std::vector<alphabet::Symbol>& symbols, std::string tagName) const { - out.push_back(sax::Token(tagName, sax::Token::TokenType::START_ELEMENT)); +void AutomatonToXMLComposer::composeInitialStackSymbols(std::list<sax::Token>& out, const std::set<alphabet::Symbol>& symbols) const { + out.push_back(sax::Token("initialStackSymbols", sax::Token::TokenType::START_ELEMENT)); for (auto& symbol : symbols) { - printSymbol(out, symbol, "symbol"); + out.splice(out.end(), alib::ToXMLComposers::symbolComposer.compose(symbol)); } - out.push_back(sax::Token(tagName, sax::Token::TokenType::END_ELEMENT)); + out.push_back(sax::Token("initialStackSymbols", sax::Token::TokenType::END_ELEMENT)); } -void AutomatonToXMLComposer::printShift(std::list<sax::Token>& out, const Shift shift, std::string tagName) const { - out.push_back(sax::Token(tagName, sax::Token::TokenType::START_ELEMENT)); - out.push_back(sax::Token(SHIFT_NAMES [shift], sax::Token::TokenType::CHARACTER)); - out.push_back(sax::Token(tagName, sax::Token::TokenType::END_ELEMENT)); +void AutomatonToXMLComposer::composeInitialStackSymbol(std::list<sax::Token>& out, const alphabet::Symbol& symbol) const { + out.push_back(sax::Token("initialStackSymbol", sax::Token::TokenType::START_ELEMENT)); + out.splice(out.end(), alib::ToXMLComposers::symbolComposer.compose(symbol)); + out.push_back(sax::Token("initialStackSymbol", sax::Token::TokenType::END_ELEMENT)); +} + +void AutomatonToXMLComposer::composeTapeAlphabet(std::list<sax::Token>& out, const std::set<alphabet::Symbol>& symbols) const { + out.push_back(sax::Token("tapeAlphabet", sax::Token::TokenType::START_ELEMENT)); + for (auto& symbol : symbols) { + out.splice(out.end(), alib::ToXMLComposers::symbolComposer.compose(symbol)); + } + out.push_back(sax::Token("tapeAlphabet", sax::Token::TokenType::END_ELEMENT)); } -void AutomatonToXMLComposer::printTransitions(std::list<sax::Token>& out, const UnknownAutomaton& automaton) const { +void AutomatonToXMLComposer::composeBlankSymbol(std::list<sax::Token>& out, const alphabet::Symbol& symbol) const { + out.push_back(sax::Token("blankSymbol", sax::Token::TokenType::START_ELEMENT)); + out.splice(out.end(), alib::ToXMLComposers::symbolComposer.compose(symbol)); + out.push_back(sax::Token("blankSymbol", sax::Token::TokenType::END_ELEMENT)); +} + +void AutomatonToXMLComposer::composeTransitions(std::list<sax::Token>& out, const UnknownAutomaton& automaton) const { out.push_back(sax::Token("transitions", sax::Token::TokenType::START_ELEMENT)); for (auto& transition : automaton.getTransitions()) { out.push_back(sax::Token("transition", sax::Token::TokenType::START_ELEMENT)); if (transition.hasFrom()) - printState(out, transition.getFrom(), "from"); + composeTransitionFrom(out, transition.getFrom()); if(transition.hasInput()) - printInput(out, transition.getInput(), "input"); + composeTransitionInputEpsilonSymbol(out, transition.getInput()); if(transition.hasTo()) - printState(out, transition.getTo(), "to"); + composeTransitionTo(out, transition.getTo()); if (transition.getPop().size() > 0) - printSymbols(out, transition.getPop(), "pop"); + composeTransitionPop(out, transition.getPop()); if (transition.getPush().size() > 0) - printSymbols(out, transition.getPush(), "push"); + composeTransitionPush(out, transition.getPush()); if (transition.hasOutput()) - printInput(out, transition.getOutput(), "output"); + composeTransitionOutputEpsilonSymbol(out, transition.getOutput()); if (transition.getShift() != Shift::NOT_SET) - printShift(out, transition.getShift(), "shift"); + composeTransitionShift(out, transition.getShift()); out.push_back(sax::Token("transition", sax::Token::TokenType::END_ELEMENT)); } @@ -73,14 +119,14 @@ void AutomatonToXMLComposer::printTransitions(std::list<sax::Token>& out, const } -void AutomatonToXMLComposer::printTransitions(std::list<sax::Token>& out, const DFA& automaton) const { +void AutomatonToXMLComposer::composeTransitions(std::list<sax::Token>& out, const DFA& automaton) const { out.push_back(sax::Token("transitions", sax::Token::TokenType::START_ELEMENT)); for(auto& transition : automaton.getTransitions()) { out.push_back(sax::Token("transition", sax::Token::TokenType::START_ELEMENT)); - printState(out, transition.first.first, "from"); - printInput(out, transition.first.second, "input"); - printState(out, transition.second, "to"); + composeTransitionFrom(out, transition.first.first); + composeTransitionInputSymbol(out, transition.first.second); + composeTransitionTo(out, transition.second); out.push_back(sax::Token("transition", sax::Token::TokenType::END_ELEMENT)); } @@ -88,15 +134,15 @@ void AutomatonToXMLComposer::printTransitions(std::list<sax::Token>& out, const out.push_back(sax::Token("transitions", sax::Token::TokenType::END_ELEMENT)); } -void AutomatonToXMLComposer::printTransitions(std::list<sax::Token>& out, const NFA& automaton) const { +void AutomatonToXMLComposer::composeTransitions(std::list<sax::Token>& out, const NFA& automaton) const { out.push_back(sax::Token("transitions", sax::Token::TokenType::START_ELEMENT)); for(auto& transition : automaton.getTransitions()) { for(auto& targetState: transition.second) { out.push_back(sax::Token("transition", sax::Token::TokenType::START_ELEMENT)); - printState(out, transition.first.first, "from"); - printInput(out, transition.first.second, "input"); - printState(out, targetState, "to"); + composeTransitionFrom(out, transition.first.first); + composeTransitionInputSymbol(out, transition.first.second); + composeTransitionTo(out, targetState); out.push_back(sax::Token("transition", sax::Token::TokenType::END_ELEMENT)); } @@ -105,15 +151,15 @@ void AutomatonToXMLComposer::printTransitions(std::list<sax::Token>& out, const out.push_back(sax::Token("transitions", sax::Token::TokenType::END_ELEMENT)); } -void AutomatonToXMLComposer::printTransitions(std::list<sax::Token>& out, const EpsilonNFA& automaton) const { +void AutomatonToXMLComposer::composeTransitions(std::list<sax::Token>& out, const EpsilonNFA& automaton) const { out.push_back(sax::Token("transitions", sax::Token::TokenType::START_ELEMENT)); for(auto& transition : automaton.getTransitions()) { for(auto& targetState: transition.second) { out.push_back(sax::Token("transition", sax::Token::TokenType::START_ELEMENT)); - printState(out, transition.first.first, "from"); - printInput(out, transition.first.second, "input"); - printState(out, targetState, "to"); + composeTransitionFrom(out, transition.first.first); + composeTransitionInputEpsilonSymbol(out, transition.first.second); + composeTransitionTo(out, targetState); out.push_back(sax::Token("transition", sax::Token::TokenType::END_ELEMENT)); } @@ -122,41 +168,72 @@ void AutomatonToXMLComposer::printTransitions(std::list<sax::Token>& out, const out.push_back(sax::Token("transitions", sax::Token::TokenType::END_ELEMENT)); } -void AutomatonToXMLComposer::printState(std::list<sax::Token>& out, const State& state, std::string tagName) const { - out.push_back(sax::Token(tagName, sax::Token::TokenType::START_ELEMENT)); - out.push_back(sax::Token(state.getName(), sax::Token::TokenType::CHARACTER)); - out.push_back(sax::Token(tagName, sax::Token::TokenType::END_ELEMENT)); +void AutomatonToXMLComposer::composeTransitionTo(std::list<sax::Token>& out, const State& state) const { + out.push_back(sax::Token("to", sax::Token::TokenType::START_ELEMENT)); + out.splice(out.end(), alib::ToXMLComposers::labelComposer.compose(state.getName())); + out.push_back(sax::Token("to", sax::Token::TokenType::END_ELEMENT)); +} + +void AutomatonToXMLComposer::composeTransitionFrom(std::list<sax::Token>& out, const State& state) const { + out.push_back(sax::Token("from", sax::Token::TokenType::START_ELEMENT)); + out.splice(out.end(), alib::ToXMLComposers::labelComposer.compose(state.getName())); + out.push_back(sax::Token("from", sax::Token::TokenType::END_ELEMENT)); +} + +void AutomatonToXMLComposer::composeTransitionShift(std::list<sax::Token>& out, const Shift shift) const { + out.push_back(sax::Token("shift", sax::Token::TokenType::START_ELEMENT)); + out.push_back(sax::Token(SHIFT_NAMES [shift], sax::Token::TokenType::CHARACTER)); + out.push_back(sax::Token("shift", sax::Token::TokenType::END_ELEMENT)); +} + +void AutomatonToXMLComposer::composeTransitionPop(std::list<sax::Token>& out, const std::vector<alphabet::Symbol>& symbols) const { + out.push_back(sax::Token("pop", sax::Token::TokenType::START_ELEMENT)); + for (auto& symbol : symbols) { + out.splice(out.end(), alib::ToXMLComposers::symbolComposer.compose(symbol)); + } + out.push_back(sax::Token("pop", sax::Token::TokenType::END_ELEMENT)); +} + +void AutomatonToXMLComposer::composeTransitionPush(std::list<sax::Token>& out, const std::vector<alphabet::Symbol>& symbols) const { + out.push_back(sax::Token("push", sax::Token::TokenType::START_ELEMENT)); + for (auto& symbol : symbols) { + out.splice(out.end(), alib::ToXMLComposers::symbolComposer.compose(symbol)); + } + out.push_back(sax::Token("push", sax::Token::TokenType::END_ELEMENT)); +} + +void AutomatonToXMLComposer::composeTransitionInputSymbol(std::list<sax::Token>& out, const alphabet::Symbol& symbol) const { + out.push_back(sax::Token("input", sax::Token::TokenType::START_ELEMENT)); + out.splice(out.end(), alib::ToXMLComposers::symbolComposer.compose(symbol)); + out.push_back(sax::Token("input", sax::Token::TokenType::END_ELEMENT)); } -void AutomatonToXMLComposer::printInput(std::list<sax::Token>& out, const alphabet::Symbol& symbol, std::string tagName) const { - out.push_back(sax::Token(tagName, sax::Token::TokenType::START_ELEMENT)); - out.push_back(sax::Token((std::string) symbol, sax::Token::TokenType::CHARACTER)); - out.push_back(sax::Token(tagName, sax::Token::TokenType::END_ELEMENT)); +void AutomatonToXMLComposer::composeTransitionOutputSymbol(std::list<sax::Token>& out, const alphabet::Symbol& symbol) const { + out.push_back(sax::Token("output", sax::Token::TokenType::START_ELEMENT)); + out.splice(out.end(), alib::ToXMLComposers::symbolComposer.compose(symbol)); + out.push_back(sax::Token("output", sax::Token::TokenType::END_ELEMENT)); } -void AutomatonToXMLComposer::printInput(std::list<sax::Token>& out, const std::variant<string::Epsilon, alphabet::Symbol>& symbol, std::string tagName) const { - out.push_back(sax::Token(tagName, sax::Token::TokenType::START_ELEMENT)); +void AutomatonToXMLComposer::composeTransitionInputEpsilonSymbol(std::list<sax::Token>& out, const std::variant<string::Epsilon, alphabet::Symbol>& symbol) const { + out.push_back(sax::Token("input", sax::Token::TokenType::START_ELEMENT)); if(symbol.is<string::Epsilon>()) { out.push_back(sax::Token("epsilon", sax::Token::TokenType::START_ELEMENT)); out.push_back(sax::Token("epsilon", sax::Token::TokenType::END_ELEMENT)); - } else if(symbol.get<alphabet::Symbol>().getSymbol() == alphabet::BlankSymbol::BLANK) { - out.push_back(sax::Token("blank", sax::Token::TokenType::START_ELEMENT)); - out.push_back(sax::Token("blank", sax::Token::TokenType::END_ELEMENT)); } else { - out.push_back(sax::Token((std::string) symbol.get<alphabet::Symbol>(), sax::Token::TokenType::CHARACTER)); + out.splice(out.end(), alib::ToXMLComposers::symbolComposer.compose(symbol.get<alphabet::Symbol>())); } - out.push_back(sax::Token(tagName, sax::Token::TokenType::END_ELEMENT)); + out.push_back(sax::Token("input", sax::Token::TokenType::END_ELEMENT)); } -void AutomatonToXMLComposer::printSymbol(std::list<sax::Token>& out, const alphabet::Symbol& symbol, std::string tagName) const { - out.push_back(sax::Token(tagName, sax::Token::TokenType::START_ELEMENT)); - if(symbol.getSymbol() == alphabet::BlankSymbol::BLANK) { - out.push_back(sax::Token("blank", sax::Token::TokenType::START_ELEMENT)); - out.push_back(sax::Token("blank", sax::Token::TokenType::START_ELEMENT)); +void AutomatonToXMLComposer::composeTransitionOutputEpsilonSymbol(std::list<sax::Token>& out, const std::variant<string::Epsilon, alphabet::Symbol>& symbol) const { + out.push_back(sax::Token("output", sax::Token::TokenType::START_ELEMENT)); + if(symbol.is<string::Epsilon>()) { + out.push_back(sax::Token("epsilon", sax::Token::TokenType::START_ELEMENT)); + out.push_back(sax::Token("epsilon", sax::Token::TokenType::END_ELEMENT)); } else { - out.push_back(sax::Token((std::string) symbol, sax::Token::TokenType::CHARACTER)); + out.splice(out.end(), alib::ToXMLComposers::symbolComposer.compose(symbol.get<alphabet::Symbol>())); } - out.push_back(sax::Token(tagName, sax::Token::TokenType::END_ELEMENT)); + out.push_back(sax::Token("output", sax::Token::TokenType::END_ELEMENT)); } std::list<sax::Token> AutomatonToXMLComposer::compose(const AutomatonBase& automaton) const { @@ -170,31 +247,31 @@ std::list<sax::Token> AutomatonToXMLComposer::compose(const UnknownAutomaton& au out.push_back(sax::Token("automaton", sax::Token::TokenType::START_ELEMENT)); if (automaton.getStates().size() > 0) - printStates(out, automaton.getStates(), "states"); + composeStates(out, automaton.getStates()); if (automaton.getInputSymbols().size() > 0) - printSymbols(out, automaton.getInputSymbols(), "inputAlphabet"); + composeInputAlphabet(out, automaton.getInputSymbols()); if (automaton.getTapeSymbols().size() > 0) - printSymbols(out, automaton.getTapeSymbols(), "tapeAlphabet"); + composeTapeAlphabet(out, automaton.getTapeSymbols()); if (automaton.getStackSymbols().size() > 0) - printSymbols(out, automaton.getStackSymbols(), "stackAlphabet"); + composeStackAlphabet(out, automaton.getStackSymbols()); if (automaton.hasBlankSymbol()) - printSymbol(out, automaton.getBlankSymbol(), "blankSymbol"); + composeBlankSymbol(out, automaton.getBlankSymbol()); if (automaton.getInitialSymbols().size() > 0) - printSymbols(out, automaton.getInitialSymbols(), "startSymbols"); + composeInitialStackSymbols(out, automaton.getInitialSymbols()); if (automaton.getInitialStates().size() > 0) - printStates(out, automaton.getInitialStates(), "initialStates"); + composeInitialStates(out, automaton.getInitialStates()); if (automaton.getFinalStates().size() > 0) - printStates(out, automaton.getFinalStates(), "finalStates"); + composeFinalStates(out, automaton.getFinalStates()); if (automaton.getTransitions().size() > 0) - printTransitions(out, automaton); + composeTransitions(out, automaton); out.push_back(sax::Token("automaton", sax::Token::TokenType::END_ELEMENT)); return out; @@ -204,11 +281,11 @@ std::list<sax::Token> AutomatonToXMLComposer::compose(const DFA& automaton) cons std::list<sax::Token> out; out.push_back(sax::Token("DFA", sax::Token::TokenType::START_ELEMENT)); - printStates(out, automaton.getStates(), "states"); - printSymbols(out, automaton.getInputAlphabet(), "inputAlphabet"); - printState(out, automaton.getInitialState(), "initialState"); - printStates(out, automaton.getFinalStates(), "finalStates"); - printTransitions(out, automaton); + composeStates(out, automaton.getStates()); + composeInputAlphabet(out, automaton.getInputAlphabet()); + composeInitialState(out, automaton.getInitialState()); + composeFinalStates(out, automaton.getFinalStates()); + composeTransitions(out, automaton); out.push_back(sax::Token("DFA", sax::Token::TokenType::END_ELEMENT)); return out; @@ -218,11 +295,11 @@ std::list<sax::Token> AutomatonToXMLComposer::compose(const NFA& automaton) cons std::list<sax::Token> out; out.push_back(sax::Token("NFA", sax::Token::TokenType::START_ELEMENT)); - printStates(out, automaton.getStates(), "states"); - printSymbols(out, automaton.getInputAlphabet(), "inputAlphabet"); - printStates(out, automaton.getInitialStates(), "initialStates"); - printStates(out, automaton.getFinalStates(), "finalStates"); - printTransitions(out, automaton); + composeStates(out, automaton.getStates()); + composeInputAlphabet(out, automaton.getInputAlphabet()); + composeInitialStates(out, automaton.getInitialStates()); + composeFinalStates(out, automaton.getFinalStates()); + composeTransitions(out, automaton); out.push_back(sax::Token("NFA", sax::Token::TokenType::END_ELEMENT)); return out; @@ -232,11 +309,11 @@ std::list<sax::Token> AutomatonToXMLComposer::compose(const EpsilonNFA& automato std::list<sax::Token> out; out.push_back(sax::Token("EpsilonNFA", sax::Token::TokenType::START_ELEMENT)); - printStates(out, automaton.getStates(), "states"); - printSymbols(out, automaton.getInputAlphabet(), "inputAlphabet"); - printStates(out, automaton.getInitialStates(), "initialStates"); - printStates(out, automaton.getFinalStates(), "finalStates"); - printTransitions(out, automaton); + composeStates(out, automaton.getStates()); + composeInputAlphabet(out, automaton.getInputAlphabet()); + composeInitialStates(out, automaton.getInitialStates()); + composeFinalStates(out, automaton.getFinalStates()); + composeTransitions(out, automaton); out.push_back(sax::Token("EpsilonNFA", sax::Token::TokenType::END_ELEMENT)); return out; diff --git a/alib2/src/automaton/AutomatonToXMLComposer.h b/alib2/src/automaton/AutomatonToXMLComposer.h index 6ccaf9ea4f571ef4190b9613f7d851559a6b40b2..b9e1687af4c89e4baa3f94a1c387a1c70f036839 100644 --- a/alib2/src/automaton/AutomatonToXMLComposer.h +++ b/alib2/src/automaton/AutomatonToXMLComposer.h @@ -34,20 +34,31 @@ class AutomatonToXMLComposer : public AutomatonBase::const_visitor_type { void Visit(void*, const OneTapeDTM& automaton) const; protected: - void printStates(std::list<sax::Token>&, const std::set<State>& states, std::string tagName) const; - void printSymbols(std::list<sax::Token>&, const std::set<alphabet::Symbol>& symbols, std::string tagName) const; - void printSymbols(std::list<sax::Token>&, const std::vector<alphabet::Symbol>& symbols, std::string tagName) const; - void printShift(std::list<sax::Token>&, const Shift shift, std::string tagName) const; + void composeStates(std::list<sax::Token>&, const std::set<State>& states) const; + void composeInputAlphabet(std::list<sax::Token>&, const std::set<alphabet::Symbol>& symbols) const; + void composeInitialStates(std::list<sax::Token>&, const std::set<State>& states) const; + void composeInitialState(std::list<sax::Token>&, const State& state) const; + void composeFinalStates(std::list<sax::Token>&, const std::set<State>& states) const; + void composeStackAlphabet(std::list<sax::Token>&, const std::set<alphabet::Symbol>& symbols) const; + void composeInitialStackSymbols(std::list<sax::Token>&, const std::set<alphabet::Symbol>& symbols) const; + void composeInitialStackSymbol(std::list<sax::Token>&, const alphabet::Symbol& symbols) const; + void composeTapeAlphabet(std::list<sax::Token>&, const std::set<alphabet::Symbol>& symbols) const; + void composeBlankSymbol(std::list<sax::Token>&, const alphabet::Symbol& symbol) const; - void printTransitions(std::list<sax::Token>&, const UnknownAutomaton& automaton) const; - void printTransitions(std::list<sax::Token>&, const EpsilonNFA& automaton) const; - void printTransitions(std::list<sax::Token>&, const NFA& automaton) const; - void printTransitions(std::list<sax::Token>&, const DFA& automaton) const; + void composeTransitions(std::list<sax::Token>&, const UnknownAutomaton& automaton) const; + void composeTransitions(std::list<sax::Token>&, const EpsilonNFA& automaton) const; + void composeTransitions(std::list<sax::Token>&, const NFA& automaton) const; + void composeTransitions(std::list<sax::Token>&, const DFA& automaton) const; - void printState(std::list<sax::Token>&, const State& state, std::string tagName) const; - void printSymbol(std::list<sax::Token>&, const alphabet::Symbol& symbol, std::string tagName) const; - void printInput(std::list<sax::Token>& out, const alphabet::Symbol& symbol, std::string tagName) const; - void printInput(std::list<sax::Token>&, const std::variant<string::Epsilon, alphabet::Symbol>&, std::string) const; + void composeTransitionTo(std::list<sax::Token>&, const State& state) const; + void composeTransitionFrom(std::list<sax::Token>&, const State& state) const; + void composeTransitionShift(std::list<sax::Token>&, const Shift shift) const; + void composeTransitionPop(std::list<sax::Token>&, const std::vector<alphabet::Symbol>& symbols) const; + void composeTransitionPush(std::list<sax::Token>&, const std::vector<alphabet::Symbol>& symbols) const; + void composeTransitionInputSymbol(std::list<sax::Token>&, const alphabet::Symbol& symbol) const; + void composeTransitionInputEpsilonSymbol(std::list<sax::Token>&, const std::variant<string::Epsilon, alphabet::Symbol>&) const; + void composeTransitionOutputSymbol(std::list<sax::Token>&, const alphabet::Symbol& symbol) const; + void composeTransitionOutputEpsilonSymbol(std::list<sax::Token>&, const std::variant<string::Epsilon, alphabet::Symbol>&) const; public: /** diff --git a/alib2/src/automaton/FSM/CompactNFA.cpp b/alib2/src/automaton/FSM/CompactNFA.cpp index e415f46bed89234ec6a8912d7fea502a4bfbeb2a..a0bc6fc195a42da21be8331999d3f8e431ef7885 100644 --- a/alib2/src/automaton/FSM/CompactNFA.cpp +++ b/alib2/src/automaton/FSM/CompactNFA.cpp @@ -23,16 +23,16 @@ AutomatonBase* CompactNFA::plunder() && { bool CompactNFA::removeState(const State& state) { if (initialStates.find(state) != initialStates.end()) { - throw AutomatonException("State \"" + state.getName() + "\" is initial state."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is initial state."); } if (finalStates.find(state) != finalStates.end()) { - throw AutomatonException("State \"" + state.getName() + "\" is final state."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is final state."); } for (std::map<std::pair<State, string::String>, std::set<State> >::const_iterator t = transitions.begin(); t != transitions.end(); t++) { if (t->first.first == state || t->second.find(state) != t->second.end()) - throw AutomatonException("State \"" + state.getName() + "\" is used in transition."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is used in transition."); } return states.erase(state); @@ -50,7 +50,7 @@ bool CompactNFA::removeInputSymbol(const alphabet::Symbol& symbol) { bool CompactNFA::addTransition(const State& from, const string::String& input, const State& to) { if (states.find(from) == states.end()) - throw AutomatonException("State \"" + from.getName() + "\" doesn't exist."); + throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist."); std::set<alphabet::Symbol> inputStringAlphabet; std::set_difference(inputAlphabet.begin(), inputAlphabet.end(), input.getAlphabet().begin(), input.getAlphabet().end(), std::inserter(inputStringAlphabet, inputStringAlphabet.end())); @@ -58,7 +58,7 @@ bool CompactNFA::addTransition(const State& from, const string::String& input, c throw AutomatonException("Input string is over different alphabet than automaton"); if (states.find(to) == states.end()) - throw AutomatonException("State \"" + to.getName() + "\" doesn't exist."); + throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist."); std::pair<State, string::String> key = std::make_pair(from, input); @@ -77,7 +77,7 @@ const std::map<std::pair<State, string::String>, std::set<State>>& CompactNFA::g std::map<std::pair<State, string::String>, std::set<State>> CompactNFA::getTransitionsFromState(const State& from) const { if( states.find(from) == states.end()) - throw AutomatonException("State \"" + from.getName() + "\" doesn't exist"); + throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist"); std::map<std::pair<State, string::String>, std::set<State>> transitionsFromState; for (std::map<std::pair<State, string::String>, std::set<State>>::const_iterator transition = transitions.begin(); transition != transitions.end(); @@ -92,7 +92,7 @@ std::map<std::pair<State, string::String>, std::set<State>> CompactNFA::getTrans std::map<std::pair<State, string::String>, std::set<State>> CompactNFA::getTransitionsToState(const State& to) const { if( states.find(to) == states.end()) - throw AutomatonException("State \"" + to.getName() + "\" doesn't exist"); + throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist"); std::map<std::pair<State, string::String>, std::set<State>> transitionsToState; for (std::map<std::pair<State, string::String>, std::set<State>>::const_iterator transition = transitions.begin(); transition != transitions.end(); diff --git a/alib2/src/automaton/FSM/DFA.cpp b/alib2/src/automaton/FSM/DFA.cpp index 1cd714289853c1729d49065c9c60bf4494473c17..b7e15b791c3e6fc01247e1d8f9cd8cebac42ba06 100644 --- a/alib2/src/automaton/FSM/DFA.cpp +++ b/alib2/src/automaton/FSM/DFA.cpp @@ -27,16 +27,16 @@ AutomatonBase* DFA::plunder() && { bool DFA::removeState(const State& state) { if (initialState == state) { - throw AutomatonException("State \"" + state.getName() + "\" is initial state."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is initial state."); } if (finalStates.find(state) != finalStates.end()) { - throw AutomatonException("State \"" + state.getName() + "\" is final state."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is final state."); } for (std::map<std::pair<State, alphabet::Symbol>, State>::const_iterator t = transitions.begin(); t != transitions.end(); t++) { if (t->first.first == state || t->second == state) - throw AutomatonException("State \"" + state.getName() + "\" is used in transition."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is used in transition."); } return states.erase(state); @@ -55,13 +55,13 @@ bool DFA::removeInputSymbol(const alphabet::Symbol& symbol) { bool DFA::addTransition(const State& from, const alphabet::Symbol& input, const State& to) { if (states.find(from) == states.end()) - throw AutomatonException("State \"" + from.getName() + "\" doesn't exist."); + throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist."); if (inputAlphabet.find(input) == inputAlphabet.end()) throw AutomatonException("Input symbol \"" + (std::string) input + "\" doesn't exist."); if (states.find(to) == states.end()) - throw AutomatonException("State \"" + to.getName() + "\" doesn't exist."); + throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist."); std::pair<State, alphabet::Symbol> key = std::make_pair(from, input); @@ -70,7 +70,7 @@ bool DFA::addTransition(const State& from, const alphabet::Symbol& input, const return false; else throw AutomatonException( - "Transition (\"" + from.getName() + "\", \"" + (std::string) input + "\") -> \"" + to.getName() + "Transition (\"" + (std::string) from.getName() + "\", \"" + (std::string) input + "\") -> \"" + (std::string) to.getName() + "\" already exists."); } @@ -86,8 +86,8 @@ bool DFA::removeTransition(const State& from, const alphabet::Symbol& input, con if(transitions.find(key)->second != to) throw AutomatonException( - "Transition (\"" + from.getName() + "\", \"" + (std::string) input - + "\") -> \"" + to.getName() + "\" doesn't exist."); + "Transition (\"" + (std::string) from.getName() + "\", \"" + (std::string) input + + "\") -> \"" + (std::string) to.getName() + "\" doesn't exist."); transitions.erase(key); return true; @@ -99,7 +99,7 @@ const std::map<std::pair<State, alphabet::Symbol>, State>& DFA::getTransitions() std::map<std::pair<State, alphabet::Symbol>, State> DFA::getTransitionsFromState(const State& from) const { if( states.find(from) == states.end()) - throw AutomatonException("State \"" + from.getName() + "\" doesn't exist"); + throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist"); std::map<std::pair<State, alphabet::Symbol>, State> transitionsFromState; for (std::map<std::pair<State, alphabet::Symbol>, State>::const_iterator transition = transitions.begin(); transition != transitions.end(); @@ -114,7 +114,7 @@ std::map<std::pair<State, alphabet::Symbol>, State> DFA::getTransitionsFromState std::map<std::pair<State, alphabet::Symbol>, State> DFA::getTransitionsToState(const State& to) const { if( states.find(to) == states.end()) - throw AutomatonException("State \"" + to.getName() + "\" doesn't exist"); + throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist"); std::map<std::pair<State, alphabet::Symbol>, State> transitionsToState; for (std::map<std::pair<State, alphabet::Symbol>, State>::const_iterator transition = transitions.begin(); transition != transitions.end(); diff --git a/alib2/src/automaton/FSM/EpsilonNFA.cpp b/alib2/src/automaton/FSM/EpsilonNFA.cpp index 5e4a855d4bd0fba83a02000c0b55d0e61be07f5b..e64ba09dcde48be2f11a4b9ecc85364b7cecdb85 100644 --- a/alib2/src/automaton/FSM/EpsilonNFA.cpp +++ b/alib2/src/automaton/FSM/EpsilonNFA.cpp @@ -22,16 +22,16 @@ AutomatonBase* EpsilonNFA::plunder() && { bool EpsilonNFA::removeState(const State& state) { if (initialStates.find(state) != initialStates.end()) { - throw AutomatonException("State \"" + state.getName() + "\" is initial state."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is initial state."); } if (finalStates.find(state) != finalStates.end()) { - throw AutomatonException("State \"" + state.getName() + "\" is final state."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is final state."); } for (std::map<std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> >, std::set<State> >::const_iterator t = transitions.begin(); t != transitions.end(); t++) { if (t->first.first == state || t->second.find(state) != t->second.end()) - throw AutomatonException("State \"" + state.getName() + "\" is used in transition."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is used in transition."); } return states.erase(state); @@ -52,13 +52,13 @@ bool EpsilonNFA::removeInputSymbol(const alphabet::Symbol& symbol) { bool EpsilonNFA::addTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const State& to) { if (states.find(from) == states.end()) - throw AutomatonException("State \"" + from.getName() + "\" doesn't exist."); + throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist."); if (input.is<alphabet::Symbol>() && inputAlphabet.find(input.get<alphabet::Symbol>()) == inputAlphabet.end()) throw AutomatonException("Input symbol \"" + (std::string) input.get<alphabet::Symbol>() + "\" doesn't exist."); if (states.find(to) == states.end()) - throw AutomatonException("State \"" + to.getName() + "\" doesn't exist."); + throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist."); std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> > key = std::make_pair(from, input); @@ -119,7 +119,7 @@ std::map<std::pair<State, alphabet::Symbol>, std::set<State> > EpsilonNFA::getSy std::map<std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> >, std::set<State> > EpsilonNFA::getTransitionsFromState(const State& from) const { if( states.find(from) == states.end()) - throw AutomatonException("State \"" + from.getName() + "\" doesn't exist"); + throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist"); std::map<std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> >, std::set<State> > transitionsFromState; for (std::map<std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> >, std::set<State> >::const_iterator transition = transitions.begin(); transition != transitions.end(); @@ -134,7 +134,7 @@ std::map<std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> >, std std::map<State, std::set<State> > EpsilonNFA::getEpsilonTransitionsFromState(const State& from) const { if( states.find(from) == states.end()) - throw AutomatonException("State \"" + from.getName() + "\" doesn't exist"); + throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist"); std::map<State, std::set<State> > transitionsFromState; for (std::map<std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> >, std::set<State> >::const_iterator transition = transitions.begin(); transition != transitions.end(); @@ -149,7 +149,7 @@ std::map<State, std::set<State> > EpsilonNFA::getEpsilonTransitionsFromState(con std::map<std::pair<State, alphabet::Symbol>, std::set<State> > EpsilonNFA::getSymbolTransitionsFromState(const State& from) const { if( states.find(from) == states.end()) - throw AutomatonException("State \"" + from.getName() + "\" doesn't exist"); + throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist"); std::map<std::pair<State, alphabet::Symbol>, std::set<State> > transitionsFromState; for (std::map<std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> >, std::set<State> >::const_iterator transition = transitions.begin(); transition != transitions.end(); @@ -164,7 +164,7 @@ std::map<std::pair<State, alphabet::Symbol>, std::set<State> > EpsilonNFA::getSy std::map<std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> >, std::set<State> > EpsilonNFA::getTransitionsToState(const State& to) const { if( states.find(to) == states.end()) - throw AutomatonException("State \"" + to.getName() + "\" doesn't exist"); + throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist"); std::map<std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> >, std::set<State> > transitionsToState; for (std::map<std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> >, std::set<State> >::const_iterator transition = transitions.begin(); transition != transitions.end(); @@ -179,7 +179,7 @@ std::map<std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> >, std std::map<State, std::set<State> > EpsilonNFA::getEpsilonTransitionsToState(const State& to) const { if( states.find(to) == states.end()) - throw AutomatonException("State \"" + to.getName() + "\" doesn't exist"); + throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist"); std::map<State, std::set<State> > transitionsToState; for (std::map<std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> >, std::set<State> >::const_iterator transition = transitions.begin(); transition != transitions.end(); @@ -194,7 +194,7 @@ std::map<State, std::set<State> > EpsilonNFA::getEpsilonTransitionsToState(const std::map<std::pair<State, alphabet::Symbol>, std::set<State> > EpsilonNFA::getSymbolTransitionsToState(const State& to) const { if( states.find(to) == states.end()) - throw AutomatonException("State \"" + to.getName() + "\" doesn't exist"); + throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist"); std::map<std::pair<State, alphabet::Symbol>, std::set<State> > transitionsToState; for (std::map<std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> >, std::set<State> >::const_iterator transition = transitions.begin(); transition != transitions.end(); diff --git a/alib2/src/automaton/FSM/ExtendedNFA.cpp b/alib2/src/automaton/FSM/ExtendedNFA.cpp index 92cfd5f787f48b5cbb0e09bb6d72d3fd7294ec9e..a9e6258b357acf6986fabc4961799831bb7e3d02 100644 --- a/alib2/src/automaton/FSM/ExtendedNFA.cpp +++ b/alib2/src/automaton/FSM/ExtendedNFA.cpp @@ -23,16 +23,16 @@ AutomatonBase* ExtendedNFA::plunder() && { bool ExtendedNFA::removeState(const State& state) { if (initialStates.find(state) != initialStates.end()) { - throw AutomatonException("State \"" + state.getName() + "\" is initial state."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is initial state."); } if (finalStates.find(state) != finalStates.end()) { - throw AutomatonException("State \"" + state.getName() + "\" is final state."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is final state."); } for (std::map<std::pair<State, regexp::RegExp>, std::set<State> >::const_iterator t = transitions.begin(); t != transitions.end(); t++) { if (t->first.first == state || t->second.find(state) != t->second.end()) - throw AutomatonException("State \"" + state.getName() + "\" is used in transition."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is used in transition."); } return states.erase(state); @@ -50,7 +50,7 @@ bool ExtendedNFA::removeInputSymbol(const alphabet::Symbol& symbol) { bool ExtendedNFA::addTransition(const State& from, const regexp::RegExp& input, const State& to) { if (states.find(from) == states.end()) - throw AutomatonException("State \"" + from.getName() + "\" doesn't exist."); + throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist."); std::set<alphabet::Symbol> inputRegExpAlphabet; std::set_difference(inputAlphabet.begin(), inputAlphabet.end(), input.getAlphabet().begin(), input.getAlphabet().end(), std::inserter(inputRegExpAlphabet, inputRegExpAlphabet.end())); @@ -58,7 +58,7 @@ bool ExtendedNFA::addTransition(const State& from, const regexp::RegExp& input, throw AutomatonException("Input string is over different alphabet than automaton"); if (states.find(to) == states.end()) - throw AutomatonException("State \"" + to.getName() + "\" doesn't exist."); + throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist."); std::pair<State, regexp::RegExp> key = std::make_pair(from, input); @@ -77,7 +77,7 @@ const std::map<std::pair<State, regexp::RegExp>, std::set<State> >& ExtendedNFA: std::map<std::pair<State, regexp::RegExp>, std::set<State> > ExtendedNFA::getTransitionsFromState(const State& from) const { if( states.find(from) == states.end()) - throw AutomatonException("State \"" + from.getName() + "\" doesn't exist"); + throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist"); std::map<std::pair<State, regexp::RegExp>, std::set<State>> transitionsFromState; for (std::map<std::pair<State, regexp::RegExp>, std::set<State> >::const_iterator transition = transitions.begin(); transition != transitions.end(); @@ -92,7 +92,7 @@ std::map<std::pair<State, regexp::RegExp>, std::set<State> > ExtendedNFA::getTra std::map<std::pair<State, regexp::RegExp>, std::set<State> > ExtendedNFA::getTransitionsToState(const State& to) const { if( states.find(to) == states.end()) - throw AutomatonException("State \"" + to.getName() + "\" doesn't exist"); + throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist"); std::map<std::pair<State, regexp::RegExp>, std::set<State>> transitionsToState; for (std::map<std::pair<State, regexp::RegExp>, std::set<State> >::const_iterator transition = transitions.begin(); transition != transitions.end(); diff --git a/alib2/src/automaton/FSM/NFA.cpp b/alib2/src/automaton/FSM/NFA.cpp index 0b07e0f9e1e78686f4f24023352de98b35892bb1..dd1d8f5837553446673f6b08474e92a4f79c93fa 100644 --- a/alib2/src/automaton/FSM/NFA.cpp +++ b/alib2/src/automaton/FSM/NFA.cpp @@ -21,16 +21,16 @@ AutomatonBase* NFA::plunder() && { bool NFA::removeState(const State& state) { if (initialStates.find(state) != initialStates.end()) { - throw AutomatonException("State \"" + state.getName() + "\" is initial state."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is initial state."); } if (finalStates.find(state) != finalStates.end()) { - throw AutomatonException("State \"" + state.getName() + "\" is final state."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is final state."); } for (std::map<std::pair<State, alphabet::Symbol>, std::set<State> >::const_iterator t = transitions.begin(); t != transitions.end(); t++) { if (t->first.first == state || t->second.find(state) != t->second.end()) - throw AutomatonException("State \"" + state.getName() + "\" is used in transition."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is used in transition."); } return states.erase(state); @@ -48,13 +48,13 @@ bool NFA::removeInputSymbol(const alphabet::Symbol& symbol) { bool NFA::addTransition(const State& from, const alphabet::Symbol& input, const State& to) { if (states.find(from) == states.end()) - throw AutomatonException("State \"" + from.getName() + "\" doesn't exist."); + throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist."); if (inputAlphabet.find(input) == inputAlphabet.end()) throw AutomatonException("Input symbol \"" + (std::string) input + "\" doesn't exist."); if (states.find(to) == states.end()) - throw AutomatonException("State \"" + to.getName() + "\" doesn't exist."); + throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist."); std::pair<State, alphabet::Symbol> key = std::make_pair(from, input); @@ -73,7 +73,7 @@ const std::map<std::pair<State, alphabet::Symbol>, std::set<State> >& NFA::getTr std::map<std::pair<State, alphabet::Symbol>, std::set<State> > NFA::getTransitionsFromState(const State& from) const { if( states.find(from) == states.end()) - throw AutomatonException("State \"" + from.getName() + "\" doesn't exist"); + throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist"); std::map<std::pair<State, alphabet::Symbol>, std::set<State> > transitionsFromState; for (std::map<std::pair<State, alphabet::Symbol>, std::set<State> >::const_iterator transition = transitions.begin(); transition != transitions.end(); @@ -88,7 +88,7 @@ std::map<std::pair<State, alphabet::Symbol>, std::set<State> > NFA::getTransitio std::map<std::pair<State, alphabet::Symbol>, std::set<State> > NFA::getTransitionsToState(const State& to) const { if( states.find(to) == states.end()) - throw AutomatonException("State \"" + to.getName() + "\" doesn't exist"); + throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist"); std::map<std::pair<State, alphabet::Symbol>, std::set<State> > transitionsToState; for (std::map<std::pair<State, alphabet::Symbol>, std::set<State> >::const_iterator transition = transitions.begin(); transition != transitions.end(); diff --git a/alib2/src/automaton/PDA/PDA.cpp b/alib2/src/automaton/PDA/PDA.cpp index f7973688ee247c5c277326ad79dd3a6b8a35821f..f4a6ab35b3320acfb07cd7dd16b7f5878aa28414 100644 --- a/alib2/src/automaton/PDA/PDA.cpp +++ b/alib2/src/automaton/PDA/PDA.cpp @@ -22,19 +22,19 @@ AutomatonBase* PDA::plunder() && { bool PDA::removeState(const State& state) { if (initialStates.find(state) != initialStates.end()) { - throw AutomatonException("State \"" + state.getName() + "\" is initial state."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is initial state."); } if (finalStates.find(state) != finalStates.end()) { - throw AutomatonException("State \"" + state.getName() + "\" is final state."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is final state."); } for (std::map<std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, std::vector<alphabet::Symbol> >, std::set<std::pair<State, std::vector<alphabet::Symbol> > > >::const_iterator transition = transitions.begin(); transition != transitions.end(); transition++) { if (state == std::get<0>(transition->first)) - throw AutomatonException("State \"" + state.getName() + "\" is used in transition."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is used in transition."); for(std::set<std::pair<State, std::vector<alphabet::Symbol> > >::iterator target = transition->second.begin(); target != transition->second.end(); target++) { if(target->first == state) - throw AutomatonException("State \"" + state.getName() + "\" is used in transition."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is used in transition."); } } @@ -96,7 +96,7 @@ const std::set<alphabet::Symbol>& PDA::getStackAlphabet() const { bool PDA::addTransition(const State& from, const alphabet::Symbol& input, const std::vector<alphabet::Symbol> pop, const State& to, const std::vector<alphabet::Symbol> push) { if (states.find(from) == states.end()) { - throw AutomatonException("State \"" + from.getName() + "\" doesn't exist."); + throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist."); } if (inputAlphabet.find(input) == inputAlphabet.end()) { @@ -104,7 +104,7 @@ bool PDA::addTransition(const State& from, const alphabet::Symbol& input, const } if (states.find(to) == states.end()) { - throw AutomatonException("State \"" + to.getName() + "\" doesn't exist."); + throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist."); } for(std::vector<alphabet::Symbol>::const_iterator popSymbol = pop.begin(); popSymbol != pop.end(); popSymbol++) { diff --git a/alib2/src/automaton/TM/OneTapeDTM.cpp b/alib2/src/automaton/TM/OneTapeDTM.cpp index a9992677c5e232a762b5107f996af463190e4cbb..5431ce474c6832e57c8f708f9721da17244df8b8 100644 --- a/alib2/src/automaton/TM/OneTapeDTM.cpp +++ b/alib2/src/automaton/TM/OneTapeDTM.cpp @@ -27,16 +27,16 @@ AutomatonBase* OneTapeDTM::plunder() && { bool OneTapeDTM::removeState(const State& state) { if (initialState == state) { - throw AutomatonException("State \"" + state.getName() + "\" is initial state."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is initial state."); } if (finalStates.find(state) != finalStates.end()) { - throw AutomatonException("State \"" + state.getName() + "\" is final state."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is final state."); } for (std::map<std::pair<State, alphabet::Symbol>, std::tuple<State, alphabet::Symbol, Shift> >::const_iterator transition = transitions.begin(); transition != transitions.end(); transition++) { if (state == transition->first.first || state == std::get<0>(transition->second)) - throw AutomatonException("State \"" + state.getName() + "\" is used in a transition."); + throw AutomatonException("State \"" + (std::string) state.getName() + "\" is used in a transition."); } return states.erase(state); @@ -61,7 +61,7 @@ bool OneTapeDTM::removeTapeSymbol(const alphabet::Symbol& symbol) { bool OneTapeDTM::addTransition(const State& from, const alphabet::Symbol& input, const State& to, const alphabet::Symbol& output, const Shift& shift) { if (states.find(from) == states.end()) { - throw AutomatonException("State \"" + from.getName() + "\" doesn't exist."); + throw AutomatonException("State \"" + (std::string) from.getName() + "\" doesn't exist."); } if (input != blankSymbol) @@ -70,7 +70,7 @@ bool OneTapeDTM::addTransition(const State& from, const alphabet::Symbol& input, } if (states.find(to) == states.end()) { - throw AutomatonException("State \"" + to.getName() + "\" doesn't exist."); + throw AutomatonException("State \"" + (std::string) to.getName() + "\" doesn't exist."); } if (input != blankSymbol) @@ -87,7 +87,7 @@ bool OneTapeDTM::addTransition(const State& from, const alphabet::Symbol& input, return false; else throw AutomatonException( - "Transition (\"" + from.getName() + "\", \"" + (std::string) input + "\") -> ? already exists."); + "Transition (\"" + (std::string) from.getName() + "\", \"" + (std::string) input + "\") -> ? already exists."); } transitions.insert(std::make_pair(key, value)); @@ -103,7 +103,7 @@ bool OneTapeDTM::removeTransition(const State& from, const alphabet::Symbol& inp std::tuple<State, alphabet::Symbol, Shift > value(to, output, shift); if(transitions.find(key)->second != value) throw AutomatonException( - "Transition (\"" + from.getName() + "\", \"" + (std::string) input + "\") -> ? doesn't exists."); + "Transition (\"" + (std::string) from.getName() + "\", \"" + (std::string) input + "\") -> ? doesn't exists."); transitions.erase(key); return true; diff --git a/alib2/src/automaton/common/MultiInitialStates.cpp b/alib2/src/automaton/common/MultiInitialStates.cpp index 53a6f2a3667c57b4d07f986b625bb6d5763551a3..4eb92235e79e77d7251621aaa9688ee2051c34e9 100644 --- a/alib2/src/automaton/common/MultiInitialStates.cpp +++ b/alib2/src/automaton/common/MultiInitialStates.cpp @@ -15,7 +15,7 @@ namespace automaton { bool MultiInitialStates::addInitialState(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."); + throw AutomatonException("State " + (std::string) state.getName() + " cannot be set as initial state. It is not present in the automaton."); } return initialStates.insert(state).second; diff --git a/alib2/src/automaton/common/SingleInitialState.cpp b/alib2/src/automaton/common/SingleInitialState.cpp index bd084a6a801f6da955c5c1b9597d44d500bad96b..2c8ec4997d7c19fcfc080560cee43279688e70a4 100644 --- a/alib2/src/automaton/common/SingleInitialState.cpp +++ b/alib2/src/automaton/common/SingleInitialState.cpp @@ -17,7 +17,7 @@ SingleInitialState::SingleInitialState(const State& initialState) : initialState void SingleInitialState::setInitialState(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."); + throw AutomatonException("State " + (std::string) state.getName() + " cannot be set as initial state. It is not present in the automaton."); } initialState = state; diff --git a/alib2/src/automaton/common/State.cpp b/alib2/src/automaton/common/State.cpp index 2c99e39ebea0a1a6696caee3c6af7daa839d6976..87b7e473316724b69f1f4dfa2f9614f08b04f7fb 100644 --- a/alib2/src/automaton/common/State.cpp +++ b/alib2/src/automaton/common/State.cpp @@ -9,11 +9,10 @@ namespace automaton { -State::State(const std::string& name) { - this->name = name; +State::State(const label::Label& name) : name(name) { } -const std::string& State::getName() const { +const label::Label& State::getName() const { return name; } diff --git a/alib2/src/automaton/common/State.h b/alib2/src/automaton/common/State.h index 7b321c03c0c87231c19a05e91e3243b3490659d5..aecbe2d23cf15f07d9d17a424c148bd5e0970025 100644 --- a/alib2/src/automaton/common/State.h +++ b/alib2/src/automaton/common/State.h @@ -11,6 +11,8 @@ #include <string> #include <ostream> +#include "../../label/Label.h" + namespace automaton { /** @@ -18,10 +20,10 @@ namespace automaton { */ class State { private: - std::string name; + label::Label name; public: - State(const std::string& name); - const std::string& getName() const; + State(const label::Label& name); + const label::Label& getName() const; bool operator < (const State& other) const; bool operator == (const State& other) const; diff --git a/alib2/src/automaton/common/States.cpp b/alib2/src/automaton/common/States.cpp index 2e007bbc4c4daa7ee8b4088d33c05163385095c2..daa263c186b17dd9986f9601121f98c32eb71fff 100644 --- a/alib2/src/automaton/common/States.cpp +++ b/alib2/src/automaton/common/States.cpp @@ -10,6 +10,7 @@ #include <set> #include <algorithm> #include "../AutomatonException.h" +#include "../../label/NextLabel.h" namespace automaton { @@ -43,7 +44,7 @@ const std::set<State>& States::getStates() const { bool States::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."); + throw AutomatonException("State " + (std::string) state.getName() + " cannot be set as final state. It is not present in the automaton."); } return finalStates.insert(state).second; @@ -73,37 +74,17 @@ const std::set<State>& States::getFinalStates() const { return finalStates; } -const State& States::createUniqueState(const std::string& name, bool integerSuffix){ - try{ - State uniqueState(name); - addState(uniqueState); - return * states.find(uniqueState); - } - catch(AutomatonException& e){ - } +State States::createUniqueState(const State& base, const std::set<State>& other) { + label::NextLabel nextLabelCreator; int i = 0; - std::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); - } + do { + label::Label nextLabel = nextLabelCreator.nextLabel(base.getName()); + if(other.count(State(nextLabel)) == 0) + return State(nextLabel); + } while(++i < INT_MAX); - throw AutomatonException("Could not create unique state with name " + name + "." ); + throw AutomatonException("Could not create unique state with base name " + (std::string) base.getName() + "." ); } } /* namespace automaton */ diff --git a/alib2/src/automaton/common/States.h b/alib2/src/automaton/common/States.h index a73f7602ec3e81ce88ffd1be2e1622f128000e5a..4279d8e80fe1ca55ea392cdb265fde7a02c7c5dd 100644 --- a/alib2/src/automaton/common/States.h +++ b/alib2/src/automaton/common/States.h @@ -90,7 +90,7 @@ public: * @throws AutomatonException if state could not be created * @return created state */ - const State& createUniqueState(const std::string& name, bool integerSuffix = false); + static State createUniqueState(const State& base, const std::set<State>& other); }; } /* namespace automaton */ diff --git a/alib2/src/label/NextLabel.cpp b/alib2/src/label/NextLabel.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c08ae9ff5f5c12849e2048431620ba568959631c --- /dev/null +++ b/alib2/src/label/NextLabel.cpp @@ -0,0 +1,48 @@ +/* + * NextLabel.cpp + * + * Created on: Nov 23, 2013 + * Author: Jan Travnicek + */ + +#include "NextLabel.h" +#include "IntegerLabel.h" +#include "StringLabel.h" +#include "CharacterLabel.h" +#include "../std/itos.h" + +namespace label { + +void NextLabel::Visit(void* userData, const IntegerLabel& label) { + Label* &out = *((Label**) userData); + + out = new Label(IntegerLabel(label.getData() + 1)); +} + +void NextLabel::Visit(void* userData, const CharacterLabel& label) { + Label* &out = *((Label**) userData); + + out = new Label(CharacterLabel(label.getData() + 1)); +} + +void NextLabel::Visit(void* userData, const StringLabel& label) { + Label* &out = *((Label**) userData); + + out = new Label(StringLabel(label.getData() + '\'')); +} + +void NextLabel::Visit(void* userData, const Label& label) { + label.getLabel().Accept(userData, *this); + +} + +Label NextLabel::nextLabel(const Label& label) { + Label* out; + Visit((void*) &out, label); + + Label nextLabel(std::move(*out)); + delete out; + return std::move(nextLabel); +} + +} /* namespace label */ diff --git a/alib2/src/label/NextLabel.h b/alib2/src/label/NextLabel.h new file mode 100644 index 0000000000000000000000000000000000000000..62e07d67087fd6a96d20b1cf073e1afa782ba1fe --- /dev/null +++ b/alib2/src/label/NextLabel.h @@ -0,0 +1,37 @@ +/* + * NextLabel.h + * + * Created on: Nov 23, 2013 + * Author: Jan Travnciek + */ + +#ifndef NEXT_LABEL_H_ +#define NEXT_LABEL_H_ + +#include <list> +#include "Label.h" +#include "../sax/Token.h" + +namespace label { + +/** + * This class contains methods to print XML representation of string to the output stream. + */ +class NextLabel : public LabelBase::visitor_type { + void Visit(void*, const Label& label); + void Visit(void*, const StringLabel& label); + void Visit(void*, const IntegerLabel& label); + void Visit(void*, const CharacterLabel& label); + +public: + /** + * Prints XML representation of String to the output stream. + * @param string String to print + * @param out output stream to which print the String + */ + Label nextLabel(const Label& label); +}; + +} /* namespace label */ + +#endif /* NEXT_LABEL_H_ */ diff --git a/alib2/test-src/automaton/AutomatonTest.cpp b/alib2/test-src/automaton/AutomatonTest.cpp index 6b92539852146b1208b10e4140845c145c510d5e..301afd656392708b2a5e2a3d6d6fe9edeb5e3dda 100644 --- a/alib2/test-src/automaton/AutomatonTest.cpp +++ b/alib2/test-src/automaton/AutomatonTest.cpp @@ -13,6 +13,7 @@ #include "factory/AutomatonFactory.h" #include "label/StringLabel.h" +#include "label/IntegerLabel.h" #include "label/Label.h" #include "alphabet/LabeledSymbol.h" @@ -28,7 +29,7 @@ void AutomatonTest::tearDown() { void AutomatonTest::testXMLParser() { automaton::UnknownAutomaton automaton; - automaton.setStates({automaton::State("1"), automaton::State("2"), automaton::State("3")}); + automaton.setStates({automaton::State(label::Label(label::IntegerLabel(1))), automaton::State(label::Label(label::IntegerLabel(2))), automaton::State(label::Label(label::IntegerLabel(3)))}); automaton.setInputSymbols({alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel("a")))), alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel("b"))))}); automaton.addTransition(automaton::UnknownTransition()); @@ -55,18 +56,18 @@ void AutomatonTest::testXMLParser() { } void AutomatonTest::testDFAParser() { - automaton::DFA automaton(automaton::State("1")); + automaton::DFA automaton(automaton::State(label::Label(label::IntegerLabel(1)))); - automaton.addState(automaton::State("1")); - automaton.addState(automaton::State("2")); - automaton.addState(automaton::State("3")); + automaton.addState(automaton::State(label::Label(label::IntegerLabel(1)))); + automaton.addState(automaton::State(label::Label(label::IntegerLabel(2)))); + automaton.addState(automaton::State(label::Label(label::IntegerLabel(3)))); automaton.addInputSymbol(alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel("a"))))); automaton.addInputSymbol(alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel("b"))))); - automaton.addTransition(automaton::State("1"), alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel("a")))), automaton::State("2")); - automaton.addTransition(automaton::State("2"), alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel("b")))), automaton::State("1")); + automaton.addTransition(automaton::State(label::Label(label::IntegerLabel(1))), alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel("a")))), automaton::State(label::Label(label::IntegerLabel(2)))); + automaton.addTransition(automaton::State(label::Label(label::IntegerLabel(2))), alphabet::Symbol(alphabet::LabeledSymbol(label::Label(label::StringLabel("b")))), automaton::State(label::Label(label::IntegerLabel(1)))); - automaton.addFinalState(automaton::State("3")); + automaton.addFinalState(automaton::State(label::Label(label::IntegerLabel(3)))); CPPUNIT_ASSERT( automaton == automaton ); {