- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample-4.cpp
More file actions
Latest commit
141 lines (119 loc) · 2.79 KB
/
Copy pathsample-4.cpp
File metadata and controls
141 lines (119 loc) · 2.79 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<iostream>
#include<string>
#include<numeric>
#include<boost/lexical_cast.hpp>
#include<boost/function.hpp>
#include"peg.hpp"
namespacefusion= boost::fusion;
usingnamespacepeg;
usingnamespacestd;
template<classT>
structt : publicT
{
voidaction(const T& t, const rep<peg::ws>&) {
*static_cast<T*>(this) = t;
}
};
template<char c, classT>
structop_t : publicT
{
voidaction(t<ch<c> >) {}
};
template<classT, classU>
structop_or : publicboost::function<int(int,int)>
{
typedef boost::function<int(int,int)> Base;
op_or& operator=(const Base& x) {
*static_cast<Base *>(this) = x;
return *this;
}
voidaction_0(const T& t) {
*this = t;
}
voidaction_1(const U& u) {
*this = u;
}
};
typedefop_t<'+', std::plus<int> > op_plus;
typedefop_t<'-', std::minus<int> > op_minus;
typedefop_t<'*', std::multiplies<int> > op_multiplies;
typedefop_t<'/', std::divides<int> > op_divides;
structint_lit_s
{
int v;
voidaction(const replus<digit>& s) {
v = boost::lexical_cast<int>(s);
}
};
typedef t<int_lit_s> int_lit;
typedef t<ch<'('> > open_paren;
typedef t<ch<')'> > close_paren;
structtree;
typedef boost::shared_ptr<tree> ptree;
structtree
{
typedef ptree result_type;
virtual~tree() {}
virtualinteval() const = 0;
};
structbinary_tree : publictree
{
boost::function<int(int,int)> op;
ptree left;
ptree right;
inteval() const { returnop(left->eval(), right->eval()); }
};
template<classT, classOp>
structleft_assoc_expr : publicbinary_tree
{
typedef left_assoc_expr<T, Op> self_type;
structf {
ptree operator()(const ptree e, const fusion::vector<Op, ptr<T> >& x) {
self_type *p = new self_type;
p->op = fusion::at_c<0>(x);
p->left = e;
p->right = fusion::at_c<1>(x);
returnptree(p);
}
};
static ptree action(const ptr<T>& e, const rep<fusion::vector<Op, ptr<T> > >& elist) {
returnstd::accumulate(elist.begin(), elist.end(), e, f());
}
};
structprimary_expr;
typedef left_assoc_expr<primary_expr, op_or<op_multiplies, op_divides> > multi_expr;
typedef left_assoc_expr<multi_expr, op_or<op_plus, op_minus> > expr;
structprimary_expr : publictree
{
int v;
inteval() const { return v; }
static ptree action_0(int_lit lit) {
primary_expr *p = new primary_expr;
p->v = lit.v;
returnptree(p);
}
static ptree action_1(open_paren, const ptr<expr>& e, close_paren) {
return e;
}
};
voidtest(const string& s)
{
cout << s << " ---> ";
parser<ptr<expr> > parser;
ptr<expr> e;
if (!parser.parse(e, s.begin(), s.end())) {
cout << "parse error" << endl;
} else {
cout << e->eval() << endl;
}
}
intmain()
{
test("1");
test("42");
test("10 + 13");
test("10 + 20 * 3");
test("(2 + 4) * (5 - 2)");
test("2 * 4 / 8");
return0;
}