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 ## ---------------- ##
87 ## ---------------- ##
89 AT_SETUP([Exotic Dollars])
91 AT_DATA_GRAMMAR([[input.y]],
95 static void yyerror (const char *msg);
96 static int yylex (void);
98 # define YYERROR_VERBOSE 1
106 %type <val> a_1 a_2 a_5
107 sum_of_the_five_previous_values
110 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
111 sum_of_the_five_previous_values
120 sum_of_the_five_previous_values:
122 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
134 yyerror (const char *msg)
136 fprintf (stderr, "%s\n", msg);
146 AT_CHECK([bison -d -v -o input.c input.y])
148 AT_PARSER_CHECK([./input], 0,
156 ## -------------------------- ##
157 ## Printers and Destructors. ##
158 ## -------------------------- ##
160 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, BISON-DIRECTIVE)
161 # -------------------------------------------------------------
162 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
163 [m4_if([$1$2$3], $[1]$[2]$[3], [],
164 [m4_fatal([$0: Invalid arguments: $@])])dnl
166 AT_SETUP([Printers and Destructors: $4])
168 # Make sure complex $n work.
170 AT_DATA_GRAMMAR([[input.y]],
177 static int yylex (void);
178 static void yyerror (const char *msg);
188 %type <ival> 'x' thing line input
190 %printer { fprintf (yyout, "%d@%d", $$, @$.first_line); }
194 { fprintf (stdout, "Freeing nterm input (%d@%d)\n", $$, @$.first_line); }
198 { fprintf (stdout, "Freeing nterm line (%d@%d)\n", $$, @$.first_line); }
202 { fprintf (stdout, "Freeing nterm thing (%d@%d)\n", $$, @$.first_line); }
206 { fprintf (stdout, "Freeing token 'x' (%d@%d)\n", $$, @$.first_line); }
214 printf ("input(%d@%d): /* Nothing */';'\n", $$, @$.first_line);
216 | line input /* Right recursive to load the stack so that popping at
217 EOF can be exercised. */
220 printf ("input(%d@%d): line(%d@%d) input(%d@%d)';'\n",
221 $$, @$.first_line, $1, @1.first_line, $2, @2.first_line);
226 thing thing thing ';'
229 printf ("line(%d@%d): thing(%d@%d) thing(%d@%d) thing(%d@%d) ';'\n",
230 $$, @$.first_line, $1, @1.first_line, $2, @2.first_line,
236 printf ("line(%d@%d): thing(%d@%d) thing(%d@%d) ';'\n",
237 $$, @$.first_line, $1, @1.first_line, $2, @2.first_line);
242 printf ("line(%d@%d): thing(%d@%d) ';'\n",
243 $$, @$.first_line, $1, @1.first_line);
248 printf ("line(%d@%d): error(@%d) ';'\n",
249 $$, @$.first_line, @1.first_line);
257 printf ("thing(%d@%d): 'x'(%d@%d)\n",
258 $$, @$.first_line, $1, @1.first_line);
265 static const unsigned int input[] =
267 /* Exericise the discarding of stack top and input until `error'
269 'x', 'x', 'x', 'x', 'x', 'x', ';',
271 /* Load the stack and provoke an error that cannot be caught by
272 the grammar, to check that the stack is cleared. */
277 static unsigned int counter = 0;
279 if (counter < (sizeof(input) / sizeof (input[0])))
281 yylval.ival = counter;
282 /* As in BASIC, line numbers go from 10 to 10. */
283 yylloc.first_line = 10 * counter;
284 printf ("sending: '%c' (value = %d, line %d)\n",
285 input[counter], yylval.ival, yylloc.first_line);
286 return (int) input[counter++];
290 printf ("sending: EOF\n");
296 yyerror (const char *msg)
298 fprintf (stdout, "%d: %s\n", yylloc.first_line, msg);
304 yydebug = !!getenv ("YYDEBUG");
307 fprintf (stdout, "Parsing FAILED.\n");
310 fprintf (stdout, "Successful parse.\n");
315 AT_CHECK([bison -o input.c input.y])
317 AT_PARSER_CHECK([./input], 1,
318 [[sending: 'x' (value = 0, line 0)
320 sending: 'x' (value = 1, line 10)
321 thing(1@10): 'x'(1@10)
322 sending: 'x' (value = 2, line 20)
323 thing(2@20): 'x'(2@20)
324 sending: 'x' (value = 3, line 30)
325 30: syntax error, unexpected 'x', expecting ';'
326 Freeing nterm thing (2@20)
327 Freeing nterm thing (1@10)
328 Freeing nterm thing (0@0)
329 Freeing token 'x' (3@30)
330 sending: 'x' (value = 4, line 40)
331 Freeing token 'x' (4@40)
332 sending: 'x' (value = 5, line 50)
333 Freeing token 'x' (5@50)
334 sending: ';' (value = 6, line 60)
335 line(-1@50): error(@50) ';'
336 sending: 'x' (value = 7, line 70)
337 thing(7@70): 'x'(7@70)
338 sending: 'x' (value = 8, line 80)
339 thing(8@80): 'x'(8@80)
340 sending: ';' (value = 9, line 90)
341 line(7@70): thing(7@70) thing(8@80) ';'
342 sending: 'x' (value = 10, line 100)
343 thing(10@100): 'x'(10@100)
344 sending: ';' (value = 11, line 110)
345 line(10@100): thing(10@100) ';'
346 sending: 'y' (value = 12, line 120)
347 120: syntax error, unexpected $undefined, expecting $end or 'x'
349 Freeing nterm line (10@100)
350 Freeing nterm line (7@70)
351 Freeing nterm line (-1@50)
359 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS])
360 # ------------------------------------------------
362 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
363 [_AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], [$1])
367 AT_CHECK_PRINTER_AND_DESTRUCTOR()
368 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])