cpp-peglib/example/calc4.cc

43 lines
1.1 KiB
C++
Raw Normal View History

2020-02-07 16:55:21 +00:00
#include <assert.h>
#include <iostream>
2020-10-02 01:26:04 +00:00
#include <peglib.h>
2020-02-07 16:55:21 +00:00
using namespace peg;
using namespace std;
int main(void) {
2020-10-02 01:26:04 +00:00
parser parser(R"(
2020-02-07 16:55:21 +00:00
EXPRESSION <- ATOM (OPERATOR ATOM)* {
precedence
L - +
L / *
}
ATOM <- NUMBER / '(' EXPRESSION ')'
OPERATOR <- < [-+/*] >
NUMBER <- < '-'? [0-9]+ >
%whitespace <- [ \t\r\n]*
)");
2020-10-02 01:26:04 +00:00
parser["EXPRESSION"] = [](const SemanticValues &vs) {
auto result = any_cast<long>(vs[0]);
if (vs.size() > 1) {
auto ope = any_cast<char>(vs[1]);
auto num = any_cast<long>(vs[2]);
switch (ope) {
case '+': result += num; break;
case '-': result -= num; break;
case '*': result *= num; break;
case '/': result /= num; break;
}
}
return result;
};
parser["OPERATOR"] = [](const SemanticValues &vs) { return *vs.sv().data(); };
parser["NUMBER"] = [](const SemanticValues &vs) { return atol(vs.sv().data()); };
2020-02-07 16:55:21 +00:00
2020-10-02 01:26:04 +00:00
long val;
parser.parse(" -1 + (1 + 2) * 3 - -1", val);
2020-02-07 16:55:21 +00:00
2020-10-02 01:26:04 +00:00
assert(val == 9);
2020-02-07 16:55:21 +00:00
}