1 # Executing Actions. -*- Autotest -*-
2 # Copyright (C) 2001, 2002, 2003, 2004, 2005 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]],
36 static void yyerror (const char *msg);
37 static int yylex (void);
39 # define YYERROR_VERBOSE 1
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]],
97 static void yyerror (const char *msg);
98 static int yylex (void);
100 # define YYERROR_VERBOSE 1
108 %type <val> a_1 a_2 a_5
109 sum_of_the_five_previous_values
112 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
113 sum_of_the_five_previous_values
122 sum_of_the_five_previous_values:
124 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
136 yyerror (const char *msg)
138 fprintf (stderr, "%s\n", msg);
148 AT_CHECK([bison -d -v -o input.c input.y])
150 AT_PARSER_CHECK([./input], 0,
158 ## -------------------------- ##
159 ## Printers and Destructors. ##
160 ## -------------------------- ##
162 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
163 # -----------------------------------------------------------------------------
164 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
165 [# Make sure complex $n work.
166 m4_if([$1$2$3], $[1]$[2]$[3], [],
167 [m4_fatal([$0: Invalid arguments: $@])])dnl
169 # Be sure to pass all the %directives to this macro to have correct
170 # helping macros. So don't put any directly in the Bison file.
171 AT_BISON_OPTION_PUSHDEFS([$5])
172 AT_DATA_GRAMMAR([[input.y]],
178 #define YYINITDEPTH 10
179 #define YYMAXDEPTH 10
181 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
182 [#define RANGE(Location) (Location).first_line, (Location).last_line])
186 m4_ifval([$6], [%union
192 ]AT_LALR1_CC_IF([typedef yy::location YYLTYPE;
193 m4_ifval([$6], , [#define YYSTYPE int])])
194 [static int yylex (]AT_LEX_FORMALS[);
195 ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
198 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input])[
202 ]AT_LALR1_CC_IF([debug_stream () << $$;],
203 [fprintf (yyoutput, "%d", $$)])[;
205 input line thing 'x' 'y'
208 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
212 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
216 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
220 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
224 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
229 This grammar is made to exercise error recovery.
230 "Lines" starting with `(' support error recovery, with
231 ')' as synchronizing token. Lines starting with 'x' can never
232 be recovered from if in error.
239 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
241 | line input /* Right recursive to load the stack so that popping at
242 EOF can be exercised. */
245 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
246 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
251 thing thing thing ';'
254 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
255 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
256 $3, RANGE (@3), $4, RANGE (@4));
258 | '(' thing thing ')'
261 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
262 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
263 $3, RANGE (@3), $4, RANGE (@4));
268 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
269 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
274 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
275 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
283 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
284 $$, RANGE (@$), $1, RANGE (@1));
288 /* Alias to ARGV[1]. */
289 const char *yysource = 0;
292 yylex (]AT_LEX_FORMALS[)
294 static unsigned int counter = 0;
296 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
297 /* As in BASIC, line numbers go from 10 to 10. */
299 [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
300 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
302 [ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
303 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
307 printf ("sending: '%c'", yysource[c]);
309 printf ("sending: EOF");
310 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
315 [/* A C++ error reporting function. */
317 yy::parser::error (const location& l, const std::string& m)
319 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
327 parser.set_debug_level (yydebug);
328 return parser.parse ();
332 yyerror (const char *msg)
334 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
338 main (int argc, const char *argv[])
341 yydebug = !!getenv ("YYDEBUG");
347 case 0: printf ("Successful parse.\n"); break;
348 case 1: printf ("Parsing FAILED.\n"); break;
349 default: printf ("Parsing FAILED (status %d).\n", status); break;
356 [AT_CHECK([bison -o input.cc input.y])
357 AT_COMPILE_CXX([input])],
358 [AT_CHECK([bison -o input.c input.y])
359 AT_COMPILE([input])])
362 # Check the location of "empty"
363 # -----------------------------
364 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
365 # an empty "line" nterm.
366 # FIXME: This location is not satisfying. Depend on the lookahead?
367 AT_PARSER_CHECK([./input '(x)'], 0,
368 [[sending: '(' (0@0-9)
369 sending: 'x' (1@10-19)
370 thing (1@10-19): 'x' (1@10-19)
371 sending: ')' (2@20-29)
372 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
373 sending: EOF (3@30-39)
374 input (0@29-29): /* Nothing */
375 input (2@0-29): line (0@0-29) input (0@29-29)
376 Freeing nterm input (2@0-29)
381 # Check locations in error recovery
382 # ---------------------------------
383 # '(y)' is an error, but can be recovered from. But what's the location
384 # of the error itself ('y'), and of the resulting reduction ('(error)').
385 AT_PARSER_CHECK([./input '(y)'], 0,
386 [[sending: '(' (0@0-9)
387 sending: 'y' (1@10-19)
388 10-19: syntax error, unexpected 'y', expecting 'x'
389 Freeing token 'y' (1@10-19)
390 sending: ')' (2@20-29)
391 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
392 sending: EOF (3@30-39)
393 input (0@29-29): /* Nothing */
394 input (2@0-29): line (-1@0-29) input (0@29-29)
395 Freeing nterm input (2@0-29)
400 # Syntax errors caught by the parser
401 # ----------------------------------
402 # Exercise the discarding of stack top and input until `error'
405 # '(', 'x', 'x', 'x', 'x', 'x', ')',
407 # Load the stack and provoke an error that cannot be caught by the
408 # grammar, to check that the stack is cleared. And make sure the
409 # lookahead is freed.
414 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
415 [[sending: '(' (0@0-9)
416 sending: 'x' (1@10-19)
417 thing (1@10-19): 'x' (1@10-19)
418 sending: 'x' (2@20-29)
419 thing (2@20-29): 'x' (2@20-29)
420 sending: 'x' (3@30-39)
421 30-39: syntax error, unexpected 'x', expecting ')'
422 Freeing nterm thing (2@20-29)
423 Freeing nterm thing (1@10-19)
424 Freeing token 'x' (3@30-39)
425 sending: 'x' (4@40-49)
426 Freeing token 'x' (4@40-49)
427 sending: 'x' (5@50-59)
428 Freeing token 'x' (5@50-59)
429 sending: ')' (6@60-69)
430 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
431 sending: '(' (7@70-79)
432 sending: 'x' (8@80-89)
433 thing (8@80-89): 'x' (8@80-89)
434 sending: ')' (9@90-99)
435 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
436 sending: '(' (10@100-109)
437 sending: 'x' (11@110-119)
438 thing (11@110-119): 'x' (11@110-119)
439 sending: ')' (12@120-129)
440 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
441 sending: 'y' (13@130-139)
442 input (0@129-129): /* Nothing */
443 input (2@100-129): line (10@100-129) input (0@129-129)
444 input (2@70-129): line (7@70-99) input (2@100-129)
445 input (2@0-129): line (-1@0-69) input (2@70-129)
446 130-139: syntax error, unexpected 'y', expecting $end
447 Freeing nterm input (2@0-129)
448 Freeing token 'y' (13@130-139)
452 # Check destruction upon stack overflow
453 # -------------------------------------
454 # Upon stack overflow, all symbols on the stack should be destroyed.
455 # Only check for yacc.c.
457 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
458 [[sending: '(' (0@0-9)
459 sending: 'x' (1@10-19)
460 thing (1@10-19): 'x' (1@10-19)
461 sending: ')' (2@20-29)
462 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
463 sending: '(' (3@30-39)
464 sending: 'x' (4@40-49)
465 thing (4@40-49): 'x' (4@40-49)
466 sending: ')' (5@50-59)
467 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
468 sending: '(' (6@60-69)
469 sending: 'x' (7@70-79)
470 thing (7@70-79): 'x' (7@70-79)
471 sending: ')' (8@80-89)
472 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
473 sending: '(' (9@90-99)
474 sending: 'x' (10@100-109)
475 thing (10@100-109): 'x' (10@100-109)
476 sending: ')' (11@110-119)
477 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
478 sending: '(' (12@120-129)
479 sending: 'x' (13@130-139)
480 thing (13@130-139): 'x' (13@130-139)
481 sending: ')' (14@140-149)
482 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
483 sending: '(' (15@150-159)
484 sending: 'x' (16@160-169)
485 thing (16@160-169): 'x' (16@160-169)
486 sending: ')' (17@170-179)
487 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
488 sending: '(' (18@180-189)
489 sending: 'x' (19@190-199)
490 thing (19@190-199): 'x' (19@190-199)
491 sending: ')' (20@200-209)
492 200-209: memory exhausted
493 Freeing nterm thing (19@190-199)
494 Freeing nterm line (15@150-179)
495 Freeing nterm line (12@120-149)
496 Freeing nterm line (9@90-119)
497 Freeing nterm line (6@60-89)
498 Freeing nterm line (3@30-59)
499 Freeing nterm line (0@0-29)
500 Parsing FAILED (status 2).
507 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
508 # ---------------------------------------------------------------------------
509 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
510 [AT_SETUP([Printers and Destructors $2: $1])
513 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
524 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
525 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
527 # These tests currently fail on a Debian GNU/Linux 3.0r2 x86 host,
528 # but the 2nd test succeeds on a Solaris 9 sparc hosts (Forte 7 cc).
529 # Skip them until we figure out what the problem is.
530 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
531 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
533 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
534 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])