This commit is contained in:
yhirose 2018-08-01 14:58:24 -04:00
parent 5445b5c2bb
commit fa5ffd8211

View File

@ -35,13 +35,14 @@ This is a simple calculator sample. It shows how to define grammar, associate sa
// (1) Include the header file // (1) Include the header file
#include <peglib.h> #include <peglib.h>
#include <assert.h> #include <assert.h>
#include <iostream>
using namespace peg; using namespace peg;
using namespace std; using namespace std;
int main(void) { int main(void) {
// (2) Make a parser // (2) Make a parser
auto syntax = R"( auto grammar = R"(
# Grammar for Calculator... # Grammar for Calculator...
Additive <- Multitive '+' Additive / Multitive Additive <- Multitive '+' Additive / Multitive
Multitive <- Primary '*' Multitive / Primary Multitive <- Primary '*' Multitive / Primary
@ -50,7 +51,14 @@ int main(void) {
%whitespace <- [ \t]* %whitespace <- [ \t]*
)"; )";
parser parser(syntax); parser parser;
parser.log = [](size_t line, size_t col, const string& msg) {
cerr << line << ":" << col << ": " << msg << "\n";
};
auto ok = parser.load_grammar(grammar);
assert(ok);
// (3) Setup actions // (3) Setup actions
parser["Additive"] = [](const SemanticValues& sv) { parser["Additive"] = [](const SemanticValues& sv) {