1 # Executing Actions. -*- Autotest -*-
2 # Copyright 2001 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 /* Exericise 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' (value = %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,
201 [[sending: 'x' (value = 0)
203 sending: 'x' (value = 1)
205 sending: 'x' (value = 2)
207 sending: 'x' (value = 3)
209 sending: 'x' (value = 4)
210 sending: 'x' (value = 5)
211 sending: ';' (value = 6)
212 line(-1): error ';'(6)
213 sending: 'x' (value = 7)
215 sending: 'x' (value = 8)
217 sending: ';' (value = 9)
218 line(7): thing(7) thing(8) ';'(9)
219 sending: 'x' (value = 10)
221 sending: ';' (value = 11)
222 line(10): thing(10) ';'(11)
223 sending: 'y' (value = 12)
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, 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: $4])
315 # Make sure complex $n work.
317 AT_DATA_GRAMMAR([[input.y]],
324 static int yylex (void);
325 static void yyerror (const char *msg);
335 %type <ival> 'x' thing line input
337 %printer { fprintf (yyout, "%d@%d", $$, @$.first_line); }
341 { fprintf (stdout, "Freeing nterm input (%d@%d)\n", $$, @$.first_line); }
345 { fprintf (stdout, "Freeing nterm line (%d@%d)\n", $$, @$.first_line); }
349 { fprintf (stdout, "Freeing nterm thing (%d@%d)\n", $$, @$.first_line); }
353 { fprintf (stdout, "Freeing token 'x' (%d@%d)\n", $$, @$.first_line); }
361 printf ("input(%d@%d): /* Nothing */';'\n", $$, @$.first_line);
363 | line input /* Right recursive to load the stack so that popping at
364 EOF can be exercised. */
367 printf ("input(%d@%d): line(%d@%d) input(%d@%d)';'\n",
368 $$, @$.first_line, $1, @1.first_line, $2, @2.first_line);
373 thing thing thing ';'
376 printf ("line(%d@%d): thing(%d@%d) thing(%d@%d) thing(%d@%d) ';'\n",
377 $$, @$.first_line, $1, @1.first_line, $2, @2.first_line,
383 printf ("line(%d@%d): thing(%d@%d) thing(%d@%d) ';'\n",
384 $$, @$.first_line, $1, @1.first_line, $2, @2.first_line);
389 printf ("line(%d@%d): thing(%d@%d) ';'\n",
390 $$, @$.first_line, $1, @1.first_line);
395 printf ("line(%d@%d): error(@%d) ';'\n",
396 $$, @$.first_line, @1.first_line);
404 printf ("thing(%d@%d): 'x'(%d@%d)\n",
405 $$, @$.first_line, $1, @1.first_line);
412 static const unsigned int input[] =
414 /* Exericise the discarding of stack top and input until `error'
416 'x', 'x', 'x', 'x', 'x', 'x', ';',
418 /* Load the stack and provoke an error that cannot be caught by
419 the grammar, to check that the stack is cleared. */
424 static unsigned int counter = 0;
426 if (counter < (sizeof(input) / sizeof (input[0])))
428 yylval.ival = counter;
429 /* As in BASIC, line numbers go from 10 to 10. */
430 yylloc.first_line = 10 * counter;
431 printf ("sending: '%c' (value = %d, line %d)\n",
432 input[counter], yylval.ival, yylloc.first_line);
433 return (int) input[counter++];
437 printf ("sending: EOF\n");
443 yyerror (const char *msg)
445 fprintf (stdout, "%d: %s\n", yylloc.first_line, msg);
451 yydebug = !!getenv ("YYDEBUG");
454 fprintf (stdout, "Parsing FAILED.\n");
457 fprintf (stdout, "Successful parse.\n");
462 AT_CHECK([bison -o input.c input.y])
464 AT_PARSER_CHECK([./input], 1,
465 [[sending: 'x' (value = 0, line 0)
467 sending: 'x' (value = 1, line 10)
468 thing(1@10): 'x'(1@10)
469 sending: 'x' (value = 2, line 20)
470 thing(2@20): 'x'(2@20)
471 sending: 'x' (value = 3, line 30)
472 30: syntax error, unexpected 'x', expecting ';'
473 Freeing nterm thing (2@20)
474 Freeing nterm thing (1@10)
475 Freeing nterm thing (0@0)
476 Freeing token 'x' (3@30)
477 sending: 'x' (value = 4, line 40)
478 Freeing token 'x' (4@40)
479 sending: 'x' (value = 5, line 50)
480 Freeing token 'x' (5@50)
481 sending: ';' (value = 6, line 60)
482 line(-1@50): error(@50) ';'
483 sending: 'x' (value = 7, line 70)
484 thing(7@70): 'x'(7@70)
485 sending: 'x' (value = 8, line 80)
486 thing(8@80): 'x'(8@80)
487 sending: ';' (value = 9, line 90)
488 line(7@70): thing(7@70) thing(8@80) ';'
489 sending: 'x' (value = 10, line 100)
490 thing(10@100): 'x'(10@100)
491 sending: ';' (value = 11, line 110)
492 line(10@100): thing(10@100) ';'
493 sending: 'y' (value = 12, line 120)
494 120: syntax error, unexpected $undefined, expecting $end or 'x'
496 Freeing nterm line (10@100)
497 Freeing nterm line (7@70)
498 Freeing nterm line (-1@50)
506 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS])
507 # ------------------------------------------------
509 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
510 [_AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], [$1])
514 AT_CHECK_PRINTER_AND_DESTRUCTOR()
515 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])