Awali
Another Weighted Automata library
printer.hxx
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_CORE_RAT_PRINTER_HXX
18 # define AWALI_CORE_RAT_PRINTER_HXX
19 
21 #include <awali/sttc/misc/indent.hh>
22 #include <awali/sttc/misc/raise.hh>
23 
24 namespace awali { namespace sttc
25 {
26  namespace rat
27  {
28 
29  inline
30  std::ostream&
31  operator<<(std::ostream& o, type_t t)
32  {
33  switch (t)
34  {
35 # define CASE(T) case type_t::T: o << #T; break
36  CASE(zero);
37  CASE(one);
38  CASE(atom);
39  CASE(sum);
40  CASE(prod);
41  CASE(ldiv);
43  CASE(shuffle);
44  CASE(star);
46  CASE(lweight);
47  CASE(rweight);
49 # undef CASE
50  }
51  return o;
52  }
53 
54  template <typename RatExpSet>
55  inline
57  std::ostream& out,
58  const bool debug)
59  : out_(out)
60  , ctx_(rs.context())
61  , identities_(rs.identities())
62  , debug_(debug)
63  {}
64 
65 
66 # define DEFINE \
67  template <typename RatExpSet> \
68  inline \
69  auto \
70  printer<RatExpSet>
71 
72  DEFINE::operator()(const node_t& v)
73  -> std::ostream&
74  {
75  static bool print = !! getenv("AWALI_PRINT");
76  static bool debug = !! getenv("AWALI_DEBUG");
77  if (print)
78  out_ << '<' << v.type() << '>' << sttc::incendl;
79  if (debug && format_ == "latex" && identities_ == identities_t::series)
80  out_ << "{\\color{red}{";
81  else if (debug && format_ == "latex" && identities_ == identities_t::trivial)
82  out_ << "{\\color{blue}{";
83  v.accept(*this);
84  if (debug && format_ == "latex" && identities_ == identities_t::series)
85  out_ << "}}";
86  else if (debug && format_ == "latex" && identities_ == identities_t::trivial)
87  out_ << "}}";
88  if (print)
89  out_ << sttc::decendl << "</" << v.type() << '>';
90  return out_;
91  }
92 
93  DEFINE::format(const std::string& format)
94  -> void
95  {
96  format_ = format;
97  if (format_ == "latex")
98  {
99  lgroup_ = "{";
100  rgroup_ = "}";
101  langle_ = " \\langle ";
102  rangle_ = " \\rangle ";
103  lparen_ = "\\left(";
104  rparen_ = "\\right)";
105  star_ = "^{*}";
106  complement_ = "^{c}";
107  transposition_ = "^{T}";
108  conjunction_ = " \\& ";
109  shuffle_ = " \\between ";
110  product_ = " \\, ";
111  sum_
112  = (identities_ == identities_t::series) ? " \\oplus " : " + ";
113  zero_ = "\\emptyset";
114  one_ = "\\varepsilon";
115  lmul_ = "\\,";
116  rmul_ = "\\,";
117  ldiv_ = " \\backslash ";
118  }
119  else if (format_ == "text")
120  {
121  lgroup_ = "";
122  rgroup_ = "";
123  langle_ = "<";
124  rangle_ = ">";
125  lparen_ = "(";
126  rparen_ = ")";
127  star_ = "*";
128  complement_ = "{c}";
129  transposition_ = "{T}";
130  conjunction_ = "&";
131  shuffle_ = ":";
132  product_ = "";
133  sum_ = "+";
134  zero_ = "\\z";
135  one_ = "\\e";
136  lmul_ = "";
137  rmul_ = "";
138  ldiv_ = "{\\}";
139  }
140  else
141  raise("invalid output format for ratexp: ", str_escape(format));
142  }
143 
144  DEFINE::precedence(const node_t& v) const
145  -> precedence_t
146  {
147  const atom_t* atom = dynamic_cast<const atom_t*>(&v);
148  if (atom && ! ctx_.labelset()->is_letter(atom->value()))
149  return precedence_t::word;
150  else
151  switch (v.type())
152  {
153 # define CASE(Type) \
154  case exp::type_t::Type: \
155  return precedence_t::Type;
156  CASE(atom);
157  CASE(complement);
158  CASE(conjunction);
159  CASE(ldiv);
160  CASE(lweight);
161  CASE(one);
162  CASE(prod);
163  CASE(rweight);
164  CASE(shuffle);
165  CASE(star);
166  CASE(sum);
168  CASE(zero);
169 # undef CASE
170  }
171  abort(); // Unreachable.
172  }
173 
174 # define VISIT(Type) \
175  DEFINE::visit(const Type ## _t& v) \
176  -> void
177 
178  VISIT(lweight)
179  {
180  out_ << langle_;
181  ctx_.weightset()->print(v.weight(), out_, format_);
182  out_ << rangle_ << lmul_;
183  print_child(*v.sub(), v);
184  }
185 
186  VISIT(rweight)
187  {
188  print_child(*v.sub(), v);
189  out_ << rmul_ << langle_;
190  ctx_.weightset()->print(v.weight(), out_, format_);
191  out_ << rangle_;
192  }
193 
194  VISIT(zero)
195  {
196  (void) v;
197  out_ << zero_;
198  }
199 
200  VISIT(one)
201  {
202  (void) v;
203  out_ << one_;
204  }
205 
206  VISIT(atom)
207  {
208  ctx_.labelset()->print(v.value(), out_, format_);
209  }
210 
211  DEFINE::print_child(const node_t& child, const node_t& parent)
212  -> void
213  {
214  static bool force = !! getenv("AWALI_PARENS");
215  bool parent_has_precedence = precedence(child) <= precedence(parent);
216  bool needs_parens =
217  (force
218  || (parent_has_precedence
219  && ! (parent.is_unary() && child.is_unary())));
220  if (needs_parens)
221  out_ << lparen_;
222  else if (parent.is_unary())
223  out_ << lgroup_;
224  operator()(child);
225  if (needs_parens)
226  out_ << rparen_;
227  else if (parent.is_unary())
228  out_ << rgroup_;
229  }
230 
231  template <typename RatExpSet>
232  template <type_t Type>
233  inline
234  auto
235  printer<RatExpSet>::print(const unary_t<Type>& v, const char* op)
236  -> void
237  {
238  print_child(*v.sub(), v);
239  out_ << op;
240  }
241 
242  template <typename RatExpSet>
243  template <type_t Type>
244  inline
245  auto
247  -> void
248  {
249  bool first = true;
250  for (auto i: n)
251  {
252  if (! first)
253  out_ << op;
254  print_child(*i, n);
255  first = false;
256  }
257  }
258 
259 # undef VISIT
260 # undef DEFINE
261 
262  } // namespace rat
263 }}//end of ns awali::stc
264 
265 #endif // !AWALI_CORE_RAT_PRINTER_HXX
carries the algebraic settings of automata
Definition: context.hh:40
The semiring of Natural numbers.
Definition: n.hh:34
Definition: ratexp.hh:280
const label_t & value() const
Definition: ratexp.hxx:53
Definition: ratexp.hh:262
Definition: printer.hh:36
typename super_type::node_t node_t
Definition: printer.hh:43
RatExpSet ratexpset_t
Definition: printer.hh:38
typename super_type::template unary_t< Type > unary_t
Definition: printer.hh:46
printer(const ratexpset_t &rs, std::ostream &out, const bool debug=!!getenv("AWALI_PARENS"))
Definition: printer.hxx:56
typename super_type::template variadic_t< Type > variadic_t
Definition: printer.hh:48
Definition: ratexp.hh:176
An inner node with multiple children.
Definition: ratexp.hh:115
An inner node implementing a weight.
Definition: ratexp.hh:208
weight_node< type_t::lweight, Label, Weight > lweight
Definition: fwd.hh:164
constant< type_t::zero, Label, Weight > zero
Definition: fwd.hh:113
type_t
The possible types of ratexps.
Definition: fwd.hh:48
weight_node< type_t::rweight, Label, Weight > rweight
Definition: fwd.hh:167
std::ostream & operator<<(std::ostream &o, type_t t)
Definition: printer.hxx:31
constant< type_t::one, Label, Weight > one
Definition: fwd.hh:116
identities
A ratexpset can implement several different sets of identities on expressions.
Definition: identities.hh:32
std::ostream & str_escape(std::ostream &os, const int c)
Definition: escape.hh:30
auto format(const ValueSet &vs, const typename ValueSet::value_t &v, Args &&... args) -> std::string
Format v via vs.print.
Definition: stream.hh:109
std::ostream & print(const RatExpSet &rs, const typename RatExpSet::ratexp_t &e, std::ostream &o, bool with_dot=false)
Definition: print_exp.hh:201
Main namespace of Awali.
Definition: ato.hh:22
#define CASE(T)
#define VISIT(Type)
Definition: printer.hxx:174