@@ -41,6 +41,8 @@ Author: Daniel Kroening, kroening@kroening.com
4141#include < solvers/lowering/expr_lowering.h>
4242#include < solvers/prop/literal_expr.h>
4343
44+ #include " smt2_tokenizer.h"
45+
4446// Mark different kinds of error conditions
4547
4648// Unexpected types and other combinations not implemented and not
@@ -218,7 +220,7 @@ void smt2_convt::write_footer()
218220 if (solver!=solvert::BOOLECTOR)
219221 {
220222 for (const auto &id : smt2_identifiers)
221- out << " (get-value (| " << id << " | ))"
223+ out << " (get-value (" << id << " ))"
222224 << " \n " ;
223225 }
224226
@@ -260,7 +262,7 @@ void smt2_convt::define_object_size(
260262 << " ((_ extract " << h << " " << l << " ) " ;
261263 convert_expr (ptr);
262264 out << " ) (_ bv" << number << " " << config.bv_encoding .object_bits << " ))"
263- << " (= | " << id << " | (_ bv" << *object_size << " " << size_width
265+ << " (= " << id << " (_ bv" << *object_size << " " << size_width
264266 << " ))))\n " ;
265267
266268 ++number;
@@ -837,16 +839,17 @@ literalt smt2_convt::convert(const exprt &expr)
837839 out << " () Bool)\n " ;
838840 out << " (assert (= " ;
839841 convert_literal (l);
842+ out << ' ' ;
840843 convert_expr (prepared_expr);
841844 out << " ))\n " ;
842845 }
843846 else
844847 {
845- defined_expressions[expr] =
846- std::string{" | B" } + std::to_string (l.var_no ()) + " | " ;
847- out << " (define-fun " ;
848- convert_literal (l );
849- out << " () Bool " ;
848+ auto identifier =
849+ convert_identifier ( std::string{" B" } + std::to_string (l.var_no ())) ;
850+ defined_expressions[expr] = identifier ;
851+ smt2_identifiers. insert (identifier );
852+ out << " (define-fun " << identifier << " () Bool " ;
850853 convert_expr (prepared_expr);
851854 out << " )\n " ;
852855 }
@@ -874,12 +877,15 @@ void smt2_convt::convert_literal(const literalt l)
874877 if (l.sign ())
875878 out << " (not " ;
876879
877- out << " |B" << l.var_no () << " |" ;
880+ const auto identifier =
881+ convert_identifier (" B" + std::to_string (l.var_no ()));
882+
883+ out << identifier;
878884
879885 if (l.sign ())
880886 out << " )" ;
881887
882- smt2_identifiers.insert (" B " + std::to_string (l. var_no ()) );
888+ smt2_identifiers.insert (identifier );
883889 }
884890}
885891
@@ -900,18 +906,37 @@ void smt2_convt::pop()
900906 assumptions.clear ();
901907}
902908
909+ static bool is_smt2_simple_identifier (const std::string &identifier)
910+ {
911+ if (identifier.empty ())
912+ return false ;
913+
914+ if (isdigit (identifier[0 ]))
915+ return false ;
916+
917+ for (auto ch : id2string (identifier))
918+ {
919+ if (!is_smt2_simple_symbol_character (ch))
920+ return false ;
921+ }
922+
923+ return true ;
924+ }
925+
903926std::string smt2_convt::convert_identifier (const irep_idt &identifier)
904927{
928+ // Is this a "simple identifier"?
929+ if (is_smt2_simple_identifier (id2string (identifier)))
930+ return id2string (identifier);
931+
905932 // Backslashes are disallowed in quoted symbols just for simplicity.
906933 // Otherwise, for Common Lisp compatibility they would have to be treated
907934 // as escaping symbols.
908935
909- std::string result;
936+ std::string result = " | " ;
910937
911- for (std:: size_t i= 0 ; i<identifier. size (); i++ )
938+ for (auto ch : identifier )
912939 {
913- char ch=identifier[i];
914-
915940 switch (ch)
916941 {
917942 case ' |' :
@@ -928,6 +953,8 @@ std::string smt2_convt::convert_identifier(const irep_idt &identifier)
928953 }
929954 }
930955
956+ result += ' |' ;
957+
931958 return result;
932959}
933960
@@ -989,7 +1016,7 @@ void smt2_convt::convert_floatbv(const exprt &expr)
9891016 if (expr.id ()==ID_symbol)
9901017 {
9911018 const irep_idt &id = to_symbol_expr (expr).get_identifier ();
992- out << ' | ' << convert_identifier (id) << ' | ' ;
1019+ out << convert_identifier (id);
9931020 return ;
9941021 }
9951022
@@ -1003,9 +1030,9 @@ void smt2_convt::convert_floatbv(const exprt &expr)
10031030 INVARIANT (
10041031 !expr.operands ().empty (), " non-symbol expressions shall have operands" );
10051032
1006- out << " (|float_bv. " << expr. id ()
1007- << floatbv_suffix (expr)
1008- << ' | ' ;
1033+ out << ' ( '
1034+ << convert_identifier (
1035+ " float_bv. " + expr. id_string () + floatbv_suffix (expr)) ;
10091036
10101037 forall_operands (it, expr)
10111038 {
@@ -1023,13 +1050,13 @@ void smt2_convt::convert_expr(const exprt &expr)
10231050 {
10241051 const irep_idt &id = to_symbol_expr (expr).get_identifier ();
10251052 DATA_INVARIANT (!id.empty (), " symbol must have identifier" );
1026- out << ' | ' << convert_identifier (id) << ' | ' ;
1053+ out << convert_identifier (id);
10271054 }
10281055 else if (expr.id ()==ID_nondet_symbol)
10291056 {
10301057 const irep_idt &id = to_nondet_symbol_expr (expr).get_identifier ();
10311058 DATA_INVARIANT (!id.empty (), " nondet symbol must have identifier" );
1032- out << ' | ' << convert_identifier (" nondet_" + id2string (id)) << ' | ' ;
1059+ out << convert_identifier (" nondet_" + id2string (id));
10331060 }
10341061 else if (expr.id ()==ID_smt2_symbol)
10351062 {
@@ -2149,7 +2176,7 @@ void smt2_convt::convert_expr(const exprt &expr)
21492176 else if (
21502177 const auto object_size = expr_try_dynamic_cast<object_size_exprt>(expr))
21512178 {
2152- out << " | " << object_sizes[*object_size] << " | " ;
2179+ out << object_sizes[*object_size];
21532180 }
21542181 else if (expr.id ()==ID_let)
21552182 {
@@ -4619,7 +4646,7 @@ void smt2_convt::set_to(const exprt &expr, bool value)
46194646 smt2_identifiers.insert (smt2_identifier);
46204647
46214648 out << " ; set_to true (equal)\n " ;
4622- out << " (define-fun | " << smt2_identifier << ' | ' ;
4649+ out << " (define-fun " << smt2_identifier;
46234650
46244651 if (equal_expr.lhs ().type ().id () == ID_mathematical_function)
46254652 {
@@ -4803,7 +4830,7 @@ void smt2_convt::find_symbols(const exprt &expr)
48034830 smt2_identifiers.insert (smt2_identifier);
48044831
48054832 out << " ; find_symbols\n " ;
4806- out << " (declare-fun | " << smt2_identifier << ' | ' ;
4833+ out << " (declare-fun " << smt2_identifier;
48074834
48084835 if (expr.type ().id () == ID_mathematical_function)
48094836 {
@@ -4982,8 +5009,9 @@ void smt2_convt::find_symbols(const exprt &expr)
49825009 {
49835010 if (object_sizes.find (*object_size) == object_sizes.end ())
49845011 {
4985- const irep_idt id = " object_size." + std::to_string (object_sizes.size ());
4986- out << " (declare-fun |" << id << " | () " ;
5012+ const irep_idt id = convert_identifier (
5013+ " object_size." + std::to_string (object_sizes.size ()));
5014+ out << " (declare-fun " << id << " () " ;
49875015 convert_type (object_size->type ());
49885016 out << " )"
49895017 << " \n " ;
@@ -5016,8 +5044,8 @@ void smt2_convt::find_symbols(const exprt &expr)
50165044 to_multi_ary_expr (expr).op0 ().type ().id () == ID_floatbv)))
50175045 // clang-format on
50185046 {
5019- irep_idt function=
5020- " | float_bv." + expr.id_string ()+ floatbv_suffix (expr)+ " | " ;
5047+ irep_idt function =
5048+ convert_identifier ( " float_bv." + expr.id_string () + floatbv_suffix (expr)) ;
50215049
50225050 if (bvfp_set.insert (function).second )
50235051 {
0 commit comments