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