Awali
Another Weighted Automata library
print_exp.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_ALGOS_PRINT_EXP_HH
18 # define AWALI_ALGOS_PRINT_EXP_HH
19 
21 #include <awali/sttc/ctx/fwd.hh>
22 #include <awali/sttc/misc/raise.hh>
23 # include <stack>
24 # include <iostream>
25 
26 # if DEBUG
27 # define DEBUG_IFELSE(Then, Else) Then
28 # else
29 # define DEBUG_IFELSE(Then, Else) Else
30 # endif
31 
32 # define DEBUG_IF(Then) DEBUG_IFELSE(Then,)
33 
34 namespace awali {
35  namespace sttc {
36 
37 
38  /*,----------------.
39  | print(ratexp). |
40  `----------------'*/
41 
42  namespace rat
43  {
44  template <typename RatExpSet>
46  : public RatExpSet::const_visitor
47  {
48  public:
49  using ratexpset_t = RatExpSet;
52  using ratexp_t = typename ratexpset_t::value_t;
54  using super_type = typename ratexpset_t::const_visitor;
55  using node_t = typename super_type::node_t;
56 
57  constexpr static const char* me() { return "print-exp"; }
58 
59  print_visitor(const ratexpset_t& rs, std::ostream& o, bool with_dot)
60  : rs_(rs), stream_(o), with_dot_(with_dot)
61  {}
62 
63  std::ostream& operator()(const ratexp_t& v)
64  {
65  v->accept(*this);
66  return stream_;
67  }
68 
71 
73  {
74  stream_ << "\\z";
75  }
76 
78  {
79  stream_ << "\\e";
80  }
81 
83  {
84  ls_.print(e.value(), stream_);
85  }
86 
88  {
89  for (unsigned i = 0, n = e.size(); i < n; ++i) {
90  if(i>0)
91  stream_ << '+';
92  const auto& v = e[i];
93  if(i>0 && v->type()==type_t::sum)
94  stream_ << '(';
95  v->accept(*this);
96  if(i>0 && v->type()==type_t::sum)
97  stream_ << ')';
98  }
99  }
100 
102  {
103  for (unsigned i = 0, n = e.size(); i < n; ++i) {
104  const auto& v = e[i];
105  if(i>0 && with_dot_)
106  stream_ << '.';
107  if(v->type()==type_t::sum || (i>0 && v->type()==type_t::prod))
108  stream_ << '(';
109  v->accept(*this);
110  if(v->type()==type_t::sum || (i>0 && v->type()==type_t::prod))
111  stream_ << ')';
112  }
113  }
114 
116  {
117  char c='(';
118  for (unsigned i = 0, n = e.size(); i < n; ++i) {
119  stream_ << c;
120  c='&';
121  const auto& v = e[i];
122  v->accept(*this);
123  }
124  stream_ << ')';
125  }
126 
128  {
129  char c='(';
130  for (unsigned i = 0, n = e.size(); i < n; ++i) {
131  stream_ << c;
132  c='$';
133  const auto& v = e[i];
134  v->accept(*this);
135  }
136  stream_ << ')';
137  }
138 
140  {
141  stream_ << '(';
142  e.sub()->accept(*this);
143  stream_ << "){c}";
144  }
145 
147  {
148  const auto& v = e.sub();
149  if(v->type()==type_t::sum || v->type()==type_t::prod)
150  stream_ << '(';
151  v->accept(*this);
152  if(v->type()==type_t::sum || v->type()==type_t::prod)
153  stream_ << ')';
154  stream_ << '*';
155  }
156 
158  {
159  const auto& v = e.sub();
160  stream_ << '<';
161  ws_.print(e.weight(), stream_);
162  stream_ << '>';
163  if( v->type()==type_t::sum
164  || v->type()==type_t::prod
165  || v->type()==type_t::star )
166  stream_ << '(';
167  v->accept(*this);
168  if( v->type()==type_t::sum
169  || v->type()==type_t::prod
170  || v->type()==type_t::star )
171  stream_ << ')';
172  }
173 
174  AWALI_RAT_VISIT(rweight, e)
175  {
176  const auto& v = e.sub();
177  if(v->type()==type_t::sum || v->type()==type_t::prod || v->type()==type_t::star)
178  stream_ << '(';
179  v->accept(*this);
180  if(v->type()==type_t::sum || v->type()==type_t::prod || v->type()==type_t::star)
181  stream_ << ')';
182  stream_ << '<';
183  ws_.print(e.weight(), stream_);
184  stream_ << '>';
185  }
186 
187  private:
188  ratexpset_t rs_;
189  std::ostream& stream_;
190  bool with_dot_;
192  const weightset_t& ws_ = *rs_.weightset();
193  const labelset_t& ls_ = *rs_.labelset();
194  };
195 
196  } // rat::
197 
198  template <typename RatExpSet>
199  inline
200  std::ostream&
201  print(const RatExpSet& rs,
202  const typename RatExpSet::ratexp_t& e,
203  std::ostream& o,
204  bool with_dot=false)
205  {
206  rat::print_visitor<RatExpSet> printer{rs,o,with_dot};
207  return printer(e);
208  }
209 
210 
211 }}//end of ns awali::stc
212 
213 #endif // !AWALI_ALGOS_PRINT_EXP_HH
The semiring of complex numbers.
Definition: c.hh:44
The semiring of Natural numbers.
Definition: n.hh:34
Definition: ratexp.hh:280
Definition: ratexp.hh:262
Definition: print_exp.hh:47
std::ostream & operator()(const ratexp_t &v)
Definition: print_exp.hh:63
print_visitor(const ratexpset_t &rs, std::ostream &o, bool with_dot)
Definition: print_exp.hh:59
AWALI_RAT_VISIT(prod, e)
Definition: print_exp.hh:101
AWALI_RAT_VISIT(conjunction, e)
Definition: print_exp.hh:115
context_t_of< ratexpset_t > context_t
Definition: print_exp.hh:50
constexpr static const char * me()
Definition: print_exp.hh:57
AWALI_RAT_VISIT(one,)
Definition: print_exp.hh:77
AWALI_RAT_VISIT(zero,)
Definition: print_exp.hh:72
AWALI_RAT_VISIT(shuffle, e)
Definition: print_exp.hh:127
RatExpSet ratexpset_t
Definition: print_exp.hh:49
AWALI_RAT_VISIT(atom, e)
Definition: print_exp.hh:82
AWALI_RAT_VISIT(lweight, e)
Definition: print_exp.hh:157
AWALI_RAT_VISIT(star, e)
Definition: print_exp.hh:146
labelset_t_of< context_t > labelset_t
Definition: print_exp.hh:51
AWALI_RAT_VISIT(complement, e)
Definition: print_exp.hh:139
typename ratexpset_t::const_visitor super_type
Definition: print_exp.hh:54
AWALI_RAT_VISIT(sum, e)
Definition: print_exp.hh:87
typename super_type::node_t node_t
Definition: print_exp.hh:55
weightset_t_of< ratexpset_t > weightset_t
Definition: print_exp.hh:53
typename ratexpset_t::value_t ratexp_t
Definition: print_exp.hh:52
Definition: ratexp.hh:176
An inner node with multiple children.
Definition: ratexp.hh:115
An inner node implementing a weight.
Definition: ratexp.hh:208
typename internal::context_t_of_impl< internal::base_t< ValueSet > >::type context_t_of
Helper to retrieve the type of the context of a value set.
Definition: traits.hh:66
typename internal::labelset_t_of_impl< internal::base_t< ValueSet > >::type labelset_t_of
Helper to retrieve the type of the labelset of a value set.
Definition: traits.hh:76
typename internal::weightset_t_of_impl< internal::base_t< ValueSet > >::type weightset_t_of
Helper to retrieve the type of the weightset of a value set.
Definition: traits.hh:86
Main namespace of Awali.
Definition: ato.hh:22
#define AWALI_RAT_UNSUPPORTED(Type)
Definition: visitor.hh:73