diff --git a/alib2data/src/grammar/Regular/LeftRG.h b/alib2data/src/grammar/Regular/LeftRG.h index 3342013e449ba72c0f671a5bbee4b14da1fe9e9a..1576b922b4dcdd8cc52a9ab1f49ffcf297eacf59 100644 --- a/alib2data/src/grammar/Regular/LeftRG.h +++ b/alib2data/src/grammar/Regular/LeftRG.h @@ -21,7 +21,7 @@ namespace grammar { * G = (N, T, P, S, E) * N = nonempty finite set of nonterminal symbols * T = finite set of terminal symbols - having this empty won't let grammar do much though - * P = set of production rules of the form A -> aB or A -> a (where A, B \in N and a \in T) + * P = set of production rules of the form A -> aB or A -> a, where A, B \in N and a \in T * S = initial nonterminal symbol * E = boolean signaling wheter grammar generates empty string or don't * @@ -38,43 +38,122 @@ class LeftRG : public std::element<LeftRG, GrammarBase>, public TerminalNontermi */ bool generatesEpsilon; public: + /** + * Creates a new instance of Left regular grammar with concrete initial symbol + */ LeftRG(const alphabet::Symbol& initialSymbol); + /** + * Creates a new instance of Left regular grammar with concrete nonterminal, terminal alphabet and initial symbol + */ LeftRG(const std::set<alphabet::Symbol>& nonTerminalSymbols, const std::set<alphabet::Symbol>& terminalSymbols, const alphabet::Symbol& initialSymbol); + /** + * @copydoc alib::base::clone() + */ virtual GrammarBase* clone() const; + /** + * @copydoc alib::base::plunder() + */ virtual GrammarBase* plunder() &&; + /** + * Add a new rule of a grammar in form of A -> aB or A -> a, where A, B \in N and a \in T + */ bool addRule(const alphabet::Symbol& leftHandSide, const std::variant<alphabet::Symbol, std::pair<alphabet::Symbol, alphabet::Symbol>>& rightHandSide); + + /** + * Add a new rule of a grammar in form of A -> a, where A \in N and a \in T + */ bool addRule(const alphabet::Symbol& leftHandSide, const alphabet::Symbol& rightHandSide); + + /** + * Add a new rule of a grammar in form of A -> aB, where A, B \in N and a \in T + */ bool addRule(const alphabet::Symbol& leftHandSide, const std::pair<alphabet::Symbol, alphabet::Symbol>& rightHandSide); + /** + * Get rules of the grammar + */ const std::map<alphabet::Symbol, std::set<std::variant<alphabet::Symbol, std::pair<alphabet::Symbol, alphabet::Symbol>>>> getRules() const; + /** + * Add a new rule of a grammar in form of A -> aB or A -> a, where A, B \in N and a \in T + */ bool removeRule(const alphabet::Symbol& leftHandSide, const std::variant<alphabet::Symbol, std::pair<alphabet::Symbol, alphabet::Symbol>>& rightHandSide); + + /** + * Add a new rule of a grammar in form of A -> a, where A \in N and a \in T + */ bool removeRule(const alphabet::Symbol& leftHandSide, const alphabet::Symbol& rightHandSide); + + /** + * Add a new rule of a grammar in form of A -> aB, where A, B \in N and a \in T + */ bool removeRule(const alphabet::Symbol& leftHandSide, const std::pair<alphabet::Symbol, alphabet::Symbol>& rightHandSide); - bool removeTerminalSymbol(const alphabet::Symbol& symbol); + /** + * @copydoc TerminalNonterminalAlphabetInitialSymbol::removeTerminalSymbol() + */ + virtual bool removeTerminalSymbol(const alphabet::Symbol& symbol); - bool removeNonterminalSymbol(const alphabet::Symbol& symbol); + /** + * @copydoc TerminalNonterminalAlphabetInitialSymbol::removeNonterminalSymbol() + */ + virtual bool removeNonterminalSymbol(const alphabet::Symbol& symbol); + /** + * Sets sing representing that grammar generates or doesn't generate empty word + */ void setGeneratesEpsilon(bool genEps); + + /** + * Gets sing representing that grammar generates or doesn't generate empty word + */ bool getGeneratesEpsilon() const; + /** + * double dispatch operator helper + */ virtual bool operator <(const GrammarBase& other) const; + + /** + * double dispatch operator helper + */ virtual bool operator ==(const GrammarBase& other) const; + + /** + * double dispatch operator helper + */ virtual bool operator >(const GrammarBase& other) const; + /** + * equals operator + * @param other the other instance + */ virtual bool operator==(const LeftRG& other) const; + /** + * less operator + * @param other the other instance + */ virtual bool operator<(const LeftRG& other) const; + /** + * print this instance as raw representation to ostream + * @param os ostream where to print + */ virtual void operator>>(std::ostream& os) const; + /** + * casts this instance to as compact as possible string representation + */ virtual operator std::string () const; + /** + * @copydoc alib::base_base::selfTypeId + */ virtual int selfTypeId() const { return typeId(*this); } diff --git a/alib2data/src/grammar/common/TerminalNonterminalAlphabetInitialSymbol.h b/alib2data/src/grammar/common/TerminalNonterminalAlphabetInitialSymbol.h index bd40e9c782031e53c9f9b6f3e4798fcbd5b40f7e..321f647627ea1f7b93d265f8cc4a233752a4ee01 100644 --- a/alib2data/src/grammar/common/TerminalNonterminalAlphabetInitialSymbol.h +++ b/alib2data/src/grammar/common/TerminalNonterminalAlphabetInitialSymbol.h @@ -16,17 +16,31 @@ namespace grammar { /** - * Abstract class representing grammar. Implements methods for manipulation - * with terminal and nonterminal symbols. Only method, which is not implemented - * is grammar specific isValidRule. + * Abstract class representing nonterminal and terminal alphabets and initial symbol. Implements methods for manipulation + * with terminal and nonterminal alphabets. Only pure virtual methods are remove terminal and nonterminal symbol. */ class TerminalNonterminalAlphabetInitialSymbol { protected: + /** + * terminal alphabet + */ std::set<alphabet::Symbol> terminalAlphabet; + + /** + * nonterminal alphabet + */ std::set<alphabet::Symbol> nonterminalAlphabet; + + /** + * initial symbol + */ alphabet::Symbol initialSymbol; public: + /** + * Creates a new instance, initial symbol is included in the nonterminal alphabet + * @param initialSymbol + */ TerminalNonterminalAlphabetInitialSymbol(const alphabet::Symbol& initialSymbol); /**