]>
Commit | Line | Data |
---|---|---|
82c035a8 | 1 | # Executing Actions. -*- Autotest -*- |
6e30ede8 | 2 | |
c932d613 | 3 | # Copyright (C) 2001-2012 Free Software Foundation, Inc. |
82c035a8 | 4 | |
f16b0819 | 5 | # This program is free software: you can redistribute it and/or modify |
82c035a8 | 6 | # it under the terms of the GNU General Public License as published by |
f16b0819 PE |
7 | # the Free Software Foundation, either version 3 of the License, or |
8 | # (at your option) any later version. | |
9 | # | |
82c035a8 AD |
10 | # This program is distributed in the hope that it will be useful, |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
f16b0819 | 14 | # |
82c035a8 | 15 | # You should have received a copy of the GNU General Public License |
f16b0819 | 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
82c035a8 AD |
17 | |
18 | AT_BANNER([[User Actions.]]) | |
19 | ||
20 | ## ------------------ ## | |
21 | ## Mid-rule actions. ## | |
22 | ## ------------------ ## | |
23 | ||
24 | AT_SETUP([Mid-rule actions]) | |
25 | ||
26 | # Bison once forgot the mid-rule actions. It was because the action | |
27 | # was attached to the host rule (the one with the mid-rule action), | |
28 | # instead of being attached to the empty rule dedicated to this | |
29 | # action. | |
30 | ||
55f48c48 | 31 | AT_BISON_OPTION_PUSHDEFS |
9501dc6e | 32 | AT_DATA_GRAMMAR([[input.y]], |
8f3596a6 AD |
33 | [[%error-verbose |
34 | %debug | |
35 | %{ | |
55f48c48 AD |
36 | ]AT_YYERROR_DECLARE[ |
37 | ]AT_YYLEX_DECLARE[ | |
82c035a8 AD |
38 | %} |
39 | %% | |
931394cb AD |
40 | exp: { putchar ('0'); } |
41 | '1' { putchar ('1'); } | |
42 | '2' { putchar ('2'); } | |
43 | '3' { putchar ('3'); } | |
44 | '4' { putchar ('4'); } | |
45 | '5' { putchar ('5'); } | |
46 | '6' { putchar ('6'); } | |
47 | '7' { putchar ('7'); } | |
48 | '8' { putchar ('8'); } | |
49 | '9' { putchar ('9'); } | |
50 | { putchar ('\n'); } | |
82c035a8 AD |
51 | ; |
52 | %% | |
55f48c48 AD |
53 | ]AT_YYERROR_DEFINE[ |
54 | ]AT_YYLEX_DEFINE(123456789)[ | |
82c035a8 AD |
55 | int |
56 | main (void) | |
57 | { | |
58 | return yyparse (); | |
59 | } | |
60 | ]]) | |
55f48c48 | 61 | AT_BISON_OPTION_POPDEFS |
82c035a8 | 62 | |
da730230 | 63 | AT_BISON_CHECK([-d -v -o input.c input.y]) |
1154cced AD |
64 | AT_COMPILE([input]) |
65 | AT_PARSER_CHECK([./input], 0, | |
931394cb | 66 | [[0123456789 |
82c035a8 AD |
67 | ]]) |
68 | ||
69 | AT_CLEANUP | |
75d1fe16 AD |
70 | |
71 | ||
72 | ||
5dac0025 PE |
73 | |
74 | ||
75d1fe16 AD |
75 | ## ---------------- ## |
76 | ## Exotic Dollars. ## | |
77 | ## ---------------- ## | |
78 | ||
79 | AT_SETUP([Exotic Dollars]) | |
80 | ||
55f48c48 | 81 | AT_BISON_OPTION_PUSHDEFS |
9501dc6e | 82 | AT_DATA_GRAMMAR([[input.y]], |
8f3596a6 AD |
83 | [[%error-verbose |
84 | %debug | |
85 | %{ | |
55f48c48 AD |
86 | ]AT_YYERROR_DECLARE[ |
87 | ]AT_YYLEX_DECLARE[ | |
affac613 | 88 | # define USE(Var) |
75d1fe16 AD |
89 | %} |
90 | ||
91 | %union | |
92 | { | |
93 | int val; | |
94 | }; | |
95 | ||
0ff67d71 | 96 | %type <val> a_1 a_2 a_5 |
378f4bd8 | 97 | sum_of_the_five_previous_values |
75d1fe16 AD |
98 | |
99 | %% | |
0ff67d71 PE |
100 | exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5 |
101 | sum_of_the_five_previous_values | |
75d1fe16 | 102 | { |
84866159 | 103 | USE (($1, $2, $<foo>3, $<foo>4, $5)); |
75d1fe16 AD |
104 | printf ("%d\n", $6); |
105 | } | |
106 | ; | |
107 | a_1: { $$ = 1; }; | |
108 | a_2: { $$ = 2; }; | |
75d1fe16 AD |
109 | a_5: { $$ = 5; }; |
110 | ||
111 | sum_of_the_five_previous_values: | |
112 | { | |
113 | $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4; | |
114 | } | |
115 | ; | |
116 | ||
117 | %% | |
55f48c48 AD |
118 | ]AT_YYERROR_DEFINE[ |
119 | ]AT_YYLEX_DEFINE([])[ | |
75d1fe16 AD |
120 | int |
121 | main (void) | |
122 | { | |
123 | return yyparse (); | |
124 | } | |
125 | ]]) | |
126 | ||
da730230 | 127 | AT_BISON_CHECK([-d -v -o input.c input.y], 0) |
1154cced AD |
128 | AT_COMPILE([input]) |
129 | AT_PARSER_CHECK([./input], 0, | |
75d1fe16 AD |
130 | [[15 |
131 | ]]) | |
132 | ||
eb8c66bb JD |
133 | # Make sure that fields after $n or $-n are parsed correctly. At one |
134 | # point while implementing dashes in symbol names, we were dropping | |
135 | # fields after $-n. | |
136 | AT_DATA_GRAMMAR([[input.y]], | |
137 | [[ | |
138 | %{ | |
55f48c48 AD |
139 | #include <stdio.h> |
140 | ]AT_YYERROR_DECLARE[ | |
141 | ]AT_YYLEX_DECLARE[ | |
eb8c66bb JD |
142 | typedef struct { int val; } stype; |
143 | # define YYSTYPE stype | |
144 | %} | |
145 | ||
146 | %% | |
147 | start: one two { $$.val = $1.val + $2.val; } sum ; | |
148 | one: { $$.val = 1; } ; | |
149 | two: { $$.val = 2; } ; | |
150 | sum: { printf ("%d\n", $0.val + $-1.val + $-2.val); } ; | |
151 | ||
152 | %% | |
55f48c48 | 153 | ]AT_YYERROR_DEFINE[ |
290a8ff2 | 154 | ]AT_YYLEX_DEFINE()[ |
eb8c66bb JD |
155 | int |
156 | main (void) | |
157 | { | |
158 | return yyparse (); | |
159 | } | |
160 | ]]) | |
161 | ||
162 | AT_BISON_CHECK([[-o input.c input.y]]) | |
163 | AT_COMPILE([[input]]) | |
164 | AT_PARSER_CHECK([[./input]], [[0]], | |
165 | [[6 | |
166 | ]]) | |
167 | ||
55f48c48 | 168 | AT_BISON_OPTION_POPDEFS |
75d1fe16 | 169 | AT_CLEANUP |
9280d3ef AD |
170 | |
171 | ||
172 | ||
e776192e AD |
173 | ## -------------------------- ## |
174 | ## Printers and Destructors. ## | |
175 | ## -------------------------- ## | |
9280d3ef | 176 | |
ac700aa6 PE |
177 | # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG) |
178 | # ----------------------------------------------------------------------------- | |
3df37415 | 179 | m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR], |
9c66f418 AD |
180 | [# Make sure complex $n work. |
181 | m4_if([$1$2$3], $[1]$[2]$[3], [], | |
3df37415 AD |
182 | [m4_fatal([$0: Invalid arguments: $@])])dnl |
183 | ||
9c66f418 AD |
184 | # Be sure to pass all the %directives to this macro to have correct |
185 | # helping macros. So don't put any directly in the Bison file. | |
c2729758 | 186 | AT_BISON_OPTION_PUSHDEFS([$5]) |
9501dc6e | 187 | AT_DATA_GRAMMAR([[input.y]], |
16dc6a9e | 188 | [[%code requires { |
9280d3ef AD |
189 | #include <stdio.h> |
190 | #include <stdlib.h> | |
cf806753 | 191 | #include <string.h> |
9c66f418 | 192 | #include <assert.h> |
80ce3401 PE |
193 | |
194 | #define YYINITDEPTH 10 | |
195 | #define YYMAXDEPTH 10 | |
c2729758 ADL |
196 | ]AT_LALR1_CC_IF( |
197 | [#define RANGE(Location) (Location).begin.line, (Location).end.line], | |
198 | [#define RANGE(Location) (Location).first_line, (Location).last_line]) | |
9bc0dd67 | 199 | [} |
9c66f418 AD |
200 | |
201 | $5] | |
202 | m4_ifval([$6], [%union | |
9280d3ef AD |
203 | { |
204 | int ival; | |
ac700aa6 | 205 | }]) |
16dc6a9e JD |
206 | AT_LALR1_CC_IF([%define global_tokens_and_yystype]) |
207 | m4_ifval([$6], [[%code provides {]], [[%code {]]) | |
230a3db4 AD |
208 | AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])[ |
209 | ]AT_YYLEX_DECLARE[ | |
55f48c48 | 210 | ]AT_LALR1_CC_IF([], [AT_YYERROR_DECLARE]) |
9bc0dd67 | 211 | [} |
c2729758 | 212 | |
868d2d96 | 213 | ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[ |
e3170060 | 214 | |
868d2d96 | 215 | /* FIXME: This %printer isn't actually tested. */ |
a5eb1ed2 AD |
216 | %printer |
217 | { | |
9a1e9989 | 218 | ]AT_LALR1_CC_IF([debug_stream () << $$;], |
3fc16193 | 219 | [fprintf (yyoutput, "%d", $$)])[; |
a5eb1ed2 | 220 | } |
9c66f418 | 221 | input line thing 'x' 'y' |
e3170060 AD |
222 | |
223 | %destructor | |
4c6cc1db | 224 | { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e | 225 | input |
5719c109 | 226 | |
7bd6c77e | 227 | %destructor |
4c6cc1db | 228 | { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e AD |
229 | line |
230 | ||
231 | %destructor | |
4c6cc1db | 232 | { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e AD |
233 | thing |
234 | ||
235 | %destructor | |
4c6cc1db | 236 | { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e | 237 | 'x' |
5719c109 | 238 | |
9c66f418 AD |
239 | %destructor |
240 | { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); } | |
241 | 'y' | |
242 | ||
868d2d96 JD |
243 | %token END 0 |
244 | %destructor | |
245 | { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); } | |
246 | END | |
247 | ||
9280d3ef | 248 | %% |
9c66f418 AD |
249 | /* |
250 | This grammar is made to exercise error recovery. | |
251 | "Lines" starting with `(' support error recovery, with | |
252 | ')' as synchronizing token. Lines starting with 'x' can never | |
253 | be recovered from if in error. | |
254 | */ | |
255 | ||
9280d3ef AD |
256 | input: |
257 | /* Nothing. */ | |
5719c109 AD |
258 | { |
259 | $$ = 0; | |
16f37b35 | 260 | printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$)); |
5719c109 AD |
261 | } |
262 | | line input /* Right recursive to load the stack so that popping at | |
868d2d96 | 263 | END can be exercised. */ |
5719c109 AD |
264 | { |
265 | $$ = 2; | |
16f37b35 | 266 | printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n", |
4c6cc1db | 267 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2)); |
5719c109 | 268 | } |
9280d3ef AD |
269 | ; |
270 | ||
271 | line: | |
272 | thing thing thing ';' | |
5719c109 AD |
273 | { |
274 | $$ = $1; | |
16f37b35 | 275 | printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n", |
4c6cc1db | 276 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), |
16f37b35 | 277 | $3, RANGE (@3), $4, RANGE (@4)); |
5719c109 | 278 | } |
9c66f418 | 279 | | '(' thing thing ')' |
5719c109 AD |
280 | { |
281 | $$ = $1; | |
9c66f418 AD |
282 | printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n", |
283 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), | |
284 | $3, RANGE (@3), $4, RANGE (@4)); | |
5719c109 | 285 | } |
9c66f418 | 286 | | '(' thing ')' |
5719c109 AD |
287 | { |
288 | $$ = $1; | |
9c66f418 AD |
289 | printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n", |
290 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3)); | |
5719c109 | 291 | } |
9c66f418 | 292 | | '(' error ')' |
5719c109 AD |
293 | { |
294 | $$ = -1; | |
9c66f418 AD |
295 | printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n", |
296 | $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3)); | |
5719c109 | 297 | } |
9280d3ef AD |
298 | ; |
299 | ||
300 | thing: | |
5719c109 AD |
301 | 'x' |
302 | { | |
303 | $$ = $1; | |
4c6cc1db AD |
304 | printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n", |
305 | $$, RANGE (@$), $1, RANGE (@1)); | |
5719c109 | 306 | } |
9280d3ef AD |
307 | ; |
308 | %% | |
9c66f418 | 309 | /* Alias to ARGV[1]. */ |
ef51bfa7 | 310 | const char *source = YY_NULL; |
9c66f418 | 311 | |
230a3db4 AD |
312 | static |
313 | ]AT_YYLEX_PROTOTYPE[ | |
9280d3ef | 314 | { |
5a08f1ce | 315 | static unsigned int counter = 0; |
9280d3ef | 316 | |
9c66f418 AD |
317 | int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++; |
318 | /* As in BASIC, line numbers go from 10 to 10. */ | |
c2729758 | 319 | ]AT_LALR1_CC_IF( |
9c66f418 AD |
320 | [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c; |
321 | AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9; | |
c2729758 | 322 | ], |
9c66f418 AD |
323 | [ AT_LOC.first_line = AT_LOC.first_column = 10 * c; |
324 | AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9; | |
c2729758 | 325 | ])[ |
9c66f418 | 326 | |
77519a7d | 327 | assert (0 <= c && c <= strlen (source)); |
a9739e7c PE |
328 | if (source[c]) |
329 | printf ("sending: '%c'", source[c]); | |
9280d3ef | 330 | else |
868d2d96 | 331 | printf ("sending: END"); |
9c66f418 | 332 | printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[)); |
a9739e7c | 333 | return source[c]; |
9280d3ef AD |
334 | } |
335 | ||
c2729758 | 336 | ]AT_LALR1_CC_IF( |
68e11668 | 337 | [/* A C++ error reporting function. */ |
c2729758 | 338 | void |
99880de5 | 339 | yy::parser::error (const location& l, const std::string& m) |
c2729758 | 340 | { |
efeed023 | 341 | printf ("%d-%d: %s\n", RANGE (l), m.c_str()); |
c2729758 ADL |
342 | } |
343 | ||
344 | static bool yydebug; | |
345 | int | |
346 | yyparse () | |
347 | { | |
99880de5 | 348 | yy::parser parser; |
a3cb6248 | 349 | parser.set_debug_level (yydebug); |
c2729758 ADL |
350 | return parser.parse (); |
351 | } | |
352 | ], | |
353 | [static void | |
9280d3ef AD |
354 | yyerror (const char *msg) |
355 | { | |
4c6cc1db | 356 | printf ("%d-%d: %s\n", RANGE (yylloc), msg); |
c2729758 | 357 | }])[ |
9280d3ef | 358 | |
9280d3ef | 359 | int |
9c66f418 | 360 | main (int argc, const char *argv[]) |
9280d3ef | 361 | { |
6100a9aa | 362 | int status; |
9280d3ef | 363 | yydebug = !!getenv ("YYDEBUG"); |
9c66f418 | 364 | assert (argc == 2); |
a9739e7c | 365 | source = argv[1]; |
6100a9aa PE |
366 | status = yyparse (); |
367 | switch (status) | |
9280d3ef | 368 | { |
6100a9aa PE |
369 | case 0: printf ("Successful parse.\n"); break; |
370 | case 1: printf ("Parsing FAILED.\n"); break; | |
371 | default: printf ("Parsing FAILED (status %d).\n", status); break; | |
9280d3ef | 372 | } |
6100a9aa | 373 | return status; |
9280d3ef AD |
374 | } |
375 | ]]) | |
376 | ||
11c4e57d | 377 | AT_FULL_COMPILE([input]) |
9c66f418 AD |
378 | |
379 | ||
380 | # Check the location of "empty" | |
381 | # ----------------------------- | |
382 | # I.e., epsilon-reductions, as in "(x)" which ends by reducing | |
383 | # an empty "line" nterm. | |
384 | # FIXME: This location is not satisfying. Depend on the lookahead? | |
385 | AT_PARSER_CHECK([./input '(x)'], 0, | |
386 | [[sending: '(' (0@0-9) | |
387 | sending: 'x' (1@10-19) | |
388 | thing (1@10-19): 'x' (1@10-19) | |
389 | sending: ')' (2@20-29) | |
390 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
868d2d96 | 391 | sending: END (3@30-39) |
b4a20338 AD |
392 | input (0@29-29): /* Nothing */ |
393 | input (2@0-29): line (0@0-29) input (0@29-29) | |
868d2d96 | 394 | Freeing token END (3@30-39) |
258b75ca | 395 | Freeing nterm input (2@0-29) |
9c66f418 AD |
396 | Successful parse. |
397 | ]]) | |
398 | ||
399 | ||
400 | # Check locations in error recovery | |
401 | # --------------------------------- | |
402 | # '(y)' is an error, but can be recovered from. But what's the location | |
403 | # of the error itself ('y'), and of the resulting reduction ('(error)'). | |
404 | AT_PARSER_CHECK([./input '(y)'], 0, | |
405 | [[sending: '(' (0@0-9) | |
406 | sending: 'y' (1@10-19) | |
407 | 10-19: syntax error, unexpected 'y', expecting 'x' | |
408 | Freeing token 'y' (1@10-19) | |
409 | sending: ')' (2@20-29) | |
410 | line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29) | |
868d2d96 | 411 | sending: END (3@30-39) |
b4a20338 AD |
412 | input (0@29-29): /* Nothing */ |
413 | input (2@0-29): line (-1@0-29) input (0@29-29) | |
868d2d96 | 414 | Freeing token END (3@30-39) |
258b75ca | 415 | Freeing nterm input (2@0-29) |
9c66f418 AD |
416 | Successful parse. |
417 | ]]) | |
418 | ||
419 | ||
420 | # Syntax errors caught by the parser | |
421 | # ---------------------------------- | |
422 | # Exercise the discarding of stack top and input until `error' | |
423 | # can be reduced. | |
424 | # | |
425 | # '(', 'x', 'x', 'x', 'x', 'x', ')', | |
426 | # | |
427 | # Load the stack and provoke an error that cannot be caught by the | |
428 | # grammar, to check that the stack is cleared. And make sure the | |
429 | # lookahead is freed. | |
430 | # | |
431 | # '(', 'x', ')', | |
432 | # '(', 'x', ')', | |
433 | # 'y' | |
434 | AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1, | |
435 | [[sending: '(' (0@0-9) | |
4c6cc1db AD |
436 | sending: 'x' (1@10-19) |
437 | thing (1@10-19): 'x' (1@10-19) | |
438 | sending: 'x' (2@20-29) | |
439 | thing (2@20-29): 'x' (2@20-29) | |
440 | sending: 'x' (3@30-39) | |
9c66f418 | 441 | 30-39: syntax error, unexpected 'x', expecting ')' |
4c6cc1db AD |
442 | Freeing nterm thing (2@20-29) |
443 | Freeing nterm thing (1@10-19) | |
4c6cc1db AD |
444 | Freeing token 'x' (3@30-39) |
445 | sending: 'x' (4@40-49) | |
446 | Freeing token 'x' (4@40-49) | |
447 | sending: 'x' (5@50-59) | |
448 | Freeing token 'x' (5@50-59) | |
9c66f418 AD |
449 | sending: ')' (6@60-69) |
450 | line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69) | |
451 | sending: '(' (7@70-79) | |
4c6cc1db AD |
452 | sending: 'x' (8@80-89) |
453 | thing (8@80-89): 'x' (8@80-89) | |
9c66f418 AD |
454 | sending: ')' (9@90-99) |
455 | line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99) | |
456 | sending: '(' (10@100-109) | |
457 | sending: 'x' (11@110-119) | |
458 | thing (11@110-119): 'x' (11@110-119) | |
459 | sending: ')' (12@120-129) | |
460 | line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129) | |
461 | sending: 'y' (13@130-139) | |
b4a20338 AD |
462 | input (0@129-129): /* Nothing */ |
463 | input (2@100-129): line (10@100-129) input (0@129-129) | |
9c66f418 AD |
464 | input (2@70-129): line (7@70-99) input (2@100-129) |
465 | input (2@0-129): line (-1@0-69) input (2@70-129) | |
868d2d96 | 466 | 130-139: syntax error, unexpected 'y', expecting END |
9c66f418 AD |
467 | Freeing nterm input (2@0-129) |
468 | Freeing token 'y' (13@130-139) | |
5719c109 | 469 | Parsing FAILED. |
9280d3ef AD |
470 | ]]) |
471 | ||
868d2d96 JD |
472 | |
473 | # Syntax error caught by the parser where lookahead = END | |
474 | # -------------------------------------------------------- | |
475 | # Load the stack and provoke an error that cannot be caught by the | |
476 | # grammar, to check that the stack is cleared. And make sure the | |
477 | # lookahead is freed. | |
478 | # | |
479 | # '(', 'x', ')', | |
480 | # '(', 'x', ')', | |
481 | # 'x' | |
482 | AT_PARSER_CHECK([./input '(x)(x)x'], 1, | |
483 | [[sending: '(' (0@0-9) | |
484 | sending: 'x' (1@10-19) | |
485 | thing (1@10-19): 'x' (1@10-19) | |
486 | sending: ')' (2@20-29) | |
487 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
488 | sending: '(' (3@30-39) | |
489 | sending: 'x' (4@40-49) | |
490 | thing (4@40-49): 'x' (4@40-49) | |
491 | sending: ')' (5@50-59) | |
492 | line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59) | |
493 | sending: 'x' (6@60-69) | |
494 | thing (6@60-69): 'x' (6@60-69) | |
495 | sending: END (7@70-79) | |
496 | 70-79: syntax error, unexpected END, expecting 'x' | |
497 | Freeing nterm thing (6@60-69) | |
498 | Freeing nterm line (3@30-59) | |
499 | Freeing nterm line (0@0-29) | |
500 | Freeing token END (7@70-79) | |
501 | Parsing FAILED. | |
502 | ]]) | |
503 | ||
504 | ||
80ce3401 PE |
505 | # Check destruction upon stack overflow |
506 | # ------------------------------------- | |
507 | # Upon stack overflow, all symbols on the stack should be destroyed. | |
508 | # Only check for yacc.c. | |
509 | AT_YACC_IF([ | |
6100a9aa | 510 | AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2, |
80ce3401 PE |
511 | [[sending: '(' (0@0-9) |
512 | sending: 'x' (1@10-19) | |
513 | thing (1@10-19): 'x' (1@10-19) | |
514 | sending: ')' (2@20-29) | |
515 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
516 | sending: '(' (3@30-39) | |
517 | sending: 'x' (4@40-49) | |
518 | thing (4@40-49): 'x' (4@40-49) | |
519 | sending: ')' (5@50-59) | |
520 | line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59) | |
521 | sending: '(' (6@60-69) | |
522 | sending: 'x' (7@70-79) | |
523 | thing (7@70-79): 'x' (7@70-79) | |
524 | sending: ')' (8@80-89) | |
525 | line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89) | |
526 | sending: '(' (9@90-99) | |
527 | sending: 'x' (10@100-109) | |
528 | thing (10@100-109): 'x' (10@100-109) | |
529 | sending: ')' (11@110-119) | |
530 | line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119) | |
531 | sending: '(' (12@120-129) | |
532 | sending: 'x' (13@130-139) | |
533 | thing (13@130-139): 'x' (13@130-139) | |
534 | sending: ')' (14@140-149) | |
535 | line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149) | |
536 | sending: '(' (15@150-159) | |
537 | sending: 'x' (16@160-169) | |
538 | thing (16@160-169): 'x' (16@160-169) | |
539 | sending: ')' (17@170-179) | |
540 | line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179) | |
541 | sending: '(' (18@180-189) | |
542 | sending: 'x' (19@190-199) | |
543 | thing (19@190-199): 'x' (19@190-199) | |
544 | sending: ')' (20@200-209) | |
1a059451 | 545 | 200-209: memory exhausted |
80ce3401 PE |
546 | Freeing nterm thing (19@190-199) |
547 | Freeing nterm line (15@150-179) | |
548 | Freeing nterm line (12@120-149) | |
549 | Freeing nterm line (9@90-119) | |
550 | Freeing nterm line (6@60-89) | |
551 | Freeing nterm line (3@30-59) | |
552 | Freeing nterm line (0@0-29) | |
6100a9aa | 553 | Parsing FAILED (status 2). |
80ce3401 PE |
554 | ]]) |
555 | ]) | |
556 | ||
55f48c48 AD |
557 | AT_BISON_OPTION_POPDEFS |
558 | ])# _AT_CHECK_PRINTER_AND_DESTRUCTOR | |
3df37415 AD |
559 | |
560 | ||
a14a26fa | 561 | # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG]) |
046ac74e | 562 | # --------------------------------------------------------------------------- |
3df37415 | 563 | m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR], |
9c66f418 AD |
564 | [AT_SETUP([Printers and Destructors $2: $1]) |
565 | ||
a14a26fa | 566 | $3 |
9c66f418 AD |
567 | _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4], |
568 | [%error-verbose | |
569 | %debug | |
570 | %verbose | |
571 | %locations | |
572 | $1], [$2]) | |
573 | ||
574 | AT_CLEANUP | |
3df37415 AD |
575 | ]) |
576 | ||
577 | ||
ac700aa6 PE |
578 | AT_CHECK_PRINTER_AND_DESTRUCTOR([]) |
579 | AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union]) | |
046ac74e | 580 | |
fd19f271 AD |
581 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"]) |
582 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union]) | |
046ac74e | 583 | |
1576d44d AD |
584 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser]) |
585 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union]) | |
ec5479ce JD |
586 | |
587 | ||
588 | ||
12e35840 JD |
589 | ## ----------------------------------------- ## |
590 | ## Default tagless %printer and %destructor. ## | |
591 | ## ----------------------------------------- ## | |
ec5479ce JD |
592 | |
593 | # Check that the right %printer and %destructor are called, that they're not | |
594 | # called for $end, and that $$ and @$ work correctly. | |
595 | ||
12e35840 | 596 | AT_SETUP([Default tagless %printer and %destructor]) |
55f48c48 | 597 | AT_BISON_OPTION_PUSHDEFS([%locations]) |
ec5479ce JD |
598 | AT_DATA_GRAMMAR([[input.y]], |
599 | [[%error-verbose | |
600 | %debug | |
601 | %locations | |
602 | %initial-action { | |
603 | @$.first_line = @$.last_line = 1; | |
604 | @$.first_column = @$.last_column = 1; | |
605 | } | |
606 | ||
607 | %{ | |
608 | # include <stdio.h> | |
609 | # include <stdlib.h> | |
55f48c48 AD |
610 | ]AT_YYLEX_DECLARE[ |
611 | ]AT_YYERROR_DECLARE[ | |
ec5479ce JD |
612 | # define USE(SYM) |
613 | %} | |
614 | ||
615 | %printer { | |
12e35840 JD |
616 | fprintf (yyoutput, "<*> printer should not be called.\n"); |
617 | } <*> | |
618 | ||
619 | %printer { | |
3ebecc24 JD |
620 | fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column); |
621 | } <> | |
ec5479ce | 622 | %destructor { |
3ebecc24 JD |
623 | fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column); |
624 | } <> | |
ec5479ce JD |
625 | |
626 | %printer { | |
627 | fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column); | |
628 | } 'b' 'c' | |
629 | %destructor { | |
630 | fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column); | |
631 | } 'b' 'c' | |
632 | ||
12e35840 JD |
633 | %destructor { |
634 | fprintf (yyoutput, "<*> destructor should not be called.\n"); | |
635 | } <*> | |
636 | ||
ec5479ce JD |
637 | %% |
638 | ||
639 | start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ; | |
640 | ||
641 | %% | |
55f48c48 AD |
642 | ]AT_YYERROR_DEFINE[ |
643 | ]AT_YYLEX_DEFINE([abcd], [[yylval = res]])[ | |
ec5479ce JD |
644 | |
645 | int | |
646 | main (void) | |
647 | { | |
648 | yydebug = 1; | |
649 | return yyparse (); | |
650 | } | |
651 | ]]) | |
652 | ||
da730230 | 653 | AT_BISON_CHECK([-o input.c input.y]) |
ec5479ce JD |
654 | AT_COMPILE([input]) |
655 | AT_PARSER_CHECK([./input], 1, | |
3ebecc24 | 656 | [[<> destructor for 'd' @ 4. |
ec5479ce JD |
657 | 'b'/'c' destructor for 'c' @ 3. |
658 | 'b'/'c' destructor for 'b' @ 2. | |
3ebecc24 | 659 | <> destructor for 'a' @ 1. |
ec5479ce JD |
660 | ]], |
661 | [[Starting parse | |
662 | Entering state 0 | |
3ebecc24 JD |
663 | Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1) |
664 | Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1) | |
ec5479ce JD |
665 | Entering state 1 |
666 | Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) | |
667 | Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) | |
668 | Entering state 3 | |
669 | Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) | |
670 | Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) | |
671 | Entering state 5 | |
3ebecc24 JD |
672 | Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4) |
673 | Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4) | |
ec5479ce JD |
674 | Entering state 6 |
675 | Reading a token: Now at end of input. | |
74909941 | 676 | 1.5-4: syntax error, unexpected $end, expecting 'e' |
3ebecc24 | 677 | Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4) |
ec5479ce JD |
678 | Stack now 0 1 3 5 |
679 | Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) | |
680 | Stack now 0 1 3 | |
681 | Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) | |
682 | Stack now 0 1 | |
3ebecc24 | 683 | Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1) |
ec5479ce JD |
684 | Stack now 0 |
685 | Cleanup: discarding lookahead token $end (1.5-1.5: ) | |
686 | Stack now 0 | |
687 | ]]) | |
688 | ||
55f48c48 | 689 | AT_BISON_OPTION_POPDEFS |
ec5479ce JD |
690 | AT_CLEANUP |
691 | ||
692 | ||
693 | ||
12e35840 JD |
694 | ## ------------------------------------------------------ ## |
695 | ## Default tagged and per-type %printer and %destructor. ## | |
696 | ## ------------------------------------------------------ ## | |
b2a0b7ca | 697 | |
12e35840 | 698 | AT_SETUP([Default tagged and per-type %printer and %destructor]) |
55f48c48 | 699 | AT_BISON_OPTION_PUSHDEFS |
b2a0b7ca JD |
700 | AT_DATA_GRAMMAR([[input.y]], |
701 | [[%error-verbose | |
702 | %debug | |
703 | ||
704 | %{ | |
705 | # include <stdio.h> | |
706 | # include <stdlib.h> | |
55f48c48 AD |
707 | ]AT_YYERROR_DECLARE[ |
708 | ]AT_YYLEX_DECLARE[ | |
b2a0b7ca JD |
709 | # define USE(SYM) |
710 | %} | |
711 | ||
12e35840 | 712 | %printer { |
3ebecc24 JD |
713 | fprintf (yyoutput, "<> printer should not be called.\n"); |
714 | } <> | |
12e35840 | 715 | |
b2a0b7ca JD |
716 | %union { int field0; int field1; int field2; } |
717 | %type <field0> start 'a' 'g' | |
718 | %type <field1> 'e' | |
719 | %type <field2> 'f' | |
720 | %printer { | |
12e35840 JD |
721 | fprintf (yyoutput, "<*>/<field2>/e printer"); |
722 | } <*> 'e' <field2> | |
b2a0b7ca | 723 | %destructor { |
12e35840 JD |
724 | fprintf (stdout, "<*>/<field2>/e destructor.\n"); |
725 | } <*> 'e' <field2> | |
b2a0b7ca JD |
726 | |
727 | %type <field1> 'b' | |
728 | %printer { fprintf (yyoutput, "<field1> printer"); } <field1> | |
729 | %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1> | |
730 | ||
731 | %type <field0> 'c' | |
732 | %printer { fprintf (yyoutput, "'c' printer"); } 'c' | |
733 | %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c' | |
734 | ||
735 | %type <field1> 'd' | |
736 | %printer { fprintf (yyoutput, "'d' printer"); } 'd' | |
737 | %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd' | |
738 | ||
12e35840 | 739 | %destructor { |
3ebecc24 JD |
740 | fprintf (yyoutput, "<> destructor should not be called.\n"); |
741 | } <> | |
12e35840 | 742 | |
b2a0b7ca JD |
743 | %% |
744 | ||
745 | start: | |
746 | 'a' 'b' 'c' 'd' 'e' 'f' 'g' | |
747 | { | |
748 | USE(($1, $2, $3, $4, $5, $6, $7)); | |
749 | $$ = 'S'; | |
750 | } | |
751 | ; | |
752 | ||
753 | %% | |
55f48c48 AD |
754 | ]AT_YYERROR_DEFINE[ |
755 | ]AT_YYLEX_DEFINE([abcdef])[ | |
b2a0b7ca JD |
756 | |
757 | int | |
758 | main (void) | |
759 | { | |
760 | yydebug = 1; | |
761 | return yyparse (); | |
762 | } | |
763 | ]]) | |
764 | ||
da730230 | 765 | AT_BISON_CHECK([-o input.c input.y]) |
b2a0b7ca JD |
766 | AT_COMPILE([input]) |
767 | AT_PARSER_CHECK([./input], 1, | |
12e35840 JD |
768 | [[<*>/<field2>/e destructor. |
769 | <*>/<field2>/e destructor. | |
b2a0b7ca JD |
770 | 'd' destructor. |
771 | 'c' destructor. | |
772 | <field1> destructor. | |
12e35840 | 773 | <*>/<field2>/e destructor. |
b2a0b7ca JD |
774 | ]], |
775 | [[Starting parse | |
776 | Entering state 0 | |
12e35840 JD |
777 | Reading a token: Next token is token 'a' (<*>/<field2>/e printer) |
778 | Shifting token 'a' (<*>/<field2>/e printer) | |
b2a0b7ca JD |
779 | Entering state 1 |
780 | Reading a token: Next token is token 'b' (<field1> printer) | |
781 | Shifting token 'b' (<field1> printer) | |
782 | Entering state 3 | |
783 | Reading a token: Next token is token 'c' ('c' printer) | |
784 | Shifting token 'c' ('c' printer) | |
785 | Entering state 5 | |
786 | Reading a token: Next token is token 'd' ('d' printer) | |
787 | Shifting token 'd' ('d' printer) | |
788 | Entering state 6 | |
12e35840 JD |
789 | Reading a token: Next token is token 'e' (<*>/<field2>/e printer) |
790 | Shifting token 'e' (<*>/<field2>/e printer) | |
b2a0b7ca | 791 | Entering state 7 |
12e35840 JD |
792 | Reading a token: Next token is token 'f' (<*>/<field2>/e printer) |
793 | Shifting token 'f' (<*>/<field2>/e printer) | |
b2a0b7ca JD |
794 | Entering state 8 |
795 | Reading a token: Now at end of input. | |
796 | syntax error, unexpected $end, expecting 'g' | |
12e35840 | 797 | Error: popping token 'f' (<*>/<field2>/e printer) |
b2a0b7ca | 798 | Stack now 0 1 3 5 6 7 |
12e35840 | 799 | Error: popping token 'e' (<*>/<field2>/e printer) |
b2a0b7ca JD |
800 | Stack now 0 1 3 5 6 |
801 | Error: popping token 'd' ('d' printer) | |
802 | Stack now 0 1 3 5 | |
803 | Error: popping token 'c' ('c' printer) | |
804 | Stack now 0 1 3 | |
805 | Error: popping token 'b' (<field1> printer) | |
806 | Stack now 0 1 | |
12e35840 | 807 | Error: popping token 'a' (<*>/<field2>/e printer) |
b2a0b7ca JD |
808 | Stack now 0 |
809 | Cleanup: discarding lookahead token $end () | |
810 | Stack now 0 | |
811 | ]]) | |
812 | ||
55f48c48 | 813 | AT_BISON_OPTION_POPDEFS |
b2a0b7ca JD |
814 | AT_CLEANUP |
815 | ||
816 | ||
817 | ||
ec5479ce | 818 | ## ------------------------------------------------------------- ## |
12e35840 | 819 | ## Default %printer and %destructor for user-defined end token. ## |
ec5479ce JD |
820 | ## ------------------------------------------------------------- ## |
821 | ||
3508ce36 | 822 | AT_SETUP([Default %printer and %destructor for user-defined end token]) |
ec5479ce | 823 | |
12e35840 | 824 | # _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED) |
a703b669 | 825 | # ------------------------------------------------------------- |
12e35840 JD |
826 | m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN], |
827 | [m4_if($1, 0, | |
3ebecc24 JD |
828 | [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])], |
829 | [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])]) | |
12e35840 | 830 | |
55f48c48 | 831 | AT_BISON_OPTION_PUSHDEFS([%locations]) |
12e35840 | 832 | AT_DATA_GRAMMAR([[input]]$1[[.y]], |
ec5479ce JD |
833 | [[%error-verbose |
834 | %debug | |
835 | %locations | |
836 | %initial-action { | |
837 | @$.first_line = @$.last_line = 1; | |
838 | @$.first_column = @$.last_column = 1; | |
839 | } | |
840 | ||
841 | %{ | |
842 | # include <stdio.h> | |
843 | # include <stdlib.h> | |
55f48c48 AD |
844 | ]AT_YYERROR_DECLARE[ |
845 | ]AT_YYLEX_DECLARE[ | |
ec5479ce JD |
846 | # define USE(SYM) |
847 | %} | |
848 | ||
12e35840 JD |
849 | %destructor { |
850 | fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n"); | |
851 | } <]]not_kind[[> | |
852 | ||
ec5479ce JD |
853 | %token END 0 |
854 | %printer { | |
12e35840 JD |
855 | fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column); |
856 | } <]]kind[[> | |
ec5479ce | 857 | %destructor { |
12e35840 JD |
858 | fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column); |
859 | } <]]kind[[> | |
860 | ||
861 | %printer { | |
862 | fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n"); | |
863 | } <]]not_kind[[> | |
864 | ||
865 | ]]m4_if($1, 0, [[[ | |
866 | ]]], | |
867 | [[[%union { char tag; } | |
868 | %type <tag> start END]]])[[ | |
ec5479ce JD |
869 | |
870 | %% | |
871 | ||
872 | start: { $$ = 'S'; } ; | |
873 | ||
874 | %% | |
875 | ||
876 | static int | |
877 | yylex (void) | |
878 | { | |
cf806753 PE |
879 | static int called; |
880 | if (called++) | |
881 | abort (); | |
12e35840 | 882 | yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E'; |
ec5479ce JD |
883 | yylloc.first_line = yylloc.last_line = 1; |
884 | yylloc.first_column = yylloc.last_column = 1; | |
885 | return 0; | |
886 | } | |
55f48c48 | 887 | ]AT_YYERROR_DEFINE[ |
ec5479ce JD |
888 | |
889 | int | |
890 | main (void) | |
891 | { | |
892 | yydebug = 1; | |
893 | return yyparse (); | |
894 | } | |
895 | ]]) | |
55f48c48 | 896 | AT_BISON_OPTION_POPDEFS |
ec5479ce | 897 | |
da730230 | 898 | AT_BISON_CHECK([-o input$1.c input$1.y]) |
12e35840 JD |
899 | AT_COMPILE([input$1]) |
900 | AT_PARSER_CHECK([./input$1], 0, | |
901 | [[<]]kind[[> for 'E' @ 1. | |
902 | <]]kind[[> for 'S' @ 1. | |
ec5479ce JD |
903 | ]], |
904 | [[Starting parse | |
905 | Entering state 0 | |
12e35840 JD |
906 | Reducing stack by rule 1 (line 46): |
907 | -> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1) | |
ec5479ce JD |
908 | Stack now 0 |
909 | Entering state 1 | |
910 | Reading a token: Now at end of input. | |
12e35840 | 911 | Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1) |
ec5479ce JD |
912 | Entering state 2 |
913 | Stack now 0 1 2 | |
12e35840 JD |
914 | Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1) |
915 | Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1) | |
ec5479ce JD |
916 | ]]) |
917 | ||
12e35840 JD |
918 | m4_popdef([kind]) |
919 | m4_popdef([not_kind]) | |
920 | ]) | |
921 | ||
922 | _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0) | |
923 | _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1) | |
924 | ||
ec5479ce | 925 | AT_CLEANUP |
9350499c JD |
926 | |
927 | ||
928 | ||
929 | ## ------------------------------------------------------------------ ## | |
930 | ## Default %printer and %destructor are not for error or $undefined. ## | |
931 | ## ------------------------------------------------------------------ ## | |
932 | ||
287b314e | 933 | AT_SETUP([Default %printer and %destructor are not for error or $undefined]) |
9350499c JD |
934 | |
935 | # If Bison were to apply the default %printer and %destructor to the error | |
936 | # token or to $undefined: | |
937 | # - For the error token: | |
938 | # - It would generate warnings for unused $n. | |
939 | # - It would invoke the %printer and %destructor on the error token's | |
940 | # semantic value, which would be initialized from the lookahead, which | |
941 | # would be destroyed separately. | |
942 | # - For $undefined, who knows what the semantic value would be. | |
55f48c48 | 943 | AT_BISON_OPTION_PUSHDEFS |
9350499c JD |
944 | AT_DATA_GRAMMAR([[input.y]], |
945 | [[%debug | |
946 | ||
947 | %{ | |
948 | # include <stdio.h> | |
cf806753 | 949 | # include <stdlib.h> |
55f48c48 AD |
950 | ]AT_YYERROR_DECLARE[ |
951 | ]AT_YYLEX_DECLARE[ | |
9350499c JD |
952 | # define USE(SYM) |
953 | %} | |
954 | ||
955 | %printer { | |
956 | fprintf (yyoutput, "'%c'", $$); | |
3ebecc24 | 957 | } <> <*> |
9350499c JD |
958 | %destructor { |
959 | fprintf (stderr, "DESTROY '%c'\n", $$); | |
3ebecc24 | 960 | } <> <*> |
9350499c JD |
961 | |
962 | %% | |
963 | ||
964 | start: | |
965 | { $$ = 'S'; } | |
966 | /* In order to reveal the problems that this bug caused during parsing, add | |
967 | * $2 to USE. */ | |
968 | | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; } | |
969 | ; | |
970 | ||
971 | %% | |
55f48c48 AD |
972 | ]AT_YYERROR_DEFINE[ |
973 | ]AT_YYLEX_DEFINE([abd], [yylval = res])[ | |
9350499c JD |
974 | int |
975 | main (void) | |
976 | { | |
977 | yydebug = 1; | |
978 | return yyparse (); | |
979 | } | |
980 | ]]) | |
55f48c48 | 981 | AT_BISON_OPTION_POPDEFS |
9350499c | 982 | |
da730230 | 983 | AT_BISON_CHECK([-o input.c input.y]) |
9350499c JD |
984 | AT_COMPILE([input]) |
985 | AT_PARSER_CHECK([./input], [1], [], | |
986 | [[Starting parse | |
987 | Entering state 0 | |
988 | Reading a token: Next token is token 'a' ('a') | |
989 | Shifting token 'a' ('a') | |
990 | Entering state 1 | |
991 | Reading a token: Next token is token 'b' ('b') | |
992 | syntax error | |
993 | Shifting token error () | |
994 | Entering state 3 | |
995 | Next token is token 'b' ('b') | |
996 | Shifting token 'b' ('b') | |
997 | Entering state 5 | |
998 | Reading a token: Next token is token $undefined () | |
999 | Error: popping token 'b' ('b') | |
1000 | DESTROY 'b' | |
1001 | Stack now 0 1 3 | |
1002 | Error: popping token error () | |
1003 | Stack now 0 1 | |
1004 | Shifting token error () | |
1005 | Entering state 3 | |
1006 | Next token is token $undefined () | |
1007 | Error: discarding token $undefined () | |
1008 | Error: popping token error () | |
1009 | Stack now 0 1 | |
1010 | Shifting token error () | |
1011 | Entering state 3 | |
1012 | Reading a token: Now at end of input. | |
1013 | Cleanup: discarding lookahead token $end () | |
1014 | Stack now 0 1 3 | |
1015 | Cleanup: popping token error () | |
1016 | Cleanup: popping token 'a' ('a') | |
1017 | DESTROY 'a' | |
1018 | ]]) | |
1019 | ||
1020 | AT_CLEANUP | |
1021 | ||
1022 | ||
1023 | ||
1024 | ## ------------------------------------------------------ ## | |
1025 | ## Default %printer and %destructor are not for $accept. ## | |
1026 | ## ------------------------------------------------------ ## | |
1027 | ||
287b314e | 1028 | AT_SETUP([Default %printer and %destructor are not for $accept]) |
9350499c JD |
1029 | |
1030 | # If YYSTYPE is a union and Bison were to apply the default %printer and | |
1031 | # %destructor to $accept: | |
1032 | # - The %printer and %destructor code generated for $accept would always be | |
1033 | # dead code because $accept is currently never shifted onto the stack. | |
1034 | # - $$ for $accept would always be of type YYSTYPE because it's not possible | |
1035 | # to declare `%type <field> $accept'. (Also true for $undefined.) | |
1036 | # - Thus, the compiler might complain that the user code assumes the wrong | |
1037 | # type for $$ since the code might assume the type associated with a | |
1038 | # specific union field, which is especially reasonable in C++ since that | |
1039 | # type may be a base type. This test case checks for this problem. (Also | |
1040 | # true for $undefined and the error token, so there are three warnings for | |
1041 | # %printer and three for %destructor.) | |
1042 | ||
55f48c48 | 1043 | AT_BISON_OPTION_PUSHDEFS |
9350499c JD |
1044 | AT_DATA_GRAMMAR([[input.y]], |
1045 | [[%debug /* So that %printer is actually compiled. */ | |
1046 | ||
1047 | %{ | |
1048 | # include <stdio.h> | |
cf806753 | 1049 | # include <stdlib.h> |
55f48c48 AD |
1050 | ]AT_YYERROR_DECLARE[ |
1051 | ]AT_YYLEX_DECLARE[ | |
9350499c JD |
1052 | # define USE(SYM) |
1053 | %} | |
1054 | ||
1055 | %printer { | |
1056 | char chr = $$; | |
1057 | fprintf (yyoutput, "'%c'", chr); | |
3ebecc24 | 1058 | } <> <*> |
9350499c JD |
1059 | %destructor { |
1060 | char chr = $$; | |
1061 | fprintf (stderr, "DESTROY '%c'\n", chr); | |
3ebecc24 | 1062 | } <> <*> |
9350499c JD |
1063 | |
1064 | %union { char chr; } | |
1065 | %type <chr> start | |
1066 | ||
1067 | %% | |
1068 | ||
1069 | start: { USE($$); } ; | |
1070 | ||
1071 | %% | |
55f48c48 AD |
1072 | ]AT_YYERROR_DEFINE[ |
1073 | ]AT_YYLEX_DEFINE([])[ | |
9350499c JD |
1074 | int |
1075 | main (void) | |
1076 | { | |
1077 | return yyparse (); | |
1078 | } | |
1079 | ]]) | |
55f48c48 | 1080 | AT_BISON_OPTION_POPDEFS |
9350499c | 1081 | |
da730230 | 1082 | AT_BISON_CHECK([-o input.c input.y]) |
9350499c JD |
1083 | AT_COMPILE([input]) |
1084 | ||
1085 | AT_CLEANUP | |
f91b1629 JD |
1086 | |
1087 | ||
1088 | ||
1089 | ## ------------------------------------------------------ ## | |
1090 | ## Default %printer and %destructor for mid-rule values. ## | |
1091 | ## ------------------------------------------------------ ## | |
1092 | ||
1093 | AT_SETUP([Default %printer and %destructor for mid-rule values]) | |
1094 | ||
55f48c48 | 1095 | AT_BISON_OPTION_PUSHDEFS |
f91b1629 JD |
1096 | AT_DATA_GRAMMAR([[input.y]], |
1097 | [[%debug /* So that %printer is actually compiled. */ | |
1098 | ||
1099 | %{ | |
1100 | # include <stdio.h> | |
1101 | # include <stdlib.h> | |
55f48c48 AD |
1102 | ]AT_YYERROR_DECLARE[ |
1103 | ]AT_YYLEX_DECLARE[ | |
f91b1629 JD |
1104 | # define USE(SYM) |
1105 | # define YYLTYPE int | |
c9e2da4f | 1106 | # define YYLLOC_DEFAULT(Current, Rhs, N) (void)(Rhs) |
f91b1629 JD |
1107 | # define YY_LOCATION_PRINT(File, Loc) |
1108 | %} | |
1109 | ||
3ebecc24 JD |
1110 | %printer { fprintf (yyoutput, "%d", @$); } <> |
1111 | %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <> | |
12e35840 JD |
1112 | %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*> |
1113 | %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*> | |
f91b1629 JD |
1114 | |
1115 | %% | |
1116 | ||
1117 | start: | |
1118 | { @$ = 1; } // Not set or used. | |
1119 | { USE ($$); @$ = 2; } // Both set and used. | |
1120 | { USE ($$); @$ = 3; } // Only set. | |
1121 | { @$ = 4; } // Only used. | |
1122 | 'c' | |
1123 | { USE (($$, $2, $4, $5)); @$ = 0; } | |
1124 | ; | |
1125 | ||
1126 | %% | |
55f48c48 AD |
1127 | ]AT_YYERROR_DEFINE[ |
1128 | ]AT_YYLEX_DEFINE([])[ | |
f91b1629 JD |
1129 | int |
1130 | main (void) | |
1131 | { | |
1132 | yydebug = 1; | |
1133 | return yyparse (); | |
1134 | } | |
1135 | ]]) | |
55f48c48 | 1136 | AT_BISON_OPTION_POPDEFS |
f91b1629 | 1137 | |
da730230 | 1138 | AT_BISON_CHECK([-o input.c input.y], 0,, |
12e35840 JD |
1139 | [[input.y:33.3-23: warning: unset value: $$ |
1140 | input.y:30.3-35.37: warning: unused value: $3 | |
f91b1629 JD |
1141 | ]]) |
1142 | ||
1143 | AT_COMPILE([input]) | |
1144 | AT_PARSER_CHECK([./input], 1,, | |
1145 | [[Starting parse | |
1146 | Entering state 0 | |
12e35840 | 1147 | Reducing stack by rule 1 (line 30): |
f91b1629 JD |
1148 | -> $$ = nterm $@1 (: ) |
1149 | Stack now 0 | |
1150 | Entering state 2 | |
12e35840 | 1151 | Reducing stack by rule 2 (line 31): |
f91b1629 JD |
1152 | -> $$ = nterm @2 (: 2) |
1153 | Stack now 0 2 | |
1154 | Entering state 4 | |
12e35840 | 1155 | Reducing stack by rule 3 (line 32): |
f91b1629 JD |
1156 | -> $$ = nterm @3 (: 3) |
1157 | Stack now 0 2 4 | |
1158 | Entering state 5 | |
12e35840 | 1159 | Reducing stack by rule 4 (line 33): |
f91b1629 JD |
1160 | -> $$ = nterm @4 (: 4) |
1161 | Stack now 0 2 4 5 | |
1162 | Entering state 6 | |
1163 | Reading a token: Now at end of input. | |
1164 | syntax error | |
1165 | Error: popping nterm @4 (: 4) | |
1166 | DESTROY 4 | |
1167 | Stack now 0 2 4 5 | |
1168 | Error: popping nterm @3 (: 3) | |
1169 | DESTROY 3 | |
1170 | Stack now 0 2 4 | |
1171 | Error: popping nterm @2 (: 2) | |
1172 | DESTROY 2 | |
1173 | Stack now 0 2 | |
1174 | Error: popping nterm $@1 (: ) | |
1175 | Stack now 0 | |
1176 | Cleanup: discarding lookahead token $end (: ) | |
1177 | Stack now 0 | |
1178 | ]]) | |
1179 | ||
1180 | AT_CLEANUP | |
e785ccf7 JD |
1181 | |
1182 | ||
1183 | ## ----------------------- ## | |
1184 | ## @$ implies %locations. ## | |
1185 | ## ----------------------- ## | |
1186 | ||
1187 | # Bison once forgot to check for @$ in actions other than semantic actions. | |
1188 | ||
1189 | # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE) | |
55f48c48 | 1190 | # ------------------------------------------- |
e785ccf7 JD |
1191 | m4_define([AT_CHECK_ACTION_LOCATIONS], |
1192 | [AT_SETUP([[@$ in ]$1[ implies %locations]]) | |
55f48c48 | 1193 | AT_BISON_OPTION_PUSHDEFS |
e785ccf7 JD |
1194 | AT_DATA_GRAMMAR([[input.y]], |
1195 | [[%code { | |
1196 | #include <stdio.h> | |
55f48c48 AD |
1197 | ]AT_YYERROR_DECLARE[ |
1198 | ]AT_YYLEX_DECLARE[ | |
e785ccf7 JD |
1199 | } |
1200 | ||
1201 | %debug | |
1202 | ||
1203 | ]$1[ { | |
1204 | printf ("%d\n", @$.first_line); | |
1205 | } ]m4_if($1, [%initial-action], [], [[start]])[ | |
1206 | ||
1207 | %% | |
1208 | ||
1209 | start: ; | |
1210 | ||
1211 | %% | |
1212 | ||
1213 | static int | |
1214 | yylex (void) | |
1215 | { | |
1216 | return 0; | |
1217 | } | |
1218 | ||
55f48c48 | 1219 | ]AT_YYERROR_DEFINE[ |
e785ccf7 JD |
1220 | int |
1221 | main (void) | |
1222 | { | |
1223 | return yyparse (); | |
1224 | } | |
1225 | ]]) | |
1226 | ||
da730230 | 1227 | AT_BISON_CHECK([[-o input.c input.y]]) |
e785ccf7 | 1228 | AT_COMPILE([[input]]) |
55f48c48 | 1229 | AT_BISON_OPTION_POPDEFS |
e785ccf7 JD |
1230 | AT_CLEANUP]) |
1231 | ||
1232 | AT_CHECK_ACTION_LOCATIONS([[%initial-action]]) | |
1233 | AT_CHECK_ACTION_LOCATIONS([[%destructor]]) | |
1234 | AT_CHECK_ACTION_LOCATIONS([[%printer]]) | |
42f4393a DJ |
1235 | |
1236 | ||
1237 | ## ----------------------------------------------- ## | |
1238 | ## Fix user actions without a trailing semicolon. ## | |
1239 | ## ----------------------------------------------- ## | |
1240 | ||
1241 | AT_SETUP([[Fix user actions without a trailing semicolon]]) | |
1242 | ||
1243 | # This feature is undocumented, but we accidentally broke it in 2.3a, | |
1244 | # and there was a complaint at: | |
1245 | # <http://lists.gnu.org/archive/html/bug-bison/2008-11/msg00001.html>. | |
55f48c48 | 1246 | AT_BISON_OPTION_PUSHDEFS |
42f4393a DJ |
1247 | AT_DATA([input.y], |
1248 | [[%% | |
1249 | start: test2 test1 test0 testc; | |
1250 | ||
1251 | test2 | |
1252 | : 'a' { semi; /* TEST:N:2 */ } | |
1253 | | 'b' { if (0) {no_semi} /* TEST:N:2 */ } | |
1254 | | 'c' { if (0) {semi;} /* TEST:N:2 */ } | |
1255 | | 'd' { semi; no_semi /* TEST:Y:2 */ } | |
1256 | | 'e' { semi(); no_semi() /* TEST:Y:2 */ } | |
1257 | | 'f' { semi[]; no_semi[] /* TEST:Y:2 */ } | |
1258 | | 'g' { semi++; no_semi++ /* TEST:Y:2 */ } | |
1259 | | 'h' { {no_semi} no_semi /* TEST:Y:2 */ } | |
1260 | | 'i' { {semi;} no_semi /* TEST:Y:2 */ } | |
1261 | ; | |
1262 | test1 | |
1263 | : 'a' { semi; // TEST:N:1 ; | |
1264 | } | 'b' { if (0) {no_semi} // TEST:N:1 ; | |
1265 | } | 'c' { if (0) {semi;} // TEST:N:1 ; | |
1266 | } | 'd' { semi; no_semi // TEST:Y:1 ; | |
1267 | } | 'e' { semi(); no_semi() // TEST:Y:1 ; | |
1268 | } | 'f' { semi[]; no_semi[] // TEST:Y:1 ; | |
1269 | } | 'g' { semi++; no_semi++ // TEST:Y:1 ; | |
1270 | } | 'h' { {no_semi} no_semi // TEST:Y:1 ; | |
1271 | } | 'i' { {semi;} no_semi // TEST:Y:1 ; | |
1272 | } ; | |
1273 | test0 | |
1274 | : 'a' { semi; // TEST:N:1 {} | |
1275 | } | 'b' { if (0) {no_semi} // TEST:N:1 {} | |
1276 | } | 'c' { if (0) {semi;} // TEST:N:1 {} | |
1277 | } | 'd' { semi; no_semi // TEST:Y:1 {} | |
1278 | } | 'e' { semi(); no_semi() // TEST:Y:1 {} | |
1279 | } | 'f' { semi[]; no_semi[] // TEST:Y:1 {} | |
1280 | } | 'g' { semi++; no_semi++ // TEST:Y:1 {} | |
1281 | } | 'h' { {no_semi} no_semi // TEST:Y:1 {} | |
1282 | } | 'i' { {semi;} no_semi // TEST:Y:1 {} | |
1283 | } ; | |
1284 | ||
1285 | testc | |
1286 | : 'a' { | |
1287 | #define TEST_MACRO_N \ | |
1288 | []"broken\" $ @ $$ @$ [];\ | |
1289 | string;"} | |
1290 | | 'b' { | |
1291 | no_semi | |
1292 | #define TEST_MACRO_N \ | |
1293 | []"broken\" $ @ $$ @$ [];\ | |
1294 | string;"} | |
1295 | ]]) | |
55f48c48 | 1296 | AT_BISON_OPTION_POPDEFS |
42f4393a DJ |
1297 | |
1298 | AT_BISON_CHECK([[-o input.c input.y]], [0], [], | |
9874f80b JM |
1299 | [[input.y:8.48: warning: a ';' might be needed at the end of action code |
1300 | input.y:8.48: warning: future versions of Bison will not add the ';' | |
1301 | input.y:9.48: warning: a ';' might be needed at the end of action code | |
1302 | input.y:9.48: warning: future versions of Bison will not add the ';' | |
1303 | input.y:10.48: warning: a ';' might be needed at the end of action code | |
1304 | input.y:10.48: warning: future versions of Bison will not add the ';' | |
1305 | input.y:11.48: warning: a ';' might be needed at the end of action code | |
1306 | input.y:11.48: warning: future versions of Bison will not add the ';' | |
1307 | input.y:12.48: warning: a ';' might be needed at the end of action code | |
1308 | input.y:12.48: warning: future versions of Bison will not add the ';' | |
1309 | input.y:13.48: warning: a ';' might be needed at the end of action code | |
1310 | input.y:13.48: warning: future versions of Bison will not add the ';' | |
1311 | input.y:20.1: warning: a ';' might be needed at the end of action code | |
1312 | input.y:20.1: warning: future versions of Bison will not add the ';' | |
1313 | input.y:21.1: warning: a ';' might be needed at the end of action code | |
1314 | input.y:21.1: warning: future versions of Bison will not add the ';' | |
1315 | input.y:22.1: warning: a ';' might be needed at the end of action code | |
1316 | input.y:22.1: warning: future versions of Bison will not add the ';' | |
1317 | input.y:23.1: warning: a ';' might be needed at the end of action code | |
1318 | input.y:23.1: warning: future versions of Bison will not add the ';' | |
1319 | input.y:24.1: warning: a ';' might be needed at the end of action code | |
1320 | input.y:24.1: warning: future versions of Bison will not add the ';' | |
1321 | input.y:25.1: warning: a ';' might be needed at the end of action code | |
1322 | input.y:25.1: warning: future versions of Bison will not add the ';' | |
1323 | input.y:31.1: warning: a ';' might be needed at the end of action code | |
1324 | input.y:31.1: warning: future versions of Bison will not add the ';' | |
1325 | input.y:32.1: warning: a ';' might be needed at the end of action code | |
1326 | input.y:32.1: warning: future versions of Bison will not add the ';' | |
1327 | input.y:33.1: warning: a ';' might be needed at the end of action code | |
1328 | input.y:33.1: warning: future versions of Bison will not add the ';' | |
1329 | input.y:34.1: warning: a ';' might be needed at the end of action code | |
1330 | input.y:34.1: warning: future versions of Bison will not add the ';' | |
1331 | input.y:35.1: warning: a ';' might be needed at the end of action code | |
1332 | input.y:35.1: warning: future versions of Bison will not add the ';' | |
1333 | input.y:36.1: warning: a ';' might be needed at the end of action code | |
1334 | input.y:36.1: warning: future versions of Bison will not add the ';' | |
42f4393a | 1335 | ]]) |
c4fae1ef AD |
1336 | |
1337 | AT_MATCHES_CHECK([input.c], [[/\* TEST:N:2 \*/ \}$]], [[3]]) | |
1338 | AT_MATCHES_CHECK([input.c], [[/\* TEST:Y:2 \*/ ;\}$]], [[6]]) | |
1339 | AT_MATCHES_CHECK([input.c], [[// TEST:N:1 [;{}]*\n\}$]], [[6]]) | |
1340 | AT_MATCHES_CHECK([input.c], [[// TEST:Y:1 [;{}]*\n;\}$]], [[12]]) | |
1341 | AT_MATCHES_CHECK([input.c], [[#define TEST_MACRO_N \\\n\[\]"broken\\" \$ \@ \$\$ \@\$ \[\];\\\nstring;"\}]], [[2]]) | |
42f4393a DJ |
1342 | |
1343 | AT_CLEANUP | |
abcc7c03 JD |
1344 | |
1345 | ||
1346 | ## -------------------------------------------------- ## | |
1347 | ## Destroying lookahead assigned by semantic action. ## | |
1348 | ## -------------------------------------------------- ## | |
1349 | ||
1350 | AT_SETUP([[Destroying lookahead assigned by semantic action]]) | |
1351 | ||
55f48c48 | 1352 | AT_BISON_OPTION_PUSHDEFS |
abcc7c03 JD |
1353 | AT_DATA_GRAMMAR([input.y], |
1354 | [[ | |
1355 | %code { | |
1356 | #include <assert.h> | |
1357 | #include <stdio.h> | |
55f48c48 AD |
1358 | ]AT_YYERROR_DECLARE[ |
1359 | ]AT_YYLEX_DECLARE[ | |
abcc7c03 JD |
1360 | #define USE(Var) |
1361 | } | |
1362 | ||
1363 | %destructor { fprintf (stderr, "'a' destructor\n"); } 'a' | |
1364 | %destructor { fprintf (stderr, "'b' destructor\n"); } 'b' | |
1365 | ||
1366 | %% | |
1367 | ||
1368 | // In a previous version of Bison, yychar assigned by the semantic | |
1369 | // action below was not translated into yytoken before the lookahead was | |
1370 | // discarded and thus before its destructor (selected according to | |
1371 | // yytoken) was called in order to return from yyparse. This would | |
1372 | // happen even if YYACCEPT was performed in a later semantic action as | |
1373 | // long as only consistent states with default reductions were visited | |
1374 | // in between. However, we leave YYACCEPT in the same semantic action | |
1375 | // for this test in order to show that skeletons cannot simply translate | |
1376 | // immediately after every semantic action because a semantic action | |
1377 | // that has set yychar might not always return normally. Instead, | |
1378 | // skeletons must translate before every use of yytoken. | |
1379 | start: 'a' accept { USE($1); } ; | |
1380 | accept: /*empty*/ { | |
1381 | assert (yychar == YYEMPTY); | |
1382 | yychar = 'b'; | |
1383 | YYACCEPT; | |
1384 | } ; | |
1385 | ||
1386 | %% | |
55f48c48 AD |
1387 | ]AT_YYERROR_DEFINE[ |
1388 | ]AT_YYLEX_DEFINE([a])[ | |
abcc7c03 JD |
1389 | int |
1390 | main (void) | |
1391 | { | |
1392 | return yyparse (); | |
1393 | } | |
1394 | ]]) | |
55f48c48 | 1395 | AT_BISON_OPTION_POPDEFS |
abcc7c03 JD |
1396 | AT_BISON_CHECK([[-o input.c input.y]]) |
1397 | AT_COMPILE([[input]]) | |
1398 | AT_PARSER_CHECK([[./input]], [[0]], [], | |
1399 | [['b' destructor | |
1400 | 'a' destructor | |
1401 | ]]) | |
1402 | ||
1403 | AT_CLEANUP | |
94556574 AD |
1404 | |
1405 | ## ---------- ## | |
1406 | ## YYBACKUP. ## | |
1407 | ## ---------- ## | |
1408 | ||
1409 | AT_SETUP([[YYBACKUP]]) | |
1410 | ||
55f48c48 AD |
1411 | AT_BISON_OPTION_PUSHDEFS([%pure-parser]) |
1412 | ||
94556574 AD |
1413 | AT_DATA_GRAMMAR([input.y], |
1414 | [[ | |
1415 | %error-verbose | |
1416 | %debug | |
1417 | %pure-parser | |
1418 | %code { | |
1419 | # include <stdio.h> | |
1420 | # include <stdlib.h> | |
1421 | # include <assert.h> | |
1422 | ||
55f48c48 | 1423 | ]AT_YYERROR_DECLARE[ |
94556574 AD |
1424 | static int yylex (YYSTYPE *yylval); |
1425 | } | |
1426 | %% | |
1427 | input: | |
1428 | exp exp {} | |
1429 | ; | |
1430 | ||
1431 | exp: | |
1432 | 'a' { printf ("a: %d\n", $1); } | |
1433 | | 'b' { YYBACKUP('a', 123); } | |
1434 | | 'c' 'd' { YYBACKUP('a', 456); } | |
1435 | ; | |
1436 | ||
1437 | %% | |
55f48c48 | 1438 | ]AT_YYERROR_DEFINE[ |
94556574 AD |
1439 | static int |
1440 | yylex (YYSTYPE *yylval) | |
1441 | { | |
1442 | static char const input[] = "bcd"; | |
1443 | static size_t toknum; | |
1444 | assert (toknum < sizeof input); | |
1445 | *yylval = (toknum + 1) * 10; | |
1446 | return input[toknum++]; | |
1447 | } | |
1448 | ||
94556574 AD |
1449 | int |
1450 | main (void) | |
1451 | { | |
1452 | yydebug = !!getenv("YYDEBUG"); | |
1453 | return yyparse (); | |
1454 | } | |
1455 | ]]) | |
55f48c48 | 1456 | AT_BISON_OPTION_POPDEFS |
94556574 AD |
1457 | |
1458 | AT_BISON_CHECK([[-o input.c input.y]]) | |
1459 | AT_COMPILE([[input]]) | |
1460 | AT_PARSER_CHECK([[./input]], [[0]], | |
1461 | [[a: 123 | |
1462 | a: 456 | |
94556574 AD |
1463 | ]]) |
1464 | ||
1465 | AT_CLEANUP |