Newer
Older
/*
* Brzozowski.cpp
*
* Created on: 11. 1. 2014
* Author: tomas
*/
#include "Brzozowski.h"
using namespace alib;
Brzozowski::Brzozowski( const RegExp & re ) : m_re( re )
RegExpOptimize opt;
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;
Qi.push_back( set<RegExp>( ) ); // initialize set Q_i
for( const auto & regexp : Qi.at( i - 1 ) )
RegExpDerivation deriv( regexp );
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 );
}
}
// ------------------------------------------------------------------------
FSM automaton;
int stateId = 0;
map<RegExp, State> stateMap;
for( const auto & r : Q )
stateMap.insert( std::pair<RegExp,State>( r, q ) );
automaton.addState( q );
}
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 );