Updated README

This commit is contained in:
yhirose 2020-02-07 21:52:54 -05:00
parent 16a746bb87
commit 2c7d94761f
2 changed files with 22 additions and 23 deletions

View File

@ -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. 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). 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 > _ T(x) ← < x > _
``` ```
Parsing expressions by Precedence climbing altorithm Parsing infix expression by Precedence climbing
---------------------------------------------------- -----------------------------------------------
```cpp ```cpp
parser parser(R"( parser parser(R"(
EXPRESSION <- PRECEDENCE_PARSING(ATOM, OPERATOR) EXPRESSION <- INFIX_EXPRESSION(ATOM, OPERATOR)
ATOM <- NUMBER / '(' EXPRESSION ')' ATOM <- NUMBER / '(' EXPRESSION ')'
OPERATOR <- < [-+/*] > OPERATOR <- < [-+/*] >
NUMBER <- < '-'? [0-9]+ > NUMBER <- < '-'? [0-9]+ >
%whitespace <- [ \t]* %whitespace <- [ \t]*
# Parsing expressions by Precedence climbing altorithm INFIX_EXPRESSION(A, O) <- A (O A)* {
PRECEDENCE_PARSING(A, O) <- A (O A)* { precedence
precedence L + -
L + - L * /
L * / }
}
)"); )");
parser["PRECEDENCE_PARSING"] = [](const SemanticValues& sv) -> long { parser["INFIX_EXPRESSION"] = [](const SemanticValues& sv) -> long {
auto result = any_cast<long>(sv[0]); auto result = any_cast<long>(sv[0]);
if (sv.size() > 1) { if (sv.size() > 1) {
auto ope = any_cast<char>(sv[1]); auto ope = any_cast<char>(sv[1]);

View File

@ -158,23 +158,23 @@ TEST_CASE("Precedence climbing with macro", "[precedence]")
{ {
// Create a PEG parser // Create a PEG parser
parser parser(R"( parser parser(R"(
EXPRESSION <- PRECEDENCE_PARSING(ATOM, OPERATOR) EXPRESSION <- INFIX_EXPRESSION(ATOM, OPERATOR)
PRECEDENCE_PARSING(A, O) <- A (O A)* { INFIX_EXPRESSION(A, O) <- A (O A)* {
precedence precedence
L + - L + -
L * / L * /
} }
ATOM <- NUMBER / '(' EXPRESSION ')' ATOM <- NUMBER / '(' EXPRESSION ')'
OPERATOR <- < [-+/*] > OPERATOR <- < [-+/*] >
NUMBER <- < '-'? [0-9]+ > NUMBER <- < '-'? [0-9]+ >
%whitespace <- [ \t]* %whitespace <- [ \t]*
)"); )");
bool ret = parser; bool ret = parser;
REQUIRE(ret == true); REQUIRE(ret == true);
// Setup actions // Setup actions
parser["PRECEDENCE_PARSING"] = [](const SemanticValues& sv) -> long { parser["INFIX_EXPRESSION"] = [](const SemanticValues& sv) -> long {
auto result = any_cast<long>(sv[0]); auto result = any_cast<long>(sv[0]);
if (sv.size() > 1) { if (sv.size() > 1) {
auto ope = any_cast<char>(sv[1]); auto ope = any_cast<char>(sv[1]);
@ -256,7 +256,7 @@ TEST_CASE("Precedence climbing error3", "[precedence]") {
EXPRESSION <- PRECEDENCE_PARSING(ATOM, OPERATOR) EXPRESSION <- PRECEDENCE_PARSING(ATOM, OPERATOR)
PRECEDENCE_PARSING(A, O) <- A (O A)+ { PRECEDENCE_PARSING(A, O) <- A (O A)+ {
precedence precedence
L + - L + -
L * / L * /
} }
ATOM <- NUMBER / '(' EXPRESSION ')' ATOM <- NUMBER / '(' EXPRESSION ')'