| 1 |
In a simple interactive command parser where each input is one line, it |
| 2 |
may be sufficient to allow tt(parse()) to return tt(PARSE_ABORT) on error and |
| 3 |
have the caller ignore the rest of the input line when that happens (and then |
| 4 |
call tt(parse()) again). But this is inadequate for a compiler, because it |
| 5 |
forgets all the syntactic context leading up to the error. A syntactic error |
| 6 |
deep within a function in the compiler input should not cause the compiler to |
| 7 |
treat the following line like the beginning of a source file. |
| 8 |
|
| 9 |
It is possible to specify how to recover from a syntactic error by |
| 10 |
writing rules recognizing the special token tt(error). This is a terminal |
| 11 |
symbol that is always defined (it must em(not) be declared) and is reserved |
| 12 |
for error handling. The b() parser generates an tt(error) token whenever a |
| 13 |
syntactic error is detected; if a rule was provided recognizing this token |
| 14 |
in the current context, the parse can continue. For example: |
| 15 |
verb( |
| 16 |
statements: |
| 17 |
// empty |
| 18 |
| |
| 19 |
statements '\n' |
| 20 |
| |
| 21 |
statements expression '\n' |
| 22 |
| |
| 23 |
statements error '\n' |
| 24 |
) |
| 25 |
The fourth rule in this example says that an error followed by a newline |
| 26 |
makes a valid addition to any tt(statements). |
| 27 |
|
| 28 |
What happens if a syntactic error occurs in the middle of an |
| 29 |
tt(expression)? The error recovery rule, interpreted strictly, applies to the |
| 30 |
precise sequence of a tt(statements), an error and a newline. If an error |
| 31 |
occurs in the middle of an tt(expression), there will probably be some |
| 32 |
additional tokens and subexpressions on the parser's stack after the last |
| 33 |
tt(statements), and there will be tokens waiting to be read before the next |
| 34 |
newline. So the rule is not applicable in the ordinary way. |
| 35 |
|
| 36 |
b(), however, can force the situation to fit the rule, by em(discarding) |
| 37 |
part of the semantic context and part of the input. When a (syntactic) error |
| 38 |
occurs the parsing algorithm tries to recover from the error in the |
| 39 |
following way: First it discards states from the stack until it encounters a |
| 40 |
state in which the tt(error) token is acceptable (meaning that the |
| 41 |
subexpressions already parsed are discarded, back to the last complete |
| 42 |
tt(statements)). At this point the error token is shifted. Then, if the |
| 43 |
available look-ahead token is not acceptable to be shifted next, the parser |
| 44 |
continues to read tokens and to discard them until it finds a token which |
| 45 |
em(is) acceptable. I.e., a token which em(can) follow an tt(error) token in |
| 46 |
the current state. In this example, b() reads and discards input until the |
| 47 |
next newline was read so that the fourth rule can apply. |
| 48 |
|
| 49 |
The choice of error rules in the grammar is a choice of strategies for |
| 50 |
error recovery. A simple and useful strategy is simply to skip the rest of the |
| 51 |
current input line or current statement if an error is detected: |
| 52 |
verb( |
| 53 |
statement: |
| 54 |
error ';' // on error, skip until ';' is read |
| 55 |
) |
| 56 |
Another useful recovery strategy is to recover to the matching |
| 57 |
close-delimiter of an opening-delimiter that has already been |
| 58 |
parsed. Otherwise the close-delimiter probably appears to be unmatched, |
| 59 |
generating another, spurious error message: |
| 60 |
verb( |
| 61 |
primary: |
| 62 |
'(' expression ')' |
| 63 |
| |
| 64 |
'(' error ')' |
| 65 |
| |
| 66 |
... |
| 67 |
; |
| 68 |
) |
| 69 |
Error recovery strategies are necessarily guesses. When they guess wrong, |
| 70 |
one syntactic error often leads to another. In the above example, the error |
| 71 |
recovery rule guesses that an error is caused by bad input within one |
| 72 |
statement. Suppose that instead a spurious semicolon is inserted in the middle |
| 73 |
of a valid statement. After the error recovery rule recovers from the first |
| 74 |
error, another syntactic error will immediately be found, since the text |
| 75 |
following the spurious semicolon is also an invalid statement. |
| 76 |
|
| 77 |
To prevent an outpouring of error messages, the parser may be configured |
| 78 |
in such a way that no error message are generated for another syntactic error |
| 79 |
that happens shortly after the first. E.g., only after three consecutive input |
| 80 |
tokens have been successfully shifted error messages may again be |
| 81 |
generated. This configuration is currently not available in b()'s parsers. |
| 82 |
|
| 83 |
Note that rules using the tt(error) token may have actions, just as any |
| 84 |
other rules can. |
| 85 |
|
| 86 |
The token causing an error is re-analyzed immediately when an error |
| 87 |
occurs. If this is unacceptable, then the member function tt(clearin()) may be |
| 88 |
called to skip this token. The function can be called by any member function |
| 89 |
of the Parser class. For example, suppose that on a parse error, an error |
| 90 |
handling routine is called that advances the input stream to some point where |
| 91 |
parsing should once again commence. The next symbol returned by the lexical |
| 92 |
scanner is probably correct. The previous token ought to be discarded using |
| 93 |
tt(clearin()). |
| 94 |
|
| 95 |
|