Skip to content
Snippets Groups Projects
Commit d69831bc authored by Jan Trávníček's avatar Jan Trávníček
Browse files

add epsilon transitions to PDAs

parent 6a68b1d1
No related branches found
No related tags found
No related merge requests found
......@@ -70,13 +70,13 @@ bool NPDA::removeStackSymbol(const alphabet::Symbol& symbol) {
return stackAlphabet.erase(symbol);
}
 
bool NPDA::addTransition(const State& from, const alphabet::Symbol& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push) {
bool NPDA::addTransition(const State& from, const std::variant<string::Epsilon, 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 \"" + (std::string) from.getName() + "\" doesn't exist.");
}
 
if (inputAlphabet.find(input) == inputAlphabet.end()) {
throw AutomatonException("Input symbol \"" + (std::string) input + "\" 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()) {
......@@ -95,25 +95,43 @@ bool NPDA::addTransition(const State& from, const alphabet::Symbol& input, const
}
}
 
std::variant<string::Epsilon, alphabet::Symbol> inputVariant;
inputVariant.set<alphabet::Symbol>(input);
std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, std::vector<alphabet::Symbol> > key(from, inputVariant, pop);
std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, std::vector<alphabet::Symbol> > key(from, input, pop);
std::pair<automaton::State, std::vector<alphabet::Symbol> > value = std::make_pair(to, push);
return transitions[key].insert(value).second;
}
 
bool NPDA::removeTransition(const State& from, const alphabet::Symbol& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push) {
bool NPDA::addTransition(const State& from, const alphabet::Symbol& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push) {
std::variant<string::Epsilon, alphabet::Symbol> inputVariant;
inputVariant.set<alphabet::Symbol>(input);
std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, std::vector<alphabet::Symbol> > key(from, inputVariant, pop);
return addTransition(from, inputVariant, pop, to, push);
}
bool NPDA::addTransition(const State& from, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push) {
std::variant<string::Epsilon, alphabet::Symbol> inputVariant;
inputVariant.set<string::Epsilon>(string::Epsilon::EPSILON);
return addTransition(from, inputVariant, pop, to, push);
}
bool NPDA::removeTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push) {
std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, std::vector<alphabet::Symbol> > key(from, input, pop);
std::pair<automaton::State, std::vector<alphabet::Symbol> > value = std::make_pair(to, push);
return transitions[key].erase(value);
}
 
bool NPDA::removeTransition(const State& from, const alphabet::Symbol& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push) {
std::variant<string::Epsilon, alphabet::Symbol> inputVariant;
inputVariant.set<alphabet::Symbol>(input);
return removeTransition(from, inputVariant, pop, to, push);
}
bool NPDA::removeTransition(const State& from, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push) {
std::variant<string::Epsilon, alphabet::Symbol> inputVariant;
inputVariant.set<string::Epsilon>(string::Epsilon::EPSILON);
return removeTransition(from, inputVariant, pop, to, push);
}
const std::map<std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, std::vector<alphabet::Symbol> >, std::set<std::pair<State, std::vector<alphabet::Symbol> > > >& NPDA::getTransitions() const {
return transitions;
}
......
......@@ -53,15 +53,23 @@ public:
* @throws AutomatonException when some part of the transition is not present
* in the NPDA (state, input symbol, stack symbol) or when transition already exists
*/
bool addTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push);
bool addTransition(const State& from, const alphabet::Symbol& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push);
 
bool addTransition(const State& from, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push);
/**
* Removes the transition from the NPDA.
* @param transition transition to remove
* @throws AutomatonException when transition is not present in the NPDA
*/
bool removeTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push);
bool removeTransition(const State& from, const alphabet::Symbol& input, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push);
 
bool removeTransition(const State& from, const std::vector<alphabet::Symbol>& pop, const State& to, const std::vector<alphabet::Symbol>& push);
/**
* @return NPDA transitions
*/
......
......@@ -68,13 +68,13 @@ bool SinglePopNPDA::removeStackSymbol(const alphabet::Symbol& symbol) {
return stackAlphabet.erase(symbol);
}
 
bool SinglePopNPDA::addTransition(const State& from, const alphabet::Symbol& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push) {
bool SinglePopNPDA::addTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push) {
if (states.find(from) == states.end()) {
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 (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()) {
......@@ -91,25 +91,43 @@ bool SinglePopNPDA::addTransition(const State& from, const alphabet::Symbol& inp
}
}
 
std::variant<string::Epsilon, alphabet::Symbol> inputVariant;
inputVariant.set<alphabet::Symbol>(input);
std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, alphabet::Symbol> key(from, inputVariant, pop);
std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, alphabet::Symbol> key(from, input, pop);
std::pair<automaton::State, std::vector<alphabet::Symbol> > value = std::make_pair(to, push);
return transitions[key].insert(value).second;
}
 
bool SinglePopNPDA::removeTransition(const State& from, const alphabet::Symbol& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push) {
bool SinglePopNPDA::addTransition(const State& from, const alphabet::Symbol& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push) {
std::variant<string::Epsilon, alphabet::Symbol> inputVariant;
inputVariant.set<alphabet::Symbol>(input);
std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, alphabet::Symbol> key(from, inputVariant, pop);
return addTransition(from, inputVariant, pop, to, push);
}
bool SinglePopNPDA::addTransition(const State& from, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push) {
std::variant<string::Epsilon, alphabet::Symbol> inputVariant;
inputVariant.set<string::Epsilon>(string::Epsilon::EPSILON);
return addTransition(from, inputVariant, pop, to, push);
}
bool SinglePopNPDA::removeTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push) {
std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, alphabet::Symbol> key(from, input, pop);
std::pair<automaton::State, std::vector<alphabet::Symbol> > value = std::make_pair(to, push);
return transitions[key].erase(value);
}
 
bool SinglePopNPDA::removeTransition(const State& from, const alphabet::Symbol& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push) {
std::variant<string::Epsilon, alphabet::Symbol> inputVariant;
inputVariant.set<alphabet::Symbol>(input);
return removeTransition(from, inputVariant, pop, to, push);
}
bool SinglePopNPDA::removeTransition(const State& from, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push) {
std::variant<string::Epsilon, alphabet::Symbol> inputVariant;
inputVariant.set<string::Epsilon>(string::Epsilon::EPSILON);
return removeTransition(from, inputVariant, pop, to, push);
}
const std::map<std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol>, alphabet::Symbol>, std::set<std::pair<State, std::vector<alphabet::Symbol> > > >& SinglePopNPDA::getTransitions() const {
return transitions;
}
......
......@@ -53,14 +53,22 @@ public:
* @throws AutomatonException when some part of the transition is not present
* in the SinglePopNPDA (state, input symbol, stack symbol) or when transition already exists
*/
bool addTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push);
bool addTransition(const State& from, const alphabet::Symbol& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push);
bool addTransition(const State& from, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push);
 
/**
* Removes the transition from the SinglePopNPDA.
* @param transition transition to remove
* @throws AutomatonException when transition is not present in the SinglePopNPDA
*/
bool removeTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push);
bool removeTransition(const State& from, const alphabet::Symbol& input, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push);
bool removeTransition(const State& from, const alphabet::Symbol& pop, const State& to, const std::vector<alphabet::Symbol>& push);
 
/**
* @return SinglePopNPDA transitions
......
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