1 # Executing Actions. -*- Autotest -*-
2 # Copyright (C) 2001, 2002, 2003, 2004 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., 59 Temple Place - Suite 330, 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,
85 ## ---------------------- ##
86 ## Actions after errors. ##
87 ## ---------------------- ##
89 AT_SETUP([Actions after errors])
93 AT_DATA_GRAMMAR([[input.y]],
98 static int yylex (void);
99 static void yyerror (char const *);
104 %type <ival> 'x' ';' thing line input
111 printf ("input (%d): /* Nothing */\n", $$);
113 | line input /* Right recursive to load the stack so that popping at
114 EOF can be exercised. */
117 printf ("input (%d): line (%d) input (%d)\n", $$, $1, $2);
122 thing thing thing ';'
125 printf ("line (%d): thing (%d) thing (%d) thing (%d) ';' (%d)\n",
131 printf ("line (%d): thing (%d) thing (%d) ';' (%d)\n", $$, $1, $2, $3);
136 printf ("line (%d): thing (%d) ';' (%d)\n", $$, $1, $2);
141 printf ("line (%d): error ';' (%d)\n", $$, $2);
149 printf ("thing (%d): 'x' (%d)\n", $$, $1);
153 static size_t counter;
158 static char const input[] =
160 /* Exercise the discarding of stack top and input until `error'
162 'x', 'x', 'x', 'x', 'x', 'x', ';',
164 /* Load the stack and provoke an error that cannot be caught by
165 the grammar, to check that the stack is cleared. */
171 if (counter < sizeof input)
173 yylval.ival = counter;
174 printf ("sending: '%c' (%d)\n", input[counter], yylval.ival);
175 return input[counter++];
179 printf ("sending: EOF\n");
185 yyerror (char const *msg)
187 printf ("%lu: %s\n", (unsigned long int) counter, msg);
193 yydebug = !!getenv ("YYDEBUG");
198 AT_CHECK([bison -o input.c input.y])
200 AT_PARSER_CHECK([./input], 1,
212 line (-1): error ';' (6)
218 line (7): thing (7) thing (8) ';' (9)
222 line (10): thing (10) ';' (11)
232 ## ---------------- ##
233 ## Exotic Dollars. ##
234 ## ---------------- ##
236 AT_SETUP([Exotic Dollars])
238 AT_DATA_GRAMMAR([[input.y]],
242 static void yyerror (const char *msg);
243 static int yylex (void);
245 # define YYERROR_VERBOSE 1
253 %type <val> a_1 a_2 a_5
254 sum_of_the_five_previous_values
257 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
258 sum_of_the_five_previous_values
267 sum_of_the_five_previous_values:
269 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
281 yyerror (const char *msg)
283 fprintf (stderr, "%s\n", msg);
293 AT_CHECK([bison -d -v -o input.c input.y])
295 AT_PARSER_CHECK([./input], 0,
303 ## -------------------------- ##
304 ## Printers and Destructors. ##
305 ## -------------------------- ##
307 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE)
308 # -----------------------------------------------------------------
309 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
310 [m4_if([$1$2$3], $[1]$[2]$[3], [],
311 [m4_fatal([$0: Invalid arguments: $@])])dnl
313 AT_SETUP([Printers and Destructors: $5])
315 # Make sure complex $n work.
317 AT_BISON_OPTION_PUSHDEFS([$5])
318 AT_DATA_GRAMMAR([[input.y]],
324 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
325 [#define RANGE(Location) (Location).first_line, (Location).last_line])
337 ]AT_LALR1_CC_IF([typedef yy::Location YYLTYPE;])
338 [static int yylex (]AT_LEX_FORMALS[);
339 ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
342 %type <ival> 'x' ';' thing line input
344 %printer { fprintf (yyoutput, "%d@%d-%d", $$, RANGE (@$)); }
348 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
352 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
356 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
360 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
368 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
370 | line input /* Right recursive to load the stack so that popping at
371 EOF can be exercised. */
374 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
375 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
380 thing thing thing ';'
383 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
384 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
385 $3, RANGE (@3), $4, RANGE (@4));
390 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
391 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
396 printf ("line (%d@%d-%d): thing (%d@%d-%d) ';' (%d@%d-%d)\n",
397 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
402 printf ("line (%d@%d-%d): error (@%d-%d) ';' (%d@%d-%d)\n",
403 $$, RANGE (@$), RANGE (@1), $2, RANGE (@2));
411 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
412 $$, RANGE (@$), $1, RANGE (@1));
417 yylex (]AT_LEX_FORMALS[)
419 static const unsigned int input[] =
421 /* Exercise the discarding of stack top and input until `error'
423 'x', 'x', 'x', 'x', 'x', 'x', ';',
425 /* Load the stack and provoke an error that cannot be caught by
426 the grammar, to check that the stack is cleared. */
431 static unsigned int counter = 0;
433 if (counter < (sizeof(input) / sizeof (input[0])))
436 [ yylval->ival = counter;
437 /* As in BASIC, line numbers go from 10 to 10. */
438 yylloc->begin.line = yylloc->begin.column = 10 * counter;
439 yylloc->end.line = yylloc->end.column = yylloc->begin.line + 9;
440 printf ("sending: '%c' (%d@%d-%d)\n",
441 input[[counter]], yylval->ival, RANGE (*yylloc));
442 return (int) input[[counter++]];
444 [ yylval.ival = counter;
445 /* As in BASIC, line numbers go from 10 to 10. */
446 yylloc.first_line = yylloc.first_column = 10 * counter;
447 yylloc.last_line = yylloc.last_column = yylloc.first_line + 9;
448 printf ("sending: '%c' (%d@%d-%d)\n",
449 input[[counter]], yylval.ival, RANGE (yylloc));
450 return (int) input[[counter++]];
455 printf ("sending: EOF\n");
461 [/* Currently, print_ is required in C++. */
463 yy::Parser::print_ ()
465 std::cerr << location;
468 /* A C++ error reporting function. */
470 yy::Parser::error_ ()
472 printf ("%d-%d: %s\n", RANGE (location), message.c_str());
479 yy::Parser parser (yydebug, yy::Location ());
480 return parser.parse ();
484 yyerror (const char *msg)
486 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
492 yydebug = !!getenv ("YYDEBUG");
495 printf ("Parsing FAILED.\n");
498 printf ("Successful parse.\n");
504 [AT_CHECK([bison -o input.cc input.y])
505 AT_COMPILE_CXX([input])],
506 [AT_CHECK([bison -o input.c input.y])
507 AT_COMPILE([input])])
508 AT_PARSER_CHECK([./input], 1,
509 [[sending: 'x' (0@0-9)
510 thing (0@0-9): 'x' (0@0-9)
511 sending: 'x' (1@10-19)
512 thing (1@10-19): 'x' (1@10-19)
513 sending: 'x' (2@20-29)
514 thing (2@20-29): 'x' (2@20-29)
515 sending: 'x' (3@30-39)
516 30-39: syntax error, unexpected 'x', expecting ';'
517 Freeing nterm thing (2@20-29)
518 Freeing nterm thing (1@10-19)
519 Freeing nterm thing (0@0-9)
520 Freeing token 'x' (3@30-39)
521 sending: 'x' (4@40-49)
522 Freeing token 'x' (4@40-49)
523 sending: 'x' (5@50-59)
524 Freeing token 'x' (5@50-59)
525 sending: ';' (6@60-69)
526 line (-1@0-69): error (@0-59) ';' (6@60-69)
527 sending: 'x' (7@70-79)
528 thing (7@70-79): 'x' (7@70-79)
529 sending: 'x' (8@80-89)
530 thing (8@80-89): 'x' (8@80-89)
531 sending: ';' (9@90-99)
532 line (7@70-99): thing (7@70-79) thing (8@80-89) ';' (9@90-99)
533 sending: 'x' (10@100-109)
534 thing (10@100-109): 'x' (10@100-109)
535 sending: ';' (11@110-119)
536 line (10@100-119): thing (10@100-109) ';' (11@110-119)
537 sending: 'y' (12@120-129)
538 120-129: syntax error, unexpected $undefined, expecting $end or 'x'
540 Freeing nterm line (10@100-119)
541 Freeing nterm line (7@70-99)
542 Freeing nterm line (-1@0-69)
550 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS])
551 # ------------------------------------------------
553 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
554 [_AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4], [$1])
558 AT_CHECK_PRINTER_AND_DESTRUCTOR()
559 AT_CHECK_PRINTER_AND_DESTRUCTOR([%locations %defines %skeleton "lalr1.cc"])
561 # FIXME. This test case fails.
562 #AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])