SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
Lexicon.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <boost/version.hpp>
4 #include "Common.h"
5 #include "theories/TheoryTypes.h"
6 
7 namespace smtrat {
8 namespace parser {
9 
10 /**
11  * Specialization of qi::real_policies for a Rational.
12  * Specifies that neither NaN nor Inf is allowed.
13  */
14 struct RationalPolicies : qi::real_policies<Rational> {
15  template <typename It, typename Attr>
16  static bool parse_nan(It&, It const&, Attr&) { return false; }
17  template <typename It, typename Attr>
18  static bool parse_inf(It&, It const&, Attr&) { return false; }
19 };
20 
21 /**
22  * Parses numerals: `(0 | [1-9][0-9]*)`
23  */
24 struct NumeralParser: qi::uint_parser<Integer,10,1,-1> {};
25 
26 /**
27  * Parses decimals: `numeral.0*numeral`
28  */
29 struct DecimalParser: qi::real_parser<Rational, RationalPolicies> {};
30 
31 /**
32  * Parses hexadecimals: `#x[0-9a-fA-F]+`
33  */
34 struct HexadecimalParser: public qi::grammar<Iterator, FixedWidthConstant<Integer>(), Skipper> {
35  typedef boost::iterator_range<Iterator> ITRange;
36  HexadecimalParser(): HexadecimalParser::base_type(main2, "hexadecimal") {
37  main = "#x" > (qi::raw[number[qi::_a = qi::_1]])[qi::_val = px::bind(&HexadecimalParser::build, px::ref(*this), qi::_1, qi::_a)];
38  main2 = main;
39  }
41  return FixedWidthConstant<Integer>(val, 4*std::string(itr.begin(), itr.end()).size());
42  }
43  qi::uint_parser<Integer,16,1,-1> number;
44  qi::rule<Iterator, FixedWidthConstant<Integer>(), Skipper, qi::locals<Integer>> main;
45  qi::rule<Iterator, FixedWidthConstant<Integer>(), Skipper> main2;
46 };
47 
48 /**
49  * Parses binaries: `#b[01]+`
50  */
51 struct BinaryParser: public qi::grammar<Iterator, FixedWidthConstant<Integer>(), Skipper> {
52  typedef boost::iterator_range<Iterator> ITRange;
53  BinaryParser(): BinaryParser::base_type(main2, "binary") {
54  main = "#b" > (qi::raw[number[qi::_a = qi::_1]])[qi::_val = px::bind(&BinaryParser::build, px::ref(*this), qi::_1, qi::_a)];
55  main2 = main;
56  }
58  return FixedWidthConstant<Integer>(val, std::string(itr.begin(), itr.end()).size());
59  }
60  qi::uint_parser<Integer,2,1,-1> number;
61  qi::rule<Iterator, FixedWidthConstant<Integer>(), Skipper, qi::locals<Integer>> main;
62  qi::rule<Iterator, FixedWidthConstant<Integer>(), Skipper> main2;
63 };
64 
65 /**
66  * Parses strings: `".+"` with escape sequences `\\"` and `\\\\`
67  */
68 struct StringParser: public qi::grammar<Iterator, std::string(), Skipper> {
69  StringParser(): StringParser::base_type(main, "string") {
70  main = qi::lexeme[qi::char_('"') > +(escapes | ~qi::char_('"')) > qi::char_('"')];
71  main.name("string");
72  escapes.add("\\\\", '\\');
73  escapes.add("\\\"", '"');
74  escapes.name("escape sequences");
75  }
76  qi::symbols<char, char> escapes;
77  qi::rule<Iterator, std::string(), Skipper> main;
78 };
79 
80 /*
81  * Reserved words: Not used yet.
82  */
83 
84 /**
85  * Parses symbols: `simple_symbol | quoted_symbol` where
86  * - `simple_symbol` is any string of `[0-9a-zA-Z~!@$%^&*_-+=<>.?/]` that does not start with a digit and is not a reserved word.
87  * - `quoted_symbol` is any string of printable characters (including space, tab, line-breaks) except `\` and `|` enclosed in `|` characters.
88  */
89 struct SimpleSymbolParser: public qi::grammar<Iterator, std::string(), Skipper> {
90  SimpleSymbolParser(): SimpleSymbolParser::base_type(main, "simple symbol") {
91  // Attention: "-" must be the first or last character!
92  main = qi::lexeme[ (qi::alpha | qi::char_("~!@$%^&*_+=<>.?/-")) > *(qi::alnum | qi::char_("~!@$%^&*_+=<>.?/-"))];
93  }
94  qi::rule<Iterator, std::string(), Skipper> main;
95 };
96 struct SymbolParser: public qi::grammar<Iterator, std::string(), Skipper> {
97  SymbolParser(): SymbolParser::base_type(main, "symbol") {
98  main = quoted | simple;
99  main.name("symbol");
100  quoted = qi::lit('|') > qi::no_skip[*(~qi::char_("|")) > qi::lit('|')];
101  quoted.name("quoted symbol");
102  }
103  qi::rule<Iterator, std::string(), Skipper> main;
104  qi::rule<Iterator, std::string(), Skipper> quoted;
106 };
107 
108 /**
109  * Parses keywords: `:simple_symbol`
110  */
111 struct KeywordParser: public qi::grammar<Iterator, std::string(), Skipper> {
112  KeywordParser(): KeywordParser::base_type(main, "keyword") {
113  main = qi::lit(":") >> simple;
114  main.name("keyword");
115  }
116  qi::rule<Iterator, std::string(), Skipper> main;
118 };
119 
120 }
121 }
122 
123 namespace boost { namespace spirit { namespace traits {
124 #if BOOST_VERSION >= 105900
125  template<> inline bool scale(int exp, smtrat::Rational& r, smtrat::Rational rin) {
126  if (exp >= 0)
127  r = rin * carl::pow(smtrat::Rational(10), (unsigned)exp);
128  else
129  r = rin / carl::pow(smtrat::Rational(10), (unsigned)(-exp));
130  return true;
131  }
132 #else
133  template<> inline void scale(int exp, smtrat::Rational& r) {
134  if (exp >= 0)
135  r *= carl::pow(smtrat::Rational(10), (unsigned)exp);
136  else
137  r /= carl::pow(smtrat::Rational(10), (unsigned)(-exp));
138  }
139 #endif
140 #if BOOST_VERSION < 107000
141  template<> inline bool is_equal_to_one(const smtrat::Rational& value) {
142  return value == 1;
143  }
144 #endif
145  /**
146  * Specialization of standard implementation to fix compilation errors.
147  * Standard implementation looks like this:
148  * <code>return neg ? -n : n;</code>
149  * However, if using gmpxx <code>-n</code> and <code>n</code> have different types.
150  * This issue is fixed in this implementation.
151  */
152  template<> inline smtrat::Rational negate(bool neg, const smtrat::Rational& n) {
153  return neg ? smtrat::Rational(-n) : n;
154  }
155 }}}
Lit neg(Lit p)
Definition: SolverTypes.h:65
PositionIteratorType Iterator
Definition: Common.h:49
Class to create the formulas for axioms.
mpq_class Rational
Definition: types.h:19
carl::IntegralType< Rational >::type Integer
Definition: types.h:21
Parses binaries: #b[01]+
Definition: Lexicon.h:51
boost::iterator_range< Iterator > ITRange
Definition: Lexicon.h:52
qi::uint_parser< Integer, 2, 1,-1 > number
Definition: Lexicon.h:60
FixedWidthConstant< Integer > build(const ITRange &itr, const Integer &val)
Definition: Lexicon.h:57
qi::rule< Iterator, FixedWidthConstant< Integer >), Skipper > main2
Definition: Lexicon.h:62
qi::rule< Iterator, FixedWidthConstant< Integer >), Skipper, qi::locals< Integer > > main
Definition: Lexicon.h:61
Parses decimals: numeral.0*numeral
Definition: Lexicon.h:29
Represents a constant of a fixed width.
Definition: TheoryTypes.h:38
Parses hexadecimals: #x[0-9a-fA-F]+
Definition: Lexicon.h:34
boost::iterator_range< Iterator > ITRange
Definition: Lexicon.h:35
qi::rule< Iterator, FixedWidthConstant< Integer >), Skipper > main2
Definition: Lexicon.h:45
qi::uint_parser< Integer, 16, 1,-1 > number
Definition: Lexicon.h:43
qi::rule< Iterator, FixedWidthConstant< Integer >), Skipper, qi::locals< Integer > > main
Definition: Lexicon.h:44
FixedWidthConstant< Integer > build(const ITRange &itr, const Integer &val)
Definition: Lexicon.h:40
Parses keywords: :simple_symbol
Definition: Lexicon.h:111
qi::rule< Iterator, std::string(), Skipper > main
Definition: Lexicon.h:116
SimpleSymbolParser simple
Definition: Lexicon.h:117
Parses numerals: (0 | [1-9][0-9]*)
Definition: Lexicon.h:24
Specialization of qi::real_policies for a Rational.
Definition: Lexicon.h:14
static bool parse_nan(It &, It const &, Attr &)
Definition: Lexicon.h:16
static bool parse_inf(It &, It const &, Attr &)
Definition: Lexicon.h:18
Parses symbols: simple_symbol | quoted_symbol where.
Definition: Lexicon.h:89
qi::rule< Iterator, std::string(), Skipper > main
Definition: Lexicon.h:94
Parses strings: ".+" with escape sequences \\" and \\\\
Definition: Lexicon.h:68
qi::symbols< char, char > escapes
Definition: Lexicon.h:76
qi::rule< Iterator, std::string(), Skipper > main
Definition: Lexicon.h:77
SimpleSymbolParser simple
Definition: Lexicon.h:105
qi::rule< Iterator, std::string(), Skipper > quoted
Definition: Lexicon.h:104
qi::rule< Iterator, std::string(), Skipper > main
Definition: Lexicon.h:103