diff --git a/aconversions/src/aconversion/ConversionHandler.cpp b/aconversions/src/aconversion/ConversionHandler.cpp index c7f8637b50b559c74cce895c0bb7c2e11ad5430e..7230d808b12b7ba6b80a32907ed7536b3fb72b5e 100644 --- a/aconversions/src/aconversion/ConversionHandler.cpp +++ b/aconversions/src/aconversion/ConversionHandler.cpp @@ -247,7 +247,7 @@ void ConversionHandler::convertLRGtoFSM( void ) { const LeftRegularGrammar lrg = GrammarFactory::buildLeftRegularGrammar( GrammarParser::parse( m_tokens ) ); - AbstractLRGtoFAConverter* conv; + AbstractRGtoFAConverter* conv; switch( m_algorithm ) { @@ -264,7 +264,7 @@ void ConversionHandler::convertRRGtoFSM( void ) { const RightRegularGrammar rrg = GrammarFactory::buildRightRegularGrammar( GrammarParser::parse( m_tokens ) ); - AbstractRRGtoFAConverter* conv; + AbstractRGtoFAConverter* conv; switch( m_algorithm ) { @@ -281,7 +281,7 @@ void ConversionHandler::convertLRGtoRE( void ) { const LeftRegularGrammar lrg = GrammarFactory::buildLeftRegularGrammar( GrammarParser::parse( m_tokens ) ); - AbstractLRGtoREConverter* conv; + AbstractRGtoREConverter* conv; switch( m_algorithm ) { @@ -299,7 +299,7 @@ void ConversionHandler::convertRRGtoRE( void ) { const RightRegularGrammar rrg = GrammarFactory::buildRightRegularGrammar( GrammarParser::parse( m_tokens ) ); - AbstractRRGtoREConverter* conv; + AbstractRGtoREConverter* conv; switch( m_algorithm ) { diff --git a/aconversions/src/fa2re/AbstractFAtoREConverter.cpp b/aconversions/src/fa2re/AbstractFAtoREConverter.cpp index 29344135526e496cfe90ab40acf9eaecf6500bee..7d056806ed478f107f6545fbfce21fc07be62cc0 100644 --- a/aconversions/src/fa2re/AbstractFAtoREConverter.cpp +++ b/aconversions/src/fa2re/AbstractFAtoREConverter.cpp @@ -6,11 +6,6 @@ using namespace regexp; namespace conversions { -AbstractFAtoREConverter::AbstractFAtoREConverter( const FSM & automaton ) : m_automaton( automaton ) -{ - -} - AbstractFAtoREConverter::~AbstractFAtoREConverter( void ) { diff --git a/aconversions/src/fa2re/AbstractFAtoREConverter.h b/aconversions/src/fa2re/AbstractFAtoREConverter.h index 4ea9b2e98c08a8851edc92de6a3555686664c276..dbc23772567d4d046237ae93f6a6af73f07187f2 100644 --- a/aconversions/src/fa2re/AbstractFAtoREConverter.h +++ b/aconversions/src/fa2re/AbstractFAtoREConverter.h @@ -12,7 +12,7 @@ namespace conversions { /** - * Abstract finite automata to regular expression converter. + * Interface for finite automaton to regular expression conversion. */ class AbstractFAtoREConverter : public Conversion { @@ -20,25 +20,10 @@ public: /** * @return regular expression equivalent to source automata. */ - virtual regexp::RegExp convert( ) = 0; + virtual regexp::RegExp convert( void ) = 0; virtual ~AbstractFAtoREConverter( void ); -protected: - /** - * @param automaton Original FSM. - */ - AbstractFAtoREConverter( const automaton::FSM & automaton ); - - /** - * Original FSM. - */ - const automaton::FSM m_automaton; - - /** - * New RE. - */ - regexp::RegExp m_re; }; } /* namespace conversions */ diff --git a/aconversions/src/fa2re/BrzozowskiAlgebraic.cpp b/aconversions/src/fa2re/BrzozowskiAlgebraic.cpp index aa9ef137b6ceba445e4c1077dbfa8fa7235df4d6..6d9d38f04c5e9bf73ad525f3aaee9894d4041cfb 100644 --- a/aconversions/src/fa2re/BrzozowskiAlgebraic.cpp +++ b/aconversions/src/fa2re/BrzozowskiAlgebraic.cpp @@ -14,7 +14,7 @@ using namespace regexp; namespace conversions { -BrzozowskiAlgebraic::BrzozowskiAlgebraic( const FSM & fsm ) : AbstractFAtoREConverter( fsm ) +BrzozowskiAlgebraic::BrzozowskiAlgebraic( const FSM & automaton ) : m_automaton( automaton ) { } @@ -24,20 +24,22 @@ BrzozowskiAlgebraic::~BrzozowskiAlgebraic( void ) } -void BrzozowskiAlgebraic::initEquations( void ) +RegExp BrzozowskiAlgebraic::convert( void ) { + RightRegularEquationSolver solver; + + // initialize equations for( const auto & q : m_automaton.getStates( ) ) - m_solver.addSymbol( Symbol( q.getName( ) ) ); + solver.addSymbol( Symbol( q.getName( ) ) ); for( const auto & q : m_automaton.getStates( ) ) { if( isInSet( q, m_automaton.getFinalStates( ) ) ) - m_solver.addEquation( Symbol( q.getName( ) ), new RegExpEpsilon( ) ); + solver.addEquation( Symbol( q.getName( ) ), new RegExpEpsilon( ) ); else - m_solver.addEquation( Symbol( q.getName( ) ), new RegExpEmpty( ) ); + solver.addEquation( Symbol( q.getName( ) ), new RegExpEmpty( ) ); } - for( const auto & p : m_automaton.getStates( ) ) { for( const auto & q : m_automaton.getStates( ) ) @@ -49,17 +51,14 @@ void BrzozowskiAlgebraic::initEquations( void ) if ( isInSet( TransitionFSM( p, symbol, q ), m_automaton.getTransitions( ) ) ) alt->getElements( ).push_back( new RegExpSymbol( symbol.getSymbol( ) ) ); - m_solver.addEquation( Symbol( p.getName( ) ), Symbol( q.getName( ) ), alt ); + solver.addEquation( Symbol( p.getName( ) ), Symbol( q.getName( ) ), alt ); } } -} - -RegExp BrzozowskiAlgebraic::convert( void ) -{ - initEquations( ); + // set symbol for which the solver will solve equation system Symbol initSymbol( ( * m_automaton.getInitialStates( ).begin( ) ).getName( ) ); - return m_solver.solve( initSymbol ); + + return solver.solve( initSymbol ); } } /* namespace conversions */ diff --git a/aconversions/src/fa2re/BrzozowskiAlgebraic.h b/aconversions/src/fa2re/BrzozowskiAlgebraic.h index 6623b169d0dac2eee8474e85e7b17a92d6455957..dbfee0b4bccb684b19af5ec083b24d2fdda969de 100644 --- a/aconversions/src/fa2re/BrzozowskiAlgebraic.h +++ b/aconversions/src/fa2re/BrzozowskiAlgebraic.h @@ -27,7 +27,7 @@ namespace conversions * Converts FA to RE using Brzozowski's algebraic method using right regular equations. * Source : Melichar 2.122 */ -class BrzozowskiAlgebraic: public conversions::AbstractFAtoREConverter +class BrzozowskiAlgebraic: public AbstractFAtoREConverter { public: /** @@ -39,17 +39,15 @@ public: /** * Performs conversion. - * @return regular expression equivalent to source automata. + * @return regular expression equivalent to input automaton. */ regexp::RegExp convert( void ); private: - void initEquations( void ); - /** - * Regular equation solver. + * Input automaton */ - RightRegularEquationSolver m_solver; + const automaton::FSM & m_automaton; }; } /* namespace conversions */ diff --git a/aconversions/src/fa2re/StateElimination.cpp b/aconversions/src/fa2re/StateElimination.cpp index e849756356544173262cacf5f31dec6e3e0ad8c5..b693a2443a6318662222aa5d98cf838ecb023ba6 100644 --- a/aconversions/src/fa2re/StateElimination.cpp +++ b/aconversions/src/fa2re/StateElimination.cpp @@ -15,7 +15,7 @@ using namespace std; namespace conversions { -StateElimination::StateElimination( const FSM & fsm ) : AbstractFAtoREConverter( fsm ) +StateElimination::StateElimination( const FSM & automaton ) : m_automaton( automaton ) { } @@ -59,10 +59,13 @@ RegExp StateElimination::convert( void ) concat->getElements( ).push_back( iter ); RegExpOptimize opt; - m_re.setRegExp( opt.optimize( concat ) ); + + RegExp ret; + ret.setRegExp( opt.optimize( concat ) ); + delete concat; - return m_re; + return ret; } void StateElimination::constructExtendedNFA( void ) diff --git a/aconversions/src/fa2re/StateElimination.h b/aconversions/src/fa2re/StateElimination.h index f5d27e6bba50ba8fa1bff7e64efc767b0ca6025a..6c02f2eb0bff454242835d2c71122c77d18a0438 100644 --- a/aconversions/src/fa2re/StateElimination.h +++ b/aconversions/src/fa2re/StateElimination.h @@ -99,6 +99,11 @@ private: * Extended NFA. */ ExtendedNFA m_MR; + + /** + * Input automaton + */ + const automaton::FSM & m_automaton; }; } /* namespace conversions */ diff --git a/aconversions/src/fa2rg/AbstractFAtoRGConverter.cpp b/aconversions/src/fa2rg/AbstractFAtoRGConverter.cpp index dedff158feb0bb136988c4c03619ae3478fb2dc0..499919b3514cea4feaa8c39ebbca65a76dc20d7f 100644 --- a/aconversions/src/fa2rg/AbstractFAtoRGConverter.cpp +++ b/aconversions/src/fa2rg/AbstractFAtoRGConverter.cpp @@ -14,12 +14,6 @@ using namespace grammar; namespace conversions { -AbstractFAtoRGConverter::AbstractFAtoRGConverter( const FSM & fsm ) : m_fsm( fsm ) -{ - if( ! m_fsm.isEpsilonFree( ) ) - throw AlibException( "FSM has epsilon transitions" ); -} - AbstractFAtoRGConverter::~AbstractFAtoRGConverter( void ) { diff --git a/aconversions/src/fa2rg/AbstractFAtoRGConverter.h b/aconversions/src/fa2rg/AbstractFAtoRGConverter.h index b05163aa80744c9f477defd3b0ea388c484af43f..76af14f1c659cfa4ecf18ee9ed6e7b4191a187bc 100644 --- a/aconversions/src/fa2rg/AbstractFAtoRGConverter.h +++ b/aconversions/src/fa2rg/AbstractFAtoRGConverter.h @@ -13,7 +13,6 @@ #include <AlibException.h> #include "../abstract/Conversion.h" -#include "StateNonterminalMap.h" namespace conversions { @@ -24,18 +23,7 @@ namespace conversions class AbstractFAtoRGConverter : public Conversion { public: - /** - * @param fsm Source automaton. - */ - AbstractFAtoRGConverter( const automaton::FSM & fsm ); - ~AbstractFAtoRGConverter( void ); - -protected: - /** - * Source automaton. - */ - const automaton::FSM m_fsm; }; } /* namespace conversions */ diff --git a/aconversions/src/fa2rg/StateNonterminalMap.cpp b/aconversions/src/fa2rg/StateNonterminalMap.cpp deleted file mode 100644 index cb6f03c1d06443be194db2a0a7c03b9b836dba93..0000000000000000000000000000000000000000 --- a/aconversions/src/fa2rg/StateNonterminalMap.cpp +++ /dev/null @@ -1,45 +0,0 @@ -/* - * StateNonterminalMap.cpp - * - * Created on: 15. 3. 2014 - * Author: tomas - */ - -#include "StateNonterminalMap.h" - -using namespace alib; -using namespace alphabet; -using namespace automaton; -using namespace grammar; - -namespace conversions -{ - -StateNonterminalMap::StateNonterminalMap( const FSM & fsm, RegularGrammar & grammar ) : - m_fsm( fsm ), - m_grammar( grammar ) -{ - initMap( ); -} - -const Symbol & StateNonterminalMap::getNonTerminal( const State & state ) -{ - auto it = m_map.find( state ); - - // probably harmless to return reference to object stored in map - if( it != m_map.end( ) ) - return it->second; - - throw AlibException( "No nonterminal for this state! Should not happen unless you manipulated with automata after initialize mapper initialize." ); -} - -void StateNonterminalMap::initMap( void ) -{ - for( const auto & state : m_fsm.getStates( ) ) - { - Symbol nonTerminal = m_grammar.createUniqueNonTerminalSymbol( state.getName( ), false ); - m_map.insert( make_pair( state, nonTerminal ) ); - } -} - -} /* namespace conversions */ diff --git a/aconversions/src/fa2rg/StateNonterminalMap.h b/aconversions/src/fa2rg/StateNonterminalMap.h deleted file mode 100644 index eb8053b72c16b0ad7beeb63c966c80ec7b4c72b7..0000000000000000000000000000000000000000 --- a/aconversions/src/fa2rg/StateNonterminalMap.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * StateNonterminalMap.h - * - * Created on: 15. 3. 2014 - * Author: tomas - */ - -#ifndef STATENONTERMINALMAP_H_ -#define STATENONTERMINALMAP_H_ - -#include <map> - -#include <automaton/FSM/FSM.h> -#include <grammar/Regular/RegularGrammar.h> -#include <AlibException.h> - -#include "../include/macros.h" - -namespace conversions -{ - -/** - * This class maps grammar nonterminals to automata states in order to avoid - * symbol collision. - */ -class StateNonterminalMap -{ -public: - /** - * Initiates mapping function, also creates nonterminals states in target grammar. - * - * @param fsm Source FSM. - * @param grammar Target grammar. - */ - StateNonterminalMap( const automaton::FSM & fsm, grammar::RegularGrammar & grammar ); - const alphabet::Symbol & getNonTerminal( const automaton::State & state ); - -private: - void initMap( void ); - - /** - * Maps automaton's state to grammar's Symbol - */ - std::map<const automaton::State, const alphabet::Symbol> m_map; - - /** - * Source automata. - */ - const automaton::FSM m_fsm; - - /** - * Target grammar. - */ - grammar::RegularGrammar & m_grammar; - -}; - -} /* namespace conversions */ - -#endif /* STATENONTERMINALMAP_H_ */ diff --git a/aconversions/src/fa2rg/fa2lrg/AbstractFAtoLRGConverter.cpp b/aconversions/src/fa2rg/fa2lrg/AbstractFAtoLRGConverter.cpp index 341a5874d15fa867e4902e7e1512ddb5a0b87109..e6b531f79a1bb867f0489cd192b15acdb01f08be 100644 --- a/aconversions/src/fa2rg/fa2lrg/AbstractFAtoLRGConverter.cpp +++ b/aconversions/src/fa2rg/fa2lrg/AbstractFAtoLRGConverter.cpp @@ -1,17 +1,8 @@ #include "AbstractFAtoLRGConverter.h" -using namespace alib; -using namespace automaton; -using namespace grammar; - namespace conversions { -AbstractFAtoLRGConverter::AbstractFAtoLRGConverter( const FSM & automaton ) : AbstractFAtoRGConverter( automaton ) -{ - -} - AbstractFAtoLRGConverter::~AbstractFAtoLRGConverter( void ) { diff --git a/aconversions/src/fa2rg/fa2lrg/AbstractFAtoLRGConverter.h b/aconversions/src/fa2rg/fa2lrg/AbstractFAtoLRGConverter.h index 360f2515a7556a6553cfc909c956c08861867ef4..38b61b7a9959079e018120937730975e059d40fc 100644 --- a/aconversions/src/fa2rg/fa2lrg/AbstractFAtoLRGConverter.h +++ b/aconversions/src/fa2rg/fa2lrg/AbstractFAtoLRGConverter.h @@ -20,19 +20,8 @@ public: /** * @return left regular grammar equivalent to source automata. */ - virtual grammar::LeftRegularGrammar convert( ) = 0; + virtual grammar::LeftRegularGrammar convert( void ) = 0; virtual ~AbstractFAtoLRGConverter( void ); - -protected: - /** - * New left regular grammar. - */ - grammar::LeftRegularGrammar m_grammar; - - /** - * @param automaton Original FSM. - */ - AbstractFAtoLRGConverter( const automaton::FSM & automaton ); }; } /* namespace conversions */ diff --git a/aconversions/src/fa2rg/fa2lrg/FAtoLRGConverter.cpp b/aconversions/src/fa2rg/fa2lrg/FAtoLRGConverter.cpp index ae572db176d242a77c0ecdbd5a40259213013538..ebafba78796fa5d6a35eee35471d94607e2fc898 100644 --- a/aconversions/src/fa2rg/fa2lrg/FAtoLRGConverter.cpp +++ b/aconversions/src/fa2rg/fa2lrg/FAtoLRGConverter.cpp @@ -3,13 +3,15 @@ using namespace std; using namespace automaton; using namespace grammar; +using namespace alib; namespace conversions { -FAtoLRGConverter::FAtoLRGConverter( const FSM & automaton ) : AbstractFAtoLRGConverter( automaton ) +FAtoLRGConverter::FAtoLRGConverter( const FSM & automaton ) : m_automaton( automaton ) { - + if( ! automaton.isEpsilonFree( ) ) + throw AlibException( "FSM has epsilon transitions." ); } FAtoLRGConverter::~FAtoLRGConverter( void ) @@ -19,61 +21,69 @@ FAtoLRGConverter::~FAtoLRGConverter( void ) LeftRegularGrammar FAtoLRGConverter::convert( void ) { - for( const auto & symbol : m_fsm.getInputAlphabet( ) ) - m_grammar.addTerminalSymbol( symbol ); + LeftRegularGrammar grammar; + map<State, Symbol> nonterminalMap; - StateNonterminalMap symbolMap( m_fsm, m_grammar ); + for( const auto & symbol : m_automaton.getInputAlphabet( ) ) + grammar.addTerminalSymbol( symbol ); + + for( const auto & state : m_automaton.getStates( ) ) + { + Symbol nt = grammar.createUniqueNonTerminalSymbol( state.getName( ) ); + nonterminalMap.insert( pair<State,Symbol>( state, nt ) ); + } // step 2 - create set of P in G - for( const auto & transition : m_fsm.getTransitions( ) ) + for( const auto & transition : m_automaton.getTransitions( ) ) { // 2a list<Symbol> leftSide, rightSide; - leftSide.push_back( symbolMap.getNonTerminal( transition.getTo( ) ) ); - rightSide.push_back( symbolMap.getNonTerminal( transition.getFrom( ) ) ); + leftSide.push_back( nonterminalMap.find( transition.getTo( ) )->second ); + rightSide.push_back( nonterminalMap.find( transition.getFrom( ) )->second ); rightSide.push_back( transition.getInput( ) ); - m_grammar.addRule( Rule( leftSide, rightSide ) ); + grammar.addRule( Rule( leftSide, rightSide ) ); // 2b - if( isInSet( transition.getFrom( ), m_fsm.getInitialStates( ) ) ) + if( isInSet( transition.getFrom( ), m_automaton.getInitialStates( ) ) ) { list<Symbol> leftSide, rightSide; - leftSide.push_back( symbolMap.getNonTerminal( transition.getTo( ) ) ); + leftSide.push_back( nonterminalMap.find( transition.getTo( ) )->second ); rightSide.push_back( transition.getInput( ) ); - m_grammar.addRule( Rule( leftSide, rightSide ) ); + grammar.addRule( Rule( leftSide, rightSide ) ); } } // step 3 - Symbol newStart = m_grammar.createUniqueNonTerminalSymbol( "S", false ); - m_grammar.setStartSymbol( newStart ); + Symbol newStart = grammar.createUniqueNonTerminalSymbol( "S", false ); + grammar.setStartSymbol( newStart ); // - if( isInSet( * m_fsm.getInitialStates( ).begin( ), m_fsm.getFinalStates( ) ) ) + if( isInSet( * m_automaton.getInitialStates( ).begin( ), m_automaton.getFinalStates( ) ) ) { list<Symbol> leftSide = { newStart }, rightSide; - m_grammar.addRule( Rule( leftSide, rightSide ) ); + grammar.addRule( Rule( leftSide, rightSide ) ); } // pro vsecky neterminaly, ktere vznikly z finalnich stavu: // kopiruj jejich prave strany k newStart // pokud nejsou nikde vpravo, smaz je - for( const auto & rule : m_grammar.getRules( ) ) + for( const auto & rule : grammar.getRules( ) ) { const Symbol & leftSide = rule.getLeftSide( ).front( ); - if( isInSet( State( leftSide.getSymbol( ) ), m_fsm.getFinalStates( ) ) ) + if( isInSet( State( leftSide.getSymbol( ) ), m_automaton.getFinalStates( ) ) ) { list<Symbol> leftSide = { newStart }, rightSide( rule.getRightSide( ) ); - m_grammar.addRule( Rule( leftSide, rightSide ) ); + grammar.addRule( Rule( leftSide, rightSide ) ); } // jetenhle krok nutny,??? - // if( leftSide != m_grammar.getStartSymbol( ) && ! m_grammar.isNonTerminalOnRightSideOfAnyRule( leftSide ) ) - // m_grammar.removeRule( rule ); + // if( leftSide != grammar.getStartSymbol( ) && ! grammar.isNonTerminalOnRightSideOfAnyRule( leftSide ) ) + // grammar.removeRule( rule ); } - return m_grammar; + return grammar; } + } /* namespace conversions */ diff --git a/aconversions/src/fa2rg/fa2lrg/FAtoLRGConverter.h b/aconversions/src/fa2rg/fa2lrg/FAtoLRGConverter.h index 348384ecf50efa0d0452fe611277c116e40e18f2..4bc20c401d27ff3b1998972e47638daf494d17a5 100644 --- a/aconversions/src/fa2rg/fa2lrg/FAtoLRGConverter.h +++ b/aconversions/src/fa2rg/fa2lrg/FAtoLRGConverter.h @@ -1,6 +1,8 @@ #ifndef __FATOLRGCONVERTER_H__ #define __FATOLRGCONVERTER_H__ +#include <map> + #include <grammar/Regular/LeftRegularGrammar.h> #include <automaton/FSM/FSM.h> @@ -31,9 +33,9 @@ public: protected: /** - * New left regular grammar. + * input automaton */ - grammar::LeftRegularGrammar m_grammar; + const automaton::FSM & m_automaton; }; } /* namespace conversions */ diff --git a/aconversions/src/fa2rg/fa2rrg/AbstractFAtoRRGConverter.cpp b/aconversions/src/fa2rg/fa2rrg/AbstractFAtoRRGConverter.cpp index 2201255bceca77d744dae1e2098a154e8b58e244..1af46b4f827289835a681c9d5e514b596afc4712 100644 --- a/aconversions/src/fa2rg/fa2rrg/AbstractFAtoRRGConverter.cpp +++ b/aconversions/src/fa2rg/fa2rrg/AbstractFAtoRRGConverter.cpp @@ -1,16 +1,8 @@ #include "AbstractFAtoRRGConverter.h" -using namespace automaton; -using namespace grammar; - namespace conversions { -AbstractFAtoRRGConverter::AbstractFAtoRRGConverter( const FSM & automaton ) : AbstractFAtoRGConverter( automaton ) -{ - -} - AbstractFAtoRRGConverter::~AbstractFAtoRRGConverter( void ) { diff --git a/aconversions/src/fa2rg/fa2rrg/AbstractFAtoRRGConverter.h b/aconversions/src/fa2rg/fa2rrg/AbstractFAtoRRGConverter.h index 44e7edea9e868687d20be4e27e2b9944e878a22e..6df8f25e871256a83eb74a433bb413bee8dd514c 100644 --- a/aconversions/src/fa2rg/fa2rrg/AbstractFAtoRRGConverter.h +++ b/aconversions/src/fa2rg/fa2rrg/AbstractFAtoRRGConverter.h @@ -20,19 +20,9 @@ public: /** * @return right regular grammar equivalent to source automata. */ - virtual grammar::RightRegularGrammar convert( ) = 0; - virtual ~AbstractFAtoRRGConverter( void ); - -protected: - /** - * New right regular grammar. - */ - grammar::RightRegularGrammar m_grammar; + virtual grammar::RightRegularGrammar convert( void ) = 0; - /** - * @param automaton Original automaton. - */ - AbstractFAtoRRGConverter( const automaton::FSM & automaton ); + virtual ~AbstractFAtoRRGConverter( void ); }; } /* namespace conversions */ diff --git a/aconversions/src/fa2rg/fa2rrg/FAtoRRGConverter.cpp b/aconversions/src/fa2rg/fa2rrg/FAtoRRGConverter.cpp index 28b1db614752ab0cba3bb37be58a63d785de688e..4a87c0a6d23bd8a4ce9334da9692dcd59370ebe2 100644 --- a/aconversions/src/fa2rg/fa2rrg/FAtoRRGConverter.cpp +++ b/aconversions/src/fa2rg/fa2rrg/FAtoRRGConverter.cpp @@ -3,13 +3,15 @@ using namespace std; using namespace automaton; using namespace grammar; +using namespace alib; namespace conversions { -FAtoRRGConverter::FAtoRRGConverter( const FSM & automaton ) : AbstractFAtoRRGConverter( automaton ) +FAtoRRGConverter::FAtoRRGConverter( const FSM & automaton ) : m_automaton( automaton ) { - + if( ! automaton.isEpsilonFree( ) ) + throw AlibException( "FSM has epsilon transitions." ); } FAtoRRGConverter::~FAtoRRGConverter( void ) @@ -19,73 +21,80 @@ FAtoRRGConverter::~FAtoRRGConverter( void ) RightRegularGrammar FAtoRRGConverter::convert( void ) { - for( const auto & symbol : m_fsm.getInputAlphabet( ) ) - m_grammar.addTerminalSymbol( symbol ); + RightRegularGrammar grammar; + map<State, Symbol> nonterminalMap; + + for( const auto & symbol : m_automaton.getInputAlphabet( ) ) + grammar.addTerminalSymbol( symbol ); - StateNonterminalMap symbolMap( m_fsm, m_grammar ); + for( const auto & state : m_automaton.getStates( ) ) + { + Symbol nt = grammar.createUniqueNonTerminalSymbol( state.getName( ) ); + nonterminalMap.insert( pair<State,Symbol>( state, nt ) ); + } // step 2 - create set of P in G - for( const auto & transition : m_fsm.getTransitions( ) ) + for( const auto & transition : m_automaton.getTransitions( ) ) { // 2a list<Symbol> leftSide, rightSide; - leftSide.push_back( symbolMap.getNonTerminal( transition.getFrom( ) ) ); + leftSide.push_back( nonterminalMap.find( transition.getFrom( ) )->second ); rightSide.push_back( transition.getInput( ) ); - rightSide.push_back( symbolMap.getNonTerminal( transition.getTo( ) ) ); - m_grammar.addRule( Rule( leftSide, rightSide ) ); + rightSide.push_back( nonterminalMap.find( transition.getTo( ) )->second ); + grammar.addRule( Rule( leftSide, rightSide ) ); // 2b - if( isInSet( transition.getTo( ), m_fsm.getFinalStates( ) ) ) + if( isInSet( transition.getTo( ), m_automaton.getFinalStates( ) ) ) { list<Symbol> leftSide, rightSide; - leftSide.push_back( symbolMap.getNonTerminal( transition.getFrom( ) ) ); + leftSide.push_back( nonterminalMap.find( transition.getFrom( ) )->second ); rightSide.push_back( transition.getInput( ) ); /* * Rule might already in - consider case: * \delta(A,a) = { F, G }, where both F and G are final states */ - if( ! isInSet( Rule( leftSide, rightSide), m_grammar.getRules( ) ) ) - m_grammar.addRule( Rule( leftSide, rightSide ) ); + if( ! isInSet( Rule( leftSide, rightSide), grammar.getRules( ) ) ) + grammar.addRule( Rule( leftSide, rightSide ) ); } } // step 3 - set start symbol of G - m_grammar.setStartSymbol( symbolMap.getNonTerminal( * m_fsm.getInitialStates( ).begin( ) ) ); + grammar.setStartSymbol( nonterminalMap.find( * m_automaton.getInitialStates( ).begin( ) )->second ); // step 4 - if( isInSet( * m_fsm.getInitialStates( ).begin( ), m_fsm.getFinalStates( ) ) ) + if( isInSet( * m_automaton.getInitialStates( ).begin( ), m_automaton.getFinalStates( ) ) ) { // 4a - if( ! m_grammar.isNonTerminalOnRightSideOfAnyRule( m_grammar.getStartSymbol( ) ) ) + if( ! grammar.isNonTerminalOnRightSideOfAnyRule( grammar.getStartSymbol( ) ) ) { list<Symbol> leftSide, rightSide; - leftSide.push_back( m_grammar.getStartSymbol( ) ); - m_grammar.addRule( Rule( leftSide, rightSide ) ); + leftSide.push_back( grammar.getStartSymbol( ) ); + grammar.addRule( Rule( leftSide, rightSide ) ); } else // 4b { - Symbol newStart = m_grammar.createUniqueNonTerminalSymbol( m_grammar.getStartSymbol( ).getSymbol( ), false ); + Symbol newStart = grammar.createUniqueNonTerminalSymbol( grammar.getStartSymbol( ).getSymbol( ), false ); - for( const auto & rule : m_grammar.getRules( ) ) + for( const auto & rule : grammar.getRules( ) ) { - if( rule.getLeftSide( ).front( ) == m_grammar.getStartSymbol( ) ) + if( rule.getLeftSide( ).front( ) == grammar.getStartSymbol( ) ) { list<Symbol> leftSide, rightSide( rule.getRightSide( ) ); leftSide.push_back( newStart ); - m_grammar.addRule( Rule( leftSide, rightSide ) ); + grammar.addRule( Rule( leftSide, rightSide ) ); } } - m_grammar.setStartSymbol( newStart ); + grammar.setStartSymbol( newStart ); list<Symbol> leftSide, rightSide; leftSide.push_back( newStart ); Rule rr( leftSide, rightSide ); - m_grammar.addRule( Rule( leftSide, rightSide ) ); + grammar.addRule( Rule( leftSide, rightSide ) ); } } - return m_grammar; + return grammar; } } /* namespace conversions */ diff --git a/aconversions/src/fa2rg/fa2rrg/FAtoRRGConverter.h b/aconversions/src/fa2rg/fa2rrg/FAtoRRGConverter.h index 074d36ddd5b39e0c98ebb2d4d0cd80b5d4b5fea9..dc759f8488bd845c84c0fc393601aed6f34a617d 100644 --- a/aconversions/src/fa2rg/fa2rrg/FAtoRRGConverter.h +++ b/aconversions/src/fa2rg/fa2rrg/FAtoRRGConverter.h @@ -1,6 +1,8 @@ #ifndef __FATORRGCONVERTER_H__ #define __FATORRGCONVERTER_H__ +#include <map> + #include <grammar/Regular/RightRegularGrammar.h> #include <automaton/FSM/FSM.h> @@ -31,9 +33,9 @@ public: protected: /** - * New right regular grammar. + * input automaton */ - grammar::RightRegularGrammar m_grammar; + const automaton::FSM & m_automaton; }; } /* namespace conversions */ diff --git a/aconversions/src/re2fa/AbstractREtoFAConverter.cpp b/aconversions/src/re2fa/AbstractREtoFAConverter.cpp index af481d488e637b0c8066bd7fc3173a57a4230c49..c99bff419f1f15c9bf9fa6990f2f4790479a4ed7 100644 --- a/aconversions/src/re2fa/AbstractREtoFAConverter.cpp +++ b/aconversions/src/re2fa/AbstractREtoFAConverter.cpp @@ -5,11 +5,6 @@ using namespace regexp; namespace conversions { -AbstractREtoFAConverter::AbstractREtoFAConverter(const RegExp & re ) : m_re(re) -{ - -} - AbstractREtoFAConverter::~AbstractREtoFAConverter(void) { diff --git a/aconversions/src/re2fa/AbstractREtoFAConverter.h b/aconversions/src/re2fa/AbstractREtoFAConverter.h index 69af927a2d72b3c6f92882ad0032b72d4b9e282f..cfea00444534e3bd8ffe398cfd3e901543b6629a 100644 --- a/aconversions/src/re2fa/AbstractREtoFAConverter.h +++ b/aconversions/src/re2fa/AbstractREtoFAConverter.h @@ -13,7 +13,7 @@ namespace conversions { /** - * Abstract regular expression to finite automata converter. + * Interface for regular expression to finite automata conversion. */ class AbstractREtoFAConverter : public Conversion { @@ -24,22 +24,6 @@ public: virtual automaton::FSM convert( void ) = 0; virtual ~AbstractREtoFAConverter( void ); - -protected: - /** - * @param re Source regular expression. - */ - AbstractREtoFAConverter(const regexp::RegExp & re ); - - /** - * Source regular expression. - */ - const regexp::RegExp m_re; - - /** - * New automaton. - */ - automaton::FSM m_fsm; }; } /* namespace conversions */ diff --git a/aconversions/src/re2fa/Brzozowski.cpp b/aconversions/src/re2fa/Brzozowski.cpp index 24415f77393dde06cdef2d2cd349bc730c44368e..e6779762db520c4fc5f43cd6acf9aacf8e0ad09e 100644 --- a/aconversions/src/re2fa/Brzozowski.cpp +++ b/aconversions/src/re2fa/Brzozowski.cpp @@ -7,6 +7,7 @@ #include "Brzozowski.h" +using namespace std; using namespace alib; using namespace automaton; using namespace regexp; @@ -14,7 +15,7 @@ using namespace regexp; namespace conversions { -Brzozowski::Brzozowski( const RegExp & re ) : AbstractREtoFAConverter( re ) +Brzozowski::Brzozowski( const RegExp & re ) : m_re( re ) { } @@ -69,70 +70,42 @@ FSM Brzozowski::convert( void ) // ------------------------------------------------------------------------ // 3. - StateBuilder builder( Q ); + FSM automaton; + int stateId = 0; + map<RegExp, State> stateMap; for( const auto & r : Q ) - m_fsm.addState( builder.getState( r ) ); + { + State q( toBase26( ++ stateId ) ); + stateMap.insert( std::pair<RegExp,State>( r, q ) ); + automaton.addState( q ); + } for( const auto & a : alphabet ) - m_fsm.addInputSymbol( a.getSymbol( ) ); + automaton.addInputSymbol( a.getSymbol( ) ); for( const auto & r : Q ) { RegExpDerivation deriv( r ); - for( const auto & a: m_fsm.getInputAlphabet( ) ) + for( const auto & a: automaton.getInputAlphabet( ) ) { RegExp derived = deriv.derivation( a ); derived = opt.optimize( derived ); - TransitionFSM t( builder.getState( r ), a, builder.getState( derived ) ); - if( ! isInSet( t, m_fsm.getTransitions( ) ) ) - m_fsm.addTransition( t ); + TransitionFSM t( stateMap.find( r )->second, a, stateMap.find( derived )->second ); + if( ! isInSet( t, automaton.getTransitions( ) ) ) + automaton.addTransition( t ); } } - m_fsm.addInitialState( builder.getState( V ) ); + automaton.addInitialState( stateMap.find( V )->second ); for( const auto & U : Q ) if( U.containsEmptyString( ) ) - m_fsm.addFinalState( builder.getState( U ) ); - - return m_fsm; -} - -// ---------------------------------------------------------------------------- - -Brzozowski::StateBuilder::StateBuilder( const set<RegExp> & Q ) -{ - m_stateId = 0; - - for( const auto & regexp : Q ) - m_states.insert( pair<RegExp, State>( regexp, State( createNewName( ) ) ) ); -} - -const State & Brzozowski::StateBuilder::getState( const RegExp & re ) const -{ - auto state = m_states.find( re ); - if( state != m_states.end() ) return state->second; - - throw AlibException( "Brzozowski::StateBuilder - Regular expression not found!" ); -} - -string Brzozowski::StateBuilder::createNewName( void ) -{ - // http://en.wikipedia.org/wiki/Hexavigesimal - - unsigned int n = m_stateId ++; - string name; - do - { - unsigned int remainder = n % 26; - name += ( char )( remainder + 'A' ); - n = (n - remainder) / 26; - } while (n > 0); + automaton.addFinalState( stateMap.find( U )->second ); - return string( name.rbegin( ), name.rend( ) ); + return automaton; } } /* namespace conversions */ diff --git a/aconversions/src/re2fa/Brzozowski.h b/aconversions/src/re2fa/Brzozowski.h index c858dfb5c7cb37297aaf20709ceb382b528be00f..e145355cd7752cacf7f5c938859978aca7943fd2 100644 --- a/aconversions/src/re2fa/Brzozowski.h +++ b/aconversions/src/re2fa/Brzozowski.h @@ -10,15 +10,16 @@ #include <map> #include <set> +#include <string> #include <deque> #include <automaton/State.h> #include <AlibException.h> #include "AbstractREtoFAConverter.h" +#include "../shared/Hexavigesimal.h" #include "RegExpDerivation.h" - #include "RegExpOptimize.h" namespace conversions @@ -46,37 +47,9 @@ public: private: /** - * Maps every regular expression to unique state of automata. + * input regexp */ - class StateBuilder - { - public: - /** - * @param Q set of regular expressions. - */ - StateBuilder( const std::set<regexp::RegExp> & Q ); - - /** - * Get automata state representing given regular expression. - * - * @param re Regular expression - * @return automata state representing given regular expression. - */ - const automaton::State & getState( const regexp::RegExp & re ) const; - - private: - /** - * Creates original state name. - */ - std::string createNewName( void ); - - /** - * Map - */ - std::map<const regexp::RegExp, automaton::State> m_states; - - unsigned int m_stateId; - }; + const regexp::RegExp & m_re; }; } /* namespace conversions */ diff --git a/aconversions/src/re2fa/Glushkov.cpp b/aconversions/src/re2fa/Glushkov.cpp index b3b3e6f9d56545f1182f402a7f5a24be223f6c54..9c86441c799467e15cee42359c929aceb76c9337 100644 --- a/aconversions/src/re2fa/Glushkov.cpp +++ b/aconversions/src/re2fa/Glushkov.cpp @@ -16,7 +16,7 @@ using namespace std; namespace conversions { -Glushkov::Glushkov( const RegExp & re ) : AbstractREtoFAConverter( re ) +Glushkov::Glushkov( const RegExp & re ) : m_re( re ) { } @@ -28,9 +28,11 @@ Glushkov::~Glushkov( void ) FSM Glushkov::convert( void ) { + FSM automaton; + // step 1 for( auto const& symbol : m_re.getAlphabet( ) ) - m_fsm.addInputSymbol( symbol.getSymbol( ) ); + automaton.addInputSymbol( symbol.getSymbol( ) ); // steps 2, 3, 4 m_first = GlushkovTraversal::first( m_re ); @@ -46,14 +48,14 @@ FSM Glushkov::convert( void ) // step 5 State q0( "q0" ); - m_fsm.addState( q0 ); - m_fsm.addInitialState( q0 ); + automaton.addState( q0 ); + automaton.addInitialState( q0 ); for( auto const& symbol : GlushkovTraversal::getSymbols( m_re ) ) { State q( to_string( symbol.getId( ) ) ); m_stateMap.insert( std::pair<GlushkovSymbol, State>( symbol, q ) ); - m_fsm.addState( q ); + automaton.addState( q ); } // step 6 @@ -61,7 +63,7 @@ FSM Glushkov::convert( void ) { State const& q = m_stateMap.find( symbol )->second; - m_fsm.addTransition( q0, symbol.getInputSymbol( ), q ); + automaton.addTransition( q0, symbol.getInputSymbol( ), q ); } for( auto const& pair : m_pairs ) @@ -69,20 +71,20 @@ FSM Glushkov::convert( void ) State const& p = m_stateMap.find( pair.getFirst( ) )->second; State const& q = m_stateMap.find( pair.getSecond( ) )->second; - m_fsm.addTransition( p, pair.getSecond( ).getInputSymbol( ), q ); + automaton.addTransition( p, pair.getSecond( ).getInputSymbol( ), q ); } // step 7 for( auto const& symbol : m_last ) { State const & q = m_stateMap.find( symbol )->second; - m_fsm.addFinalState( q ); + automaton.addFinalState( q ); } if( m_re.containsEmptyString( ) ) - m_fsm.addFinalState( q0 ); + automaton.addFinalState( q0 ); - return m_fsm; + return automaton; } } /* namespace conversions */ diff --git a/aconversions/src/re2fa/Glushkov.h b/aconversions/src/re2fa/Glushkov.h index 1ce77e74a0738f1601a969fa5c952557d51c14be..59faa884b443e8f6fe7e078025fb464fd6af53bc 100644 --- a/aconversions/src/re2fa/Glushkov.h +++ b/aconversions/src/re2fa/Glushkov.h @@ -47,6 +47,11 @@ private: std::set<GlushkovSymbol> m_first, m_last; std::set<GlushkovPair> m_pairs; + /** + * input regexp + */ + const regexp::RegExp & m_re; + }; } /* namespace conversions */ diff --git a/aconversions/src/re2fa/Thompson.cpp b/aconversions/src/re2fa/Thompson.cpp index e0e17d6cc158beaa4adcecbbf226e2c56af1d331..c92832ec6e99cf2a6e330786433606910ff0685f 100644 --- a/aconversions/src/re2fa/Thompson.cpp +++ b/aconversions/src/re2fa/Thompson.cpp @@ -13,7 +13,7 @@ using namespace regexp; namespace conversions { -Thompson::Thompson( const RegExp & re ) : AbstractREtoFAConverter( re ) +Thompson::Thompson( const RegExp & re ) : m_re( re ) { } @@ -26,6 +26,8 @@ Thompson::~Thompson( void ) FSM Thompson::convert( void ) { + m_fsm = FSM( ); + for( const auto & symbol : m_re.getAlphabet( ) ) m_fsm.addInputSymbol( symbol.getSymbol( ) ); diff --git a/aconversions/src/re2fa/Thompson.h b/aconversions/src/re2fa/Thompson.h index 3ba43932c739957018dbd7873319988d3ef9e3ae..3e1455415fb29c7821431ad9cac3d6477d2cd08c 100644 --- a/aconversions/src/re2fa/Thompson.h +++ b/aconversions/src/re2fa/Thompson.h @@ -44,6 +44,16 @@ public: automaton::FSM convert( void ); private: + /** + * input regexp + */ + const regexp::RegExp & m_re; + + /** + * output FSM ($\varepsilon$--NFA) + */ + automaton::FSM m_fsm; + /** * Stores head and tail state of "subautomaton" created in regexp subtree. */ diff --git a/aconversions/src/re2rg/AbstractREtoRGConverter.cpp b/aconversions/src/re2rg/AbstractREtoRGConverter.cpp index f561376cf36fee2acfdb50a3a7cab9a69faecfe6..1d5cff0f8306d5ce4fdfe35eefb51a3b1b22baba 100644 --- a/aconversions/src/re2rg/AbstractREtoRGConverter.cpp +++ b/aconversions/src/re2rg/AbstractREtoRGConverter.cpp @@ -12,11 +12,6 @@ using namespace regexp; namespace conversions { -AbstractREtoRGConverter::AbstractREtoRGConverter( const RegExp & re ) : m_re( re ) -{ - -} - AbstractREtoRGConverter::~AbstractREtoRGConverter( void ) { diff --git a/aconversions/src/re2rg/AbstractREtoRGConverter.h b/aconversions/src/re2rg/AbstractREtoRGConverter.h index 86f6f8e32cce4181b834710bb605b251721605dd..b18bb971529db20a4ee93ced03f202e28e65cc42 100644 --- a/aconversions/src/re2rg/AbstractREtoRGConverter.h +++ b/aconversions/src/re2rg/AbstractREtoRGConverter.h @@ -17,23 +17,12 @@ namespace conversions { /** - * Abstract regular expression to grammar converter. + * Interface for regular expression to regular grammar conversion. */ class AbstractREtoRGConverter : public Conversion { public: virtual ~AbstractREtoRGConverter( void ); - -protected: - /** - * @param re Source regular expression. - */ - AbstractREtoRGConverter( const regexp::RegExp & re ); - - /** - * Source regular expression. - */ - const regexp::RegExp m_re; }; } /* namespace conversions */ diff --git a/aconversions/src/re2rg/re2rrg/AbstractREtoRRGConverter.cpp b/aconversions/src/re2rg/re2rrg/AbstractREtoRRGConverter.cpp index 8011b86bf9920b87de10bb389ac6aabdc52b4a3a..2012a5a800e24b028b0995e3e813f62c46ad6108 100644 --- a/aconversions/src/re2rg/re2rrg/AbstractREtoRRGConverter.cpp +++ b/aconversions/src/re2rg/re2rrg/AbstractREtoRRGConverter.cpp @@ -12,11 +12,6 @@ using namespace regexp; namespace conversions { -AbstractREtoRRGConverter::AbstractREtoRRGConverter( const RegExp & re ) : AbstractREtoRGConverter( re ) -{ - -} - AbstractREtoRRGConverter::~AbstractREtoRRGConverter( void ) { diff --git a/aconversions/src/re2rg/re2rrg/AbstractREtoRRGConverter.h b/aconversions/src/re2rg/re2rrg/AbstractREtoRRGConverter.h index 4c2dd5e5d4dbe8ecef9db87c08857d112d769eac..36cd9224f2ff24b940f21b4b68d4c656263e7148 100644 --- a/aconversions/src/re2rg/re2rrg/AbstractREtoRRGConverter.h +++ b/aconversions/src/re2rg/re2rrg/AbstractREtoRRGConverter.h @@ -18,7 +18,7 @@ namespace conversions { /** - * Abstract regular epxression to right regular grammar converter. + * Interface for regular epxression to right regular grammar conversion. */ class AbstractREtoRRGConverter: public AbstractREtoRGConverter { @@ -29,17 +29,6 @@ public: * @return right regular grammar equivalent to source regexp. */ virtual grammar::RightRegularGrammar convert( void ) = 0; - -protected: - /** - * @param re Source regular expression. - */ - AbstractREtoRRGConverter( const regexp::RegExp & re ); - - /** - * New right regular grammar. - */ - grammar::RightRegularGrammar m_grammar; }; } /* namespace conversions */ diff --git a/aconversions/src/re2rg/re2rrg/BrzozowskiDerivationRRG.cpp b/aconversions/src/re2rg/re2rrg/BrzozowskiDerivationRRG.cpp index 0797f640ce957dc7494ab8e44a147368078d20cf..8ba4a892b59764d0452a6e32a577e515650a26da 100644 --- a/aconversions/src/re2rg/re2rrg/BrzozowskiDerivationRRG.cpp +++ b/aconversions/src/re2rg/re2rrg/BrzozowskiDerivationRRG.cpp @@ -15,7 +15,7 @@ using namespace regexp; namespace conversions { -BrzozowskiDerivationRRG::BrzozowskiDerivationRRG( const RegExp & regexp ) : AbstractREtoRRGConverter( regexp ) +BrzozowskiDerivationRRG::BrzozowskiDerivationRRG( const RegExp & re ) : m_re( re ) { } @@ -66,11 +66,18 @@ RightRegularGrammar BrzozowskiDerivationRRG::convert( void ) } // 3. + RightRegularGrammar grammar; + map<RegExp, Symbol> nonterminalMap; + int nonterminalId = 0; for( const auto & s : alphabet ) - m_grammar.addTerminalSymbol( s.getSymbol( ) ); + grammar.addTerminalSymbol( s.getSymbol( ) ); - NonTerminalBuilder builder( N, m_grammar ); + for( const auto & r : N ) + { + Symbol nt = grammar.createUniqueNonTerminalSymbol( toBase26( ++ nonterminalId ) ); + nonterminalMap.insert( pair<RegExp, Symbol>( r, nt ) ); + } for( const auto & r : N ) { @@ -81,86 +88,47 @@ RightRegularGrammar BrzozowskiDerivationRRG::convert( void ) RegExp derived = deriv.derivation( a ); derived = opt.optimize( derived ); - list<Symbol> leftSide = { builder.getNonTerminal( r ) }; - list<Symbol> rightSide = { a, builder.getNonTerminal( derived ) }; + list<Symbol> leftSide = { nonterminalMap.find( r )->second }; + list<Symbol> rightSide = { a, nonterminalMap.find( derived )->second }; Rule r( leftSide, rightSide ); - m_grammar.addRule( r ); + grammar.addRule( r ); if( derived.containsEmptyString( ) ) { list<Symbol> rightSide = { a }; Rule r( leftSide, rightSide ); - m_grammar.addRule( r ); + grammar.addRule( r ); } } } - m_grammar.setStartSymbol( builder.getNonTerminal( V ) ); + grammar.setStartSymbol( nonterminalMap.find( V )->second ); if( V.containsEmptyString( ) ) { - list<Symbol> leftSide = { builder.getNonTerminal( V ) }; + list<Symbol> leftSide = { nonterminalMap.find( V )->second }; list<Symbol> rightSide; - if( m_grammar.isNonTerminalOnRightSideOfAnyRule( m_grammar.getStartSymbol( ) ) ) + if( grammar.isNonTerminalOnRightSideOfAnyRule( grammar.getStartSymbol( ) ) ) { - Symbol newStart = m_grammar.createUniqueNonTerminalSymbol( m_grammar.getStartSymbol( ).getSymbol( ), false ); + Symbol newStart = grammar.createUniqueNonTerminalSymbol( grammar.getStartSymbol( ).getSymbol( ), false ); list<Symbol> leftSideNewStart = { newStart }; - for( const auto & rule : m_grammar.getRules( ) ) - if( rule.getLeftSide( ).front( ) == m_grammar.getStartSymbol( ) ) - m_grammar.addRule( Rule( leftSideNewStart, rule.getRightSide( ) ) ); + for( const auto & rule : grammar.getRules( ) ) + if( rule.getLeftSide( ).front( ) == grammar.getStartSymbol( ) ) + grammar.addRule( Rule( leftSideNewStart, rule.getRightSide( ) ) ); - m_grammar.setStartSymbol( newStart ); - m_grammar.addRule( Rule( leftSideNewStart, rightSide ) ); + grammar.setStartSymbol( newStart ); + grammar.addRule( Rule( leftSideNewStart, rightSide ) ); } else { - m_grammar.addRule( Rule ( leftSide, rightSide ) ); + grammar.addRule( Rule ( leftSide, rightSide ) ); } } - return m_grammar; -} - -// ---------------------------------------------------------------------------- - -BrzozowskiDerivationRRG::NonTerminalBuilder::NonTerminalBuilder( const set<RegExp> & Q, RightRegularGrammar & grammar ) : m_grammar( grammar ) -{ - m_nonTerminalId = 0; - - for( const auto & regexp : Q ) - { - const string nonTerminalName = createNewName( ); - Symbol NT = m_grammar.createUniqueNonTerminalSymbol( nonTerminalName, false ); - - m_nonTerminals.insert( pair<RegExp, Symbol>( regexp, NT ) ); - } -} - -const Symbol & BrzozowskiDerivationRRG::NonTerminalBuilder::getNonTerminal( const RegExp & re ) const -{ - auto state = m_nonTerminals.find( re ); - if( state != m_nonTerminals.end( ) ) return state->second; - - throw AlibException( "BrzozowskiDerivationRRG::NonTerminalBuilder - Regular expression not found!" ); -} - -string BrzozowskiDerivationRRG::NonTerminalBuilder::createNewName( void ) -{ - // http://en.wikipedia.org/wiki/Hexavigesimal - - unsigned int n = m_nonTerminalId ++; - string name; - do - { - unsigned int remainder = n % 26; - name += ( char )( remainder + 'A' ); - n = (n - remainder) / 26; - } while (n > 0); - - return string( name.rbegin( ), name.rend( ) ); + return grammar; } } /* namespace conversions */ diff --git a/aconversions/src/re2rg/re2rrg/BrzozowskiDerivationRRG.h b/aconversions/src/re2rg/re2rrg/BrzozowskiDerivationRRG.h index 90182cf0cc046be93762dfb98dbd69ade66957b9..83f1218b682da0e8f1322227f016d633b2ecc206 100644 --- a/aconversions/src/re2rg/re2rrg/BrzozowskiDerivationRRG.h +++ b/aconversions/src/re2rg/re2rrg/BrzozowskiDerivationRRG.h @@ -17,6 +17,7 @@ #include <regexp/RegExp.h> #include "AbstractREtoRRGConverter.h" +#include "../../shared/Hexavigesimal.h" #include "RegExpOptimize.h" #include "RegExpDerivation.h" @@ -45,18 +46,10 @@ public: grammar::RightRegularGrammar convert( void ); protected: - class NonTerminalBuilder - { - public: - NonTerminalBuilder( const std::set<regexp::RegExp> & Q, grammar::RightRegularGrammar & grammar ); - const alphabet::Symbol & getNonTerminal( const regexp::RegExp & re ) const; - - private: - grammar::RightRegularGrammar & m_grammar; - std::map<const regexp::RegExp, alphabet::Symbol> m_nonTerminals; - std::string createNewName( void ); - unsigned int m_nonTerminalId; - }; + /* + * input regexp + */ + const regexp::RegExp & m_re; }; } /* namespace conversions */ diff --git a/aconversions/src/re2rg/re2rrg/GlushkovRRG.cpp b/aconversions/src/re2rg/re2rrg/GlushkovRRG.cpp index 3973dd030dac864a63a9b3d9f9a22bf21becb629..e7dc539e770b096837b60ae5c921efec09f357ae 100644 --- a/aconversions/src/re2rg/re2rrg/GlushkovRRG.cpp +++ b/aconversions/src/re2rg/re2rrg/GlushkovRRG.cpp @@ -16,7 +16,7 @@ using namespace std; namespace conversions { -GlushkovRRG::GlushkovRRG( const RegExp & re ) : AbstractREtoRRGConverter( re ) +GlushkovRRG::GlushkovRRG( const RegExp & re ) : m_re( re ) { } @@ -28,9 +28,11 @@ GlushkovRRG::~GlushkovRRG( void ) RightRegularGrammar GlushkovRRG::convert( void ) { + RightRegularGrammar grammar; + // step 1 for( auto const& symbol : m_re.getAlphabet( ) ) - m_grammar.addTerminalSymbol( symbol.getSymbol( ) ); + grammar.addTerminalSymbol( symbol.getSymbol( ) ); // steps 2, 3, 4 m_first = GlushkovTraversal::first( m_re ); @@ -41,12 +43,12 @@ RightRegularGrammar GlushkovRRG::convert( void ) // \e in q0 check is in step 7 // step 5 - Symbol S = m_grammar.createUniqueNonTerminalSymbol( "S" ); - m_grammar.setStartSymbol( S ); + Symbol S = grammar.createUniqueNonTerminalSymbol( "S" ); + grammar.setStartSymbol( S ); for( auto const& symbol : GlushkovTraversal::getSymbols( m_re ) ) { - Symbol a = m_grammar.createUniqueNonTerminalSymbol( symbol.getInputSymbol( ).getSymbol( ) + to_string( symbol.getId( ) ) ); + Symbol a = grammar.createUniqueNonTerminalSymbol( symbol.getInputSymbol( ).getSymbol( ) + to_string( symbol.getId( ) ) ); m_symbolMap.insert( std::pair<GlushkovSymbol, Symbol>( symbol, a ) ); } @@ -58,7 +60,7 @@ RightRegularGrammar GlushkovRRG::convert( void ) list<Symbol> leftSide = { S }; list<Symbol> rightSide = { symbol.getInputSymbol( ), a }; - m_grammar.addRule( Rule( leftSide, rightSide ) ); + grammar.addRule( Rule( leftSide, rightSide ) ); } for( auto const& pair : m_pairs ) @@ -69,7 +71,7 @@ RightRegularGrammar GlushkovRRG::convert( void ) list<Symbol> leftSide = { a }; list<Symbol> rightSide = { pair.getSecond( ).getInputSymbol( ), b }; - m_grammar.addRule( Rule( leftSide, rightSide ) ); + grammar.addRule( Rule( leftSide, rightSide ) ); } // step 7 @@ -83,7 +85,7 @@ RightRegularGrammar GlushkovRRG::convert( void ) Symbol const& a = m_symbolMap.find( symbol )->second; - for( auto const & rule : m_grammar.getRules( ) ) + for( auto const & rule : grammar.getRules( ) ) { if( isInList( a, rule.getRightSide( ) ) ) { @@ -91,8 +93,8 @@ RightRegularGrammar GlushkovRRG::convert( void ) list<Symbol> rightSide = { symbol.getInputSymbol( ) }; Rule r( leftSide, rightSide ); - if( ! isInSet( r, m_grammar.getRules( ) ) ) - m_grammar.addRule( r ); + if( ! isInSet( r, grammar.getRules( ) ) ) + grammar.addRule( r ); } } } @@ -101,10 +103,10 @@ RightRegularGrammar GlushkovRRG::convert( void ) { list<Symbol> leftSide = { S }; list<Symbol> rightSide = { }; - m_grammar.addRule( Rule( leftSide, rightSide ) ); + grammar.addRule( Rule( leftSide, rightSide ) ); } - return m_grammar; + return grammar; } } /* namespace conversions */ diff --git a/aconversions/src/re2rg/re2rrg/GlushkovRRG.h b/aconversions/src/re2rg/re2rrg/GlushkovRRG.h index c17d8751ba5f55254c7badae90d4e98aa396fc2e..e7e10807c74f656cadd1900621bb58e881628eff 100644 --- a/aconversions/src/re2rg/re2rrg/GlushkovRRG.h +++ b/aconversions/src/re2rg/re2rrg/GlushkovRRG.h @@ -43,6 +43,11 @@ private: std::map<GlushkovSymbol, alphabet::Symbol> m_symbolMap; std::set<GlushkovSymbol> m_first, m_last; std::set<GlushkovPair> m_pairs; + + /* + * input regexp + */ + const regexp::RegExp & m_re; }; } /* namespace conversions */ diff --git a/aconversions/src/rg2fa/AbstractRGtoFAConverter.cpp b/aconversions/src/rg2fa/AbstractRGtoFAConverter.cpp index c7439b614ea9ffce7cc6765f2dc23f2641dc9011..4dce814a342ef1021e90f96dd1d87122f9e2d86e 100644 --- a/aconversions/src/rg2fa/AbstractRGtoFAConverter.cpp +++ b/aconversions/src/rg2fa/AbstractRGtoFAConverter.cpp @@ -3,11 +3,6 @@ namespace conversions { -AbstractRGtoFAConverter::AbstractRGtoFAConverter( void ) -{ - -} - AbstractRGtoFAConverter::~AbstractRGtoFAConverter( void ) { diff --git a/aconversions/src/rg2fa/AbstractRGtoFAConverter.h b/aconversions/src/rg2fa/AbstractRGtoFAConverter.h index f4ed104590ebf5e7e905aac2449a915cb3b39550..6a93df1afdaf16ff4d6edfdb0487f2e4468dafe9 100644 --- a/aconversions/src/rg2fa/AbstractRGtoFAConverter.h +++ b/aconversions/src/rg2fa/AbstractRGtoFAConverter.h @@ -11,7 +11,7 @@ namespace conversions { /** - * Abstract regular grammar to finite state machine conversion. + * Interface for regular grammar to finite automaton conversion. */ class AbstractRGtoFAConverter : public Conversion { @@ -21,14 +21,6 @@ public: */ virtual automaton::FSM convert( void ) = 0; virtual ~AbstractRGtoFAConverter( void ); - -protected: - AbstractRGtoFAConverter( void ); - - /** - * New automaton. - */ - automaton::FSM m_automaton; }; } /* namespace conversions */ diff --git a/aconversions/src/rg2fa/lrg2fa/AbstractLRGtoFAConverter.cpp b/aconversions/src/rg2fa/lrg2fa/AbstractLRGtoFAConverter.cpp deleted file mode 100644 index 3fe26c9770e867ad7d3c7714064c194c7c3b428e..0000000000000000000000000000000000000000 --- a/aconversions/src/rg2fa/lrg2fa/AbstractLRGtoFAConverter.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/* - * AbstractLRGtoFAConverter.cpp - * - * Created on: 16. 3. 2014 - * Author: tomas - */ - -#include "AbstractLRGtoFAConverter.h" - -using namespace grammar; - -namespace conversions -{ - -AbstractLRGtoFAConverter::AbstractLRGtoFAConverter( const LeftRegularGrammar & lrg ) : m_grammar( lrg ) -{ - -} - -AbstractLRGtoFAConverter::~AbstractLRGtoFAConverter( void ) -{ - -} - -} /* namespace conversions */ diff --git a/aconversions/src/rg2fa/lrg2fa/AbstractLRGtoFAConverter.h b/aconversions/src/rg2fa/lrg2fa/AbstractLRGtoFAConverter.h deleted file mode 100644 index 8e1448ff79267bc3b6bec87407ebd7da2b1ad4da..0000000000000000000000000000000000000000 --- a/aconversions/src/rg2fa/lrg2fa/AbstractLRGtoFAConverter.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * AbstractLRGtoFAConverter.h - * - * Created on: 16. 3. 2014 - * Author: tomas - */ - -#ifndef ABSTRACTLRGTOFACONVERTER_H_ -#define ABSTRACTLRGTOFACONVERTER_H_ - -#include <grammar/Regular/LeftRegularGrammar.h> - -#include "../AbstractRGtoFAConverter.h" - -namespace conversions -{ - -/** - * Abstract left regular grammar to finite automata conversion. - */ -class AbstractLRGtoFAConverter: public AbstractRGtoFAConverter -{ -public: - ~AbstractLRGtoFAConverter( void ); - -protected: - /** - * @param Source left regular grammar. - */ - AbstractLRGtoFAConverter( const grammar::LeftRegularGrammar & lrg ); - - /** - * Source grammar. - */ - const grammar::LeftRegularGrammar m_grammar; - -}; - -} /* namespace conversions */ - -#endif /* ABSTRACTLRGTOFACONVERTER_H_ */ diff --git a/aconversions/src/rg2fa/lrg2fa/LRGtoFAConverter.cpp b/aconversions/src/rg2fa/lrg2fa/LRGtoFAConverter.cpp index 81650e2418fe4e4ee92e4f14bd3aed947173fa17..dd06004fcb4dd6e5537a9c680196174c6df214d9 100644 --- a/aconversions/src/rg2fa/lrg2fa/LRGtoFAConverter.cpp +++ b/aconversions/src/rg2fa/lrg2fa/LRGtoFAConverter.cpp @@ -7,9 +7,9 @@ using namespace grammar; namespace conversions { -LRGtoFAConverter::LRGtoFAConverter( const LeftRegularGrammar & grammar ) : AbstractLRGtoFAConverter( grammar ) +LRGtoFAConverter::LRGtoFAConverter( const LeftRegularGrammar & grammar ) : m_grammar( grammar ) { - //TODO: if is not LRG throw Exception + } LRGtoFAConverter::~LRGtoFAConverter( void ) @@ -19,13 +19,15 @@ LRGtoFAConverter::~LRGtoFAConverter( void ) FSM LRGtoFAConverter::convert( void ) { + FSM automaton; + for( const auto & symbol : m_grammar.getTerminalSymbols( ) ) - m_automaton.addInputSymbol( symbol ); + automaton.addInputSymbol( symbol ); for( const auto & symbol : m_grammar.getNonTerminalSymbols( ) ) - m_automaton.addState( State( symbol.getSymbol( ) ) ); + automaton.addState( State( symbol.getSymbol( ) ) ); - const State & startState = m_automaton.createUniqueState( "q0", true ); + const State & startState = automaton.createUniqueState( "q0", true ); // step 3, constructing \delta for( const auto & rule : m_grammar.getRules( ) ) @@ -39,7 +41,7 @@ FSM LRGtoFAConverter::convert( void ) if( rule.getRightSide( ).size( ) == 1 ) { const Symbol & input( rule.getRightSide( ).front( ).getSymbol( ) ); - m_automaton.addTransition( startState, input, current ); + automaton.addTransition( startState, input, current ); } // if B->Ca => \delta(C,a)=B else if( rule.getRightSide( ).size( ) == 2 ) @@ -47,20 +49,20 @@ FSM LRGtoFAConverter::convert( void ) State next( rule.getRightSide( ).front( ).getSymbol( ) ); const Symbol & input = rule.getRightSide( ).back( ); - m_automaton.addTransition( next, input, current ); + automaton.addTransition( next, input, current ); } } // step 4 - m_automaton.addInitialState( startState ); + automaton.addInitialState( startState ); // step 5 - m_automaton.addFinalState( State( m_grammar.getStartSymbol().getSymbol() ) ); + automaton.addFinalState( State( m_grammar.getStartSymbol().getSymbol() ) ); for( const auto & rule : m_grammar.getRules( ) ) if( m_grammar.isEpsilonRule( rule ) ) - m_automaton.addFinalState( startState ); + automaton.addFinalState( startState ); - return m_automaton; + return automaton; } } /* namespace conversions */ diff --git a/aconversions/src/rg2fa/lrg2fa/LRGtoFAConverter.h b/aconversions/src/rg2fa/lrg2fa/LRGtoFAConverter.h index 28ff087a46a9f8bea4fc41e6c0eb469a28d1db97..19f42c89051b7bcf74b30ab2d9a26cd7c7213c01 100644 --- a/aconversions/src/rg2fa/lrg2fa/LRGtoFAConverter.h +++ b/aconversions/src/rg2fa/lrg2fa/LRGtoFAConverter.h @@ -1,7 +1,10 @@ #ifndef __LRGTOFACONVERTER_H__ #define __LRGTOFACONVERTER_H__ -#include "AbstractLRGtoFAConverter.h" +#include <grammar/Regular/LeftRegularGrammar.h> +#include <automaton/FSM/FSM.h> + +#include "../AbstractRGtoFAConverter.h" namespace conversions { @@ -10,7 +13,7 @@ namespace conversions * Converts left regular grammar to finite automata. * Source: Melichar 2.102. */ -class LRGtoFAConverter : public AbstractLRGtoFAConverter +class LRGtoFAConverter : public AbstractRGtoFAConverter { public: /** @@ -25,6 +28,12 @@ public: * @return FSM equivalent to source grammar. */ automaton::FSM convert( void ); + +private: + /** + * input grammar + */ + const grammar::LeftRegularGrammar & m_grammar; }; } /* namespace conversions */ diff --git a/aconversions/src/rg2fa/rrg2fa/AbstractRRGtoFAConverter.cpp b/aconversions/src/rg2fa/rrg2fa/AbstractRRGtoFAConverter.cpp deleted file mode 100644 index c67474eb5058a25a39d01358416d8104ea02621f..0000000000000000000000000000000000000000 --- a/aconversions/src/rg2fa/rrg2fa/AbstractRRGtoFAConverter.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/* - * AbstractRRGtoFAConverter.cpp - * - * Created on: 16. 3. 2014 - * Author: tomas - */ - -#include "AbstractRRGtoFAConverter.h" - -using namespace grammar; - -namespace conversions -{ - -AbstractRRGtoFAConverter::AbstractRRGtoFAConverter( const RightRegularGrammar & rrg ) : m_grammar( rrg ) -{ - -} - -AbstractRRGtoFAConverter::~AbstractRRGtoFAConverter() -{ - -} - -} /* namespace conversions */ diff --git a/aconversions/src/rg2fa/rrg2fa/AbstractRRGtoFAConverter.h b/aconversions/src/rg2fa/rrg2fa/AbstractRRGtoFAConverter.h deleted file mode 100644 index 7704aa6f7267567d241c73681e18a3f02ed837d5..0000000000000000000000000000000000000000 --- a/aconversions/src/rg2fa/rrg2fa/AbstractRRGtoFAConverter.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * AbstractRRGtoFAConverter.h - * - * Created on: 16. 3. 2014 - * Author: tomas - */ - -#ifndef ABSTRACTRRGTOFACONVERTER_H_ -#define ABSTRACTRRGTOFACONVERTER_H_ - -#include <grammar/Regular/RightRegularGrammar.h> - -#include "../AbstractRGtoFAConverter.h" - -namespace conversions -{ - -/** - * Abstract right regular grammar to finite automata conversion. - */ -class AbstractRRGtoFAConverter: public AbstractRGtoFAConverter -{ -public: - virtual ~AbstractRRGtoFAConverter( void ); - -protected: - /** - * @param rrg Source right regular grammar. - */ - AbstractRRGtoFAConverter( const grammar::RightRegularGrammar & rrg ); - - /** - * Source grammar. - */ - const grammar::RightRegularGrammar m_grammar; -}; - -} /* namespace conversions */ - -#endif /* ABSTRACTRRGTOFACONVERTER_H_ */ diff --git a/aconversions/src/rg2fa/rrg2fa/RRGtoFAConverter.cpp b/aconversions/src/rg2fa/rrg2fa/RRGtoFAConverter.cpp index 30e6ee74edb6bb9d595840d145f85646754a1806..ea121d2fdbc81a8f4af7859f2b38e1ec8dc65ea2 100644 --- a/aconversions/src/rg2fa/rrg2fa/RRGtoFAConverter.cpp +++ b/aconversions/src/rg2fa/rrg2fa/RRGtoFAConverter.cpp @@ -7,9 +7,9 @@ using namespace grammar; namespace conversions { -RRGtoFAConverter::RRGtoFAConverter( const RightRegularGrammar & grammar ) : AbstractRRGtoFAConverter( grammar ) +RRGtoFAConverter::RRGtoFAConverter( const RightRegularGrammar & grammar ) : m_grammar( grammar ) { - //TODO: if is not RRG throw Exception + } RRGtoFAConverter::~RRGtoFAConverter( void ) @@ -20,13 +20,15 @@ RRGtoFAConverter::~RRGtoFAConverter( void ) FSM RRGtoFAConverter::convert( void ) { + FSM automaton; + for( const auto & symbol : m_grammar.getTerminalSymbols( ) ) - m_automaton.addInputSymbol( symbol ); + automaton.addInputSymbol( symbol ); for( const auto & symbol : m_grammar.getNonTerminalSymbols( ) ) - m_automaton.addState( State( symbol.getSymbol( ) ) ); + automaton.addState( State( symbol.getSymbol( ) ) ); - const State & AState = m_automaton.createUniqueState( "A", true ); + const State & AState = automaton.createUniqueState( "A", true ); for( const auto & rule : m_grammar.getRules( ) ) { @@ -38,27 +40,27 @@ FSM RRGtoFAConverter::convert( void ) if( rule.getRightSide( ).size( ) == 1 ) // if B->a => \delta(B,a)=AState { const Symbol & input( rule.getRightSide( ).front( ).getSymbol( ) ); - m_automaton.addTransition( current, input, AState ); + automaton.addTransition( current, input, AState ); } else if( rule.getRightSide( ).size( ) == 2 ) // if B->aC => \delta(B,a)=C { const Symbol & input = rule.getRightSide( ).front( ); State next( rule.getRightSide( ).back( ).getSymbol( ) ); - m_automaton.addTransition( current, input, next ); + automaton.addTransition( current, input, next ); } } // step 4 - m_automaton.addInitialState( m_grammar.getStartSymbol().getSymbol() ); + automaton.addInitialState( m_grammar.getStartSymbol().getSymbol() ); // step 5 - m_automaton.addFinalState( AState ); + automaton.addFinalState( AState ); for( const auto & rule : m_grammar.getRules( ) ) if( m_grammar.isEpsilonRule( rule ) ) - m_automaton.addFinalState( State( m_grammar.getStartSymbol( ).getSymbol( ) ) ); + automaton.addFinalState( State( m_grammar.getStartSymbol( ).getSymbol( ) ) ); - return m_automaton; + return automaton; } } /* namespace conversions */ diff --git a/aconversions/src/rg2fa/rrg2fa/RRGtoFAConverter.h b/aconversions/src/rg2fa/rrg2fa/RRGtoFAConverter.h index 0eae2d96580c290e60beea0b46ea61fd0538e8a8..854790c7616c43ec0d081f3f70fdaf20021fecc6 100644 --- a/aconversions/src/rg2fa/rrg2fa/RRGtoFAConverter.h +++ b/aconversions/src/rg2fa/rrg2fa/RRGtoFAConverter.h @@ -1,7 +1,10 @@ #ifndef __RRGTOFACONVERTER_H__ #define __RRGTOFACONVERTER_H__ -#include "AbstractRRGtoFAConverter.h" +#include <grammar/Regular/RightRegularGrammar.h> +#include <automaton/FSM/FSM.h> + +#include "../AbstractRGtoFAConverter.h" namespace conversions { @@ -10,7 +13,7 @@ namespace conversions * Converts left regular grammar to finite automata. * Source: Melichar 2.98 */ -class RRGtoFAConverter : public AbstractRRGtoFAConverter +class RRGtoFAConverter : public AbstractRGtoFAConverter { public: /** @@ -25,6 +28,12 @@ public: * @return FSM equivalent to source grammar. */ automaton::FSM convert( void ); + +private: + /** + * input grammar + */ + const grammar::RightRegularGrammar & m_grammar; }; } /* namespace conversions */ diff --git a/aconversions/src/rg2re/AbstractRGtoREConverter.cpp b/aconversions/src/rg2re/AbstractRGtoREConverter.cpp index 60bae347d1021ee6066a20e4afb6eca2dd8f8a15..d6df0214fab43672f8fd91193ca6a9159e058c53 100644 --- a/aconversions/src/rg2re/AbstractRGtoREConverter.cpp +++ b/aconversions/src/rg2re/AbstractRGtoREConverter.cpp @@ -10,11 +10,6 @@ namespace conversions { -AbstractRGtoREConverter::AbstractRGtoREConverter( void ) -{ - -} - AbstractRGtoREConverter::~AbstractRGtoREConverter( void ) { diff --git a/aconversions/src/rg2re/AbstractRGtoREConverter.h b/aconversions/src/rg2re/AbstractRGtoREConverter.h index afc7790257d663cddeaeefc8c55895e62ba4c72d..bfc2ea1310db6a99338500016cec453a30bea525 100644 --- a/aconversions/src/rg2re/AbstractRGtoREConverter.h +++ b/aconversions/src/rg2re/AbstractRGtoREConverter.h @@ -16,7 +16,7 @@ namespace conversions { /** - * Abstract regular grammar to regular expression conversion. + * Interface for regular grammar to regular expression conversion. */ class AbstractRGtoREConverter : public Conversion { @@ -27,14 +27,6 @@ public: virtual regexp::RegExp convert( void ) = 0; virtual ~AbstractRGtoREConverter( void ); - -protected: - AbstractRGtoREConverter( void ); - - /** - * New regular expression. - */ - regexp::RegExp m_re; }; } /* namespace conversions */ diff --git a/aconversions/src/rg2re/lrg2re/AbstractLRGtoREConverter.cpp b/aconversions/src/rg2re/lrg2re/AbstractLRGtoREConverter.cpp deleted file mode 100644 index 10e34c2d20eda3f79c716f373a143bf4358660ff..0000000000000000000000000000000000000000 --- a/aconversions/src/rg2re/lrg2re/AbstractLRGtoREConverter.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/* - * AbstractLRGtoREConverter.cpp - * - * Created on: 16. 3. 2014 - * Author: tomas - */ - -#include "AbstractLRGtoREConverter.h" - -using namespace grammar; - -namespace conversions -{ - -AbstractLRGtoREConverter::AbstractLRGtoREConverter( const LeftRegularGrammar & lrg ) : m_grammar( lrg ) -{ - -} - -AbstractLRGtoREConverter::~AbstractLRGtoREConverter( void ) -{ - -} - -} /* namespace conversions */ diff --git a/aconversions/src/rg2re/lrg2re/AbstractLRGtoREConverter.h b/aconversions/src/rg2re/lrg2re/AbstractLRGtoREConverter.h deleted file mode 100644 index f753cd70bdacefab245c3270582e3133a0ba4bdd..0000000000000000000000000000000000000000 --- a/aconversions/src/rg2re/lrg2re/AbstractLRGtoREConverter.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * AbstractLRGtoREConverter.h - * - * Created on: 16. 3. 2014 - * Author: tomas - */ - -#ifndef ABSTRACTLRGTORECONVERTER_H_ -#define ABSTRACTLRGTORECONVERTER_H_ - -#include <grammar/Regular/LeftRegularGrammar.h> - -#include "../AbstractRGtoREConverter.h" - -namespace conversions -{ - -class AbstractLRGtoREConverter: public AbstractRGtoREConverter -{ -public: - virtual ~AbstractLRGtoREConverter( void ); - -protected: - /** - * @param lrg source left reg. grammar. - */ - AbstractLRGtoREConverter( const grammar::LeftRegularGrammar & lrg ); - - /** - * source left reg. grammar. - */ - const grammar::LeftRegularGrammar m_grammar; -}; - -} /* namespace conversions */ - -#endif /* ABSTRACTLRGTORECONVERTER_H_ */ diff --git a/aconversions/src/rg2re/lrg2re/LRGAlgebraic.cpp b/aconversions/src/rg2re/lrg2re/LRGAlgebraic.cpp index d7036f06c30423de8de25085f6ebd533dda45d1b..b4fed3c3e3d470493fed1207a60442eb1a84e501 100644 --- a/aconversions/src/rg2re/lrg2re/LRGAlgebraic.cpp +++ b/aconversions/src/rg2re/lrg2re/LRGAlgebraic.cpp @@ -14,7 +14,7 @@ using namespace regexp; namespace conversions { -LRGAlgebraic::LRGAlgebraic( const LeftRegularGrammar & rg ) : AbstractLRGtoREConverter( rg ) +LRGAlgebraic::LRGAlgebraic( const LeftRegularGrammar & lrg ) : m_grammar( lrg ) { } @@ -26,30 +26,32 @@ LRGAlgebraic::~LRGAlgebraic( void ) RegExp LRGAlgebraic::convert( void ) { + LeftRegularEquationSolver solver; + for( const auto & symb : m_grammar.getNonTerminalSymbols( ) ) - m_solver.addSymbol( symb ); + solver.addSymbol( symb ); for( const auto & rule : m_grammar.getRules( ) ) { const Symbol & leftSide = rule.getLeftSide( ).front( ); if( rule.getRightSide( ).size( ) == 0 ) { - m_solver.addEquation( leftSide, new RegExpEpsilon( ) ); + solver.addEquation( leftSide, new RegExpEpsilon( ) ); } else if( rule.getRightSide( ).size( ) == 1 ) { const Symbol & input = rule.getRightSide( ).front( ); - m_solver.addEquation( leftSide, new RegExpSymbol( input.getSymbol( ) ) ); + solver.addEquation( leftSide, new RegExpSymbol( input.getSymbol( ) ) ); } else { const Symbol nt = rule.getRightSide( ).front( ); const Symbol input = rule.getRightSide( ).back( ); - m_solver.addEquation( leftSide, nt, new RegExpSymbol( input.getSymbol( ) ) ); + solver.addEquation( leftSide, nt, new RegExpSymbol( input.getSymbol( ) ) ); } } - return m_solver.solve( m_grammar.getStartSymbol( ) ); + return solver.solve( m_grammar.getStartSymbol( ) ); } } /* namespace conversions */ diff --git a/aconversions/src/rg2re/lrg2re/LRGAlgebraic.h b/aconversions/src/rg2re/lrg2re/LRGAlgebraic.h index ac9b41835fe0545d8113aa872deda8a4d422e73c..53e06251518a6288303f11b5c1d94968de639537 100644 --- a/aconversions/src/rg2re/lrg2re/LRGAlgebraic.h +++ b/aconversions/src/rg2re/lrg2re/LRGAlgebraic.h @@ -8,13 +8,16 @@ #ifndef LRGALGEBRAIC_H_ #define LRGALGEBRAIC_H_ +#include <grammar/Regular/LeftRegularGrammar.h> +#include <regexp/RegExp.h> + #include "../../shared/equations/LeftRegularEquationSolver.h" -#include "AbstractLRGtoREConverter.h" +#include "../AbstractRGtoREConverter.h" namespace conversions { -class LRGAlgebraic: public AbstractLRGtoREConverter +class LRGAlgebraic: public AbstractRGtoREConverter { public: /** @@ -31,9 +34,9 @@ public: protected: /** - * Equation solver. + * input grammar */ - LeftRegularEquationSolver m_solver; + const grammar::LeftRegularGrammar & m_grammar; }; } /* namespace conversions */ diff --git a/aconversions/src/rg2re/rrg2re/AbstractRRGtoREConverter.cpp b/aconversions/src/rg2re/rrg2re/AbstractRRGtoREConverter.cpp deleted file mode 100644 index 0a3c41ad1041a5f3772ccdc1e2f0623ff51cf325..0000000000000000000000000000000000000000 --- a/aconversions/src/rg2re/rrg2re/AbstractRRGtoREConverter.cpp +++ /dev/null @@ -1,25 +0,0 @@ -/* - * AbstractRRGtoREConverter.cpp - * - * Created on: 16. 3. 2014 - * Author: tomas - */ - -#include "AbstractRRGtoREConverter.h" - -using namespace grammar; - -namespace conversions -{ - -AbstractRRGtoREConverter::AbstractRRGtoREConverter( const RightRegularGrammar & rrg ) : m_grammar( rrg ) -{ - -} - -AbstractRRGtoREConverter::~AbstractRRGtoREConverter( void ) -{ - -} - -} /* namespace conversions */ diff --git a/aconversions/src/rg2re/rrg2re/AbstractRRGtoREConverter.h b/aconversions/src/rg2re/rrg2re/AbstractRRGtoREConverter.h deleted file mode 100644 index ce21d1c6fd93179cc4a894d9df1166a22c3661ee..0000000000000000000000000000000000000000 --- a/aconversions/src/rg2re/rrg2re/AbstractRRGtoREConverter.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * AbstractRRGtoREConverter.h - * - * Created on: 16. 3. 2014 - * Author: tomas - */ - -#ifndef ABSTRACTRRGTORECONVERTER_H_ -#define ABSTRACTRRGTORECONVERTER_H_ - -#include <grammar/Regular/RightRegularGrammar.h> - -#include "../AbstractRGtoREConverter.h" - -namespace conversions -{ - -class AbstractRRGtoREConverter: public AbstractRGtoREConverter -{ -public: - virtual ~AbstractRRGtoREConverter( void ); - -protected: - /** - * @param rrg Source right regular grammar. - */ - AbstractRRGtoREConverter( const grammar::RightRegularGrammar & rrg ); - - /** - * Source left regular grammar. - */ - const grammar::RightRegularGrammar m_grammar; -}; - -} /* namespace conversions */ - -#endif /* ABSTRACTRRGTORECONVERTER_H_ */ diff --git a/aconversions/src/rg2re/rrg2re/RRGAlgebraic.cpp b/aconversions/src/rg2re/rrg2re/RRGAlgebraic.cpp index 71c92816fea4335e0ac2e13bc94f9e3142cc396e..a1060197ca78d77d7cbf1ac78b2ea62b15f6a0c4 100644 --- a/aconversions/src/rg2re/rrg2re/RRGAlgebraic.cpp +++ b/aconversions/src/rg2re/rrg2re/RRGAlgebraic.cpp @@ -14,7 +14,7 @@ using namespace regexp; namespace conversions { -RRGAlgebraic::RRGAlgebraic( const RightRegularGrammar & rg ) : AbstractRRGtoREConverter( rg ) +RRGAlgebraic::RRGAlgebraic( const RightRegularGrammar & rrg ) : m_grammar( rrg ) { } @@ -26,30 +26,32 @@ RRGAlgebraic::~RRGAlgebraic( void ) RegExp RRGAlgebraic::convert( void ) { + RightRegularEquationSolver solver; + for( const auto & symb : m_grammar.getNonTerminalSymbols( ) ) - m_solver.addSymbol( symb ); + solver.addSymbol( symb ); for( const auto & rule : m_grammar.getRules( ) ) { const Symbol & leftSide = rule.getLeftSide( ).front( ); if( rule.getRightSide( ).size( ) == 0 ) { - m_solver.addEquation( leftSide, new RegExpEpsilon( ) ); + solver.addEquation( leftSide, new RegExpEpsilon( ) ); } else if( rule.getRightSide( ).size( ) == 1 ) { const Symbol & input = rule.getRightSide( ).front( ); - m_solver.addEquation( leftSide, new RegExpSymbol( input.getSymbol( ) ) ); + solver.addEquation( leftSide, new RegExpSymbol( input.getSymbol( ) ) ); } else { const Symbol input = rule.getRightSide( ).front( ); const Symbol nt = rule.getRightSide( ).back( ); - m_solver.addEquation( leftSide, nt, new RegExpSymbol( input.getSymbol( ) ) ); + solver.addEquation( leftSide, nt, new RegExpSymbol( input.getSymbol( ) ) ); } } - return m_solver.solve( m_grammar.getStartSymbol( ) ); + return solver.solve( m_grammar.getStartSymbol( ) ); } } /* namespace conversions */ diff --git a/aconversions/src/rg2re/rrg2re/RRGAlgebraic.h b/aconversions/src/rg2re/rrg2re/RRGAlgebraic.h index dd9d8ea1a260f1a794c0214d2dad5883f3803781..cb40fea3e417dea00c95b9bb323d266f37f78dd7 100644 --- a/aconversions/src/rg2re/rrg2re/RRGAlgebraic.h +++ b/aconversions/src/rg2re/rrg2re/RRGAlgebraic.h @@ -8,13 +8,16 @@ #ifndef RRGALGEBRAIC_H_ #define RRGALGEBRAIC_H_ +#include <grammar/Regular/RightRegularGrammar.h> +#include <regexp/RegExp.h> + #include "../../shared/equations/RightRegularEquationSolver.h" -#include "AbstractRRGtoREConverter.h" +#include "../AbstractRGtoREConverter.h" namespace conversions { -class RRGAlgebraic: public AbstractRRGtoREConverter +class RRGAlgebraic: public AbstractRGtoREConverter { public: /** @@ -31,9 +34,9 @@ public: protected: /** - * Equation solver. + * input grammar */ - RightRegularEquationSolver m_solver; + const grammar::RightRegularGrammar & m_grammar; }; } /* namespace conversions */ diff --git a/aconversions/src/rg2rg/lrg2rrg/AbstractLRGtoRRGConverter.cpp b/aconversions/src/rg2rg/lrg2rrg/AbstractLRGtoRRGConverter.cpp index 210887b68a09da9ed94f685ec30ee9d33c48f0fc..212a0b1e22adbbdcc353890ea2861aae6f321975 100644 --- a/aconversions/src/rg2rg/lrg2rrg/AbstractLRGtoRRGConverter.cpp +++ b/aconversions/src/rg2rg/lrg2rrg/AbstractLRGtoRRGConverter.cpp @@ -12,11 +12,6 @@ using namespace grammar; namespace conversions { -AbstractLRGtoRRGConverter::AbstractLRGtoRRGConverter( const LeftRegularGrammar & lrg ) : m_lrg( lrg ) -{ - -} - AbstractLRGtoRRGConverter::~AbstractLRGtoRRGConverter( void ) { diff --git a/aconversions/src/rg2rg/lrg2rrg/AbstractLRGtoRRGConverter.h b/aconversions/src/rg2rg/lrg2rrg/AbstractLRGtoRRGConverter.h index 9a5a3064b670c58c9ec049bb8a4c7f38d5248d20..bcbbe93676bb5f6f1342bf7f423f9aebf401fbe6 100644 --- a/aconversions/src/rg2rg/lrg2rrg/AbstractLRGtoRRGConverter.h +++ b/aconversions/src/rg2rg/lrg2rrg/AbstractLRGtoRRGConverter.h @@ -28,22 +28,6 @@ public: * @return right regular grammar which is equivalent to source left regular grammar. */ virtual grammar::RightRegularGrammar convert( void ) = 0; - -protected: - /** - * @param lrg Original left regular grammar. - */ - AbstractLRGtoRRGConverter( const grammar::LeftRegularGrammar & lrg ); - - /** - * Original left regular grammar. - */ - const grammar::LeftRegularGrammar m_lrg; - - /** - * New right regular grammar. - */ - grammar::RightRegularGrammar m_rrg; }; } /* namespace conversions */ diff --git a/aconversions/src/rg2rg/lrg2rrg/LeftToRightRegularGrammar.cpp b/aconversions/src/rg2rg/lrg2rrg/LeftToRightRegularGrammar.cpp index 80d5559eb2ca331b1feeb5e30b1b955957d1ab61..755cf1ba54afe6302bdf4577fcd2253bd940a47f 100644 --- a/aconversions/src/rg2rg/lrg2rrg/LeftToRightRegularGrammar.cpp +++ b/aconversions/src/rg2rg/lrg2rrg/LeftToRightRegularGrammar.cpp @@ -14,7 +14,7 @@ using namespace std; namespace conversions { -LeftToRightRegularGrammar::LeftToRightRegularGrammar( const grammar::LeftRegularGrammar & lrg ) : AbstractLRGtoRRGConverter( lrg ) +LeftToRightRegularGrammar::LeftToRightRegularGrammar( const grammar::LeftRegularGrammar & lrg ) : m_lrg( lrg ) { } @@ -26,15 +26,17 @@ LeftToRightRegularGrammar::~LeftToRightRegularGrammar( void ) RightRegularGrammar LeftToRightRegularGrammar::convert( void ) { + RightRegularGrammar rrg; + for( const auto & symbol : m_lrg.getNonTerminalSymbols( ) ) - m_rrg.addNonTerminalSymbol( symbol ); + rrg.addNonTerminalSymbol( symbol ); for( const auto & symbol : m_lrg.getTerminalSymbols( ) ) - m_rrg.addTerminalSymbol( symbol ); + rrg.addTerminalSymbol( symbol ); // 1. - Symbol s = m_rrg.createUniqueNonTerminalSymbol( m_lrg.getStartSymbol( ).getSymbol( ), false ); - m_rrg.setStartSymbol( s ); + Symbol s = rrg.createUniqueNonTerminalSymbol( m_lrg.getStartSymbol( ).getSymbol( ), false ); + rrg.setStartSymbol( s ); // 2. for( const auto & rule : m_lrg.getRules( ) ) @@ -43,38 +45,38 @@ RightRegularGrammar LeftToRightRegularGrammar::convert( void ) { list<Symbol> leftSide = { rule.getRightSide( ).front( ) }; list<Symbol> rightSide = { rule.getRightSide( ).back( ), rule.getLeftSide( ).front( ) }; - m_rrg.addRule( Rule( leftSide, rightSide ) ); + rrg.addRule( Rule( leftSide, rightSide ) ); if( rule.getLeftSide( ).front( ) == m_lrg.getStartSymbol( ) ) { list<Symbol> leftSide = { rule.getRightSide( ).front( ) }; list<Symbol> rightSide = { rule.getRightSide( ).back( ) }; - m_rrg.addRule( Rule( leftSide, rightSide ) ); + rrg.addRule( Rule( leftSide, rightSide ) ); } } else if( rule.getRightSide( ).size( ) == 1 ) { - list<Symbol> leftSide = { m_rrg.getStartSymbol( ) }; + list<Symbol> leftSide = { rrg.getStartSymbol( ) }; list<Symbol> rightSide = { rule.getRightSide( ).front( ), rule.getLeftSide( ).front( ) }; - m_rrg.addRule( Rule( leftSide, rightSide ) ); + rrg.addRule( Rule( leftSide, rightSide ) ); if( rule.getLeftSide( ).front( ) == m_lrg.getStartSymbol( ) ) { - list<Symbol> leftSide = { m_rrg.getStartSymbol( ) }; + list<Symbol> leftSide = { rrg.getStartSymbol( ) }; list<Symbol> rightSide = { rule.getRightSide( ).front( ) }; - m_rrg.addRule( Rule( leftSide, rightSide ) ); + rrg.addRule( Rule( leftSide, rightSide ) ); } } else { // assert leftSide == rrg.getStartSymbol( ) - list<Symbol> leftSide = { m_rrg.getStartSymbol( ) }; + list<Symbol> leftSide = { rrg.getStartSymbol( ) }; list<Symbol> rightSide; - m_rrg.addRule( Rule( leftSide, rightSide) ); + rrg.addRule( Rule( leftSide, rightSide) ); } } - return m_rrg; + return rrg; } } /* namespace conversions */ diff --git a/aconversions/src/rg2rg/lrg2rrg/LeftToRightRegularGrammar.h b/aconversions/src/rg2rg/lrg2rrg/LeftToRightRegularGrammar.h index 6feae684b94d8ace168a19db2e5874a13647e0d6..8e65008481d9592e865e93c4370acb051fb792bf 100644 --- a/aconversions/src/rg2rg/lrg2rrg/LeftToRightRegularGrammar.h +++ b/aconversions/src/rg2rg/lrg2rrg/LeftToRightRegularGrammar.h @@ -28,6 +28,12 @@ public: * @return right regular grammar which is equivalent to source left regular grammar. */ grammar::RightRegularGrammar convert( void ); + +protected: + /** + * input grammar + */ + const grammar::LeftRegularGrammar & m_lrg; }; } /* namespace conversions */ diff --git a/aconversions/src/rg2rg/rrg2lrg/AbstractRRGtoLRGConverter.cpp b/aconversions/src/rg2rg/rrg2lrg/AbstractRRGtoLRGConverter.cpp index 79857d39b0b2320847df3431c41478d9965bb0fb..c904f0c912e98c0a94364eae0a9f74e2720f63da 100644 --- a/aconversions/src/rg2rg/rrg2lrg/AbstractRRGtoLRGConverter.cpp +++ b/aconversions/src/rg2rg/rrg2lrg/AbstractRRGtoLRGConverter.cpp @@ -12,11 +12,6 @@ using namespace grammar; namespace conversions { -AbstractRRGtoLRGConverter::AbstractRRGtoLRGConverter( const RightRegularGrammar & rrg ) : m_rrg( rrg ) -{ - -} - AbstractRRGtoLRGConverter::~AbstractRRGtoLRGConverter( void ) { diff --git a/aconversions/src/rg2rg/rrg2lrg/AbstractRRGtoLRGConverter.h b/aconversions/src/rg2rg/rrg2lrg/AbstractRRGtoLRGConverter.h index 5cb8e7b38c46bd2557433a9e27ce8052fbee1444..ce556b30869e2a1fd722aac7eb5f581e16e4c58d 100644 --- a/aconversions/src/rg2rg/rrg2lrg/AbstractRRGtoLRGConverter.h +++ b/aconversions/src/rg2rg/rrg2lrg/AbstractRRGtoLRGConverter.h @@ -28,22 +28,6 @@ public: * @return left regular grammar which is equivalent to source right regular grammar. */ virtual grammar::LeftRegularGrammar convert( void ) = 0; - -protected: - /** - * @param rrg Source right regular grammar. - */ - AbstractRRGtoLRGConverter( const grammar::RightRegularGrammar & rrg ); - - /** - * Source right regular grammar. - */ - const grammar::RightRegularGrammar m_rrg; - - /** - * New left regular grammar. - */ - grammar::LeftRegularGrammar m_lrg; }; } /* namespace conversions */ diff --git a/aconversions/src/rg2rg/rrg2lrg/RightToLeftRegularGrammar.cpp b/aconversions/src/rg2rg/rrg2lrg/RightToLeftRegularGrammar.cpp index 35490051646cb43bad0ee5c55e624c7586cf1680..e90b736c458c3c8488dd21d099bfd91df926e86a 100644 --- a/aconversions/src/rg2rg/rrg2lrg/RightToLeftRegularGrammar.cpp +++ b/aconversions/src/rg2rg/rrg2lrg/RightToLeftRegularGrammar.cpp @@ -14,7 +14,7 @@ using namespace std; namespace conversions { -RightToLeftRegularGrammar::RightToLeftRegularGrammar( const RightRegularGrammar & rrg ) : AbstractRRGtoLRGConverter( rrg ) +RightToLeftRegularGrammar::RightToLeftRegularGrammar( const RightRegularGrammar & rrg ) : m_rrg( rrg ) { } @@ -26,15 +26,17 @@ RightToLeftRegularGrammar::~RightToLeftRegularGrammar( void ) LeftRegularGrammar RightToLeftRegularGrammar::convert( void ) { + LeftRegularGrammar lrg; + for( const auto & symbol : m_rrg.getNonTerminalSymbols( ) ) - m_lrg.addNonTerminalSymbol( symbol ); + lrg.addNonTerminalSymbol( symbol ); for( const auto & symbol : m_rrg.getTerminalSymbols( ) ) - m_lrg.addTerminalSymbol( symbol ); + lrg.addTerminalSymbol( symbol ); // 1. - Symbol s = m_lrg.createUniqueNonTerminalSymbol( m_rrg.getStartSymbol( ).getSymbol( ), false ); - m_lrg.setStartSymbol( s ); + Symbol s = lrg.createUniqueNonTerminalSymbol( m_rrg.getStartSymbol( ).getSymbol( ), false ); + lrg.setStartSymbol( s ); // 2. for( const auto & rule : m_rrg.getRules( ) ) @@ -43,38 +45,38 @@ LeftRegularGrammar RightToLeftRegularGrammar::convert( void ) { list<Symbol> leftSide = { rule.getRightSide( ).back( ) }; list<Symbol> rightSide = { rule.getLeftSide( ).front( ), rule.getRightSide( ).front( ) }; - m_lrg.addRule( Rule( leftSide, rightSide ) ); + lrg.addRule( Rule( leftSide, rightSide ) ); if( rule.getLeftSide( ).front( ) == m_rrg.getStartSymbol( ) ) { list<Symbol> leftSide = { rule.getRightSide( ).back( ) }; list<Symbol> rightSide = { rule.getRightSide( ).front( ) }; - m_lrg.addRule( Rule( leftSide, rightSide ) ); + lrg.addRule( Rule( leftSide, rightSide ) ); } } else if( rule.getRightSide( ).size( ) == 1 ) { - list<Symbol> leftSide = { m_lrg.getStartSymbol( ) }; + list<Symbol> leftSide = { lrg.getStartSymbol( ) }; list<Symbol> rightSide = { rule.getLeftSide( ).front( ), rule.getRightSide( ).front( ) }; - m_lrg.addRule( Rule( leftSide, rightSide ) ); + lrg.addRule( Rule( leftSide, rightSide ) ); if( rule.getLeftSide( ).front( ) == m_rrg.getStartSymbol( ) ) { - list<Symbol> leftSide = { m_lrg.getStartSymbol( ) }; + list<Symbol> leftSide = { lrg.getStartSymbol( ) }; list<Symbol> rightSide = { rule.getRightSide( ).front( ) }; - m_lrg.addRule( Rule( leftSide, rightSide ) ); + lrg.addRule( Rule( leftSide, rightSide ) ); } } else { // assert leftSide == rrg.getStartSymbol( ) - list<Symbol> leftSide = { m_lrg.getStartSymbol( ) }; + list<Symbol> leftSide = { lrg.getStartSymbol( ) }; list<Symbol> rightSide; - m_lrg.addRule( Rule( leftSide, rightSide) ); + lrg.addRule( Rule( leftSide, rightSide) ); } } - return m_lrg; + return lrg; } } /* namespace conversions */ diff --git a/aconversions/src/rg2rg/rrg2lrg/RightToLeftRegularGrammar.h b/aconversions/src/rg2rg/rrg2lrg/RightToLeftRegularGrammar.h index 0a956c7c11cf91668074dd334644b719aaf5c73b..23a94c318b7df7d95e884c6d327ce67b49e866a9 100644 --- a/aconversions/src/rg2rg/rrg2lrg/RightToLeftRegularGrammar.h +++ b/aconversions/src/rg2rg/rrg2lrg/RightToLeftRegularGrammar.h @@ -31,6 +31,12 @@ public: * @return left regular grammar which is equivalent to source right regular grammar. */ grammar::LeftRegularGrammar convert( void ); + +private: + /** + * + */ + const grammar::RightRegularGrammar & m_rrg; }; } /* namespace conversions */ diff --git a/aconversions/src/shared/Hexavigesimal.cpp b/aconversions/src/shared/Hexavigesimal.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77bb1f34124f1924f5a12b0b67b09253319a0d77 --- /dev/null +++ b/aconversions/src/shared/Hexavigesimal.cpp @@ -0,0 +1,31 @@ +/* + * Hexavigesimal.cpp + * + * Created on: 19. 4. 2014 + * Author: tomas + */ + +#include "Hexavigesimal.h" + +using namespace std; + +namespace conversions +{ + +string toBase26( int id ) +{ + // http://en.wikipedia.org/wiki/Hexavigesimal + + unsigned int n = id; + string name; + do + { + unsigned int remainder = n % 26; + name += ( char )( remainder + 'A' ); + n = (n - remainder) / 26; + } while (n > 0); + + return string( name.rbegin( ), name.rend( ) ); +} + +} /* namespace conversions */ diff --git a/aconversions/src/shared/Hexavigesimal.h b/aconversions/src/shared/Hexavigesimal.h new file mode 100644 index 0000000000000000000000000000000000000000..2c01e12d3d7580813ac086d7944e8670c4b51491 --- /dev/null +++ b/aconversions/src/shared/Hexavigesimal.h @@ -0,0 +1,24 @@ +/* + * Hexavigesimal.h + * + * Created on: 19. 4. 2014 + * Author: tomas + */ + +#ifndef HEXAVIGESIMAL_H_ +#define HEXAVIGESIMAL_H_ + +#include <string> + +namespace conversions +{ + +/** + * Maps 1 -> A, 2 -> B, ..., AA, AB, AC, ... , AAA, AAB, ... + * http://en.wikipedia.org/wiki/Hexavigesimal + */ +std::string toBase26( int n ); + +} /* namespace conversions */ + +#endif /* HEXAVIGESIMAL_H_ */