mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-11-13 22:55:29 +00:00
52 lines
1.2 KiB
C++
52 lines
1.2 KiB
C++
#include <assert.h>
|
|
#include <iostream>
|
|
#include <peglib.h>
|
|
|
|
using namespace peg;
|
|
using namespace std;
|
|
|
|
int main(void) {
|
|
// (2) Make a parser
|
|
parser parser(R"(
|
|
# Grammar for Calculator...
|
|
Additive <- Multitive '+' Additive / Multitive
|
|
Multitive <- Primary '*' Multitive / Primary
|
|
Primary <- '(' Additive ')' / Number
|
|
Number <- < [0-9]+ >
|
|
%whitespace <- [ \t]*
|
|
)");
|
|
|
|
assert(static_cast<bool>(parser) == true);
|
|
|
|
// (3) Setup actions
|
|
parser["Additive"] = [](const SemanticValues &vs) {
|
|
switch (vs.choice()) {
|
|
case 0: // "Multitive '+' Additive"
|
|
return any_cast<int>(vs[0]) + any_cast<int>(vs[1]);
|
|
default: // "Multitive"
|
|
return any_cast<int>(vs[0]);
|
|
}
|
|
};
|
|
|
|
parser["Multitive"] = [](const SemanticValues &vs) {
|
|
switch (vs.choice()) {
|
|
case 0: // "Primary '*' Multitive"
|
|
return any_cast<int>(vs[0]) * any_cast<int>(vs[1]);
|
|
default: // "Primary"
|
|
return any_cast<int>(vs[0]);
|
|
}
|
|
};
|
|
|
|
parser["Number"] = [](const SemanticValues &vs) {
|
|
return vs.token_to_number<int>();
|
|
};
|
|
|
|
// (4) Parse
|
|
parser.enable_packrat_parsing(); // Enable packrat parsing.
|
|
|
|
int val;
|
|
parser.parse(" (1 + 2) * 3 ", val);
|
|
|
|
assert(val == 9);
|
|
}
|