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])
76 AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
77 AT_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])
147 AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
148 AT_CHECK([./input], 0,
160 AT_SETUP([Destructors])
162 # Make sure complex $n work.
170 #define YYERROR_VERBOSE 1
172 #define YYPRINT yyprint
179 %type <ival> 'x' thing line input
180 %destructor { printf ("Freeing input %d\n", $$); } input
181 %destructor { printf ("Freeing line %d\n", $$); } line
182 %destructor { printf ("Freeing thing %d\n", $$); } thing
183 %destructor { printf ("Freeing 'x' %d\n", $$); } 'x'
186 static int yylex (void);
187 static void yyerror (const char *msg);
188 static void yyprint (FILE *out, int num, YYSTYPE val);
197 printf ("input(%d): /* Nothing */';'\n", $$);
199 | line input /* Right recursive to load the stack so that popping at
200 EOF can be exercised. */
203 printf ("input(%d): line(%d) input(%d)';'\n", $$, $1, $2);
208 thing thing thing ';'
211 printf ("line(%d): thing(%d) thing(%d) thing(%d) ';'\n", $$, $1, $2, $3);
216 printf ("line(%d): thing(%d) thing(%d) ';'\n", $$, $1, $2);
221 printf ("line(%d): thing(%d) ';'\n", $$, $1);
226 printf ("line(%d): error ';'\n", $$);
234 printf ("thing(%d): 'x'(%d)\n", $$, $1);
241 static const unsigned int input[] =
243 /* Exericise the discarding of stack top and input until `error'
245 'x', 'x', 'x', 'x', 'x', 'x', ';',
247 /* Load the stack and provoke an error that cannot be caught be
248 the grammar, and check that the stack is cleared. */
253 static int counter = 0;
255 if (counter < (sizeof(input) / sizeof (input[0])))
257 yylval.ival = counter;
258 printf ("sending: '%c'(%d)\n", input[counter], counter);
259 return input[counter++];
263 printf ("sending: EOF\n");
269 yyerror (const char *msg)
271 fprintf (stdout, "%s\n", msg);
275 yyprint (FILE *out, int num, YYSTYPE val)
277 fprintf (out, " = %d", val.ival);
283 yydebug = !!getenv ("YYDEBUG");
286 fprintf (stdout, "Parsing FAILED.\n");
289 fprintf (stdout, "Successful parse.\n");
294 AT_CHECK([bison input.y -d -v -o input.c])
295 AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
296 AT_CHECK([./input], 1,
304 parse error, unexpected 'x', expecting ';'
320 line(7): thing(7) thing(8) ';'
324 line(10): thing(10) ';'
326 parse error, unexpected $undefined., expecting $ or error or 'x'