17 #ifndef VCSN_ALGORITHMS_THOMPSON_HXX
18 # define VCSN_ALGORITHMS_THOMPSON_HXX
24 # include <vaucanson/automata/concept/automata_base.hh>
25 # include <vaucanson/misc/usual_macros.hh>
34 template <
class Auto_,
class Mono
id_,
class Semiring_>
35 class ThompsonVisitor :
36 public rat::ConstNodeVisitor<Monoid_, Semiring_>
39 typedef Auto_ automaton_t;
40 typedef typename automaton_t::hstate_t hstate_t;
41 typedef typename automaton_t::set_t automata_set_t;
42 typedef typename automaton_t::series_set_t series_set_t;
43 typedef typename automaton_t::series_set_elt_t series_set_elt_t;
44 typedef typename series_set_elt_t::semiring_elt_t semiring_elt_t;
45 typedef Monoid_ monoid_elt_value_t;
46 typedef Semiring_ semiring_elt_value_t;
47 typedef rat::Node<monoid_elt_value_t, semiring_elt_value_t> node_t;
51 ThompsonVisitor(automaton_t &a)
60 product(
const node_t* lhs,
const node_t* rhs)
65 std::pair<hstate_t, hstate_t> left_ini_fin = ini_fin_;
70 auto_.add_spontaneous(left_ini_fin.second, ini_fin_.first);
73 ini_fin_.first = left_ini_fin.first;
77 sum(
const node_t* lhs,
const node_t* rhs)
81 std::pair<hstate_t, hstate_t> left_ini_fin = ini_fin_;
82 hstate_t new_i = auto_.add_state();
83 hstate_t new_f = auto_.add_state();
85 auto_.add_spontaneous(new_i, left_ini_fin.first);
86 auto_.add_spontaneous(left_ini_fin.second, new_f);
90 auto_.add_spontaneous(new_i, ini_fin_.first);
91 auto_.add_spontaneous(ini_fin_.second, new_f);
93 ini_fin_.first = new_i;
94 ini_fin_.second = new_f;
98 star(
const node_t* node)
101 auto_.add_spontaneous(ini_fin_.second, ini_fin_.first);
103 hstate_t new_i = auto_.add_state();
104 hstate_t new_f = auto_.add_state();
106 auto_.add_spontaneous(new_i, new_f);
107 auto_.add_spontaneous(new_i, ini_fin_.first);
108 auto_.add_spontaneous(ini_fin_.second, new_f);
110 ini_fin_.first = new_i;
111 ini_fin_.second = new_f;
115 left_weight(
const semiring_elt_value_t& w,
const node_t* node)
119 hstate_t new_i = auto_.add_state();
120 hstate_t new_f = auto_.add_state();
122 series_set_elt_t t = auto_.series().zero_;
123 t.assoc(auto_.series().monoid().identity(
SELECT(monoid_elt_value_t)), w);
124 auto_.add_series_transition(new_i, ini_fin_.first, t);
126 auto_.add_spontaneous(ini_fin_.second, new_f);
128 ini_fin_.first = new_i;
129 ini_fin_.second = new_f;
133 right_weight(
const semiring_elt_value_t& w,
const node_t* node)
137 hstate_t new_i = auto_.add_state();
138 hstate_t new_f = auto_.add_state();
140 auto_.add_spontaneous(new_i, ini_fin_.first);
142 series_set_elt_t t = auto_.series().zero_;
143 t.assoc(auto_.series().monoid().identity(
SELECT(monoid_elt_value_t)), w);
144 auto_.add_series_transition(ini_fin_.second, new_f, t);
146 ini_fin_.first = new_i;
147 ini_fin_.second = new_f;
151 constant(
const monoid_elt_value_t& m)
153 ini_fin_.first = auto_.add_state();
156 typename monoid_elt_value_t::const_iterator i = m.begin();
158 ini_fin_.second = auto_.add_state();
159 auto_.add_letter_transition(ini_fin_.first, ini_fin_.second, *i);
164 link_state = auto_.add_state();
165 auto_.add_spontaneous(ini_fin_.second, link_state);
166 ini_fin_.second = auto_.add_state();
167 auto_.add_letter_transition(link_state, ini_fin_.second, *i);
175 ini_fin_.first = auto_.add_state();
176 ini_fin_.second = auto_.add_state();
182 ini_fin_.first = auto_.add_state();
183 ini_fin_.second = auto_.add_state();
184 auto_.add_spontaneous(ini_fin_.first, ini_fin_.second);
187 const automaton_t &get_auto()
const
189 auto_.clear_initial();
191 auto_.set_initial(ini_fin_.first);
192 auto_.set_final(ini_fin_.second);
201 auto_.set_initial(ini_fin_.first);
202 auto_.set_final(ini_fin_.second);
207 std::pair<hstate_t, hstate_t> ini_fin_;
210 template <
typename A,
typename auto_t,
211 typename Letter,
typename Weight>
213 do_thompson_of(
const AutomataBase<A>&,
215 const rat::exp<Letter, Weight>& kexp)
218 ThompsonVisitor<auto_t, Letter, Weight> visitor(output);
222 kexp.accept(visitor);
226 template<
typename A,
typename T,
227 typename Letter,
typename Weight>
232 do_thompson_of(out.
structure(), out, kexp);
235 template <
typename AutoType,
typename S,
typename K,
typename T>
236 Element<Automata<S, K>, AutoType>
247 #endif // ! VCSN_ALGORITHMS_THOMPSON_HXX