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