Skip to content
Snippets Groups Projects
Brzozowski.cpp 2.45 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     * Brzozowski.cpp
     *
     *  Created on: 11. 1. 2014
     *      Author: tomas
     */
    
    #include "Brzozowski.h"
    
    
    using namespace std;
    
    Tomáš Pecka's avatar
    Tomáš Pecka committed
    using namespace automaton;
    using namespace regexp;
    
    
    namespace conversions
    {
    
    
    Brzozowski::Brzozowski( const RegExp & re ) : m_re( re )
    
    Tomáš Pecka's avatar
    Tomáš Pecka committed
    Brzozowski::~Brzozowski( void )
    {
    
    }
    
    
    FSM Brzozowski::convert( void )
    
    Tomáš Pecka's avatar
    Tomáš Pecka committed
    {
    
        RegExp V = opt.optimize( m_re );
    
        set<alphabet::Symbol> alphabet = m_re.getAlphabet( );
    
        set<RegExp> Q = { V };
        deque<set<RegExp>> Qi;
    
        Qi.push_back( set<RegExp>( ) );
        Qi.at( 0 ).insert( V );
    
        int i = 1;
    
        while( ! Qi.at( i - 1 ).empty( ) )
    
            Qi.push_back( set<RegExp>( ) ); // initialize set Q_i
    
            for( const auto & regexp : Qi.at( i - 1 ) )
    
                RegExpDerivation deriv( regexp );
    
    Tomáš Pecka's avatar
    Tomáš Pecka committed
    
    
                for( const auto & a : alphabet )
    
                    RegExp derived = deriv.derivation( a );
    
                    derived = opt.optimize( derived );
    
                    // this will also add \emptyset as a regexp (and as FA state)
                    if( ! isInSet( derived, Q ) ) // if this state has already been found, do not add
                        Qi.at( i ).insert( derived );
    
    Jan Trávníček's avatar
    Jan Trávníček committed
            Q.insert( Qi.at( i ).begin( ), Qi.at( i ).end( ) );
    
        // ------------------------------------------------------------------------
    
        FSM automaton;
        int stateId = 0;
        map<RegExp, State> stateMap;
    
            State q( toBase26( stateId ++ ) );
    
            stateMap.insert( std::pair<RegExp,State>( r, q ) );
            automaton.addState( q );
        }
    
        for( const auto & a : alphabet )
    
            automaton.addInputSymbol( a.getSymbol( ) );
    
        for( const auto & r : Q )
        {
            RegExpDerivation deriv( r );
    
    
            for( const auto & a: automaton.getInputAlphabet( ) )
    
            {
                RegExp derived = deriv.derivation( a );
                derived = opt.optimize( derived );
    
    
                TransitionFSM t( stateMap.find( r )->second, a, stateMap.find( derived )->second );
                if( ! isInSet( t, automaton.getTransitions( ) ) )
                    automaton.addTransition( t );
    
        automaton.addInitialState( stateMap.find( V )->second );
    
        for( const auto & U : Q )
            if( U.containsEmptyString( ) )
    
                automaton.addFinalState( stateMap.find( U )->second );
    
        return automaton;
    
    } /* namespace conversions */