SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
Script.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Common.h"
4 #include "Attribute.h"
5 #include "InstructionHandler.h"
6 #include "Lexicon.h"
7 #include "Sort.h"
8 #include "Term.h"
9 
10 namespace smtrat {
11 namespace parser {
12 
13 struct LogicParser: public qi::symbols<char, carl::Logic> {
15  add("QF_BV", carl::Logic::QF_BV);
16  add("QF_IDL", carl::Logic::QF_IDL);
17  add("QF_LIA", carl::Logic::QF_LIA);
18  add("QF_LIRA", carl::Logic::QF_LIA);
19  add("QF_LRA", carl::Logic::QF_LRA);
20  add("QF_NIA", carl::Logic::QF_NIA);
21  add("QF_NIRA", carl::Logic::QF_NIA);
22  add("QF_NRA", carl::Logic::QF_NRA);
23  add("QF_PB", carl::Logic::QF_PB);
24  add("QF_RDL", carl::Logic::QF_RDL);
25  add("QF_UF", carl::Logic::QF_UF);
26  add("NRA", carl::Logic::NRA);
27  add("LRA", carl::Logic::LRA);
28  }
29 };
30 struct ErrorHandler {
31  template<typename> struct result { typedef qi::error_handler_result type; };
32  template<typename T1, typename T2, typename T3, typename T4>
33  qi::error_handler_result operator()(T1 b, T2 e, T3 where, T4 const& what) const {
34  auto line_start = spirit::get_line_start(b, where);
35  auto line_end = std::find(where, e, '\n');
36  std::string line(++line_start, line_end);
37 
38  SMTRAT_LOG_ERROR("smtrat.parser", "Parsing error at " << spirit::get_line(where) << ":" << spirit::get_column(line_start, where));
39  SMTRAT_LOG_ERROR("smtrat.parser", "expected" << std::endl << "\t" << what.tag << ": " << what);
40  SMTRAT_LOG_ERROR("smtrat.parser", "but got" << std::endl << "\t" << std::string(where, line_end));
41  SMTRAT_LOG_ERROR("smtrat.parser", "in line " << spirit::get_line(where) << std::endl << "\t" << line);
42  return qi::fail;
43  }
44 };
45 
46 template<typename Callee>
47 struct ScriptParser: public qi::grammar<Iterator, Skipper> {
49  ScriptParser::base_type(main, "script"),
50  handler(h),
51  callee(callee),
52  state(h),
54  term(&theories)
55  {
56  functionDefinitionArg = sortedvariable[qi::_val = px::bind(&Theories::declareFunctionArgument, px::ref(theories), qi::_1)];
58  (
59  qi::eps[px::bind(&ScriptParser::startFunctionDefinition, px::ref(*this))] >
60  symbol > "(" > *(functionDefinitionArg) > ")" > sort > term > ")" >
61  qi::eps[px::bind(&Theories::popScriptScope, px::ref(theories), 1)]
62  )[px::bind(&Theories::defineFunction, px::ref(theories), qi::_1, qi::_2, qi::_3, qi::_4)];
63  command = qi::lit("(") >> (
64  (qi::lit("assert-soft") >> term >> ")")[px::bind(&Callee::add, px::ref(callee), qi::_1, true, Rational(1), std::string())]
65  | (qi::lit("assert-soft") >> term >> qi::lit(":weight") >> decimal >> ")")[px::bind(&Callee::add, px::ref(callee), qi::_1, true, qi::_2, std::string())]
66  | (qi::lit("assert-soft") >> term >> qi::lit(":weight") >> decimal >> qi::lit(":id") >> symbol >> ")")[px::bind(&Callee::add, px::ref(callee), qi::_1, true, qi::_2, qi::_3)]
67  | (qi::lit("assert-soft") >> term >> qi::lit(":id") >> symbol >> qi::lit(":weight") >> decimal >> ")")[px::bind(&Callee::add, px::ref(callee), qi::_1, true, qi::_3, qi::_2)]
68  | (qi::lit("assert-soft") >> term >> qi::lit(":id") >> symbol >> ")")[px::bind(&Callee::add, px::ref(callee), qi::_1, true, Rational(1), qi::_2)]
69  | (qi::lit("assert") > term > ")")[px::bind(&Callee::add, px::ref(callee), qi::_1, false, Rational(-1), std::string())]
70  | (qi::lit("check-sat") > ")")[px::bind(&Callee::check, px::ref(callee))]
71  | (qi::lit("declare-const") > symbol > sort > ")")[px::bind(&Callee::declareConst, px::ref(callee), qi::_1, qi::_2)]
72  | (qi::lit("declare-fun") > symbol > "(" > *sort > ")" > sort > ")")[px::bind(&Callee::declareFun, px::ref(callee), qi::_1, qi::_2, qi::_3)]
73  | (qi::lit("declare-sort") > symbol > numeral > ")")[px::bind(&Callee::declareSort, px::ref(callee), qi::_1, qi::_2)]
74  | (qi::lit("define-fun") > functionDefinition)
75  //| (qi::lit("define-sort") > symbol > "(" > (*symbol)[px::bind(&SortParser::setParameters, px::ref(sort), qi::_1)] > ")" > sort > ")")[px::bind(&ScriptParser::defineSort, px::ref(callee), qi::_1, qi::_2, qi::_3)]
76  | (qi::lit("echo") > string > ")")[px::bind(&Callee::echo, px::ref(callee), qi::_1)]
77  | (qi::lit("apply qe") >> ")")[px::bind(&Callee::qe, px::ref(callee))]
78  | (qi::lit("exit") > ")")[px::bind(&Callee::exit, px::ref(callee))]
79  | (qi::lit("get-all-models") > ")")[px::bind(&Callee::getAllModels, px::ref(callee))]
80  | (qi::lit("get-assertions") > ")")[px::bind(&Callee::getAssertions, px::ref(callee))]
81  | (qi::lit("get-assignment") > ")")[px::bind(&Callee::getAssignment, px::ref(callee))]
82  | (qi::lit("get-info") > keyword > ")")[px::bind(&Callee::getInfo, px::ref(callee), qi::_1)]
83  | (qi::lit("get-model") > ")")[px::bind(&Callee::getModel, px::ref(callee))]
84  | (qi::lit("get-objectives") > ")")[px::bind(&Callee::getObjectives, px::ref(callee))]
85  | (qi::lit("get-option") > keyword > ")")[px::bind(&Callee::getOption, px::ref(callee), qi::_1)]
86  | (qi::lit("get-proof") > ")")[px::bind(&Callee::getProof, px::ref(callee))]
87  | (qi::lit("get-unsat-core") > ")")[px::bind(&Callee::getUnsatCore, px::ref(callee))]
88  | (qi::lit("get-value") > "(" > +term > ")" > ")")[px::bind(&Callee::getValue, px::ref(callee), qi::_1)]
89  | (qi::lit("maximize") > term > ")")[px::bind(&Callee::addObjective, px::ref(callee), qi::_1, OptimizationType::Maximize)]
90  | (qi::lit("minimize") > term > ")")[px::bind(&Callee::addObjective, px::ref(callee), qi::_1, OptimizationType::Minimize)]
91  | (qi::lit("pop") > (numeral | qi::attr(carl::constant_one<Integer>::get())) > ")")[px::bind(&Callee::pop, px::ref(callee), qi::_1)]
92  | (qi::lit("push") > (numeral | qi::attr(carl::constant_one<Integer>::get())) > ")")[px::bind(&Callee::push, px::ref(callee), qi::_1)]
93  | (qi::lit("reset") > ")")[px::bind(&Callee::reset, px::ref(callee))]
94  | (qi::lit("reset-assertions") > ")")[px::bind(&Callee::resetAssertions, px::ref(callee))]
95  | (qi::lit("set-info") > attribute > ")")[px::bind(&Callee::setInfo, px::ref(callee), qi::_1)]
96  | (qi::lit("set-logic") > logic > ")")[px::bind(&Callee::setLogic, px::ref(callee), qi::_1)]
97  | (qi::lit("set-option") > attribute > ")")[px::bind(&Callee::setOption, px::ref(callee), qi::_1)]
98  );
99  main = *command > qi::eoi;
100  qi::on_error<qi::fail>(main, errorHandler(qi::_1, qi::_2, qi::_3, qi::_4));
101  }
102 
104  Callee& callee;
107 
118 
120  qi::rule<Iterator, Skipper> functionDefinition;
121  qi::rule<Iterator, Skipper> command;
122  qi::rule<Iterator, Skipper> main;
123 
124  px::function<ErrorHandler> errorHandler;
125 
128  state.auxiliary_variables.clear();
129  }
130 };
131 
132 }
133 }
static bool find(V &ts, const T &t)
Definition: Alg.h:47
auto get(const It &it, level)
carl::mpl_variant_of< VariableTypes >::type VariableType
Variant type for all variables.
Definition: TheoryTypes.h:132
PositionIteratorType Iterator
Definition: Common.h:49
std::optional< FormulaT > qe(const FormulaT &input)
Definition: qe.cpp:14
Class to create the formulas for axioms.
mpq_class Rational
Definition: types.h:19
#define SMTRAT_LOG_ERROR(channel, msg)
Definition: logging.h:32
Parses decimals: numeral.0*numeral
Definition: Lexicon.h:29
qi::error_handler_result type
Definition: Script.h:31
qi::error_handler_result operator()(T1 b, T2 e, T3 where, T4 const &what) const
Definition: Script.h:33
Parses keywords: :simple_symbol
Definition: Lexicon.h:111
Parses numerals: (0 | [1-9][0-9]*)
Definition: Lexicon.h:24
std::set< types::VariableType > auxiliary_variables
Definition: ParserState.h:63
InstructionHandler & handler
Definition: Script.h:103
qi::rule< Iterator, types::VariableType(), Skipper > functionDefinitionArg
Definition: Script.h:119
DecimalParser decimal
Definition: Script.h:112
qi::rule< Iterator, Skipper > functionDefinition
Definition: Script.h:120
NumeralParser numeral
Definition: Script.h:111
SortedVariableParser sortedvariable
Definition: Script.h:114
KeywordParser keyword
Definition: Script.h:110
qi::rule< Iterator, Skipper > main
Definition: Script.h:122
qi::rule< Iterator, Skipper > command
Definition: Script.h:121
ScriptParser(InstructionHandler &h, Theories &theories, Callee &callee)
Definition: Script.h:48
AttributeParser attribute
Definition: Script.h:109
px::function< ErrorHandler > errorHandler
Definition: Script.h:124
Parses strings: ".+" with escape sequences \\" and \\\\
Definition: Lexicon.h:68
The Theories class combines all implemented theories and provides a single interface to interact with...
Definition: Theories.h:28
void defineFunction(const std::string &name, const std::vector< types::VariableType > &arguments, const carl::Sort &sort, const types::TermType &definition)
Definition: Theories.h:157
void pushScriptScope(std::size_t n)
Definition: Theories.h:206
void popScriptScope(std::size_t n)
Definition: Theories.h:209
types::VariableType declareFunctionArgument(const std::pair< std::string, carl::Sort > &arg)
Definition: Theories.h:141