acc.cc 33.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// -*- coding: utf-8 -*-
// Copyright (C) 2015 Laboratoire de Recherche et Développement de
// l'Epita.
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.


#include <iostream>
22
#include <sstream>
23
#include <set>
24
25
#include <cctype>
#include <cstring>
26
#include <map>
27
28
29
30
#include <spot/twa/acc.hh>
#include "spot/priv/bddalloc.hh"
#include <spot/misc/minato.hh>
#include <spot/misc/random.hh>
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

namespace spot
{
  std::ostream& operator<<(std::ostream& os, spot::acc_cond::mark_t m)
  {
    auto a = m.id;
    os << '{';
    unsigned level = 0;
    const char* comma = "";
    while (a)
      {
	if (a & 1)
	  {
	    os << comma << level;
	    comma = ",";
	  }
	a >>= 1;
	++level;
      }
    os << '}';
    return os;
  }

54
55
56
57
58
  std::ostream& operator<<(std::ostream& os, const acc_cond& acc)
  {
    return os << '(' << acc.num_sets() << ", " << acc.get_acceptance() << ')';
  }

59
60
  namespace
  {
61
62
63
64
65
66
    void default_set_printer(std::ostream& os, int v)
    {
      os << v;
    }

    template<bool html>
67
68
    static void
    print_code(std::ostream& os,
69
70
	       const acc_cond::acc_code& code, unsigned pos,
	       std::function<void(std::ostream&, int)> set_printer)
71
72
73
74
75
76
77
78
    {
      const char* op = " | ";
      auto& w = code[pos];
      const char* negated = "";
      bool top = pos == code.size() - 1;
      switch (w.op)
	{
	case acc_cond::acc_op::And:
79
	  op = html ? " &amp; " : " & ";
80
81
82
83
84
85
86
87
88
89
90
91
92
	case acc_cond::acc_op::Or:
	  {
	    unsigned sub = pos - w.size;
	    if (!top)
	      os << '(';
	    bool first = true;
	    while (sub < pos)
	      {
		--pos;
		if (first)
		  first = false;
		else
		  os << op;
93
		print_code<html>(os, code, pos, set_printer);
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
		pos -= code[pos].size;
	      }
	    if (!top)
	      os << ')';
	  }
	  break;
	case acc_cond::acc_op::InfNeg:
	  negated = "!";
	case acc_cond::acc_op::Inf:
	  {
	    auto a = code[pos - 1].mark.id;
	    if (a == 0U)
	      {
		os << 't';
	      }
	    else
	      {
111
112
113
		if (!top)
		  // Avoid extra parentheses if there is only one set
		  top = code[pos - 1].mark.count() == 1;
114
115
116
117
118
119
120
121
		unsigned level = 0;
		const char* and_ = "";
		if (!top)
		  os << '(';
		while (a)
		  {
		    if (a & 1)
		      {
122
123
124
125
			os << and_ << "Inf(" << negated;
			set_printer(os, level);
			os << ')';
			and_ = html ? "&amp;" : "&";
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
		      }
		    a >>= 1;
		    ++level;
		  }
		if (!top)
		  os << ')';
	      }
	  }
	  break;
	case acc_cond::acc_op::FinNeg:
	  negated = "!";
	case acc_cond::acc_op::Fin:
	  {
	    auto a = code[pos - 1].mark.id;
	    if (a == 0U)
	      {
		os << 'f';
	      }
	    else
	      {
146
147
148
		if (!top)
		  // Avoid extra parentheses if there is only one set
		  top = code[pos - 1].mark.count() == 1;
149
150
151
152
153
154
155
156
		unsigned level = 0;
		const char* or_ = "";
		if (!top)
		  os << '(';
		while (a)
		  {
		    if (a & 1)
		      {
157
158
159
			os << or_ << "Fin(" << negated;
			set_printer(os, level);
			os << ')';
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
			or_ = "|";
		      }
		    a >>= 1;
		    ++level;
		  }
		if (!top)
		  os << ')';
	      }
	  }
	  break;
	}
    }


    static bool
175
    eval(acc_cond::mark_t inf, const acc_cond::acc_word* pos)
176
    {
177
      switch (pos->op)
178
179
180
	{
	case acc_cond::acc_op::And:
	  {
181
	    auto sub = pos - pos->size;
182
183
184
	    while (sub < pos)
	      {
		--pos;
185
		if (!eval(inf, pos))
186
		  return false;
187
		pos -= pos->size;
188
189
190
191
192
	      }
	    return true;
	  }
	case acc_cond::acc_op::Or:
	  {
193
	    auto sub = pos - pos->size;
194
195
196
	    while (sub < pos)
	      {
		--pos;
197
		if (eval(inf, pos))
198
		  return true;
199
		pos -= pos->size;
200
201
202
203
	      }
	    return false;
	  }
	case acc_cond::acc_op::Inf:
204
	  return (pos[-1].mark & inf) == pos[-1].mark;
205
	case acc_cond::acc_op::Fin:
206
	  return (pos[-1].mark & inf) != pos[-1].mark;
207
208
209
210
211
212
213
	case acc_cond::acc_op::FinNeg:
	case acc_cond::acc_op::InfNeg:
	  SPOT_UNREACHABLE();
	}
      SPOT_UNREACHABLE();
      return false;
    }
214

215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
    static bool
    inf_eval(acc_cond::mark_t inf, const acc_cond::acc_word* pos)
    {
      switch (pos->op)
	{
	case acc_cond::acc_op::And:
	  {
	    auto sub = pos - pos->size;
	    while (sub < pos)
	      {
		--pos;
		if (!inf_eval(inf, pos))
		  return false;
		pos -= pos->size;
	      }
	    return true;
	  }
	case acc_cond::acc_op::Or:
	  {
	    auto sub = pos - pos->size;
	    while (sub < pos)
	      {
		--pos;
		if (inf_eval(inf, pos))
		  return true;
		pos -= pos->size;
	      }
	    return false;
	  }
	case acc_cond::acc_op::Inf:
	  return (pos[-1].mark & inf) == pos[-1].mark;
	case acc_cond::acc_op::Fin:
	  return true;
	case acc_cond::acc_op::FinNeg:
	case acc_cond::acc_op::InfNeg:
	  SPOT_UNREACHABLE();
	}
      SPOT_UNREACHABLE();
      return false;
    }

256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
    static acc_cond::mark_t
    eval_sets(acc_cond::mark_t inf, const acc_cond::acc_word* pos)
    {
      switch (pos->op)
	{
	case acc_cond::acc_op::And:
	  {
	    auto sub = pos - pos->size;
	    acc_cond::mark_t m = 0U;
	    while (sub < pos)
	      {
		--pos;
		if (auto s = eval_sets(inf, pos))
		  m |= s;
		else
		  return 0U;
		pos -= pos->size;
	      }
	    return m;
	  }
	case acc_cond::acc_op::Or:
	  {
	    auto sub = pos - pos->size;
	    while (sub < pos)
	      {
		--pos;
		if (auto s = eval_sets(inf, pos))
		  return s;
		pos -= pos->size;
	      }
	    return 0U;
	  }
	case acc_cond::acc_op::Inf:
	  if ((pos[-1].mark & inf) == pos[-1].mark)
	    return pos[-1].mark;
	  else
	    return 0U;
	case acc_cond::acc_op::Fin:
	case acc_cond::acc_op::FinNeg:
	case acc_cond::acc_op::InfNeg:
	  SPOT_UNREACHABLE();
	}
      SPOT_UNREACHABLE();
      return false;
    }
301
302
  }

303
  bool acc_cond::acc_code::accepting(mark_t inf) const
304
  {
305
    if (empty())
306
      return true;
307
    return eval(inf, &back());
308
309
  }

310
  bool acc_cond::acc_code::inf_satisfiable(mark_t inf) const
311
  {
312
    if (empty())
313
      return true;
314
    return inf_eval(inf, &back());
315
316
  }

317
318
319
320
321
322
323
324
325
326
  acc_cond::mark_t acc_cond::accepting_sets(mark_t inf) const
  {
    if (uses_fin_acceptance())
      throw std::runtime_error
	("Fin acceptance is not supported by this code path.");
    if (code_.empty())
      return 0U;
    return eval_sets(inf, &code_.back());
  }

327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  bool acc_cond::check_fin_acceptance() const
  {
    if (code_.empty())
      return false;
    unsigned pos = code_.size();
    do
      {
	switch (code_[pos - 1].op)
	  {
	  case acc_cond::acc_op::And:
	  case acc_cond::acc_op::Or:
	    --pos;
	    break;
	  case acc_cond::acc_op::Inf:
	  case acc_cond::acc_op::InfNeg:
	    pos -= 2;
	    break;
	  case acc_cond::acc_op::Fin:
345
346
347
348
349
	    if (code_[pos - 2].mark == 0U) // f
	      {
		pos -= 2;
		break;
	      }
350
351
352
353
354
355
356
357
	  case acc_cond::acc_op::FinNeg:
	    return true;
	  }
      }
    while (pos > 0);
    return false;
  }

358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416

  namespace
  {
    // Is Rabin or Streett, depending on highop and lowop.
    static bool
    is_rs(const acc_cond::acc_code& code,
	  acc_cond::acc_op highop,
	  acc_cond::acc_op lowop,
	  acc_cond::mark_t all_sets)
    {
      unsigned s = code.back().size;
      auto mainop = code.back().op;
      if (mainop == highop)
	{
	  // The size must be a multiple of 5.
	  if ((s != code.size() - 1) || (s % 5))
	    return false;
	}
      else			// Single pair?
	{
	  if (s != 4 || mainop != lowop)
	    return false;
	  // Pretend we where in a unary highop.
	  s = 5;
	}
      acc_cond::mark_t seen_fin = 0U;
      acc_cond::mark_t seen_inf = 0U;
      while (s)
	{
	  if (code[--s].op != lowop)
	    return false;
	  auto o1 = code[--s].op;
	  auto m1 = code[--s].mark;
	  auto o2 = code[--s].op;
	  auto m2 = code[--s].mark;

	  // We assume
	  //   Fin(n) lowop Inf(n+1)
	  //   o1 (m1)       o2 (m2)
	  // swap if it is the converse
	  if (o2 == acc_cond::acc_op::Fin)
	    {
	      std::swap(o1, o2);
	      std::swap(m1, m2);
	    }
	  if (o1 != acc_cond::acc_op::Fin
	      || o2 != acc_cond::acc_op::Inf
	      || m1.count() != 1
	      || m2.id != (m1.id << 1))
	    return false;
	  seen_fin |= m1;
	  seen_inf |= m2;
	}

      return (!(seen_fin & seen_inf)
	      && (seen_fin | seen_inf) == all_sets);
    }
  }

417
418
  int acc_cond::is_rabin() const
  {
419
    if (code_.is_f())
420
      return num_ == 0 ? 0 : -1;
421
    if ((num_ & 1) || code_.is_t())
422
      return -1;
423
424
425
426

    if (is_rs(code_, acc_op::Or, acc_op::And, all_sets()))
      return num_ / 2;
    else
427
428
429
430
431
      return -1;
  }

  int acc_cond::is_streett() const
  {
432
    if (code_.is_t())
433
      return num_ == 0 ? 0 : -1;
434
    if ((num_ & 1) || code_.is_f())
435
      return -1;
436
437
438
439

    if (is_rs(code_, acc_op::And, acc_op::Or, all_sets()))
      return num_ / 2;
    else
440
441
442
      return -1;
  }

443
  // PAIRS contains the number of Inf in each pair.
444
445
446
447
448
449
450
451
  bool acc_cond::is_generalized_rabin(std::vector<unsigned>& pairs) const
  {
    pairs.clear();
    if (is_generalized_co_buchi())
      {
	pairs.resize(num_);
	return true;
      }
452
    if (code_.is_t()
453
454
	|| code_.back().op != acc_op::Or)
      return false;
455

456
    auto s = code_.back().size;
457
458
459
460
461
    acc_cond::mark_t seen_fin = 0U;
    acc_cond::mark_t seen_inf = 0U;
    // Each pairs is the position of a Fin followed
    // by the number of Inf.
    std::map<unsigned, unsigned> p;
462
463
464
465
466
    while (s)
      {
	--s;
	if (code_[s].op == acc_op::And)
	  {
467
468
469
470
471
472
473
474
475
476
	    auto o1 = code_[--s].op;
	    auto m1 = code_[--s].mark;
	    auto o2 = code_[--s].op;
	    auto m2 = code_[--s].mark;

	    // We assume
	    //   Fin(n) & Inf({n+1,n+2,...,n+i})
	    //   o1 (m1)       o2 (m2)
	    // swap if it is the converse
	    if (o2 == acc_cond::acc_op::Fin)
477
	      {
478
479
		std::swap(o1, o2);
		std::swap(m1, m2);
480
	      }
481
482
483
484

	    if (o1 != acc_cond::acc_op::Fin
		|| o2 != acc_cond::acc_op::Inf
		|| m1.count() != 1)
485
	      return false;
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500

	    unsigned i = m2.count();
	    // If we have seen this pair already, it must have the
	    // same size.
	    if (p.emplace(m1.max_set(), i).first->second != i)
	      return false;
	    assert(i > 0);
	    unsigned j = m1.max_set(); // == n+1
	    do
	      if (!m2.has(j++))
		return false;
	    while (--i);

	    seen_fin |= m1;
	    seen_inf |= m2;
501
502
503
	  }
	else if (code_[s].op == acc_op::Fin)
	  {
504
	    auto m1 = code_[--s].mark;
505
506
507
508
509
	    for (auto s: m1.sets())
	      // If we have seen this pair already, it must have the
	      // same size.
	      if (p.emplace(s, 0U).first->second != 0U)
		return false;
510
	    seen_fin |= m1;
511
512
513
514
515
516
	  }
	else
	  {
	    return false;
	  }
      }
517
518
519
520
    for (auto i: p)
      pairs.push_back(i.second);
    return (!(seen_fin & seen_inf)
	    && (seen_fin | seen_inf) == all_sets());
521
522
  }

523
524
525
  acc_cond::acc_code
  acc_cond::acc_code::parity(bool max, bool odd, unsigned sets)
  {
526
527
528
529
530
531
532
    acc_cond::acc_code res;

    if (max)
      res = odd ? t() : f();
    else
      res = (sets & 1) == odd ? t() : f();

533
    if (sets == 0)
534
535
      return res;

536
537
538
539
540
541
542
543
544
545
    // When you look at something like
    //    acc-name: parity min even 5
    //    Acceptance: 5 Inf(0) | (Fin(1) & (Inf(2) | (Fin(3) & Inf(4))))
    // remember that we build it from right to left.
    int start = max ? 0 : sets - 1;
    int inc = max ? 1 : -1;
    int end = max ? sets : -1;
    for (int i = start; i != end; i += inc)
      {
	if ((i & 1) == odd)
546
	  res |= inf({(unsigned)i});
547
	else
548
	  res &= fin({(unsigned)i});
549
550
551
552
      }
    return res;
  }

553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
  acc_cond::acc_code
  acc_cond::acc_code::random(unsigned n_accs, double reuse)
  {
    // With 0 acceptance sets, we always generate the true acceptance.
    // (Working with false is somehow pointless, and the formulas we
    // generate for n_accs>0 are always satisfiable, so it makes sense
    // that it should be satisfiable for n_accs=0 as well.)
    if (n_accs == 0)
      return {};

    std::vector<acc_cond::acc_code> codes;
    codes.reserve(n_accs);
    for (unsigned i = 0; i < n_accs; ++i)
      {
	if (drand() < 0.5)
	  codes.push_back(inf({i}));
	else
	  codes.push_back(fin({i}));
	if (reuse > 0.0 && drand() < reuse)
	  --i;
      }

    int s = codes.size();
    while (s > 1)
      {
	// Pick a random code and put it at the end
	int p1 = mrand(s--);
	std::swap(codes[p1], codes[s]);
	// and another one
	int p2 = mrand(s);

	if (drand() < 0.5)
585
	  codes[p2] |= std::move(codes.back());
586
	else
587
	  codes[p2] &= std::move(codes.back());
588
589
590
591
592
593

	codes.pop_back();
      }
    return codes[0];
  }

594
595
  namespace
  {
596
    bdd to_bdd_rec(const acc_cond::acc_word* c, const bdd* map)
597
598
599
600
601
602
603
604
605
    {
      auto sz = c->size;
      auto start = c - sz - 1;
      auto op = c->op;
      switch (op)
	{
	case acc_cond::acc_op::Or:
	  {
	    --c;
606
	    bdd res = bddfalse;
607
608
	    do
	      {
609
		res |= to_bdd_rec(c, map);
610
611
612
613
614
615
616
617
		c -= c->size + 1;
	      }
	    while (c > start);
	    return res;
	  }
	case acc_cond::acc_op::And:
	  {
	    --c;
618
	    bdd res = bddtrue;
619
620
	    do
	      {
621
		res &= to_bdd_rec(c, map);
622
623
624
625
626
627
628
		c -= c->size + 1;
	      }
	    while (c > start);
	    return res;
	  }
	case acc_cond::acc_op::Fin:
	  {
629
	    bdd res = bddfalse;
630
	    for (auto i: c[-1].mark.sets())
631
632
	      res |= !map[i];
	    return res;
633
634
635
	  }
	case acc_cond::acc_op::Inf:
	  {
636
637
638
639
	    bdd res = bddtrue;
	    for (auto i: c[-1].mark.sets())
	      res &= map[i];
	    return res;
640
641
642
643
	  }
	case acc_cond::acc_op::InfNeg:
	case acc_cond::acc_op::FinNeg:
	  SPOT_UNREACHABLE();
644
	  return bddfalse;
645
646
	}
      SPOT_UNREACHABLE();
647
      return bddfalse;
648
    }
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676

    static bool
    equiv_codes(const acc_cond::acc_code& lhs,
		const acc_cond::acc_code& rhs)
    {
      auto used = lhs.used_sets() | rhs.used_sets();

      unsigned c = used.count();
      unsigned umax = used.max_set();

      bdd_allocator ba;
      int base = ba.allocate_variables(c);
      assert(base == 0);
      std::vector<bdd> r;
      for (unsigned i = 0; r.size() < umax; ++i)
	if (used.has(i))
	  r.push_back(bdd_ithvar(base++));
	else
	  r.push_back(bddfalse);
      return to_bdd_rec(&lhs.back(), &r[0]) == to_bdd_rec(&rhs.back(), &r[0]);
    }
  }

  bool acc_cond::is_parity(bool& max, bool& odd, bool equiv) const
  {
    unsigned sets = num_;
    if (sets == 0)
      {
677
	max = true;
678
	odd = is_t();
679
	return true;
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
      }
    acc_cond::mark_t u_inf;
    acc_cond::mark_t u_fin;
    std::tie(u_inf, u_fin) = code_.used_inf_fin_sets();

    odd = !u_inf.has(0);
    for (auto s: u_inf.sets())
      if ((s & 1) != odd)
	return false;

    auto max_code = acc_code::parity(true, odd, sets);
    if (max_code == code_)
      {
	max = true;
	return true;
      }
    auto min_code = acc_code::parity(false, odd, sets);
    if (min_code == code_)
      {
	max = false;
	return true;
      }

    if (!equiv)
      return false;

    if (equiv_codes(code_, max_code))
      {
	max = true;
	return true;
      }
    if (equiv_codes(code_, min_code))
      {
	max = false;
	return true;
      }
    return false;
717
718
719
720
  }

  acc_cond::acc_code acc_cond::acc_code::to_dnf() const
  {
721
    if (empty() || size() == 2)
722
      return *this;
723
724
725

    auto used = acc_cond::acc_code::used_sets();
    unsigned c = used.count();
726
    unsigned max = used.max_set();
727
728
729

    bdd_allocator ba;
    int base = ba.allocate_variables(c);
730
    assert(base == 0);
731
732
    std::vector<bdd> r;
    std::vector<unsigned> sets(c);
733
    for (unsigned i = 0; r.size() < max; ++i)
734
      {
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
	if (used.has(i))
	  {
	    sets[base] = i;
	    r.push_back(bdd_ithvar(base++));
	  }
	else
	  {
	    r.push_back(bddfalse);
	  }
      }

    bdd res = to_bdd_rec(&back(), &r[0]);

    if (res == bddtrue)
      return t();
    if (res == bddfalse)
      return f();

    minato_isop isop(res);
    bdd cube;
    acc_code rescode = f();
    while ((cube = isop.next()) != bddfalse)
      {
	mark_t i = 0U;
	acc_code c;
	while (cube != bddtrue)
	  {
	    // The acceptance set associated to this BDD variable
	    mark_t s = 0U;
	    s.set(sets[bdd_var(cube)]);

	    bdd h = bdd_high(cube);
	    if (h == bddfalse)	// Negative variable? -> Fin
	      {
		cube = bdd_low(cube);
		// The strange order here make sure we can smaller set
		// numbers at the end of the acceptance code, i.e., at
		// the front of the output.
773
		auto a = fin(s) & std::move(c);
774
775
776
777
778
779
780
781
		std::swap(a, c);
	      }
	    else		// Positive variable? -> Inf
	      {
		i |= s;
		cube = h;
	      }
	  }
782
	c &= inf(i);
783
	// See comment above for the order.
784
	c |= std::move(rescode);
785
	std::swap(c, rescode);
786
      }
787

788
    return rescode;
789
790
  }

791
792
793
794
795
796
797
  acc_cond::acc_code acc_cond::acc_code::to_cnf() const
  {
    if (empty() || size() == 2)
      return *this;

    auto used = acc_cond::acc_code::used_sets();
    unsigned c = used.count();
798
    unsigned max = used.max_set();
799
800
801

    bdd_allocator ba;
    int base = ba.allocate_variables(c);
802
    assert(base == 0);
803
804
    std::vector<bdd> r;
    std::vector<unsigned> sets(c);
805
    for (unsigned i = 0; r.size() < max; ++i)
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
      {
	if (used.has(i))
	  {
	    sets[base] = i;
	    r.push_back(bdd_ithvar(base++));
	  }
	else
	  {
	    r.push_back(bddfalse);
	  }
      }

    bdd res = to_bdd_rec(&back(), &r[0]);

    if (res == bddtrue)
      return t();
    if (res == bddfalse)
      return f();

    minato_isop isop(!res);
    bdd cube;
    acc_code rescode;
    while ((cube = isop.next()) != bddfalse)
      {
	mark_t m = 0U;
	acc_code c = f();
	while (cube != bddtrue)
	  {
	    // The acceptance set associated to this BDD variable
	    mark_t s = 0U;
	    s.set(sets[bdd_var(cube)]);

	    bdd h = bdd_high(cube);
	    if (h == bddfalse)	// Negative variable? -> Inf
	      {
		cube = bdd_low(cube);
		// The strange order here make sure we can smaller set
		// numbers at the end of the acceptance code, i.e., at
		// the front of the output.
845
		auto a = inf(s) | std::move(c);
846
847
848
849
850
851
852
853
		std::swap(a, c);
	      }
	    else		// Positive variable? -> Fin
	      {
		m |= s;
		cube = h;
	      }
	  }
854
	c |= fin(m);
855
	// See comment above for the order.
856
	c &= std::move(rescode);
857
858
859
860
861
	std::swap(c, rescode);
      }
    return rescode;
  }

862
  std::pair<bool, acc_cond::mark_t>
863
  acc_cond::unsat_mark() const
864
  {
865
    if (is_t())
866
      return {false, 0U};
867
868
    if (!uses_fin_acceptance())
      return {true, 0U};
869

870
    auto used = code_.used_sets();
871
    unsigned c = used.count();
872
    unsigned max = used.max_set();
873
874
875
876
877
878

    bdd_allocator ba;
    int base = ba.allocate_variables(c);
    assert(base == 0);
    std::vector<bdd> r;
    std::vector<unsigned> sets(c);
879
    for (unsigned i = 0; r.size() < max; ++i)
880
881
882
883
884
885
886
887
888
889
890
891
      {
	if (used.has(i))
	  {
	    sets[base] = i;
	    r.push_back(bdd_ithvar(base++));
	  }
	else
	  {
	    r.push_back(bddfalse);
	  }
      }

892
    bdd res = to_bdd_rec(&code_.back(), &r[0]);
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919

    if (res == bddtrue)
      return {false, 0U};
    if (res == bddfalse)
      return {true, 0U};

    bdd cube = bdd_satone(!res);
    mark_t i = 0U;
    while (cube != bddtrue)
      {
	// The acceptance set associated to this BDD variable
	int s = sets[bdd_var(cube)];

	bdd h = bdd_high(cube);
	if (h == bddfalse)	// Negative variable? -> skip
	  {
	    cube = bdd_low(cube);
	  }
	else		// Positive variable? -> Inf
	  {
	    i.set(s);
	    cube = h;
	  }
      }
    return {true, i};
  }

920
921
922
923
924
925
926
927
928
929
930
931
932
933
  std::vector<std::vector<int>>
  acc_cond::acc_code::missing(mark_t inf, bool accepting) const
  {
    if (empty())
      {
	if (accepting)
	  return {};
	else
	  return {
	    {}
	  };
      }
    auto used = acc_cond::acc_code::used_sets();
    unsigned c = used.count();
934
    unsigned max = used.max_set();
935
936
937
938
939
940
941

    bdd_allocator ba;
    int base = ba.allocate_variables(c);
    assert(base == 0);
    std::vector<bdd> r;
    std::vector<unsigned> sets(c);
    bdd known = bddtrue;
942
    for (unsigned i = 0; r.size() < max; ++i)
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
      {
	if (used.has(i))
	  {
	    sets[base] = i;
	    bdd v = bdd_ithvar(base++);
	    r.push_back(v);
	    if (inf.has(i))
	      known &= v;
	  }
	else
	  {
	    r.push_back(bddfalse);
	  }
      }

    bdd res = to_bdd_rec(&back(), &r[0]);

    res = bdd_restrict(res, known);

    if (accepting)
      res = !res;

    if (res == bddfalse)
      return {};

    minato_isop isop(res);
    bdd cube;
    std::vector<std::vector<int>> result;
    while ((cube = isop.next()) != bddfalse)
      {
	std::vector<int> partial;
	while (cube != bddtrue)
	  {
	    // The acceptance set associated to this BDD variable
	    int s = sets[bdd_var(cube)];

	    bdd h = bdd_high(cube);
	    if (h == bddfalse)	// Negative variable
	      {
		partial.push_back(s);
		cube = bdd_low(cube);
	      }
	    else		// Positive variable
	      {
		partial.push_back(-s - 1);
		cube = h;
	      }
	  }
	result.emplace_back(std::move(partial));
      }
    return result;
  }

996
997
  bool acc_cond::acc_code::is_dnf() const
  {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
998
    if (empty() || size() == 2)
999
1000
1001
      return true;
    auto pos = &back();
    auto start = &front();
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1002
    auto and_scope = pos + 1;
1003
1004
1005
1006
1007
1008
1009
1010
1011
    if (pos->op == acc_cond::acc_op::Or)
      --pos;
    while (pos > start)
      {
	switch (pos->op)
	  {
	  case acc_cond::acc_op::Or:
	    return false;
	  case acc_cond::acc_op::And:
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1012
	    and_scope = std::min(and_scope, pos - pos->size);
1013
1014
1015
1016
	    --pos;
	    break;
	  case acc_cond::acc_op::Fin:
	  case acc_cond::acc_op::FinNeg:
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1017
1018
1019
1020
1021
	    if (pos[-1].mark.count() > 1 && pos > and_scope)
	      return false;
	    /* fall through */
	  case acc_cond::acc_op::Inf:
	  case acc_cond::acc_op::InfNeg:
1022
1023
1024
1025
1026
1027
1028
	    pos -= 2;
	    break;
	  }
      }
    return true;
  }

1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
  bool acc_cond::acc_code::is_cnf() const
  {
    if (empty() || size() == 2)
      return true;
    auto pos = &back();
    auto start = &front();
    auto or_scope = pos + 1;
    if (pos->op == acc_cond::acc_op::And)
      --pos;
    while (pos > start)
      {
	switch (pos->op)
	  {
	  case acc_cond::acc_op::And:
	    return false;
	  case acc_cond::acc_op::Or:
	    or_scope = std::min(or_scope, pos - pos->size);
	    --pos;
	    break;
	  case acc_cond::acc_op::Inf:
	  case acc_cond::acc_op::InfNeg:
	    if (pos[-1].mark.count() > 1 && pos > or_scope)
	      return false;
	    /* fall through */
	  case acc_cond::acc_op::Fin:
	  case acc_cond::acc_op::FinNeg:
	    pos -= 2;
	    break;
	  }
      }
    return true;
  }

1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
  namespace
  {
    acc_cond::acc_code complement_rec(const acc_cond::acc_word* pos)
    {
      auto start = pos - pos->size;
      switch (pos->op)
	{
	case acc_cond::acc_op::And:
	  {
	    --pos;
1072
	    auto res = acc_cond::acc_code::f();
1073
1074
	    do
	      {
1075
		auto tmp = complement_rec(pos) | std::move(res);
1076
1077
1078
1079
1080
1081
1082
1083
1084
		std::swap(tmp, res);
		pos -= pos->size + 1;
	      }
	    while (pos > start);
	    return res;
	  }
	case acc_cond::acc_op::Or:
	  {
	    --pos;
1085
	    auto res = acc_cond::acc_code::t();
1086
1087
	    do
	      {
1088
		auto tmp = complement_rec(pos) & std::move(res);
1089
1090
1091
1092
1093
1094
1095
		std::swap(tmp, res);
		pos -= pos->size + 1;
	      }
	    while (pos > start);
	    return res;
	  }
	case acc_cond::acc_op::Fin:
1096
	  return acc_cond::acc_code::inf(pos[-1].mark);
1097
	case acc_cond::acc_op::Inf:
1098
	  return acc_cond::acc_code::fin(pos[-1].mark);
1099
	case acc_cond::acc_op::FinNeg:
1100
	  return acc_cond::acc_code::inf_neg(pos[-1].mark);
1101
	case acc_cond::acc_op::InfNeg:
1102
	  return acc_cond::acc_code::fin_neg(pos[-1].mark);
1103
1104
	}
      SPOT_UNREACHABLE();
1105
      return {};
1106
1107
1108
1109
1110
1111
1112
    }

  }


  acc_cond::acc_code acc_cond::acc_code::complement() const
  {
1113
    if (is_t())
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
      return acc_cond::acc_code::f();
    return complement_rec(&back());
  }

  namespace
  {
    static acc_cond::acc_code
    strip_rec(const acc_cond::acc_word* pos, acc_cond::mark_t rem, bool missing)
    {
      auto start = pos - pos->size;
      switch (pos->op)
	{
	case acc_cond::acc_op::And:
	  {
	    --pos;
	    auto res = acc_cond::acc_code::t();
	    do
	      {
1132
		auto tmp = strip_rec(pos, rem, missing) & std::move(res);
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
		std::swap(tmp, res);
		pos -= pos->size + 1;
	      }
	    while (pos > start);
	    return res;
	  }
	case acc_cond::acc_op::Or:
	  {
	    --pos;
	    auto res = acc_cond::acc_code::f();
	    do
	      {
1145
		auto tmp = strip_rec(pos, rem, missing) | std::move(res);
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
		std::swap(tmp, res);
		pos -= pos->size + 1;
	      }
	    while (pos > start);
	    return res;
	  }
	case acc_cond::acc_op::Fin:
	  if (missing && (pos[-1].mark & rem))
	    return acc_cond::acc_code::t();
	  return acc_cond::acc_code::fin(pos[-1].mark.strip(rem));
	case acc_cond::acc_op::Inf:
	  if (missing && (pos[-1].mark & rem))
	    return acc_cond::acc_code::f();
	  return acc_cond::acc_code::inf(pos[-1].mark.strip(rem));
	case acc_cond::acc_op::FinNeg:
	case acc_cond::acc_op::InfNeg:
	  SPOT_UNREACHABLE();
	  return {};
	}
      SPOT_UNREACHABLE();
      return {};
    }
  }

  acc_cond::acc_code
  acc_cond::acc_code::strip(acc_cond::mark_t rem, bool missing) const
  {
1173
    if (is_t() || is_f())
1174
1175
1176
1177
1178
1179
1180
      return *this;
    return strip_rec(&back(), rem, missing);
  }

  acc_cond::mark_t
  acc_cond::acc_code::used_sets() const
  {
1181
    if (is_t() || is_f())
1182
1183
1184
1185
1186
      return 0U;
    acc_cond::mark_t used_in_cond = 0U;
    auto pos = &back();
    auto end = &front();
    while (pos > end)
1187
      {
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
	switch (pos->op)
	  {
	  case acc_cond::acc_op::And:
	  case acc_cond::acc_op::Or:
	    --pos;
	    break;
	  case acc_cond::acc_op::Fin:
	  case acc_cond::acc_op::Inf:
	  case acc_cond::acc_op::FinNeg:
	  case acc_cond::acc_op::InfNeg:
	    used_in_cond |= pos[-1].mark;
	    pos -= 2;
	    break;
	  }
1202
      }
1203
    return used_in_cond;
1204
  }
1205

1206
1207
1208
  std::pair<acc_cond::mark_t, acc_cond::mark_t>
  acc_cond::acc_code::used_inf_fin_sets() const
  {
1209
    if (is_t() || is_f())
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
      return {0U, 0U};

    acc_cond::mark_t used_fin = 0U;
    acc_cond::mark_t used_inf = 0U;
    auto pos = &back();
    auto end = &front();
    while (pos > end)
      {
	switch (pos->op)
	  {
	  case acc_cond::acc_op::And:
	  case acc_cond::acc_op::Or:
	    --pos;
	    break;
	  case acc_cond::acc_op::Fin:
	  case acc_cond::acc_op::FinNeg:
	    used_fin |= pos[-1].mark;
	    pos -= 2;
	    break;
	  case acc_cond::acc_op::Inf:
	  case acc_cond::acc_op::InfNeg:
	    used_inf |= pos[-1].mark;
	    pos -= 2;
	    break;
	  }
      }
    return {used_inf, used_fin};
  }

  std::ostream&
  acc_cond::acc_code::to_html(std::ostream& os,
			      std::function<void(std::ostream&, int)>
			      set_printer) const
1243
  {
1244
    if (empty())
1245
1246
      os << 't';
    else
1247
1248
      print_code<true>(os, *this, size() - 1,
		       set_printer ? set_printer : default_set_printer);
1249
1250
    return os;
  }
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270

  std::ostream&
  acc_cond::acc_code::to_text(std::ostream& os,
			      std::function<void(std::ostream&, int)>
			      set_printer) const
  {
    if (empty())
      os << 't';
    else
      print_code<false>(os, *this, size() - 1,
			set_printer ? set_printer : default_set_printer);
    return os;
  }


  std::ostream& operator<<(std::ostream& os,
			   const spot::acc_cond::acc_code& code)
  {
    return code.to_text(os);
  }
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305



  namespace
  {

    /// acc ::= term | term "|" acc
    /// term := "t" | "f" | "Inf" "(" num ")"
    ///       | "Fin" "(" num ") " | "(" acc ")
    ///       | term "&" term

    static void skip_space(const char*& input)
    {
      while (std::isspace(*input))
	++input;
    }

    // Eat c and remove the following spaces.
    // Complain if there is no c.
    void expect(const char*& input, char c)
    {
      if (*input != c)
	{
	  std::ostringstream s;
	  s << "syntax error at '" << input << "', was expecting " << c << '.';
	  throw parse_error(s.str());
	}
      ++input;
      skip_space(input);
    }

    static acc_cond::acc_code parse_term(const char*& input);

    static acc_cond::acc_code parse_acc(const char*& input)
    {
1306
      auto res = parse_term(input);
1307
1308
1309
1310
1311
      skip_space(input);
      while (*input == '|')
	{
	  ++input;
	  skip_space(input);
1312
1313
1314
	  // Prepend instead of append, to preserve the input order.
	  auto tmp = parse_term(input);
	  std::swap(tmp, res);
1315
	  res |= std::move(tmp);
1316
	}
1317
      return res;
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
    }

    static unsigned parse_num(const char*& input)
    {

      errno = 0;
      char* end;
      unsigned long n = strtoul(input, &end, 10);
      unsigned num = n;
      if (errno || num != n)
	{
	  std::ostringstream s;
1330
	  s << "syntax error at '" << input << "': invalid number.";
1331
1332
1333
	  throw parse_error(s.str());
	}
      input = end;
1334
1335
      return n;
    }
1336

1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
    static unsigned parse_range(const char*& str)
    {
      skip_space(str);
      int min;
      int max;
      char* end;
      errno = 0;
      min = strtol(str, &end, 10);
      if (end == str || errno)
	{
	  // No leading number.  It's OK as long as the string is not
	  // empty.
	  if (!*end || errno)
	    {
	      std::ostringstream s;
	      s << "syntax error at '" << str << "': invalid range.";
	      throw parse_error(s.str());
	    }
	  min = 1;
	}
      if (!*end || (*end != ':' && *end != '.'))
	{
	  // Only one number.
	  max = min;
	}
      else
	{
	  // Skip : or ..
	  if (end[0] == ':')
	    ++end;
	  else if (end[0] == '.' && end[1] == '.')
	    end += 2;

	  // Parse the next integer.
	  char* end2;
	  max = strtol(end, &end2, 10);
	  if (end == end2 || errno)
	    {
	      std::ostringstream s;
	      s << "syntax error at '" << str
		<< "': invalid range (missing end?)";
	      throw parse_error(s.str());
	    }
	  end = end2;
	}

      if (min < 0 || max < 0)
	{
	  std::ostringstream s;
	  s << "invalid range '" << str
	    << "': values must be positive";
	  throw parse_error(s.str());
	}

      str = end;

      if (min == max)
	return min;

      if (min > max)
	std::swap(min, max);
      return spot::rrand(min, max);
1399
1400
1401
1402
1403
1404
1405
    }

    static unsigned parse_par_num(const char*& input)
    {
      skip_space(input);
      expect(input, '(');
      unsigned num = parse_num(input);
1406
1407
1408
1409
1410
      skip_space(input);
      expect(input, ')');
      return num;
    }

1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
    static double parse_proba(const char*& input)
    {
      errno = 0;
      char* end;
      double n = strtod(input, &end);
      if (errno)
	{
	  std::ostringstream s;
	  s << "syntax error at '" << input << "': can't convert to double";
	  throw parse_error(s.str());
	}
      if (!(n >= 0.0 && n <= 1.0))
	{
	  std::ostringstream s;
	  s << "syntax error at '" << input
	    << "': value should be between 0 and 1.";
	  throw parse_error(s.str());
	}
      input = end;
      return n;
    }

1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
    static acc_cond::acc_code parse_term(const char*& input)
    {
      acc_cond::acc_code res;
      if (*input == 't')
	{
	  ++input;
	  res = acc_cond::acc_code::t();
	}
      else if (*input == 'f')
	{
	  ++input;
	  res = acc_cond::acc_code::f();
	}
      else if (*input == '(')
	{
	  ++input;
	  skip_space(input);
	  res = parse_acc(input);
	  skip_space(input);
	  expect(input, ')');
	}
      else if (!strncmp(input, "Inf", 3))
	{
	  input += 3;
1457
	  res = acc_cond::acc_code::inf({parse_par_num(input)});
1458
1459
1460
1461
	}
      else if (!strncmp(input, "Fin", 3))
	{
	  input += 3;
1462
	  res = acc_cond::acc_code::fin({parse_par_num(input)});
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
	}
      else
	{
	  std::ostringstream s;
	  s << "syntax error at '" << input << "', unexpected character.";
	  throw parse_error(s.str());
	}

      skip_space(input);
      while (*input == '&')
	{
	  ++input;
	  skip_space(input);
1476
1477
1478
	  // Prepend instead of append, to preserve the input order.
	  auto tmp = parse_term(input);
	  std::swap(tmp, res);
1479
	  res &= std::move(tmp);
1480
1481
1482
1483
	}
      return res;
    }

1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
    static bool max_or_min(const char*& input)
    {
      skip_space(input);
      if (!strncmp(input, "max", 3))
	{
	  input += 3;
	  return true;
	}
      if (!strncmp(input, "min", 3))
	{
	  input += 3;
	  return false;
	}
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
      if (!strncmp(input, "rand", 4))
	{
	  input += 4;
	  return drand() < 0.5;
	}
      if (!strncmp(input, "random", 6))
	{
	  input += 6;
	  return drand() < 0.5;
	}
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
      std::ostringstream s;
      s << "syntax error at '" << input
	<< "': expecting 'min' or 'max'";
      throw parse_error(s.str());
    }

    static bool odd_or_even(const char*& input)
    {
      skip_space(input);
      if (!strncmp(input, "odd", 3))
	{
	  input += 3;
	  return true;
	}
      if (!strncmp(input, "even", 4))
	{
	  input += 4;
	  return false;
	}
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
      if (!strncmp(input, "rand", 4))
	{
	  input += 4;
	  return drand() < 0.5;
	}
      if (!strncmp(input, "random", 6))
	{
	  input += 6;
	  return drand() < 0.5;
	}
1536
1537
1538
1539
1540
1541
      std::ostringstream s;
      s << "syntax error at '" << input
	<< "': expecting 'odd' or 'even'";
      throw parse_error(s.str());
    }

1542
1543
1544
1545
1546
  }

  acc_cond::acc_code parse_acc_code(const char* input)
  {
    skip_space(input);
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
    acc_cond::acc_code c;
    if (!strncmp(input, "all", 3))
      {
	input += 3;
	c = acc_cond::acc_code::t();
      }
    else if (!strncmp(input, "none", 4))
      {
	input += 4;
	c = acc_cond::acc_code::f();
      }
    else if (!strncmp(input, "Buchi", 5))
      {
	input += 5;
	c = acc_cond::acc_code::buchi();
      }
    else if (!strncmp(input, "co-Buchi", 8))
      {
	input += 8;
	c = acc_cond::acc_code::cobuchi();
      }
    else if (!strncmp(input, "generalized-Buchi", 17))
      {
	input += 17;
1571
	c = acc_cond::acc_code::generalized_buchi(parse_range(input));
1572
1573
1574
1575
      }
    else if (!strncmp(input, "generalized-co-Buchi", 20))
      {
	input += 20;
1576
	c = acc_cond::acc_code::generalized_co_buchi(parse_range(input));
1577
1578
1579
1580
      }
    else if (!strncmp(input, "Rabin", 5))
      {
	input += 5;
1581
	c = acc_cond::acc_code::rabin(parse_range(input));
1582
1583
1584
1585
      }
    else if (!strncmp(input, "Streett", 7))
      {
	input += 7;
1586
	c = acc_cond::acc_code::streett(parse_range(input));
1587
1588
1589
1590
1591
1592
1593
1594
1595
      }
    else if (!strncmp(input, "generalized-Rabin", 17))
      {
	input += 17;
	unsigned num = parse_num(input);
	std::vector<unsigned> v;
	v.reserve(num);
	while (num > 0)
	  {
1596
	    v.push_back(parse_range(input));
1597
1598
1599
1600
	    --num;
	  }
	c = acc_cond::acc_code::generalized_rabin(v.begin(), v.end());
      }
1601
1602
1603
1604
1605
    else if (!strncmp(input, "parity", 6))
      {
	input += 6;
	bool max = max_or_min(input);
	bool odd = odd_or_even(input);
1606
	unsigned num = parse_range(input);
1607
1608
	c = acc_cond::acc_code::parity(max, odd, num);
      }
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
    else if (!strncmp(input, "random", 6))
      {
	input += 6;
	unsigned n = parse_range(input);
	skip_space(input);
	double reuse = (*input) ? parse_proba(input) : 0.0;
	if (reuse >= 1.0)
	  throw parse_error("probability for set reuse should be <1.");
	c = acc_cond::acc_code::random(n, reuse);
      }
1619
1620
1621
1622
1623
    else
      {
	c = parse_acc(input);
      }
    skip_space(input);
1624
1625
1626
1627
1628
1629
1630
1631
    if (*input)
      {
	std::ostringstream s;
	s << "syntax error at '" << input << "', unexpected character.";
	throw parse_error(s.str());
      }
    return c;
  }
1632
}