diff --git a/alib2algo/src/automaton/convert/ToRegExpStateElimination.cpp b/alib2algo/src/automaton/convert/ToRegExpStateElimination.cpp index 88bdb12a9beb6e42701ec060efaa225a08757542..45d54100118e5bcc7d318d0a2c1f89545e3f9ca5 100644 --- a/alib2algo/src/automaton/convert/ToRegExpStateElimination.cpp +++ b/alib2algo/src/automaton/convert/ToRegExpStateElimination.cpp @@ -2,7 +2,7 @@ * ToRegExpStateElimination.cpp * * Created on: 9. 2. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #include "ToRegExpStateElimination.h" @@ -31,24 +31,24 @@ regexp::RegExp ToRegExpStateElimination::convert(const automaton::Automaton& aut template<class T> regexp::RegExp ToRegExpStateElimination::convert(const T& automaton) { - if(automaton.getFinalStates().size() == 0) - return regexp::RegExp(regexp::UnboundedRegExp(regexp::UnboundedRegExpEmpty())); + if(automaton.getFinalStates().size() == 0) + return regexp::RegExp(regexp::UnboundedRegExp(regexp::UnboundedRegExpEmpty())); - // steps 1 + 2 - automaton::ExtendedNFA extendedAutomaton(automaton); - extendExtendedNFA(extendedAutomaton); + // steps 1 + 2 + automaton::ExtendedNFA extendedAutomaton(automaton); + extendExtendedNFA(extendedAutomaton); - // step 3 - Exterminate! - // select all states that are neither final nor initial - std::set<automaton::State> statesToEliminate = extendedAutomaton.getStates(); - statesToEliminate.erase(extendedAutomaton.getInitialState()); - statesToEliminate.erase(*extendedAutomaton.getFinalStates().begin()); + // step 3 - Exterminate! + // select all states that are neither final nor initial + std::set<automaton::State> statesToEliminate = extendedAutomaton.getStates(); + statesToEliminate.erase(extendedAutomaton.getInitialState()); + statesToEliminate.erase(*extendedAutomaton.getFinalStates().begin()); - for(const auto& state : statesToEliminate) - extendedAutomaton = eliminateState(extendedAutomaton, state); + for(const auto& state : statesToEliminate) + extendedAutomaton = eliminateState(extendedAutomaton, state); - // step 4 - return regexp::simplify::RegExpOptimize::optimize(regexp::RegExpConcatenate::concatenate( + // step 4 + return regexp::simplify::RegExpOptimize::optimize(regexp::RegExpConcatenate::concatenate( transition(extendedAutomaton, extendedAutomaton.getInitialState(), *extendedAutomaton.getFinalStates().begin()), regexp::RegExpIterate::iterate(transition(extendedAutomaton, *extendedAutomaton.getFinalStates().begin(), *extendedAutomaton.getFinalStates().begin())))); } @@ -56,108 +56,108 @@ regexp::RegExp ToRegExpStateElimination::convert(const T& automaton) automaton::ExtendedNFA ToRegExpStateElimination::eliminateState(const automaton::ExtendedNFA& extendedAutomaton, const automaton::State& q) { - automaton::ExtendedNFA newAutomaton(extendedAutomaton.getInitialState()); // sure that q is neither initial nor final (follows from step 2 - extending ExtendedNFA) - newAutomaton.setStates(extendedAutomaton.getStates()); - newAutomaton.removeState(q); // preserve all states but q (the one to eliminate) - newAutomaton.setInputSymbols(extendedAutomaton.getInputAlphabet()); - newAutomaton.setFinalStates(extendedAutomaton.getFinalStates()); - - for(const auto& p: newAutomaton.getStates()) - { - for(const auto& r : newAutomaton.getStates()) - { - regexp::RegExp concat = transition(extendedAutomaton, p, q); - concat = regexp::RegExpConcatenate::concatenate(concat, regexp::RegExpIterate::iterate(transition(extendedAutomaton, q, q))); - concat = regexp::RegExpConcatenate::concatenate(concat, transition(extendedAutomaton, q, r)); - - regexp::RegExp alt = regexp::RegExpAlternate::alternate(concat, transition(extendedAutomaton, p, r)); - - newAutomaton.addTransition(p, regexp::simplify::RegExpOptimize::optimize(alt), r); - } - } - - return newAutomaton; + automaton::ExtendedNFA newAutomaton(extendedAutomaton.getInitialState()); // sure that q is neither initial nor final (follows from step 2 - extending ExtendedNFA) + newAutomaton.setStates(extendedAutomaton.getStates()); + newAutomaton.removeState(q); // preserve all states but q (the one to eliminate) + newAutomaton.setInputSymbols(extendedAutomaton.getInputAlphabet()); + newAutomaton.setFinalStates(extendedAutomaton.getFinalStates()); + + for(const auto& p: newAutomaton.getStates()) + { + for(const auto& r : newAutomaton.getStates()) + { + regexp::RegExp concat = transition(extendedAutomaton, p, q); + concat = regexp::RegExpConcatenate::concatenate(concat, regexp::RegExpIterate::iterate(transition(extendedAutomaton, q, q))); + concat = regexp::RegExpConcatenate::concatenate(concat, transition(extendedAutomaton, q, r)); + + regexp::RegExp alt = regexp::RegExpAlternate::alternate(concat, transition(extendedAutomaton, p, r)); + + newAutomaton.addTransition(p, regexp::simplify::RegExpOptimize::optimize(alt), r); + } + } + + return newAutomaton; } const regexp::RegExp ToRegExpStateElimination::transition(const automaton::ExtendedNFA& automaton, const automaton::State& from, const automaton::State& to) { - regexp::RegExp ret(regexp::UnboundedRegExp(regexp::UnboundedRegExpEmpty { })); + regexp::RegExp ret(regexp::UnboundedRegExp(regexp::UnboundedRegExpEmpty { })); - for(const auto& transition: automaton.getTransitionsFromState(from)) - if(transition.second.count(to) > 0) - ret = regexp::RegExpAlternate::alternate(ret, transition.first.second); + for(const auto& transition: automaton.getTransitionsFromState(from)) + if(transition.second.count(to) > 0) + ret = regexp::RegExpAlternate::alternate(ret, transition.first.second); - return regexp::simplify::RegExpOptimize::optimize(ret); + return regexp::simplify::RegExpOptimize::optimize(ret); } void ToRegExpStateElimination::extendExtendedNFA(automaton::ExtendedNFA& automaton) { - const automaton::State& initState = automaton.getInitialState(); - if(automaton.getFinalStates().count(initState) > 0 || automaton.getTransitionsToState(initState).size() > 0 ) - { - const automaton::State q0 = automaton::createUniqueState(initState, automaton.getStates()); - automaton.addState(q0); - - regexp::UnboundedRegExp regexp; - regexp.setRegExp(regexp::UnboundedRegExpEpsilon()); - automaton.addTransition(q0, regexp::RegExp{regexp}, initState); - - automaton.setInitialState(q0); - } - - if(automaton.getFinalStates().size() > 1) - { - const automaton::State f = automaton::createUniqueState(automaton::State("f"), automaton.getStates()); - automaton.addState(f); - - const std::set<automaton::State> automatonFinalStates = automaton.getFinalStates(); - for(const auto &state : automatonFinalStates) - { - regexp::UnboundedRegExp regexp; - regexp.setRegExp(regexp::UnboundedRegExpEpsilon()); - automaton.addTransition(state, regexp::RegExp{regexp}, f); - - automaton.removeFinalState(state); - } - - automaton.addFinalState(f); - } + const automaton::State& initState = automaton.getInitialState(); + if(automaton.getFinalStates().count(initState) > 0 || automaton.getTransitionsToState(initState).size() > 0 ) + { + const automaton::State q0 = automaton::createUniqueState(initState, automaton.getStates()); + automaton.addState(q0); + + regexp::UnboundedRegExp regexp; + regexp.setRegExp(regexp::UnboundedRegExpEpsilon()); + automaton.addTransition(q0, regexp::RegExp{regexp}, initState); + + automaton.setInitialState(q0); + } + + if(automaton.getFinalStates().size() > 1) + { + const automaton::State f = automaton::createUniqueState(automaton::State("f"), automaton.getStates()); + automaton.addState(f); + + const std::set<automaton::State> automatonFinalStates = automaton.getFinalStates(); + for(const auto &state : automatonFinalStates) + { + regexp::UnboundedRegExp regexp; + regexp.setRegExp(regexp::UnboundedRegExpEpsilon()); + automaton.addTransition(state, regexp::RegExp{regexp}, f); + + automaton.removeFinalState(state); + } + + automaton.addFinalState(f); + } } void ToRegExpStateElimination::Visit(void* data, const automaton::EpsilonNFA& automaton) const { - regexp::RegExp* & out = *((regexp::RegExp**) data); - out = new regexp::RegExp(convert(automaton)); + regexp::RegExp* & out = *((regexp::RegExp**) data); + out = new regexp::RegExp(convert(automaton)); } void ToRegExpStateElimination::Visit(void* data, const automaton::MultiInitialStateNFA& automaton) const { - regexp::RegExp* & out = *((regexp::RegExp**) data); - out = new regexp::RegExp(convert(automaton)); + regexp::RegExp* & out = *((regexp::RegExp**) data); + out = new regexp::RegExp(convert(automaton)); } void ToRegExpStateElimination::Visit(void* data, const automaton::NFA& automaton) const { - regexp::RegExp* & out = *((regexp::RegExp**) data); - out = new regexp::RegExp(convert(automaton)); + regexp::RegExp* & out = *((regexp::RegExp**) data); + out = new regexp::RegExp(convert(automaton)); } void ToRegExpStateElimination::Visit(void* data, const automaton::DFA& automaton) const { - regexp::RegExp* & out = *((regexp::RegExp**) data); - out = new regexp::RegExp(convert(automaton)); + regexp::RegExp* & out = *((regexp::RegExp**) data); + out = new regexp::RegExp(convert(automaton)); } void ToRegExpStateElimination::Visit(void* data, const automaton::ExtendedNFA& automaton) const { - regexp::RegExp* & out = *((regexp::RegExp**) data); - out = new regexp::RegExp(convert(automaton)); + regexp::RegExp* & out = *((regexp::RegExp**) data); + out = new regexp::RegExp(convert(automaton)); } void ToRegExpStateElimination::Visit(void* data, const automaton::CompactNFA& automaton) const { - regexp::RegExp* & out = *((regexp::RegExp**) data); - out = new regexp::RegExp(convert(automaton)); + regexp::RegExp* & out = *((regexp::RegExp**) data); + out = new regexp::RegExp(convert(automaton)); } const ToRegExpStateElimination ToRegExpStateElimination::TO_REG_EXP_STATE_ELIMINATION; diff --git a/alib2algo/src/automaton/convert/ToRegExpStateElimination.h b/alib2algo/src/automaton/convert/ToRegExpStateElimination.h index a70f35786f360798690a08044790f78f12cbb04a..f11cd297928a127670375bbef134fc5029613df4 100644 --- a/alib2algo/src/automaton/convert/ToRegExpStateElimination.h +++ b/alib2algo/src/automaton/convert/ToRegExpStateElimination.h @@ -2,7 +2,7 @@ * ToRegExpStateElimination.h * * Created on: 9. 2. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #ifndef TO_REG_EXP_STATE_ELIMINATION_H_ @@ -28,31 +28,31 @@ namespace convert { */ class ToRegExpStateElimination : public automaton::VisitableConstFSMBase { public: - /** - * Performs conversion. - * @param automaton automaton to convert - * @return regular expression equivalent to source NFA. - */ - static regexp::RegExp convert(const automaton::Automaton& automaton); + /** + * Performs conversion. + * @param automaton automaton to convert + * @return regular expression equivalent to source NFA. + */ + static regexp::RegExp convert(const automaton::Automaton& automaton); - template<class T> - static regexp::RegExp convert(const T& automaton); + template<class T> + static regexp::RegExp convert(const T& automaton); private: - void Visit(void*, const automaton::EpsilonNFA& automaton) const; - void Visit(void*, const automaton::MultiInitialStateNFA& automaton) const; - void Visit(void*, const automaton::NFA& automaton) const; - void Visit(void*, const automaton::DFA& automaton) const; - void Visit(void*, const automaton::ExtendedNFA& automaton) const; - void Visit(void*, const automaton::CompactNFA& automaton) const; + void Visit(void*, const automaton::EpsilonNFA& automaton) const; + void Visit(void*, const automaton::MultiInitialStateNFA& automaton) const; + void Visit(void*, const automaton::NFA& automaton) const; + void Visit(void*, const automaton::DFA& automaton) const; + void Visit(void*, const automaton::ExtendedNFA& automaton) const; + void Visit(void*, const automaton::CompactNFA& automaton) const; - static void extendExtendedNFA(automaton::ExtendedNFA& automaton); + static void extendExtendedNFA(automaton::ExtendedNFA& automaton); - static const regexp::RegExp transition(const automaton::ExtendedNFA& automaton, const automaton::State& from, const automaton::State& to); + static const regexp::RegExp transition(const automaton::ExtendedNFA& automaton, const automaton::State& from, const automaton::State& to); - static automaton::ExtendedNFA eliminateState(const automaton::ExtendedNFA& extendedAutomaton, const automaton::State& state); + static automaton::ExtendedNFA eliminateState(const automaton::ExtendedNFA& extendedAutomaton, const automaton::State& state); - static const ToRegExpStateElimination TO_REG_EXP_STATE_ELIMINATION; + static const ToRegExpStateElimination TO_REG_EXP_STATE_ELIMINATION; }; } /* namespace convert */ diff --git a/alib2algo/src/automaton/generate/RandomAutomatonFactory.h b/alib2algo/src/automaton/generate/RandomAutomatonFactory.h index 0f2881e34ac187fd9d26f16969dd93e2a6789d46..5c94899f7a08eb5b6d41faf2c27a756839e419eb 100644 --- a/alib2algo/src/automaton/generate/RandomAutomatonFactory.h +++ b/alib2algo/src/automaton/generate/RandomAutomatonFactory.h @@ -2,7 +2,7 @@ * RandomAutomatonFactory.h * * Created on: 27. 3. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #ifndef RANDOM_AUTOMATON_FACTORY_H_ @@ -20,11 +20,11 @@ namespace generate { class RandomAutomatonFactory { public: - static automaton::NFA generateNFA( size_t statesCount, std::set<alphabet::Symbol> alphabet, double density ); - static automaton::NFA generateNFA( size_t statesCount, size_t alphabetSize, double density ); + static automaton::NFA generateNFA( size_t statesCount, std::set<alphabet::Symbol> alphabet, double density ); + static automaton::NFA generateNFA( size_t statesCount, size_t alphabetSize, double density ); private: - static automaton::NFA LeslieConnectedNFA( size_t n, const std::deque<alphabet::Symbol> & alphabet, double density ); + static automaton::NFA LeslieConnectedNFA( size_t n, const std::deque<alphabet::Symbol> & alphabet, double density ); }; } /* namespace generate */ diff --git a/alib2algo/src/automaton/run/Accept.cpp b/alib2algo/src/automaton/run/Accept.cpp index d810e76289730ff3680dd72f0bb1a6c5cf5fbceb..6c68c9a12ce69a11ce39a76f3f5c501db8a54cf6 100644 --- a/alib2algo/src/automaton/run/Accept.cpp +++ b/alib2algo/src/automaton/run/Accept.cpp @@ -2,7 +2,7 @@ * Accept.cpp * * Created on: 9. 2. 2014 - * Author: Jan Travnicek + * Author: Jan Travnicek */ #include "Accept.h" @@ -35,19 +35,19 @@ bool Accept::accept(const automaton::DFA& automaton, const string::LinearString& } bool recursiveAccept(const automaton::NFA& automaton, const automaton::State& state, const string::LinearString& string) { - if( string.getContent().empty( ) ) - return automaton.getFinalStates().count( state ); + if( string.getContent().empty( ) ) + return automaton.getFinalStates().count( state ); - const alphabet::Symbol & inputSymbol = string.getContent().front( ); + const alphabet::Symbol & inputSymbol = string.getContent().front( ); - string::LinearString newString(std::vector<alphabet::Symbol>{ string.getContent().begin() + 1, string.getContent().end()}); + string::LinearString newString(std::vector<alphabet::Symbol>{ string.getContent().begin() + 1, string.getContent().end()}); - for( const auto & transition : automaton.getTransitionsFromState( state ) ) - for(const auto& to : transition.second) - if( transition.first.second == inputSymbol && recursiveAccept( automaton, to, newString ) ) - return true; + for( const auto & transition : automaton.getTransitionsFromState( state ) ) + for(const auto& to : transition.second) + if( transition.first.second == inputSymbol && recursiveAccept( automaton, to, newString ) ) + return true; - return false; + return false; } bool Accept::accept( const automaton::NFA& automaton, const string::LinearString & string ) { diff --git a/alib2algo/src/automaton/run/Occurrences.cpp b/alib2algo/src/automaton/run/Occurrences.cpp index c4638d816673da661db3af1014a2881ed59d6855..f898a074747bf04dcf47024bb2b006d3df78486d 100644 --- a/alib2algo/src/automaton/run/Occurrences.cpp +++ b/alib2algo/src/automaton/run/Occurrences.cpp @@ -2,7 +2,7 @@ * Occurrences.cpp * * Created on: 9. 2. 2014 - * Author: Jan Travnicek + * Author: Jan Travnicek */ #include "Occurrences.h" diff --git a/alib2algo/src/automaton/run/Occurrences.h b/alib2algo/src/automaton/run/Occurrences.h index ef2946fa9f842954e8c5b9832b5a39611c8345d7..844e4312437be15846e0a4caeb8be7ee16a650f3 100644 --- a/alib2algo/src/automaton/run/Occurrences.h +++ b/alib2algo/src/automaton/run/Occurrences.h @@ -2,7 +2,7 @@ * Occurrences.h * * Created on: 9. 2. 2014 - * Author: Jan Travnicek + * Author: Jan Travnicek */ #ifndef _AUTOMATON_OCCURRENCES_H__ diff --git a/alib2algo/src/automaton/simplify/EpsilonRemover.cpp b/alib2algo/src/automaton/simplify/EpsilonRemover.cpp index 13df9d02a54cb0f8cd8b12ab8a3da94f2d0766f9..a50786e7a3293a8fa5687d3453a5b59beb631cc7 100644 --- a/alib2algo/src/automaton/simplify/EpsilonRemover.cpp +++ b/alib2algo/src/automaton/simplify/EpsilonRemover.cpp @@ -15,17 +15,17 @@ namespace simplify { automaton::DFA EpsilonRemover::remove(const automaton::DFA& origFSM) { - return origFSM; + return origFSM; } automaton::MultiInitialStateNFA EpsilonRemover::remove(const automaton::MultiInitialStateNFA& origFSM) { - return origFSM; + return origFSM; } automaton::NFA EpsilonRemover::remove(const automaton::NFA& origFSM) { - return origFSM; + return origFSM; } automaton::NFA EpsilonRemover::remove( const automaton::EpsilonNFA & origFSM ) { diff --git a/alib2algo/src/automaton/simplify/EpsilonRemover.h b/alib2algo/src/automaton/simplify/EpsilonRemover.h index 38aa69f2e356ef0011ae618a8ffaa621a250b325..b7a336ed0fa191960e2cac5584b5b98f98dd518d 100644 --- a/alib2algo/src/automaton/simplify/EpsilonRemover.h +++ b/alib2algo/src/automaton/simplify/EpsilonRemover.h @@ -2,7 +2,7 @@ * EpsilonRemover.h * * Created on: 16. 1. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #ifndef EPSILON_REMOVER_H_ diff --git a/alib2algo/src/automaton/simplify/Minimize.cpp b/alib2algo/src/automaton/simplify/Minimize.cpp index 548354a3d65b95256027b3cf0e37ed4ba0e8d084..6c01ed6541a6fda7b151601d7983b4eaa4e8527c 100644 --- a/alib2algo/src/automaton/simplify/Minimize.cpp +++ b/alib2algo/src/automaton/simplify/Minimize.cpp @@ -2,7 +2,7 @@ * Minimize.cpp * * Created on: Dec 9, 2013 - * Author: honza + * Author: honza */ #include "Minimize.h" diff --git a/alib2algo/src/automaton/simplify/Minimize.h b/alib2algo/src/automaton/simplify/Minimize.h index cc16d9f6200d94fcae80febf1c920f11afe201ab..8cd094c7de01d9a4a5ea4188a5d1673e100b7123 100644 --- a/alib2algo/src/automaton/simplify/Minimize.h +++ b/alib2algo/src/automaton/simplify/Minimize.h @@ -2,7 +2,7 @@ * Minimize.h * * Created on: Dec 9, 2013 - * Author: Jan Travnicek + * Author: Jan Travnicek */ #ifndef MINIMIZE_H_ diff --git a/alib2algo/src/automaton/simplify/Normalize.h b/alib2algo/src/automaton/simplify/Normalize.h index 79523da733cc1c1e989cdd85508c98164c6dd516..2918934782faf7202fbae0cfaa6d0ee49b6bf4e4 100644 --- a/alib2algo/src/automaton/simplify/Normalize.h +++ b/alib2algo/src/automaton/simplify/Normalize.h @@ -2,7 +2,7 @@ * Normalize.h * * Created on: Dec 9, 2013 - * Author: Jan Travnicek + * Author: Jan Travnicek */ #ifndef NORMALIZE_H_ diff --git a/alib2algo/src/regexp/GlushkovPair.cpp b/alib2algo/src/regexp/GlushkovPair.cpp index fb0e0bc0190b9a509d69dcb7eb67e07cf7933056..8d06d8e5d478e14fb2f41933ac9fdc8e4c5bf5ad 100644 --- a/alib2algo/src/regexp/GlushkovPair.cpp +++ b/alib2algo/src/regexp/GlushkovPair.cpp @@ -2,7 +2,7 @@ * GlushkovPair.cpp * * Created on: 14. 4. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #include "GlushkovPair.h" @@ -11,26 +11,26 @@ namespace regexp { GlushkovPair::GlushkovPair( GlushkovSymbol const& first, GlushkovSymbol const& second ) : - m_first( first ), m_second( second ) + m_first( first ), m_second( second ) { } bool GlushkovPair::operator<( GlushkovPair const& x ) const { - if( m_first.getId( ) == x.m_first.getId( ) ) - return m_second.getId( ) < x.m_second.getId( ); - else - return m_first.getId( ) < x.m_first.getId( ); + if( m_first.getId( ) == x.m_first.getId( ) ) + return m_second.getId( ) < x.m_second.getId( ); + else + return m_first.getId( ) < x.m_first.getId( ); } GlushkovSymbol const& GlushkovPair::getFirst( void ) const { - return m_first; + return m_first; } GlushkovSymbol const& GlushkovPair::getSecond( void ) const { - return m_second; + return m_second; } } /* namespace conversions */ diff --git a/alib2algo/src/regexp/GlushkovPair.h b/alib2algo/src/regexp/GlushkovPair.h index 0a36788125301461e485e64382b193f2431a23c2..5ad87945d526f3ecb750e23f324edcaba4988aea 100644 --- a/alib2algo/src/regexp/GlushkovPair.h +++ b/alib2algo/src/regexp/GlushkovPair.h @@ -2,7 +2,7 @@ * GlushkovPair.h * * Created on: 14. 4. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #ifndef GLUSHKOVPAIR_H_ @@ -18,14 +18,14 @@ namespace regexp { class GlushkovPair { private: - GlushkovSymbol const m_first; - GlushkovSymbol const m_second; + GlushkovSymbol const m_first; + GlushkovSymbol const m_second; public: - GlushkovPair( GlushkovSymbol const& first, GlushkovSymbol const& second ); - bool operator<( GlushkovPair const& x ) const; - GlushkovSymbol const& getFirst( void ) const; - GlushkovSymbol const& getSecond( void ) const; + GlushkovPair( GlushkovSymbol const& first, GlushkovSymbol const& second ); + bool operator<( GlushkovPair const& x ) const; + GlushkovSymbol const& getFirst( void ) const; + GlushkovSymbol const& getSecond( void ) const; }; } /* namespace conversions */ diff --git a/alib2algo/src/regexp/GlushkovSymbol.cpp b/alib2algo/src/regexp/GlushkovSymbol.cpp index 6245462bb657782066f35d9afb72dfad6327f271..eebd3323579636dc74832a1f85115cf7f496e03c 100644 --- a/alib2algo/src/regexp/GlushkovSymbol.cpp +++ b/alib2algo/src/regexp/GlushkovSymbol.cpp @@ -2,7 +2,7 @@ * GlushkovSymbol.cpp * * Created on: 14. 4. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #include "GlushkovSymbol.h" @@ -14,29 +14,29 @@ namespace regexp { GlushkovSymbol::GlushkovSymbol( regexp::UnboundedRegExpSymbol const * const & node, int i ) : - m_regexpSymbol( node ), m_i( i ) + m_regexpSymbol( node ), m_i( i ) { } bool GlushkovSymbol::operator<( GlushkovSymbol const& x ) const { - return m_i < x.m_i; + return m_i < x.m_i; } int GlushkovSymbol::getId( void ) const { - return m_i; + return m_i; } Symbol GlushkovSymbol::getInputSymbol( void ) const { - return Symbol( m_regexpSymbol->getSymbol( ) ); + return Symbol( m_regexpSymbol->getSymbol( ) ); } regexp::UnboundedRegExpSymbol const * GlushkovSymbol::getSymbolPtr( void ) const { - return m_regexpSymbol; + return m_regexpSymbol; } } /* namespace conversions */ diff --git a/alib2algo/src/regexp/GlushkovSymbol.h b/alib2algo/src/regexp/GlushkovSymbol.h index 88ed666365717569fe77ddeb6212b715827aa824..10d3f7863d826816a693e2570d0ad82246a9d1f5 100644 --- a/alib2algo/src/regexp/GlushkovSymbol.h +++ b/alib2algo/src/regexp/GlushkovSymbol.h @@ -2,7 +2,7 @@ * GlushkovSymbol.h * * Created on: 14. 4. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #ifndef GLUSHKOVSYMBOL_H_ @@ -20,15 +20,15 @@ namespace regexp { class GlushkovSymbol { private: - regexp::UnboundedRegExpSymbol const * const m_regexpSymbol; - int m_i; + regexp::UnboundedRegExpSymbol const * const m_regexpSymbol; + int m_i; public: - GlushkovSymbol( regexp::UnboundedRegExpSymbol const * const & node, int i ); - bool operator<( GlushkovSymbol const& x ) const; - int getId( void ) const; - alphabet::Symbol getInputSymbol( void ) const; - regexp::UnboundedRegExpSymbol const * getSymbolPtr( void ) const; + GlushkovSymbol( regexp::UnboundedRegExpSymbol const * const & node, int i ); + bool operator<( GlushkovSymbol const& x ) const; + int getId( void ) const; + alphabet::Symbol getInputSymbol( void ) const; + regexp::UnboundedRegExpSymbol const * getSymbolPtr( void ) const; }; } /* namespace conversions */ diff --git a/alib2algo/src/regexp/GlushkovTraversal.cpp b/alib2algo/src/regexp/GlushkovTraversal.cpp index b622ed1a01b315c4cd03eac7e3ade2fd37d101bb..07060c8d358b639b5313f5e5c87b1b687e2eb61d 100644 --- a/alib2algo/src/regexp/GlushkovTraversal.cpp +++ b/alib2algo/src/regexp/GlushkovTraversal.cpp @@ -2,7 +2,7 @@ * GlushkovTraversal.cpp * * Created on: 13. 3. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #include "GlushkovTraversal.h" @@ -18,423 +18,423 @@ namespace regexp const GlushkovSymbol& GlushkovTraversal::findSymbol( regexp::UnboundedRegExpSymbol const * const symbol, const std::set<GlushkovSymbol> & symbolSet ) { - auto it = find_if( symbolSet.begin( ), symbolSet.end( ), [ symbol ]( GlushkovSymbol const& gs ) -> bool { - return gs.getSymbolPtr( ) == symbol; - } ); + auto it = find_if( symbolSet.begin( ), symbolSet.end( ), [ symbol ]( GlushkovSymbol const& gs ) -> bool { + return gs.getSymbolPtr( ) == symbol; + } ); - if( it == symbolSet.end( ) ) - throw( "GlushkovTraversal - Can not find GlushkovSymbol for regexp node." ); + if( it == symbolSet.end( ) ) + throw( "GlushkovTraversal - Can not find GlushkovSymbol for regexp node." ); - return * it; + return * it; } bool GlushkovTraversal::pos( GlushkovSymbol const& symbol, regexp::UnboundedRegExp const * const & node ) { - return pos( & node->getRegExp(), symbol.getSymbolPtr( ) ); + return pos( & node->getRegExp(), symbol.getSymbolPtr( ) ); } set<GlushkovSymbol> GlushkovTraversal::first( regexp::UnboundedRegExp const& re ) { - set<GlushkovSymbol> firstSet, symbolSet = getSymbols( re ); + set<GlushkovSymbol> firstSet, symbolSet = getSymbols( re ); - for( auto const& s : first( & re.getRegExp() ) ) - firstSet.insert( findSymbol( s, symbolSet ) ); + for( auto const& s : first( & re.getRegExp() ) ) + firstSet.insert( findSymbol( s, symbolSet ) ); - return firstSet; + return firstSet; } set<GlushkovSymbol> GlushkovTraversal::last( regexp::UnboundedRegExp const& re ) { - set<GlushkovSymbol> lastSet, symbolSet = getSymbols( re ); + set<GlushkovSymbol> lastSet, symbolSet = getSymbols( re ); - for( auto const& s : last( & re.getRegExp() ) ) - lastSet.insert( findSymbol( s, symbolSet ) ); + for( auto const& s : last( & re.getRegExp() ) ) + lastSet.insert( findSymbol( s, symbolSet ) ); - return lastSet; + return lastSet; } set<GlushkovSymbol> GlushkovTraversal::follow( regexp::UnboundedRegExp const& re, GlushkovSymbol const& symbol ) { - set<GlushkovSymbol> followSet, symbolSet = getSymbols( re ); + set<GlushkovSymbol> followSet, symbolSet = getSymbols( re ); - for( auto const& s: follow( & re.getRegExp(), symbol.getSymbolPtr( ) ) ) - followSet.insert( findSymbol( s, symbolSet ) ); + for( auto const& s: follow( & re.getRegExp(), symbol.getSymbolPtr( ) ) ) + followSet.insert( findSymbol( s, symbolSet ) ); - return followSet; + return followSet; } // ----------------------------------------------------------------------------- set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::first( regexp::UnboundedRegExpElement const * const & node ) { - regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast<regexp::UnboundedRegExpAlternation const * const>( node ); - regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast<regexp::UnboundedRegExpConcatenation const * const>( node ); - regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast<regexp::UnboundedRegExpIteration const * const>( node ); - regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast<regexp::UnboundedRegExpSymbol const * const>( node ); - regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast<regexp::UnboundedRegExpEmpty const * const>( node ); - regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast<regexp::UnboundedRegExpEpsilon const * const>( node ); - - if( alternation ) - return first( alternation ); - else if( concatenation ) - return first( concatenation ); - else if( iteration ) - return first( iteration ); - else if( eps ) - return first( eps ); - else if( empty ) - return first( empty ); - else if( symbol ) - return first( symbol ); - - throw exception::AlibException( "GlushkovTraversal::first - invalid RegExpElement node" ); + regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast<regexp::UnboundedRegExpAlternation const * const>( node ); + regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast<regexp::UnboundedRegExpConcatenation const * const>( node ); + regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast<regexp::UnboundedRegExpIteration const * const>( node ); + regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast<regexp::UnboundedRegExpSymbol const * const>( node ); + regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast<regexp::UnboundedRegExpEmpty const * const>( node ); + regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast<regexp::UnboundedRegExpEpsilon const * const>( node ); + + if( alternation ) + return first( alternation ); + else if( concatenation ) + return first( concatenation ); + else if( iteration ) + return first( iteration ); + else if( eps ) + return first( eps ); + else if( empty ) + return first( empty ); + else if( symbol ) + return first( symbol ); + + throw exception::AlibException( "GlushkovTraversal::first - invalid RegExpElement node" ); } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::first( regexp::UnboundedRegExpAlternation const * const & node ) { - set<regexp::UnboundedRegExpSymbol const *> ret, tmp; + set<regexp::UnboundedRegExpSymbol const *> ret, tmp; - for( auto const& element : node->getElements( ) ) - { - tmp = first( element ); - ret.insert( tmp.begin( ), tmp.end( ) ); - } + for( auto const& element : node->getElements( ) ) + { + tmp = first( element ); + ret.insert( tmp.begin( ), tmp.end( ) ); + } - return ret; + return ret; } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::first( regexp::UnboundedRegExpConcatenation const * const & node ) { - set<regexp::UnboundedRegExpSymbol const *> ret, tmp; + set<regexp::UnboundedRegExpSymbol const *> ret, tmp; - for( auto const& element : node->getElements( ) ) - { - tmp = first( element ); - ret.insert( tmp.begin( ), tmp.end( ) ); + for( auto const& element : node->getElements( ) ) + { + tmp = first( element ); + ret.insert( tmp.begin( ), tmp.end( ) ); - if(! regexp::properties::RegExpEpsilon::languageContainsEpsilon(*element)) // If regexp of this subtree can match epsilon, then we need to add next subtree - break; - } + if(! regexp::properties::RegExpEpsilon::languageContainsEpsilon(*element)) // If regexp of this subtree can match epsilon, then we need to add next subtree + break; + } - return ret; + return ret; } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::first( regexp::UnboundedRegExpIteration const * const & node ) { - return first( & node->getElement( ) ); + return first( & node->getElement( ) ); } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::first( regexp::UnboundedRegExpSymbol const * const & node ) { - return set<regexp::UnboundedRegExpSymbol const *> { node }; + return set<regexp::UnboundedRegExpSymbol const *> { node }; } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::first( regexp::UnboundedRegExpEpsilon const * const & /* node */ ) { - return set<regexp::UnboundedRegExpSymbol const *>( ); + return set<regexp::UnboundedRegExpSymbol const *>( ); } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::first( regexp::UnboundedRegExpEmpty const * const & /* node */ ) { - return set<regexp::UnboundedRegExpSymbol const *>( ); + return set<regexp::UnboundedRegExpSymbol const *>( ); } // ---------------------------------------------------------------------------- set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::last( regexp::UnboundedRegExpElement const * const & node ) { - regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast<regexp::UnboundedRegExpAlternation const * const>( node ); - regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast<regexp::UnboundedRegExpConcatenation const * const>( node ); - regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast<regexp::UnboundedRegExpIteration const * const>( node ); - regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast<regexp::UnboundedRegExpSymbol const * const>( node ); - regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast<regexp::UnboundedRegExpEmpty const * const>( node ); - regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast<regexp::UnboundedRegExpEpsilon const * const>( node ); - - if( symbol ) - return last( symbol ); - else if( alternation ) - return last( alternation ); - else if( concatenation ) - return last( concatenation ); - else if( iteration ) - return last( iteration ); - else if( eps ) - return last( eps ); - else if( empty ) - return last( empty ); - - throw exception::AlibException( "GlushkovTraversal::last - invalid RegExpElement node" ); + regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast<regexp::UnboundedRegExpAlternation const * const>( node ); + regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast<regexp::UnboundedRegExpConcatenation const * const>( node ); + regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast<regexp::UnboundedRegExpIteration const * const>( node ); + regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast<regexp::UnboundedRegExpSymbol const * const>( node ); + regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast<regexp::UnboundedRegExpEmpty const * const>( node ); + regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast<regexp::UnboundedRegExpEpsilon const * const>( node ); + + if( symbol ) + return last( symbol ); + else if( alternation ) + return last( alternation ); + else if( concatenation ) + return last( concatenation ); + else if( iteration ) + return last( iteration ); + else if( eps ) + return last( eps ); + else if( empty ) + return last( empty ); + + throw exception::AlibException( "GlushkovTraversal::last - invalid RegExpElement node" ); } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::last( regexp::UnboundedRegExpAlternation const * const & node ) { - set<regexp::UnboundedRegExpSymbol const *> ret; + set<regexp::UnboundedRegExpSymbol const *> ret; - for( auto const& element : node->getElements( ) ) - { - set<regexp::UnboundedRegExpSymbol const *> tmp = last( element ); - ret.insert( tmp.begin( ), tmp.end( ) ); - } + for( auto const& element : node->getElements( ) ) + { + set<regexp::UnboundedRegExpSymbol const *> tmp = last( element ); + ret.insert( tmp.begin( ), tmp.end( ) ); + } - return ret; + return ret; } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::last( regexp::UnboundedRegExpConcatenation const * const & node ) { - set<regexp::UnboundedRegExpSymbol const *> ret, tmp; + set<regexp::UnboundedRegExpSymbol const *> ret, tmp; - for( auto it = node->getElements( ).rbegin( ); it != node->getElements( ).rend( ) ; it ++ ) - { - tmp = last( *it ); - ret.insert( tmp.begin( ), tmp.end( ) ); + for( auto it = node->getElements( ).rbegin( ); it != node->getElements( ).rend( ) ; it ++ ) + { + tmp = last( *it ); + ret.insert( tmp.begin( ), tmp.end( ) ); - if( ! regexp::properties::RegExpEpsilon::languageContainsEpsilon(**it) ) - break; - } + if( ! regexp::properties::RegExpEpsilon::languageContainsEpsilon(**it) ) + break; + } - return ret; + return ret; } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::last( regexp::UnboundedRegExpIteration const * const & node ) { - return last( & node->getElement( ) ); + return last( & node->getElement( ) ); } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::last( regexp::UnboundedRegExpSymbol const * const & node ) { - return set<regexp::UnboundedRegExpSymbol const *> { node }; + return set<regexp::UnboundedRegExpSymbol const *> { node }; } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::last( regexp::UnboundedRegExpEpsilon const * const & /* node */ ) { - return set<regexp::UnboundedRegExpSymbol const *>( ); + return set<regexp::UnboundedRegExpSymbol const *>( ); } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::last( regexp::UnboundedRegExpEmpty const * const & /* node */ ) { - return set<regexp::UnboundedRegExpSymbol const *>( ); + return set<regexp::UnboundedRegExpSymbol const *>( ); } // ---------------------------------------------------------------------------- set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::follow( regexp::UnboundedRegExpElement const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { - regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast<regexp::UnboundedRegExpAlternation const * const>( node ); - regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast<regexp::UnboundedRegExpConcatenation const * const>( node ); - regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast<regexp::UnboundedRegExpIteration const * const>( node ); - regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast<regexp::UnboundedRegExpSymbol const * const>( node ); - regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast<regexp::UnboundedRegExpEmpty const * const>( node ); - regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast<regexp::UnboundedRegExpEpsilon const * const>( node ); + regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast<regexp::UnboundedRegExpAlternation const * const>( node ); + regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast<regexp::UnboundedRegExpConcatenation const * const>( node ); + regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast<regexp::UnboundedRegExpIteration const * const>( node ); + regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast<regexp::UnboundedRegExpSymbol const * const>( node ); + regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast<regexp::UnboundedRegExpEmpty const * const>( node ); + regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast<regexp::UnboundedRegExpEpsilon const * const>( node ); - if( alternation ) - return follow( alternation, symbolptr ); + if( alternation ) + return follow( alternation, symbolptr ); - else if( concatenation ) - return follow( concatenation, symbolptr ); + else if( concatenation ) + return follow( concatenation, symbolptr ); - else if( iteration ) - return follow( iteration, symbolptr ); + else if( iteration ) + return follow( iteration, symbolptr ); - else if( symbol ) - return follow( symbol, symbolptr ); + else if( symbol ) + return follow( symbol, symbolptr ); - else if( empty ) - return follow( empty, symbolptr ); + else if( empty ) + return follow( empty, symbolptr ); - else if( eps ) - return follow( eps, symbolptr ); + else if( eps ) + return follow( eps, symbolptr ); - throw exception::AlibException( "GlushkovTraversal::follow() - unknown RegExpElement node" ); + throw exception::AlibException( "GlushkovTraversal::follow() - unknown RegExpElement node" ); } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::follow( regexp::UnboundedRegExpAlternation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { - for( auto const& element : node->getElements( ) ) - if( pos( element, symbolptr ) ) - return follow( element, symbolptr ); + for( auto const& element : node->getElements( ) ) + if( pos( element, symbolptr ) ) + return follow( element, symbolptr ); - throw exception::AlibException( "GlushkovTraversal::follow(Alt)" ); + throw exception::AlibException( "GlushkovTraversal::follow(Alt)" ); } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::follow( regexp::UnboundedRegExpConcatenation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { - set<regexp::UnboundedRegExpSymbol const *> ret, tmp, lastSet; + set<regexp::UnboundedRegExpSymbol const *> ret, tmp, lastSet; - for( auto e = node->getElements( ).begin( ); e != node->getElements( ).end( ); e ++ ) - { - if( ! pos( *e, symbolptr ) ) - continue; + for( auto e = node->getElements( ).begin( ); e != node->getElements( ).end( ); e ++ ) + { + if( ! pos( *e, symbolptr ) ) + continue; - tmp = follow( *e, symbolptr ); - ret.insert( tmp.begin( ), tmp.end( ) ); + tmp = follow( *e, symbolptr ); + ret.insert( tmp.begin( ), tmp.end( ) ); - lastSet = last( *e ); - if( isInSet( symbolptr, lastSet ) ) - { - for( auto f = next( e ); f != node->getElements( ).end( ); f ++ ) - { - tmp = first( *f ); - ret.insert( tmp.begin( ), tmp.end( ) ); + lastSet = last( *e ); + if( isInSet( symbolptr, lastSet ) ) + { + for( auto f = next( e ); f != node->getElements( ).end( ); f ++ ) + { + tmp = first( *f ); + ret.insert( tmp.begin( ), tmp.end( ) ); - if( ! regexp::properties::RegExpEpsilon::languageContainsEpsilon( **f ) ) - break; - } - } - } - return ret; + if( ! regexp::properties::RegExpEpsilon::languageContainsEpsilon( **f ) ) + break; + } + } + } + return ret; } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::follow( regexp::UnboundedRegExpIteration const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { - set<regexp::UnboundedRegExpSymbol const *> ret = follow( & node->getElement( ), symbolptr ); - set<regexp::UnboundedRegExpSymbol const *> lastSet = last( & node->getElement( ) ); + set<regexp::UnboundedRegExpSymbol const *> ret = follow( & node->getElement( ), symbolptr ); + set<regexp::UnboundedRegExpSymbol const *> lastSet = last( & node->getElement( ) ); - if( isInSet( symbolptr, lastSet ) ) - { - set<regexp::UnboundedRegExpSymbol const *> firstSet = first( & node->getElement( ) ); - ret.insert( firstSet.begin( ), firstSet.end( ) ); - } + if( isInSet( symbolptr, lastSet ) ) + { + set<regexp::UnboundedRegExpSymbol const *> firstSet = first( & node->getElement( ) ); + ret.insert( firstSet.begin( ), firstSet.end( ) ); + } - return ret; + return ret; } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::follow( regexp::UnboundedRegExpSymbol const * const & /* node */, regexp::UnboundedRegExpSymbol const * const & /* symbolptr */ ) { - return set<regexp::UnboundedRegExpSymbol const *>( ); + return set<regexp::UnboundedRegExpSymbol const *>( ); } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::follow( regexp::UnboundedRegExpEmpty const * const & /* node */, regexp::UnboundedRegExpSymbol const * const & /* symbolptr */ ) { - return set<regexp::UnboundedRegExpSymbol const *>( ); + return set<regexp::UnboundedRegExpSymbol const *>( ); } set<regexp::UnboundedRegExpSymbol const *> GlushkovTraversal::follow( regexp::UnboundedRegExpEpsilon const * const & /* node */, regexp::UnboundedRegExpSymbol const * const & /* symbolptr */ ) { - return set<regexp::UnboundedRegExpSymbol const *>( ); + return set<regexp::UnboundedRegExpSymbol const *>( ); } // ---------------------------------------------------------------------------- bool GlushkovTraversal::pos( regexp::UnboundedRegExpElement const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { - regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast<regexp::UnboundedRegExpAlternation const * const>( node ); - regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast<regexp::UnboundedRegExpConcatenation const * const>( node ); - regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast<regexp::UnboundedRegExpIteration const * const>( node ); - regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast<regexp::UnboundedRegExpSymbol const * const>( node ); - regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast<regexp::UnboundedRegExpEmpty const * const>( node ); - regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast<regexp::UnboundedRegExpEpsilon const * const>( node ); + regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast<regexp::UnboundedRegExpAlternation const * const>( node ); + regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast<regexp::UnboundedRegExpConcatenation const * const>( node ); + regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast<regexp::UnboundedRegExpIteration const * const>( node ); + regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast<regexp::UnboundedRegExpSymbol const * const>( node ); + regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast<regexp::UnboundedRegExpEmpty const * const>( node ); + regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast<regexp::UnboundedRegExpEpsilon const * const>( node ); - if( alternation ) - return pos( alternation, symbolptr ); + if( alternation ) + return pos( alternation, symbolptr ); - else if( concatenation ) - return pos( concatenation, symbolptr ); + else if( concatenation ) + return pos( concatenation, symbolptr ); - else if( iteration ) - return pos( iteration, symbolptr ); + else if( iteration ) + return pos( iteration, symbolptr ); - else if( symbol ) - return pos( symbol, symbolptr ); + else if( symbol ) + return pos( symbol, symbolptr ); - else if( empty ) - return pos( empty, symbolptr ); + else if( empty ) + return pos( empty, symbolptr ); - else if( eps ) - return pos( eps, symbolptr ); + else if( eps ) + return pos( eps, symbolptr ); - throw exception::AlibException( "GlushkovTraversal::pos() - unknown RegExpElement node" ); + throw exception::AlibException( "GlushkovTraversal::pos() - unknown RegExpElement node" ); } bool GlushkovTraversal::pos( regexp::UnboundedRegExpAlternation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { - for( auto const& element : node->getElements( ) ) - if( pos( element, symbolptr ) ) - return true; - return false; + for( auto const& element : node->getElements( ) ) + if( pos( element, symbolptr ) ) + return true; + return false; } bool GlushkovTraversal::pos( regexp::UnboundedRegExpConcatenation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { - for( auto const& element : node->getElements( ) ) - if( pos( element, symbolptr ) ) - return true; - return false; + for( auto const& element : node->getElements( ) ) + if( pos( element, symbolptr ) ) + return true; + return false; } bool GlushkovTraversal::pos( regexp::UnboundedRegExpIteration const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { - return pos( & node->getElement( ), symbolptr ); + return pos( & node->getElement( ), symbolptr ); } bool GlushkovTraversal::pos( regexp::UnboundedRegExpSymbol const * const & node, regexp::UnboundedRegExpSymbol const * const & symbolptr ) { - return symbolptr == node; + return symbolptr == node; } bool GlushkovTraversal::pos( regexp::UnboundedRegExpEmpty const * const & /* node */, regexp::UnboundedRegExpSymbol const * const & /* symbolptr */ ) { - return false; + return false; } bool GlushkovTraversal::pos( regexp::UnboundedRegExpEpsilon const * const & /* node */, regexp::UnboundedRegExpSymbol const * const & /* symbolptr */ ) { - return false; + return false; } // ---------------------------------------------------------------------------- set<GlushkovSymbol> GlushkovTraversal::getSymbols( regexp::UnboundedRegExp const& re ) { - set<GlushkovSymbol> alphabet; - int i = 1; + set<GlushkovSymbol> alphabet; + int i = 1; - getSymbols( & re.getRegExp( ), alphabet, i ); + getSymbols( & re.getRegExp( ), alphabet, i ); - return alphabet; + return alphabet; } void GlushkovTraversal::getSymbols( regexp::UnboundedRegExpElement const * const & node, set<GlushkovSymbol> & alphabet, int & i ) { - regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast<regexp::UnboundedRegExpAlternation const * const>( node ); - regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast<regexp::UnboundedRegExpConcatenation const * const>( node ); - regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast<regexp::UnboundedRegExpIteration const * const>( node ); - regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast<regexp::UnboundedRegExpSymbol const * const>( node ); - regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast<regexp::UnboundedRegExpEmpty const * const>( node ); - regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast<regexp::UnboundedRegExpEpsilon const * const>( node ); - - if( symbol ) - { - alphabet.insert( GlushkovSymbol( symbol, i ++ ) ); - return; - } - - else if( alternation ) - { - for( const auto & element : alternation->getElements( ) ) - getSymbols( element, alphabet, i ); - return; - } - - else if( concatenation ) - { - for( const auto & element : concatenation->getElements( ) ) - getSymbols( element, alphabet, i ); - return; - } - - else if( iteration ) - { - getSymbols( & iteration->getElement( ), alphabet, i ); - return; - } - - else if( empty ) - return; - - else if( eps ) - return; - - throw exception::AlibException( "GlushkovTraversal::getSymbols() - unknown RegExpElement node" ); + regexp::UnboundedRegExpAlternation const * const alternation = dynamic_cast<regexp::UnboundedRegExpAlternation const * const>( node ); + regexp::UnboundedRegExpConcatenation const * const concatenation = dynamic_cast<regexp::UnboundedRegExpConcatenation const * const>( node ); + regexp::UnboundedRegExpIteration const * const iteration = dynamic_cast<regexp::UnboundedRegExpIteration const * const>( node ); + regexp::UnboundedRegExpSymbol const * const symbol = dynamic_cast<regexp::UnboundedRegExpSymbol const * const>( node ); + regexp::UnboundedRegExpEmpty const * const empty = dynamic_cast<regexp::UnboundedRegExpEmpty const * const>( node ); + regexp::UnboundedRegExpEpsilon const * const eps = dynamic_cast<regexp::UnboundedRegExpEpsilon const * const>( node ); + + if( symbol ) + { + alphabet.insert( GlushkovSymbol( symbol, i ++ ) ); + return; + } + + else if( alternation ) + { + for( const auto & element : alternation->getElements( ) ) + getSymbols( element, alphabet, i ); + return; + } + + else if( concatenation ) + { + for( const auto & element : concatenation->getElements( ) ) + getSymbols( element, alphabet, i ); + return; + } + + else if( iteration ) + { + getSymbols( & iteration->getElement( ), alphabet, i ); + return; + } + + else if( empty ) + return; + + else if( eps ) + return; + + throw exception::AlibException( "GlushkovTraversal::getSymbols() - unknown RegExpElement node" ); } } /* namespace conversions */ diff --git a/alib2algo/src/regexp/GlushkovTraversal.h b/alib2algo/src/regexp/GlushkovTraversal.h index 7a5a9114121307ecba411ba30a6b4f80e1d0b890..b685e2152519d155f6f7be13d24940ee45dd25a9 100644 --- a/alib2algo/src/regexp/GlushkovTraversal.h +++ b/alib2algo/src/regexp/GlushkovTraversal.h @@ -2,7 +2,7 @@ * GlushkovTraversal.h * * Created on: 13. 3. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #ifndef GLUSHKOVTRAVERSAL_H_ @@ -38,77 +38,77 @@ namespace regexp class GlushkovTraversal { public: - /** - * @param re RegExp to probe - * @return all RegExpSymbols whichcan start the word. - */ - static std::set<GlushkovSymbol> first( regexp::UnboundedRegExp const& re ); - - /** - * @param re RegExp to probe - * @return all RegExpSymbols that can terminate the word. - */ - static std::set<GlushkovSymbol> last( regexp::UnboundedRegExp const& re ); - - /** - * @param re RegExp to probe - * @param symbol GlushkovSymbol for which we need the follow() - * @return all symbols that can follow specific symbol in word - */ - static std::set<GlushkovSymbol> follow( regexp::UnboundedRegExp const& re, GlushkovSymbol const& symbol ); - - /** - * @param re RegExp to probe - * @return symbols of regexp tree in order of they occurence in regexp. - */ - static std::set<GlushkovSymbol> getSymbols( regexp::UnboundedRegExp const& re ); + /** + * @param re RegExp to probe + * @return all RegExpSymbols whichcan start the word. + */ + static std::set<GlushkovSymbol> first( regexp::UnboundedRegExp const& re ); + + /** + * @param re RegExp to probe + * @return all RegExpSymbols that can terminate the word. + */ + static std::set<GlushkovSymbol> last( regexp::UnboundedRegExp const& re ); + + /** + * @param re RegExp to probe + * @param symbol GlushkovSymbol for which we need the follow() + * @return all symbols that can follow specific symbol in word + */ + static std::set<GlushkovSymbol> follow( regexp::UnboundedRegExp const& re, GlushkovSymbol const& symbol ); + + /** + * @param re RegExp to probe + * @return symbols of regexp tree in order of they occurence in regexp. + */ + static std::set<GlushkovSymbol> getSymbols( regexp::UnboundedRegExp const& re ); private: - /** - * @param symbol ptr to symbol - * @param symbolSet set of gl.symbols - * @return GlushkovSymbol equivalent for RegExpSymbol pointer - */ - static GlushkovSymbol const& findSymbol( regexp::UnboundedRegExpSymbol const * const symbol, const std::set<GlushkovSymbol> & symbolSet ); - - /** - * @return bool true if symbol pointer is in this subtree - */ - static bool pos( GlushkovSymbol const & symbol, regexp::UnboundedRegExp const * const & node ); - - static void getSymbols( regexp::UnboundedRegExpElement const * const & node, std::set<GlushkovSymbol> & alphabet, int & i ); - - static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpElement const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpAlternation const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpConcatenation const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpIteration const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpSymbol const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpEmpty const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpEpsilon const * const & node ); - - static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpElement const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpAlternation const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpConcatenation const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpIteration const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpSymbol const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpEmpty const * const & node ); - static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpEpsilon const * const & node ); - - static bool pos( regexp::UnboundedRegExpElement const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); - static bool pos( regexp::UnboundedRegExpAlternation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); - static bool pos( regexp::UnboundedRegExpConcatenation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); - static bool pos( regexp::UnboundedRegExpIteration const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); - static bool pos( regexp::UnboundedRegExpSymbol const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); - static bool pos( regexp::UnboundedRegExpEmpty const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); - static bool pos( regexp::UnboundedRegExpEpsilon const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); - - static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpElement const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); - static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpAlternation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); - static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpConcatenation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); - static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpIteration const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); - static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpSymbol const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); - static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpEmpty const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); - static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpEpsilon const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); + /** + * @param symbol ptr to symbol + * @param symbolSet set of gl.symbols + * @return GlushkovSymbol equivalent for RegExpSymbol pointer + */ + static GlushkovSymbol const& findSymbol( regexp::UnboundedRegExpSymbol const * const symbol, const std::set<GlushkovSymbol> & symbolSet ); + + /** + * @return bool true if symbol pointer is in this subtree + */ + static bool pos( GlushkovSymbol const & symbol, regexp::UnboundedRegExp const * const & node ); + + static void getSymbols( regexp::UnboundedRegExpElement const * const & node, std::set<GlushkovSymbol> & alphabet, int & i ); + + static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpElement const * const & node ); + static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpAlternation const * const & node ); + static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpConcatenation const * const & node ); + static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpIteration const * const & node ); + static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpSymbol const * const & node ); + static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpEmpty const * const & node ); + static std::set<regexp::UnboundedRegExpSymbol const *> first( regexp::UnboundedRegExpEpsilon const * const & node ); + + static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpElement const * const & node ); + static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpAlternation const * const & node ); + static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpConcatenation const * const & node ); + static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpIteration const * const & node ); + static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpSymbol const * const & node ); + static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpEmpty const * const & node ); + static std::set<regexp::UnboundedRegExpSymbol const *> last( regexp::UnboundedRegExpEpsilon const * const & node ); + + static bool pos( regexp::UnboundedRegExpElement const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); + static bool pos( regexp::UnboundedRegExpAlternation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); + static bool pos( regexp::UnboundedRegExpConcatenation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); + static bool pos( regexp::UnboundedRegExpIteration const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); + static bool pos( regexp::UnboundedRegExpSymbol const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); + static bool pos( regexp::UnboundedRegExpEmpty const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); + static bool pos( regexp::UnboundedRegExpEpsilon const * const & node, regexp::UnboundedRegExpSymbol const * const & symbSearch ); + + static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpElement const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); + static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpAlternation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); + static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpConcatenation const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); + static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpIteration const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); + static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpSymbol const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); + static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpEmpty const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); + static std::set<regexp::UnboundedRegExpSymbol const *> follow( regexp::UnboundedRegExpEpsilon const * const & node, regexp::UnboundedRegExpSymbol const * const & symbFollow ); }; } /* namespace conversions */ diff --git a/alib2algo/src/regexp/convert/ToGrammarRightRGGlushkov.cpp b/alib2algo/src/regexp/convert/ToGrammarRightRGGlushkov.cpp index cb1ba5388b18391895ef03502df76fa223757992..7821ef3a9e207001ec7e0f1462a49a6a03291308 100644 --- a/alib2algo/src/regexp/convert/ToGrammarRightRGGlushkov.cpp +++ b/alib2algo/src/regexp/convert/ToGrammarRightRGGlushkov.cpp @@ -2,7 +2,7 @@ * ToGrammarRightRGGlushkov.cpp * * Created on: 11. 1. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #include "ToGrammarRightRGGlushkov.h" @@ -25,91 +25,91 @@ namespace convert { grammar::Grammar ToGrammarRightRGGlushkov::convert(const regexp::RegExp& regexp) { - grammar::Grammar* out = NULL; - regexp.getData().Accept((void*) &out, ToGrammarRightRGGlushkov::TO_GRAMMAR_RIGHT_RG_GLUSHKOV); - grammar::Grammar res = std::move(*out); - delete out; - return res; + grammar::Grammar* out = NULL; + regexp.getData().Accept((void*) &out, ToGrammarRightRGGlushkov::TO_GRAMMAR_RIGHT_RG_GLUSHKOV); + grammar::Grammar res = std::move(*out); + delete out; + return res; } grammar::RightRG ToGrammarRightRGGlushkov::convert(const regexp::UnboundedRegExp& regexp) { - alphabet::Symbol S = alphabet::symbolFrom("S"); - grammar::RightRG grammar(S); - - // step 1 - grammar.setTerminalAlphabet(regexp.getAlphabet()); - - // steps 2, 3, 4 - std::set<regexp::GlushkovPair> pairs; - const std::set<regexp::GlushkovSymbol> first = regexp::GlushkovTraversal::first(regexp); - const std::set<regexp::GlushkovSymbol> last = regexp::GlushkovTraversal::last(regexp); - for(const auto& x : regexp::GlushkovTraversal::getSymbols(regexp)) - for(const auto& f : regexp::GlushkovTraversal::follow(regexp, x)) - pairs.insert(regexp::GlushkovPair(x, f)); - // \e in q0 check is in step 7 - - // step 5 - std::map<regexp::GlushkovSymbol, alphabet::Symbol> symbolMap; - - for(const auto& symbol : regexp::GlushkovTraversal::getSymbols(regexp)) - { - alphabet::Symbol nt(alphabet::LabeledSymbol(label::Label(label::LabelPairLabel(std::make_pair(label::labelFrom(symbol.getInputSymbol()), label::labelFrom(symbol.getId())))))); - symbolMap.insert(std::make_pair(symbol, nt)); - grammar.addNonterminalSymbol(nt); - } - - // step 6 - for(const auto& symbol : first) - grammar.addRule(S, std::make_pair(symbol.getInputSymbol(), symbolMap.find(symbol)->second)); - - for(const auto& pair : pairs) - { - const alphabet::Symbol& a = symbolMap.find(pair.getFirst())->second; - const alphabet::Symbol& b = symbolMap.find(pair.getSecond())->second; - grammar.addRule(a, std::make_pair(pair.getSecond().getInputSymbol(), b)); - } - - // step 7 - for(const auto& symbol : last) - { - /* - * for all rules where ns.m_nonTerminal is on rightSide: - * add Rule: leftSide -> symbol.getSymbol( ) - * unless it already exists - */ - - const alphabet::Symbol& a = symbolMap.find(symbol)->second; - - for(const auto& rule : grammar.getRawRules()) - for(const auto& rhs : rule.second) - if(std::find(rhs.begin(), rhs.end(), a) != rhs.end()) - grammar.addRule(rule.first, rhs.at(0)); - } - - if(regexp::properties::RegExpEpsilon::languageContainsEpsilon(regexp)) - grammar.setGeneratesEpsilon(true); - - return grammar; + alphabet::Symbol S = alphabet::symbolFrom("S"); + grammar::RightRG grammar(S); + + // step 1 + grammar.setTerminalAlphabet(regexp.getAlphabet()); + + // steps 2, 3, 4 + std::set<regexp::GlushkovPair> pairs; + const std::set<regexp::GlushkovSymbol> first = regexp::GlushkovTraversal::first(regexp); + const std::set<regexp::GlushkovSymbol> last = regexp::GlushkovTraversal::last(regexp); + for(const auto& x : regexp::GlushkovTraversal::getSymbols(regexp)) + for(const auto& f : regexp::GlushkovTraversal::follow(regexp, x)) + pairs.insert(regexp::GlushkovPair(x, f)); + // \e in q0 check is in step 7 + + // step 5 + std::map<regexp::GlushkovSymbol, alphabet::Symbol> symbolMap; + + for(const auto& symbol : regexp::GlushkovTraversal::getSymbols(regexp)) + { + alphabet::Symbol nt(alphabet::LabeledSymbol(label::Label(label::LabelPairLabel(std::make_pair(label::labelFrom(symbol.getInputSymbol()), label::labelFrom(symbol.getId())))))); + symbolMap.insert(std::make_pair(symbol, nt)); + grammar.addNonterminalSymbol(nt); + } + + // step 6 + for(const auto& symbol : first) + grammar.addRule(S, std::make_pair(symbol.getInputSymbol(), symbolMap.find(symbol)->second)); + + for(const auto& pair : pairs) + { + const alphabet::Symbol& a = symbolMap.find(pair.getFirst())->second; + const alphabet::Symbol& b = symbolMap.find(pair.getSecond())->second; + grammar.addRule(a, std::make_pair(pair.getSecond().getInputSymbol(), b)); + } + + // step 7 + for(const auto& symbol : last) + { + /* + * for all rules where ns.m_nonTerminal is on rightSide: + * add Rule: leftSide -> symbol.getSymbol( ) + * unless it already exists + */ + + const alphabet::Symbol& a = symbolMap.find(symbol)->second; + + for(const auto& rule : grammar.getRawRules()) + for(const auto& rhs : rule.second) + if(std::find(rhs.begin(), rhs.end(), a) != rhs.end()) + grammar.addRule(rule.first, rhs.at(0)); + } + + if(regexp::properties::RegExpEpsilon::languageContainsEpsilon(regexp)) + grammar.setGeneratesEpsilon(true); + + return grammar; } grammar::RightRG ToGrammarRightRGGlushkov::convert(const regexp::FormalRegExp& regexp) { - throw exception::AlibException("Glushkov: Converting FormalRegExp NYI"); // TODO + throw exception::AlibException("Glushkov: Converting FormalRegExp NYI"); // TODO } void ToGrammarRightRGGlushkov::Visit(void* userData, const regexp::FormalRegExp& regexp) const { - /* - grammar::Grammar* & out = *((grammar::Grammar**) userData); - out = new grammar::Grammar(this->convert(regexp)); - */ - throw exception::AlibException("Glushkov: Converting FormalRegExp NYI"); // TODO + /* + grammar::Grammar* & out = *((grammar::Grammar**) userData); + out = new grammar::Grammar(this->convert(regexp)); + */ + throw exception::AlibException("Glushkov: Converting FormalRegExp NYI"); // TODO } void ToGrammarRightRGGlushkov::Visit(void* userData, const regexp::UnboundedRegExp& regexp) const { - grammar::Grammar* & out = *((grammar::Grammar**) userData); - out = new grammar::Grammar(this->convert(regexp)); + grammar::Grammar* & out = *((grammar::Grammar**) userData); + out = new grammar::Grammar(this->convert(regexp)); } const ToGrammarRightRGGlushkov ToGrammarRightRGGlushkov::TO_GRAMMAR_RIGHT_RG_GLUSHKOV; diff --git a/alib2algo/src/regexp/convert/ToGrammarRightRGGlushkov.h b/alib2algo/src/regexp/convert/ToGrammarRightRGGlushkov.h index 42a9cb9e82f76a6c94e63c5cd75a9763672e1957..7eec5a03077006c6350897ade7557ff6032b394e 100644 --- a/alib2algo/src/regexp/convert/ToGrammarRightRGGlushkov.h +++ b/alib2algo/src/regexp/convert/ToGrammarRightRGGlushkov.h @@ -2,7 +2,7 @@ * ToGrammarRightRGGlushkov.h * * Created on: 11. 1. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #ifndef TO_GRAMMAR_RIGHT_RG_GLUSHKOV_H_ @@ -26,21 +26,21 @@ namespace convert { class ToGrammarRightRGGlushkov : public regexp::VisitableRegExpBase::const_visitor_type { public: - /** - * Performs conversion. - * @param regexp original regular expression - * @return right regular grammar equivalent to source regexp. - */ - static grammar::Grammar convert(const regexp::RegExp& regexp); + /** + * Performs conversion. + * @param regexp original regular expression + * @return right regular grammar equivalent to source regexp. + */ + static grammar::Grammar convert(const regexp::RegExp& regexp); - static grammar::RightRG convert(const regexp::FormalRegExp& regexp); - static grammar::RightRG convert(const regexp::UnboundedRegExp& regexp); + static grammar::RightRG convert(const regexp::FormalRegExp& regexp); + static grammar::RightRG convert(const regexp::UnboundedRegExp& regexp); private: - void Visit(void*, const regexp::FormalRegExp& regexp) const; - void Visit(void*, const regexp::UnboundedRegExp& regexp) const; + void Visit(void*, const regexp::FormalRegExp& regexp) const; + void Visit(void*, const regexp::UnboundedRegExp& regexp) const; - static const ToGrammarRightRGGlushkov TO_GRAMMAR_RIGHT_RG_GLUSHKOV; + static const ToGrammarRightRGGlushkov TO_GRAMMAR_RIGHT_RG_GLUSHKOV; }; } /* namespace convert */ diff --git a/alib2algo/src/regexp/properties/RegExpEpsilon.cpp b/alib2algo/src/regexp/properties/RegExpEpsilon.cpp index 58c9ed6497bf7235d1c71695143cf1ab1004481f..946a5f6c268a9123c28e34d1c5904d12522f9bca 100644 --- a/alib2algo/src/regexp/properties/RegExpEpsilon.cpp +++ b/alib2algo/src/regexp/properties/RegExpEpsilon.cpp @@ -2,7 +2,7 @@ * RegExpEpsilon.cpp * * Created on: 19. 1. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #include "RegExpEpsilon.h" @@ -13,9 +13,9 @@ namespace properties { bool RegExpEpsilon::languageContainsEpsilon(const regexp::RegExp& regexp) { - bool out; - regexp.getData().Accept((void*) &out, RegExpEpsilon::REG_EXP_EPSILON); - return out; + bool out; + regexp.getData().Accept((void*) &out, RegExpEpsilon::REG_EXP_EPSILON); + return out; } bool RegExpEpsilon::languageContainsEpsilon(const regexp::FormalRegExp& regexp) @@ -163,14 +163,14 @@ void RegExpEpsilon::Visit(void* data, const regexp::FormalRegExpEpsilon&) const void RegExpEpsilon::Visit(void* data, const regexp::FormalRegExp& regexp) const { - bool &ret = *(bool*) data; - ret = RegExpEpsilon::REG_EXP_EPSILON.languageContainsEpsilon(regexp); + bool &ret = *(bool*) data; + ret = RegExpEpsilon::REG_EXP_EPSILON.languageContainsEpsilon(regexp); } void RegExpEpsilon::Visit(void* data, const regexp::UnboundedRegExp& regexp) const { - bool &ret = *(bool*) data; - ret = RegExpEpsilon::REG_EXP_EPSILON.languageContainsEpsilon(regexp); + bool &ret = *(bool*) data; + ret = RegExpEpsilon::REG_EXP_EPSILON.languageContainsEpsilon(regexp); } const RegExpEpsilon RegExpEpsilon::REG_EXP_EPSILON; diff --git a/alib2algo/src/regexp/properties/RegExpEpsilon.h b/alib2algo/src/regexp/properties/RegExpEpsilon.h index 58d2518919684da699a1d26048048ac3d0e72198..c5ba4793b8a0188d0cc6a136df2b1f4e89d598fa 100644 --- a/alib2algo/src/regexp/properties/RegExpEpsilon.h +++ b/alib2algo/src/regexp/properties/RegExpEpsilon.h @@ -24,10 +24,10 @@ namespace properties { class RegExpEpsilon : public regexp::VisitableRegExpBase::const_visitor_type, regexp::FormalRegExpElement::const_visitor_type, regexp::UnboundedRegExpElement::const_visitor_type { public: - static bool languageContainsEpsilon(const regexp::RegExp& regexp); + static bool languageContainsEpsilon(const regexp::RegExp& regexp); - static bool languageContainsEpsilon(const regexp::FormalRegExp& regexp); - static bool languageContainsEpsilon(const regexp::UnboundedRegExp& regexp); + static bool languageContainsEpsilon(const regexp::FormalRegExp& regexp); + static bool languageContainsEpsilon(const regexp::UnboundedRegExp& regexp); static bool languageContainsEpsilon(const regexp::UnboundedRegExpElement& element); static bool languageContainsEpsilon(const regexp::FormalRegExpElement& element); @@ -51,7 +51,7 @@ private: void Visit(void* data, const regexp::FormalRegExpEpsilon& epsilon) const; - static const RegExpEpsilon REG_EXP_EPSILON; + static const RegExpEpsilon REG_EXP_EPSILON; }; } /* namespace properties */ diff --git a/alib2algo/src/regexp/simplify/RegExpOptimize.cpp b/alib2algo/src/regexp/simplify/RegExpOptimize.cpp index e3c630f1a4863b676e1ce32f9d2235e4939ae32a..e9f614588e5cbede5cbf297705f16370bb6fef83 100644 --- a/alib2algo/src/regexp/simplify/RegExpOptimize.cpp +++ b/alib2algo/src/regexp/simplify/RegExpOptimize.cpp @@ -19,25 +19,25 @@ namespace simplify { regexp::RegExp RegExpOptimize::optimize(const regexp::RegExp& regexp) { - regexp::RegExp * out = NULL; - regexp.getData().Accept((void*) &out, RegExpOptimize::REG_EXP_OPTIMIZE); - regexp::RegExp res( std::move( * out ) ); - delete out; - return res; + regexp::RegExp * out = NULL; + regexp.getData().Accept((void*) &out, RegExpOptimize::REG_EXP_OPTIMIZE); + regexp::RegExp res( std::move( * out ) ); + delete out; + return res; } void RegExpOptimize::Visit(void* userData, const regexp::FormalRegExp& regexp) const { - regexp::RegExp * &ret = *(regexp::RegExp **) userData; + regexp::RegExp * &ret = *(regexp::RegExp **) userData; - ret = new regexp::RegExp( optimize( regexp ) ); + ret = new regexp::RegExp( optimize( regexp ) ); } void RegExpOptimize::Visit(void* userData, const regexp::UnboundedRegExp& regexp) const { - regexp::RegExp * &ret = *(regexp::RegExp **) userData; + regexp::RegExp * &ret = *(regexp::RegExp **) userData; - ret = new regexp::RegExp( optimize( regexp ) ); + ret = new regexp::RegExp( optimize( regexp ) ); } FormalRegExp RegExpOptimize::optimize( FormalRegExp const & regexp ) diff --git a/alib2algo/src/regexp/simplify/RegExpOptimize.h b/alib2algo/src/regexp/simplify/RegExpOptimize.h index f0d7ba78c0fb52a2045b8ac01daa2f1f07b95c5d..3c1b3d2182983bd0a58b451e2af931c91afe1dda 100644 --- a/alib2algo/src/regexp/simplify/RegExpOptimize.h +++ b/alib2algo/src/regexp/simplify/RegExpOptimize.h @@ -2,7 +2,7 @@ * RegExpOptimize.h * * Created on: 20. 1. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #ifndef REGEXPOPTIMIZE_H_ @@ -32,9 +32,9 @@ namespace simplify { * All methods return new tree. * * List of optimization on nodes: - * - RegExpAlternation: A1, A2, A3, A4, A9, V2, V5, V6 - * - RegExpConcatenation: A5, A6, A7, A8, V8, V9 - * - RegExpIteration: A10, V1, V3, V4, V10 + * - RegExpAlternation: A1, A2, A3, A4, A9, V2, V5, V6 + * - RegExpConcatenation: A5, A6, A7, A8, V8, V9 + * - RegExpIteration: A10, V1, V3, V4, V10 * * Details: ( id : direction of optim. : optim ) * - A1 : -> : x + ( y + z ) = ( x + y ) + z = x + y + z @@ -54,7 +54,7 @@ namespace simplify { * - V4 : <- : ( x + y )* = (x*y*)* * - V5 : <- : x*y = y + x*xy * - V6 : <- : x*y = y + xx*y - * - V7 : : bleh + * - V7 : : bleh * - V8 : -> : if \e in h(x) => xx* = x* * - V9 : -> : (xy)*x = x(yx)* * - V10: <- : ( x + y )* = ( x* + y* )* @@ -64,76 +64,76 @@ namespace simplify { class RegExpOptimize : public regexp::VisitableRegExpBase::const_visitor_type { public: - static regexp::RegExp optimize( const regexp::RegExp & regexp ); + static regexp::RegExp optimize( const regexp::RegExp & regexp ); - static regexp::UnboundedRegExp optimize( const regexp::UnboundedRegExp & regexp ); - static void optimize( regexp::UnboundedRegExpElement & regexp ); + static regexp::UnboundedRegExp optimize( const regexp::UnboundedRegExp & regexp ); + static void optimize( regexp::UnboundedRegExpElement & regexp ); - static regexp::FormalRegExp optimize( const regexp::FormalRegExp & regexp ); - static void optimize( regexp::FormalRegExpElement & regexp ); + static regexp::FormalRegExp optimize( const regexp::FormalRegExp & regexp ); + static void optimize( regexp::FormalRegExpElement & regexp ); private: - regexp::FormalRegExpElement * optimize( regexp::FormalRegExpElement const * const & node ) const; + regexp::FormalRegExpElement * optimize( regexp::FormalRegExpElement const * const & node ) const; - regexp::UnboundedRegExpElement * optimize( regexp::UnboundedRegExpElement const * const & node ) const; - regexp::UnboundedRegExpElement * optimize( regexp::UnboundedRegExpAlternation const * const & node ) const; - regexp::UnboundedRegExpElement * optimize( regexp::UnboundedRegExpConcatenation const * const & node ) const; - regexp::UnboundedRegExpElement * optimize( regexp::UnboundedRegExpIteration const * const & node ) const; - regexp::UnboundedRegExpElement * optimize( regexp::UnboundedRegExpSymbol const * const & node ) const; - regexp::UnboundedRegExpElement * optimize( regexp::UnboundedRegExpEpsilon const * const & node ) const; - regexp::UnboundedRegExpElement * optimize( regexp::UnboundedRegExpEmpty const * const & node ) const; + regexp::UnboundedRegExpElement * optimize( regexp::UnboundedRegExpElement const * const & node ) const; + regexp::UnboundedRegExpElement * optimize( regexp::UnboundedRegExpAlternation const * const & node ) const; + regexp::UnboundedRegExpElement * optimize( regexp::UnboundedRegExpConcatenation const * const & node ) const; + regexp::UnboundedRegExpElement * optimize( regexp::UnboundedRegExpIteration const * const & node ) const; + regexp::UnboundedRegExpElement * optimize( regexp::UnboundedRegExpSymbol const * const & node ) const; + regexp::UnboundedRegExpElement * optimize( regexp::UnboundedRegExpEpsilon const * const & node ) const; + regexp::UnboundedRegExpElement * optimize( regexp::UnboundedRegExpEmpty const * const & node ) const; - void Visit(void*, const regexp::UnboundedRegExp& regexp) const; - void Visit(void*, const regexp::FormalRegExp& regexp) const; + void Visit(void*, const regexp::UnboundedRegExp& regexp) const; + void Visit(void*, const regexp::FormalRegExp& regexp) const; private: - bool A1( regexp::UnboundedRegExpAlternation * const & node ) const; - bool A2( regexp::UnboundedRegExpAlternation * const & node ) const; - bool A3( regexp::UnboundedRegExpAlternation * const & node ) const; - bool A4( regexp::UnboundedRegExpAlternation * const & node ) const; - bool A5( regexp::UnboundedRegExpConcatenation * const & node ) const; - bool A6( regexp::UnboundedRegExpConcatenation * const & node ) const; - bool A7( regexp::UnboundedRegExpConcatenation * const & node ) const; - bool A8( regexp::UnboundedRegExpConcatenation * const & node ) const; - bool A9( regexp::UnboundedRegExpConcatenation * const & node ) const; - bool A10( regexp::UnboundedRegExpAlternation * const & node ) const; - bool A11( regexp::UnboundedRegExpIteration * const & node ) const; - bool V1( regexp::UnboundedRegExpIteration * const & node ) const; - bool V2( regexp::UnboundedRegExpAlternation * const & node ) const; - bool V3( regexp::UnboundedRegExpIteration * const & node ) const; - bool V4( regexp::UnboundedRegExpIteration * const & node ) const; - bool V5( regexp::UnboundedRegExpAlternation * const & node ) const; - bool V6( regexp::UnboundedRegExpAlternation * const & node ) const; - bool V8( regexp::UnboundedRegExpConcatenation * const & node ) const; - bool V9( regexp::UnboundedRegExpConcatenation * const & node ) const; - bool V10( regexp::UnboundedRegExpIteration * const & node ) const; - - bool X1( regexp::UnboundedRegExpAlternation * const & node ) const; - - bool S( regexp::FormalRegExpElement * & node ) const; - bool A1( regexp::FormalRegExpElement * & node ) const; - bool A2( regexp::FormalRegExpElement * & node ) const; - bool A3( regexp::FormalRegExpElement * & node ) const; - bool A4( regexp::FormalRegExpElement * & node ) const; - bool A5( regexp::FormalRegExpElement * & node ) const; - bool A6( regexp::FormalRegExpElement * & node ) const; - bool A7( regexp::FormalRegExpElement * & node ) const; - bool A8( regexp::FormalRegExpElement * & node ) const; - bool A9( regexp::FormalRegExpElement * & node ) const; - bool A10( regexp::FormalRegExpElement * & node ) const; - bool A11( regexp::FormalRegExpElement * & node ) const; - bool V1( regexp::FormalRegExpElement * & node ) const; - bool V2( regexp::FormalRegExpElement * & node ) const; - bool V3( regexp::FormalRegExpElement * & node ) const; - bool V4( regexp::FormalRegExpElement * & node ) const; - bool V5( regexp::FormalRegExpElement * & node ) const; - bool V6( regexp::FormalRegExpElement * & node ) const; - bool V8( regexp::FormalRegExpElement * & node ) const; - bool V9( regexp::FormalRegExpElement * & node ) const; - bool V10( regexp::FormalRegExpElement * & node ) const; - - bool X1( regexp::FormalRegExpElement * & node ) const; - - static const RegExpOptimize REG_EXP_OPTIMIZE; + bool A1( regexp::UnboundedRegExpAlternation * const & node ) const; + bool A2( regexp::UnboundedRegExpAlternation * const & node ) const; + bool A3( regexp::UnboundedRegExpAlternation * const & node ) const; + bool A4( regexp::UnboundedRegExpAlternation * const & node ) const; + bool A5( regexp::UnboundedRegExpConcatenation * const & node ) const; + bool A6( regexp::UnboundedRegExpConcatenation * const & node ) const; + bool A7( regexp::UnboundedRegExpConcatenation * const & node ) const; + bool A8( regexp::UnboundedRegExpConcatenation * const & node ) const; + bool A9( regexp::UnboundedRegExpConcatenation * const & node ) const; + bool A10( regexp::UnboundedRegExpAlternation * const & node ) const; + bool A11( regexp::UnboundedRegExpIteration * const & node ) const; + bool V1( regexp::UnboundedRegExpIteration * const & node ) const; + bool V2( regexp::UnboundedRegExpAlternation * const & node ) const; + bool V3( regexp::UnboundedRegExpIteration * const & node ) const; + bool V4( regexp::UnboundedRegExpIteration * const & node ) const; + bool V5( regexp::UnboundedRegExpAlternation * const & node ) const; + bool V6( regexp::UnboundedRegExpAlternation * const & node ) const; + bool V8( regexp::UnboundedRegExpConcatenation * const & node ) const; + bool V9( regexp::UnboundedRegExpConcatenation * const & node ) const; + bool V10( regexp::UnboundedRegExpIteration * const & node ) const; + + bool X1( regexp::UnboundedRegExpAlternation * const & node ) const; + + bool S( regexp::FormalRegExpElement * & node ) const; + bool A1( regexp::FormalRegExpElement * & node ) const; + bool A2( regexp::FormalRegExpElement * & node ) const; + bool A3( regexp::FormalRegExpElement * & node ) const; + bool A4( regexp::FormalRegExpElement * & node ) const; + bool A5( regexp::FormalRegExpElement * & node ) const; + bool A6( regexp::FormalRegExpElement * & node ) const; + bool A7( regexp::FormalRegExpElement * & node ) const; + bool A8( regexp::FormalRegExpElement * & node ) const; + bool A9( regexp::FormalRegExpElement * & node ) const; + bool A10( regexp::FormalRegExpElement * & node ) const; + bool A11( regexp::FormalRegExpElement * & node ) const; + bool V1( regexp::FormalRegExpElement * & node ) const; + bool V2( regexp::FormalRegExpElement * & node ) const; + bool V3( regexp::FormalRegExpElement * & node ) const; + bool V4( regexp::FormalRegExpElement * & node ) const; + bool V5( regexp::FormalRegExpElement * & node ) const; + bool V6( regexp::FormalRegExpElement * & node ) const; + bool V8( regexp::FormalRegExpElement * & node ) const; + bool V9( regexp::FormalRegExpElement * & node ) const; + bool V10( regexp::FormalRegExpElement * & node ) const; + + bool X1( regexp::FormalRegExpElement * & node ) const; + + static const RegExpOptimize REG_EXP_OPTIMIZE; }; } /* namespace simplify */ diff --git a/alib2algo/src/regexp/transform/RegExpDerivation.cpp b/alib2algo/src/regexp/transform/RegExpDerivation.cpp index cbf0f3a744251b1c9541e9cdca6735b6ef92a87f..8fad12a3dc2c626a62314786cf139ffffc550e23 100644 --- a/alib2algo/src/regexp/transform/RegExpDerivation.cpp +++ b/alib2algo/src/regexp/transform/RegExpDerivation.cpp @@ -2,7 +2,7 @@ * RegExpDerivation.cpp * * Created on: 19. 1. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #include "RegExpDerivation.h" @@ -14,135 +14,135 @@ namespace regexp regexp::RegExp RegExpDerivation::derivation(const regexp::RegExp& regexp, const string::LinearString& string) { - std::pair<regexp::RegExp*, const string::LinearString> out(nullptr, string); - regexp.getData().Accept((void*) &out, RegExpDerivation::REGEXP_DERIVATION); - regexp::RegExp res = std::move(*(out.first)); - delete out.first; - return res; + std::pair<regexp::RegExp*, const string::LinearString> out(nullptr, string); + regexp.getData().Accept((void*) &out, RegExpDerivation::REGEXP_DERIVATION); + regexp::RegExp res = std::move(*(out.first)); + delete out.first; + return res; } regexp::FormalRegExp RegExpDerivation::derivation(const regexp::FormalRegExp& regexp, const string::LinearString& string) { - const regexp::FormalRegExpElement* newRegExp = regexp.getRegExp().clone(); + const regexp::FormalRegExpElement* newRegExp = regexp.getRegExp().clone(); - for(const auto& symbol : string.getContent()) - { - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> out(symbol, nullptr); - newRegExp->Accept((void*) &out, RegExpDerivation::REGEXP_DERIVATION); - delete newRegExp; - newRegExp = out.second; - } + for(const auto& symbol : string.getContent()) + { + std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> out(symbol, nullptr); + newRegExp->Accept((void*) &out, RegExpDerivation::REGEXP_DERIVATION); + delete newRegExp; + newRegExp = out.second; + } - regexp::FormalRegExp res(std::move(*newRegExp)); - delete newRegExp; - return res; + regexp::FormalRegExp res(std::move(*newRegExp)); + delete newRegExp; + return res; } regexp::UnboundedRegExp RegExpDerivation::derivation(const regexp::UnboundedRegExp& regexp, const string::LinearString& string) { - const regexp::UnboundedRegExpElement* newRegExp = regexp.getRegExp().clone(); + const regexp::UnboundedRegExpElement* newRegExp = regexp.getRegExp().clone(); - for(const auto& symbol : string.getContent()) - { - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> out(symbol, nullptr); - newRegExp->Accept((void*) &out, RegExpDerivation::REGEXP_DERIVATION); - delete newRegExp; - newRegExp = out.second; - } + for(const auto& symbol : string.getContent()) + { + std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> out(symbol, nullptr); + newRegExp->Accept((void*) &out, RegExpDerivation::REGEXP_DERIVATION); + delete newRegExp; + newRegExp = out.second; + } - regexp::UnboundedRegExp res(std::move(*newRegExp)); - delete newRegExp; - return res; + regexp::UnboundedRegExp res(std::move(*newRegExp)); + delete newRegExp; + return res; } // ---------------------------------------------------------------------------- void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExp& regexp) const { - std::pair<regexp::RegExp*, const string::LinearString>& out = *((std::pair<regexp::RegExp*, const string::LinearString>*) userData); - out.first = new regexp::RegExp(this->derivation(regexp, out.second)); + std::pair<regexp::RegExp*, const string::LinearString>& out = *((std::pair<regexp::RegExp*, const string::LinearString>*) userData); + out.first = new regexp::RegExp(this->derivation(regexp, out.second)); } void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExp& regexp) const { - std::pair<regexp::RegExp*, const string::LinearString>& out = *((std::pair<regexp::RegExp*, const string::LinearString>*) userData); - out.first = new regexp::RegExp(this->derivation(regexp, out.second)); + std::pair<regexp::RegExp*, const string::LinearString>& out = *((std::pair<regexp::RegExp*, const string::LinearString>*) userData); + out.first = new regexp::RegExp(this->derivation(regexp, out.second)); } // ---------------------------------------------------------------------------- void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpAlternation& alternation) const { - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); + std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - static_cast<const regexp::FormalRegExpElement&>(alternation.getLeftElement()).Accept(userData, *this); - const regexp::FormalRegExpElement* leftDerivative = out.second; + static_cast<const regexp::FormalRegExpElement&>(alternation.getLeftElement()).Accept(userData, *this); + const regexp::FormalRegExpElement* leftDerivative = out.second; - static_cast<const regexp::FormalRegExpElement&>(alternation.getRightElement()).Accept(userData, *this); - const regexp::FormalRegExpElement* rightDerivative = out.second; + static_cast<const regexp::FormalRegExpElement&>(alternation.getRightElement()).Accept(userData, *this); + const regexp::FormalRegExpElement* rightDerivative = out.second; - out.second = new regexp::FormalRegExpAlternation(std::move(*leftDerivative), std::move(*rightDerivative)); - delete leftDerivative; - delete rightDerivative; + out.second = new regexp::FormalRegExpAlternation(std::move(*leftDerivative), std::move(*rightDerivative)); + delete leftDerivative; + delete rightDerivative; } void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpConcatenation& concatenation) const { - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); + std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - static_cast<const regexp::FormalRegExpElement&>(concatenation.getLeftElement()).Accept(userData, *this); - const regexp::FormalRegExpElement* leftDerivative = out.second; - const regexp::FormalRegExpElement* rightElement = concatenation.getRightElement().clone(); - out.second = new regexp::FormalRegExpConcatenation(std::move(*leftDerivative), std::move(*rightElement)); - delete leftDerivative; - delete rightElement; + static_cast<const regexp::FormalRegExpElement&>(concatenation.getLeftElement()).Accept(userData, *this); + const regexp::FormalRegExpElement* leftDerivative = out.second; + const regexp::FormalRegExpElement* rightElement = concatenation.getRightElement().clone(); + out.second = new regexp::FormalRegExpConcatenation(std::move(*leftDerivative), std::move(*rightElement)); + delete leftDerivative; + delete rightElement; - if(regexp::properties::RegExpEpsilon::languageContainsEpsilon(concatenation.getLeftElement())) - { - const regexp::FormalRegExpElement *alternation = out.second, *rightDerivative = nullptr; - static_cast<const regexp::FormalRegExpElement&>(concatenation.getRightElement()).Accept(userData, *this); - rightDerivative = out.second; + if(regexp::properties::RegExpEpsilon::languageContainsEpsilon(concatenation.getLeftElement())) + { + const regexp::FormalRegExpElement *alternation = out.second, *rightDerivative = nullptr; + static_cast<const regexp::FormalRegExpElement&>(concatenation.getRightElement()).Accept(userData, *this); + rightDerivative = out.second; - out.second = new regexp::FormalRegExpAlternation(std::move(*alternation), std::move(*rightDerivative)); - delete alternation; - delete rightDerivative; - } + out.second = new regexp::FormalRegExpAlternation(std::move(*alternation), std::move(*rightDerivative)); + delete alternation; + delete rightDerivative; + } } void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpIteration& iteration) const { - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); + std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - static_cast<const regexp::FormalRegExpElement&>(iteration.getElement()).Accept(userData, *this); - const regexp::FormalRegExpElement* iterDerivative = out.second; - const regexp::FormalRegExpElement* iter = iteration.clone(); + static_cast<const regexp::FormalRegExpElement&>(iteration.getElement()).Accept(userData, *this); + const regexp::FormalRegExpElement* iterDerivative = out.second; + const regexp::FormalRegExpElement* iter = iteration.clone(); - out.second = new regexp::FormalRegExpConcatenation(std::move(*iterDerivative), std::move(*iter)); - delete iterDerivative; - delete iter; + out.second = new regexp::FormalRegExpConcatenation(std::move(*iterDerivative), std::move(*iter)); + delete iterDerivative; + delete iter; } void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpSymbol& symbol) const { - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); + std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - if(out.first == symbol.getSymbol()) - out.second = new regexp::FormalRegExpEpsilon(); - else - out.second = new regexp::FormalRegExpEmpty(); + if(out.first == symbol.getSymbol()) + out.second = new regexp::FormalRegExpEpsilon(); + else + out.second = new regexp::FormalRegExpEmpty(); } void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpEpsilon&) const { - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - out.second = new regexp::FormalRegExpEmpty(); + std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); + out.second = new regexp::FormalRegExpEmpty(); } void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpEmpty&) const { - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - out.second = new regexp::FormalRegExpEmpty(); + std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); + out.second = new regexp::FormalRegExpEmpty(); } // ---------------------------------------------------------------------------- @@ -150,87 +150,87 @@ void RegExpDerivation::Visit(void* userData, const regexp::FormalRegExpEmpty&) c void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpAlternation& alternation) const { - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - regexp::UnboundedRegExpAlternation* ret = new regexp::UnboundedRegExpAlternation(); + std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); + regexp::UnboundedRegExpAlternation* ret = new regexp::UnboundedRegExpAlternation(); - for(const auto& child : alternation.getElements()) - { - static_cast<const regexp::UnboundedRegExpElement&>(*child).Accept(userData, *this); - ret->appendElement(std::move(*out.second)); - delete out.second; - } + for(const auto& child : alternation.getElements()) + { + static_cast<const regexp::UnboundedRegExpElement&>(*child).Accept(userData, *this); + ret->appendElement(std::move(*out.second)); + delete out.second; + } - out.second = ret; + out.second = ret; } void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpConcatenation& concatenation) const { - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - regexp::UnboundedRegExpAlternation* ret = new regexp::UnboundedRegExpAlternation(); + std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); + regexp::UnboundedRegExpAlternation* ret = new regexp::UnboundedRegExpAlternation(); - for(auto child = concatenation.getElements().begin(); child != concatenation.getElements().end(); child ++) - { - regexp::UnboundedRegExpConcatenation* concat = new regexp::UnboundedRegExpConcatenation(); - static_cast<const regexp::UnboundedRegExpElement&>(**child).Accept(userData, *this); - concat->appendElement(std::move(*out.second)); - delete out.second; + for(auto child = concatenation.getElements().begin(); child != concatenation.getElements().end(); child ++) + { + regexp::UnboundedRegExpConcatenation* concat = new regexp::UnboundedRegExpConcatenation(); + static_cast<const regexp::UnboundedRegExpElement&>(**child).Accept(userData, *this); + concat->appendElement(std::move(*out.second)); + delete out.second; - auto succeedingElement = child; - while(++succeedingElement != concatenation.getElements().end()) - { - regexp::UnboundedRegExpElement* tmp = (*succeedingElement)->clone(); - concat->appendElement(std::move(*tmp)); - delete tmp; - } + auto succeedingElement = child; + while(++succeedingElement != concatenation.getElements().end()) + { + regexp::UnboundedRegExpElement* tmp = (*succeedingElement)->clone(); + concat->appendElement(std::move(*tmp)); + delete tmp; + } - ret->appendElement(std::move(*concat)); - delete concat; + ret->appendElement(std::move(*concat)); + delete concat; - if(regexp::properties::RegExpEpsilon::languageContainsEpsilon(**child)) - continue; // this IF construct is intentional "to match algorithm" - break; - } + if(regexp::properties::RegExpEpsilon::languageContainsEpsilon(**child)) + continue; // this IF construct is intentional "to match algorithm" + break; + } - out.second = ret; + out.second = ret; } void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpIteration& iteration) const { - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); + std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - static_cast<const regexp::UnboundedRegExpElement&>(iteration.getElement()).Accept(userData, *this); - const regexp::UnboundedRegExpElement* iterDerivative = out.second; - const regexp::UnboundedRegExpElement* iter = iteration.clone(); + static_cast<const regexp::UnboundedRegExpElement&>(iteration.getElement()).Accept(userData, *this); + const regexp::UnboundedRegExpElement* iterDerivative = out.second; + const regexp::UnboundedRegExpElement* iter = iteration.clone(); - regexp::UnboundedRegExpConcatenation* ret = new regexp::UnboundedRegExpConcatenation(); - ret->appendElement(std::move(*iterDerivative)); - ret->appendElement(std::move(*iter)); - delete iterDerivative; - delete iter; + regexp::UnboundedRegExpConcatenation* ret = new regexp::UnboundedRegExpConcatenation(); + ret->appendElement(std::move(*iterDerivative)); + ret->appendElement(std::move(*iter)); + delete iterDerivative; + delete iter; - out.second = ret; + out.second = ret; } void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpSymbol& symbol) const { - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); + std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - if(out.first == symbol.getSymbol()) - out.second = new regexp::UnboundedRegExpEpsilon(); - else - out.second = new regexp::UnboundedRegExpEmpty(); + if(out.first == symbol.getSymbol()) + out.second = new regexp::UnboundedRegExpEpsilon(); + else + out.second = new regexp::UnboundedRegExpEmpty(); } void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpEpsilon&) const { - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - out.second = new regexp::UnboundedRegExpEmpty(); + std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); + out.second = new regexp::UnboundedRegExpEmpty(); } void RegExpDerivation::Visit(void* userData, const regexp::UnboundedRegExpEmpty&) const { - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - out.second = new regexp::UnboundedRegExpEmpty(); + std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); + out.second = new regexp::UnboundedRegExpEmpty(); } const RegExpDerivation RegExpDerivation::REGEXP_DERIVATION; diff --git a/alib2algo/src/regexp/transform/RegExpDerivation.h b/alib2algo/src/regexp/transform/RegExpDerivation.h index a9efd8b1f61f83765c5baae387a1ad42ff941f17..d7402ceec1119af461e370ebc20e60d06200df49 100644 --- a/alib2algo/src/regexp/transform/RegExpDerivation.h +++ b/alib2algo/src/regexp/transform/RegExpDerivation.h @@ -2,7 +2,7 @@ * RegExpDerivation.h * * Created on: 19. 1. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #ifndef REGEXPDERIVATION_H_ @@ -27,35 +27,35 @@ namespace regexp class RegExpDerivation : public regexp::VisitableRegExpBase::const_visitor_type, regexp::FormalRegExpElement::const_visitor_type, regexp::UnboundedRegExpElement::const_visitor_type { public: - /** - * @param regexp RegExp to derivate - * @param string String to derivate given RegExp over - * @return derivative of regexp - */ - static regexp::RegExp derivation(const regexp::RegExp& regexp, const string::LinearString& string); + /** + * @param regexp RegExp to derivate + * @param string String to derivate given RegExp over + * @return derivative of regexp + */ + static regexp::RegExp derivation(const regexp::RegExp& regexp, const string::LinearString& string); - static regexp::FormalRegExp derivation(const regexp::FormalRegExp& regexp, const string::LinearString& string); - static regexp::UnboundedRegExp derivation(const regexp::UnboundedRegExp& regexp, const string::LinearString& string); + static regexp::FormalRegExp derivation(const regexp::FormalRegExp& regexp, const string::LinearString& string); + static regexp::UnboundedRegExp derivation(const regexp::UnboundedRegExp& regexp, const string::LinearString& string); private: - void Visit(void*, const regexp::FormalRegExp& empty) const; - void Visit(void*, const regexp::UnboundedRegExp& empty) const; - - void Visit(void*, const regexp::FormalRegExpAlternation& alternation) const; - void Visit(void*, const regexp::FormalRegExpConcatenation& concatenation) const; - void Visit(void*, const regexp::FormalRegExpIteration& iteration) const; - void Visit(void*, const regexp::FormalRegExpSymbol& symbol) const; - void Visit(void*, const regexp::FormalRegExpEpsilon& epsilon) const; - void Visit(void*, const regexp::FormalRegExpEmpty& empty) const; - - void Visit(void*, const regexp::UnboundedRegExpAlternation& alternation) const; - void Visit(void*, const regexp::UnboundedRegExpConcatenation& concatenation) const; - void Visit(void*, const regexp::UnboundedRegExpIteration& iteration) const; - void Visit(void*, const regexp::UnboundedRegExpSymbol& symbol) const; - void Visit(void*, const regexp::UnboundedRegExpEpsilon& epsilon) const; - void Visit(void*, const regexp::UnboundedRegExpEmpty& empty) const; - - static const RegExpDerivation REGEXP_DERIVATION; + void Visit(void*, const regexp::FormalRegExp& empty) const; + void Visit(void*, const regexp::UnboundedRegExp& empty) const; + + void Visit(void*, const regexp::FormalRegExpAlternation& alternation) const; + void Visit(void*, const regexp::FormalRegExpConcatenation& concatenation) const; + void Visit(void*, const regexp::FormalRegExpIteration& iteration) const; + void Visit(void*, const regexp::FormalRegExpSymbol& symbol) const; + void Visit(void*, const regexp::FormalRegExpEpsilon& epsilon) const; + void Visit(void*, const regexp::FormalRegExpEmpty& empty) const; + + void Visit(void*, const regexp::UnboundedRegExpAlternation& alternation) const; + void Visit(void*, const regexp::UnboundedRegExpConcatenation& concatenation) const; + void Visit(void*, const regexp::UnboundedRegExpIteration& iteration) const; + void Visit(void*, const regexp::UnboundedRegExpSymbol& symbol) const; + void Visit(void*, const regexp::UnboundedRegExpEpsilon& epsilon) const; + void Visit(void*, const regexp::UnboundedRegExpEmpty& empty) const; + + static const RegExpDerivation REGEXP_DERIVATION; }; } /* namespace regexp */ diff --git a/alib2algo/src/regexp/transform/RegExpIntegral.cpp b/alib2algo/src/regexp/transform/RegExpIntegral.cpp index 62fc7717c47fb3bd48317d6e4991b0a8f6043ad0..b8ad59ebb42f9c7f2eb1292108fbcfb8ae8e3cfe 100644 --- a/alib2algo/src/regexp/transform/RegExpIntegral.cpp +++ b/alib2algo/src/regexp/transform/RegExpIntegral.cpp @@ -2,7 +2,7 @@ * RegExpIntegral.cpp * * Created on: 24. 2. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #include "RegExpIntegral.h" @@ -12,215 +12,215 @@ namespace regexp regexp::RegExp RegExpIntegral::integral(const regexp::RegExp& regexp, const string::LinearString& string) { - std::pair<regexp::RegExp*, const string::LinearString> out(nullptr, string); - regexp.getData().Accept((void*) &out, RegExpIntegral::REGEXP_INTEGRAL); - regexp::RegExp res = std::move(*(out.first)); - delete out.first; - return res; + std::pair<regexp::RegExp*, const string::LinearString> out(nullptr, string); + regexp.getData().Accept((void*) &out, RegExpIntegral::REGEXP_INTEGRAL); + regexp::RegExp res = std::move(*(out.first)); + delete out.first; + return res; } regexp::FormalRegExp RegExpIntegral::integral(const regexp::FormalRegExp& regexp, const string::LinearString& string) { - const regexp::FormalRegExpElement* newRegExp = regexp.getRegExp().clone(); + const regexp::FormalRegExpElement* newRegExp = regexp.getRegExp().clone(); - for(auto it = string.getContent().begin(); it != string.getContent().end(); it ++) - { - const auto& symbol = *it; + for(auto it = string.getContent().begin(); it != string.getContent().end(); it ++) + { + const auto& symbol = *it; - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> out(symbol, nullptr); - newRegExp->Accept((void*) &out, RegExpIntegral::REGEXP_INTEGRAL); - delete newRegExp; - newRegExp = out.second; - } + std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> out(symbol, nullptr); + newRegExp->Accept((void*) &out, RegExpIntegral::REGEXP_INTEGRAL); + delete newRegExp; + newRegExp = out.second; + } - regexp::FormalRegExp res(std::move(*newRegExp)); - delete newRegExp; - return res; + regexp::FormalRegExp res(std::move(*newRegExp)); + delete newRegExp; + return res; } regexp::UnboundedRegExp RegExpIntegral::integral(const regexp::UnboundedRegExp& regexp, const string::LinearString& string) { - const regexp::UnboundedRegExpElement* newRegExp = regexp.getRegExp().clone(); + const regexp::UnboundedRegExpElement* newRegExp = regexp.getRegExp().clone(); - for(auto it = string.getContent().begin(); it != string.getContent().end(); it ++) - { - const auto& symbol = *it; + for(auto it = string.getContent().begin(); it != string.getContent().end(); it ++) + { + const auto& symbol = *it; - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> out(symbol, nullptr); - newRegExp->Accept((void*) &out, RegExpIntegral::REGEXP_INTEGRAL); - delete newRegExp; - newRegExp = out.second; - } + std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> out(symbol, nullptr); + newRegExp->Accept((void*) &out, RegExpIntegral::REGEXP_INTEGRAL); + delete newRegExp; + newRegExp = out.second; + } - regexp::UnboundedRegExp res(std::move(*newRegExp)); - delete newRegExp; - return res; + regexp::UnboundedRegExp res(std::move(*newRegExp)); + delete newRegExp; + return res; } // ---------------------------------------------------------------------------- void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExp& regexp) const { - std::pair<regexp::RegExp*, const string::LinearString>& out = *((std::pair<regexp::RegExp*, const string::LinearString>*) userData); - out.first = new regexp::RegExp(this->integral(regexp, out.second)); + std::pair<regexp::RegExp*, const string::LinearString>& out = *((std::pair<regexp::RegExp*, const string::LinearString>*) userData); + out.first = new regexp::RegExp(this->integral(regexp, out.second)); } void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExp& regexp) const { - std::pair<regexp::RegExp*, const string::LinearString>& out = *((std::pair<regexp::RegExp*, const string::LinearString>*) userData); - out.first = new regexp::RegExp(this->integral(regexp, out.second)); + std::pair<regexp::RegExp*, const string::LinearString>& out = *((std::pair<regexp::RegExp*, const string::LinearString>*) userData); + out.first = new regexp::RegExp(this->integral(regexp, out.second)); } // ---------------------------------------------------------------------------- void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpAlternation& alternation) const { - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); + std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - static_cast<const regexp::FormalRegExpElement&>(alternation.getLeftElement()).Accept(userData, *this); - const regexp::FormalRegExpElement* leftIntegral = out.second; + static_cast<const regexp::FormalRegExpElement&>(alternation.getLeftElement()).Accept(userData, *this); + const regexp::FormalRegExpElement* leftIntegral = out.second; - static_cast<const regexp::FormalRegExpElement&>(alternation.getRightElement()).Accept(userData, *this); - const regexp::FormalRegExpElement* rightIntegral = out.second; + static_cast<const regexp::FormalRegExpElement&>(alternation.getRightElement()).Accept(userData, *this); + const regexp::FormalRegExpElement* rightIntegral = out.second; - out.second = new regexp::FormalRegExpAlternation(std::move(*leftIntegral), std::move(*rightIntegral)); - delete leftIntegral; - delete rightIntegral; + out.second = new regexp::FormalRegExpAlternation(std::move(*leftIntegral), std::move(*rightIntegral)); + delete leftIntegral; + delete rightIntegral; } void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpConcatenation& concatenation) const { - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); + std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - regexp::FormalRegExpSymbol* symb = new regexp::FormalRegExpSymbol(out.first); - regexp::FormalRegExpElement* concat = concatenation.clone(); - regexp::FormalRegExpConcatenation* ret = new regexp::FormalRegExpConcatenation(std::move(*symb), std::move(*concat)); + regexp::FormalRegExpSymbol* symb = new regexp::FormalRegExpSymbol(out.first); + regexp::FormalRegExpElement* concat = concatenation.clone(); + regexp::FormalRegExpConcatenation* ret = new regexp::FormalRegExpConcatenation(std::move(*symb), std::move(*concat)); - delete concat; - delete symb; + delete concat; + delete symb; - out.second = ret; + out.second = ret; } void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpIteration& iteration) const { - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); + std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - regexp::FormalRegExpSymbol* symb = new regexp::FormalRegExpSymbol(out.first); - regexp::FormalRegExpElement* iter = iteration.clone(); - regexp::FormalRegExpConcatenation* ret = new regexp::FormalRegExpConcatenation(std::move(*symb), std::move(*iter)); + regexp::FormalRegExpSymbol* symb = new regexp::FormalRegExpSymbol(out.first); + regexp::FormalRegExpElement* iter = iteration.clone(); + regexp::FormalRegExpConcatenation* ret = new regexp::FormalRegExpConcatenation(std::move(*symb), std::move(*iter)); - delete iter; - delete symb; + delete iter; + delete symb; - out.second = ret; + out.second = ret; } void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpSymbol& symbol) const { - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); + std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - regexp::FormalRegExpSymbol* symb = new regexp::FormalRegExpSymbol(out.first); - regexp::FormalRegExpElement* symb2 = symbol.clone(); - regexp::FormalRegExpConcatenation* ret = new regexp::FormalRegExpConcatenation(std::move(*symb), std::move(*symb2)); + regexp::FormalRegExpSymbol* symb = new regexp::FormalRegExpSymbol(out.first); + regexp::FormalRegExpElement* symb2 = symbol.clone(); + regexp::FormalRegExpConcatenation* ret = new regexp::FormalRegExpConcatenation(std::move(*symb), std::move(*symb2)); - delete symb2; - delete symb; + delete symb2; + delete symb; - out.second = ret; + out.second = ret; } void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpEpsilon&) const { - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - regexp::FormalRegExpSymbol* symb = new regexp::FormalRegExpSymbol(out.first); - out.second = symb; + std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); + regexp::FormalRegExpSymbol* symb = new regexp::FormalRegExpSymbol(out.first); + out.second = symb; } void RegExpIntegral::Visit(void* userData, const regexp::FormalRegExpEmpty&) const { - std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); - out.second = new regexp::FormalRegExpEmpty(); + std::pair<alphabet::Symbol, regexp::FormalRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::FormalRegExpElement*>*) userData); + out.second = new regexp::FormalRegExpEmpty(); } // ---------------------------------------------------------------------------- void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExpAlternation& alternation) const { - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - regexp::UnboundedRegExpAlternation* ret = new regexp::UnboundedRegExpAlternation(); + std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); + regexp::UnboundedRegExpAlternation* ret = new regexp::UnboundedRegExpAlternation(); - for(const auto& child : alternation.getElements()) - { - static_cast<const regexp::UnboundedRegExpElement&>(*child).Accept(userData, *this); - ret->appendElement(std::move(*out.second)); - delete out.second; - } + for(const auto& child : alternation.getElements()) + { + static_cast<const regexp::UnboundedRegExpElement&>(*child).Accept(userData, *this); + ret->appendElement(std::move(*out.second)); + delete out.second; + } - out.second = ret; + out.second = ret; } void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExpConcatenation& concatenation) const { - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); + std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - regexp::UnboundedRegExpSymbol* symb = new regexp::UnboundedRegExpSymbol(out.first); - regexp::UnboundedRegExpElement* concat = concatenation.clone(); - regexp::UnboundedRegExpConcatenation* ret = new regexp::UnboundedRegExpConcatenation(); - ret->appendElement(std::move(*symb)); - ret->appendElement(std::move(*concat)); + regexp::UnboundedRegExpSymbol* symb = new regexp::UnboundedRegExpSymbol(out.first); + regexp::UnboundedRegExpElement* concat = concatenation.clone(); + regexp::UnboundedRegExpConcatenation* ret = new regexp::UnboundedRegExpConcatenation(); + ret->appendElement(std::move(*symb)); + ret->appendElement(std::move(*concat)); - delete concat; - delete symb; + delete concat; + delete symb; - out.second = ret; + out.second = ret; } void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExpIteration& iteration) const { - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); + std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - regexp::UnboundedRegExpSymbol* symb = new regexp::UnboundedRegExpSymbol(out.first); - regexp::UnboundedRegExpElement* iter = iteration.clone(); - regexp::UnboundedRegExpConcatenation* ret = new regexp::UnboundedRegExpConcatenation(); - ret->appendElement(std::move(*symb)); - ret->appendElement(std::move(*iter)); + regexp::UnboundedRegExpSymbol* symb = new regexp::UnboundedRegExpSymbol(out.first); + regexp::UnboundedRegExpElement* iter = iteration.clone(); + regexp::UnboundedRegExpConcatenation* ret = new regexp::UnboundedRegExpConcatenation(); + ret->appendElement(std::move(*symb)); + ret->appendElement(std::move(*iter)); - delete iter; - delete symb; + delete iter; + delete symb; - out.second = ret; + out.second = ret; } void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExpSymbol& symbol) const { - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); + std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - regexp::UnboundedRegExpSymbol* symb = new regexp::UnboundedRegExpSymbol(out.first); - regexp::UnboundedRegExpElement* symb2 = symbol.clone(); - regexp::UnboundedRegExpConcatenation* ret = new regexp::UnboundedRegExpConcatenation(); - ret->appendElement(std::move(*symb)); - ret->appendElement(std::move(*symb2)); + regexp::UnboundedRegExpSymbol* symb = new regexp::UnboundedRegExpSymbol(out.first); + regexp::UnboundedRegExpElement* symb2 = symbol.clone(); + regexp::UnboundedRegExpConcatenation* ret = new regexp::UnboundedRegExpConcatenation(); + ret->appendElement(std::move(*symb)); + ret->appendElement(std::move(*symb2)); - delete symb2; - delete symb; + delete symb2; + delete symb; - out.second = ret; + out.second = ret; } void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExpEpsilon&) const { - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); + std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - regexp::UnboundedRegExpSymbol* symb = new regexp::UnboundedRegExpSymbol(out.first); - out.second = symb; + regexp::UnboundedRegExpSymbol* symb = new regexp::UnboundedRegExpSymbol(out.first); + out.second = symb; } void RegExpIntegral::Visit(void* userData, const regexp::UnboundedRegExpEmpty&) const { - std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); - out.second = new regexp::UnboundedRegExpEmpty(); + std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*> &out = *((std::pair<alphabet::Symbol, regexp::UnboundedRegExpElement*>*) userData); + out.second = new regexp::UnboundedRegExpEmpty(); } const RegExpIntegral RegExpIntegral::REGEXP_INTEGRAL; diff --git a/alib2algo/src/regexp/transform/RegExpIntegral.h b/alib2algo/src/regexp/transform/RegExpIntegral.h index 558cb85a4188e73751c2ac204357cafdabd74ae1..1f8d6ffc2a4b195e128a81c74ab52616e2b99136 100644 --- a/alib2algo/src/regexp/transform/RegExpIntegral.h +++ b/alib2algo/src/regexp/transform/RegExpIntegral.h @@ -2,7 +2,7 @@ * RegExpIntegral.h * * Created on: 24. 2. 2014 - * Author: Tomas Pecka + * Author: Tomas Pecka */ #ifndef REGEXPINTEGRAL_H_ @@ -24,35 +24,35 @@ namespace regexp class RegExpIntegral : public regexp::VisitableRegExpBase::const_visitor_type, regexp::FormalRegExpElement::const_visitor_type, regexp::UnboundedRegExpElement::const_visitor_type { public: - /** - * @param regexp RegExp to integrate - * @param string String to integrate given RegExp over - * @return integral of regexp - */ - static regexp::RegExp integral(const regexp::RegExp& regexp, const string::LinearString& string); + /** + * @param regexp RegExp to integrate + * @param string String to integrate given RegExp over + * @return integral of regexp + */ + static regexp::RegExp integral(const regexp::RegExp& regexp, const string::LinearString& string); - static regexp::FormalRegExp integral(const regexp::FormalRegExp& regexp, const string::LinearString& string); - static regexp::UnboundedRegExp integral(const regexp::UnboundedRegExp& regexp, const string::LinearString& string); + static regexp::FormalRegExp integral(const regexp::FormalRegExp& regexp, const string::LinearString& string); + static regexp::UnboundedRegExp integral(const regexp::UnboundedRegExp& regexp, const string::LinearString& string); private: - void Visit(void*, const regexp::FormalRegExp& empty) const; - void Visit(void*, const regexp::UnboundedRegExp& empty) const; - - void Visit(void*, const regexp::FormalRegExpAlternation& alternation) const; - void Visit(void*, const regexp::FormalRegExpConcatenation& concatenation) const; - void Visit(void*, const regexp::FormalRegExpIteration& iteration) const; - void Visit(void*, const regexp::FormalRegExpSymbol& symbol) const; - void Visit(void*, const regexp::FormalRegExpEpsilon& epsilon) const; - void Visit(void*, const regexp::FormalRegExpEmpty& empty) const; - - void Visit(void*, const regexp::UnboundedRegExpAlternation& alternation) const; - void Visit(void*, const regexp::UnboundedRegExpConcatenation& concatenation) const; - void Visit(void*, const regexp::UnboundedRegExpIteration& iteration) const; - void Visit(void*, const regexp::UnboundedRegExpSymbol& symbol) const; - void Visit(void*, const regexp::UnboundedRegExpEpsilon& epsilon) const; - void Visit(void*, const regexp::UnboundedRegExpEmpty& empty) const; - - static const RegExpIntegral REGEXP_INTEGRAL; + void Visit(void*, const regexp::FormalRegExp& empty) const; + void Visit(void*, const regexp::UnboundedRegExp& empty) const; + + void Visit(void*, const regexp::FormalRegExpAlternation& alternation) const; + void Visit(void*, const regexp::FormalRegExpConcatenation& concatenation) const; + void Visit(void*, const regexp::FormalRegExpIteration& iteration) const; + void Visit(void*, const regexp::FormalRegExpSymbol& symbol) const; + void Visit(void*, const regexp::FormalRegExpEpsilon& epsilon) const; + void Visit(void*, const regexp::FormalRegExpEmpty& empty) const; + + void Visit(void*, const regexp::UnboundedRegExpAlternation& alternation) const; + void Visit(void*, const regexp::UnboundedRegExpConcatenation& concatenation) const; + void Visit(void*, const regexp::UnboundedRegExpIteration& iteration) const; + void Visit(void*, const regexp::UnboundedRegExpSymbol& symbol) const; + void Visit(void*, const regexp::UnboundedRegExpEpsilon& epsilon) const; + void Visit(void*, const regexp::UnboundedRegExpEmpty& empty) const; + + static const RegExpIntegral REGEXP_INTEGRAL; }; } /* namespace regexp */ diff --git a/alib2algo/src/string/compare/CyclicStringCompare.cpp b/alib2algo/src/string/compare/CyclicStringCompare.cpp index 0e33dfbe2a59f8bc0255e2ee2d8f28346597dc13..418bc5cbc1fb53fe660598eadd658662b36ce4e1 100644 --- a/alib2algo/src/string/compare/CyclicStringCompare.cpp +++ b/alib2algo/src/string/compare/CyclicStringCompare.cpp @@ -2,7 +2,7 @@ * CyclicStringCompare.cpp * * Created on: Oct 9, 2014 - * Author: Radomir Polach */ + * Author: Radomir Polach */ #include "CyclicStringCompare.h" @@ -11,34 +11,34 @@ namespace string { namespace compare { bool CyclicStringCompare::equals(const string::LinearString& u, const string::LinearString& v) { - int n = (int)u.getContent().size(); - int i = -1, j = -1, k; - if (n != (int) v.getContent().size()) return false; - - while(i < n - 1 && j < n - 1) - { - k = 1; - while(k <= n && u.getContent()[(i + k) % n] == v.getContent()[(j + k) % n]) k++; - if (k > n) return true; - if (u.getContent()[(i + k) % n] > v.getContent()[(j + k) % n]) i += k; else j += k; - } - return false; + int n = (int)u.getContent().size(); + int i = -1, j = -1, k; + if (n != (int) v.getContent().size()) return false; + + while(i < n - 1 && j < n - 1) + { + k = 1; + while(k <= n && u.getContent()[(i + k) % n] == v.getContent()[(j + k) % n]) k++; + if (k > n) return true; + if (u.getContent()[(i + k) % n] > v.getContent()[(j + k) % n]) i += k; else j += k; + } + return false; } int CyclicStringCompare::compare(const string::LinearString& u, const string::LinearString& v) { - int n = (int) u.getContent().size(), m = (int)v.getContent().size(); - int i = -1, j = -1, k; - - bool last = 0; - while(i < n - 1 && j < m - 1) - { - k = 1; - while(k <= n && u.getContent()[(i + k) % n] == v.getContent()[(j + k) % m]) k++; - if (k > n) return 0; - last = u.getContent()[(i + k) % n] > v.getContent()[(j + k) % m]; - if (last) i += k; else j += k; - } - return last ? 1 : - 1; + int n = (int) u.getContent().size(), m = (int)v.getContent().size(); + int i = -1, j = -1, k; + + bool last = 0; + while(i < n - 1 && j < m - 1) + { + k = 1; + while(k <= n && u.getContent()[(i + k) % n] == v.getContent()[(j + k) % m]) k++; + if (k > n) return 0; + last = u.getContent()[(i + k) % n] > v.getContent()[(j + k) % m]; + if (last) i += k; else j += k; + } + return last ? 1 : - 1; } } /* namespace compare */ diff --git a/alib2algo/src/string/compare/CyclicStringCompare.h b/alib2algo/src/string/compare/CyclicStringCompare.h index 267a791376918e9f287bb714706e58e1163ce2ca..2e545021553d3a1901575c593b1a348138f3945f 100644 --- a/alib2algo/src/string/compare/CyclicStringCompare.h +++ b/alib2algo/src/string/compare/CyclicStringCompare.h @@ -2,7 +2,7 @@ * CyclicStringCompare.h * * Created on: Oct 14, 2014 - * Author: Radomir Polach + * Author: Radomir Polach */ #ifndef CYCLIC_STRING_COMPARE_H_