-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
85 lines (75 loc) · 1.89 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <string>
#include <iostream>
namespace x3 = boost::spirit::x3;
using x3::double_;
using x3::_attr;
using x3::space;
using x3::phrase_parse;
using x3::_val;
x3::rule<class expression, double> const expression("expression");
x3::rule<class term, double> const term("term");
x3::rule<class factor, double> const factor("factor");
auto assign_ = [](auto& ctx)
{
_val(ctx) = _attr(ctx);
};
auto add_ = [](auto& ctx)
{
_val(ctx) += _attr(ctx);
};
auto sub_ = [](auto& ctx)
{
_val(ctx) -= _attr(ctx);
};
auto mul_ = [](auto& ctx)
{
_val(ctx) *= _attr(ctx);
};
auto div_ = [](auto& ctx)
{
_val(ctx) /= _attr(ctx);
};
auto const expression_def =
term[assign_]
>> *( ('+' >> term [add_])
| ('-' >> term [sub_])
)
;
auto const term_def =
factor[assign_]
>> *( ('*' >> factor [mul_])
| ('/' >> factor [div_])
)
;
auto const factor_def =
double_
| '(' >> expression >> ')'
;
BOOST_SPIRIT_DEFINE(
expression
, term
, factor
);
int main()
{
std::string line;
while (true) {
std::getline(std::cin, line);
if (line == "q") {
break;
}
auto it = line.begin();
auto const end = line.end();
double result = 0.0;
bool ret = phrase_parse(it, end, expression, space, result);
if (ret && it == end) {
std::cout << result << std::endl;
} else {
std::cout <<std::string("failed to parse at: \"").append(std::string(it, end)).append("\"") << std::endl;;
}
}
return 0;
}