@@ -90,7 +90,8 @@ Function: expr2smvt::convert_binary
90
90
expr2smvt::resultt expr2smvt::convert_binary (
91
91
const exprt &src,
92
92
const std::string &symbol,
93
- precedencet precedence)
93
+ precedencet precedence,
94
+ bool is_associative)
94
95
{
95
96
if (src.operands ().size ()<2 )
96
97
return convert_norep (src);
@@ -112,10 +113,18 @@ expr2smvt::resultt expr2smvt::convert_binary(
112
113
113
114
auto op_rec = convert_rec (*it);
114
115
115
- if (precedence > op_rec.p )
116
+ // clang-format off
117
+ bool use_parentheses =
118
+ src.id () == it->id () && is_associative ? false
119
+ : precedence >= op_rec.p ;
120
+ // clang-format on
121
+
122
+ if (use_parentheses)
116
123
dest += ' (' ;
124
+
117
125
dest += op_rec.s ;
118
- if (precedence > op_rec.p )
126
+
127
+ if (use_parentheses)
119
128
dest += ' )' ;
120
129
}
121
130
@@ -434,14 +443,14 @@ Function: expr2smvt::convert_rec
434
443
expr2smvt::resultt expr2smvt::convert_rec (const exprt &src)
435
444
{
436
445
if (src.id ()==ID_plus)
437
- return convert_binary (src, " +" , precedencet::PLUS);
446
+ return convert_binary (src, " +" , precedencet::PLUS, true );
438
447
439
448
else if (src.id ()==ID_minus)
440
449
{
441
450
if (src.operands ().size ()<2 )
442
451
return convert_norep (src);
443
452
else
444
- return convert_binary (src, " -" , precedencet::PLUS);
453
+ return convert_binary (src, " -" , precedencet::PLUS, true );
445
454
}
446
455
447
456
else if (src.id ()==ID_unary_minus)
@@ -455,57 +464,60 @@ expr2smvt::resultt expr2smvt::convert_rec(const exprt &src)
455
464
else if (src.id ()==ID_index)
456
465
return convert_index (to_index_expr (src), precedencet::INDEX);
457
466
458
- else if (src.id ()==ID_mult || src.id ()==ID_div)
459
- return convert_binary (src, src.id_string (), precedencet::MULT);
467
+ else if (src.id () == ID_mult)
468
+ return convert_binary (src, src.id_string (), precedencet::MULT, true );
469
+
470
+ else if (src.id () == ID_div)
471
+ return convert_binary (src, src.id_string (), precedencet::MULT, false );
460
472
461
473
else if (src.id () == ID_mod)
462
- return convert_binary (src, src.id_string (), precedencet::MULT);
474
+ return convert_binary (src, src.id_string (), precedencet::MULT, false );
463
475
464
476
else if (src.id () == ID_smv_setin)
465
- return convert_binary (src, " in" , precedencet::IN);
477
+ return convert_binary (src, " in" , precedencet::IN, false );
466
478
467
479
else if (src.id () == ID_smv_setnotin)
468
- return convert_binary (src, " notin" , precedencet::IN);
480
+ return convert_binary (src, " notin" , precedencet::IN, false );
469
481
470
482
else if (src.id () == ID_smv_union)
471
- return convert_binary (src, " union" , precedencet::UNION);
483
+ return convert_binary (src, " union" , precedencet::UNION, false );
472
484
473
485
else if (src.id ()==ID_lt || src.id ()==ID_gt ||
474
486
src.id ()==ID_le || src.id ()==ID_ge)
475
- return convert_binary (src, src.id_string (), precedencet::REL);
487
+ return convert_binary (src, src.id_string (), precedencet::REL, false );
476
488
477
489
else if (src.id ()==ID_equal)
478
490
{
479
491
if (src.get_bool (ID_C_smv_iff))
480
- return convert_binary (src, " <->" , precedencet::IFF);
492
+ return convert_binary (src, " <->" , precedencet::IFF, false );
481
493
else
482
- return convert_binary (src, " =" , precedencet::REL);
494
+ return convert_binary (src, " =" , precedencet::REL, false );
483
495
}
484
496
485
497
else if (src.id ()==ID_notequal)
486
- return convert_binary (src, " !=" , precedencet::REL);
498
+ return convert_binary (src, " !=" , precedencet::REL, false );
487
499
488
500
else if (src.id ()==ID_not)
489
501
return convert_unary (to_not_expr (src), " !" , precedencet::NOT);
490
502
491
503
else if (src.id () == ID_and || src.id () == ID_bitand)
492
- return convert_binary (src, " &" , precedencet::AND);
504
+ return convert_binary (src, " &" , precedencet::AND, true );
493
505
494
506
else if (src.id () == ID_or || src.id () == ID_bitor)
495
- return convert_binary (src, " |" , precedencet::OR);
507
+ return convert_binary (src, " |" , precedencet::OR, true );
496
508
497
509
else if (src.id () == ID_implies || src.id () == ID_smv_bitimplies)
498
- return convert_binary (src, " ->" , precedencet::IMPLIES);
510
+ return convert_binary (src, " ->" , precedencet::IMPLIES, false );
499
511
500
512
else if (src.id () == ID_xor || src.id () == ID_bitxor)
501
- return convert_binary (src, " xor" , precedencet::OR);
513
+ return convert_binary (src, " xor" , precedencet::OR, true );
502
514
503
515
else if (src.id () == ID_xnor || src.id () == ID_bitxnor)
504
516
{
505
517
if (src.get_bool (ID_C_smv_iff))
506
- return convert_binary (src, " <->" , precedencet::IFF);
518
+ return convert_binary (src, " <->" , precedencet::IFF, false );
507
519
else
508
- return convert_binary (src, " xnor" , precedencet::OR);
520
+ return convert_binary (src, " xnor" , precedencet::OR, false );
509
521
}
510
522
511
523
else if (
@@ -540,7 +552,7 @@ expr2smvt::resultt expr2smvt::convert_rec(const exprt &src)
540
552
src.id () == ID_ER || src.id () == ID_U)
541
553
{
542
554
return convert_binary (
543
- to_binary_expr (src), src.id_string (), precedencet::TEMPORAL);
555
+ to_binary_expr (src), src.id_string (), precedencet::TEMPORAL, false );
544
556
}
545
557
546
558
else if (
@@ -565,15 +577,17 @@ expr2smvt::resultt expr2smvt::convert_rec(const exprt &src)
565
577
else if (src.id () == ID_R)
566
578
{
567
579
// LTL release is "V" in NuSMV
568
- return convert_binary (to_binary_expr (src), " V" , precedencet::TEMPORAL);
580
+ return convert_binary (
581
+ to_binary_expr (src), " V" , precedencet::TEMPORAL, false );
569
582
}
570
583
571
584
else if (src.id () == ID_smv_S || src.id () == ID_smv_T)
572
585
{
573
586
return convert_binary (
574
587
to_binary_expr (src),
575
588
std::string (src.id_string (), 4 , std::string::npos),
576
- precedencet::TEMPORAL);
589
+ precedencet::TEMPORAL,
590
+ false );
577
591
}
578
592
579
593
else if (src.id () == ID_if)
@@ -608,17 +622,17 @@ expr2smvt::resultt expr2smvt::convert_rec(const exprt &src)
608
622
609
623
else if (src.id () == ID_concatenation)
610
624
{
611
- return convert_binary (to_binary_expr (src), " ::" , precedencet::CONCAT);
625
+ return convert_binary (to_binary_expr (src), " ::" , precedencet::CONCAT, true );
612
626
}
613
627
614
628
else if (src.id () == ID_shl)
615
629
{
616
- return convert_binary (to_binary_expr (src), " <<" , precedencet::SHIFT);
630
+ return convert_binary (to_binary_expr (src), " <<" , precedencet::SHIFT, false );
617
631
}
618
632
619
633
else if (src.id () == ID_lshr || src.id () == ID_ashr)
620
634
{
621
- return convert_binary (to_binary_expr (src), " >>" , precedencet::SHIFT);
635
+ return convert_binary (to_binary_expr (src), " >>" , precedencet::SHIFT, false );
622
636
}
623
637
624
638
else if (src.id () == ID_smv_extend)
0 commit comments