SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
Parser.h
Go to the documentation of this file.
1 /**
2  * @file Parser.h
3  * @author Gereon Kremer <gereon.kremer@cs.rwth-aachen.de>
4  */
5 
6 #pragma once
7 
8 
9 #include "Common.h"
10 #include "InstructionHandler.h"
11 #include "Script.h"
12 #include "theories/ParserState.h"
13 #include "theories/Theories.h"
14 
15 namespace smtrat {
16 namespace parser {
18 {
19 
20 private:
21  std::istream* mInputStream;
22 
23 public:
29 public:
30 
34  state(handler),
35  theories(&state),
36  parser(handler, theories, *this)
37  {
38  }
39 
41  }
42 
43  bool parse(std::istream& in) {
44  in.unsetf(std::ios::skipws);
45  mInputStream = &in;
46  Skipper skipper;
47  BaseIteratorType basebegin(in);
48  Iterator begin(basebegin);
49  Iterator end;
50  if (qi::phrase_parse(begin, end, parser, skipper)) {
52  return true;
53  } else {
54  SMTRAT_LOG_WARN("smtrat.parser", "Remaining to parse: \"" << std::string(begin, end) << "\"");
55  return false;
56  }
57  }
58 
59  void add(const types::TermType& t, bool isSoftFormula = false, Rational weight = Rational(1), const std::string id = std::string()) {
60  SMTRAT_LOG_DEBUG("smtrat.parser", "Adding " << t << " as " << (isSoftFormula ? "soft" : "hard") << " constraint.");
61  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(assert " << t << ")");
62  FormulaT f;
64  if (!conv(t, f)) {
65  SMTRAT_LOG_ERROR("smtrat.parser", "assert requires it's argument to be a formula, but it is \"" << t << "\".");
66  return;
67  }
68 
69  // Check if there are global formulas to be added.
70  // These may be due to ite expressions or alike.
71  FormulasT additional;
72  theories.addGlobalFormulas(additional);
73  if (!additional.empty()) {
74  additional.push_back(f);
75  f = FormulaT(carl::FormulaType::AND, std::move(additional));
76  }
77 
78  if (isSoftFormula) {
80  } else {
82  }
83 
84  }
85 
86  void check() {
87  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(check-sat)");
89  }
90  void declareConst(const std::string& name, const carl::Sort& sort) {
91  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(declare-const " << name << " " << sort << ")");
93  }
94  void declareFun(const std::string& name, const std::vector<carl::Sort>& args, const carl::Sort& sort) {
95  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(declare-fun " << name << " () " << sort << ")");
96  if (args.size() == 0) {
98  } else {
99  theories.declareFunction(name, args, sort);
100  }
101  }
102  void declareSort(const std::string& name, Integer arity) {
103  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(declare-sort " << name << " " << arity << ")");
104  if (!carl::SortManager::getInstance().declare(name, carl::to_int<carl::uint>(arity))) {
105  SMTRAT_LOG_ERROR("smtrat.parser", "A sort \"" << name << "\" with arity " << arity << " has already been declared.");
106  }
107  }
108  void echo(const std::string& s) {
109  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(echo \"" << s << "\")");
111  }
112  void qe() {
113  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(apply qe)");
115  }
116  void exit() {
117  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(exit)");
118  this->mInputStream->setstate(std::ios::eofbit);
120  }
121  void getAllModels() {
122  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(get-all-models)");
124  }
125  void getAssertions() {
126  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(get-assertions)");
128  }
129  void getAssignment() {
130  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(get-assignment)");
132  }
133  void getInfo(const std::string& key) {
134  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(get-info " << key << ")");
136  }
137  void getModel() {
138  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(get-model)");
140  }
141  void getObjectives() {
142  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(get-objectives)");
144  }
145  void getOption(const std::string& key) {
146  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(get-option " << key << ")");
148  }
149  void getProof() {
150  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(get-proof)");
152  }
153  void getUnsatCore() {
154  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(get-unsat-core)");
156  }
157  void getValue([[maybe_unused]] const std::vector<types::TermType>& vars) {
158  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(get-value " << vars << ")");
159  // TODO callHandler(&InstructionHandler::getValue, vars);
160  }
162  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(" << ot << " " << t << ")");
163  Poly p;
165  if (!conv(t, p)) {
166  SMTRAT_LOG_ERROR("smtrat.parser", "objectives are required to be arithmetic, but it is \"" << t << "\".");
167  return;
168  }
170  }
171  void pop(const Integer& n) {
172  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(pop " << n << ")");
173  auto nint = carl::to_int<carl::uint>(n);
174  if (nint > state.script_scope_size()) {
175  SMTRAT_LOG_ERROR("smtrat.parser", "Can not pop " << nint << " scopes, we only have " << state.script_scope_size() << " right now.");
176  handler.error() << "Can not pop " << nint << " scopes, we only have " << state.script_scope_size() << " right now.";
177  return;
178  }
179  theories.popScriptScope(nint);
181  }
182  void push(const Integer& n) {
183  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(push " << n << ")");
184  theories.pushScriptScope(carl::to_int<carl::uint>(n));
185  callHandler(&InstructionHandler::push, carl::to_int<carl::uint>(n));
186  }
187  void reset() {
188  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(reset)");
189  state.reset();
191  }
193  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(reset-assertions)");
195  }
196  void setInfo(const Attribute& attribute) {
197  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(set-info :" << attribute << ")");
199  }
200  void setLogic(const carl::Logic& name) {
201  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(set-logic " << name << ")");
202  state.logic = name;
204  }
205  void setOption(const Attribute& option) {
206  if (handler.printInstruction()) SMTRAT_LOG_INFO("smtrat.parser", "(set-option " << option << ")");
208  }
209 
210  template<typename Function, typename... Args>
211  void callHandler(const Function& f, const Args&... args) {
212  if (queueInstructions) {
213  handler.addInstruction(std::bind(f, &handler, args...));
214  } else {
215  (handler.*f)(args...);
216  }
217  }
218 };
219 
220 }
221 }
Represents an Attribute.
Definition: Attribute.h:14
void setOption(const Attribute &option)
virtual void add(const FormulaT &f)=0
void getOption(const std::string &key)
void getInfo(const std::string &key)
virtual void addSoft(const FormulaT &f, Rational weight, const std::string &id)=0
void setArtificialVariables(std::vector< smtrat::ModelVariable > &&vars)
void setInfo(const Attribute &attr)
virtual void echo(const std::string &s)
void addInstruction(std::function< void()> bind)
virtual void setLogic(const carl::Logic &)=0
virtual void pop(std::size_t)=0
virtual void push(std::size_t)=0
virtual void addObjective(const Poly &p, OptimizationType ot)=0
void getValue([[maybe_unused]] const std::vector< types::TermType > &vars)
Definition: Parser.h:157
void setOption(const Attribute &option)
Definition: Parser.h:205
std::istream * mInputStream
Definition: Parser.h:21
void declareFun(const std::string &name, const std::vector< carl::Sort > &args, const carl::Sort &sort)
Definition: Parser.h:94
void echo(const std::string &s)
Definition: Parser.h:108
void declareConst(const std::string &name, const carl::Sort &sort)
Definition: Parser.h:90
ScriptParser< SMTLIBParser > parser
Definition: Parser.h:28
void add(const types::TermType &t, bool isSoftFormula=false, Rational weight=Rational(1), const std::string id=std::string())
Definition: Parser.h:59
void getInfo(const std::string &key)
Definition: Parser.h:133
void push(const Integer &n)
Definition: Parser.h:182
void addObjective(const types::TermType &t, OptimizationType ot)
Definition: Parser.h:161
void callHandler(const Function &f, const Args &... args)
Definition: Parser.h:211
void setInfo(const Attribute &attribute)
Definition: Parser.h:196
void setLogic(const carl::Logic &name)
Definition: Parser.h:200
SMTLIBParser(InstructionHandler &handler, bool queueInstructions)
Definition: Parser.h:31
void pop(const Integer &n)
Definition: Parser.h:171
bool parse(std::istream &in)
Definition: Parser.h:43
InstructionHandler & handler
Definition: Parser.h:25
void getOption(const std::string &key)
Definition: Parser.h:145
void declareSort(const std::string &name, Integer arity)
Definition: Parser.h:102
void sort(T *array, int size, LessThan lt)
Definition: Sort.h:67
carl::mpl_variant_of< TermTypes >::type TermType
Variant type for all terms.
Definition: TheoryTypes.h:160
boost::spirit::istream_iterator BaseIteratorType
Definition: Common.h:47
PositionIteratorType Iterator
Definition: Common.h:49
std::vector< carl::Variable > vars(const std::pair< QuantifierType, std::vector< carl::Variable >> &p)
Definition: QEQuery.h:43
Class to create the formulas for axioms.
carl::Formula< Poly > FormulaT
Definition: types.h:37
carl::MultivariatePolynomial< Rational > Poly
Definition: types.h:25
carl::Formulas< Poly > FormulasT
Definition: types.h:39
mpq_class Rational
Definition: types.h:19
carl::IntegralType< Rational >::type Integer
Definition: types.h:21
#define SMTRAT_LOG_WARN(channel, msg)
Definition: logging.h:33
#define SMTRAT_LOG_INFO(channel, msg)
Definition: logging.h:34
#define SMTRAT_LOG_ERROR(channel, msg)
Definition: logging.h:32
#define SMTRAT_LOG_DEBUG(channel, msg)
Definition: logging.h:35
std::size_t script_scope_size() const
Definition: ParserState.h:108
std::vector< smtrat::ModelVariable > artificialVariables
Definition: ParserState.h:72
The Theories class combines all implemented theories and provides a single interface to interact with...
Definition: Theories.h:28
void addGlobalFormulas(FormulasT &formulas)
Definition: Theories.h:103
void declareVariable(const std::string &name, const carl::Sort &sort)
Definition: Theories.h:108
void pushScriptScope(std::size_t n)
Definition: Theories.h:206
void popScriptScope(std::size_t n)
Definition: Theories.h:209
void declareFunction(const std::string &name, const std::vector< carl::Sort > &args, const carl::Sort &sort)
Definition: Theories.h:122
Converts variants to some type using the Converter class.
Definition: Conversions.h:99