Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Concepts
ToFTAThompson.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <ext/algorithm>
9
10#include <global/GlobalData.h>
11
13
16
17namespace rte {
18
19namespace convert {
20
27 template < class SymbolType >
29 ext::vector < unsigned > substTargets;
30 for ( const std::pair < const ext::variant < unsigned, ext::pair < common::ranked_symbol < SymbolType >, ext::vector < unsigned > > >, unsigned > & transition : automaton.getTransitions ( ) ) {
31 if ( transition.first.template is < ext::pair < common::ranked_symbol < SymbolType >, ext::vector < unsigned > > > ( ) ) {
32 const ext::pair < common::ranked_symbol < SymbolType >, ext::vector < unsigned > > & source = transition.first.template get < ext::pair < common::ranked_symbol < SymbolType >, ext::vector < unsigned > > > ( );
33
34 if ( source.first == substitutionSymbol.getSymbol ( ) )
35 substTargets.push_back ( transition.second );
36 }
37 }
38 return substTargets;
39 }
40
41public:
52 template < class SymbolType >
54 unsigned nextState = 0;
55 const unsigned * tailArg = nullptr;
56
58 automaton.setInputAlphabet ( rte.getAlphabet ( ) );
59 automaton.addInputSymbols ( rte.getSubstitutionAlphabet ( ) );
60
61 rte.getRTE ( ).getStructure ( ).template accept < void, ToFTAThompson::Formal < SymbolType > > ( automaton, nextState, tailArg );
62
63 automaton.setInputAlphabet ( rte.getAlphabet ( ) );
64
65 automaton.addFinalState ( * tailArg );
66 return automaton;
67 }
68
69 template < class SymbolType >
70 class Formal {
71 public:
72 static void visit ( const FormalRTEAlternation < SymbolType > & node, automaton::EpsilonNFTA < SymbolType, unsigned > & automaton, unsigned & nextState, const unsigned * & tailArg );
73 static void visit ( const FormalRTESubstitution < SymbolType > & node, automaton::EpsilonNFTA < SymbolType, unsigned > & automaton, unsigned & nextState, const unsigned * & tailArg );
74 static void visit ( const FormalRTEIteration < SymbolType > & node, automaton::EpsilonNFTA < SymbolType, unsigned > & automaton, unsigned & nextState, const unsigned * & tailArg );
75 static void visit ( const FormalRTESymbolAlphabet < SymbolType > & node, automaton::EpsilonNFTA < SymbolType, unsigned > & automaton, unsigned & nextState, const unsigned * & tailArg );
76 static void visit ( const FormalRTESymbolSubst < SymbolType > & node, automaton::EpsilonNFTA < SymbolType, unsigned > & automaton, unsigned & nextState, const unsigned * & tailArg );
77 static void visit ( const FormalRTEEmpty < SymbolType > & node, automaton::EpsilonNFTA < SymbolType, unsigned > & automaton, unsigned & nextState, const unsigned * & tailArg );
78 };
79};
80
81template < class SymbolType >
83 unsigned state = nextState ++;
84 automaton.addState ( state );
85
86 node.getLeftElement ( ).template accept < void, ToFTAThompson::Formal < SymbolType > > ( automaton, nextState, tailArg );
87 automaton.addTransition ( * tailArg, state );
88
89 node.getRightElement ( ).template accept < void, ToFTAThompson::Formal < SymbolType > > ( automaton, nextState, tailArg );
90 automaton.addTransition ( * tailArg, state );
91
92 tailArg = & * automaton.getStates ( ).find ( state );
93}
94
95template < class SymbolType >
97 node.getLeftElement ( ).template accept < void, ToFTAThompson::Formal < SymbolType > > ( automaton, nextState, tailArg );
98 const unsigned * result = tailArg;
99
100 ext::vector < unsigned > substTargets = substitutionTargets ( node.getSubstitutionSymbol ( ), automaton );
101
102 for ( unsigned substTarget : substTargets ) {
103 automaton.removeTransition ( node.getSubstitutionSymbol ( ).getSymbol ( ), { }, substTarget);
104 }
105
106 node.getRightElement ( ).template accept < void, ToFTAThompson::Formal < SymbolType > > ( automaton, nextState, tailArg );
107
108 for ( unsigned substTarget : substTargets ) {
109 automaton.addTransition ( * tailArg, substTarget );
110 }
111
112 tailArg = result;
113}
114
115template < class SymbolType >
117 unsigned state = nextState ++;
118
119 node.getElement ( ).template accept < void, ToFTAThompson::Formal < SymbolType > > ( automaton, nextState, tailArg );
120
121 ext::vector < unsigned > substTargets = substitutionTargets ( node.getSubstitutionSymbol ( ), automaton );
122
123 automaton.addState ( state );
124 for ( unsigned substTarget : substTargets ) {
125 std::cout << "eps transition from " << * tailArg << " to " << substTarget << std::endl;
126 automaton.addTransition ( * tailArg, substTarget );
127 }
128
129 automaton.addTransition ( * tailArg, state );
130 automaton.addTransition ( node.getSubstitutionSymbol ( ).getSymbol ( ), { }, state );
131
132 tailArg = & * automaton.getStates ( ).find ( state );
133}
134
135template < class SymbolType >
137 unsigned state = nextState ++;
138 automaton.addState ( state );
140 for ( const rte::FormalRTEElement < SymbolType > & c : node.getElements ( ) ) {
141 c.template accept < void, ToFTAThompson::Formal < SymbolType > > ( automaton, nextState, tailArg );
142 from.push_back ( * tailArg );
143 }
144 automaton.addTransition ( node.getSymbol ( ), from, state );
145 tailArg = & * automaton.getStates ( ).find ( state );
146}
147
148template < class SymbolType >
150 unsigned state = nextState ++;
151 automaton.addState ( state );
152 automaton.addTransition ( node.getSymbol ( ), { }, state );
153 tailArg = & * automaton.getStates ( ).find ( state );
154}
155
156template < class SymbolType >
158 unsigned state = nextState ++;
159 automaton.addState ( state );
160 tailArg = & * automaton.getStates ( ).find ( state );
161}
162
163} /* namespace convert */
164
165} /* namespace rte */
Epsilon nondeterministic finite tree automaton. Accepts regular tree languages.
Definition: EpsilonNFTA.h:73
Definition: ranked_symbol.hpp:20
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Implementation of the variant class allowing to store any type of those listed in the template parame...
Definition: variant.hpp:98
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
Represents the alternation operator in the regular tree expression. The node must have exactly two ch...
Definition: FormalRTEAlternation.h:44
Definition: FormalRTEElement.h:54
Represents the empty expression in the regular tree expression. The node can't have any children.
Definition: FormalRTEEmpty.h:40
Represents the iteration operator in the regular tree expression. The node has exactly one child.
Definition: FormalRTEIteration.h:45
Represents the concatenation operator in the regular tree expression. The node must have exactly two ...
Definition: FormalRTESubstitution.h:44
Represents the terminal symbol in the regular tree expression. The number of children must be the sam...
Definition: FormalRTESymbolAlphabet.h:44
Represents the substitution symbol in the regular tree expression. The node can't have any children.
Definition: FormalRTESymbolSubst.h:43
const common::ranked_symbol< SymbolType > & getSymbol() const &
Definition: FormalRTESymbol.h:71
Formal regular tree expression represents regular tree expression. It describes regular tree language...
Definition: FormalRTE.h:71
Definition: ToFTAThompson.h:70
static void visit(const FormalRTEAlternation< SymbolType > &node, automaton::EpsilonNFTA< SymbolType, unsigned > &automaton, unsigned &nextState, const unsigned *&tailArg)
Definition: ToFTAThompson.h:82
Definition: ToFTAThompson.h:26
static automaton::EpsilonNFTA< SymbolType, unsigned > convert(const rte::FormalRTE< SymbolType > &rte)
Definition: ToFTAThompson.h:53
for(const StateType &state :fsm.getStates()) renamingData.insert(std Rename::RenamedAutomaton< T > result(renamingData.at(fsm.getInitialState()))
Definition: Rename.h:253
Definition: ToGrammar.h:31
Definition: converterCommon.hpp:8
ostream cout
Definition: Node.cpp:11
Definition: ToFTAGlushkov.h:22