Debian Codegraph @

documentation/manual/error/recovery.yo

raw
1 B() implements a simple error recovery mechanism. When the tt(lookup())
2 function cannot find an action for the current token in the current state it
3 throws an tt(UNEXPECTED_TOKEN_) exception.
4
5 This exception is caught by the parsing function, calling the
6 tt(errorRecovery()) member function. By default, this member function
7 terminates the parsing process. The non-default recovery procedure is
8 available once an tt(error) token is used in a production rule. When the
9 parsing process throws bf(UNEXPECTED_TOKEN_) the recovery procedure is
10 started (i.e., it is started whenever a syntactical error is encountered or
11 tt(ERROR()) is called).
12
13 The recovery procedure consists of
14 itemization(
15 it() looking for the first state on the state-stack having an
16 error-production, followed by:
17 it() handling all state transitions that are possible without retrieving a
18 terminal token.
19 it() then, in the state requiring a terminal token and starting
20 with the initial unexpected token (3) all subsequent terminal tokens are
21 ignored until a token is retrieved which is a continuation token in that
22 state.
23 )
24
25 If the error recovery procedure fails (i.e., if no acceptable token is
26 ever encountered) error recovery falls back to the default recovery
27 mode (i.e., the parsing process is terminated).
28
29 Not all syntactic errors are always reported: the option
30 link(--required-tokens)(REQUIRED) can be used to specify the minimum number of
31 tokens that must have been successfully processed before another syntactic
32 error is reported (and counted).
33
34 The option link(--error-verbose)(ERRORVERBOSE) may be specified to obtain the
35 contents of the state stack when a syntactic error is reported.
36
37 The example grammar may be provided with an tt(error) production rule:
38 verb(
39 %token NR
40
41 %left '+'
42
43 %%
44
45 start:
46 start expr
47 |
48 // empty
49 ;
50
51 expr:
52 error
53 |
54 NR
55 |
56 expr '+' expr
57 ;
58 )
59 The resulting grammar has one additional state (handling the error
60 production) and one state in which the tt(ERR_ITEM) flag has been set. When
61 and error is encountered, this state obtains tokens until a token having a
62 valid continuation was received, after which normal processing continues.
63
64 In the parser's verbose output (using the tt(-V) option) the various grammar
65 rules and state transition tables are shown. The debug output below refers to
66 this information.
67
68 The rules are:
69 verb(
70 1: start -> start expr
71 2: start -> <empty>
72 3: expr (errTok_) -> errTok_
73 4: expr (NR) -> NR
74 5: expr ('+') -> expr '+' expr
75 6: start_$ -> start
76 )
77
78 The state-transitions are:
79 verb(
80 State 0:
81 6: start_$ -> . start
82 0: On start to state 1
83 Reduce by 2: start -> .
84
85
86 State 1:
87 6: start_$ -> start .
88 1: start -> start . expr
89 0: On expr to state 2
90 1: On errTok_ to state 3
91 2: On NR to state 4
92
93
94 State 2:
95 1: start -> start expr .
96 5: expr -> expr . '+' expr
97 0: On '+' to state 5
98 Reduce by 1: start -> start expr .
99
100
101 State 3:
102 3: expr -> errTok_ .
103 Reduce by 3: expr -> errTok_ .
104
105
106 State 4:
107 4: expr -> NR .
108 Reduce by 4: expr -> NR .
109
110
111 State 5:
112 5: expr -> expr '+' . expr
113 0: On expr to state 6
114 1: On errTok_ to state 3
115 2: On NR to state 4
116
117
118 State 6:
119 5: expr -> expr '+' expr .
120 5: expr -> expr . '+' expr
121 0 (removed by precedence): On '+' to state 5
122 Reduce by 5: expr -> expr '+' expr .
123 )
124
125
126 The following output from the tt(parse()) function, generated by b() using the
127 tt(--debug) option illustrates error recovery for the above grammar, entering
128 the input
129 verb(
130 a
131 3+a
132 )
133 results in:
134 verb(
135 parse(): Parsing starts
136
137 PUSH 0 (initializing the state stack)
138
139
140 LOOKUP: [0, 'Reserved_::UNDETERMINED_'] -> default reduce using rule 2
141
142 REDUCE: rule 2
143 execute action 2 ...
144 ... completed
145 rule 2: pop 0 elements. Next will be: [0, 'start']
146 pop 0 elements from the stack having size 1
147 next: [0, 'start']
148
149
150 PUSH: [0, 'start'] -> 1
151
152 scanner token `a' (97)
153
154 ERROR: [1, `a' (97)] -> ??. Errors: 1
155 Syntax error
156 Reached ERROR state 1
157
158 PUSH: [1, 'errTok_'] -> 3
159
160
161 LOOKUP: [3, `a' (97)] -> default reduce using rule 3
162
163 REDUCE: rule 3
164 rule 3: pop 1 elements. Next will be: [1, 'expr']
165 pop 1 elements from the stack having size 3
166 next: [1, 'expr']
167
168 available token 'expr'
169
170 PUSH: [1, 'expr'] -> 2
171
172 available token `a' (97)
173
174 LOOKUP: [2, `a' (97)] -> default reduce using rule 1
175
176 REDUCE: rule 1
177 rule 1: pop 2 elements. Next will be: [0, 'start']
178 pop 2 elements from the stack having size 3
179 next: [0, 'start']
180
181
182 PUSH: [0, 'start'] -> 1
183
184 available token `a' (97)
185
186 scanner token 'NR'
187
188 PUSH: [1, 'NR'] -> 4
189 ERROR RECOVERED: next state 4
190
191
192 LOOKUP: [4, 'Reserved_::UNDETERMINED_'] -> default reduce using rule 4
193
194 REDUCE: rule 4
195 execute action 4 ...
196 ... completed
197 rule 4: pop 1 elements. Next will be: [1, 'expr']
198 pop 1 elements from the stack having size 3
199 next: [1, 'expr']
200
201 available token 'expr'
202
203 PUSH: [1, 'expr'] -> 2
204
205 scanner token 'Reserved_::EOF_'
206
207 LOOKUP: [2, 'Reserved_::EOF_'] -> default reduce using rule 1
208
209 REDUCE: rule 1
210 execute action 1 ...
211 ... completed
212 rule 1: pop 2 elements. Next will be: [0, 'start']
213 pop 2 elements from the stack having size 3
214 next: [0, 'start']
215
216
217 PUSH: [0, 'start'] -> 1
218
219 available token 'Reserved_::EOF_'
220 ACCEPT(): Parsing successful
221 parse(): returns 0 or 1
222 )
223
224 The final debug message should be interpreted as a bf(C++) expression: the
225 0 indicates that tt(Parse::ACCEPT) rather than tt(Parser::ABORT) was called,
226 the 1 shows the value of tt(d_nErrors_). Consequently, tt(parse()) returns 1
227 (i.e., tt(`0 or 1')).
228
229
230
231
232