From 720af47b9c8359f8f76d468347a2999d2ccf5e9b Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Mon, 5 May 2014 13:41:20 +0200 Subject: [PATCH] method to set initial set of symbols at once --- alib2/src/automaton/PDA/PDA.cpp | 17 ++++++++++++++--- alib2/src/automaton/PDA/PDA.h | 10 +++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/alib2/src/automaton/PDA/PDA.cpp b/alib2/src/automaton/PDA/PDA.cpp index 87969dbb7b..80cc0edc1a 100644 --- a/alib2/src/automaton/PDA/PDA.cpp +++ b/alib2/src/automaton/PDA/PDA.cpp @@ -139,7 +139,6 @@ const std::map<std::tuple<State, std::variant<string::Epsilon, alphabet::Symbol> } void PDA::addInitialSymbol(const alphabet::Symbol& start) { - if (stackAlphabet.find(start) == stackAlphabet.end()) { throw AutomatonException("Stack symbol \"" + start.getSymbol() + "\" doesn't exist."); } @@ -151,10 +150,22 @@ void PDA::addInitialSymbol(const alphabet::Symbol& start) { } void PDA::removeInitialSymbol(const alphabet::Symbol& start) { + if(this->initialSymbols.size() <= 1) + throw AutomatonException("There must be at least one initial symbol"); int removed = this->initialSymbols.erase(start); - if (!removed) { + if (!removed) throw AutomatonException("Transition doesn't exist."); - } +} + +void PDA::setInitialSymbols(const std::set<alphabet::Symbol>& symbols) { + if(symbols.size() < 1) + throw AutomatonException("There must be at least one initial symbol"); + std::set<alphabet::Symbol> tmp(symbols); + tmp.erase(this->stackAlphabet.begin(), this->stackAlphabet.end()); + if(tmp.size() != 0) + throw AutomatonException("Initial symbols not in stack alphabet"); + + this->initialSymbols = symbols; } const std::set<alphabet::Symbol>& PDA::getInitialSymbols() const { diff --git a/alib2/src/automaton/PDA/PDA.h b/alib2/src/automaton/PDA/PDA.h index 74b2998746..e767112b4c 100644 --- a/alib2/src/automaton/PDA/PDA.h +++ b/alib2/src/automaton/PDA/PDA.h @@ -97,9 +97,17 @@ public: * Adds initial symbol. Initial symbols are symbols that are pushed * to the stack when PDA is created. * @param start new initial symbol - * @throws AutomatonException when symbol is not present in the stack alphabet or it is not present in the set + * @throws AutomatonException when symbol is not present in the set of initial symbols */ void removeInitialSymbol(const alphabet::Symbol& start); + + /** + * Sets initial symbols. Initial symbols are symbols that are pushed + * to the stack when PDA is created. + * @param symbols new initial symbols + * @throws AutomatonException when any of symbols is not present in the stack alphabet + */ + void setInitialSymbols(const std::set<alphabet::Symbol>& symbols); /** * @return list of start symbols -- GitLab