diff --git a/alib2/src/regexp/Alternation.cpp b/alib2/src/regexp/Alternation.cpp index aac9219f55ed47a13bc213dfcab3fb030db76925..24377bd12e240191935b055a39ed018a7e80a656 100644 --- a/alib2/src/regexp/Alternation.cpp +++ b/alib2/src/regexp/Alternation.cpp @@ -87,6 +87,11 @@ bool Alternation::operator<(const Alternation& other) const { return **thisIter < **otherIter; } +void Alternation::getAlphabet( std::set<alphabet::Symbol> & alphabet ) const { + for(const auto& child : this->elements) + child->getAlphabet(alphabet); +} + bool Alternation::operator==(const Alternation& other) const { if(this->elements.size() != other.elements.size()) return false; diff --git a/alib2/src/regexp/Alternation.h b/alib2/src/regexp/Alternation.h index 70bcc6d0c8de01fe83e5d5bf09fbdac8d10736e4..930dce0e97c083a5dc4a59a5ebc4e8ac5f1aa2a1 100644 --- a/alib2/src/regexp/Alternation.h +++ b/alib2/src/regexp/Alternation.h @@ -50,7 +50,12 @@ public: virtual bool operator<(const Concatenation&) const; virtual bool operator<(const Alternation&) const; virtual bool operator==(const Alternation&) const; - + + /** + * @copydoc RegExpElement::getAlphabet() const + */ + virtual void getAlphabet( std::set<alphabet::Symbol> & alphabet ) const; + /** * @copydoc RegExpElement::containsEmptyString() const */ diff --git a/alib2/src/regexp/Concatenation.cpp b/alib2/src/regexp/Concatenation.cpp index 087f14074e913757b1e5df8eb53da2c03c3532b8..b678958ac903898e5d8e5dabdab98066f8ed7cb5 100644 --- a/alib2/src/regexp/Concatenation.cpp +++ b/alib2/src/regexp/Concatenation.cpp @@ -95,6 +95,11 @@ bool Concatenation::operator==(const Concatenation& other) const { return true; } +void Concatenation::getAlphabet( std::set<alphabet::Symbol> & alphabet ) const { + for(const auto& child : this->elements) + child->getAlphabet(alphabet); +} + bool Concatenation::containsEmptyString() const { for(const auto& e : getElements()) if( ! e->containsEmptyString()) diff --git a/alib2/src/regexp/Concatenation.h b/alib2/src/regexp/Concatenation.h index b56e4f658ab2bc4ef591bc9ccc931387e9ad6138..6b8b10a04ab74ba30e8423f3bbac50a7d5a58f38 100644 --- a/alib2/src/regexp/Concatenation.h +++ b/alib2/src/regexp/Concatenation.h @@ -50,6 +50,11 @@ public: virtual bool operator<(const Concatenation&) const; virtual bool operator==(const Concatenation&) const; + /** + * @copydoc RegExpElement::getAlphabet() const + */ + virtual void getAlphabet( std::set<alphabet::Symbol> & alphabet ) const; + /** * @copydoc RegExpElement::containsEmptyString() const */ diff --git a/alib2/src/regexp/Iteration.cpp b/alib2/src/regexp/Iteration.cpp index 3495f4a6bc7ce31427106dad88f49e1bce2cd91e..888985fa098e84a3fd3232911854a5a01d26f23b 100644 --- a/alib2/src/regexp/Iteration.cpp +++ b/alib2/src/regexp/Iteration.cpp @@ -93,6 +93,10 @@ bool Iteration::operator==(const Iteration& other) const { return *(this->element) == *(other.element); } +void Iteration::getAlphabet( std::set<alphabet::Symbol> & alphabet ) const { + element->getAlphabet( alphabet ); +} + bool Iteration::containsEmptyString() const { return true; } diff --git a/alib2/src/regexp/Iteration.h b/alib2/src/regexp/Iteration.h index a21e61fc848a49a77836a8dbe9ae80018beabe95..168158461eb1f2ea93a45ea97b3cbd40b0f1d204 100644 --- a/alib2/src/regexp/Iteration.h +++ b/alib2/src/regexp/Iteration.h @@ -58,6 +58,11 @@ public: virtual bool operator<(const Iteration&) const; virtual bool operator==(const Iteration&) const; + /** + * @copydoc RegExpElement::getAlphabet() const + */ + virtual void getAlphabet( std::set<alphabet::Symbol> & alphabet ) const; + /** * @copydoc RegExpElement::containsEmptyString() const */ diff --git a/alib2/src/regexp/RegExp.cpp b/alib2/src/regexp/RegExp.cpp index 61d29570c6fbfb2ad1b77754a356d35f02aa0e68..cc899dc63b12821caff94910cc46f8d391b01e5d 100644 --- a/alib2/src/regexp/RegExp.cpp +++ b/alib2/src/regexp/RegExp.cpp @@ -69,6 +69,15 @@ void RegExp::setRegExp(RegExpElement* regExp) { this->regExp = regExp; } +set<alphabet::Symbol> RegExp::getAlphabet() const { + set<alphabet::Symbol> alphabet; + + if(regExp) + regExp->getAlphabet( alphabet ); + + return alphabet; +} + bool RegExp::isEmpty() const { return regExp == NULL || regExp->isEmpty(); } diff --git a/alib2/src/regexp/RegExp.h b/alib2/src/regexp/RegExp.h index e6fd443b50247113fa865aa4b7cae079a83a2e50..8e25d80d332707d67120e9d0c3df38a093709e15 100644 --- a/alib2/src/regexp/RegExp.h +++ b/alib2/src/regexp/RegExp.h @@ -55,7 +55,13 @@ public: * @param regExp root node to set */ void setRegExp(RegExpElement* regExp); - + + /** + * Gets alphabet symbols used in RegExp. + * @return set of alphabet symbols used in regexp. + */ + std::set<alphabet::Symbol> getAlphabet() const; + /** * @return true if regexp represents empty language */ diff --git a/alib2/src/regexp/RegExpElement.h b/alib2/src/regexp/RegExpElement.h index 33d15d6673efc56f515d3e0c4c0ba46c73c2e669..4809f67cf145ff30bcfbeaff018ce2c479ea8bf6 100644 --- a/alib2/src/regexp/RegExpElement.h +++ b/alib2/src/regexp/RegExpElement.h @@ -9,6 +9,8 @@ #define REGEXPELEMENT_H_ #include "../std/visitor.hpp" +#include "../alphabet/Symbol.h" +#include <set> namespace regexp { @@ -67,6 +69,13 @@ public: * @return true if this subtree describes empty language */ virtual bool isEmpty() const = 0; + + /** + * Traverses the regexp tree to get alphabet symbols used. + * + * @param alphabet All alphabet symbols encountered are added into this set + */ + virtual void getAlphabet( std::set<alphabet::Symbol> & alphabet ) const = 0; }; } /* namespace regexp */ diff --git a/alib2/src/regexp/RegExpEmpty.cpp b/alib2/src/regexp/RegExpEmpty.cpp index 553cfd650f50ed8d2b73f1c34247ab9d07b72285..ed76ea14b1af985e17b36fba4d006d39998e3bf0 100644 --- a/alib2/src/regexp/RegExpEmpty.cpp +++ b/alib2/src/regexp/RegExpEmpty.cpp @@ -53,6 +53,10 @@ bool RegExpEmpty::operator==(const RegExpEmpty&) const { return true; } +void RegExpEmpty::getAlphabet( std::set<alphabet::Symbol> & alphabet ) const { + +} + bool RegExpEmpty::containsEmptyString() const { return false; } diff --git a/alib2/src/regexp/RegExpEmpty.h b/alib2/src/regexp/RegExpEmpty.h index a9671e70a1a8f9da4231c10bc01fbbc78d38a07a..927842634fcee2fcac8b5b24d283c4a3a3e50c65 100644 --- a/alib2/src/regexp/RegExpEmpty.h +++ b/alib2/src/regexp/RegExpEmpty.h @@ -36,7 +36,12 @@ public: virtual bool operator<(const RegExpSymbol&) const; virtual bool operator<(const RegExpEpsilon&) const; virtual bool operator==(const RegExpEmpty&) const; - + + /** + * @copydoc RegExpElement::getAlphabet() const + */ + virtual void getAlphabet( std::set<alphabet::Symbol> & alphabet ) const; + /** * @copydoc RegExpElement::containsEmptyString() const */ diff --git a/alib2/src/regexp/RegExpEpsilon.cpp b/alib2/src/regexp/RegExpEpsilon.cpp index e102539f5fe874fd45961b58c222f1c6a41bca35..f82c17a4c5835daa594b91752ac58deabbe8b600 100644 --- a/alib2/src/regexp/RegExpEpsilon.cpp +++ b/alib2/src/regexp/RegExpEpsilon.cpp @@ -49,6 +49,10 @@ bool RegExpEpsilon::operator==(const RegExpEpsilon&) const { return true; } +void RegExpElement::getAlphabet( std::set<alphabet::Symbol> & alphabet ) const { + +} + bool RegExpEpsilon::containsEmptyString() const { return true; } diff --git a/alib2/src/regexp/RegExpEpsilon.h b/alib2/src/regexp/RegExpEpsilon.h index 6821472c104eab8010957bddfe5070b5a3e8bd67..d51af0821a3a48905676f156693646ebd5172d3d 100644 --- a/alib2/src/regexp/RegExpEpsilon.h +++ b/alib2/src/regexp/RegExpEpsilon.h @@ -37,7 +37,12 @@ public: virtual bool operator<(const Iteration&) const; virtual bool operator<(const RegExpSymbol&) const; virtual bool operator==(const RegExpEpsilon&) const; - + + /** + * @copydoc RegExpElement::getAlphabet() const + */ + virtual void getAlphabet( std::set<alphabet::Symbol> & alphabet ) const; + /** * @copydoc RegExpElement::containsEmptyString() const */ diff --git a/alib2/src/regexp/RegExpSymbol.cpp b/alib2/src/regexp/RegExpSymbol.cpp index f71609fc715cc47d51ebbd7cab0a53f148e8fa1a..140eee9f5210f0e097601e6a24c1fae478a85ff6 100644 --- a/alib2/src/regexp/RegExpSymbol.cpp +++ b/alib2/src/regexp/RegExpSymbol.cpp @@ -62,6 +62,10 @@ bool RegExpSymbol::isEmpty() const { return false; } +void RegExpSymbol::getAlphabet( std::set<alphabet::Symbol> & alphabet ) const { + alphabet.insert( Symbol( this->getSymbol( ) ) ); +} + const string& RegExpSymbol::getSymbol() const { return this->symbol; } diff --git a/alib2/src/regexp/RegExpSymbol.h b/alib2/src/regexp/RegExpSymbol.h index 1d617700d7eb5b53ccf48049bda90c360cccc9b8..83d79f6bae278708f25f4898f88a8bac129f91a3 100644 --- a/alib2/src/regexp/RegExpSymbol.h +++ b/alib2/src/regexp/RegExpSymbol.h @@ -41,7 +41,11 @@ public: virtual bool operator<(const RegExpSymbol&) const; virtual bool operator==(const RegExpSymbol&) const; - + /** + * @copydoc RegExpElement::getAlphabet() const + */ + virtual void getAlphabet( std::set<alphabet::Symbol> & alphabet ) const; + /** * @copydoc RegExpElement::containsEmptyString() const */