Skip to content
Snippets Groups Projects
Commit feda8a50 authored by Martin Žák's avatar Martin Žák
Browse files

Implements UnknownAutomaton.toXML method, adds const to some UnknonwAutomaton methods

parent a9507d75
No related branches found
No related tags found
No related merge requests found
......@@ -47,7 +47,7 @@ int main(int argc, char** argv) {
UnknownAutomaton unknownAutomaton = AutomatonParser::parse(tokens);
Automaton* automaton = AutomatonFactory::buildAutomaton(
&unknownAutomaton);
automaton->toXML(cout);
unknownAutomaton.toXML(cout);
} else if (tokens.front().getData() == "grammar") {
UnknownGrammar grammar = GrammarParser::parse(tokens);
grammar.toXML(cout);
......
......@@ -27,6 +27,37 @@ void AutomatonPrinter::printAlphabet(const set<Symbol>& alphabet, ostream& out,
out << prefix << "</" << tagName << ">\n";
}
 
void AutomatonPrinter::printUnknownTransitions(const set<UnknownTransition>& transitions, ostream& out, string prefix) {
string contentPrefix = prefix + INDENTATION + INDENTATION;
out << prefix << "<transitions>\n";
for (auto transition : transitions) {
out << prefix << INDENTATION << "<transition>\n";
printState(transition.getFrom(), out, "from", contentPrefix);
printSymbol(transition.getInput(), out, "input", contentPrefix);
printState(transition.getTo(), out, "to", contentPrefix);
if (transition.getPop().size() > 0) {
printSymbolList(transition.getPop(), out, "pop", contentPrefix);
}
if (transition.getPush().size() > 0) {
printSymbolList(transition.getPush(), out, "push", contentPrefix);
}
if (transition.getOutput().getSymbol() != "") {
printSymbol(transition.getOutput(), out, "output", contentPrefix);
}
if (transition.getShift() != Shift::NOT_SET) {
printShift(transition.getShift(), out, "shift", contentPrefix);
}
out << prefix << INDENTATION << "</transition>\n";
}
out << prefix << "</transitions>\n";
}
void AutomatonPrinter::printTransitionsFSM(const set<TransitionFSM>& transitions, ostream& out, string prefix) {
string contentPrefix = prefix + INDENTATION + INDENTATION;
 
......@@ -99,6 +130,45 @@ void AutomatonPrinter::printShift(const Shift& shift, ostream& out, string tagNa
out << "</" << tagName << ">\n";
}
 
void AutomatonPrinter::toXML(const UnknownAutomaton& automaton, ostream& out) {
out << "<automaton>\n";
if (automaton.getStates().size() > 0) {
printStates(automaton.getStates(), out, "states", INDENTATION);
}
if (automaton.getInputAlphabet().size() > 0) {
printAlphabet(automaton.getInputAlphabet(), out, "inputAlphabet", INDENTATION);
}
if (automaton.getTapeAlphabet().size() > 0) {
printAlphabet(automaton.getTapeAlphabet(), out, "tapeAlphabet", INDENTATION);
}
if (automaton.getStackAlphabet().size() > 0) {
printAlphabet(automaton.getStackAlphabet(), out, "stackAlphabet", INDENTATION);
}
if (automaton.getTransitions().size() > 0) {
printUnknownTransitions(automaton.getTransitions(), out, INDENTATION);
}
if (automaton.getBlankSymbol().getSymbol() != "") {
printSymbol(automaton.getBlankSymbol(), out, "blankSymbol", INDENTATION);
}
if (automaton.getStartSymbols().size() > 0) {
printSymbolList(automaton.getStartSymbols(), out, "startSymbols", INDENTATION);
}
if (automaton.getInitialStates().size() > 0) {
printStates(automaton.getInitialStates(), out, "initialStates", INDENTATION);
}
if (automaton.getFinalStates().size() > 0) {
printStates(automaton.getFinalStates(), out, "finalStates", INDENTATION);
}
out << "</automaton>\n";
}
void AutomatonPrinter::toXML(const FSM& automaton, ostream& out) {
out << "<automaton>\n";
printStates(automaton.getStates(), out, "states", INDENTATION);
......
......@@ -9,6 +9,7 @@
#define AUTOMATONPRINTER_H_
 
#include <ostream>
#include "UnknownAutomaton.h"
#include "FSM/FSM.h"
#include "PDA/PDA.h"
#include "TM/TM.h"
......@@ -26,16 +27,19 @@ protected:
static void printStates(const set<State>& states, ostream& out, string tagName, string prefix);
static void printAlphabet(const set<Symbol>& alphabet, ostream& out, string tagName, string prefix);
 
static void printUnknownTransitions(const set<UnknownTransition>& transitions, ostream& out, string prefix);
static void printTransitionsFSM(const set<TransitionFSM>& transitions, ostream& out, string prefix);
static void printTransitionsPDA(const set<TransitionPDA>& transitions, ostream& out, string prefix);
static void printTransitionsTM(const set<TransitionTM>& transitions, ostream& out, string prefix);
 
static void printState(const State& state, ostream& out, string tagName, string prefix);
static void printSymbol(const Symbol& symbol, ostream& out, string tagName, string prefix);
static void printSymbolList(const list<Symbol>& symbols, ostream& out, string tagName, string prefix);
static void printShift(const Shift& shift, ostream& out, string tagName, string prefix);
 
public:
static void toXML(const UnknownAutomaton& automaton, ostream& out);
static void toXML(const FSM& automaton, ostream& out);
static void toXML(const PDA& automaton, ostream& out);
static void toXML(const TM& automaton, ostream& out);
......
......@@ -7,6 +7,7 @@
 
#include "UnknownAutomaton.h"
 
#include "AutomatonPrinter.h"
#include "exception/AutomatonException.h"
 
namespace automaton {
......@@ -39,12 +40,12 @@ void UnknownAutomaton::removeFinalState(const State& state) {
throw AutomatonException("Final state \"" + state.getName() + "\" doesn't exist.");
}
 
const set<Symbol>& UnknownAutomaton::getStackAlphabet() {
const set<Symbol>& UnknownAutomaton::getStackAlphabet() const {
return stackAlphabet;
}
 
void UnknownAutomaton::addStackSymbol(const Symbol& symbol) {
if(!stackAlphabet.insert(symbol).second) {
if (!stackAlphabet.insert(symbol).second) {
throw AutomatonException("Stack symbol \"" + symbol.getSymbol() + "\" already exists.");
}
}
......@@ -54,7 +55,7 @@ void UnknownAutomaton::removeStackSymbol(const Symbol& symbol) {
throw AutomatonException("Stack symbol \"" + symbol.getSymbol() + "\" doesn't exist.");
}
 
const list<Symbol>& UnknownAutomaton::getStartSymbols() {
const list<Symbol>& UnknownAutomaton::getStartSymbols() const {
return startSymbols;
}
 
......@@ -62,12 +63,12 @@ void UnknownAutomaton::setStartSymbols(const list<Symbol>& newSymbols) {
this->startSymbols = newSymbols;
}
 
const set<Symbol>& UnknownAutomaton::getTapeAlphabet() {
const set<Symbol>& UnknownAutomaton::getTapeAlphabet() const {
return tapeAlphabet;
}
 
void UnknownAutomaton::addTapeSymbol(const Symbol& symbol) {
if(!tapeAlphabet.insert(symbol).second) {
if (!tapeAlphabet.insert(symbol).second) {
throw AutomatonException("Tape symbol \"" + symbol.getSymbol() + "\" already exists.");
}
}
......@@ -79,7 +80,7 @@ void UnknownAutomaton::removeTapeSymbol(const Symbol& symbol) {
}
}
 
const Symbol& UnknownAutomaton::getBlankSymbol() {
const Symbol& UnknownAutomaton::getBlankSymbol() const {
return blankSymbol;
}
 
......@@ -92,20 +93,19 @@ const set<UnknownTransition>& UnknownAutomaton::getTransitions() const {
}
 
void UnknownAutomaton::addTransition(const UnknownTransition& transition) {
if(!transitions.insert(transition).second) {
if (!transitions.insert(transition).second) {
throw AutomatonException("Transition already exists.");
}
}
 
void UnknownAutomaton::removeTransition(const UnknownTransition& transition) {
if(!transitions.erase(transition)) {
if (!transitions.erase(transition)) {
throw AutomatonException("Transition doesn't exist.");
}
}
 
void UnknownAutomaton::toXML(ostream& out) const {
//TODO Martin Zak: implement this
throw AutomatonException("Not yet implemented");
AutomatonPrinter::toXML(*this, out);
}
 
} /* namespace automaton */
......@@ -45,18 +45,18 @@ public:
void removeInitialState(const State& state);
void removeFinalState(const State& state);
 
const set<Symbol>& getStackAlphabet();
const set<Symbol>& getStackAlphabet() const;
void addStackSymbol(const Symbol& symbol);
void removeStackSymbol(const Symbol& symbol);
 
const list<Symbol>& getStartSymbols();
const list<Symbol>& getStartSymbols() const;
void setStartSymbols(const list<Symbol>& newSymbols);
 
const set<Symbol>& getTapeAlphabet();
const set<Symbol>& getTapeAlphabet() const;
void addTapeSymbol(const Symbol& symbol);
void removeTapeSymbol(const Symbol& symbol);
 
const Symbol& getBlankSymbol();
const Symbol& getBlankSymbol() const;
void setBlankSymbol(const Symbol& symbol);
 
const set<UnknownTransition>& getTransitions() const;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment