From 561c4109d7a5f76b5249f7ee640e69ac82f46202 Mon Sep 17 00:00:00 2001 From: Martin Zak <zakmart1@fit.cvut.cz> Date: Mon, 11 Nov 2013 21:48:32 +0100 Subject: [PATCH] Adds automaton printer, changes xml (changed current/next into from/to in transitions), removes old xml printing code --- acat/src/acat.cpp | 17 +-- alib/src/alphabet/Symbol.cpp | 13 +-- alib/src/alphabet/Symbol.h | 3 - alib/src/automaton/AutomatonParser.cpp | 8 +- alib/src/automaton/AutomatonPrinter.cpp | 137 +++++++++++++++++++++++ alib/src/automaton/AutomatonPrinter.h | 45 ++++++++ alib/src/automaton/FSM/FSM.cpp | 33 +----- alib/src/automaton/FSM/TransitionFSM.cpp | 12 -- alib/src/automaton/FSM/TransitionFSM.h | 2 - alib/src/automaton/PDA/PDA.cpp | 45 +------- alib/src/automaton/PDA/TransitionPDA.cpp | 28 ----- alib/src/automaton/PDA/TransitionPDA.h | 2 - alib/src/automaton/State.cpp | 4 - alib/src/automaton/State.h | 2 - alib/src/automaton/TM/TM.cpp | 43 +------ alib/src/automaton/TM/TM.h | 2 +- alib/src/automaton/TM/TransitionTM.cpp | 14 --- alib/src/automaton/TM/TransitionTM.h | 2 - alib/src/automaton/Transition.h | 2 - alib/src/grammar/Grammar.cpp | 5 + alib/src/grammar/Grammar.h | 2 + alib/src/grammar/GrammarPrinter.cpp | 20 ++-- alib/src/grammar/GrammarPrinter.h | 2 +- examples/FSM.xml | 32 +++--- examples/FSM2.xml | 36 +++--- examples/NFSM1.xml | 20 ++-- examples/NFSM2.xml | 40 +++---- examples/NFSM3.xml | 28 ++--- examples/NFSM4.xml | 48 ++++---- examples/PDA.xml | 12 +- examples/TM.xml | 16 +-- examples/TM2.xml | 40 +++---- examples/TM3.xml | 12 +- 33 files changed, 369 insertions(+), 358 deletions(-) create mode 100644 alib/src/automaton/AutomatonPrinter.cpp create mode 100644 alib/src/automaton/AutomatonPrinter.h diff --git a/acat/src/acat.cpp b/acat/src/acat.cpp index d557e383d7..b93e7b3084 100644 --- a/acat/src/acat.cpp +++ b/acat/src/acat.cpp @@ -13,7 +13,6 @@ #include "automaton/AutomatonParser.h" #include "grammar/GrammarParser.h" -#include "grammar/GrammarPrinter.h" #include "grammar/RightRegularGrammar.h" #include "sax/SaxInterface.h" @@ -39,19 +38,23 @@ int main(int argc, char** argv) { SaxInterface::parseFile(argv[1], tokens); } else { - string input(istreambuf_iterator<char>(cin), (istreambuf_iterator<char>())); + string input(istreambuf_iterator<char>(cin), + (istreambuf_iterator<char>())); SaxInterface::parseMemory(input, tokens); } - if(tokens.front().getData() == "automaton") { + if (tokens.front().getData() == "automaton") { UnknownAutomaton unknownAutomaton = AutomatonParser::parse(tokens); - Automaton* automaton = AutomatonFactory::buildAutomaton(&unknownAutomaton); + Automaton* automaton = AutomatonFactory::buildAutomaton( + &unknownAutomaton); automaton->toXML(cout); - } else if(tokens.front().getData() == "grammar") { + } else if (tokens.front().getData() == "grammar") { UnknownGrammar grammar = GrammarParser::parse(tokens); - GrammarPrinter::toXML(grammar,cout); + grammar.toXML(cout); } else { - throw AlibException("Expected root tag automaton or grammar. Read: " + tokens.front().getData()); + throw AlibException( + "Expected root tag automaton or grammar. Read: " + + tokens.front().getData()); } } catch (AlibException& e) { diff --git a/alib/src/alphabet/Symbol.cpp b/alib/src/alphabet/Symbol.cpp index d881a9e179..e8c227a6b4 100644 --- a/alib/src/alphabet/Symbol.cpp +++ b/alib/src/alphabet/Symbol.cpp @@ -9,7 +9,8 @@ namespace alphabet { -Symbol::Symbol() : symbol("") { +Symbol::Symbol() : + symbol("") { } @@ -21,20 +22,16 @@ const std::string& Symbol::getSymbol() const { return symbol; } -bool Symbol::operator < (const Symbol& other) const { +bool Symbol::operator <(const Symbol& other) const { return symbol < other.symbol; } -bool Symbol::operator == (const Symbol& other) const { +bool Symbol::operator ==(const Symbol& other) const { return symbol == other.symbol; } -bool Symbol::operator != (const Symbol& other) const { +bool Symbol::operator !=(const Symbol& other) const { return symbol != other.symbol; } -void Symbol::toXML(std::ostream& out, const std::string& indent) const { - out << indent << "<symbol>" << symbol << "</symbol>\n"; -} - } /* namespace language */ diff --git a/alib/src/alphabet/Symbol.h b/alib/src/alphabet/Symbol.h index e0bec915cb..a404ea7b21 100644 --- a/alib/src/alphabet/Symbol.h +++ b/alib/src/alphabet/Symbol.h @@ -24,9 +24,6 @@ public: bool operator <(const Symbol& other) const; bool operator ==(const Symbol& other) const; bool operator !=(const Symbol& other) const; - - void toXML(std::ostream& out, const std::string& indent) const; - }; } #endif /* SYMBOL_H_ */ diff --git a/alib/src/automaton/AutomatonParser.cpp b/alib/src/automaton/AutomatonParser.cpp index 9ad56bd03d..425621665a 100644 --- a/alib/src/automaton/AutomatonParser.cpp +++ b/alib/src/automaton/AutomatonParser.cpp @@ -206,12 +206,12 @@ UnknownTransition AutomatonParser::parseTransition(list<Token>& input) { while (true) { if (isToken(input, Token::END_ELEMENT, "transition")) { break; - } else if (isToken(input, Token::START_ELEMENT, "current")) { - transition.setFrom(parseState(input, "current")); + } 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, "next")) { - transition.setTo(parseState(input, "next")); + } 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")) { diff --git a/alib/src/automaton/AutomatonPrinter.cpp b/alib/src/automaton/AutomatonPrinter.cpp new file mode 100644 index 0000000000..8ee7593f5c --- /dev/null +++ b/alib/src/automaton/AutomatonPrinter.cpp @@ -0,0 +1,137 @@ +/* + * 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::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 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 new file mode 100644 index 0000000000..cb3cfd59ba --- /dev/null +++ b/alib/src/automaton/AutomatonPrinter.h @@ -0,0 +1,45 @@ +/* + * AutomatonPrinter.h + * + * Created on: Nov 11, 2013 + * Author: martin + */ + +#ifndef AUTOMATONPRINTER_H_ +#define AUTOMATONPRINTER_H_ + +#include <ostream> +#include "FSM/FSM.h" +#include "PDA/PDA.h" +#include "TM/TM.h" + +namespace automaton { + +using namespace std; + +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 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: + static void toXML(const FSM& automaton, ostream& out); + static void toXML(const PDA& automaton, ostream& out); + 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 index cc608131b0..9cc2d61a6b 100644 --- a/alib/src/automaton/FSM/FSM.cpp +++ b/alib/src/automaton/FSM/FSM.cpp @@ -6,6 +6,7 @@ */ #include "FSM.h" +#include "../AutomatonPrinter.h" #include "../exception/AutomatonException.h" #include <ostream> @@ -96,37 +97,7 @@ const set<TransitionFSM>& FSM::getTransitions() const { } void FSM::toXML(ostream& out) const { - out << "<automaton>\n"; - out << "\t<states>\n"; - for (set<State>::const_iterator state = states.begin(); state != states.end(); state++) { - state->toXML(out, "\t\t"); - } - out << "\t</states>\n"; - - out << "\t<inputAlphabet>\n"; - for (set<Symbol>::const_iterator inputSymbol = inputAlphabet.begin(); inputSymbol != inputAlphabet.end(); inputSymbol++) { - inputSymbol->toXML(out, "\t\t"); - } - out << "\t</inputAlphabet>\n"; - - out << "\t<transitions>\n"; - for (set<TransitionFSM>::const_iterator transition = transitions.begin(); transition != transitions.end(); transition++) { - transition->toXML(out, "\t\t"); - } - out << "\t</transitions>\n"; - - out << "\t<initialStates>\n"; - for (set<State>::const_iterator initialState = initialStates.begin(); initialState != initialStates.end(); initialState++) { - initialState->toXML(out, "\t\t"); - } - out << "\t</initialStates>\n"; - - out << "\t<finalStates>\n"; - for (set<State>::const_iterator finalState = finalStates.begin(); finalState != finalStates.end(); finalState++) { - finalState->toXML(out, "\t\t"); - } - out << "\t</finalStates>\n"; - out << "</automaton>\n"; + AutomatonPrinter::toXML(*this, out); } } /* namespace automaton */ diff --git a/alib/src/automaton/FSM/TransitionFSM.cpp b/alib/src/automaton/FSM/TransitionFSM.cpp index d34b02cd42..950496a955 100644 --- a/alib/src/automaton/FSM/TransitionFSM.cpp +++ b/alib/src/automaton/FSM/TransitionFSM.cpp @@ -29,16 +29,4 @@ bool TransitionFSM::operator != (const TransitionFSM& other) const { return from != other.from || input != other.input || to != other.to; } -void TransitionFSM::toXML(std::ostream& out, const std::string& indent) const { - out << indent << "<transition>\n"; - out << indent << "\t" << "<current>" << from.getName() << "</current>\n"; - if(input.getSymbol() == "") { - out << indent << "\t" << "<input>" << "<eps/>" << "</input>\n"; - } else { - out << indent << "\t" << "<input>" << input.getSymbol() << "</input>\n"; - } - out << indent << "\t" << "<next>" << to.getName() << "</next>\n"; - out << indent << "</transition>\n"; -} - } /* namespace automaton */ diff --git a/alib/src/automaton/FSM/TransitionFSM.h b/alib/src/automaton/FSM/TransitionFSM.h index d9dd3517e1..960be901d5 100644 --- a/alib/src/automaton/FSM/TransitionFSM.h +++ b/alib/src/automaton/FSM/TransitionFSM.h @@ -20,8 +20,6 @@ public: bool operator <(const TransitionFSM& other) const; bool operator ==(const TransitionFSM& other) const; bool operator !=(const TransitionFSM& other) const; - - void toXML(std::ostream& out, const std::string& indent) const; }; } /* namespace automaton */ diff --git a/alib/src/automaton/PDA/PDA.cpp b/alib/src/automaton/PDA/PDA.cpp index 8227670102..7866cd1cfb 100644 --- a/alib/src/automaton/PDA/PDA.cpp +++ b/alib/src/automaton/PDA/PDA.cpp @@ -6,6 +6,7 @@ */ #include "PDA.h" +#include "../AutomatonPrinter.h" #include "../exception/AutomatonException.h" #include <algorithm> @@ -151,49 +152,7 @@ const list<Symbol>& PDA::getStartSymbols() const { } void PDA::toXML(ostream& out) const { - out << "<automaton>\n"; - out << "\t<states>\n"; - for (set<State>::const_iterator st = states.begin(); st != states.end(); st++) { - st->toXML(out, "\t\t"); - } - out << "\t</states>\n"; - - out << "\t<inputAlphabet>\n"; - for (set<Symbol>::const_iterator alpha = inputAlphabet.begin(); alpha != inputAlphabet.end(); alpha++) { - alpha->toXML(out, "\t\t"); - } - out << "\t</inputAlphabet>\n"; - - out << "\t<stackAlphabet>\n"; - for (set<Symbol>::const_iterator alpha = stackAlphabet.begin(); alpha != stackAlphabet.end(); alpha++) { - alpha->toXML(out, "\t\t"); - } - out << "\t</stackAlphabet>\n"; - - out << "\t<transitions>\n"; - for (set<TransitionPDA>::const_iterator tr = transitions.begin(); tr != transitions.end(); tr++) { - tr->toXML(out, "\t\t"); - } - out << "\t</transitions>\n"; - - out << "\t<initialStates>\n"; - for (set<State>::const_iterator ist = initialStates.begin(); ist != initialStates.end(); ist++) { - ist->toXML(out, "\t\t"); - } - out << "\t</initialStates>\n"; - - out << "\t<startSymbols>\n"; - for (list<Symbol>::const_iterator ss = startSymbols.begin(); ss != startSymbols.end(); ss++) { - ss->toXML(out, "\t\t"); - } - out << "\t</startSymbols>\n"; - - out << "\t<finalStates>\n"; - for (set<State>::const_iterator fst = finalStates.begin(); fst != finalStates.end(); fst++) { - fst->toXML(out, "\t\t"); - } - out << "\t</finalStates>\n"; - out << "</automaton>\n"; + AutomatonPrinter::toXML(*this, out); } } /* namespace automaton */ diff --git a/alib/src/automaton/PDA/TransitionPDA.cpp b/alib/src/automaton/PDA/TransitionPDA.cpp index 10779064b4..0cfec709b6 100644 --- a/alib/src/automaton/PDA/TransitionPDA.cpp +++ b/alib/src/automaton/PDA/TransitionPDA.cpp @@ -120,32 +120,4 @@ bool TransitionPDA::operator !=(const TransitionPDA& other) const { return !((*this) == other); } -void TransitionPDA::toXML(ostream& out, const string& indent) const { - out << indent << "<transition>\n"; - out << indent << "\t" << "<current>" << from.getName() << "</current>\n"; - if (input.getSymbol() == "") { - out << indent << "\t" << "<input>" << "<eps/>" << "</input>\n"; - } else { - out << indent << "\t" << "<input>" << input.getSymbol() << "</input>\n"; - } - out << indent << "\t" << "<next>" << to.getName() << "</next>\n"; - - out << indent << "\t" << "<pop>\n"; - list<Symbol>::const_iterator it = pop.begin(); - while (it != pop.end()) { - (*it).toXML(out, indent + "\t\t"); - it++; - } - out << indent << "\t" << "</pop>\n"; - - out << indent << "\t" << "<push>\n"; - it = push.begin(); - while (it != push.end()) { - (*it).toXML(out, indent + "\t\t"); - it++; - } - out << indent << "\t" << "</push>\n"; - out << indent << "</transition>\n"; -} - } /* namespace automaton */ diff --git a/alib/src/automaton/PDA/TransitionPDA.h b/alib/src/automaton/PDA/TransitionPDA.h index 0fba5f3401..8f2f0b5614 100644 --- a/alib/src/automaton/PDA/TransitionPDA.h +++ b/alib/src/automaton/PDA/TransitionPDA.h @@ -32,8 +32,6 @@ public: bool operator <(const TransitionPDA& other) const; bool operator ==(const TransitionPDA& other) const; bool operator !=(const TransitionPDA& other) const; - - virtual void toXML(ostream& out, const string& indent) const; }; } /* namespace automaton */ diff --git a/alib/src/automaton/State.cpp b/alib/src/automaton/State.cpp index 520f5c3e0c..4c2a3fb370 100644 --- a/alib/src/automaton/State.cpp +++ b/alib/src/automaton/State.cpp @@ -28,8 +28,4 @@ bool State::operator != (const State& other) const{ return name != other.name; } -void State::toXML(std::ostream& out, const std::string& indent) const { - out << indent << "<state>" << name << "</state>\n"; -} - } /* namespace automaton */ diff --git a/alib/src/automaton/State.h b/alib/src/automaton/State.h index a66d26366d..6ffad3c43f 100644 --- a/alib/src/automaton/State.h +++ b/alib/src/automaton/State.h @@ -26,8 +26,6 @@ public: bool operator < (const State& other) const; bool operator == (const State& other) const; bool operator != (const State& other) const; - - void toXML(std::ostream& out, const std::string& indent) const; }; } /* namespace automaton */ diff --git a/alib/src/automaton/TM/TM.cpp b/alib/src/automaton/TM/TM.cpp index b5855f9cb2..02f80f6167 100644 --- a/alib/src/automaton/TM/TM.cpp +++ b/alib/src/automaton/TM/TM.cpp @@ -6,6 +6,7 @@ */ #include "TM.h" +#include "../AutomatonPrinter.h" #include "../exception/AutomatonException.h" namespace automaton { @@ -85,7 +86,7 @@ void TM::removeTapeSymbol(const Symbol& symbol) { } -const std::set<Symbol>& TM::getTapeSymbols() const { +const std::set<Symbol>& TM::getTapeAlphabet() const { return tapeAlphabet; } @@ -142,45 +143,7 @@ const Symbol& TM::getBlankSymbol() const { } void TM::toXML(std::ostream& out) const { - out << "<automaton>\n"; - out << "\t<states>\n"; - for (set<State>::const_iterator st = states.begin(); st != states.end(); st++) { - st->toXML(out, "\t\t"); - } - out << "\t</states>\n"; - - out << "\t<tapeAlphabet>\n"; - for (set<Symbol>::const_iterator alpha = tapeAlphabet.begin(); alpha != tapeAlphabet.end(); alpha++) { - alpha->toXML(out, "\t\t"); - } - out << "\t</tapeAlphabet>\n"; - - out << "\t<inputAlphabet>\n"; - for (set<Symbol>::const_iterator alpha = inputAlphabet.begin(); alpha != inputAlphabet.end(); alpha++) { - alpha->toXML(out, "\t\t"); - } - out << "\t</inputAlphabet>\n"; - - out << "\t<transitions>\n"; - for (set<TransitionTM>::const_iterator tr = transitions.begin(); tr != transitions.end(); tr++) { - tr->toXML(out, "\t\t"); - } - out << "\t</transitions>\n"; - - out << "\t<initialStates>\n"; - for (set<State>::const_iterator ist = initialStates.begin(); ist != initialStates.end(); ist++) { - ist->toXML(out, "\t\t"); - } - out << "\t</initialStates>\n"; - - out << "\t<blankSymbol>" << blankSymbol.getSymbol() << "</blankSymbol>\n"; - - out << "\t<finalStates>\n"; - for (set<State>::const_iterator fst = finalStates.begin(); fst != finalStates.end(); fst++) { - fst->toXML(out, "\t\t"); - } - out << "\t</finalStates>\n"; - out << "</automaton>\n"; + AutomatonPrinter::toXML(*this, out); } } /* namespace automaton */ diff --git a/alib/src/automaton/TM/TM.h b/alib/src/automaton/TM/TM.h index b774012cf9..94b2531329 100644 --- a/alib/src/automaton/TM/TM.h +++ b/alib/src/automaton/TM/TM.h @@ -31,7 +31,7 @@ public: void addTapeSymbol(const Symbol& symbol); void removeTapeSymbol(const Symbol& symbol); - const set<Symbol>& getTapeSymbols() const; + const set<Symbol>& getTapeAlphabet() const; void addTransition(const TransitionTM& transition); void removeTransition(const TransitionTM& transition); diff --git a/alib/src/automaton/TM/TransitionTM.cpp b/alib/src/automaton/TM/TransitionTM.cpp index 7f17802a0b..2c67ffdafa 100644 --- a/alib/src/automaton/TM/TransitionTM.cpp +++ b/alib/src/automaton/TM/TransitionTM.cpp @@ -49,18 +49,4 @@ bool TransitionTM::operator !=(const TransitionTM& other) const { return !((*this) == other); } -void TransitionTM::toXML(std::ostream& out, const std::string& indent) const { - out << indent << "<transition>\n"; - out << indent << "\t" << "<current>" << from.getName() << "</current>\n"; - if (input.getSymbol() == "") { - out << indent << "\t" << "<input>" << "<eps/>" << "</input>\n"; - } else { - out << indent << "\t" << "<input>" << input.getSymbol() << "</input>\n"; - } - out << indent << "\t" << "<next>" << to.getName() << "</next>\n"; - out << indent << "\t" << "<output>" << output.getSymbol() << "</output>\n"; - out << indent << "\t" << "<shift>" << (std::string[]){"left","right","none"}[shift] << "</shift>\n"; - out << indent << "</transition>\n"; -} - } /* namespace automaton */ diff --git a/alib/src/automaton/TM/TransitionTM.h b/alib/src/automaton/TM/TransitionTM.h index 7291f56ed5..192bc4a5ac 100644 --- a/alib/src/automaton/TM/TransitionTM.h +++ b/alib/src/automaton/TM/TransitionTM.h @@ -30,8 +30,6 @@ public: bool operator <(const TransitionTM& other) const; bool operator ==(const TransitionTM& other) const; bool operator !=(const TransitionTM& other) const; - - virtual void toXML(ostream& out, const string& indent) const; }; } /* namespace automaton */ diff --git a/alib/src/automaton/Transition.h b/alib/src/automaton/Transition.h index 031b328b6e..c8146b6f16 100644 --- a/alib/src/automaton/Transition.h +++ b/alib/src/automaton/Transition.h @@ -32,8 +32,6 @@ public: const State& getTo() const; bool containsState(const State& state) const; - - virtual void toXML(std::ostream& out, const std::string& prefix) const = 0; }; } /* namespace automaton */ diff --git a/alib/src/grammar/Grammar.cpp b/alib/src/grammar/Grammar.cpp index c268cb6428..4b02480f47 100644 --- a/alib/src/grammar/Grammar.cpp +++ b/alib/src/grammar/Grammar.cpp @@ -7,6 +7,7 @@ #include "Grammar.h" #include "GrammarException.h" +#include "GrammarPrinter.h" namespace grammar { @@ -108,4 +109,8 @@ void Grammar::setStartSymbol(const Symbol& symbol) { startSymbol = symbol; } +void Grammar::toXML(ostream& out) const { + GrammarPrinter::toXML(*this, out); +} + } /* namespace grammar */ diff --git a/alib/src/grammar/Grammar.h b/alib/src/grammar/Grammar.h index c1661cf380..a16c91bce0 100644 --- a/alib/src/grammar/Grammar.h +++ b/alib/src/grammar/Grammar.h @@ -44,6 +44,8 @@ public: const Symbol& getStartSymbol() const; void setStartSymbol(const Symbol& symbol); + + virtual void toXML(ostream& out) const; }; } /* namespace grammar */ diff --git a/alib/src/grammar/GrammarPrinter.cpp b/alib/src/grammar/GrammarPrinter.cpp index fea3634a9a..0dea544f06 100644 --- a/alib/src/grammar/GrammarPrinter.cpp +++ b/alib/src/grammar/GrammarPrinter.cpp @@ -16,14 +16,14 @@ void GrammarPrinter::toXML(const Grammar& grammar, ostream& out) { printNonTerminalSymbols(grammar, out, INDENTATION); printTerminalSymbols(grammar, out, INDENTATION); printRules(grammar, out, INDENTATION); - printStartSymbol(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()) { - symbol.toXML(out, prefix + INDENTATION); + printSymbol(symbol, out, "symbol", prefix + INDENTATION); } out << prefix << "</nonTerminalSymbols>\n"; } @@ -31,9 +31,9 @@ void GrammarPrinter::printNonTerminalSymbols(const Grammar& grammar, ostream& ou void GrammarPrinter::printTerminalSymbols(const Grammar& grammar, ostream& out, string prefix) { out << prefix << "<terminalSymbols>\n"; for (auto& symbol : grammar.getTerminalSymbols()) { - symbol.toXML(out, prefix + INDENTATION); + printSymbol(symbol, out, "symbol", prefix + INDENTATION); } - out << prefix <<"</terminalSymbols>\n"; + out << prefix << "</terminalSymbols>\n"; } void GrammarPrinter::printRules(const Grammar& grammar, ostream& out, string prefix) { @@ -48,21 +48,21 @@ void GrammarPrinter::printRule(const Rule& rule, ostream& out, string prefix) { out << prefix << "<rule>\n"; out << prefix << INDENTATION << "<leftSide>\n"; for (auto& symbol : rule.getLeftSide()) { - symbol.toXML(out, prefix + INDENTATION + INDENTATION); + printSymbol(symbol, out, "symbol", prefix + INDENTATION + INDENTATION); } out << prefix << INDENTATION << "</leftSide>\n"; out << prefix << INDENTATION << "<rightSide>\n"; for (auto& symbol : rule.getRightSide()) { - symbol.toXML(out, prefix + INDENTATION + INDENTATION); + printSymbol(symbol, out, "symbol", prefix + INDENTATION + INDENTATION); } out << prefix << INDENTATION << "</rightSide>\n"; out << prefix << "</rule>\n"; } -void GrammarPrinter::printStartSymbol(const Grammar& grammar, ostream& out, string prefix) { - out << prefix << "<startSymbol>"; - out << grammar.getStartSymbol().getSymbol(); - out <<"</startSymbol>\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 index c3ad03b67b..634a8723f6 100644 --- a/alib/src/grammar/GrammarPrinter.h +++ b/alib/src/grammar/GrammarPrinter.h @@ -23,7 +23,7 @@ protected: 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 printStartSymbol(const Grammar& grammar, ostream& out, string prefix); + static void printSymbol(const Symbol& symbol, ostream& out, string tagName, string prefix); public: static void toXML(const Grammar& grammar, ostream& out); }; diff --git a/examples/FSM.xml b/examples/FSM.xml index f68e0936a5..93b78bb20f 100644 --- a/examples/FSM.xml +++ b/examples/FSM.xml @@ -11,44 +11,44 @@ </inputAlphabet> <transitions> <transition> - <current>four</current> + <from>four</from> <input>1</input> - <next>four</next> + <to>four</to> </transition> <transition> - <current>four</current> + <from>four</from> <input>0</input> - <next>one</next> + <to>one</to> </transition> <transition> - <current>one</current> + <from>one</from> <input>0</input> - <next>two</next> + <to>two</to> </transition> <transition> - <current>one</current> + <from>one</from> <input>1</input> - <next>one</next> + <to>one</to> </transition> <transition> - <current>three</current> + <from>three</from> <input>0</input> - <next>four</next> + <to>four</to> </transition> <transition> - <current>three</current> + <from>three</from> <input>1</input> - <next>three</next> + <to>three</to> </transition> <transition> - <current>two</current> + <from>two</from> <input>0</input> - <next>three</next> + <to>three</to> </transition> <transition> - <current>two</current> + <from>two</from> <input>1</input> - <next>two</next> + <to>two</to> </transition> </transitions> <initialStates> diff --git a/examples/FSM2.xml b/examples/FSM2.xml index a93d7141cc..f30bc320b4 100644 --- a/examples/FSM2.xml +++ b/examples/FSM2.xml @@ -13,50 +13,50 @@ </inputAlphabet> <transitions> <transition> - <current>four</current> + <from>four</from> <input>1</input> - <next>four</next> + <to>four</to> </transition> <transition> - <current>four</current> + <from>four</from> <input>0</input> - <next>one</next> + <to>one</to> </transition> <transition> - <current>one</current> + <from>one</from> <input>0</input> - <next>two</next> + <to>two</to> </transition> <transition> - <current>one</current> + <from>one</from> <input>1</input> - <next>one</next> + <to>one</to> </transition> <transition> - <current>three</current> + <from>three</from> <input>0</input> - <next>four</next> + <to>four</to> </transition> <transition> - <current>three</current> + <from>three</from> <input>1</input> - <next>three</next> + <to>three</to> </transition> <transition> - <current>two</current> + <from>two</from> <input>0</input> - <next>three</next> + <to>three</to> </transition> <transition> - <current>two</current> + <from>two</from> <input>1</input> - <next>two</next> + <to>two</to> </transition> <transition> - <current>five</current> + <from>five</from> <input></input> - <next>six</next> + <to>six</to> </transition> </transitions> <initialStates> diff --git a/examples/NFSM1.xml b/examples/NFSM1.xml index c7651b1ffe..6ddca86262 100644 --- a/examples/NFSM1.xml +++ b/examples/NFSM1.xml @@ -11,29 +11,29 @@ </inputAlphabet> <transitions> <transition> - <current>S</current> + <from>S</from> <input>0</input> - <next>S</next> + <to>S</to> </transition> <transition> - <current>S</current> + <from>S</from> <input>0</input> - <next>A</next> + <to>A</to> </transition> <transition> - <current>S</current> + <from>S</from> <input>1</input> - <next>S</next> + <to>S</to> </transition> <transition> - <current>A</current> + <from>A</from> <input>1</input> - <next>B</next> + <to>B</to> </transition> <transition> - <current>B</current> + <from>B</from> <input>0</input> - <next>C</next> + <to>C</to> </transition> </transitions> <initialStates> diff --git a/examples/NFSM2.xml b/examples/NFSM2.xml index ce4592304a..059b106cd2 100644 --- a/examples/NFSM2.xml +++ b/examples/NFSM2.xml @@ -11,54 +11,54 @@ </inputAlphabet> <transitions> <transition> - <current>S</current> + <from>S</from> <input>0</input> - <next>A</next> + <to>A</to> </transition> <transition> - <current>S</current> + <from>S</from> <input>0</input> - <next>B</next> + <to>B</to> </transition> <transition> - <current>S</current> + <from>S</from> <input>1</input> - <next>C</next> + <to>C</to> </transition> <transition> - <current>A</current> + <from>A</from> <input>0</input> - <next>C</next> + <to>C</to> </transition> <transition> - <current>A</current> + <from>A</from> <input>1</input> - <next>A</next> + <to>A</to> </transition> <transition> - <current>B</current> + <from>B</from> <input>0</input> - <next>C</next> + <to>C</to> </transition> <transition> - <current>B</current> + <from>B</from> <input>1</input> - <next>B</next> + <to>B</to> </transition> <transition> - <current>B</current> + <from>B</from> <input>1</input> - <next>A</next> + <to>A</to> </transition> <transition> - <current>C</current> + <from>C</from> <input>0</input> - <next>S</next> + <to>S</to> </transition> <transition> - <current>C</current> + <from>C</from> <input>0</input> - <next>A</next> + <to>A</to> </transition> </transitions> <initialStates> diff --git a/examples/NFSM3.xml b/examples/NFSM3.xml index b23048561e..6612d62f6c 100644 --- a/examples/NFSM3.xml +++ b/examples/NFSM3.xml @@ -11,39 +11,39 @@ </inputAlphabet> <transitions> <transition> - <current>S</current> + <from>S</from> <input>0</input> - <next>S</next> + <to>S</to> </transition> <transition> - <current>S</current> + <from>S</from> <input>0</input> - <next>A</next> + <to>A</to> </transition> <transition> - <current>S</current> + <from>S</from> <input>1</input> - <next>B</next> + <to>B</to> </transition> <transition> - <current>A</current> + <from>A</from> <input>1</input> - <next>A</next> + <to>A</to> </transition> <transition> - <current>A</current> + <from>A</from> <input>1</input> - <next>C</next> + <to>C</to> </transition> <transition> - <current>B</current> + <from>B</from> <input>1</input> - <next>S</next> + <to>S</to> </transition> <transition> - <current>C</current> + <from>C</from> <input>0</input> - <next>B</next> + <to>B</to> </transition> </transitions> <initialStates> diff --git a/examples/NFSM4.xml b/examples/NFSM4.xml index 0e8f79d62e..81c5bafed1 100644 --- a/examples/NFSM4.xml +++ b/examples/NFSM4.xml @@ -13,64 +13,64 @@ </inputAlphabet> <transitions> <transition> - <current>A</current> + <from>A</from> <input>a</input> - <next>A</next> + <to>A</to> </transition> <transition> - <current>A</current> + <from>A</from> <input>a</input> - <next>B</next> + <to>B</to> </transition> <transition> - <current>A</current> + <from>A</from> <input>b</input> - <next>A</next> + <to>A</to> </transition> <transition> - <current>A</current> + <from>A</from> <input>b</input> - <next>E</next> + <to>E</to> </transition> <transition> - <current>B</current> + <from>B</from> <input>a</input> - <next>C</next> + <to>C</to> </transition> <transition> - <current>C</current> + <from>C</from> <input>a</input> - <next>D</next> + <to>D</to> </transition> <transition> - <current>C</current> + <from>C</from> <input>b</input> - <next>D</next> + <to>D</to> </transition> <transition> - <current>D</current> + <from>D</from> <input>a</input> - <next>D</next> + <to>D</to> </transition> <transition> - <current>D</current> + <from>D</from> <input>b</input> - <next>D</next> + <to>D</to> </transition> <transition> - <current>E</current> + <from>E</from> <input>a</input> - <next>F</next> + <to>F</to> </transition> <transition> - <current>E</current> + <from>E</from> <input>b</input> - <next>D</next> + <to>D</to> </transition> <transition> - <current>F</current> + <from>F</from> <input>b</input> - <next>D</next> + <to>D</to> </transition> </transitions> <initialStates> diff --git a/examples/PDA.xml b/examples/PDA.xml index c17a95c34c..bc44a65ead 100644 --- a/examples/PDA.xml +++ b/examples/PDA.xml @@ -12,9 +12,9 @@ </stackAlphabet> <transitions> <transition> - <current>a</current> + <from>a</from> <input>{</input> - <next>a</next> + <to>a</to> <pop> </pop> <push> @@ -22,9 +22,9 @@ </push> </transition> <transition> - <current>a</current> + <from>a</from> <input>}</input> - <next>b</next> + <to>b</to> <pop> <symbol>0</symbol> </pop> @@ -32,9 +32,9 @@ </push> </transition> <transition> - <current>b</current> + <from>b</from> <input>}</input> - <next>b</next> + <to>b</to> <pop> <symbol>0</symbol> </pop> diff --git a/examples/TM.xml b/examples/TM.xml index df9f5b271f..6ea26b6e40 100644 --- a/examples/TM.xml +++ b/examples/TM.xml @@ -16,30 +16,30 @@ </inputAlphabet> <transitions> <transition> - <current>a1</current> + <from>a1</from> <input>B</input> - <next>a2</next> + <to>a2</to> <output>0</output> <shift>right</shift> </transition> <transition> - <current>a2</current> + <from>a2</from> <input>B</input> - <next>a3</next> + <to>a3</to> <output>B</output> <shift>right</shift> </transition> <transition> - <current>a3</current> + <from>a3</from> <input>B</input> - <next>a4</next> + <to>a4</to> <output>1</output> <shift>right</shift> </transition> <transition> - <current>a4</current> + <from>a4</from> <input>B</input> - <next>a1</next> + <to>a1</to> <output>B</output> <shift>right</shift> </transition> diff --git a/examples/TM2.xml b/examples/TM2.xml index 351f3ba821..9e7607c969 100644 --- a/examples/TM2.xml +++ b/examples/TM2.xml @@ -19,72 +19,72 @@ </inputAlphabet> <transitions> <transition> - <current>q0</current> + <from>q0</from> <input>0</input> - <next>q1</next> + <to>q1</to> <output>X</output> <shift>right</shift> </transition> <transition> - <current>q1</current> + <from>q1</from> <input>0</input> - <next>q1</next> + <to>q1</to> <output>0</output> <shift>right</shift> </transition> <transition> - <current>q1</current> + <from>q1</from> <input>Y</input> - <next>q1</next> + <to>q1</to> <output>Y</output> <shift>right</shift> </transition> <transition> - <current>q1</current> + <from>q1</from> <input>1</input> - <next>q2</next> + <to>q2</to> <output>Y</output> <shift>left</shift> </transition> <transition> - <current>q2</current> + <from>q2</from> <input>Y</input> - <next>q2</next> + <to>q2</to> <output>Y</output> <shift>left</shift> </transition> <transition> - <current>q2</current> + <from>q2</from> <input>0</input> - <next>q2</next> + <to>q2</to> <output>0</output> <shift>left</shift> </transition> <transition> - <current>q2</current> + <from>q2</from> <input>X</input> - <next>q0</next> + <to>q0</to> <output>X</output> <shift>right</shift> </transition> <transition> - <current>q0</current> + <from>q0</from> <input>Y</input> - <next>q3</next> + <to>q3</to> <output>Y</output> <shift>right</shift> </transition> <transition> - <current>q3</current> + <from>q3</from> <input>Y</input> - <next>q3</next> + <to>q3</to> <output>Y</output> <shift>right</shift> </transition> <transition> - <current>q3</current> + <from>q3</from> <input>B</input> - <next>q4</next> + <to>q4</to> <output>B</output> <shift>right</shift> </transition> diff --git a/examples/TM3.xml b/examples/TM3.xml index 471c4fb2d4..6506702c96 100644 --- a/examples/TM3.xml +++ b/examples/TM3.xml @@ -14,23 +14,23 @@ </inputAlphabet> <transitions> <transition> - <current>a1</current> + <from>a1</from> <input>0</input> - <next>a2</next> + <to>a2</to> <output>0</output> <shift>right</shift> </transition> <transition> - <current>a2</current> + <from>a2</from> <input>1</input> - <next>a1</next> + <to>a1</to> <output>1</output> <shift>left</shift> </transition> <transition> - <current>a2</current> + <from>a2</from> <input>B</input> - <next>a2</next> + <to>a2</to> <output>1</output> <shift>none</shift> </transition> -- GitLab