CBMC
smt2_parser.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #ifndef CPROVER_SOLVERS_SMT2_SMT2_PARSER_H
10 #define CPROVER_SOLVERS_SMT2_SMT2_PARSER_H
11 
12 #include <map>
13 #include <unordered_map>
14 
16 #include <util/std_expr.h>
17 
18 #include "smt2_tokenizer.h"
19 
21 {
22 public:
23  explicit smt2_parsert(std::istream &_in)
24  : exit(false), smt2_tokenizer(_in), parenthesis_level(0)
25  {
27  setup_sorts();
29  }
30 
31  void parse()
32  {
34  }
35 
36  struct idt
37  {
38  using kindt = enum { VARIABLE, BINDING, PARAMETER };
39 
40  idt(kindt _kind, const exprt &expr)
41  : kind(_kind), type(expr.type()), definition(expr)
42  {
43  }
44 
45  idt(kindt _kind, typet __type)
46  : kind(_kind), type(std::move(__type)), definition(nil_exprt())
47  {
48  }
49 
52 
53  // this is a lambda when the symbol is a function
55  };
56 
57  using id_mapt=std::map<irep_idt, idt>;
59 
60  struct named_termt
61  {
64  named_termt(const exprt &_term, const symbol_exprt &_name)
65  : term(_term), name(_name)
66  {
67  }
68 
71  };
72 
73  using named_termst = std::map<irep_idt, named_termt>;
75 
76  bool exit;
77 
78  smt2_tokenizert::smt2_errort error(const std::string &message) const
79  {
80  return smt2_tokenizer.error(message);
81  }
82 
84  {
85  return smt2_tokenizer.error();
86  }
87 
89  void skip_to_end_of_list();
90 
91 protected:
93  // we extend next_token to track the parenthesis level
94  std::size_t parenthesis_level;
96 
97  // add the given identifier to the id_map but
98  // complain if that identifier is used already
100 
102  {
104  std::vector<irep_idt> parameters;
105 
106  explicit signature_with_parameter_idst(const typet &_type) : type(_type)
107  {
108  }
109 
111  const typet &_type,
112  const std::vector<irep_idt> &_parameters)
113  : type(_type), parameters(_parameters)
114  {
115  PRECONDITION(
116  (_type.id() == ID_mathematical_function &&
117  to_mathematical_function_type(_type).domain().size() ==
118  _parameters.size()) ||
119  (_type.id() != ID_mathematical_function && _parameters.empty()));
120  }
121 
122  // a convenience helper for iterating over identifiers and types
123  std::vector<std::pair<irep_idt, typet>> ids_and_types() const
124  {
125  if(parameters.empty())
126  return {};
127  else
128  {
129  std::vector<std::pair<irep_idt, typet>> result;
130  result.reserve(parameters.size());
131  const auto &domain = to_mathematical_function_type(type).domain();
132  for(std::size_t i = 0; i < parameters.size(); i++)
133  result.emplace_back(parameters[i], domain[i]);
134  return result;
135  }
136  }
137 
138  // convenience helper for constructing a binding
140  {
142  for(auto &pair : ids_and_types())
143  result.emplace_back(pair.first, pair.second);
144  return result;
145  }
146  };
147 
148  // expressions
149  std::unordered_map<std::string, std::function<exprt()>> expressions;
150  void setup_expressions();
151  exprt expression();
154  const irep_idt &,
155  const exprt::operandst &);
167  exprt bv_mod(const exprt::operandst &, bool is_signed);
168 
169  std::pair<binding_exprt::variablest, exprt> binding(irep_idt);
174  const symbol_exprt &function,
175  const exprt::operandst &op);
176 
179 
182 
183  // sorts
184  typet sort();
186  std::unordered_map<std::string, std::function<typet()>> sorts;
187  void setup_sorts();
188 
189  // hashtable for all commands
190  std::unordered_map<std::string, std::function<void()>> commands;
191 
192  void command_sequence();
193  void command(const std::string &);
194  void ignore_command();
195  void setup_commands();
196 };
197 
198 #endif // CPROVER_SOLVERS_SMT2_SMT2_PARSER_H
smt2_parsert::command
void command(const std::string &)
Definition: smt2_parser.cpp:1534
smt2_parsert::check_matching_operand_types
void check_matching_operand_types(const exprt::operandst &) const
Definition: smt2_parser.cpp:348
smt2_parsert::quantifier_expression
exprt quantifier_expression(irep_idt)
Definition: smt2_parser.cpp:284
smt2_parsert::setup_sorts
void setup_sorts()
Definition: smt2_parser.cpp:1401
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
smt2_parsert::setup_commands
void setup_commands()
Definition: smt2_parser.cpp:1546
smt2_parsert::bv_division
exprt bv_division(const exprt::operandst &, bool is_signed)
Definition: smt2_parser.cpp:891
smt2_tokenizert::error
smt2_errort error(const std::string &message) const
generate an error exception, pre-filled with a message
Definition: smt2_tokenizer.h:93
smt2_tokenizert::tokent
enum { NONE, END_OF_FILE, STRING_LITERAL, NUMERAL, SYMBOL, KEYWORD, OPEN, CLOSE } tokent
Definition: smt2_tokenizer.h:66
typet
The type of an expression, extends irept.
Definition: type.h:28
smt2_parsert::sorts
std::unordered_map< std::string, std::function< typet()> > sorts
Definition: smt2_parser.h:186
smt2_parsert::expressions
std::unordered_map< std::string, std::function< exprt()> > expressions
Definition: smt2_parser.h:149
smt2_parsert::ignore_command
void ignore_command()
Definition: smt2_parser.cpp:89
smt2_parsert::function_application
exprt function_application()
Definition: smt2_parser.cpp:488
is_signed
bool is_signed(const typet &t)
Convenience function – is the type signed?
Definition: util.cpp:45
smt2_parsert
Definition: smt2_parser.h:20
smt2_parsert::signature_with_parameter_idst::signature_with_parameter_idst
signature_with_parameter_idst(const typet &_type, const std::vector< irep_idt > &_parameters)
Definition: smt2_parser.h:110
smt2_parsert::binary
exprt binary(irep_idt, const exprt::operandst &)
Definition: smt2_parser.cpp:393
to_mathematical_function_type
const mathematical_function_typet & to_mathematical_function_type(const typet &type)
Cast a typet to a mathematical_function_typet.
Definition: mathematical_types.h:119
exprt
Base class for all expressions.
Definition: expr.h:55
smt2_parsert::parse
void parse()
Definition: smt2_parser.h:31
smt2_parsert::idt::definition
exprt definition
Definition: smt2_parser.h:54
smt2_parsert::commands
std::unordered_map< std::string, std::function< void()> > commands
Definition: smt2_parser.h:190
smt2_parsert::named_terms
named_termst named_terms
Definition: smt2_parser.h:74
smt2_parsert::idt::kind
kindt kind
Definition: smt2_parser.h:50
smt2_parsert::function_sort
typet function_sort()
Definition: smt2_parser.cpp:1330
smt2_tokenizert
Definition: smt2_tokenizer.h:15
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:112
smt2_parsert::function_signature_definition
signature_with_parameter_idst function_signature_definition()
Definition: smt2_parser.cpp:1472
smt2_parsert::sort
typet sort()
Definition: smt2_parser.cpp:1356
smt2_parsert::exit
bool exit
Definition: smt2_parser.h:76
smt2_parsert::multi_ary
exprt multi_ary(irep_idt, const exprt::operandst &)
Definition: smt2_parser.cpp:363
smt2_parsert::unary
exprt unary(irep_idt, const exprt::operandst &)
Definition: smt2_parser.cpp:385
smt2_parsert::add_unique_id
void add_unique_id(irep_idt, exprt)
Definition: smt2_parser.cpp:134
smt2_parsert::idt::type
typet type
Definition: smt2_parser.h:51
smt2_parsert::command_sequence
void command_sequence()
Definition: smt2_parser.cpp:44
mathematical_types.h
smt2_parsert::binding
std::pair< binding_exprt::variablest, exprt > binding(irep_idt)
Definition: smt2_parser.cpp:218
smt2_parsert::parenthesis_level
std::size_t parenthesis_level
Definition: smt2_parser.h:94
smt2_parsert::function_signature_declaration
typet function_signature_declaration()
Definition: smt2_parser.cpp:1511
smt2_parsert::signature_with_parameter_idst::ids_and_types
std::vector< std::pair< irep_idt, typet > > ids_and_types() const
Definition: smt2_parser.h:123
mathematical_function_typet::domain
domaint & domain()
Definition: mathematical_types.h:72
smt2_parsert::function_application_fp
exprt function_application_fp(const exprt::operandst &)
Definition: smt2_parser.cpp:451
smt2_parsert::let_expression
exprt let_expression()
Definition: smt2_parser.cpp:148
smt2_parsert::setup_expressions
void setup_expressions()
Definition: smt2_parser.cpp:1019
smt2_tokenizer.h
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
smt2_parsert::binary_predicate
exprt binary_predicate(irep_idt, const exprt::operandst &)
Definition: smt2_parser.cpp:375
nil_exprt
The NIL expression.
Definition: std_expr.h:3025
smt2_parsert::named_termt::named_termt
named_termt(const exprt &_term, const symbol_exprt &_name)
Default-constructing a symbol_exprt is deprecated, thus make sure we always construct a named_termt f...
Definition: smt2_parser.h:64
smt2_parsert::named_termt
Definition: smt2_parser.h:60
smt2_parsert::function_application_ieee_float_op
exprt function_application_ieee_float_op(const irep_idt &, const exprt::operandst &)
Definition: smt2_parser.cpp:422
smt2_parsert::cast_bv_to_signed
exprt::operandst cast_bv_to_signed(const exprt::operandst &)
Apply typecast to signedbv to expressions in vector.
Definition: smt2_parser.cpp:313
binding_exprt::variablest
std::vector< symbol_exprt > variablest
Definition: std_expr.h:3054
irept::id
const irep_idt & id() const
Definition: irep.h:396
exprt::operandst
std::vector< exprt > operandst
Definition: expr.h:58
smt2_parsert::signature_with_parameter_idst::signature_with_parameter_idst
signature_with_parameter_idst(const typet &_type)
Definition: smt2_parser.h:106
smt2_parsert::id_mapt
std::map< irep_idt, idt > id_mapt
Definition: smt2_parser.h:57
smt2_parsert::signature_with_parameter_idst
Definition: smt2_parser.h:101
smt2_parsert::named_termst
std::map< irep_idt, named_termt > named_termst
Definition: smt2_parser.h:73
smt2_parsert::function_application_ieee_float_eq
exprt function_application_ieee_float_eq(const exprt::operandst &)
Definition: smt2_parser.cpp:403
smt2_parsert::lambda_expression
exprt lambda_expression()
Definition: smt2_parser.cpp:278
smt2_parsert::expression
exprt expression()
Definition: smt2_parser.cpp:949
smt2_tokenizert::smt2_errort
Definition: smt2_tokenizer.h:24
smt2_parsert::cast_bv_to_unsigned
exprt cast_bv_to_unsigned(const exprt &)
Apply typecast to unsignedbv to given expression.
Definition: smt2_parser.cpp:336
smt2_parsert::idt
Definition: smt2_parser.h:36
smt2_parsert::idt::kindt
enum { VARIABLE, BINDING, PARAMETER } kindt
Definition: smt2_parser.h:38
smt2_parsert::signature_with_parameter_idst::type
typet type
Definition: smt2_parser.h:103
smt2_parsert::next_token
smt2_tokenizert::tokent next_token()
Definition: smt2_parser.cpp:25
smt2_parsert::signature_with_parameter_idst::parameters
std::vector< irep_idt > parameters
Definition: smt2_parser.h:104
smt2_parsert::operands
exprt::operandst operands()
Definition: smt2_parser.cpp:122
smt2_parsert::smt2_tokenizer
smt2_tokenizert smt2_tokenizer
Definition: smt2_parser.h:92
smt2_parsert::bv_mod
exprt bv_mod(const exprt::operandst &, bool is_signed)
Definition: smt2_parser.cpp:920
smt2_parsert::signature_with_parameter_idst::binding_variables
binding_exprt::variablest binding_variables() const
Definition: smt2_parser.h:139
smt2_parsert::idt::idt
idt(kindt _kind, const exprt &expr)
Definition: smt2_parser.h:40
std_expr.h
smt2_parsert::id_map
id_mapt id_map
Definition: smt2_parser.h:58
smt2_parsert::error
smt2_tokenizert::smt2_errort error(const std::string &message) const
Definition: smt2_parser.h:78
smt2_parsert::skip_to_end_of_list
void skip_to_end_of_list()
This skips tokens until all bracketed expressions are closed.
Definition: smt2_parser.cpp:37
smt2_parsert::named_termt::name
symbol_exprt name
Definition: smt2_parser.h:70
smt2_parsert::smt2_parsert
smt2_parsert(std::istream &_in)
Definition: smt2_parser.h:23
smt2_parsert::error
smt2_tokenizert::smt2_errort error() const
Definition: smt2_parser.h:83
smt2_parsert::named_termt::term
exprt term
Definition: smt2_parser.h:69
smt2_parsert::idt::idt
idt(kindt _kind, typet __type)
Definition: smt2_parser.h:45