]> git.saurik.com Git - bison.git/blame - tests/actions.at
We spend a lot of time in quotearg, in particular when --verbose.
[bison.git] / tests / actions.at
CommitLineData
82c035a8
AD
1# Executing Actions. -*- Autotest -*-
2# Copyright 2001 Free Software Foundation, Inc.
3
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)
7# any later version.
8
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.
13
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
17# 02111-1307, USA.
18
19AT_BANNER([[User Actions.]])
20
21## ------------------ ##
22## Mid-rule actions. ##
23## ------------------ ##
24
25AT_SETUP([Mid-rule actions])
26
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
30# action.
31
32AT_DATA([[input.y]],
33[[%{
d99361e6
AD
34# include <stdio.h>
35# include <stdlib.h>
36 static void yyerror (const char *msg);
37 static int yylex (void);
38# define YYDEBUG 1
39# define YYERROR_VERBOSE 1
82c035a8
AD
40%}
41%%
931394cb
AD
42exp: { 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'); }
52 { putchar ('\n'); }
82c035a8
AD
53 ;
54%%
55static int
56yylex (void)
57{
58 static const char *input = "123456789";
59 return *input++;
60}
61
62static void
63yyerror (const char *msg)
64{
65 fprintf (stderr, "%s\n", msg);
66}
67
68int
69main (void)
70{
71 return yyparse ();
72}
73]])
74
75AT_CHECK([bison input.y -d -v -o input.c])
76AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
30d2f3d5 77AT_CHECK([./input], 0,
931394cb 78[[0123456789
82c035a8
AD
79]])
80
81AT_CLEANUP
75d1fe16
AD
82
83
84
85## ---------------- ##
86## Exotic Dollars. ##
87## ---------------- ##
88
89AT_SETUP([Exotic Dollars])
90
75d1fe16
AD
91AT_DATA([[input.y]],
92[[%{
93# include <stdio.h>
94# include <stdlib.h>
95 static void yyerror (const char *msg);
96 static int yylex (void);
97# define YYDEBUG 1
98# define YYERROR_VERBOSE 1
99%}
100
101%union
102{
103 int val;
104};
105
106%type <val> a_1 a_2 a_4 a_5
107 sum_of_the_five_previous_values
108
109%%
110exp: a_1 a_2 { $<val>$ = 3; } a_4 a_5 sum_of_the_five_previous_values
111 {
112 printf ("%d\n", $6);
113 }
114;
115a_1: { $$ = 1; };
116a_2: { $$ = 2; };
117a_4: { $$ = 4; };
118a_5: { $$ = 5; };
119
120sum_of_the_five_previous_values:
121 {
122 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
123 }
124;
125
126%%
127static int
128yylex (void)
129{
130 return EOF;
131}
132
133static void
134yyerror (const char *msg)
135{
136 fprintf (stderr, "%s\n", msg);
137}
138
139int
140main (void)
141{
142 return yyparse ();
143}
144]])
145
146AT_CHECK([bison input.y -d -v -o input.c])
147AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
148AT_CHECK([./input], 0,
149[[15
150]])
151
152AT_CLEANUP
9280d3ef
AD
153
154
155
e776192e
AD
156## -------------------------- ##
157## Printers and Destructors. ##
158## -------------------------- ##
9280d3ef 159
e776192e 160AT_SETUP([Printers and Destructors])
9280d3ef
AD
161
162# Make sure complex $n work.
163
164AT_DATA([[input.y]],
165[[%{
166#include <stdio.h>
167#include <stdlib.h>
168#include <assert.h>
169
170#define YYERROR_VERBOSE 1
171#define YYDEBUG 1
9280d3ef 172%}
5719c109 173%verbose
9280d3ef
AD
174%union
175{
176 int ival;
177}
5719c109 178%type <ival> 'x' thing line input
e3170060
AD
179
180%printer { fprintf (yyout, "%d from %d", $$, @$.first_line); }
181 input line thing 'x'
182
183%destructor
184 {
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");
190 }
191 input line thing 'x'
9280d3ef 192
5719c109
AD
193%{
194static int yylex (void);
195static void yyerror (const char *msg);
5719c109
AD
196%}
197
198
9280d3ef
AD
199%%
200input:
201 /* Nothing. */
5719c109
AD
202 {
203 $$ = 0;
204 printf ("input(%d): /* Nothing */';'\n", $$);
205 }
206| line input /* Right recursive to load the stack so that popping at
207 EOF can be exercised. */
208 {
209 $$ = 2;
210 printf ("input(%d): line(%d) input(%d)';'\n", $$, $1, $2);
211 }
9280d3ef
AD
212;
213
214line:
215 thing thing thing ';'
5719c109
AD
216 {
217 $$ = $1;
218 printf ("line(%d): thing(%d) thing(%d) thing(%d) ';'\n", $$, $1, $2, $3);
219 }
9280d3ef 220| thing thing ';'
5719c109
AD
221 {
222 $$ = $1;
223 printf ("line(%d): thing(%d) thing(%d) ';'\n", $$, $1, $2);
224 }
9280d3ef 225| thing ';'
5719c109
AD
226 {
227 $$ = $1;
228 printf ("line(%d): thing(%d) ';'\n", $$, $1);
229 }
9280d3ef 230| error ';'
5719c109
AD
231 {
232 $$ = -1;
233 printf ("line(%d): error ';'\n", $$);
234 }
9280d3ef
AD
235;
236
237thing:
5719c109
AD
238 'x'
239 {
240 $$ = $1;
241 printf ("thing(%d): 'x'(%d)\n", $$, $1);
242 }
9280d3ef
AD
243;
244%%
245static int
246yylex (void)
247{
5719c109 248 static const unsigned int input[] =
9280d3ef 249 {
5719c109
AD
250 /* Exericise the discarding of stack top and input until `error'
251 can be reduced. */
9280d3ef 252 'x', 'x', 'x', 'x', 'x', 'x', ';',
5719c109 253
5a08f1ce
AD
254 /* Load the stack and provoke an error that cannot be caught by
255 the grammar, to check that the stack is cleared. */
9280d3ef
AD
256 'x', 'x', ';',
257 'x', ';',
5719c109 258 'y'
9280d3ef 259 };
5a08f1ce 260 static unsigned int counter = 0;
9280d3ef
AD
261
262 if (counter < (sizeof(input) / sizeof (input[0])))
263 {
264 yylval.ival = counter;
93b68a0e
AD
265 /* As in BASIC, line numbers go from 10 to 10. */
266 yylloc.first_line = 10 * counter;
5a08f1ce
AD
267 printf ("sending: '%c' (value = %d, line %d)\n",
268 input[counter], yylval.ival, yylloc.first_line);
269 return (int) input[counter++];
9280d3ef
AD
270 }
271 else
5719c109
AD
272 {
273 printf ("sending: EOF\n");
274 return EOF;
275 }
9280d3ef
AD
276}
277
278static void
279yyerror (const char *msg)
280{
93b68a0e 281 fprintf (stdout, "%d: %s\n", yylloc.first_line, msg);
9280d3ef
AD
282}
283
9280d3ef
AD
284int
285main (void)
286{
287 yydebug = !!getenv ("YYDEBUG");
288 if (yyparse ())
289 {
290 fprintf (stdout, "Parsing FAILED.\n");
291 exit (1);
292 }
293 fprintf (stdout, "Successful parse.\n");
294 return 0;
295}
296]])
297
93b68a0e 298AT_CHECK([bison input.y --location -d -v -o input.c])
9280d3ef 299AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
5719c109 300AT_CHECK([./input], 1,
5a08f1ce 301[[sending: 'x' (value = 0, line 0)
5719c109 302thing(0): 'x'(0)
5a08f1ce 303sending: 'x' (value = 1, line 10)
5719c109 304thing(1): 'x'(1)
5a08f1ce 305sending: 'x' (value = 2, line 20)
5719c109 306thing(2): 'x'(2)
5a08f1ce 307sending: 'x' (value = 3, line 30)
93b68a0e 30830: parse error, unexpected 'x', expecting ';'
e3170060
AD
309Freeing nterm thing (2 from 20)
310Freeing nterm thing (1 from 10)
311Freeing nterm thing (0 from 0)
312Freeing token 'x' (3 from 30)
5a08f1ce 313sending: 'x' (value = 4, line 40)
e3170060 314Freeing token 'x' (4 from 40)
5a08f1ce 315sending: 'x' (value = 5, line 50)
e3170060 316Freeing token 'x' (5 from 50)
5a08f1ce 317sending: ';' (value = 6, line 60)
5719c109 318line(-1): error ';'
5a08f1ce 319sending: 'x' (value = 7, line 70)
5719c109 320thing(7): 'x'(7)
5a08f1ce 321sending: 'x' (value = 8, line 80)
5719c109 322thing(8): 'x'(8)
5a08f1ce 323sending: ';' (value = 9, line 90)
5719c109 324line(7): thing(7) thing(8) ';'
5a08f1ce 325sending: 'x' (value = 10, line 100)
5719c109 326thing(10): 'x'(10)
5a08f1ce 327sending: ';' (value = 11, line 110)
5719c109 328line(10): thing(10) ';'
5a08f1ce 329sending: 'y' (value = 12, line 120)
93b68a0e 330120: parse error, unexpected $undefined., expecting $ or error or 'x'
5719c109 331sending: EOF
e3170060
AD
332Freeing nterm line (10 from 100)
333Freeing nterm line (7 from 70)
334Freeing nterm line (-1 from 50)
5719c109 335Parsing FAILED.
9280d3ef
AD
336]])
337
338AT_CLEANUP