]> git.saurik.com Git - bison.git/blame - tests/actions.at
* data/m4sugar/m4sugar.m4 (m4_map): Recognize when the list of
[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
156## ------------- ##
157## Destructors. ##
158## ------------- ##
159
160AT_SETUP([Destructors])
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
172/* #define YYPRINT yyprint */
173
174static int yylex (void);
175static void yyerror (const char *msg);
176static void yyprint (FILE *out, int toknum, int tokval);
177%}
178
179%union
180{
181 int ival;
182}
183%type <ival> thing 'x'
184%destructor { printf ("Freeing thing %d\n", $$); } thing
185%destructor { printf ("Freeing 'x' %d\n", $$); } 'x'
186
187%%
188input:
189 /* Nothing. */
190| input line
191;
192
193line:
194 thing thing thing ';'
195 { printf ("input: thing(%d) thing(%d) thing(%d) ';'\n", $1, $2, $3); }
196| thing thing ';'
197 { printf ("input: thing(%d) thing(%d) ';'\n", $1, $2); }
198| thing ';'
199 { printf ("input: thing(%d) ';'\n", $1); }
200| error ';'
201 { printf ("input: error ';'\n"); }
202;
203
204thing:
205 'x' { printf ("thing: 'x' (%d)\n", $1); $$ = $1; }
206;
207%%
208static int
209yylex (void)
210{
211 static const int input[] =
212 {
213 'x', 'x', 'x', 'x', 'x', 'x', ';',
214 'x', 'x', ';',
215 'x', ';',
216 'x', 'y', ';'
217 };
218 static int counter = 0;
219
220 if (counter < (sizeof(input) / sizeof (input[0])))
221 {
222 yylval.ival = counter;
223 return input[counter++];
224 }
225 else
226 return EOF;
227}
228
229static void
230yyerror (const char *msg)
231{
232 fprintf (stdout, "%s\n", msg);
233}
234
235static void
236yyprint (FILE *out, int toknum, int tokval)
237{
238 if (0 < toknum && toknum < 256)
239 fprintf (out, " = %d", tokval);
240}
241
242int
243main (void)
244{
245 yydebug = !!getenv ("YYDEBUG");
246 if (yyparse ())
247 {
248 fprintf (stdout, "Parsing FAILED.\n");
249 exit (1);
250 }
251 fprintf (stdout, "Successful parse.\n");
252 return 0;
253}
254]])
255
256AT_CHECK([bison input.y -d -v -o input.c])
257AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
258AT_CHECK([./input], 0,
259[[thing: 'x' (0)
260thing: 'x' (1)
261thing: 'x' (2)
262parse error, unexpected 'x', expecting ';'
263Freeing thing 2
264Freeing thing 1
265Freeing thing 0
266Freeing 'x' 3
267Freeing 'x' 4
268Freeing 'x' 5
269input: error ';'
270thing: 'x' (7)
271thing: 'x' (8)
272input: thing(7) thing(8) ';'
273thing: 'x' (10)
274input: thing(10) ';'
275thing: 'x' (12)
276parse error, unexpected $undefined., expecting 'x' or ';'
277Freeing thing 12
278input: error ';'
279Successful parse.
280]])
281
282AT_CLEANUP