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
std::vector< symbol_exprt > variablest
Definition: std_expr.h:3109
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
bool empty() const
Definition: dstring.h:89
Base class for all expressions.
Definition: expr.h:56
std::vector< exprt > operandst
Definition: expr.h:58
const irep_idt & id() const
Definition: irep.h:388
The NIL expression.
Definition: std_expr.h:3081
exprt function_application_ieee_float_op(const irep_idt &, const exprt::operandst &)
std::size_t parenthesis_level
Definition: smt2_parser.h:94
void command(const std::string &)
std::map< irep_idt, named_termt > named_termst
Definition: smt2_parser.h:73
exprt::operandst operands()
void command_sequence()
Definition: smt2_parser.cpp:44
exprt bv_mod(const exprt::operandst &, bool is_signed)
exprt binary(irep_idt, const exprt::operandst &)
exprt bv_division(const exprt::operandst &, bool is_signed)
void skip_to_end_of_list()
This skips tokens until all bracketed expressions are closed.
Definition: smt2_parser.cpp:37
std::unordered_map< std::string, std::function< exprt()> > expressions
Definition: smt2_parser.h:149
smt2_parsert(std::istream &_in)
Definition: smt2_parser.h:23
exprt lambda_expression()
typet function_signature_declaration()
named_termst named_terms
Definition: smt2_parser.h:74
id_mapt id_map
Definition: smt2_parser.h:58
std::unordered_map< std::string, std::function< void()> > commands
Definition: smt2_parser.h:190
void setup_sorts()
smt2_tokenizert::smt2_errort error(const std::string &message) const
Definition: smt2_parser.h:78
exprt function_application()
void add_unique_id(irep_idt, exprt)
exprt cast_bv_to_unsigned(const exprt &)
Apply typecast to unsignedbv to given expression.
exprt::operandst cast_bv_to_signed(const exprt::operandst &)
Apply typecast to signedbv to expressions in vector.
exprt multi_ary(irep_idt, const exprt::operandst &)
std::map< irep_idt, idt > id_mapt
Definition: smt2_parser.h:57
void ignore_command()
Definition: smt2_parser.cpp:89
exprt quantifier_expression(irep_idt)
std::pair< binding_exprt::variablest, exprt > binding(irep_idt)
exprt function_application_ieee_float_eq(const exprt::operandst &)
exprt let_expression()
void check_matching_operand_types(const exprt::operandst &) const
signature_with_parameter_idst function_signature_definition()
exprt function_application_fp(const exprt::operandst &)
typet function_sort()
void parse()
Definition: smt2_parser.h:31
exprt expression()
smt2_tokenizert::smt2_errort error() const
Definition: smt2_parser.h:83
void setup_expressions()
exprt binary_predicate(irep_idt, const exprt::operandst &)
smt2_tokenizert::tokent next_token()
Definition: smt2_parser.cpp:25
std::unordered_map< std::string, std::function< typet()> > sorts
Definition: smt2_parser.h:186
smt2_tokenizert smt2_tokenizer
Definition: smt2_parser.h:92
exprt unary(irep_idt, const exprt::operandst &)
void setup_commands()
enum { NONE, END_OF_FILE, STRING_LITERAL, NUMERAL, SYMBOL, KEYWORD, OPEN, CLOSE } tokent
smt2_errort error(const std::string &message) const
generate an error exception, pre-filled with a message
Expression to hold a symbol (variable)
Definition: std_expr.h:131
The type of an expression, extends irept.
Definition: type.h:29
Mathematical types.
const mathematical_function_typet & to_mathematical_function_type(const typet &type)
Cast a typet to a mathematical_function_typet.
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
API to expression classes.
idt(kindt _kind, typet __type)
Definition: smt2_parser.h:45
idt(kindt _kind, const exprt &expr)
Definition: smt2_parser.h:40
enum { VARIABLE, BINDING, PARAMETER } kindt
Definition: smt2_parser.h:38
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
std::vector< std::pair< irep_idt, typet > > ids_and_types() const
Definition: smt2_parser.h:123
binding_exprt::variablest binding_variables() const
Definition: smt2_parser.h:139
signature_with_parameter_idst(const typet &_type, const std::vector< irep_idt > &_parameters)
Definition: smt2_parser.h:110
signature_with_parameter_idst(const typet &_type)
Definition: smt2_parser.h:106
bool is_signed(const typet &t)
Convenience function – is the type signed?
Definition: util.cpp:45