diff --git a/aconversions/src/rg2re/AbstractRGtoREConverter.cpp b/aconversions/src/rg2re/AbstractRGtoREConverter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95fb644460a43cbb7ffa6f820411f0b2cf6717e8 --- /dev/null +++ b/aconversions/src/rg2re/AbstractRGtoREConverter.cpp @@ -0,0 +1,25 @@ +/* + * AbstractRGtoREConverter.cpp + * + * Created on: 4. 3. 2014 + * Author: tomas + */ + +#include "AbstractRGtoREConverter.h" + +using namespace grammar; + +namespace conversions +{ + +AbstractRGtoREConverter::AbstractRGtoREConverter( const RegularGrammar & grammar ) : m_grammar( grammar ) +{ + +} + +AbstractRGtoREConverter::~AbstractRGtoREConverter( void ) +{ + +} + +} /* namespace conversions */ diff --git a/aconversions/src/rg2re/AbstractRGtoREConverter.h b/aconversions/src/rg2re/AbstractRGtoREConverter.h new file mode 100644 index 0000000000000000000000000000000000000000..7eda2748b1f772c9565264b9f3fb5ffcb3cf320f --- /dev/null +++ b/aconversions/src/rg2re/AbstractRGtoREConverter.h @@ -0,0 +1,34 @@ +/* + * AbstractRGtoREConverter.h + * + * Created on: 4. 3. 2014 + * Author: tomas + */ + +#ifndef ABSTRACTRGTORECONVERTER_H_ +#define ABSTRACTRGTORECONVERTER_H_ + +#include <grammar/Regular/RegularGrammar.h> +#include <regexp/RegExp.h> + +#include "../abstract/Conversion.h" + +namespace conversions +{ + +class AbstractRGtoREConverter : public Conversion +{ +public: + virtual regexp::RegExp convert( void ) = 0; + virtual ~AbstractRGtoREConverter( void ); + +protected: + AbstractRGtoREConverter( const grammar::RegularGrammar& grammar ); + + const grammar::RegularGrammar& m_grammar; + regexp::RegExp m_re; +}; + +} /* namespace conversions */ + +#endif /* ABSTRACTRGTORECONVERTER_H_ */ diff --git a/aconversions/src/rg2re/LRGAlgebraic.cpp b/aconversions/src/rg2re/LRGAlgebraic.cpp new file mode 100644 index 0000000000000000000000000000000000000000..909a47320af903155549af01def8b134af181fe4 --- /dev/null +++ b/aconversions/src/rg2re/LRGAlgebraic.cpp @@ -0,0 +1,55 @@ +/* + * LRGAlgebraic.cpp + * + * Created on: 4. 3. 2014 + * Author: tomas + */ + +#include "LRGAlgebraic.h" + +using namespace alphabet; +using namespace grammar; +using namespace regexp; + +namespace conversions +{ + +LRGAlgebraic::LRGAlgebraic( const LeftRegularGrammar & rg ) : AbstractRGtoREConverter( rg ) +{ + +} + +LRGAlgebraic::~LRGAlgebraic( void ) +{ + +} + +RegExp LRGAlgebraic::convert( void ) +{ + for( const auto & symb : m_grammar.getNonTerminalSymbols( ) ) + m_solver.addSymbol( symb ); + + for( const auto & rule : m_grammar.getRules( ) ) + { + const Symbol & leftSide = * rule.getLeftSide( ).begin( ); + if( rule.getRightSide( ).size( ) == 0 ) + { + m_solver.addEquation( leftSide, new RegExpEpsilon( ) ); + } + else if( rule.getRightSide( ).size( ) == 1 ) + { + const Symbol & input = * rule.getRightSide( ).begin( ); + m_solver.addEquation( leftSide, new RegExpSymbol( input.getSymbol( ) ) ); + } + else + { + const Symbol nt = * rule.getRightSide( ).begin( ); + const Symbol input = * ( std::next( rule.getRightSide( ).begin( ) ) ); + m_solver.addEquation( leftSide, nt, new RegExpSymbol( input.getSymbol( ) ) ); + } + } + + return m_solver.solve( m_grammar.getStartSymbol( ) ); +} + +} /* namespace conversions */ diff --git a/aconversions/src/rg2re/LRGAlgebraic.h b/aconversions/src/rg2re/LRGAlgebraic.h new file mode 100644 index 0000000000000000000000000000000000000000..09894ca22158fbd5f4f6935f32659ace36be0fdd --- /dev/null +++ b/aconversions/src/rg2re/LRGAlgebraic.h @@ -0,0 +1,33 @@ +/* + * LRGAlgebraic.h + * + * Created on: 4. 3. 2014 + * Author: tomas + */ + +#ifndef LRGALGEBRAIC_H_ +#define LRGALGEBRAIC_H_ + +#include <grammar/Regular/LeftRegularGrammar.h> +#include <regexp/RegExp.h> + +#include "../shared/LeftRegularEquationSolver.h" +#include "AbstractRGtoREConverter.h" + +namespace conversions +{ + +class LRGAlgebraic: public AbstractRGtoREConverter +{ +public: + LRGAlgebraic( const grammar::LeftRegularGrammar & rg ); + ~LRGAlgebraic( void ); + regexp::RegExp convert( void ); + +protected: + LeftRegularEquationSolver m_solver; +}; + +} /* namespace conversions */ + +#endif /* LRGALGEBRAIC_H_ */ diff --git a/aconversions/src/rg2re/RRGAlgebraic.cpp b/aconversions/src/rg2re/RRGAlgebraic.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d0f7cbdf4fc328eaa3d1821f623b610cabbdb91b --- /dev/null +++ b/aconversions/src/rg2re/RRGAlgebraic.cpp @@ -0,0 +1,55 @@ +/* + * RRGAlgebraic.cpp + * + * Created on: 4. 3. 2014 + * Author: tomas + */ + +#include "RRGAlgebraic.h" + +using namespace alphabet; +using namespace grammar; +using namespace regexp; + +namespace conversions +{ + +RRGAlgebraic::RRGAlgebraic( const RightRegularGrammar & rg ) : AbstractRGtoREConverter( rg ) +{ + +} + +RRGAlgebraic::~RRGAlgebraic( void ) +{ + +} + +RegExp RRGAlgebraic::convert( void ) +{ + for( const auto & symb : m_grammar.getNonTerminalSymbols( ) ) + m_solver.addSymbol( symb ); + + for( const auto & rule : m_grammar.getRules( ) ) + { + const Symbol & leftSide = * rule.getLeftSide( ).begin( ); + if( rule.getRightSide( ).size( ) == 0 ) + { + m_solver.addEquation( leftSide, new RegExpEpsilon( ) ); + } + else if( rule.getRightSide( ).size( ) == 1 ) + { + const Symbol & input = * rule.getRightSide( ).begin( ); + m_solver.addEquation( leftSide, new RegExpSymbol( input.getSymbol( ) ) ); + } + else + { + const Symbol input = * rule.getRightSide( ).begin( ); + const Symbol nt = * ( std::next( rule.getRightSide( ).begin( ) ) ); + m_solver.addEquation( leftSide, nt, new RegExpSymbol( input.getSymbol( ) ) ); + } + } + + return m_solver.solve( m_grammar.getStartSymbol( ) ); +} + +} /* namespace conversions */ diff --git a/aconversions/src/rg2re/RRGAlgebraic.h b/aconversions/src/rg2re/RRGAlgebraic.h new file mode 100644 index 0000000000000000000000000000000000000000..f96f407d1d683c18f94dd197644305200023f5fa --- /dev/null +++ b/aconversions/src/rg2re/RRGAlgebraic.h @@ -0,0 +1,33 @@ +/* + * RRGAlgebraic.h + * + * Created on: 4. 3. 2014 + * Author: tomas + */ + +#ifndef RRGALGEBRAIC_H_ +#define RRGALGEBRAIC_H_ + +#include <grammar/Regular/RightRegularGrammar.h> +#include <regexp/RegExp.h> + +#include "../shared/RightRegularEquationSolver.h" +#include "AbstractRGtoREConverter.h" + +namespace conversions +{ + +class RRGAlgebraic: public AbstractRGtoREConverter +{ +public: + RRGAlgebraic( const grammar::RightRegularGrammar & rg ); + ~RRGAlgebraic( void ); + regexp::RegExp convert( void ); + +protected: + RightRegularEquationSolver m_solver; +}; + +} /* namespace conversions */ + +#endif /* RRGALGEBRAIC_H_ */