Skip to content
Snippets Groups Projects
Commit a7a2ef1f authored by Jan Trávníček's avatar Jan Trávníček
Browse files

use visitors in indexation of rtes for glushkov

parent 93c11c28
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "global/GlobalData.h" #include "global/GlobalData.h"
   
#include "../glushkov/GlushkovTraversal.h" #include "../glushkov/GlushkovTraversal.h"
#include "../glushkov/GlushkovIndexate.h"
   
#include <iterator> #include <iterator>
   
...@@ -41,7 +42,7 @@ bool isSubstSymbolPresent ( const std::set < std::ranked_symbol < > > & containe ...@@ -41,7 +42,7 @@ bool isSubstSymbolPresent ( const std::set < std::ranked_symbol < > > & containe
automaton::NPDA < > ToPostfixPushdownAutomatonGlushkov::convert ( const rte::FormalRTE < > & rte ) { automaton::NPDA < > ToPostfixPushdownAutomatonGlushkov::convert ( const rte::FormalRTE < > & rte ) {
   
// step 1; index RTE // step 1; index RTE
rte::FormalRTE < > indexedRTE = rte::GlushkovTraversal::index ( rte ); rte::FormalRTE < > indexedRTE = rte::GlushkovIndexate::index ( rte );
   
// step 2; compute: // step 2; compute:
// - first set // - first set
...@@ -117,12 +118,12 @@ automaton::NPDA < > ToPostfixPushdownAutomatonGlushkov::convert ( const rte::For ...@@ -117,12 +118,12 @@ automaton::NPDA < > ToPostfixPushdownAutomatonGlushkov::convert ( const rte::For
   
for ( const std::ranked_symbol < > & symb : indexedRTE.getAlphabet ( ) ) { for ( const std::ranked_symbol < > & symb : indexedRTE.getAlphabet ( ) ) {
if ( symb.getRank ( ) == primitive::Unsigned ( 0 ) ) if ( symb.getRank ( ) == primitive::Unsigned ( 0 ) )
automaton.addTransition ( q, rte::GlushkovTraversal::getSymbolFromGlushkovPair ( symb ).getSymbol ( ), { }, q, { alphabet::Symbol ( alphabet::RankedSymbol < > ( symb ) ) } ); automaton.addTransition ( q, rte::GlushkovIndexate::getSymbolFromGlushkovPair ( symb ).getSymbol ( ), { }, q, { alphabet::Symbol ( alphabet::RankedSymbol < > ( symb ) ) } );
else else
for ( const std::vector < std::ranked_symbol < > > & f : followSet[symb] ) { for ( const std::vector < std::ranked_symbol < > > & f : followSet[symb] ) {
std::vector < alphabet::Symbol > fstring = phi ( f ); std::vector < alphabet::Symbol > fstring = phi ( f );
std::reverse ( fstring.begin ( ), fstring.end ( ) ); std::reverse ( fstring.begin ( ), fstring.end ( ) );
automaton.addTransition ( q, rte::GlushkovTraversal::getSymbolFromGlushkovPair ( symb ).getSymbol ( ), fstring, q, { alphabet::Symbol ( alphabet::RankedSymbol < > ( symb ) ) } ); automaton.addTransition ( q, rte::GlushkovIndexate::getSymbolFromGlushkovPair ( symb ).getSymbol ( ), fstring, q, { alphabet::Symbol ( alphabet::RankedSymbol < > ( symb ) ) } );
} }
   
} }
......
/*
* GlushkovIndexate.cpp
*
* Created on: 14. 4. 2016
* Author: Tomas Pecka
*/
#include "GlushkovIndexate.h"
#include <rte/formal/FormalRTEElements.h>
#include <alphabet/SymbolPairSymbol.h>
namespace rte {
std::ranked_symbol < > GlushkovIndexate::getSymbolFromGlushkovPair ( const std::ranked_symbol < > & symbol ) {
const std::pair < alphabet::Symbol, alphabet::Symbol > & sps = ( ( const alphabet::SymbolPairSymbol & ) symbol.getSymbol ( ).getData ( ) ).getData ( );
return std::ranked_symbol < > ( sps.first, symbol.getRank ( ) );
}
FormalRTE < > GlushkovIndexate::index ( const rte::FormalRTE < > & rte ) {
int i = 1;
return FormalRTE < > ( FormalRTEStructure < alphabet::Symbol, primitive::Unsigned > ( rte.getRTE ( ).getStructure ( ).accept < std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > >, GlushkovIndexate::Formal > ( i ) ) );
}
std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > GlushkovIndexate::Formal::visit ( const rte::FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > & node, int & i ) {
alphabet::SymbolPairSymbol sps = alphabet::SymbolPairSymbol ( std::make_pair ( alphabet::Symbol ( node.getSymbol ( ).getSymbol ( ) ), alphabet::symbolFrom ( i++ ) ) );
FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > * ns = new FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > ( std::ranked_symbol < > ( alphabet::Symbol ( sps ), node.getSymbol ( ).getRank ( ) ) );
for ( const std::smart_ptr < const rte::FormalRTESymbol < alphabet::Symbol, primitive::Unsigned > > & e : node.getElements ( ) ) {
std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > child = e->accept < std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > >, GlushkovIndexate::Formal::Formal > ( i );
ns->appendElement ( * static_cast < FormalRTESymbol < alphabet::Symbol, primitive::Unsigned > * > ( child->clone ( ) ) ); // FIXME typecast
}
return std::rvalue_ref < FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > > ( ns );
}
std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > GlushkovIndexate::Formal::visit ( const rte::FormalRTESymbolSubst < alphabet::Symbol, primitive::Unsigned > & node, int & ) {
return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTESymbolSubst < alphabet::Symbol, primitive::Unsigned > ( node ) );
}
std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > GlushkovIndexate::Formal::visit ( const rte::FormalRTEAlternation < alphabet::Symbol, primitive::Unsigned > & node, int & i ) {
std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > left = node.getLeftElement ( ).accept < std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > >, GlushkovIndexate::Formal::Formal > ( i );
std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > right = node.getRightElement ( ).accept < std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > >, GlushkovIndexate::Formal::Formal > ( i );
return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTEAlternation < alphabet::Symbol, primitive::Unsigned > ( left, right ) );
}
std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > GlushkovIndexate::Formal::visit ( const rte::FormalRTESubstitution < alphabet::Symbol, primitive::Unsigned > & node, int & i ) {
std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > left = node.getLeftElement ( ).accept < std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > >, GlushkovIndexate::Formal::Formal > ( i );
std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > right = node.getRightElement ( ).accept < std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > >, GlushkovIndexate::Formal::Formal > ( i );
return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTESubstitution < alphabet::Symbol, primitive::Unsigned > ( left, right, node.getSubstitutionSymbol ( ) ) );
}
std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > GlushkovIndexate::Formal::visit ( const rte::FormalRTEIteration < alphabet::Symbol, primitive::Unsigned > & node, int & i ) {
std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > element = node.getElement ( ).accept < std::rvalue_ref < rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > >, GlushkovIndexate::Formal::Formal > ( i );
return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTEIteration < alphabet::Symbol, primitive::Unsigned > ( element, node.getSubstitutionSymbol ( ) ) );
}
std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > GlushkovIndexate::Formal::visit ( const rte::FormalRTEEmpty < alphabet::Symbol, primitive::Unsigned > &, int & ) {
return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTEEmpty < alphabet::Symbol, primitive::Unsigned > ( ) );
}
} /* namespace rte */
/*
* GlushkovIndexate.h
*
* Created on: 14. 4. 2016
* Author: Tomas Pecka
*/
#ifndef RTE_GLUSHKOV_INDEXATE_H_
#define RTE_GLUSHKOV_INDEXATE_H_
#include <rte/formal/FormalRTE.h>
#include <rte/RTEFeatures.h>
#include <alphabet/RankedSymbol.h>
namespace rte {
class GlushkovIndexate {
public:
/**
* @param re rte to index
* @return FormalRTE with indexed elements
*/
static FormalRTE < > index ( const rte::FormalRTE < > & re );
/**
* @param symbol Glushkov Pair symbol, i.e., SymbolPair of RankedSymbol < > and integer index
* @return RankedSymbol < > from the pair on input
*/
static std::ranked_symbol < > getSymbolFromGlushkovPair ( const std::ranked_symbol < > & symbol );
class Formal {
public:
static std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > visit ( const rte::FormalRTEAlternation < alphabet::Symbol, primitive::Unsigned > & node, int & i );
static std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > visit ( const rte::FormalRTESubstitution < alphabet::Symbol, primitive::Unsigned > & node, int & i );
static std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > visit ( const rte::FormalRTEIteration < alphabet::Symbol, primitive::Unsigned > & node, int & i );
static std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > visit ( const rte::FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > & node, int & i );
static std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > visit ( const rte::FormalRTESymbolSubst < alphabet::Symbol, primitive::Unsigned > & node, int & i );
static std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > visit ( const rte::FormalRTEEmpty < alphabet::Symbol, primitive::Unsigned > & node, int & i );
};
};
} /* namespace rte */
#endif /* RTE_GLUSHKOV_INDEXATE_H_ */
...@@ -12,14 +12,6 @@ ...@@ -12,14 +12,6 @@
   
namespace rte { namespace rte {
   
std::ranked_symbol < > GlushkovTraversal::getSymbolFromGlushkovPair ( const std::ranked_symbol < > & symbol ) {
const std::pair < alphabet::Symbol, alphabet::Symbol > sps = ( ( const alphabet::SymbolPairSymbol & ) symbol.getSymbol ( ).getData ( ) ).getData ( );
return std::ranked_symbol < > ( sps.first, symbol.getRank ( ) );
}
// -----------------------------------------------------------------------------
bool GlushkovTraversal::pos ( const std::ranked_symbol < > & symbol, const rte::FormalRTE < > & rte ) { bool GlushkovTraversal::pos ( const std::ranked_symbol < > & symbol, const rte::FormalRTE < > & rte ) {
return pos ( rte.getRTE ( ).getStructure ( ), symbol ); return pos ( rte.getRTE ( ).getStructure ( ), symbol );
} }
...@@ -351,45 +343,4 @@ bool GlushkovTraversal::pos ( const rte::FormalRTEEmpty < alphabet::Symbol, prim ...@@ -351,45 +343,4 @@ bool GlushkovTraversal::pos ( const rte::FormalRTEEmpty < alphabet::Symbol, prim
return false; return false;
} }
   
// ----------------------------------------------------------------------------
FormalRTE < > GlushkovTraversal::index ( const rte::FormalRTE < > & rte ) {
int i = 1;
return FormalRTE < > ( FormalRTEStructure < alphabet::Symbol, primitive::Unsigned > ( index ( rte, rte.getRTE ( ).getStructure ( ), i ) ) );
}
std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > GlushkovTraversal::index ( const rte::FormalRTE < > & rte, const rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > & node, int & i ) {
const rte::FormalRTEAlternation < alphabet::Symbol, primitive::Unsigned > * alternation = dynamic_cast < const rte::FormalRTEAlternation < alphabet::Symbol, primitive::Unsigned > * > ( & node );
const rte::FormalRTESubstitution < alphabet::Symbol, primitive::Unsigned > * substitution = dynamic_cast < const rte::FormalRTESubstitution < alphabet::Symbol, primitive::Unsigned > * > ( & node );
const rte::FormalRTEIteration < alphabet::Symbol, primitive::Unsigned > * iteration = dynamic_cast < const rte::FormalRTEIteration < alphabet::Symbol, primitive::Unsigned > * > ( & node );
const rte::FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > * symbol = dynamic_cast < const rte::FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > * > ( & node );
const rte::FormalRTESymbolSubst < alphabet::Symbol, primitive::Unsigned > * substSymbol = dynamic_cast < const rte::FormalRTESymbolSubst < alphabet::Symbol, primitive::Unsigned > * > ( & node );
const rte::FormalRTEEmpty < alphabet::Symbol, primitive::Unsigned > * empty = dynamic_cast < const rte::FormalRTEEmpty < alphabet::Symbol, primitive::Unsigned > * > ( & node );
if ( symbol ) {
alphabet::SymbolPairSymbol sps = alphabet::SymbolPairSymbol ( std::make_pair ( alphabet::Symbol ( symbol->getSymbol ( ).getSymbol ( ) ), alphabet::symbolFrom ( i++ ) ) );
FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > * ns = new FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > ( std::ranked_symbol < > ( alphabet::Symbol ( sps ), symbol->getSymbol ( ).getRank ( ) ) );
for ( const std::smart_ptr < const rte::FormalRTESymbol < alphabet::Symbol, primitive::Unsigned > > & e : symbol->getElements ( ) ) {
std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > child = index ( rte, * e.get ( ), i );
ns->appendElement ( * static_cast < FormalRTESymbol < alphabet::Symbol, primitive::Unsigned > * > ( child->clone ( ) ) ); // FIXME typecast
}
return std::rvalue_ref < FormalRTESymbolAlphabet < alphabet::Symbol, primitive::Unsigned > > ( ns );
} else if ( substSymbol ) {
return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTESymbolSubst < alphabet::Symbol, primitive::Unsigned > ( * substSymbol ) );
} else if ( alternation ) {
return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTEAlternation < alphabet::Symbol, primitive::Unsigned > ( index ( rte, alternation->getLeftElement ( ), i ), index ( rte, alternation->getRightElement ( ), i ) ) );
} else if ( substitution ) {
return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTESubstitution < alphabet::Symbol, primitive::Unsigned > ( index ( rte, substitution->getLeftElement ( ), i ), index ( rte, substitution->getRightElement ( ), i ), substitution->getSubstitutionSymbol ( ) ) );
} else if ( iteration ) {
return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTEIteration < alphabet::Symbol, primitive::Unsigned > ( index ( rte, iteration->getElement ( ), i ), iteration->getSubstitutionSymbol ( ) ) );
} else if ( empty ) {
return std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > ( new FormalRTEEmpty < alphabet::Symbol, primitive::Unsigned > ( ) );
} else {
throw exception::CommonException ( "GlushkovTraversal::index() - unknown rteElement node" );
}
}
} /* namespace rte */ } /* namespace rte */
...@@ -37,26 +37,12 @@ public: ...@@ -37,26 +37,12 @@ public:
*/ */
static std::set < std::vector < std::ranked_symbol < > > > follow ( const rte::FormalRTE < > & re, const std::ranked_symbol < > & symbol ); static std::set < std::vector < std::ranked_symbol < > > > follow ( const rte::FormalRTE < > & re, const std::ranked_symbol < > & symbol );
   
/**
* @param re rte to index
* @return FormalRTE with indexed elements
*/
static FormalRTE < > index ( const rte::FormalRTE < > & re );
/**
* @param symbol Glushkov Pair symbol, i.e., SymbolPair of RankedSymbol < > and integer index
* @return RankedSymbol < > from the pair on input
*/
static std::ranked_symbol < > getSymbolFromGlushkovPair ( const std::ranked_symbol < > & symbol );
private: private:
/** /**
* @return bool true if symbol pointer is in this subtree * @return bool true if symbol pointer is in this subtree
*/ */
static bool pos ( const std::ranked_symbol < > & symbol, const rte::FormalRTE < > & node ); static bool pos ( const std::ranked_symbol < > & symbol, const rte::FormalRTE < > & node );
   
static std::rvalue_ref < FormalRTEElement < alphabet::Symbol, primitive::Unsigned > > index ( const rte::FormalRTE < > & rte, const rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > & node, int & i );
static std::set < std::ranked_symbol < > > first ( const rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > & node ); static std::set < std::ranked_symbol < > > first ( const rte::FormalRTEElement < alphabet::Symbol, primitive::Unsigned > & node );
static std::set < std::ranked_symbol < > > first ( const rte::FormalRTEAlternation < alphabet::Symbol, primitive::Unsigned > & node ); static std::set < std::ranked_symbol < > > first ( const rte::FormalRTEAlternation < alphabet::Symbol, primitive::Unsigned > & node );
static std::set < std::ranked_symbol < > > first ( const rte::FormalRTESubstitution < alphabet::Symbol, primitive::Unsigned > & node ); static std::set < std::ranked_symbol < > > first ( const rte::FormalRTESubstitution < alphabet::Symbol, primitive::Unsigned > & node );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment