From 2c7d94761f77867da9ca26c0c41aa2296ef37b3a Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 7 Feb 2020 21:52:54 -0500 Subject: [PATCH] Updated README --- README.md | 21 ++++++++++----------- test/test2.cc | 24 ++++++++++++------------ 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index fce00a6..eac7bf6 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ The PEG syntax is well described on page 2 in the [document](http://www.brynosau This library supports the linear-time parsing known as the [*Packrat*](http://pdos.csail.mit.edu/~baford/packrat/thesis/thesis.pdf) parsing. -*Parsing expressions by [Precedence climbing](https://eli.thegreenplace.net/2012/08/02/parsing-expressions-by-precedence-climbing)* algorithm is also supported. +*Parsing infix expression by [Precedence climbing](https://eli.thegreenplace.net/2012/08/02/parsing-expressions-by-precedence-climbing)* algorithm is also supported. IMPORTANT NOTE for some Linux distributions such as Ubuntu and CentOS: Need `-pthread` option when linking. See [#23](https://github.com/yhirose/cpp-peglib/issues/23#issuecomment-261126127), [#46](https://github.com/yhirose/cpp-peglib/issues/46#issuecomment-417870473) and [#62](https://github.com/yhirose/cpp-peglib/issues/62#issuecomment-492032680). @@ -343,26 +343,25 @@ List(I, D) ← I (D I)* T(x) ← < x > _ ``` -Parsing expressions by Precedence climbing altorithm ----------------------------------------------------- +Parsing infix expression by Precedence climbing +----------------------------------------------- ```cpp parser parser(R"( - EXPRESSION <- PRECEDENCE_PARSING(ATOM, OPERATOR) + EXPRESSION <- INFIX_EXPRESSION(ATOM, OPERATOR) ATOM <- NUMBER / '(' EXPRESSION ')' OPERATOR <- < [-+/*] > NUMBER <- < '-'? [0-9]+ > %whitespace <- [ \t]* - # Parsing expressions by Precedence climbing altorithm - PRECEDENCE_PARSING(A, O) <- A (O A)* { - precedence - L + - - L * / - } + INFIX_EXPRESSION(A, O) <- A (O A)* { + precedence + L + - + L * / + } )"); -parser["PRECEDENCE_PARSING"] = [](const SemanticValues& sv) -> long { +parser["INFIX_EXPRESSION"] = [](const SemanticValues& sv) -> long { auto result = any_cast(sv[0]); if (sv.size() > 1) { auto ope = any_cast(sv[1]); diff --git a/test/test2.cc b/test/test2.cc index bb342ff..e7b93c9 100644 --- a/test/test2.cc +++ b/test/test2.cc @@ -158,23 +158,23 @@ TEST_CASE("Precedence climbing with macro", "[precedence]") { // Create a PEG parser parser parser(R"( - EXPRESSION <- PRECEDENCE_PARSING(ATOM, OPERATOR) - PRECEDENCE_PARSING(A, O) <- A (O A)* { - precedence - L + - - L * / - } - ATOM <- NUMBER / '(' EXPRESSION ')' - OPERATOR <- < [-+/*] > - NUMBER <- < '-'? [0-9]+ > - %whitespace <- [ \t]* + EXPRESSION <- INFIX_EXPRESSION(ATOM, OPERATOR) + INFIX_EXPRESSION(A, O) <- A (O A)* { + precedence + L + - + L * / + } + ATOM <- NUMBER / '(' EXPRESSION ')' + OPERATOR <- < [-+/*] > + NUMBER <- < '-'? [0-9]+ > + %whitespace <- [ \t]* )"); bool ret = parser; REQUIRE(ret == true); // Setup actions - parser["PRECEDENCE_PARSING"] = [](const SemanticValues& sv) -> long { + parser["INFIX_EXPRESSION"] = [](const SemanticValues& sv) -> long { auto result = any_cast(sv[0]); if (sv.size() > 1) { auto ope = any_cast(sv[1]); @@ -256,7 +256,7 @@ TEST_CASE("Precedence climbing error3", "[precedence]") { EXPRESSION <- PRECEDENCE_PARSING(ATOM, OPERATOR) PRECEDENCE_PARSING(A, O) <- A (O A)+ { precedence - L + - + L + - L * / } ATOM <- NUMBER / '(' EXPRESSION ')'