carl  24.04
Computer ARithmetic Library
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 #include <sstream>
9 
11 #include "Common.h"
13 #include "FormulaParser.h"
14 #include "PolynomialParser.h"
15 #include "RationalFunctionParser.h"
16 
17 namespace carl::io {
18 namespace parser {
19 
20 template<typename Pol>
21 class Parser {
22 private:
27 
28  template<typename Result, typename Parser>
29  bool parse(const std::string& s, const Parser& parser, Result& res) {
30  auto begin = s.begin();
31  return qi::phrase_parse(begin, s.end(), parser, skipper, res);
32  }
33 public:
34  Parser():
35  skipper(),
37  ratfunParser(),
39  {
40  }
41 
42  Pol polynomial(const std::string& s) {
43  Pol res;
44  if (!parse(s, polynomialParser, res)) {
45  CARL_LOG_ERROR("carl.parser", "Parsing \"" << s << "\" to a polynomial failed.");
46  }
47  return res;
48  }
49 
50  RatFun<Pol> rationalFunction(const std::string& s) {
51  RatFun<Pol> res;
52  if (!parse(s, ratfunParser, res)) {
53  CARL_LOG_ERROR("carl.parser", "Parsing \"" << s << "\" to a rational function failed.");
54  }
55  return res;
56  }
57 
58  Formula<Pol> formula(const std::string& s) {
59  Formula<Pol> res;
60  if (!parse(s, formulaParser, res)) {
61  std::cout << "NOPE!" << std::endl;
62  CARL_LOG_ERROR("carl.parser", "Parsing \"" << s << "\" to a formula failed.");
63  }
64  return res;
65  }
66 
68  if( v.type() == VariableType::VT_BOOL )
69  formulaParser.addVariable(v);
70  else {
71  polynomialParser.addVariable(v);
72  ratfunParser.addVariable(v);
73  }
74  }
75 };
76 
77 }
78 }
A small wrapper that configures logging for carl.
#define CARL_LOG_ERROR(channel, msg)
Definition: carl-logging.h:40
MultivariatePolynomial< Rational > Pol
Definition: HornerTest.cpp:17
boost::spirit::qi::space_type Skipper
Definition: Common.h:34
A Variable represents an algebraic variable that can be used throughout carl.
Definition: Variable.h:85
constexpr VariableType type() const noexcept
Retrieves the type of the variable.
Definition: Variable.h:131
Represent an SMT formula, which can be an atom for some background theory or a boolean combination of...
Definition: Formula.h:47
bool parse(const std::string &s, const Parser &parser, Result &res)
Definition: Parser.h:29
Pol polynomial(const std::string &s)
Definition: Parser.h:42
Formula< Pol > formula(const std::string &s)
Definition: Parser.h:58
FormulaParser< Pol > formulaParser
Definition: Parser.h:26
void addVariable(Variable::Arg v)
Definition: Parser.h:67
RationalFunctionParser< Pol > ratfunParser
Definition: Parser.h:25
RatFun< Pol > rationalFunction(const std::string &s)
Definition: Parser.h:50
PolynomialParser< Pol > polynomialParser
Definition: Parser.h:24