dReal4
driver.h
1 #include <istream>
2 #include <string>
3 #include <vector>
4 
5 #include "dreal/dr/location.hh"
6 #include "dreal/dr/scanner.h"
7 #include "dreal/solver/context.h"
8 #include "dreal/util/scoped_unordered_map.h"
9 
10 namespace dreal {
11 
12 /** The DrDriver class brings together all components. It creates an
13  * instance of the Parser and Scanner classes and connects them. Then
14  * the input stream is fed into the scanner object and the parser gets
15  * it's token sequence. Furthermore the driver object is available in
16  * the grammar rules as a parameter. Therefore the driver class
17  * contains a reference to the structure into which the parsed data is
18  * saved. */
19 class DrDriver {
20  public:
21  /// construct a new parser driver context
22  DrDriver() = default;
23  explicit DrDriver(Context context);
24 
25  /** Invoke the scanner and parser for a stream.
26  * @param in input stream
27  * @param sname stream name for error messages
28  * @return true if successfully parsed
29  */
30  bool parse_stream(std::istream& in,
31  const std::string& sname = "stream input");
32 
33  /** Invoke the scanner and parser on an input string.
34  * @param input input string
35  * @param sname stream name for error messages
36  * @return true if successfully parsed
37  */
38  bool parse_string(const std::string& input,
39  const std::string& sname = "string stream");
40 
41  /** Invoke the scanner and parser on a file. Use parse_stream with a
42  * std::ifstream if detection of file reading errors is required.
43  * @param filename input file name
44  * @return true if successfully parsed
45  */
46  bool parse_file(const std::string& filename);
47 
48  // To demonstrate pure handling of parse errors, instead of
49  // simply dumping them on the standard error output, we will pass
50  // them to the driver using the following two member functions.
51 
52  /** Error handling with associated line number. This can be modified to
53  * output the error e.g. to a dialog box. */
54  static void error(const location& l, const std::string& m);
55 
56  /** General error handling. This can be modified to output the error
57  * e.g. to a dialog box. */
58  static void error(const std::string& m);
59 
60  /// Returns a variable associated with a name @p name.
61  ///
62  /// @throws if no variable is associated with @p name.
63  const Variable& lookup_variable(const std::string& name);
64 
65  /// Declare a variable @p v.
66  void DeclareVariable(const Variable& v);
67 
68  /// Declare a variable @p v which is bounded by an interval `[lb, ub]`.
69  void DeclareVariable(const Variable& v, const Expression& lb,
70  const Expression& ub);
71 
72  /// Asserts a formula @p f.
73  void Assert(const Formula& f);
74 
75  /// Asserts a formula maximizing a cost function @p f.
76  void Minimize(const Expression& f);
77 
78  /// Solves the constructed problem.
79  void Solve();
80 
81  /// enable debug output in the flex scanner
82  bool trace_scanning_{false};
83 
84  /// enable debug output in the bison parser
85  bool trace_parsing_{false};
86 
87  /// stream name (file or input stream) used for error messages.
88  std::string streamname_;
89 
90  /** Pointer to the current scanenr instance, this is used to connect the
91  * parser to the scanner. It is used in the yylex macro. */
92  DrScanner* scanner_{nullptr};
93 
94  private:
95  /** The context filled during parsing of the expressions. */
96  Context context_;
97  std::vector<Formula> constraints_;
98  std::vector<Expression> objectives_;
99  /** Scoped map from a string to a corresponding Variable. */
101 };
102 
103 } // namespace dreal
Context class that holds a set of constraints and provide Assert/Push/Pop/CheckSat functionalities...
Definition: context.h:23
void Assert(const Formula &f)
Asserts a formula f.
Definition: driver.cc:80
static void error(const location &l, const std::string &m)
Error handling with associated line number.
Definition: driver.cc:55
The DrDriver class brings together all components.
Definition: driver.h:19
bool parse_file(const std::string &filename)
Invoke the scanner and parser on a file.
Definition: driver.cc:38
void Minimize(const Expression &f)
Asserts a formula maximizing a cost function f.
Definition: driver.cc:82
Sum type of symbolic::Expression and symbolic::Formula.
Definition: api.cc:9
bool parse_string(const std::string &input, const std::string &sname="string stream")
Invoke the scanner and parser on an input string.
Definition: driver.cc:50
Represents a symbolic variable.
Definition: symbolic_variable.h:16
bool parse_stream(std::istream &in, const std::string &sname="stream input")
Invoke the scanner and parser for a stream.
Definition: driver.cc:26
Backtrackable map.
Definition: scoped_unordered_map.h:19
DrScanner * scanner_
Pointer to the current scanenr instance, this is used to connect the parser to the scanner...
Definition: driver.h:92
const Variable & lookup_variable(const std::string &name)
Returns a variable associated with a name name.
Definition: driver.cc:61
DrDriver()=default
construct a new parser driver context
std::string streamname_
stream name (file or input stream) used for error messages.
Definition: driver.h:88
void Solve()
Solves the constructed problem.
Definition: driver.cc:84
void DeclareVariable(const Variable &v)
Declare a variable v.
Definition: driver.cc:69
Represents a symbolic form of an expression.
Definition: symbolic_expression.h:164
DrScanner is a derived class to add some extra function to the scanner class.
Definition: scanner.h:34
bool trace_scanning_
enable debug output in the flex scanner
Definition: driver.h:82
bool trace_parsing_
enable debug output in the bison parser
Definition: driver.h:85
Represents a symbolic form of a first-order logic formula.
Definition: symbolic_formula.h:101