parseaut.yy 48.3 KB
Newer Older
1
/* -*- coding: utf-8 -*-
2
** Copyright (C) 2014, 2015 Laboratoire de Recherche et Développement
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
** de l'Epita (LRDE).
**
** 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/>.
*/
%language "C++"
%locations
%defines
%expect 0 // No shift/reduce
%name-prefix "hoayy"
%debug
%error-verbose
27
%lex-param { spot::parse_aut_error_list& error_list }
28
29
30
31
32
33
34
%define api.location.type "spot::location"

%code requires
{
#include <string>
#include <cstring>
#include <sstream>
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
35
#include <unordered_map>
36
#include <algorithm>
37
#include "ltlast/constant.hh"
38
#include "twa/formula2bdd.hh"
39
#include "public.hh"
40
41
#include "priv/accmap.hh"
#include "ltlparse/public.hh"
42

43
  inline namespace hoayy_support
44
  {
45
46
    typedef std::map<int, bdd> map_t;

47
48
49
50
51
52
53
    /* Cache parsed formulae.  Labels on arcs are frequently identical
       and it would be a waste of time to parse them to formula* over and
       over, and to register all their atomic_propositions in the
       bdd_dict.  Keep the bdd result around so we can reuse it.  */
    typedef std::map<std::string, bdd> formula_cache;

    typedef std::pair<int, std::string*> pair;
54
    typedef spot::twa_graph::namer<std::string> named_tgba_t;
55
56
57
58
59
60
61
62

    // Note: because this parser is meant to be used on a stream of
    // automata, it tries hard to recover from errors, so that we get a
    // chance to reach the end of the current automaton in order to
    // process the next one.  Several variables below are used to keep
    // track of various error conditions.
    enum label_style_t { Mixed_Labels, State_Labels, Trans_Labels,
			 Implicit_Labels };
63
    enum acc_style_t { Mixed_Acc, State_Acc, Trans_Acc };
64
65

    struct result_
66
    {
67
68
      struct state_info
      {
69
70
	bool declared = false;
	bool used = false;
71
72
	spot::location used_loc;
      };
73
      spot::parsed_aut_ptr h;
74
75
76
77
78
79
80
      spot::ltl::environment* env;
      formula_cache fcache;
      named_tgba_t* namer = nullptr;
      spot::acc_mapper_int* acc_mapper = nullptr;
      std::vector<int> ap;
      std::vector<bdd> guards;
      std::vector<bdd>::const_iterator cur_guard;
81
      map_t dest_map;
82
      std::vector<state_info> info_states; // States declared and used.
83
84
85
86
87
88
89
90
91
92
      std::vector<std::pair<spot::location, unsigned>> start; // Initial states;
      std::unordered_map<std::string, bdd> alias;
      std::unordered_map<std::string, spot::location> props;
      spot::location states_loc;
      spot::location ap_loc;
      spot::location state_label_loc;
      spot::location accset_loc;
      spot::acc_cond::mark_t acc_state;
      spot::acc_cond::mark_t neg_acc_sets = 0U;
      spot::acc_cond::mark_t pos_acc_sets = 0U;
93
94
      int plus;
      int minus;
95
      std::vector<std::string>* state_names = nullptr;
96
      std::map<unsigned, unsigned> states_map;
97
      std::set<int> ap_set;
98
99
100
101
102
103
104
105
106
      unsigned cur_state;
      int states = -1;
      int ap_count = -1;
      int accset = -1;
      bdd state_label;
      bdd cur_label;
      bool has_state_label = false;
      bool ignore_more_ap = false; // Set to true after the first "AP:"
      // line has been read.
107
108
      bool ignore_acc = false;	// Set to true in case of missing
				// Acceptance: lines.
109
110
      bool ignore_acc_silent = false;
      bool ignore_more_acc = false; // Set to true after the first
111
				// "Acceptance:" line has been read.
112
113

      label_style_t label_style = Mixed_Labels;
114
      acc_style_t acc_style = Mixed_Acc;
115
116
117
118
119
120
121

      bool accept_all_needed = false;
      bool accept_all_seen = false;
      bool aliased_states = false;

      bool deterministic = false;
      bool complete = false;
122
      bool trans_acc_seen = false;
123
124
125
126
127
128
129
130
131
132

      std::map<std::string, spot::location> labels;

      ~result_()
      {
	delete namer;
	delete acc_mapper;
      }
    };
  }
133
134
}

135
%parse-param {spot::parse_aut_error_list& error_list}
136
%parse-param {result_& res}
137
138
%parse-param {spot::location initial_loc}

139
%initial-action { @$ = res.h->loc = initial_loc; }
140

141
142
143
144
145
146
%union
{
  std::string* str;
  unsigned int num;
  int b;
  spot::acc_cond::mark_t mark;
147
148
  pair* p;
  std::list<pair>* list;
149
  spot::acc_cond::acc_code* code;
150
151
152
153
154
}

%code
{
#include <sstream>
155
156
157
#include "ltlast/constant.hh"
#include "ltlparse/public.hh"

158
  /* parseaut.hh and parsedecl.hh include each other recursively.
159
160
161
   We must ensure that YYSTYPE is declared (by the above %union)
   before parsedecl.hh uses it. */
#include "parsedecl.hh"
162
163

  static void fill_guards(result_& res);
164
165
}

166
/**** HOA tokens ****/
167
168
169
170
171
172
173
174
175
176
177
178
179
%token HOA "HOA:"
%token STATES "States:"
%token START "Start:"
%token AP "AP:"
%token ALIAS "Alias:"
%token ACCEPTANCE "Acceptance:"
%token ACCNAME "acc-name:"
%token TOOL "tool:"
%token NAME "name:"
%token PROPERTIES "properties:"
%token BODY "--BODY--"
%token END "--END--"
%token STATE "State:";
180
%token <str> IDENTIFIER "identifier";  // also used by neverclaim
181
182
183
184
%token <str> HEADERNAME "header name";
%token <str> ANAME "alias name";
%token <str> STRING "string";
%token <num> INT "integer";
185
%token ENDOFFILE 0 "end of file"
186

187
188
189
190
191
192
193
194
195
%token DRA "DRA"
%token DSA "DSA"
%token V2 "v2"
%token EXPLICIT "explicit"
%token ACCPAIRS "Acceptance-Pairs:"
%token ACCSIG "Acc-Sig:";
%token ENDOFHEADER "---";


196
197
198
199
%left '|'
%left '&'
%nonassoc '!'

200
%type <num> checked-state-num state-num acc-set sign
201
%type <b> label-expr
202
%type <mark> acc-sig acc-sets trans-acc_opt state-acc_opt
203
             dstar_accsigs dstar_state_accsig
204
%type <str> string_opt
205

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**** NEVERCLAIM tokens ****/

%token NEVER "never"
%token SKIP "skip"
%token IF "if"
%token FI "fi"
%token DO "do"
%token OD "od"
%token ARROW "->"
%token GOTO "goto"
%token FALSE "false"
%token ATOMIC "atomic"
%token ASSERT "assert"
%token <str> FORMULA "boolean formula"

%type <b> nc-formula
%type <str> nc-opt-dest nc-formula-or-ident
%type <p> nc-transition nc-src-dest
%type <list> nc-transitions nc-transition-block
%type <str> nc-one-ident nc-ident-list
226
%type <code> acceptance-cond
227

228
229
230
/**** LBTT tokens *****/
 // Also using INT, STRING
%token ENDAUT "-1"
231
%token ENDDSTAR "end of DSTAR automaton"
232
233
234
235
236
237
238
%token <num> LBTT "LBTT header"
%token <num> INT_S "state acceptance"
%token <num> LBTT_EMPTY "acceptance sets for empty automaton"
%token <num> ACC "acceptance set"
%token <num> STATE_NUM "state number"
%token <num> DEST_NUM "destination number"
%type <mark> lbtt-acc
239

240
241
%destructor { delete $$; } <str>
%destructor { bdd_delref($$); } <b>
242
%destructor { bdd_delref($$->first); delete $$->second; delete $$; } <p>
243
%destructor { delete $$; } <code>
244
245
246
247
248
249
250
251
252
%destructor {
  for (std::list<pair>::iterator i = $$->begin();
       i != $$->end(); ++i)
  {
    bdd_delref(i->first);
    delete i->second;
  }
  delete $$;
  } <list>
253
254
255
256
257
258
259
260
%printer {
    if ($$)
      debug_stream() << *$$;
    else
      debug_stream() << "\"\""; } <str>
%printer { debug_stream() << $$; } <num>

%%
261
262
aut: aut-1     { res.h->loc = @$; YYACCEPT; }
   | ENDOFFILE { YYABORT; }
263
   | error ENDOFFILE { YYABORT; }
264
265
266
267
268
269
   | error aut-1
     {
       error(@1, "leading garbage was ignored");
       res.h->loc = @2;
       YYACCEPT;
     }
270

271
272
273
274
aut-1: hoa   { res.h->type = spot::parsed_aut_type::HOA; }
     | never { res.h->type = spot::parsed_aut_type::NeverClaim; }
     | lbtt  { res.h->type = spot::parsed_aut_type::LBTT; }
     | dstar /* will set type as DSA or DRA while parsing first line */
275
276
277
278
279

/**********************************************************************/
/*                          Rules for HOA                             */
/**********************************************************************/

280
hoa: header "--BODY--" body "--END--"
281
   | "HOA:" error "--END--"
282

283
284
string_opt: { $$ = nullptr; }
          | STRING { $$ = $1; }
285
286
287
288
BOOLEAN: 't' | 'f'

header: format-version header-items
        {
289
	  // Preallocate the states if we know their number.
290
	  if (res.states >= 0)
291
292
	    {
	      unsigned states = res.states;
293
294
295
296
297
298
299
300
	      for (auto& p : res.start)
		if ((unsigned) res.states <= p.second)
		  {
		    error(p.first,
			  "initial state number is larger than state count...");
		    error(res.states_loc, "... declared here.");
		    states = std::max(states, p.second + 1);
		  }
301
	      res.h->aut->new_states(states);
302
	      res.info_states.resize(states);
303
	    }
304
305
306
	  if (res.accset < 0)
	    {
	      error(@$, "missing 'Acceptance:' header");
307
	      res.ignore_acc = true;
308
	    }
309
310
	  // Process properties.
	  {
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
	    auto explicit_labels = res.props.find("explicit-labels");
	    auto implicit_labels = res.props.find("implicit-labels");
	    if (implicit_labels != res.props.end())
	      {
		if (explicit_labels == res.props.end())
		  {
		    res.label_style = Implicit_Labels;
		  }
		else
		  {
		    error(implicit_labels->second,
			  "'property: implicit-labels' is incompatible "
			  "with...");
		    error(explicit_labels->second,
			  "... 'property: explicit-labels'.");
		  }
	      }

329
330
331
332
333
334
	    auto trans_labels = res.props.find("trans-labels");
	    auto state_labels = res.props.find("state-labels");
	    if (trans_labels != res.props.end())
	      {
		if (state_labels == res.props.end())
		  {
335
336
		    if (res.label_style != Implicit_Labels)
		      res.label_style = Trans_Labels;
337
338
339
340
341
342
343
344
345
346
347
		  }
		else
		  {
		    error(trans_labels->second,
			  "'property: trans-labels' is incompatible with...");
		    error(state_labels->second,
			  "... 'property: state-labels'.");
		  }
	      }
	    else if (state_labels != res.props.end())
	      {
348
349
350
351
352
353
354
355
356
357
358
		if (res.label_style != Implicit_Labels)
		  {
		    res.label_style = State_Labels;
		  }
		else
		  {
		    error(state_labels->second,
			  "'property: state-labels' is incompatible with...");
		    error(implicit_labels->second,
			  "... 'property: implicit-labels'.");
		  }
359
	      }
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380

	    auto state_acc = res.props.find("state-acc");
	    auto trans_acc = res.props.find("trans-acc");
	    if (trans_acc != res.props.end())
	      {
		if (state_acc == res.props.end())
		  {
		    res.acc_style = Trans_Acc;
		  }
		else
		  {
		    error(trans_acc->second,
			  "'property: trans-acc' is incompatible with...");
		    error(state_acc->second,
			  "... 'property: state-acc'.");
		  }
	      }
	    else if (state_acc != res.props.end())
	      {
		res.acc_style = State_Acc;
	      }
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
417
418
	  {
	    unsigned ss = res.start.size();
	    if (ss > 1)
	      {
		auto det = res.props.find("deterministic");
		if (det != res.props.end())
		  {
		    error(det->second,
			  "deterministic automata should have at most "
			  "one initial state");
		  }
		res.deterministic = false;
	      }
	    else
	      {
		// Assume the automaton is deterministic until proven
		// wrong.
		res.deterministic = true;
	      }
	    if (ss < 1)
	      {
		auto complete = res.props.find("complete");
		if (complete != res.props.end())
		  {
		    error(complete->second,
			  "complete automata should have at least "
			  "one initial state");
		  }
		res.complete = false;
	      }
	    else
	      {
		// Assume the automaton is complete until proven
		// wrong.
		res.complete = true;
	      }
	  }
419
420
	}

421
422
423
424
425
426
427
428
version: IDENTIFIER
         {
	   if (*$1 != "v1")
	     error(@$, "unsupported version of the HOA format");
	   delete $1;
	 }

format-version: "HOA:" { res.h->loc = @1; } version
429

430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
aps: "AP:" INT {
	                 if (res.ignore_more_ap)
			   {
			     error(@1, "ignoring this redeclaration of APs...");
			     error(res.ap_loc, "... previously declared here.");
			   }
			 else
			   {
			     res.ap_count = $2;
			     res.ap.reserve($2);
			   }
	               }
                   ap-names
	     {
	       if (!res.ignore_more_ap)
		 {
		   res.ap_loc = @1 + @2;
		   if ((int) res.ap.size() != res.ap_count)
		     {
		       std::ostringstream out;
		       out << "found " << res.ap.size()
			   << " atomic propositions instead of the "
			   << res.ap_count << " announced";
		       error(@$, out.str());
		     }
		   res.ignore_more_ap = true;
		 }
	     }

459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
header-items: | header-items header-item
header-item: "States:" INT
           {
	     if (res.states >= 0)
	       {
		 error(@$, "redefinition of the number of states...");
		 error(res.states_loc, "... previously defined here.");
	       }
	     else
	       {
		 res.states_loc = @$;
	       }
	     if (((int) $2) < 0)
	       {
		 error(@$, "too many states for this implementation");
		 YYABORT;
	       }
	     res.states = std::max(res.states, (int) $2);
	   }
478
           | "Start:" init-state-conj-2
479
480
481
482
483
484
	     {
	       error(@2, "alternation is not yet supported");
	       YYABORT;
	     }
           | "Start:" state-num
	     {
485
	       res.start.emplace_back(@$, $2);
486
	     }
487
           | aps
488
489
           | "Alias:" ANAME label-expr
             {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
490
491
492
493
494
495
	       if (!res.alias.emplace(*$2, bdd_from_int($3)).second)
		 {
		   std::ostringstream o;
		   o << "ignoring redefinition of alias @" << *$2;
		   error(@$, o.str());
		 }
496
497
498
499
500
	       delete $2;
	       bdd_delref($3);
	     }
           | "Acceptance:" INT
	      {
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
		if (res.ignore_more_acc)
		  {
		    error(@1 + @2, "ignoring this redefinition of the "
			  "acceptance condition...");
		    error(res.accset_loc, "... previously defined here.");
		  }
		else if ($2 > 8 * sizeof(spot::acc_cond::mark_t::value_t))
		  {
		    error(@1 + @2,
			  "this implementation cannot support such a large "
			  "number of acceptance sets");
		    YYABORT;
		  }
		else
		  {
516
		    res.h->aut->acc().add_sets($2);
517
518
519
		    res.accset = $2;
		    res.accset_loc = @1 + @2;
		  }
520
521
	     }
             acceptance-cond
522
523
	     {
	       res.ignore_more_acc = true;
524
525
	       res.h->aut->set_acceptance($2, *$4);
	       delete $4;
526
	     }
527
528
529
530
531
532
533
           | "acc-name:" IDENTIFIER acc-spec
             {
	       delete $2;
	     }
           | "tool:" STRING string_opt
             {
	       delete $2;
534
	       delete $3;
535
536
537
	     }
           | "name:" STRING
             {
538
	       res.h->aut->set_named_prop("automaton-name", $2);
539
540
541
	     }
           | "properties:" properties
           | HEADERNAME header-spec
542
543
544
545
546
547
548
549
	     {
	       char c = (*$1)[0];
	       if (c >= 'A' && c <= 'Z')
		 error(@$, "ignoring unsupported header \"" + *$1 + ":\"\n\t"
		       "(but the capital indicates information that should not"
		       " be ignored)");
	       delete $1;
	     }
550
           | error
551
552
553
554

ap-names: | ap-names ap-name
ap-name: STRING
	 {
555
	   if (!res.ignore_more_ap)
556
	     {
557
	       auto f = res.env->require(*$1);
558
	       int b = 0;
559
560
561
562
563
564
565
	       if (f == nullptr)
		 {
		   std::ostringstream out;
		   out << "unknown atomic proposition \"" << *$1 << "\"";
		   error(@1, out.str());
		   f = spot::ltl::default_environment::instance()
		     .require("$unknown$");
566
		   b = res.h->aut->register_ap(f);
567
568
569
		 }
	       else
		 {
570
		   b = res.h->aut->register_ap(f);
571
572
573
574
575
576
		   if (!res.ap_set.emplace(b).second)
		     {
		       std::ostringstream out;
		       out << "duplicate atomic proposition \"" << *$1 << "\"";
		       error(@1, out.str());
		     }
577
578
579
		 }
	       f->destroy();
	       res.ap.push_back(b);
580
581
582
583
584
585
586
587
588
589
590
591
	     }
	   delete $1;
	 }

acc-spec: | acc-spec BOOLEAN
	  | acc-spec INT
	  | acc-spec IDENTIFIER
            {
	      delete $2;
	    }
properties: | properties IDENTIFIER
	      {
592
		res.props.emplace(*$2, @2);
593
594
595
596
597
598
599
600
601
602
603
604
605
		delete $2;
	      }
header-spec: | header-spec BOOLEAN
             | header-spec INT
             | header-spec STRING
	       {
		 delete $2;
	       }
             | header-spec IDENTIFIER
	       {
		 delete $2;
	       }

606
607
state-conj-2: checked-state-num '&' checked-state-num
            | state-conj-2 '&' checked-state-num
608

609
610
611
612
613
	      // Currently we do not check the number of these states
	      // since we do not support alternation.
init-state-conj-2: state-num '&' state-num
            | init-state-conj-2 '&' state-num

614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
label-expr: 't'
	    {
	      $$ = bddtrue.id();
	    }
          | 'f'
	    {
	      $$ = bddfalse.id();
	    }
	  | INT
	    {
	      if ($1 >= res.ap.size())
		{
		  error(@1, "AP number is larger than the number of APs...");
		  error(res.ap_loc, "... declared here");
		  $$ = bddtrue.id();
		}
	      else
		{
		  $$ = bdd_ithvar(res.ap[$1]).id();
		  bdd_addref($$);
		}
	    }
          | ANAME
	    {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
638
639
640
641
642
643
644
645
646
647
648
	      auto i = res.alias.find(*$1);
	      if (i == res.alias.end())
		{
		  error(@$, "unknown alias @" + *$1);
		  $$ = 1;
		}
	      else
		{
		  $$ = i->second.id();
		  bdd_addref($$);
		}
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
	      delete $1;
	    }
          | '!' label-expr
	    {
              $$ = bdd_not($2);
              bdd_delref($2);
              bdd_addref($$);
            }
          | label-expr '&' label-expr
	    {
              $$ = bdd_and($1, $3);
              bdd_delref($1);
              bdd_delref($3);
              bdd_addref($$);
            }
          | label-expr '|' label-expr
	    {
              $$ = bdd_or($1, $3);
              bdd_delref($1);
              bdd_delref($3);
              bdd_addref($$);
            }
671
672
673
674
          | '(' label-expr ')'
	  {
	    $$ = $2;
	  }
675
676
677
678
679
680


acc-set: INT
            {
	      if ((int) $1 >= res.accset)
		{
681
682
683
684
685
686
687
688
689
690
691
		  if (!res.ignore_acc)
		    {
		      error(@1, "number is larger than the count "
			    "of acceptance sets...");
		      error(res.accset_loc, "... declared here.");
		    }
		  $$ = -1U;
		}
	      else
		{
		  $$ = $1;
692
693
694
695
696
		}
	    }

acceptance-cond: IDENTIFIER '(' acc-set ')'
		 {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
697
		   if ($3 != -1U)
698
699
700
701
702
703
704
705
706
707
708
709
710
		     {
		       res.pos_acc_sets |= res.h->aut->acc().mark($3);
		       if (*$1 == "Inf")
			 $$ = new spot::acc_cond::acc_code
			   (res.h->aut->acc().inf({$3}));
		       else
			 $$ = new spot::acc_cond::acc_code
			   (res.h->aut->acc().fin({$3}));
		     }
		   else
		     {
		       $$ = new spot::acc_cond::acc_code;
		     }
711
712
713
714
		   delete $1;
		 }
               | IDENTIFIER '(' '!' acc-set ')'
		 {
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
715
		   if ($4 != -1U)
716
717
718
719
720
721
722
723
724
725
726
727
728
		     {
		       res.neg_acc_sets |= res.h->aut->acc().mark($4);
		       if (*$1 == "Inf")
			 $$ = new spot::acc_cond::acc_code
			   (res.h->aut->acc().inf_neg({$4}));
		       else
			 $$ = new spot::acc_cond::acc_code
			   (res.h->aut->acc().fin_neg({$4}));
		     }
		   else
		     {
		       $$ = new spot::acc_cond::acc_code;
		     }
729
730
731
		   delete $1;
		 }
               | '(' acceptance-cond ')'
732
733
734
	         {
		   $$ = $2;
		 }
735
               | acceptance-cond '&' acceptance-cond
736
737
738
739
740
	         {
		   $3->append_and(std::move(*$1));
		   $$ = $3;
		   delete $1;
		 }
741
742
               | acceptance-cond '|' acceptance-cond
	         {
743
744
745
		   $3->append_or(std::move(*$1));
		   $$ = $3;
		   delete $1;
746
		 }
747
               | 't'
748
749
750
	         {
		   $$ = new spot::acc_cond::acc_code;
		 }
751
752
	       | 'f'
	       {
753
754
755
	         {
		   $$ = new spot::acc_cond::acc_code(res.h->aut->acc().fin({}));
		 }
756
	       }
757
758
759


body: states
760
761
      {
	for (auto& p: res.start)
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
	  if (p.second >= res.info_states.size()
	      || !res.info_states[p.second].declared)
	    {
	      error(p.first,
		    "initial state " + std::to_string(p.second) +
		    " has no definition");
	      // Pretend that the state is declared so we do not
	      // mention it in the next loop.
	      if (p.second < res.info_states.size())
		res.info_states[p.second].declared = true;
	    }
	unsigned n = res.info_states.size();
	// States with number above res.states have already caused a
	// diagnostic, so let not add another one.
	if (res.states >= 0)
	  n = res.states;
	for (unsigned i = 0; i < n; ++i)
	  {
	    auto& p = res.info_states[i];
	    if (p.used && !p.declared)
	      error(p.used_loc,
		    "state " + std::to_string(i) + " has no definition");
	  }
785
      }
786
787
788
789
790
791
792
793
794
795
796

state-num: INT
	   {
	     if (((int) $1) < 0)
	       {
		 error(@1, "state number is too large for this implementation");
		 YYABORT;
	       }
	     $$ = $1;
	   }

797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
checked-state-num: state-num
		   {
		     if ((int) $1 >= res.states)
		       {
			 if (res.states >= 0)
			   {
			     error(@1, "state number is larger than state "
				   "count...");
			     error(res.states_loc, "... declared here.");
			   }
			 int missing =
			   ((int) $1) - res.h->aut->num_states() + 1;
			 if (missing >= 0)
			   {
			     res.h->aut->new_states(missing);
812
813
			     res.info_states.resize
			       (res.info_states.size() + missing);
814
815
			   }
		       }
816
817
818
819
820
821
822
		     // Remember the first place were a state is the
		     // destination of a transition.
		     if (!res.info_states[$1].used)
		       {
			 res.info_states[$1].used = true;
			 res.info_states[$1].used_loc = @1;
		       }
823
824
825
		     $$ = $1;
		   }

826
states: | states state
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
        {
	  if (res.deterministic || res.complete)
	    {
	      bdd available = bddtrue;
	      bool det = true;
	      for (auto& t: res.h->aut->out(res.cur_state))
		{
		  if (det && !bdd_implies(t.cond, available))
		    det = false;
		  available -= t.cond;
		}
	      if (res.deterministic && !det)
		{
		  res.deterministic = false;
		  auto p = res.props.find("deterministic");
		  if (p != res.props.end())
		    {
		      error(@2, "automaton is not deterministic...");
		      error(p->second, "... despite 'property: deterministic'");
		    }
		}
	      if (res.complete && available != bddfalse)
		{
		  res.complete = false;
		  auto p = res.props.find("complete");
		  if (p != res.props.end())
		    {
		      error(@2, "automaton is not complete...");
		      error(p->second, "... despite 'property: complete'");
		    }
		}
	    }
	}
860
861
862
state: state-name labeled-edges
     | state-name unlabeled-edges
       {
863
	 if (!res.has_state_label) // Implicit labels
864
865
866
	   {
	     if (res.cur_guard != res.guards.end())
	       error(@$, "not enough transitions for this state");
867
868
869
870
871
872
873
874
875
876
877

	     if (res.label_style == State_Labels)
	       {
		 error(@2, "these transitions have implicit labels but the"
		       " automaton is...");
		 error(res.props["state-labels"], "... declared with "
		       "'property: state-labels'");
		 // Do not repeat this message.
		 res.label_style = Mixed_Labels;
	       }

878
879
880
	     res.cur_guard = res.guards.begin();
	   }
       }
881
     | error
882
883
884
885
886
       {
	 // Assume the worse.
	 res.deterministic = false;
	 res.complete = false;
       }
887

888

889
state-name: "State:" state-label_opt checked-state-num string_opt state-acc_opt
890
891
	  {
	    res.cur_state = $3;
892
	    if (res.info_states[$3].declared)
893
894
895
896
897
	      {
		std::ostringstream o;
		o << "redeclaration of state " << $3;
		error(@1 + @3, o.str());
	      }
898
	    res.info_states[$3].declared = true;
899
	    res.acc_state = $5;
900
901
902
903
904
905
906
907
908
909
910
	    if ($4)
	      {
		if (!res.state_names)
		  res.state_names =
		    new std::vector<std::string>(res.states > 0 ?
						 res.states : 0);
		if (res.state_names->size() < $3 + 1)
		  res.state_names->resize($3 + 1);
		(*res.state_names)[$3] = std::move(*$4);
		delete $4;
	      }
911
912
913
914
915
916
	  }
label: '[' label-expr ']'
	   {
             res.cur_label = bdd_from_int($2);
             bdd_delref($2);
	   }
917
918
919
920
921
     | '[' error ']'
           {
	     error(@$, "ignoring this invalid label");
	     res.cur_label = bddtrue;
	   }
922
state-label_opt:       { res.has_state_label = false; }
923
924
925
926
927
               | label
	       {
		 res.has_state_label = true;
		 res.state_label_loc = @1;
		 res.state_label = res.cur_label;
928
929
		 if (res.label_style == Trans_Labels
		     || res.label_style == Implicit_Labels)
930
931
932
		   {
		     error(@$,
			   "state label used although the automaton was...");
933
934
935
936
937
938
939
		     if (res.label_style == Trans_Labels)
		       error(res.props["trans-labels"],
			     "... declared with 'property: trans-labels' here");
		     else
		       error(res.props["implicit-labels"],
			     "... declared with 'property: implicit-labels' "
			     "here");
940
941
942
943
		     // Do not show this error anymore.
		     res.label_style = Mixed_Labels;
		   }
	       }
944
trans-label: label
945
946
947
	         {
		   if (res.has_state_label)
		     {
948
		       error(@1, "cannot label this edge because...");
949
950
		       error(res.state_label_loc,
			     "... the state is already labeled.");
951
		       res.cur_label = res.state_label;
952
		     }
953
954
		   if (res.label_style == State_Labels
		       || res.label_style == Implicit_Labels)
955
956
957
		     {
		       error(@$, "transition label used although the "
			     "automaton was...");
958
959
960
961
962
963
964
965
		       if (res.label_style == State_Labels)
			 error(res.props["state-labels"],
			       "... declared with 'property: state-labels' "
			       "here");
		       else
			 error(res.props["implicit-labels"],
			       "... declared with 'property: implicit-labels' "
			       "here");
966
967
968
		       // Do not show this error anymore.
		       res.label_style = Mixed_Labels;
		     }
969
970
		 }

971
acc-sig: '{' acc-sets '}'
972
973
974
975
976
977
978
979
980
981
	     {
	       $$ = $2;
	       if (res.ignore_acc && !res.ignore_acc_silent)
		 {
		   error(@$, "ignoring acceptance sets because of "
			 "missing acceptance condition");
		   // Emit this message only once.
		   res.ignore_acc_silent = true;
		 }
	     }
982
983
984
985
           | '{' error '}'
	     {
	       error(@$, "ignoring this invalid acceptance set");
	     }
986
987
988
989
acc-sets:
          {
	    $$ = spot::acc_cond::mark_t(0U);
	  }
990
991
        | acc-sets acc-set
	  {
992
993
994
995
	    if (res.ignore_acc || $2 == -1U)
	      $$ = spot::acc_cond::mark_t(0U);
	    else
	      $$ = $1 | res.h->aut->acc().mark($2);
996
	  }
997

998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
state-acc_opt:
               {
                 $$ = spot::acc_cond::mark_t(0U);
               }
             | acc-sig
               {
		 $$ = $1;
		 if (res.acc_style == Trans_Acc)
		   {
		     error(@$, "state-based acceptance used despite...");
		     error(res.props["trans-acc"],
			   "... declaration of transition-based acceptance.");
		     res.acc_style = Mixed_Acc;
		   }
	       }
trans-acc_opt:
               {
                 $$ = spot::acc_cond::mark_t(0U);
               }
             | acc-sig
               {
		 $$ = $1;
		 res.trans_acc_seen = true;
		 if (res.acc_style == State_Acc)
		   {
		     error(@$, "trans-based acceptance used despite...");
		     error(res.props["state-acc"],
			   "... declaration of state-based acceptance.");
		     res.acc_style = Mixed_Acc;
		   }
	       }

1030
1031
1032
1033
1034
/* block of labeled-edges, with occasional (incorrect) unlabeled edge */
labeled-edges: | some-labeled-edges
some-labeled-edges: labeled-edge
                  | some-labeled-edges labeled-edge
                  | some-labeled-edges incorrectly-unlabeled-edge
1035
incorrectly-unlabeled-edge: checked-state-num trans-acc_opt
1036
1037
1038
1039
1040
1041
1042
                            {
			      bdd cond = bddtrue;
			      if (!res.has_state_label)
				error(@$, "missing label for this edge "
				      "(previous edge is labeled)");
			      else
				cond = res.state_label;
1043
1044
			      res.h->aut->new_edge(res.cur_state, $1,
						   cond, $2 | res.acc_state);
1045
			    }
1046
labeled-edge: trans-label checked-state-num trans-acc_opt
1047
	      {
1048
		if (res.cur_label != bddfalse)
1049
1050
		  res.h->aut->new_edge(res.cur_state, $2,
				       res.cur_label, $3 | res.acc_state);
1051
	      }
1052
	    | trans-label state-conj-2 trans-acc_opt
1053
1054
1055
1056
1057
	      {
		error(@2, "alternation is not yet supported");
		YYABORT;
	      }

1058
1059
1060
1061
1062
1063
/* Block of unlabeled edge, with occasional (incorrect) labeled
   edge. We never have zero unlabeled edges, these are considered as
   zero labeled edges. */
unlabeled-edges: unlabeled-edge
               | unlabeled-edges unlabeled-edge
               | unlabeled-edges incorrectly-labeled-edge
1064
unlabeled-edge: checked-state-num trans-acc_opt
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
		{
		  bdd cond;
		  if (res.has_state_label)
		    {
		      cond = res.state_label;
		    }
		  else
		    {
		      if (res.guards.empty())
			fill_guards(res);
		      if (res.cur_guard == res.guards.end())
			{
1077
			  error(@$, "too many transitions for this state, "
1078
1079
1080
1081
1082
1083
1084
1085
				"ignoring this one");
			  cond = bddfalse;
			}
		      else
			{
			  cond = *res.cur_guard++;
			}
		    }
1086
		  if (cond != bddfalse)
1087
1088
		    res.h->aut->new_edge(res.cur_state, $1,
					 cond, $2 | res.acc_state);
1089
		}
1090
	      | state-conj-2 trans-acc_opt
1091
1092
1093
1094
		{
		  error(@1, "alternation is not yet supported");
		  YYABORT;
		}
1095
1096
1097
1098
1099
incorrectly-labeled-edge: trans-label unlabeled-edge
                          {
			    error(@1, "ignoring this label, because previous"
				  " edge has no label");
                          }
1100

1101
1102
1103
1104
1105

/**********************************************************************/
/*                   Rules for LTL2DSTAR's format                     */
/**********************************************************************/

1106
1107
1108
1109
1110
dstar: dstar_type "v2" "explicit" dstar_header "---" dstar_states ENDDSTAR
     | dstar_type error ENDDSTAR
       {
	 error(@$, "failed to parse this as an ltl2dstar automaton");
       }
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124

dstar_type: "DRA"
       {
         res.h->type = spot::parsed_aut_type::DRA;
         res.plus = 1;
         res.minus = 0;
       }
       | "DSA"
       {
	 res.h->type = spot::parsed_aut_type::DSA;
         res.plus = 0;
         res.minus = 1;
       }

1125
dstar_header: dstar_sizes
1126
1127
  {
    if (res.states < 0)
1128
      error(@1, "missing state count");
1129
    if (!res.ignore_more_acc)
1130
      error(@1, "missing acceptance-pair count");
1131
    if (res.start.empty())
1132
      error(@1, "missing start-state number");
1133
    if (!res.ignore_more_ap)
1134
1135
1136
      error(@1, "missing atomic propositions definition");

    if (res.states > 0)
1137
      {
1138
1139
	res.h->aut->new_states(res.states);;
	res.info_states.resize(res.states);
1140
1141
1142
1143
      }
    res.h->aut->prop_state_based_acc();
    res.h->aut->prop_deterministic();
    // res.h->aut->prop_complete();
1144
1145
    fill_guards(res);
    res.cur_guard = res.guards.end();
1146
1147
1148
  }

dstar_sizes:
1149
  | dstar_sizes error
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
  | dstar_sizes "Acceptance-Pairs:" INT
  {
    if (res.ignore_more_acc)
      {
	error(@1 + @2, "ignoring this redefinition of the "
	      "acceptance pairs...");
	error(res.accset_loc, "... previously defined here.");
      }
    else{
      res.accset = $3;
      res.h->aut->set_acceptance(2 * $3,
				 res.h->type == spot::parsed_aut_type::DRA
				 ? spot::acc_cond::acc_code::rabin($3)
				 : spot::acc_cond::acc_code::streett($3));
      res.accset_loc = @3;
      res.ignore_more_acc = true;
    }
  }
  | dstar_sizes "States:" INT
  {
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
    if (res.states < 0)
      {
	res.states = $3;
      }
    else
      {
	error(@$, "redeclaration of state count");
	if ((unsigned) res.states < $3)
	  res.states = $3;
      }
1180
1181
1182
1183
1184
1185
1186
  }
  | dstar_sizes "Start:" INT
  {
    res.start.emplace_back(@3, $3);
  }
  | dstar_sizes aps

1187
dstar_state_id: "State:" INT string_opt
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
  {
    if (res.cur_guard != res.guards.end())
      error(@1, "not enough transitions for previous state");
    if (res.states < 0 || $2 >= (unsigned) res.states)
      {
	std::ostringstream o;
	if (res.states > 0)
	  {
	    o << "state numbers should be in the range [0.."
	      << res.states - 1 << "]";
	  }
	else
	  {
	    o << "no states have been declared";
	  }
	error(@2, o.str());
      }
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
    else
      {
	res.info_states[$2].declared = true;

	if ($3)
	  {
	    if (!res.state_names)
	      res.state_names =
		new std::vector<std::string>(res.states > 0 ?
					     res.states : 0);
	    if (res.state_names->size() < $2 + 1)
	      res.state_names->resize($2 + 1);
	    (*res.state_names)[$2] = std::move(*$3);
	    delete $3;
	  }
      }

1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
    res.cur_guard = res.guards.begin();
    res.dest_map.clear();
    res.cur_state = $2;
  }

sign: '+' { $$ = res.plus; }
  |   '-' { $$ = res.minus; }

// Membership to a pair is represented as (+NUM,-NUM)
dstar_accsigs:
  {
    $$ = 0U;
  }
  | dstar_accsigs sign INT
  {
    if (res.states < 0 || res.cur_state >= (unsigned) res.states)
      break;
    if (res.accset > 0 && $3 < (unsigned) res.accset)
      {
	$$ = $1;
	$$.set($3 * 2 + $2);
      }
    else
      {
	std::ostringstream o;
	if (res.accset > 0)
	  {
	    o << "acceptance pairs should be in the range [0.."
	      << res.accset - 1 << "]";
	  }
	else
	  {
	    o << "no acceptance pairs have been declared";
	  }
	error(@3, o.str());
      }
  }

dstar_state_accsig: "Acc-Sig:" dstar_accsigs { $$ = $2; }

dstar_transitions:
  | dstar_transitions INT
  {
    std::pair<map_t::iterator, bool> i =
      res.dest_map.emplace($2, *res.cur_guard);
    if (!i.second)
      i.first->second |= *res.cur_guard;
    ++res.cur_guard;
  }

dstar_states:
1273
  | dstar_states error
1274
1275
1276
1277
1278
1279
  | dstar_states dstar_state_id dstar_state_accsig dstar_transitions
  {
    for (auto i: res.dest_map)
      res.h->aut->new_edge(res.cur_state, i.first, i.second, $3);
  }

1280
1281
1282
1283
1284
/**********************************************************************/
/*                      Rules for neverclaims                         */
/**********************************************************************/

never: "never" { res.namer = res.h->aut->create_namer<std::string>();
1285
	         res.h->aut->set_buchi();
1286
		 res.h->aut->prop_state_based_acc();
1287
1288
1289
		 res.acc_state = State_Acc;
		 res.pos_acc_sets = res.h->aut->acc().all_sets();
	       }
1290
1291
1292
1293
1294
1295
       '{' nc-states '}'
       {
	 // Add an accept_all state if needed.
	 if (res.accept_all_needed && !res.accept_all_seen)
	   {
	     unsigned n = res.namer->new_state("accept_all");
1296
	     res.h->aut->new_acc_edge(n, n, bddtrue);
1297
	   }
1298
1299
1300
1301
	 // If we aliased existing state, we have some unreachable
	 // states to remove.
	 if (res.aliased_states)
	   res.h->aut->purge_unreachable_states();
1302
1303
1304
1305
	 res.info_states.resize(res.h->aut->num_states());
	 // Pretend that we have declared all states.
	 for (auto& p: res.info_states)
	   p.declared = true;
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
       }

nc-states:
  /* empty */
  | nc-state
  | nc-states ';' nc-state
  | nc-states ';'

nc-one-ident: IDENTIFIER ':'
    {
      auto r = res.labels.insert(std::make_pair(*$1, @1));
      if (!r.second)
	{
	  error(@1, std::string("redefinition of ") + *$1 + "...");
	  error(r.first->second, std::string("... ")  + *$1
		+ " previously defined here");
	}
      $$ = $1;
    }

nc-ident-list: nc-one-ident
1327
1328
1329
1330
1331
1332
1333
1334
1335
    {
      unsigned n = res.namer->new_state(*$1);
      if (res.start.empty())
	{
	  // The first state is initial.
	  res.start.emplace_back(@$, n);
	}
      $$ = $1;
    }
1336
1337
  | nc-ident-list nc-one-ident
    {
1338
1339
      res.aliased_states |=
	res.namer->alias_state(res.namer->get_state(*$1), *$2);
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
      // Keep any identifier that starts with accept.
      if (strncmp("accept", $1->c_str(), 6))
        {
          delete $1;
          $$ = $2;
        }
      else
        {
	  delete $2;
	  $$ = $1;
        }
    }

nc-transition-block:
  "if" nc-transitions "fi"
    {
      $$ = $2;
    }
  | "do" nc-transitions "od"
    {
      $$ = $2;
    }

nc-state:
  nc-ident-list "skip"
    {
      if (*$1 == "accept_all")
	res.accept_all_seen = true;

1369
1370
      auto acc = !strncmp("accept", $1->c_str(), 6) ?
	res.h->aut->acc().all_sets() : spot::acc_cond::mark_t(0U);
1371
      res.namer->new_edge(*$1, *$1, bddtrue, acc);
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
      delete $1;
    }
  | nc-ident-list { delete $1; }
  | nc-ident-list "false" { delete $1; }
  | nc-ident-list nc-transition-block
    {
      auto acc = !strncmp("accept", $1->c_str(), 6) ?
	res.h->aut->acc().all_sets() : spot::acc_cond::mark_t(0U);
      for (auto& p: *$2)
	{
	  bdd c = bdd_from_int(p.first);
	  bdd_delref(p.first);
1384
	  res.namer->new_edge(*$1, *p.second, c, acc);
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
	  delete p.second;
	}
      delete $1;
      delete $2;
    }

nc-transitions:
  /* empty */ { $$ = new std::list<pair>; }
  | nc-transitions nc-transition
    {
      if ($2)
	{
	  $1->push_back(*$2);
	  delete $2;
	}
      $$ = $1;
    }

nc-formula-or-ident: FORMULA | IDENTIFIER

nc-formula: nc-formula-or-ident
     {
       auto i = res.fcache.find(*$1);
       if (i == res.fcache.end())
	 {
	   spot::ltl::parse_error_list pel;
1411
1412
	   auto f = spot::ltl::parse_infix_boolean(*$1, pel, *res.env,
						   debug_level(), true);
1413
1414
1415
1416
1417
	   for (auto& j: pel)
	     {
	       // Adjust the diagnostic to the current position.
	       spot::location here = @1;
	       here.end.line = here.begin.line + j.first.end.line - 1;
1418
	       here.end.column = here.begin.column + j.first.end.column - 1;
1419
	       here.begin.line += j.first.begin.line - 1;
1420
	       here.begin.column += j.first.begin.column - 1;
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
	       error_list.emplace_back(here, j.second);
	     }
           bdd cond = bddfalse;
	   if (f)
	     {
	       cond = spot::formula_to_bdd(f, res.h->aut->get_dict(),
					   res.h->aut);
	       f->destroy();
	     }
	   $$ = (res.fcache[*$1] = cond).id();
	 }
       else
	 {
	   $$ = i->second.id();
	 }
       bdd_addref($$);
       delete $1;
     }
   | "false"
     {
       $$ = 0;
     }

nc-opt-dest:
  /* empty */
    {
      $$ = 0;
    }
  | "->" "goto" IDENTIFIER
    {
      $$ = $3;
    }
  | "->" "assert" FORMULA
    {
      delete $3;
      $$ = new std::string("accept_all");
      res.accept_all_needed = true;
    }

nc-src-dest: nc-formula nc-opt-dest
    {
      // If there is no destination, do ignore the transition.
      // This happens for instance with
      //   if
      //   :: false
      //   fi
      if (!$2)
	{
	  $$ = 0;
	}
      else
	{
	  $$ = new pair($1, $2);
	  res.namer->new_state(*$2);
	}
    }

nc-transition:
  ':' ':' "atomic" '{' nc-src-dest '}'
    {
      $$ = $5;
    }
  | ':' ':' nc-src-dest
    {
      $$ = $3;
    }

1488
1489
1490
1491
1492
/**********************************************************************/
/*                         Rules for LBTT                             */
/**********************************************************************/

lbtt: lbtt-header lbtt-body ENDAUT
1493
      {
1494
1495
	auto& acc = res.h->aut->acc();
	unsigned num = acc.num_sets();
1496
	res.h->aut->set_generalized_buchi(num);
1497
	res.pos_acc_sets = acc.all_sets();
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
	assert(!res.states_map.empty());
	auto n = res.states_map.size();
	if (n != (unsigned) res.states)
	  {
	    std::ostringstream err;
	    err << res.states << " states have been declared, but "
		<< n << " different state numbers have been used";
	    error(@$, err.str());
	  }
	if (res.states_map.rbegin()->first > (unsigned) res.states)
	  {
1509
1510
	    // We have seen numbers larger that the total number of
	    // states in the automaton.  Usually this happens when the
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
	    // states are numbered from 1 instead of 0, but the LBTT
	    // documentation actually allow any number to be used.
	    // What we have done is to map all input state numbers 0
	    // <= .. < n to the digraph states with the same number,
	    // and any time we saw a number larger than n, we mapped
	    // it to a new state.  The correspondence is given by
	    // res.states_map.  Now we just need to remove the useless
	    // states we allocated.
	    std::vector<unsigned> rename(res.h->aut->num_states(), -1U);
	    unsigned s = 0;
	    for (auto& i: res.states_map)
	      rename[i.second] = s++;
	    assert(s == (unsigned) res.states);
	    for (auto& i: res.start)
	      i.second = rename[i.second];
	    res.h->aut->get_graph().defrag_states(std::move(rename), s);
	  }
1528
1529
1530
	 res.info_states.resize(res.h->aut->num_states());
	 for (auto& s: res.info_states)
	   s.declared = true;
1531
      }
1532
1533
    | lbtt-header-states LBTT_EMPTY
      {
1534
        res.h->aut->set_generalized_buchi($2);
1535
	res.pos_acc_sets = res.h->aut->acc().all_sets();
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
      }

lbtt-header-states: LBTT
                  {
		    res.states = $1;
		    res.states_loc = @1;
		    res.h->aut->new_states($1);
		  }
lbtt-header: lbtt-header-states INT_S
           {
	     res.acc_mapper = new spot::acc_mapper_int(res.h->aut, $2);
	     res.h->aut->prop_state_based_acc();
1548
	     res.acc_state = State_Acc;
1549
1550
1551
1552
	   }
           | lbtt-header-states INT
           {
	     res.acc_mapper = new spot::acc_mapper_int(res.h->aut, $2);
1553
	     res.trans_acc_seen = true;
1554
1555
1556
1557
	   }
lbtt-body: lbtt-states
lbtt-states:
           | lbtt-states lbtt-state lbtt-transitions
1558

1559
1560
lbtt-state: STATE_NUM INT lbtt-acc
          {
1561
	    if ($1 >= (unsigned) res.states)
1562
	      {
1563
1564
1565
1566
		auto p = res.states_map.emplace($1, 0);
		if (p.second)
		  p.first->second = res.h->aut->new_state();
		res.cur_state = p.first->second;
1567
	      }
1568
1569
1570
1571
1572
1573
1574
	    else
	      {
		res.states_map.emplace($1, $1);
		res.cur_state = $1;
	      }
	    if ($2)
	      res.start.emplace_back(@1 + @2, res.cur_state);
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
	    res.acc_state = $3;
	  }
lbtt-acc:               { $$ = 0U; }
        | lbtt-acc ACC
	{
	  $$  = $1;
	  auto p = res.acc_mapper->lookup($2);
	  if (p.first)
	    $$ |= p.second;
	  else
	    error(@2, "more acceptance sets used than declared");
	}
lbtt-guard: STRING
          {
	    spot::ltl::parse_error_list pel;
1590
	    auto* f = spot::ltl::parse_prefix_ltl(*$1, pel, *res.env);
1591
	    if (!f || !pel.empty())
1592
1593
1594
1595
1596
	      {
		std::string s = "failed to parse guard: ";
		s += *$1;
		error(@$, s);
	      }
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
	    if (!pel.empty())
	      for (auto& j: pel)
		{
		  // Adjust the diagnostic to the current position.
		  spot::location here = @1;
		  here.end.line = here.begin.line + j.first.end.line - 1;
		  here.end.column = here.begin.column + j.first.end.column - 1;
		  here.begin.line += j.first.begin.line - 1;
		  here.begin.column += j.first.begin.column - 1;
		  error_list.emplace_back(here, j.second);
		}
	    if (!f)
1609
1610
1611
1612
1613
	      {
		res.cur_label = bddtrue;
	      }
	    else
	      {
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
		if (!f->is_boolean())
		  {
		    error(@$,
			  "non-Boolean transition label (replaced by true)");
		    res.cur_label = bddtrue;
		  }
		else
		  {
		    res.cur_label =
		      formula_to_bdd(f, res.h->aut->get_dict(), res.h->aut);
		  }
1625
1626
1627
1628
1629
1630
1631
		f->destroy();
	      }
	    delete $1;
	  }
lbtt-transitions:
                | lbtt-transitions DEST_NUM lbtt-acc lbtt-guard
                {
1632
1633
		  unsigned dst = $2;
		  if (dst >= (unsigned) res.states)
1634
		    {
1635
1636
1637
1638
		      auto p = res.states_map.emplace(dst, 0);
		      if (p.second)
			p.first->second = res.h->aut->new_state();
		      dst = p.first->second;
1639
1640
		    }
		  else
1641
1642
1643
		    {
		      res.states_map.emplace(dst, dst);
	            }
1644
1645
1646
		  res.h->aut->new_edge(res.cur_state, dst,
				       res.cur_label,
				       res.acc_state | $3);
1647
1648
		}

1649
1650
%%

1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
static void fill_guards(result_& r)
{
  unsigned nap = r.ap.size();

  int* vars = new int[nap];
  for (unsigned i = 0; i < nap; ++i)
    vars[i] = r.ap[nap - 1 - i];

  // build the 2^nap possible guards
  r.guards.reserve(1U << nap);
  for (size_t i = 0; i < (1U << nap); ++i)
    r.guards.push_back(bdd_ibuildcube(i, nap, vars));
  r.cur_guard = r.guards.begin();

  delete[] vars;
}

1668
1669
void
hoayy::parser::error(const location_type& location,
1670
		     const std::string& message)
1671
1672
1673
1674
{
  error_list.emplace_back(location, message);
}

1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
static spot::acc_cond::acc_code
fix_acceptance_aux(spot::acc_cond& acc,
		   spot::acc_cond::acc_code in, unsigned pos,
		   spot::acc_cond::mark_t onlyneg,
		   spot::acc_cond::mark_t both,
		   unsigned base)
{
  auto& w = in[pos];
  switch (w.op)
    {
    case spot::acc_cond::acc_op::And:
      {
	unsigned sub = pos - w.size;
	--pos;
	auto c = fix_acceptance_aux(acc, in, pos, onlyneg, both, base);
	pos -= in[pos].size;
	while (sub < pos)
	  {
	    --pos;
	    c.append_and(fix_acceptance_aux(acc, in, pos, onlyneg, both, base));
	    pos -= in[pos].size;
	  }
	return c;
      }
    case spot::acc_cond::acc_op::Or:
      {
	unsigned sub = pos - w.size;
	--pos;
	auto c = fix_acceptance_aux(acc, in, pos, onlyneg, both, base);
	pos -= in[pos].size;
	while (sub < pos)
	  {
	    --pos;
	    c.append_or(fix_acceptance_aux(acc, in, pos, onlyneg, both, base));
	    pos -= in[pos].size;
	  }
	return c;
      }
    case spot::acc_cond::acc_op::Inf:
      return acc.inf(in[pos - 1].mark);
    case spot::acc_cond::acc_op::Fin:
      return acc.fin(in[pos - 1].mark);
    case spot::acc_cond::acc_op::FinNeg:
      {
	auto m = in[pos - 1].mark;
	auto c = acc.fin(onlyneg & m);
	spot::acc_cond::mark_t tmp = 0U;
	for (auto i: both.sets())
	  {
	    if (m.has(i))
	      tmp.set(base);
	    ++base;
	  }
	if (tmp)
	  c.append_or(acc.fin(tmp));
	return c;
      }
    case spot::acc_cond::acc_op::InfNeg:
      {
	auto m = in[pos - 1].mark;
	auto c = acc.inf(onlyneg & m);
	spot::acc_cond::mark_t tmp = 0U;
	for (auto i: both.sets())
	  {
	    if (m.has(i))
	      tmp.set(base);
	    ++base;
	  }
	if (tmp)
	  c.append_and(acc.inf(tmp));
	return c;
      }
    }
  SPOT_UNREACHABLE();
  return {};
}

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1752
1753
static void fix_acceptance(result_& r)
{
1754
1755
  auto& acc = r.h->aut->acc();

Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1756
1757
  // If a set x appears only as Inf(!x), we can complement it so that
  // we work with Inf(x) instead.
1758
1759
1760
  auto onlyneg = r.neg_acc_sets - r.pos_acc_sets;
  if (onlyneg)
    {
1761
      for (auto& t: r.h->aut->edge_vector())
1762
1763
	t.acc ^= onlyneg;
    }
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1764
1765
1766
1767
1768
1769

  // However if set x is used elsewhere, for instance in
  //   Inf(!x) & Inf(x)
  // complementing x would be wrong.  We need to create a
  // new set, y, that is the complement of x, and rewrite
  // this as Inf(y) & Inf(x).
1770
1771
1772
  auto both = r.neg_acc_sets & r.pos_acc_sets;
  unsigned base = 0;
  if (both)
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1773
1774
1775
    {
      auto v = acc.sets(both);
      auto vs = v.size();
1776
      base = acc.add_sets(vs);
1777
      for (auto& t: r.h->aut->edge_vector())
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1778
1779
	if ((t.acc & both) != both)
	  for (unsigned i = 0; i < vs; ++i)
1780
	    if (!t.acc.has(v[i]))
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1781
1782
	      t.acc |= acc.mark(base + i);
    }
1783

1784
  if (onlyneg || both)
1785
    {
1786
1787
1788
1789
1790
      auto& acc = r.h->aut->acc();
      auto code = acc.get_acceptance();
      r.h->aut->set_acceptance(acc.num_sets(),
			       fix_acceptance_aux(acc, code, code.size() - 1,
						  onlyneg, both, base));
1791
    }
Alexandre Duret-Lutz's avatar
Alexandre Duret-Lutz committed
1792
1793
}

1794
1795
static void fix_initial_state(result_& r)
{
1796
1797
1798
1799
1800

  std::vector<unsigned> start;
  start.reserve(r.start.size());
  for (auto& p : r.start)
    // Ignore initial states without declaration
1801
1802
    if (p.second < r.info_states.size()
	&& r.info_states[p.second].declared)
1803
1804
1805
      start.push_back(p.second);

  if (start.empty())
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
    {
      // If no initial state has been declared, add one, because
      // Spot will not work without one.
      r.h->aut->set_init_state(r.h->aut->new_state());
      return;
    }

  // Remove any duplicate initial state.
  std::sort(start.begin(), start.end());
  auto res = std::unique(start.begin(), start.end());
  start.resize(std::distance(start.begin(), res));

  assert(start.size() >= 1);
  if (start.size() == 1)
    {
      r.h->aut->set_init_state(start.front());
    }
  else
    {