1 # Executing Actions. -*- Autotest -*-
2 # Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 AT_BANNER([[User Actions.]])
21 ## ------------------ ##
22 ## Mid-rule actions. ##
23 ## ------------------ ##
25 AT_SETUP([Mid-rule actions])
27 # Bison once forgot the mid-rule actions. It was because the action
28 # was attached to the host rule (the one with the mid-rule action),
29 # instead of being attached to the empty rule dedicated to this
32 AT_DATA_GRAMMAR([[input.y]],
38 static void yyerror (const char *msg);
39 static int yylex (void);
42 exp: { putchar ('0'); }
43 '1' { putchar ('1'); }
44 '2' { putchar ('2'); }
45 '3' { putchar ('3'); }
46 '4' { putchar ('4'); }
47 '5' { putchar ('5'); }
48 '6' { putchar ('6'); }
49 '7' { putchar ('7'); }
50 '8' { putchar ('8'); }
51 '9' { putchar ('9'); }
58 static const char *input = "123456789";
63 yyerror (const char *msg)
65 fprintf (stderr, "%s\n", msg);
75 AT_CHECK([bison -d -v -o input.c input.y])
77 AT_PARSER_CHECK([./input], 0,
87 ## ---------------- ##
89 ## ---------------- ##
91 AT_SETUP([Exotic Dollars])
93 AT_DATA_GRAMMAR([[input.y]],
99 static void yyerror (const char *msg);
100 static int yylex (void);
109 %type <val> a_1 a_2 a_5
110 sum_of_the_five_previous_values
113 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
114 sum_of_the_five_previous_values
116 USE (($1, $2, $<foo>3, $<foo>4, $5));
124 sum_of_the_five_previous_values:
126 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
138 yyerror (const char *msg)
140 fprintf (stderr, "%s\n", msg);
150 AT_CHECK([bison -d -v -o input.c input.y], 0)
152 AT_PARSER_CHECK([./input], 0,
160 ## -------------------------- ##
161 ## Printers and Destructors. ##
162 ## -------------------------- ##
164 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
165 # -----------------------------------------------------------------------------
166 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
167 [# Make sure complex $n work.
168 m4_if([$1$2$3], $[1]$[2]$[3], [],
169 [m4_fatal([$0: Invalid arguments: $@])])dnl
171 # Be sure to pass all the %directives to this macro to have correct
172 # helping macros. So don't put any directly in the Bison file.
173 AT_BISON_OPTION_PUSHDEFS([$5])
174 AT_DATA_GRAMMAR([[input.y]],
180 #define YYINITDEPTH 10
181 #define YYMAXDEPTH 10
183 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
184 [#define RANGE(Location) (Location).first_line, (Location).last_line])
188 m4_ifval([$6], [%union
192 AT_LALR1_CC_IF([%define "global_tokens_and_yystype"])
193 m4_ifval([$6], [[%end-header {]], [[%start-header {]])
194 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;
195 m4_ifval([$6], , [#define YYSTYPE int])])
196 [static int yylex (]AT_LEX_FORMALS[);
197 ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
200 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
202 /* FIXME: This %printer isn't actually tested. */
205 ]AT_LALR1_CC_IF([debug_stream () << $$;],
206 [fprintf (yyoutput, "%d", $$)])[;
208 input line thing 'x' 'y'
211 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
215 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
219 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
223 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
227 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
232 { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
237 This grammar is made to exercise error recovery.
238 "Lines" starting with `(' support error recovery, with
239 ')' as synchronizing token. Lines starting with 'x' can never
240 be recovered from if in error.
247 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
249 | line input /* Right recursive to load the stack so that popping at
250 END can be exercised. */
253 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
254 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
259 thing thing thing ';'
262 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
263 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
264 $3, RANGE (@3), $4, RANGE (@4));
266 | '(' thing thing ')'
269 printf ("line (%d@%d-%d): '(' (%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));
276 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
277 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
282 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
283 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
291 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
292 $$, RANGE (@$), $1, RANGE (@1));
296 /* Alias to ARGV[1]. */
297 const char *source = 0;
300 yylex (]AT_LEX_FORMALS[)
302 static unsigned int counter = 0;
304 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
305 /* As in BASIC, line numbers go from 10 to 10. */
307 [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
308 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
310 [ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
311 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
315 printf ("sending: '%c'", source[c]);
317 printf ("sending: END");
318 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
323 [/* A C++ error reporting function. */
325 yy::parser::error (const location& l, const std::string& m)
327 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
335 parser.set_debug_level (yydebug);
336 return parser.parse ();
340 yyerror (const char *msg)
342 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
346 main (int argc, const char *argv[])
349 yydebug = !!getenv ("YYDEBUG");
355 case 0: printf ("Successful parse.\n"); break;
356 case 1: printf ("Parsing FAILED.\n"); break;
357 default: printf ("Parsing FAILED (status %d).\n", status); break;
364 [AT_CHECK([bison -o input.cc input.y])
365 AT_COMPILE_CXX([input])],
366 [AT_CHECK([bison -o input.c input.y])
367 AT_COMPILE([input])])
370 # Check the location of "empty"
371 # -----------------------------
372 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
373 # an empty "line" nterm.
374 # FIXME: This location is not satisfying. Depend on the lookahead?
375 AT_PARSER_CHECK([./input '(x)'], 0,
376 [[sending: '(' (0@0-9)
377 sending: 'x' (1@10-19)
378 thing (1@10-19): 'x' (1@10-19)
379 sending: ')' (2@20-29)
380 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
381 sending: END (3@30-39)
382 input (0@29-29): /* Nothing */
383 input (2@0-29): line (0@0-29) input (0@29-29)
384 Freeing token END (3@30-39)
385 Freeing nterm input (2@0-29)
390 # Check locations in error recovery
391 # ---------------------------------
392 # '(y)' is an error, but can be recovered from. But what's the location
393 # of the error itself ('y'), and of the resulting reduction ('(error)').
394 AT_PARSER_CHECK([./input '(y)'], 0,
395 [[sending: '(' (0@0-9)
396 sending: 'y' (1@10-19)
397 10-19: syntax error, unexpected 'y', expecting 'x'
398 Freeing token 'y' (1@10-19)
399 sending: ')' (2@20-29)
400 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
401 sending: END (3@30-39)
402 input (0@29-29): /* Nothing */
403 input (2@0-29): line (-1@0-29) input (0@29-29)
404 Freeing token END (3@30-39)
405 Freeing nterm input (2@0-29)
410 # Syntax errors caught by the parser
411 # ----------------------------------
412 # Exercise the discarding of stack top and input until `error'
415 # '(', 'x', 'x', 'x', 'x', 'x', ')',
417 # Load the stack and provoke an error that cannot be caught by the
418 # grammar, to check that the stack is cleared. And make sure the
419 # lookahead is freed.
424 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
425 [[sending: '(' (0@0-9)
426 sending: 'x' (1@10-19)
427 thing (1@10-19): 'x' (1@10-19)
428 sending: 'x' (2@20-29)
429 thing (2@20-29): 'x' (2@20-29)
430 sending: 'x' (3@30-39)
431 30-39: syntax error, unexpected 'x', expecting ')'
432 Freeing nterm thing (2@20-29)
433 Freeing nterm thing (1@10-19)
434 Freeing token 'x' (3@30-39)
435 sending: 'x' (4@40-49)
436 Freeing token 'x' (4@40-49)
437 sending: 'x' (5@50-59)
438 Freeing token 'x' (5@50-59)
439 sending: ')' (6@60-69)
440 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
441 sending: '(' (7@70-79)
442 sending: 'x' (8@80-89)
443 thing (8@80-89): 'x' (8@80-89)
444 sending: ')' (9@90-99)
445 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
446 sending: '(' (10@100-109)
447 sending: 'x' (11@110-119)
448 thing (11@110-119): 'x' (11@110-119)
449 sending: ')' (12@120-129)
450 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
451 sending: 'y' (13@130-139)
452 input (0@129-129): /* Nothing */
453 input (2@100-129): line (10@100-129) input (0@129-129)
454 input (2@70-129): line (7@70-99) input (2@100-129)
455 input (2@0-129): line (-1@0-69) input (2@70-129)
456 130-139: syntax error, unexpected 'y', expecting END
457 Freeing nterm input (2@0-129)
458 Freeing token 'y' (13@130-139)
463 # Syntax error caught by the parser where lookahead = END
464 # --------------------------------------------------------
465 # Load the stack and provoke an error that cannot be caught by the
466 # grammar, to check that the stack is cleared. And make sure the
467 # lookahead is freed.
472 AT_PARSER_CHECK([./input '(x)(x)x'], 1,
473 [[sending: '(' (0@0-9)
474 sending: 'x' (1@10-19)
475 thing (1@10-19): 'x' (1@10-19)
476 sending: ')' (2@20-29)
477 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
478 sending: '(' (3@30-39)
479 sending: 'x' (4@40-49)
480 thing (4@40-49): 'x' (4@40-49)
481 sending: ')' (5@50-59)
482 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
483 sending: 'x' (6@60-69)
484 thing (6@60-69): 'x' (6@60-69)
485 sending: END (7@70-79)
486 70-79: syntax error, unexpected END, expecting 'x'
487 Freeing nterm thing (6@60-69)
488 Freeing nterm line (3@30-59)
489 Freeing nterm line (0@0-29)
490 Freeing token END (7@70-79)
495 # Check destruction upon stack overflow
496 # -------------------------------------
497 # Upon stack overflow, all symbols on the stack should be destroyed.
498 # Only check for yacc.c.
500 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
501 [[sending: '(' (0@0-9)
502 sending: 'x' (1@10-19)
503 thing (1@10-19): 'x' (1@10-19)
504 sending: ')' (2@20-29)
505 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
506 sending: '(' (3@30-39)
507 sending: 'x' (4@40-49)
508 thing (4@40-49): 'x' (4@40-49)
509 sending: ')' (5@50-59)
510 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
511 sending: '(' (6@60-69)
512 sending: 'x' (7@70-79)
513 thing (7@70-79): 'x' (7@70-79)
514 sending: ')' (8@80-89)
515 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
516 sending: '(' (9@90-99)
517 sending: 'x' (10@100-109)
518 thing (10@100-109): 'x' (10@100-109)
519 sending: ')' (11@110-119)
520 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
521 sending: '(' (12@120-129)
522 sending: 'x' (13@130-139)
523 thing (13@130-139): 'x' (13@130-139)
524 sending: ')' (14@140-149)
525 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
526 sending: '(' (15@150-159)
527 sending: 'x' (16@160-169)
528 thing (16@160-169): 'x' (16@160-169)
529 sending: ')' (17@170-179)
530 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
531 sending: '(' (18@180-189)
532 sending: 'x' (19@190-199)
533 thing (19@190-199): 'x' (19@190-199)
534 sending: ')' (20@200-209)
535 200-209: memory exhausted
536 Freeing nterm thing (19@190-199)
537 Freeing nterm line (15@150-179)
538 Freeing nterm line (12@120-149)
539 Freeing nterm line (9@90-119)
540 Freeing nterm line (6@60-89)
541 Freeing nterm line (3@30-59)
542 Freeing nterm line (0@0-29)
543 Parsing FAILED (status 2).
550 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
551 # ---------------------------------------------------------------------------
552 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
553 [AT_SETUP([Printers and Destructors $2: $1])
556 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
567 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
568 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
570 # These tests currently fail on a Debian GNU/Linux 3.0r2 x86 host,
571 # but the 2nd test succeeds on a Solaris 9 sparc hosts (Forte 7 cc).
572 # Skip them until we figure out what the problem is.
573 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
574 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
576 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
577 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
583 ## --------------------------------- ##
584 ## Default %printer and %destructor. ##
585 ## --------------------------------- ##
587 # Check that the right %printer and %destructor are called, that they're not
588 # called for $end, and that $$ and @$ work correctly.
590 AT_SETUP([Default %printer and %destructor])
592 AT_DATA_GRAMMAR([[input.y]],
597 @$.first_line = @$.last_line = 1;
598 @$.first_column = @$.last_column = 1;
604 static void yyerror (const char *msg);
605 static int yylex (void);
610 fprintf (yyoutput, "Default printer for '%c' @ %d", $$, @$.first_column);
613 fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column);
617 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
620 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
625 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
632 static const char *input = "abcd";
633 static int column = 1;
635 yylloc.first_line = yylloc.last_line = 1;
636 yylloc.first_column = yylloc.last_column = column++;
641 yyerror (const char *msg)
643 fprintf (stderr, "%s\n", msg);
654 AT_CHECK([bison -o input.c input.y])
656 AT_PARSER_CHECK([./input], 1,
657 [[Default destructor for 'd' @ 4.
658 'b'/'c' destructor for 'c' @ 3.
659 'b'/'c' destructor for 'b' @ 2.
660 Default destructor for 'a' @ 1.
664 Reading a token: Next token is token 'a' (1.1-1.1: Default printer for 'a' @ 1)
665 Shifting token 'a' (1.1-1.1: Default printer for 'a' @ 1)
667 Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
668 Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
670 Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
671 Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
673 Reading a token: Next token is token 'd' (1.4-1.4: Default printer for 'd' @ 4)
674 Shifting token 'd' (1.4-1.4: Default printer for 'd' @ 4)
676 Reading a token: Now at end of input.
677 syntax error, unexpected $end, expecting 'e'
678 Error: popping token 'd' (1.4-1.4: Default printer for 'd' @ 4)
680 Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
682 Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
684 Error: popping token 'a' (1.1-1.1: Default printer for 'a' @ 1)
686 Cleanup: discarding lookahead token $end (1.5-1.5: )
696 ## ------------------------------------------------------------- ##
697 ## Default %printer and %destructor for user-declared end token. ##
698 ## ------------------------------------------------------------- ##
700 AT_SETUP([Default %printer and %destructor for user-declared end token])
702 AT_DATA_GRAMMAR([[input.y]],
707 @$.first_line = @$.last_line = 1;
708 @$.first_column = @$.last_column = 1;
714 static void yyerror (const char *msg);
715 static int yylex (void);
721 fprintf (yyoutput, "Default printer for '%c' @ %d", $$, @$.first_column);
724 fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column);
729 start: { $$ = 'S'; } ;
737 yylloc.first_line = yylloc.last_line = 1;
738 yylloc.first_column = yylloc.last_column = 1;
743 yyerror (const char *msg)
745 fprintf (stderr, "%s\n", msg);
756 AT_CHECK([bison -o input.c input.y])
758 AT_PARSER_CHECK([./input], 0,
759 [[Default destructor for 'E' @ 1.
760 Default destructor for 'S' @ 1.
764 Reducing stack by rule 1 (line 37):
765 -> $$ = nterm start (1.1-1.1: Default printer for 'S' @ 1)
768 Reading a token: Now at end of input.
769 Shifting token END (1.1-1.1: Default printer for 'E' @ 1)
772 Cleanup: popping token END (1.1-1.1: Default printer for 'E' @ 1)
773 Cleanup: popping nterm start (1.1-1.1: Default printer for 'S' @ 1)