Skip to content
Snippets Groups Projects
Commit 0ba58873 authored by Honza's avatar Honza
Browse files

feature: print state transition symbol to ostream

parent f8114e05
No related branches found
No related tags found
No related merge requests found
Showing
with 124 additions and 0 deletions
......@@ -29,4 +29,10 @@ bool Symbol::operator !=(const Symbol& other) const {
return symbol != other.symbol;
}
 
std::ostream& operator<<(std::ostream& out, const Symbol& symbol) {
out << "Symbol " << (symbol.symbol == "" ? "\\epsilon" : symbol.symbol);
return out;
}
} /* namespace language */
......@@ -34,6 +34,8 @@ public:
bool operator <(const Symbol& other) const;
bool operator ==(const Symbol& other) const;
bool operator !=(const Symbol& other) const;
friend std::ostream& operator<<(std::ostream&, const Symbol&);
};
}
#endif /* SYMBOL_H_ */
......@@ -29,4 +29,12 @@ bool TransitionFSM::operator != (const TransitionFSM& other) const {
return from != other.from || input != other.input || to != other.to;
}
 
std::ostream& TransitionFSM::operator>>(std::ostream& out) const {
out << "TransitionFSM from = " << this->from
<< " to = " << this->to
<< " input = " << this->input;
return out;
}
} /* namespace automaton */
......@@ -29,6 +29,8 @@ public:
bool operator <(const TransitionFSM& other) const;
bool operator ==(const TransitionFSM& other) const;
bool operator !=(const TransitionFSM& other) const;
std::ostream& operator>>(std::ostream& out) const;
};
 
} /* namespace automaton */
......
......@@ -120,4 +120,31 @@ bool TransitionPDA::operator !=(const TransitionPDA& other) const {
return !((*this) == other);
}
 
std::ostream& TransitionPDA::operator>>(std::ostream& out) const {
bool first;
out << "TransitionPDA from = " << this->from
<< " to = " << this->to
<< " input = " << this->input
<< " pop = [";
first = true;
for(list<Symbol>::const_iterator iter = this->pop.begin(); iter != this->pop.end(); iter++) {
if(!first) out << ", ";
first = false;
out << *iter;
}
out << "] push = [";
first = true;
for(list<Symbol>::const_iterator iter = this->push.begin(); iter != this->push.end(); iter++) {
if(!first) out << ", ";
first = false;
out << *iter;
}
out << "]";
return out;
}
} /* namespace automaton */
......@@ -32,6 +32,8 @@ public:
bool operator <(const TransitionPDA& other) const;
bool operator ==(const TransitionPDA& other) const;
bool operator !=(const TransitionPDA& other) const;
std::ostream& operator>>(std::ostream& out) const;
};
 
} /* namespace automaton */
......
/*
* Shift.cpp
*
* Created on: Dec 8, 2013
* Author: honza
*/
#include "Shift.h"
std::string SHIFT_NAMES[] = {"LEFT", "RIGHT", "NONE", "NOT_SET" };
......@@ -8,6 +8,8 @@
#ifndef SHIFT_H_
#define SHIFT_H_
 
#include <string>
/**
* Represent movement of the reading head in the Turing machine.
*/
......@@ -15,4 +17,6 @@ enum Shift {
LEFT, RIGHT, NONE, NOT_SET
};
 
extern std::string SHIFT_NAMES[4];
#endif /* SHIFT_H_ */
......@@ -28,4 +28,9 @@ bool State::operator != (const State& other) const{
return name != other.name;
}
 
std::ostream& operator<<(std::ostream& out, const State& state) {
out << "State " << state.name;
return out;
}
} /* namespace automaton */
......@@ -26,6 +26,8 @@ public:
bool operator < (const State& other) const;
bool operator == (const State& other) const;
bool operator != (const State& other) const;
friend std::ostream& operator<<(std::ostream&, const State&);
};
 
} /* namespace automaton */
......
......@@ -49,4 +49,14 @@ bool TransitionTM::operator !=(const TransitionTM& other) const {
return !((*this) == other);
}
 
std::ostream& TransitionTM::operator>>(std::ostream& out) const {
out << "TransitionTM from = " << this->from
<< " to = " << this->to
<< " input = " << this->input
<< " output = " << this->output
<< " shift = " << SHIFT_NAMES[this->shift];
return out;
}
} /* namespace automaton */
......@@ -56,6 +56,8 @@ public:
bool operator <(const TransitionTM& other) const;
bool operator ==(const TransitionTM& other) const;
bool operator !=(const TransitionTM& other) const;
std::ostream& operator>>(std::ostream& out) const;
};
 
} /* namespace automaton */
......
......@@ -42,4 +42,14 @@ bool Transition::containsState(const State& state) const {
return from == state || to == state;
}
 
std::ostream& operator<<(std::ostream& out, const Transition& transition) {
transition >> out;
return out;
}
std::ostream& Transition::operator>>(std::ostream& out) const {
out << "Transition from = " << this->from << " to = " << this->to << " input = " << this->input;
return out;
}
} /* namespace automaton */
......@@ -62,6 +62,10 @@ public:
* @return true when transition contains the state, false otherwise
*/
bool containsState(const State& state) const;
friend std::ostream& operator<<(std::ostream&, const Transition&);
virtual std::ostream& operator>>(std::ostream&) const;
};
 
} /* namespace automaton */
......
......@@ -138,4 +138,32 @@ bool UnknownTransition::operator !=(const UnknownTransition& other) const {
return !((*this) == other);
}
 
std::ostream& UnknownTransition::operator>>(std::ostream& out) const {
bool first;
out << "UnknownTransition from = " << this->from
<< " to = " << this->to
<< " input = " << this->input
<< " output = " << this->output
<< " pop = [";
first = true;
for(list<Symbol>::const_iterator iter = this->pop.begin(); iter != this->pop.end(); iter++) {
if(!first) out << ", ";
first = false;
out << *iter;
}
out << "] push = [";
first = true;
for(list<Symbol>::const_iterator iter = this->push.begin(); iter != this->push.end(); iter++) {
if(!first) out << ", ";
first = false;
out << *iter;
}
out << "] shift = " << SHIFT_NAMES[this->shift];
return out;
}
} /* namespace automaton */
......@@ -81,6 +81,8 @@ public:
bool operator <(const UnknownTransition& other) const;
bool operator ==(const UnknownTransition& other) const;
bool operator !=(const UnknownTransition& other) const;
virtual std::ostream& operator>>(std::ostream&) const;
};
 
} /* namespace automaton */
......
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