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
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 input.y -d -v -o input.c])
77 AT_PARSER_CHECK([./input], 0,
85 ## ---------------- ##
87 ## ---------------- ##
89 AT_SETUP([Exotic Dollars])
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_4 a_5
107 sum_of_the_five_previous_values
110 exp: a_1 a_2 { $<val>$ = 3; } a_4 a_5 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 input.y -d -v -o input.c])
148 AT_PARSER_CHECK([./input], 0,
156 ## -------------------------- ##
157 ## Printers and Destructors. ##
158 ## -------------------------- ##
160 AT_SETUP([Printers and Destructors])
162 # Make sure complex $n work.
170 #define YYERROR_VERBOSE 1
178 %type <ival> 'x' thing line input
180 %printer { fprintf (yyout, "%d from %d", $$, @$.first_line); }
185 fprintf (stdout, "Freeing ");
186 /* FIXME: Ouch: INTERNAL DETAILS EXPOSED HERE. */
187 /* Cannot use $$ which is the union member, not the union itself. */
188 yysymprint (stdout, yytype, yyvalue, @$);
189 fprintf (stdout, "\n");
194 static int yylex (void);
195 static void yyerror (const char *msg);
204 printf ("input(%d): /* Nothing */';'\n", $$);
206 | line input /* Right recursive to load the stack so that popping at
207 EOF can be exercised. */
210 printf ("input(%d): line(%d) input(%d)';'\n", $$, $1, $2);
215 thing thing thing ';'
218 printf ("line(%d): thing(%d) thing(%d) thing(%d) ';'\n", $$, $1, $2, $3);
223 printf ("line(%d): thing(%d) thing(%d) ';'\n", $$, $1, $2);
228 printf ("line(%d): thing(%d) ';'\n", $$, $1);
233 printf ("line(%d): error ';'\n", $$);
241 printf ("thing(%d): 'x'(%d)\n", $$, $1);
248 static const unsigned int input[] =
250 /* Exericise the discarding of stack top and input until `error'
252 'x', 'x', 'x', 'x', 'x', 'x', ';',
254 /* Load the stack and provoke an error that cannot be caught by
255 the grammar, to check that the stack is cleared. */
260 static unsigned int counter = 0;
262 if (counter < (sizeof(input) / sizeof (input[0])))
264 yylval.ival = counter;
265 /* As in BASIC, line numbers go from 10 to 10. */
266 yylloc.first_line = 10 * counter;
267 printf ("sending: '%c' (value = %d, line %d)\n",
268 input[counter], yylval.ival, yylloc.first_line);
269 return (int) input[counter++];
273 printf ("sending: EOF\n");
279 yyerror (const char *msg)
281 fprintf (stdout, "%d: %s\n", yylloc.first_line, msg);
287 yydebug = !!getenv ("YYDEBUG");
290 fprintf (stdout, "Parsing FAILED.\n");
293 fprintf (stdout, "Successful parse.\n");
298 AT_CHECK([bison input.y --location -d -v -o input.c])
300 AT_PARSER_CHECK([./input], 1,
301 [[sending: 'x' (value = 0, line 0)
303 sending: 'x' (value = 1, line 10)
305 sending: 'x' (value = 2, line 20)
307 sending: 'x' (value = 3, line 30)
308 30: parse error, unexpected 'x', expecting ';'
309 Freeing nterm thing (2 from 20)
310 Freeing nterm thing (1 from 10)
311 Freeing nterm thing (0 from 0)
312 Freeing token 'x' (3 from 30)
313 sending: 'x' (value = 4, line 40)
314 Freeing token 'x' (4 from 40)
315 sending: 'x' (value = 5, line 50)
316 Freeing token 'x' (5 from 50)
317 sending: ';' (value = 6, line 60)
319 sending: 'x' (value = 7, line 70)
321 sending: 'x' (value = 8, line 80)
323 sending: ';' (value = 9, line 90)
324 line(7): thing(7) thing(8) ';'
325 sending: 'x' (value = 10, line 100)
327 sending: ';' (value = 11, line 110)
328 line(10): thing(10) ';'
329 sending: 'y' (value = 12, line 120)
330 120: parse error, unexpected $undefined, expecting $end or 'x'
332 Freeing nterm line (10 from 100)
333 Freeing nterm line (7 from 70)
334 Freeing nterm line (-1 from 50)