diff --git a/alib2algo/src/rte/GlushkovTraversal.cpp b/alib2algo/src/rte/GlushkovTraversal.cpp index a9bcaf446794ab0872d8d255193ca2838444cda4..d5584be35f532a4819c805f3bcef102a90532d31 100644 --- a/alib2algo/src/rte/GlushkovTraversal.cpp +++ b/alib2algo/src/rte/GlushkovTraversal.cpp @@ -215,7 +215,7 @@ std::set < rte::FormalRTESymbol const * > GlushkovTraversal::first ( rte::Formal // ---------------------------------------------------------------------------- -std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::follow ( rte::FormalRTEElement const * const & node, rte::FormalRTESymbol const * const & symbolptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & subMap ) { +std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::follow ( rte::FormalRTEElement const * const & node, rte::FormalRTESymbol const * const & symbolptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > subMap ) { rte::FormalRTEAlternation const * const alternation = dynamic_cast < rte::FormalRTEAlternation const * const > ( node ); rte::FormalRTESubstitution const * const concatenation = dynamic_cast < rte::FormalRTESubstitution const * const > ( node ); rte::FormalRTEIteration const * const iteration = dynamic_cast < rte::FormalRTEIteration const * const > ( node ); @@ -240,7 +240,24 @@ std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::fol throw exception::CommonException ( "GlushkovTraversal::follow() - unknown RegExpElement node" ); } -std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::follow ( rte::FormalRTEAlternation const * const & node, rte::FormalRTESymbol const * const & symbolptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & subMap ) { +#include <iostream> +std::ostream & operator <<( std::ostream & os, const std::set < std::vector < rte::FormalRTESymbol const * > > & x ) { + os << "{"; + + for ( const auto & f : x ) { + os << "["; + + for ( const auto & ff : f ) + os << ff->getSymbol ( ).getSymbol ( ) << "(" << ff->getSymbol ( ).getRank ( ) << "), "; + + os << "], "; + } + + os << "}"; + return os; +} + +std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::follow ( rte::FormalRTEAlternation const * const & node, rte::FormalRTESymbol const * const & symbolptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > subMap ) { std::set < std::vector < rte::FormalRTESymbol const * > > ret, tmp; tmp = follow ( & node->getLeftElement ( ), symbolptr, subMap ); @@ -249,15 +266,20 @@ std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::fol tmp = follow ( & node->getRightElement ( ), symbolptr, subMap ); ret.insert ( tmp.begin ( ), tmp.end ( ) ); - std::cerr << "Follow Alt: (" << symbolptr->getSymbol ( ) << "): " << ret << std::endl; + std::cerr << "FollowAlt(" << symbolptr->getSymbol ( ) << "): " << ret << std::endl; return ret; } -std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::follow ( rte::FormalRTESubstitution const * const & node, rte::FormalRTESymbol const * const & symbolptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & subMap ) { +std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::follow ( rte::FormalRTESubstitution const * const & node, rte::FormalRTESymbol const * const & symbolptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > subMap ) { + + std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > subMap2 ( subMap ); + auto itMap = subMap2.find ( node->getSubstitutionSymbol ( ).getSymbol ( ) ); + + itMap->second.clear ( ); for ( const auto & s : first ( & node->getRightElement ( ) ) ) - subMap[node->getSubstitutionSymbol ( ).getSymbol ( )].insert ( s ); + itMap->second.insert ( s ); /* * E sub F @@ -268,15 +290,15 @@ std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::fol std::set < std::vector < rte::FormalRTESymbol const * > > ret; if ( pos ( & node->getLeftElement ( ), symbolptr ) ) - ret = follow ( & node->getLeftElement ( ), symbolptr, subMap ); + ret = follow ( & node->getLeftElement ( ), symbolptr, subMap2 ); else - ret = follow ( & node->getRightElement ( ), symbolptr, subMap ); + ret = follow ( & node->getRightElement ( ), symbolptr, subMap2 ); - std::cerr << "Follow Sub: (" << symbolptr->getSymbol ( ) << "): " << ret << std::endl; + std::cerr << "FollowSub(" << symbolptr->getSymbol ( ) << "): " << ret << std::endl; return ret; } -std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::follow ( rte::FormalRTEIteration const * const & node, rte::FormalRTESymbol const * const & symbolptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & subMap ) { +std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::follow ( rte::FormalRTEIteration const * const & node, rte::FormalRTESymbol const * const & symbolptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > subMap ) { std::set < std::vector < rte::FormalRTESymbol const * > > ret; @@ -285,7 +307,7 @@ std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::fol ret = follow ( & node->getElement ( ), symbolptr, subMap ); - std::cerr << "Follow Sub: (" << symbolptr->getSymbol ( ) << "): " << ret << std::endl; + std::cerr << "FollowIter(" << symbolptr->getSymbol ( ) << "): " << ret << std::endl; return ret; } @@ -334,16 +356,27 @@ std::set < std::vector < rte::FormalRTESymbol const * > > replaceConstants ( con ret = tmp; } + std::cerr << "ReplC for " << f << " rets:" << std::endl; + + for ( const auto & r : ret ) { + std::cerr << "\t"; + + for ( const auto & x : r ) + std::cerr << " " << * x; + + std::cerr << std::endl; + } + return ret; } -std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::follow ( rte::FormalRTESymbol const * const & node, rte::FormalRTESymbol const * const & symbolptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & subMap ) { +std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::follow ( rte::FormalRTESymbol const * const & node, rte::FormalRTESymbol const * const & symbolptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > subMap ) { std::set < std::vector < rte::FormalRTESymbol const * > > ret; std::vector < rte::FormalRTESymbol const * > children = node->getElements ( ); if ( symbolptr == node ) { ret = replaceConstants ( children, subMap ); - std::cerr << "Follow Symb1: (" << symbolptr->getSymbol ( ) << "): " << ret << std::endl; + std::cerr << "FollowSymbol1(" << symbolptr->getSymbol ( ) << "): " << ret << std::endl; return ret; } @@ -354,7 +387,7 @@ std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::fol return std::set < std::vector < rte::FormalRTESymbol const * > > ( ); } -std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::follow ( rte::FormalRTEEmpty const * const & /* node */, rte::FormalRTESymbol const * const & /* symbolptr */, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & /* subMap */ ) { +std::set < std::vector < rte::FormalRTESymbol const * > > GlushkovTraversal::follow ( rte::FormalRTEEmpty const * const & /* node */, rte::FormalRTESymbol const * const & /* symbolptr */, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > /* subMap */ ) { return std::set < std::vector < rte::FormalRTESymbol const * > > ( ); } @@ -414,15 +447,17 @@ bool GlushkovTraversal::pos ( rte::FormalRTEEmpty const * const & /* node */, rt // ---------------------------------------------------------------------------- std::set < GlushkovSymbol > GlushkovTraversal::getSymbols ( rte::FormalRTE const & re ) { - std::set < GlushkovSymbol > alphabet; + std::set < GlushkovSymbol > posSet; int i = 1; - getSymbols ( & re.getRTE ( ), alphabet, i ); + std::set < alphabet::RankedSymbol > alphabetF = re.getAlphabet ( ); + + getSymbols ( & re.getRTE ( ), alphabetF, posSet, i ); - return alphabet; + return posSet; } -void GlushkovTraversal::getSymbols ( rte::FormalRTEElement const * const & node, std::set < GlushkovSymbol > & alphabet, int & i ) { +void GlushkovTraversal::getSymbols ( rte::FormalRTEElement const * const & node, const std::set < alphabet::RankedSymbol > & alphabetF, std::set < GlushkovSymbol > & posSet, int & i ) { rte::FormalRTEAlternation const * const alternation = dynamic_cast < rte::FormalRTEAlternation const * const > ( node ); rte::FormalRTEIteration const * const iteration = dynamic_cast < rte::FormalRTEIteration const * const > ( node ); rte::FormalRTESubstitution const * const substitution = dynamic_cast < rte::FormalRTESubstitution const * const > ( node ); @@ -430,18 +465,23 @@ void GlushkovTraversal::getSymbols ( rte::FormalRTEElement const * const & node, rte::FormalRTEEmpty const * const empty = dynamic_cast < rte::FormalRTEEmpty const * const > ( node ); if ( symbol ) { - alphabet.insert ( GlushkovSymbol ( symbol, i++ ) ); + if ( alphabetF.count ( symbol->getSymbol ( ) ) ) + posSet.insert ( GlushkovSymbol ( symbol, i++ ) ); + + for ( const auto & c : symbol->getElements ( ) ) + getSymbols ( c, alphabetF, posSet, i ); + return; } else if ( alternation ) { - getSymbols ( & alternation->getLeftElement ( ), alphabet, i ); - getSymbols ( & alternation->getRightElement ( ), alphabet, i ); + getSymbols ( & alternation->getLeftElement ( ), alphabetF, posSet, i ); + getSymbols ( & alternation->getRightElement ( ), alphabetF, posSet, i ); return; } else if ( substitution ) { - getSymbols ( & substitution->getLeftElement ( ), alphabet, i ); - getSymbols ( & substitution->getRightElement ( ), alphabet, i ); + getSymbols ( & substitution->getLeftElement ( ), alphabetF, posSet, i ); + getSymbols ( & substitution->getRightElement ( ), alphabetF, posSet, i ); return; } else if ( iteration ) { - getSymbols ( & iteration->getElement ( ), alphabet, i ); + getSymbols ( & iteration->getElement ( ), alphabetF, posSet, i ); return; } else if ( empty ) { return; diff --git a/alib2algo/src/rte/GlushkovTraversal.h b/alib2algo/src/rte/GlushkovTraversal.h index 738cf70533f3da5281dcc291b1cc84784b15ba60..dcd8def54d6995655b2e9dd0f433e73754c270bc 100644 --- a/alib2algo/src/rte/GlushkovTraversal.h +++ b/alib2algo/src/rte/GlushkovTraversal.h @@ -70,7 +70,7 @@ private: */ static bool pos ( GlushkovSymbol const & symbol, rte::FormalRTE const * const & node ); - static void getSymbols ( rte::FormalRTEElement const * const & node, std::set < GlushkovSymbol > & alphabet, int & i ); + static void getSymbols ( rte::FormalRTEElement const * const & node, const std::set < alphabet::RankedSymbol > & alphabetF, std::set < GlushkovSymbol > & posSet, int & i ); static std::set < rte::FormalRTESymbol const * > first ( rte::FormalRTEElement const * const & node ); static std::set < rte::FormalRTESymbol const * > first ( rte::FormalRTEAlternation const * const & node ); @@ -79,13 +79,6 @@ private: static std::set < rte::FormalRTESymbol const * > first ( rte::FormalRTESymbol const * const & node ); static std::set < rte::FormalRTESymbol const * > first ( rte::FormalRTEEmpty const * const & node ); - static std::set < rte::FormalRTESymbol const * > last ( rte::FormalRTEElement const * const & node ); - static std::set < rte::FormalRTESymbol const * > last ( rte::FormalRTEAlternation const * const & node ); - static std::set < rte::FormalRTESymbol const * > last ( rte::FormalRTESubstitution const * const & node ); - static std::set < rte::FormalRTESymbol const * > last ( rte::FormalRTEIteration const * const & node ); - static std::set < rte::FormalRTESymbol const * > last ( rte::FormalRTESymbol const * const & node ); - static std::set < rte::FormalRTESymbol const * > last ( rte::FormalRTEEmpty const * const & node ); - static bool pos ( rte::FormalRTEElement const * const & node, rte::FormalRTESymbol const * const & symbSearch ); static bool pos ( rte::FormalRTEAlternation const * const & node, rte::FormalRTESymbol const * const & symbSearch ); static bool pos ( rte::FormalRTESubstitution const * const & node, rte::FormalRTESymbol const * const & symbSearch ); @@ -93,12 +86,12 @@ private: static bool pos ( rte::FormalRTESymbol const * const & node, rte::FormalRTESymbol const * const & symbSearch ); static bool pos ( rte::FormalRTEEmpty const * const & node, rte::FormalRTESymbol const * const & symbSearch ); - static std::set < std::vector < rte::FormalRTESymbol const * > > follow ( rte::FormalRTEElement const * const & node, rte::FormalRTESymbol const * const & symbolptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & subMap ); - static std::set < std::vector < rte::FormalRTESymbol const * > > follow ( rte::FormalRTEAlternation const * const & node, rte::FormalRTESymbol const * const & symbolptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & subMap ); - static std::set < std::vector < rte::FormalRTESymbol const * > > follow ( rte::FormalRTESubstitution const * const & node, rte::FormalRTESymbol const * const & symbolptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & subMap ); - static std::set < std::vector < rte::FormalRTESymbol const * > > follow ( rte::FormalRTEIteration const * const & node, rte::FormalRTESymbol const * const & symbolptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & subMap ); - static std::set < std::vector < rte::FormalRTESymbol const * > > follow ( rte::FormalRTESymbol const * const & node, rte::FormalRTESymbol const * const & symbolptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & subMap ); - static std::set < std::vector < rte::FormalRTESymbol const * > > follow ( rte::FormalRTEEmpty const * const & node, rte::FormalRTESymbol const * const & symbolptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > & subMap ); + static std::set < std::vector < rte::FormalRTESymbol const * > > follow ( rte::FormalRTEElement const * const & node, rte::FormalRTESymbol const * const & symbptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > subM ); + static std::set < std::vector < rte::FormalRTESymbol const * > > follow ( rte::FormalRTEAlternation const * const & node, rte::FormalRTESymbol const * const & symbptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > subM ); + static std::set < std::vector < rte::FormalRTESymbol const * > > follow ( rte::FormalRTESubstitution const * const & node, rte::FormalRTESymbol const * const & symbptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > subM ); + static std::set < std::vector < rte::FormalRTESymbol const * > > follow ( rte::FormalRTEIteration const * const & node, rte::FormalRTESymbol const * const & symbptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > subM ); + static std::set < std::vector < rte::FormalRTESymbol const * > > follow ( rte::FormalRTESymbol const * const & node, rte::FormalRTESymbol const * const & symbptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > subM ); + static std::set < std::vector < rte::FormalRTESymbol const * > > follow ( rte::FormalRTEEmpty const * const & node, rte::FormalRTESymbol const * const & symbptr, std::map < alphabet::RankedSymbol, std::set < rte::FormalRTESymbol const * > > subM ); }; } /* namespace rte */ diff --git a/examples2/rte/rte4.xml b/examples2/rte/rte4.xml new file mode 100644 index 0000000000000000000000000000000000000000..f861e27bd5bc9bf1557fbe25c5e93c53a6fb8b9e --- /dev/null +++ b/examples2/rte/rte4.xml @@ -0,0 +1,38 @@ +<FormalRTE> + <alphabet> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>a</Character></PrimitiveLabel></LabeledSymbol><Unsigned>2</Unsigned></RankedSymbol> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>b</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol> + </alphabet> + <constantAlphabet> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>z</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol> + </constantAlphabet> + + <substitution> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>z</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol> + + <iteration> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>z</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol> + + <symbol> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>a</Character></PrimitiveLabel></LabeledSymbol><Unsigned>2</Unsigned></RankedSymbol> + <symbol> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>z</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol> + </symbol> + <symbol> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>z</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol> + </symbol> + </symbol> + </iteration> + + <symbol> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>a</Character></PrimitiveLabel></LabeledSymbol><Unsigned>2</Unsigned></RankedSymbol> + <symbol> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>b</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol> + </symbol> + <symbol> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>b</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol> + </symbol> + </symbol> + </substitution> +</FormalRTE> + diff --git a/examples2/rte/rte5.xml b/examples2/rte/rte5.xml new file mode 100644 index 0000000000000000000000000000000000000000..757ca807ebf98cf96a902aa0b054001553e6d60b --- /dev/null +++ b/examples2/rte/rte5.xml @@ -0,0 +1,31 @@ +<FormalRTE> + <alphabet> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>a</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>b</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol> + </alphabet> + <constantAlphabet> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>z</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol> + </constantAlphabet> + + <substitution> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>z</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol> + + <substitution> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>z</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol> + + <symbol> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>z</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol> + </symbol> + + <symbol> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>a</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol> + </symbol> + </substitution> + + + <symbol> + <RankedSymbol><LabeledSymbol><PrimitiveLabel><Character>b</Character></PrimitiveLabel></LabeledSymbol><Unsigned>0</Unsigned></RankedSymbol> + </symbol> + </substitution> +</FormalRTE> +