Skip to content

Commit baba52b

Browse files
committed
SMT2: do not quote simple symbols
The |...| for quoting are unneccesary when the symbol name meets the definition of a simple symbol.
1 parent c594d07 commit baba52b

File tree

5 files changed

+40
-19
lines changed

5 files changed

+40
-19
lines changed

src/solvers/smt2/smt2_conv.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Author: Daniel Kroening, [email protected]
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
@@ -904,18 +906,37 @@ void smt2_convt::pop()
904906
assumptions.clear();
905907
}
906908

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+
907926
std::string smt2_convt::convert_identifier(const irep_idt &identifier)
908927
{
928+
// Is this a "simple identifier"?
929+
if(is_smt2_simple_identifier(id2string(identifier)))
930+
return id2string(identifier);
931+
909932
// Backslashes are disallowed in quoted symbols just for simplicity.
910933
// Otherwise, for Common Lisp compatibility they would have to be treated
911934
// as escaping symbols.
912935

913936
std::string result = "|";
914937

915-
for(std::size_t i=0; i<identifier.size(); i++)
938+
for(auto ch : identifier)
916939
{
917-
char ch=identifier[i];
918-
919940
switch(ch)
920941
{
921942
case '|':

src/solvers/smt2/smt2_tokenizer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Author: Daniel Kroening, [email protected]
88

99
#include "smt2_tokenizer.h"
1010

11-
bool smt2_tokenizert::is_simple_symbol_character(char ch)
11+
bool is_smt2_simple_symbol_character(char ch)
1212
{
1313
// any non-empty sequence of letters, digits and the characters
1414
// ~ ! @ $ % ^ & * _ - + = < > . ? /
@@ -32,7 +32,7 @@ smt2_tokenizert::tokent smt2_tokenizert::get_simple_symbol()
3232
char ch;
3333
while(in->get(ch))
3434
{
35-
if(is_simple_symbol_character(ch))
35+
if(is_smt2_simple_symbol_character(ch))
3636
{
3737
buffer+=ch;
3838
}
@@ -294,7 +294,7 @@ void smt2_tokenizert::get_token_from_stream()
294294
token = get_decimal_numeral();
295295
return;
296296
}
297-
else if(is_simple_symbol_character(ch))
297+
else if(is_smt2_simple_symbol_character(ch))
298298
{
299299
in->unget();
300300
token = get_simple_symbol();

src/solvers/smt2/smt2_tokenizer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ class smt2_tokenizert
120120
tokent get_simple_symbol();
121121
tokent get_quoted_symbol();
122122
tokent get_string_literal();
123-
static bool is_simple_symbol_character(char);
124123

125124
/// read a token from the input stream and store it in 'token'
126125
void get_token_from_stream();
@@ -135,4 +134,6 @@ operator<<(smt2_tokenizert::smt2_errort &&e, const T &message)
135134
return std::move(e);
136135
}
137136

137+
bool is_smt2_simple_symbol_character(char);
138+
138139
#endif // CPROVER_SOLVERS_SMT2_SMT2_PARSER_H

unit/solvers/smt2/smt2_conv.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ TEST_CASE(
1212
"abcdefghijklmnopqrstuvwxyz0123456789$";
1313
CHECK(
1414
smt2_convt::convert_identifier(no_escaping_characters) ==
15-
"|" + no_escaping_characters + "|");
15+
no_escaping_characters);
1616
CHECK(smt2_convt::convert_identifier("\\") == "|&92;|");
1717
CHECK(smt2_convt::convert_identifier("|") == "|&124;|");
18-
CHECK(smt2_convt::convert_identifier("&") == "|&38;|");
18+
CHECK(smt2_convt::convert_identifier("&") == "&");
1919
}

unit/solvers/smt2_incremental/smt_to_smt2_string.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
TEST_CASE("Test smt_indext to string conversion", "[core][smt2_incremental]")
1515
{
16-
CHECK(smt_to_smt2_string(smt_symbol_indext{"green"}) == "|green|");
16+
CHECK(smt_to_smt2_string(smt_symbol_indext{"green"}) == "green");
1717
CHECK(smt_to_smt2_string(smt_numeral_indext{42}) == "42");
1818
}
1919

@@ -36,7 +36,7 @@ TEST_CASE(
3636
{
3737
CHECK(
3838
smt_to_smt2_string(smt_bit_vector_theoryt::extract(7, 3)(
39-
smt_bit_vector_constant_termt{0, 8})) == "((_ |extract| 7 3) (_ bv0 8))");
39+
smt_bit_vector_constant_termt{0, 8})) == "((_ extract 7 3) (_ bv0 8))");
4040
}
4141

4242
TEST_CASE(
@@ -55,7 +55,7 @@ TEST_CASE(
5555
{
5656
CHECK(
5757
smt_to_smt2_string(smt_identifier_termt{"abc", smt_bool_sortt{}}) ==
58-
"|abc|");
58+
"abc");
5959
CHECK(
6060
smt_to_smt2_string(smt_identifier_termt{"\\", smt_bool_sortt{}}) ==
6161
"|&92;|");
@@ -67,7 +67,7 @@ TEST_CASE(
6767
"foo",
6868
smt_bool_sortt{},
6969
{smt_symbol_indext{"bar"}, smt_numeral_indext{42}}}) ==
70-
"(_ |foo| |bar| 42)");
70+
"(_ foo bar 42)");
7171
}
7272
}
7373

@@ -78,8 +78,7 @@ TEST_CASE(
7878
CHECK(
7979
smt_to_smt2_string(smt_core_theoryt::equal(
8080
smt_identifier_termt{"foo", smt_bit_vector_sortt{32}},
81-
smt_identifier_termt{"bar", smt_bit_vector_sortt{32}})) ==
82-
"(|=| |foo| |bar|)");
81+
smt_identifier_termt{"bar", smt_bit_vector_sortt{32}})) == "(= foo bar)");
8382
}
8483

8584
TEST_CASE(
@@ -102,7 +101,7 @@ TEST_CASE(
102101
{
103102
CHECK(
104103
smt_to_smt2_string(smt_get_value_commandt{
105-
smt_identifier_termt{"foo", smt_bool_sortt{}}}) == "(get-value (|foo|))");
104+
smt_identifier_termt{"foo", smt_bool_sortt{}}}) == "(get-value (foo))");
106105
}
107106

108107
TEST_CASE(
@@ -138,7 +137,7 @@ TEST_CASE(
138137
smt_to_smt2_string(smt_declare_function_commandt{
139138
smt_identifier_termt{"f", smt_bit_vector_sortt{31}},
140139
{smt_bit_vector_sortt{32}, smt_bit_vector_sortt{33}}}) ==
141-
"(declare-fun |f| ((_ BitVec 32) (_ BitVec 33)) (_ BitVec 31))");
140+
"(declare-fun f ((_ BitVec 32) (_ BitVec 33)) (_ BitVec 31))");
142141
}
143142

144143
TEST_CASE(
@@ -151,8 +150,8 @@ TEST_CASE(
151150
CHECK(
152151
smt_to_smt2_string(smt_define_function_commandt{
153152
"f", {g, h}, smt_core_theoryt::equal(g, h)}) ==
154-
"(define-fun |f| ((|g| (_ BitVec 32)) (|h| (_ BitVec 32))) Bool (|=| |g| "
155-
"|h|))");
153+
"(define-fun f ((g (_ BitVec 32)) (h (_ BitVec 32))) Bool (= g "
154+
"h))");
156155
}
157156

158157
TEST_CASE(

0 commit comments

Comments
 (0)