Skip to content

Commit 3d4983c

Browse files
authored
Update readme with error recovery section. (#12)
1 parent 41ed810 commit 3d4983c

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

readme.md

+40
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ All it needs is a C++17 compiler!
2828
* [Source tracking](#source-tracking)
2929
* [Buffers](#buffers)
3030
* [Typed terms](#typed-terms)
31+
* [Error recovery](#error-recovery)
3132
* [Regular expressions](#regular-expressions)
3233
* [Diagnostics](#diagnostics)
3334

@@ -953,6 +954,45 @@ runtime switch statement on the char value like in the **`simple_expr_parser.cpp
953954
>Note: Typed terms cannot use their implicit versions like the basic terms (```char_term```, ```string_term```) in the rules. They have to be
954955
>referrenced by the typed_terms object.
955956
957+
### Error recovery
958+
959+
If a special ***error*** term in a rule is used, the parser tries to recover from syntax error.
960+
961+
Consider the example from **error_recovery.cpp** example (here, simplified):
962+
963+
```c++
964+
constexpr parser p(
965+
exprs,
966+
terms(number, o_plus, o_minus, o_mul, o_div, '(', ')', ';'),
967+
nterms(exprs, expr),
968+
rules(
969+
exprs() >= create<exprs_type>{},
970+
exprs(exprs, expr, ';') >= push_back<1, 2>{},
971+
exprs(exprs, error, ';') >= _e1,
972+
expr(expr, '+', expr) >= [](int x1, skip, int x2){ return x1 + x2; },
973+
expr(number) >= [](const auto& sv){ return get_int(sv); }
974+
)
975+
);
976+
```
977+
978+
This rule allows parser to recover from syntax error when the expression is ill formed, the ```_e1``` functor will simply pass expressions parsed to this point:
979+
980+
```c++
981+
exprs(exprs, error, ';') >= _e1,
982+
```
983+
984+
Recovery follows the rules:
985+
986+
- when syntax error occurs a special <error_recovery_token> is presented to the LR algorithm.
987+
- parser states are reverted (popped from a stack) until the state accepting the <error_recovery_token> is encountered.
988+
- if at any point the is no more states to pop, algorithm fails.
989+
- <error_recovery_token> is shifted, and shift action is performed.
990+
- terminals are consumed and ignored until the terminal which would not result in a syntax error is encountered.
991+
- if at any point end of input is encountered, the algorithm fails.
992+
993+
To see how ***error*** in rules affect the parse table generation take a look at the diagnostic output and look for the <error_recovery_token> occurrences.
994+
See the [Diagnostics](#diagnostics) section for details.
995+
956996
## Regular expressions
957997
958998
When defining a regex pattern for a regex term:

0 commit comments

Comments
 (0)