From 832a57e717cb56e7616889290bd8d65713ab460f Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Tue, 25 Feb 2014 21:07:23 +0100 Subject: [PATCH] implementation of y + x = x + y transformation --- aconversions/src/re2fa/Brzozowski.cpp | 2 +- aconvert.regexp/src/RegExpParser.cpp | 33 +++++++++++-------------- aconvert.regexp/src/RegExpParser.h | 4 +-- aderivation/src/aderivation.cpp | 2 +- aintegral/src/aintegral.cpp | 2 +- alib/src/RegExpFactory.h | 2 ++ alib/src/regexp/Alternation.cpp | 4 +-- alib/src/regexp/Alternation.h | 8 +++--- alib/src/regexp/Concatenation.cpp | 4 +-- alib/src/regexp/Concatenation.h | 8 +++--- alib/src/regexp/Iteration.h | 1 - alib/src/regexp/RegExp.cpp | 2 -- alib/src/regexp/RegExp.h | 5 ++-- alib/src/regexp/RegExpParser.cpp | 2 +- alib/src/regexp/RegExpParser.h | 4 ++- alib/src/regexp/RegExpPrinter.cpp | 2 +- alib/src/regexp/RegExpPrinter.h | 2 +- libaderivation/src/RegExpDerivation.cpp | 2 +- libaderivation/src/RegExpDerivation.h | 2 +- libaderivation/src/RegExpIntegral.cpp | 2 +- libaderivation/src/RegExpIntegral.h | 2 +- libaregexptree/src/RegExpOptimize.cpp | 8 ++++-- 22 files changed, 52 insertions(+), 51 deletions(-) diff --git a/aconversions/src/re2fa/Brzozowski.cpp b/aconversions/src/re2fa/Brzozowski.cpp index ef5d6e34cc..b481f6e27b 100644 --- a/aconversions/src/re2fa/Brzozowski.cpp +++ b/aconversions/src/re2fa/Brzozowski.cpp @@ -36,7 +36,7 @@ FSM Brzozowski::convert( void ) for( const auto & symbol : alphabet ) { - list<RegExpElement*> dString( 1, new RegExpSymbol( symbol.getSymbol( ). getSymbol( ) ) ); + vector<RegExpElement*> dString( 1, new RegExpSymbol( symbol.getSymbol( ). getSymbol( ) ) ); RegExp derived = deriv.derivation( dString ); derived.setRegExp ( opt.optimize( derived.getRegExp( ) ) ); diff --git a/aconvert.regexp/src/RegExpParser.cpp b/aconvert.regexp/src/RegExpParser.cpp index cf070c07a5..8ba459c5c1 100644 --- a/aconvert.regexp/src/RegExpParser.cpp +++ b/aconvert.regexp/src/RegExpParser.cpp @@ -19,25 +19,23 @@ regexp::RegExpElement* RegExpParser::AlternationCont(regexp::RegExpElement* left RegExpLexer::Token token = m_Lexer.token(); if(token.type == RegExpLexer::PLUS) { m_Lexer.next(); - regexp::Alternation* res = this->AlternationContCont(this->Concatenation()); - res->getElements().push_front(left); + regexp::Alternation* res = new regexp::Alternation(); + res->getElements().push_back(left); + this->AlternationContCont(res, this->Concatenation()); return res; } else { return left; } } -regexp::Alternation* RegExpParser::AlternationContCont(regexp::RegExpElement* left) { +void RegExpParser::AlternationContCont(regexp::Alternation* res, regexp::RegExpElement* left) { RegExpLexer::Token token = m_Lexer.token(); if(token.type == RegExpLexer::PLUS) { m_Lexer.next(); - regexp::Alternation* res = this->AlternationContCont(this->Concatenation()); - res->getElements().push_front(left); - return res; + res->getElements().push_back(left); + this->AlternationContCont(res, this->Concatenation()); } else { - regexp::Alternation* res = new regexp::Alternation(); - res->getElements().push_front(left); - return res; + res->getElements().push_back(left); } } @@ -50,25 +48,24 @@ regexp::RegExpElement* RegExpParser::ConcatenationCont(regexp::RegExpElement* le RegExpLexer::Token token = m_Lexer.token(); if(token.type == RegExpLexer::SYMBOL || token.type == RegExpLexer::LPAR || token.type == RegExpLexer::EPS || token.type == RegExpLexer::EMPTY) { - regexp::Concatenation* res = this->ConcatenationContCont(this->Factor()); - res->getElements().push_front(left); + regexp::Concatenation* res = new regexp::Concatenation(); + res->getElements().push_back(left); + this->ConcatenationContCont(res, this->Factor()); return res; } else { return left; } } -regexp::Concatenation* RegExpParser::ConcatenationContCont(regexp::RegExpElement* left) { +void RegExpParser::ConcatenationContCont(regexp::Concatenation* res, regexp::RegExpElement* left) { RegExpLexer::Token token = m_Lexer.token(); if(token.type == RegExpLexer::SYMBOL || token.type == RegExpLexer::LPAR || token.type == RegExpLexer::EPS || token.type == RegExpLexer::EMPTY) { - regexp::Concatenation* res = this->ConcatenationContCont(this->Factor()); - res->getElements().push_front(left); - return res; + res->getElements().push_back(left); + this->ConcatenationContCont(res, this->Factor()); } else { - regexp::Concatenation* res = new regexp::Concatenation(); - res->getElements().push_front(left); - return res; + + res->getElements().push_back(left); } } diff --git a/aconvert.regexp/src/RegExpParser.h b/aconvert.regexp/src/RegExpParser.h index 37e161181e..0177ccaaaa 100644 --- a/aconvert.regexp/src/RegExpParser.h +++ b/aconvert.regexp/src/RegExpParser.h @@ -26,11 +26,11 @@ public: private: regexp::RegExpElement* Alternation(); regexp::RegExpElement* AlternationCont(regexp::RegExpElement* left); - regexp::Alternation* AlternationContCont(regexp::RegExpElement* left); + void AlternationContCont(regexp::Alternation* res, regexp::RegExpElement* left); regexp::RegExpElement* Concatenation(); regexp::RegExpElement* ConcatenationCont(regexp::RegExpElement* left); - regexp::Concatenation* ConcatenationContCont(regexp::RegExpElement* left); + void ConcatenationContCont(regexp::Concatenation* res, regexp::RegExpElement* left); regexp::RegExpElement* Factor(); regexp::RegExpElement* Star(regexp::RegExpElement* elem); diff --git a/aderivation/src/aderivation.cpp b/aderivation/src/aderivation.cpp index 018c8da654..972a1f702f 100644 --- a/aderivation/src/aderivation.cpp +++ b/aderivation/src/aderivation.cpp @@ -27,7 +27,7 @@ int main(int argc, char** argv) SaxInterface::parseMemory(input, tokens); RegExp re = RegExpParser::parse(tokens); - list<RegExpElement*> dString; + vector<RegExpElement*> dString; for( int i = 1; i < argc ; i++ ) { string symbol( argv[ i ] ); diff --git a/aintegral/src/aintegral.cpp b/aintegral/src/aintegral.cpp index c5599a0a94..d460ab967e 100644 --- a/aintegral/src/aintegral.cpp +++ b/aintegral/src/aintegral.cpp @@ -27,7 +27,7 @@ int main(int argc, char** argv) SaxInterface::parseMemory(input, tokens); RegExp re = RegExpParser::parse(tokens); - list<RegExpElement*> dString; + vector<RegExpElement*> dString; for( int i = 1; i < argc ; i++ ) { string symbol( argv[ i ] ); diff --git a/alib/src/RegExpFactory.h b/alib/src/RegExpFactory.h index 510ddddc1d..de6d8f6430 100644 --- a/alib/src/RegExpFactory.h +++ b/alib/src/RegExpFactory.h @@ -8,6 +8,8 @@ #ifndef REGEXPFACTORY_H_ #define REGEXPFACTORY_H_ +#include <list> + #include "sax/Token.h" #include "regexp/RegExp.h" diff --git a/alib/src/regexp/Alternation.cpp b/alib/src/regexp/Alternation.cpp index 0a02374c60..d5cc96bcb6 100644 --- a/alib/src/regexp/Alternation.cpp +++ b/alib/src/regexp/Alternation.cpp @@ -42,11 +42,11 @@ Alternation::~Alternation() { elements.clear(); } -list<RegExpElement*>& Alternation::getElements() { +vector<RegExpElement*>& Alternation::getElements() { return elements; } -const list<RegExpElement*>& Alternation::getElements() const { +const vector<RegExpElement*>& Alternation::getElements() const { return elements; } diff --git a/alib/src/regexp/Alternation.h b/alib/src/regexp/Alternation.h index cfeb34d639..51884d82a1 100644 --- a/alib/src/regexp/Alternation.h +++ b/alib/src/regexp/Alternation.h @@ -8,7 +8,7 @@ #ifndef ALTERNATION_H_ #define ALTERNATION_H_ -#include <list> +#include <vector> #include "RegExpElement.h" namespace regexp { @@ -21,7 +21,7 @@ using namespace std; */ class Alternation: public RegExpElement { private: - list<RegExpElement*> elements; + vector<RegExpElement*> elements; public: Alternation(); Alternation(const Alternation& other); @@ -31,12 +31,12 @@ public: /** * @return list of operands */ - list<RegExpElement*>& getElements(); + vector<RegExpElement*>& getElements(); /** * @return list of operands */ - const list<RegExpElement*>& getElements() const; + const vector<RegExpElement*>& getElements() const; /** * @copydoc RegExpElement::clone() const diff --git a/alib/src/regexp/Concatenation.cpp b/alib/src/regexp/Concatenation.cpp index 623d2102f5..a9c8477aad 100644 --- a/alib/src/regexp/Concatenation.cpp +++ b/alib/src/regexp/Concatenation.cpp @@ -42,11 +42,11 @@ Concatenation::~Concatenation() { elements.clear(); } -list<RegExpElement*>& Concatenation::getElements() { +vector<RegExpElement*>& Concatenation::getElements() { return elements; } -const list<RegExpElement*>& Concatenation::getElements() const { +const vector<RegExpElement*>& Concatenation::getElements() const { return elements; } diff --git a/alib/src/regexp/Concatenation.h b/alib/src/regexp/Concatenation.h index e35d05eac2..f082f9d208 100644 --- a/alib/src/regexp/Concatenation.h +++ b/alib/src/regexp/Concatenation.h @@ -8,7 +8,7 @@ #ifndef CONCATENATION_H_ #define CONCATENATION_H_ -#include <list> +#include <vector> #include "RegExpElement.h" namespace regexp { @@ -21,7 +21,7 @@ using namespace std; */ class Concatenation: public RegExpElement { private: - list<RegExpElement*> elements; + vector<RegExpElement*> elements; public: Concatenation(); Concatenation(const Concatenation& other); @@ -31,12 +31,12 @@ public: /** * @return list of operands */ - list<RegExpElement*>& getElements(); + vector<RegExpElement*>& getElements(); /** * @return list of operands */ - const list<RegExpElement*>& getElements() const; + const vector<RegExpElement*>& getElements() const; /** * @copydoc RegExpElement::clone() const diff --git a/alib/src/regexp/Iteration.h b/alib/src/regexp/Iteration.h index ab34a89deb..49b9e2cb82 100644 --- a/alib/src/regexp/Iteration.h +++ b/alib/src/regexp/Iteration.h @@ -8,7 +8,6 @@ #ifndef ITERATION_H_ #define ITERATION_H_ -#include <list> #include "RegExpElement.h" namespace regexp { diff --git a/alib/src/regexp/RegExp.cpp b/alib/src/regexp/RegExp.cpp index 3d8f2dbe13..895b858ef0 100644 --- a/alib/src/regexp/RegExp.cpp +++ b/alib/src/regexp/RegExp.cpp @@ -8,8 +8,6 @@ #include "RegExp.h" #include "RegExpPrinter.h" -#include <iostream> - namespace regexp { RegExp::RegExp() { diff --git a/alib/src/regexp/RegExp.h b/alib/src/regexp/RegExp.h index 36b5de12ad..ebcc518531 100644 --- a/alib/src/regexp/RegExp.h +++ b/alib/src/regexp/RegExp.h @@ -8,9 +8,8 @@ #ifndef REGEXP_H_ #define REGEXP_H_ -#include <vector> -#include <list> -#include <string> +#include <iostream> + #include "RegExpElement.h" #include "RegExpEmpty.h" diff --git a/alib/src/regexp/RegExpParser.cpp b/alib/src/regexp/RegExpParser.cpp index 5bcad37145..81d52a3987 100644 --- a/alib/src/regexp/RegExpParser.cpp +++ b/alib/src/regexp/RegExpParser.cpp @@ -68,7 +68,7 @@ Iteration* RegExpParser::parseIteration(list<Token>& input) { return iteration; } -void RegExpParser::parseContent(list<Token>& input, list<RegExpElement*>& elements) { +void RegExpParser::parseContent(list<Token>& input, vector<RegExpElement*>& elements) { while (true) { RegExpElement* element = parseElement(input); if(!element) return; diff --git a/alib/src/regexp/RegExpParser.h b/alib/src/regexp/RegExpParser.h index 126f20ee5d..808ad17d24 100644 --- a/alib/src/regexp/RegExpParser.h +++ b/alib/src/regexp/RegExpParser.h @@ -8,6 +8,8 @@ #ifndef REGEXPPARSER_H_ #define REGEXPPARSER_H_ +#include <list> + #include "RegExp.h" #include "../sax/Token.h" #include "RegExpSymbol.h" @@ -27,7 +29,7 @@ using namespace sax; */ class RegExpParser { protected: - static void parseContent(list<Token>& input, list<RegExpElement*>& elements); + static void parseContent(list<Token>& input, vector<RegExpElement*>& elements); static RegExpElement* parseElement(list<Token>& input); static RegExpEpsilon* parseEpsilon(list<Token>& input); diff --git a/alib/src/regexp/RegExpPrinter.cpp b/alib/src/regexp/RegExpPrinter.cpp index bd8371e3c7..e49db76e03 100644 --- a/alib/src/regexp/RegExpPrinter.cpp +++ b/alib/src/regexp/RegExpPrinter.cpp @@ -56,7 +56,7 @@ void RegExpPrinter::printElement(RegExpElement* element, ostream& out, string pr } -void RegExpPrinter::printContent(list<RegExpElement*>& content, ostream& out, string prefix) { +void RegExpPrinter::printContent(vector<RegExpElement*>& content, ostream& out, string prefix) { for (auto element : content) { printElement(element, out, prefix); } diff --git a/alib/src/regexp/RegExpPrinter.h b/alib/src/regexp/RegExpPrinter.h index 49ba46d027..33c21f8da4 100644 --- a/alib/src/regexp/RegExpPrinter.h +++ b/alib/src/regexp/RegExpPrinter.h @@ -28,7 +28,7 @@ class RegExpPrinter { protected: static const string INDENTATION; static void printElement(RegExpElement* element, ostream& out, string prefix); - static void printContent(list<RegExpElement*>& content, ostream& out, string prefix); + static void printContent(vector<RegExpElement*>& content, ostream& out, string prefix); static void printAlternation(Alternation* alternation, ostream& out, string prefix); static void printConcatenation(Concatenation* concatenation, ostream& out, string prefix); diff --git a/libaderivation/src/RegExpDerivation.cpp b/libaderivation/src/RegExpDerivation.cpp index c7909a7e4f..9c08db0953 100644 --- a/libaderivation/src/RegExpDerivation.cpp +++ b/libaderivation/src/RegExpDerivation.cpp @@ -16,7 +16,7 @@ RegExpDerivation::RegExpDerivation( const RegExp & re ) : m_re( re ) } -RegExp RegExpDerivation::derivation ( const list<RegExpElement*> & dString ) const +RegExp RegExpDerivation::derivation ( const vector<RegExpElement*> & dString ) const { const RegExpElement * oldRegExp = m_re.getRegExp( )->clone( ), * derivedRegExp; diff --git a/libaderivation/src/RegExpDerivation.h b/libaderivation/src/RegExpDerivation.h index e840bfd4e2..d733773cb2 100644 --- a/libaderivation/src/RegExpDerivation.h +++ b/libaderivation/src/RegExpDerivation.h @@ -33,7 +33,7 @@ class RegExpDerivation { public: RegExpDerivation( const regexp::RegExp & re ); - regexp::RegExp derivation( const std::list<regexp::RegExpElement*> & dString ) const; + regexp::RegExp derivation( const std::vector<regexp::RegExpElement*> & dString ) const; private: regexp::RegExpElement * derivation( const regexp::RegExpElement * element, const regexp::RegExpSymbol & dSymbol ) const; diff --git a/libaderivation/src/RegExpIntegral.cpp b/libaderivation/src/RegExpIntegral.cpp index edbd68cfbf..25b8ce5b79 100644 --- a/libaderivation/src/RegExpIntegral.cpp +++ b/libaderivation/src/RegExpIntegral.cpp @@ -15,7 +15,7 @@ RegExpIntegral::RegExpIntegral( const RegExp & re ) : m_re( re ) } -RegExp RegExpIntegral::integral( const list<RegExpElement*> & dString ) const +RegExp RegExpIntegral::integral( const vector<RegExpElement*> & dString ) const { const RegExpElement * oldRegExp = m_re.getRegExp( )->clone( ), * integralRegExp; diff --git a/libaderivation/src/RegExpIntegral.h b/libaderivation/src/RegExpIntegral.h index 4e0b2b549d..cbc3a01084 100644 --- a/libaderivation/src/RegExpIntegral.h +++ b/libaderivation/src/RegExpIntegral.h @@ -22,7 +22,7 @@ class RegExpIntegral { public: RegExpIntegral( const regexp::RegExp & re ); - regexp::RegExp integral( const std::list<regexp::RegExpElement*> & dString ) const; + regexp::RegExp integral( const std::vector<regexp::RegExpElement*> & dString ) const; private: regexp::RegExpElement * integral( const regexp::RegExpElement * node, const regexp::RegExpSymbol & dSymbol ) const; diff --git a/libaregexptree/src/RegExpOptimize.cpp b/libaregexptree/src/RegExpOptimize.cpp index 49e6bb34ec..3dce1ee6b4 100644 --- a/libaregexptree/src/RegExpOptimize.cpp +++ b/libaregexptree/src/RegExpOptimize.cpp @@ -6,6 +6,7 @@ */ #include "RegExpOptimize.h" +#include <algorithm> using namespace alib; using namespace regexp; @@ -63,8 +64,11 @@ RegExpElement * RegExpOptimize::optimize( Alternation * node ) it ++; } - // TODO: A2: x + y = y + x - // reorder list by RegExpElement::operator< ? + // A2: x + y = y + x + + std::sort (nodeElements.begin( ), nodeElements.end( ), [](RegExpElement const * const & a, RegExpElement const * const & b) -> bool { + return *a < *b; + }); // A3: x + EMPTY = x for( auto it = nodeElements.begin( ) ; it != nodeElements.end( ); ) -- GitLab