00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_EXP_PATTERN_HH
00018 # define VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_EXP_PATTERN_HH
00019
00020 # include <vaucanson/design_pattern/element.hh>
00021 # include <vaucanson/algebra/implementation/series/series.hh>
00022 # include <vaucanson/algebra/implementation/series/rat/dispatch_visitor.hh>
00023
00024 namespace vcsn {
00025
00026 namespace algebra {
00027
00028
00029
00030
00032
00035 template <class T, class U>
00036 struct BinaryOp
00037 {
00038 typedef T lhs_node_type;
00039 typedef U rhs_node_type;
00040
00041 BinaryOp();
00042 BinaryOp(const BinaryOp& b);
00043 BinaryOp(const T& lhs, const U& rhs);
00044 T& lhs();
00045 const T& lhs() const;
00046 U& rhs();
00047 const U& rhs() const;
00048
00049 private:
00050 T lhs_;
00051 U rhs_;
00052 };
00053
00054
00055
00056
00058
00061 template <class T>
00062 struct UnaryOp
00063 {
00064 typedef T value_type;
00065
00066 UnaryOp();
00067 UnaryOp(const UnaryOp& b);
00068 UnaryOp(const T& node);
00069 T& value();
00070 const T& value() const;
00071
00072 private:
00073 T node_;
00074 };
00075
00076
00077
00078
00080
00083 template <class T>
00084 struct Value
00085 {
00086 typedef T value_type;
00087
00088 Value();
00089 Value(const Value& v);
00090 Value(const T& v);
00091 const T& value() const;
00092 T& value();
00093
00094 private:
00095 T v_;
00096 };
00097
00098
00099
00100
00101
00109 template <class Self, class T, class U, class F>
00110 struct GenericMatcher
00111 {
00112 typedef U return_type;
00113
00121 U
00122 match(const T& ast);
00123
00124 protected:
00125 GenericMatcher();
00126 };
00127
00128 #define DecBinaryOp(N, T, U) \
00129 struct N : public vcsn::algebra::BinaryOp<T, U> \
00130 { \
00131 N(const T& lhs, const U& rhs) : vcsn::algebra::BinaryOp<T, U>(lhs, rhs) \
00132 {} \
00133 };
00134
00135 #define DecUnaryOp(N, T) \
00136 struct N : public vcsn::algebra::UnaryOp<T> \
00137 { \
00138 N(const T& node) : vcsn::algebra::UnaryOp<T>(node) \
00139 {} \
00140 };
00141
00142 #define DecLeaf(N, U) \
00143 struct N : public vcsn::algebra::Value<U> \
00144 { \
00145 N(const U& v) : \
00146 vcsn::algebra::Value<U>(v) \
00147 {} \
00148 };
00149
00150 #define DecFinalLeaf(N) \
00151 struct N \
00152 { \
00153 N() \
00154 {} \
00155 };
00156
00157 #define MATCH__(N, Lhs, Rhs) \
00158 return_type \
00159 match_node##N(const N& p____) \
00160 { \
00161 typename N::lhs_node_type Lhs = p____.lhs(); \
00162 typename N::rhs_node_type Rhs = p____.rhs();
00163
00164 #define MATCH_(N, Val) \
00165 return_type \
00166 match_node##N(const N& p____) \
00167 { \
00168 typename N::value_type Val(p____.value());
00169
00170 #define MATCH(N) \
00171 return_type \
00172 match_node##N(const N&) \
00173 {
00174
00175 #define END }
00176
00177
00178 template <class Self, class T, class U, class F>
00179 struct KRatExpMatcher : public GenericMatcher<Self, T, U, F>
00180 {
00181 typedef U return_type;
00182 typedef typename T::semiring_elt_value_t semiring_elt_value_t;
00183 typedef typename T::monoid_elt_value_t monoid_elt_value_t;
00184
00185 DecBinaryOp(Product, T, T);
00186 DecBinaryOp(Sum, T, T);
00187 DecUnaryOp(Star, T);
00188 DecBinaryOp(LeftWeight, semiring_elt_value_t, T);
00189 DecBinaryOp(RightWeight, T, semiring_elt_value_t);
00190 DecLeaf(Constant, monoid_elt_value_t);
00191 DecFinalLeaf(One);
00192 DecFinalLeaf(Zero);
00193
00194 protected:
00195 KRatExpMatcher() {}
00196 };
00197
00198 #define INHERIT_CONSTRUCTORS(Self, T, U, F) \
00199 typedef ::vcsn::algebra::KRatExpMatcher<Self, T, U, F> krat_exp_matcher_t; \
00200 typedef typename krat_exp_matcher_t::Product Product; \
00201 typedef typename krat_exp_matcher_t::Sum Sum; \
00202 typedef typename krat_exp_matcher_t::Star Star; \
00203 typedef typename krat_exp_matcher_t::LeftWeight LeftWeight; \
00204 typedef typename krat_exp_matcher_t::RightWeight RightWeight; \
00205 typedef typename krat_exp_matcher_t::Constant Constant; \
00206 typedef typename krat_exp_matcher_t::One One; \
00207 typedef typename krat_exp_matcher_t::Zero Zero;
00208
00209 template <class Self, class Series, class T, class Dispatch>
00210 struct KRatExpIdentity : vcsn::algebra::KRatExpMatcher<
00211 Self,
00212 T,
00213 Element<Series, T>,
00214 Dispatch
00215 >
00216 {
00217 typedef Self self_t;
00218 typedef Element<Series, T> return_type;
00219 typedef typename Element<Series, T>::semiring_elt_t semiring_elt_t;
00220 typedef typename semiring_elt_t::value_t semiring_elt_value_t;
00221 typedef typename Element<Series, T>::monoid_elt_t monoid_elt_t;
00222 typedef typename monoid_elt_t::set_t monoid_t;
00223 typedef typename monoid_t::alphabet_t alphabet_t;
00224 typedef typename alphabet_t::letter_t letter_t;
00225 INHERIT_CONSTRUCTORS(self_t, T, semiring_elt_t, Dispatch);
00226
00227 KRatExpIdentity(const Element<Series, T>& exp) :
00228 exp_(exp)
00229 {}
00230
00231 MATCH__(Product, lhs, rhs)
00232 {
00233 return match(lhs) * match(rhs);
00234 }
00235 END
00236
00237 MATCH__(Sum, lhs, rhs)
00238 {
00239 return match(lhs) + match(rhs);
00240 }
00241 END
00242
00243 MATCH_(Star, e)
00244 {
00245 return_type r (match(e));
00246 r.star();
00247 return r;
00248 }
00249 END
00250
00251 MATCH__(LeftWeight, w, e)
00252 {
00253 semiring_elt_t welt (exp_.structure().semiring(), w);
00254 return welt * match(e);
00255 }
00256 END
00257
00258 MATCH__(RightWeight, e, w)
00259 {
00260 semiring_elt_t welt (exp_.structure().semiring(), w);
00261 return match(e) * welt;
00262 }
00263 END
00264
00265 MATCH_(Constant, m)
00266 {
00267 monoid_elt_t melt (exp_.structure().monoid(), m);
00268 return Element<Series, T> (exp_.structure(), melt);
00269 }
00270 END
00271
00272 MATCH(Zero)
00273 {
00274 return zero_as<T>::of(exp_.structure());
00275 }
00276 END
00277
00278 MATCH(One)
00279 {
00280 return identity_as<T>::of(exp_.structure());
00281 }
00282 END
00283
00284 protected:
00285 Element<Series, T> exp_;
00286 };
00287
00288
00289 }
00290
00291 }
00292
00293 # if !defined VCSN_USE_INTERFACE_ONLY || defined VCSN_USE_LIB
00294 # include <vaucanson/algebra/implementation/series/krat_exp_pattern.hxx>
00295 # endif // VCSN_USE_INTERFACE_ONLY
00296
00297 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SERIES_KRAT_EXP_PATTERN_HH