From 1442ea47750e25136321d75ffb9c9b4455eccbc9 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Tue, 15 Jul 2014 09:15:02 +0200 Subject: [PATCH] move createUnique* to better places --- alib2data/src/alphabet/Symbol.cpp | 22 +++++++++++++++++++ alib2data/src/alphabet/Symbol.h | 10 +++++++++ alib2data/src/automaton/Automaton.cpp | 16 ++++++++++++++ alib2data/src/automaton/Automaton.h | 12 ++++++++++ alib2data/src/automaton/common/States.cpp | 17 +------------- alib2data/src/automaton/common/States.h | 11 ---------- alib2data/src/grammar/ContextFree/CFG.h | 1 + alib2data/src/grammar/ContextFree/CNF.h | 1 + .../src/grammar/ContextFree/EpsilonFreeCFG.h | 1 + alib2data/src/grammar/ContextFree/GNF.h | 1 + alib2data/src/grammar/ContextFree/LG.h | 1 + alib2data/src/grammar/ContextSensitive/CSG.h | 1 + .../ContextSensitive/NonContractingGrammar.h | 1 + alib2data/src/grammar/Regular/LeftLG.h | 1 + alib2data/src/grammar/Regular/LeftRG.h | 1 + alib2data/src/grammar/Regular/RightLG.h | 1 + alib2data/src/grammar/Regular/RightRG.h | 1 + .../ContextPreservingUnrestrictedGrammar.h | 1 + .../Unrestricted/UnrestrictedGrammar.h | 1 + ...rminalNonterminalAlphabetInitialSymbol.cpp | 22 ++----------------- ...TerminalNonterminalAlphabetInitialSymbol.h | 10 --------- 21 files changed, 76 insertions(+), 57 deletions(-) diff --git a/alib2data/src/alphabet/Symbol.cpp b/alib2data/src/alphabet/Symbol.cpp index 42eb5f2b6d..31ec27637d 100644 --- a/alib2data/src/alphabet/Symbol.cpp +++ b/alib2data/src/alphabet/Symbol.cpp @@ -6,6 +6,10 @@ */ #include "Symbol.h" +#include "../label/NextLabel.h" +#include "LabeledSymbol.h" +#include <climits> +#include "../exception/AlibException.h" namespace alphabet { @@ -82,5 +86,23 @@ Symbol::operator std::string() const { return (std::string) *symbol; } +alphabet::Symbol Symbol::createUniqueSymbol(const alphabet::Symbol& base, const std::set<alphabet::Symbol>& terminalAlphabet, const std::set<alphabet::Symbol>& nonterminalAlphabet) { + label::NextLabel nextLabelCreator; + + const alphabet::LabeledSymbol* baseSymbol = dynamic_cast<const alphabet::LabeledSymbol*>(&(base.getSymbol())); + if(baseSymbol == NULL) + throw exception::AlibException("Could not create unique symbol with nonlabeled base symbol " + (std::string) base.getSymbol() + "." ); + + int i = 0; + do { + label::Label nextLabel = nextLabelCreator.nextLabel(baseSymbol->getLabel()); + alphabet::Symbol attempt = alphabet::Symbol(alphabet::LabeledSymbol(nextLabel)); + if(terminalAlphabet.count(attempt) == 0 && nonterminalAlphabet.count(attempt) == 0) + return attempt; + } while(++i < INT_MAX); + + throw exception::AlibException("Could not create unique symbol with base symbol " + (std::string) base.getSymbol() + "." ); +} + } /* namespace alphabet */ diff --git a/alib2data/src/alphabet/Symbol.h b/alib2data/src/alphabet/Symbol.h index c69839ab0e..3b9499efb5 100644 --- a/alib2data/src/alphabet/Symbol.h +++ b/alib2data/src/alphabet/Symbol.h @@ -10,6 +10,7 @@ #include "../std/visitor.hpp" #include "SymbolBase.h" +#include <set> namespace alphabet { @@ -43,6 +44,15 @@ public: friend std::ostream& operator<<(std::ostream& os, const Symbol& symbol); operator std::string () const; + + /** + * Creates and adds unique state to grammar. If given state name is + * already used, appends apostrophe or integer suffix + * @param name name of the state + * @throws AutomatonException if symbol could not be created + * @return created symbol + */ + static alphabet::Symbol createUniqueSymbol(const alphabet::Symbol& base, const std::set<alphabet::Symbol>& Terminals, const std::set<alphabet::Symbol>& nonterminals); }; } /* namespace alphabet */ diff --git a/alib2data/src/automaton/Automaton.cpp b/alib2data/src/automaton/Automaton.cpp index 4e762c39a7..6625e57fe5 100644 --- a/alib2data/src/automaton/Automaton.cpp +++ b/alib2data/src/automaton/Automaton.cpp @@ -6,6 +6,9 @@ */ #include "Automaton.h" +#include "../label/NextLabel.h" +#include <climits> +#include "AutomatonException.h" namespace automaton { @@ -74,5 +77,18 @@ std::ostream& operator<<(std::ostream& os, const Automaton& automaton) { return os; } +State Automaton::createUniqueState(const State& base, const std::set<State>& other) { + label::NextLabel nextLabelCreator; + + int i = 0; + do { + label::Label nextLabel = nextLabelCreator.nextLabel(base.getName()); + if(other.count(State(nextLabel)) == 0) + return State(nextLabel); + } while(++i < INT_MAX); + + throw AutomatonException("Could not create unique state with base name " + (std::string) base.getName() + "." ); +} + } /* namespace automaton */ diff --git a/alib2data/src/automaton/Automaton.h b/alib2data/src/automaton/Automaton.h index 4c9e0b8d01..cbd7c62f5e 100644 --- a/alib2data/src/automaton/Automaton.h +++ b/alib2data/src/automaton/Automaton.h @@ -11,6 +11,9 @@ #include "../std/visitor.hpp" #include "AutomatonBase.h" +#include "common/State.h" +#include <set> + namespace automaton { /** @@ -39,6 +42,15 @@ public: bool operator==(const Automaton& other) const; friend std::ostream& operator<<(std::ostream& os, const Automaton& automaton); + + /** + * Creates and adds unique state to automaton. If given state name is + * already used, appends apostrophe or integer suffix + * @param name name of the state + * @throws AutomatonException if state could not be created + * @return created state + */ + static State createUniqueState(const State& base, const std::set<State>& other); }; } /* namespace automaton */ diff --git a/alib2data/src/automaton/common/States.cpp b/alib2data/src/automaton/common/States.cpp index daa263c186..7b2a258458 100644 --- a/alib2data/src/automaton/common/States.cpp +++ b/alib2data/src/automaton/common/States.cpp @@ -6,11 +6,9 @@ */ #include "States.h" - #include <set> -#include <algorithm> #include "../AutomatonException.h" -#include "../../label/NextLabel.h" +#include <algorithm> namespace automaton { @@ -74,19 +72,6 @@ const std::set<State>& States::getFinalStates() const { return finalStates; } -State States::createUniqueState(const State& base, const std::set<State>& other) { - label::NextLabel nextLabelCreator; - - int i = 0; - do { - label::Label nextLabel = nextLabelCreator.nextLabel(base.getName()); - if(other.count(State(nextLabel)) == 0) - return State(nextLabel); - } while(++i < INT_MAX); - - throw AutomatonException("Could not create unique state with base name " + (std::string) base.getName() + "." ); -} - } /* namespace automaton */ diff --git a/alib2data/src/automaton/common/States.h b/alib2data/src/automaton/common/States.h index 4279d8e80f..57c1572dad 100644 --- a/alib2data/src/automaton/common/States.h +++ b/alib2data/src/automaton/common/States.h @@ -8,12 +8,10 @@ #ifndef STATES_H_ #define STATES_H_ -#include <climits> #include <ostream> #include <set> #include <string> #include "State.h" -#include "../../alphabet/Symbol.h" namespace automaton { @@ -82,15 +80,6 @@ public: * @return final states */ const std::set<State>& getFinalStates() const; - - /** - * Creates and adds unique state to automaton. If given state name is - * already used, appends apostrophe or integer suffix - * @param name name of the state - * @throws AutomatonException if state could not be created - * @return created state - */ - static State createUniqueState(const State& base, const std::set<State>& other); }; } /* namespace automaton */ diff --git a/alib2data/src/grammar/ContextFree/CFG.h b/alib2data/src/grammar/ContextFree/CFG.h index 0ddc621ed1..a1150d9cf4 100644 --- a/alib2data/src/grammar/ContextFree/CFG.h +++ b/alib2data/src/grammar/ContextFree/CFG.h @@ -10,6 +10,7 @@ #include "../GrammarBase.h" #include <map> +#include <vector> #include "../common/TerminalNonterminalAlphabetInitialSymbol.h" namespace grammar { diff --git a/alib2data/src/grammar/ContextFree/CNF.h b/alib2data/src/grammar/ContextFree/CNF.h index 727d89c23b..333d9246d9 100644 --- a/alib2data/src/grammar/ContextFree/CNF.h +++ b/alib2data/src/grammar/ContextFree/CNF.h @@ -10,6 +10,7 @@ #include "../GrammarBase.h" #include <map> +#include <vector> #include "../common/TerminalNonterminalAlphabetInitialSymbol.h" namespace grammar { diff --git a/alib2data/src/grammar/ContextFree/EpsilonFreeCFG.h b/alib2data/src/grammar/ContextFree/EpsilonFreeCFG.h index 1af9c54463..44d31bd50d 100644 --- a/alib2data/src/grammar/ContextFree/EpsilonFreeCFG.h +++ b/alib2data/src/grammar/ContextFree/EpsilonFreeCFG.h @@ -10,6 +10,7 @@ #include "../GrammarBase.h" #include <map> +#include <vector> #include "../common/TerminalNonterminalAlphabetInitialSymbol.h" namespace grammar { diff --git a/alib2data/src/grammar/ContextFree/GNF.h b/alib2data/src/grammar/ContextFree/GNF.h index 0ee796328c..c711939be7 100644 --- a/alib2data/src/grammar/ContextFree/GNF.h +++ b/alib2data/src/grammar/ContextFree/GNF.h @@ -10,6 +10,7 @@ #include "../GrammarBase.h" #include <map> +#include <vector> #include "../common/TerminalNonterminalAlphabetInitialSymbol.h" namespace grammar { diff --git a/alib2data/src/grammar/ContextFree/LG.h b/alib2data/src/grammar/ContextFree/LG.h index f957ec1a11..6c1594f8bd 100644 --- a/alib2data/src/grammar/ContextFree/LG.h +++ b/alib2data/src/grammar/ContextFree/LG.h @@ -10,6 +10,7 @@ #include "../GrammarBase.h" #include <map> +#include <vector> #include "../common/TerminalNonterminalAlphabetInitialSymbol.h" namespace grammar { diff --git a/alib2data/src/grammar/ContextSensitive/CSG.h b/alib2data/src/grammar/ContextSensitive/CSG.h index 88f3f3ed98..eb7dc59443 100644 --- a/alib2data/src/grammar/ContextSensitive/CSG.h +++ b/alib2data/src/grammar/ContextSensitive/CSG.h @@ -10,6 +10,7 @@ #include "../GrammarBase.h" #include <map> +#include <vector> #include "../common/TerminalNonterminalAlphabetInitialSymbol.h" namespace grammar { diff --git a/alib2data/src/grammar/ContextSensitive/NonContractingGrammar.h b/alib2data/src/grammar/ContextSensitive/NonContractingGrammar.h index 9ecb4ef3b7..9a6866faf9 100644 --- a/alib2data/src/grammar/ContextSensitive/NonContractingGrammar.h +++ b/alib2data/src/grammar/ContextSensitive/NonContractingGrammar.h @@ -10,6 +10,7 @@ #include "../GrammarBase.h" #include <map> +#include <vector> #include "../common/TerminalNonterminalAlphabetInitialSymbol.h" namespace grammar { diff --git a/alib2data/src/grammar/Regular/LeftLG.h b/alib2data/src/grammar/Regular/LeftLG.h index 807756dd64..8127000f7f 100644 --- a/alib2data/src/grammar/Regular/LeftLG.h +++ b/alib2data/src/grammar/Regular/LeftLG.h @@ -10,6 +10,7 @@ #include "../GrammarBase.h" #include <map> +#include <vector> #include "../common/TerminalNonterminalAlphabetInitialSymbol.h" namespace grammar { diff --git a/alib2data/src/grammar/Regular/LeftRG.h b/alib2data/src/grammar/Regular/LeftRG.h index b5f180483c..2160488a03 100644 --- a/alib2data/src/grammar/Regular/LeftRG.h +++ b/alib2data/src/grammar/Regular/LeftRG.h @@ -10,6 +10,7 @@ #include "../GrammarBase.h" #include <map> +#include <vector> #include "../common/TerminalNonterminalAlphabetInitialSymbol.h" namespace grammar { diff --git a/alib2data/src/grammar/Regular/RightLG.h b/alib2data/src/grammar/Regular/RightLG.h index e4ed7f764b..9cba0827f4 100644 --- a/alib2data/src/grammar/Regular/RightLG.h +++ b/alib2data/src/grammar/Regular/RightLG.h @@ -10,6 +10,7 @@ #include "../GrammarBase.h" #include <map> +#include <vector> #include "../common/TerminalNonterminalAlphabetInitialSymbol.h" namespace grammar { diff --git a/alib2data/src/grammar/Regular/RightRG.h b/alib2data/src/grammar/Regular/RightRG.h index c4f6bb5422..3df8d61d95 100644 --- a/alib2data/src/grammar/Regular/RightRG.h +++ b/alib2data/src/grammar/Regular/RightRG.h @@ -10,6 +10,7 @@ #include "../GrammarBase.h" #include <map> +#include <vector> #include "../common/TerminalNonterminalAlphabetInitialSymbol.h" namespace grammar { diff --git a/alib2data/src/grammar/Unrestricted/ContextPreservingUnrestrictedGrammar.h b/alib2data/src/grammar/Unrestricted/ContextPreservingUnrestrictedGrammar.h index c95593ad65..079e023c92 100644 --- a/alib2data/src/grammar/Unrestricted/ContextPreservingUnrestrictedGrammar.h +++ b/alib2data/src/grammar/Unrestricted/ContextPreservingUnrestrictedGrammar.h @@ -10,6 +10,7 @@ #include "../GrammarBase.h" #include <map> +#include <vector> #include "../common/TerminalNonterminalAlphabetInitialSymbol.h" namespace grammar { diff --git a/alib2data/src/grammar/Unrestricted/UnrestrictedGrammar.h b/alib2data/src/grammar/Unrestricted/UnrestrictedGrammar.h index 526f21e9c9..d161652a27 100644 --- a/alib2data/src/grammar/Unrestricted/UnrestrictedGrammar.h +++ b/alib2data/src/grammar/Unrestricted/UnrestrictedGrammar.h @@ -10,6 +10,7 @@ #include "../GrammarBase.h" #include <map> +#include <vector> #include "../common/TerminalNonterminalAlphabetInitialSymbol.h" namespace grammar { diff --git a/alib2data/src/grammar/common/TerminalNonterminalAlphabetInitialSymbol.cpp b/alib2data/src/grammar/common/TerminalNonterminalAlphabetInitialSymbol.cpp index e538109b01..5bb1f15fda 100644 --- a/alib2data/src/grammar/common/TerminalNonterminalAlphabetInitialSymbol.cpp +++ b/alib2data/src/grammar/common/TerminalNonterminalAlphabetInitialSymbol.cpp @@ -9,10 +9,10 @@ #include "../GrammarException.h" #include "../../alphabet/LabeledSymbol.h" #include "../../label/Label.h" -#include "../../label/NextLabel.h" #include "../../alphabet/LabeledSymbol.h" #include "../../alphabet/Symbol.h" -#include <limits.h> +#include <climits> +#include <algorithm> namespace grammar { @@ -88,22 +88,4 @@ void TerminalNonterminalAlphabetInitialSymbol::setInitialSymbol(const alphabet:: initialSymbol = symbol; } -alphabet::Symbol TerminalNonterminalAlphabetInitialSymbol::createUniqueSymbol(const alphabet::Symbol& base, const std::set<alphabet::Symbol>& terminalAlphabet, const std::set<alphabet::Symbol>& nonterminalAlphabet) { - label::NextLabel nextLabelCreator; - - const alphabet::LabeledSymbol* baseSymbol = dynamic_cast<const alphabet::LabeledSymbol*>(&(base.getSymbol())); - if(baseSymbol == NULL) - throw GrammarException("Could not create unique symbol with nonlabeled base symbol " + (std::string) base.getSymbol() + "." ); - - int i = 0; - do { - label::Label nextLabel = nextLabelCreator.nextLabel(baseSymbol->getLabel()); - alphabet::Symbol attempt = alphabet::Symbol(alphabet::LabeledSymbol(nextLabel)); - if(terminalAlphabet.count(attempt) == 0 && nonterminalAlphabet.count(attempt) == 0) - return attempt; - } while(++i < INT_MAX); - - throw GrammarException("Could not create unique symbol with base symbol " + (std::string) base.getSymbol() + "." ); -} - } /* namespace grammar */ diff --git a/alib2data/src/grammar/common/TerminalNonterminalAlphabetInitialSymbol.h b/alib2data/src/grammar/common/TerminalNonterminalAlphabetInitialSymbol.h index 0f2372501e..bd40e9c782 100644 --- a/alib2data/src/grammar/common/TerminalNonterminalAlphabetInitialSymbol.h +++ b/alib2data/src/grammar/common/TerminalNonterminalAlphabetInitialSymbol.h @@ -10,7 +10,6 @@ #include <set> #include <list> -#include <algorithm> #include "../../alphabet/Symbol.h" @@ -90,15 +89,6 @@ public: */ void setInitialSymbol(const alphabet::Symbol& symbol); - /** - * Creates and adds unique state to grammar. If given state name is - * already used, appends apostrophe or integer suffix - * @param name name of the state - * @throws AutomatonException if symbol could not be created - * @return created symbol - */ - static alphabet::Symbol createUniqueSymbol(const alphabet::Symbol& base, const std::set<alphabet::Symbol>& Terminals, const std::set<alphabet::Symbol>& nonterminals); - }; } /* namespace grammar */ -- GitLab