Debian Codegraph @

documentation/manual/demos/errors.yo

raw
1 Up to this point, error-recovery hasn't yet been covered i.e., how to continue
2 parsing after the parser detects a syntax error. By default tt(parse) returns
3 after calling tt(error). This means that an erroneous input line ends the
4 calculator. Now we show how to recover from erroneous input.
5
6 The b() grammar-specification language includes the reserved symbol tt(error),
7 which can be used in production rules. In the example below it has been added
8 as a new alternative for recognizing the tt(line) nonterminal:
9 verbinsert(//LINE errorcalc/parser/grammar)
10 This addition to the grammar allows the calculator to recover from syntax
11 errors. If a syntax error is encountered, the third production rule is
12 activated, skipping all tokens until a newline token has been encounters. At
13 that point tt(line) has been recognized, and parsing continues afresh at the
14 next input line (the tt(error) member function is also called, printing its
15 message). Different from bison's tt(error) implementation, b() proceeds on the
16 assumption that whenever tt(error) is used in a production rule it is the
17 grammar constructor's intention to have the parser continue parsing
18 (therefore, a statement like `tt(yyerrok)', encountered in bison grammars is
19 superfluous in b() grammars). The reserved symbol tt(error) itself causes the
20 parsing function to skip all subsequent input until a token that can follow
21 tt(error) has been encountered. In the above implementation that token is the
22 newline character `tt(\n)' (see chapter ref(RECOVERY)).
23
24 This form of error recovery deals with syntax errors. There are other
25 kinds of errors; for example, divisions by zero, which raises an exception
26 signal that is normally fatal. A real calculator program must handle this
27 signal and use whatever it takes to discard the rest of the current line of
28 input and resume parsing thereafter. Handling such signals and other forms of
29 semantic errors is not discussed here, as it is not specific to b()
30 programs. But once a semantic error has been encountered, handling functions
31 may call tt(ERROR()), resulting in the same procedure as the one that's used
32 for syntax errors: all subsequenct tokens are skipped until a token has been
33 encountered that can follow the reserved symbol tt(error).
34