]> git.saurik.com Git - bison.git/blame - tests/actions.at
(Torturing the Scanner): Surround the backslash-newline tests with
[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
9501dc6e 32AT_DATA_GRAMMAR([[input.y]],
82c035a8 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
b56471a6 75AT_CHECK([bison -d -v -o input.c input.y])
1154cced
AD
76AT_COMPILE([input])
77AT_PARSER_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
9501dc6e 91AT_DATA_GRAMMAR([[input.y]],
75d1fe16
AD
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
0ff67d71 106%type <val> a_1 a_2 a_5
75d1fe16
AD
107 sum_of_the_five_previous_values
108
109%%
0ff67d71
PE
110exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
111 sum_of_the_five_previous_values
75d1fe16
AD
112 {
113 printf ("%d\n", $6);
114 }
115;
116a_1: { $$ = 1; };
117a_2: { $$ = 2; };
75d1fe16
AD
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
b56471a6 146AT_CHECK([bison -d -v -o input.c input.y])
1154cced
AD
147AT_COMPILE([input])
148AT_PARSER_CHECK([./input], 0,
75d1fe16
AD
149[[15
150]])
151
152AT_CLEANUP
9280d3ef
AD
153
154
155
e776192e
AD
156## -------------------------- ##
157## Printers and Destructors. ##
158## -------------------------- ##
9280d3ef 159
3df37415
AD
160# _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, BISON-DIRECTIVE)
161# -------------------------------------------------------------
162m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
163[m4_if([$1$2$3], $[1]$[2]$[3], [],
164 [m4_fatal([$0: Invalid arguments: $@])])dnl
165
166AT_SETUP([Printers and Destructors: $4])
9280d3ef
AD
167
168# Make sure complex $n work.
169
9501dc6e 170AT_DATA_GRAMMAR([[input.y]],
3df37415
AD
171[[$4
172%{
9280d3ef
AD
173#include <stdio.h>
174#include <stdlib.h>
175#include <assert.h>
176
7bd6c77e
AD
177static int yylex (void);
178static void yyerror (const char *msg);
9280d3ef 179%}
7bd6c77e
AD
180%error-verbose
181%debug
5719c109 182%verbose
7bd6c77e 183%locations
9280d3ef
AD
184%union
185{
186 int ival;
187}
5719c109 188%type <ival> 'x' thing line input
e3170060 189
283f1e64 190%printer { fprintf (yyout, "%d@%d", $$, @$.first_line); }
e3170060
AD
191 input line thing 'x'
192
193%destructor
283f1e64 194 { fprintf (stdout, "Freeing nterm input (%d@%d)\n", $$, @$.first_line); }
7bd6c77e 195 input
5719c109 196
7bd6c77e 197%destructor
283f1e64 198 { fprintf (stdout, "Freeing nterm line (%d@%d)\n", $$, @$.first_line); }
7bd6c77e
AD
199 line
200
201%destructor
283f1e64 202 { fprintf (stdout, "Freeing nterm thing (%d@%d)\n", $$, @$.first_line); }
7bd6c77e
AD
203 thing
204
205%destructor
283f1e64 206 { fprintf (stdout, "Freeing token 'x' (%d@%d)\n", $$, @$.first_line); }
7bd6c77e 207 'x'
5719c109 208
9280d3ef
AD
209%%
210input:
211 /* Nothing. */
5719c109
AD
212 {
213 $$ = 0;
283f1e64 214 printf ("input(%d@%d): /* Nothing */';'\n", $$, @$.first_line);
5719c109
AD
215 }
216| line input /* Right recursive to load the stack so that popping at
217 EOF can be exercised. */
218 {
219 $$ = 2;
283f1e64
AD
220 printf ("input(%d@%d): line(%d@%d) input(%d@%d)';'\n",
221 $$, @$.first_line, $1, @1.first_line, $2, @2.first_line);
5719c109 222 }
9280d3ef
AD
223;
224
225line:
226 thing thing thing ';'
5719c109
AD
227 {
228 $$ = $1;
283f1e64
AD
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,
231 $3, @3.first_line);
5719c109 232 }
9280d3ef 233| thing thing ';'
5719c109
AD
234 {
235 $$ = $1;
283f1e64
AD
236 printf ("line(%d@%d): thing(%d@%d) thing(%d@%d) ';'\n",
237 $$, @$.first_line, $1, @1.first_line, $2, @2.first_line);
5719c109 238 }
9280d3ef 239| thing ';'
5719c109
AD
240 {
241 $$ = $1;
283f1e64
AD
242 printf ("line(%d@%d): thing(%d@%d) ';'\n",
243 $$, @$.first_line, $1, @1.first_line);
5719c109 244 }
9280d3ef 245| error ';'
5719c109
AD
246 {
247 $$ = -1;
283f1e64
AD
248 printf ("line(%d@%d): error(@%d) ';'\n",
249 $$, @$.first_line, @1.first_line);
5719c109 250 }
9280d3ef
AD
251;
252
253thing:
5719c109
AD
254 'x'
255 {
256 $$ = $1;
283f1e64
AD
257 printf ("thing(%d@%d): 'x'(%d@%d)\n",
258 $$, @$.first_line, $1, @1.first_line);
5719c109 259 }
9280d3ef
AD
260;
261%%
262static int
263yylex (void)
264{
5719c109 265 static const unsigned int input[] =
9280d3ef 266 {
5719c109
AD
267 /* Exericise the discarding of stack top and input until `error'
268 can be reduced. */
9280d3ef 269 'x', 'x', 'x', 'x', 'x', 'x', ';',
5719c109 270
5a08f1ce
AD
271 /* Load the stack and provoke an error that cannot be caught by
272 the grammar, to check that the stack is cleared. */
9280d3ef
AD
273 'x', 'x', ';',
274 'x', ';',
5719c109 275 'y'
9280d3ef 276 };
5a08f1ce 277 static unsigned int counter = 0;
9280d3ef
AD
278
279 if (counter < (sizeof(input) / sizeof (input[0])))
280 {
281 yylval.ival = counter;
93b68a0e
AD
282 /* As in BASIC, line numbers go from 10 to 10. */
283 yylloc.first_line = 10 * counter;
5a08f1ce
AD
284 printf ("sending: '%c' (value = %d, line %d)\n",
285 input[counter], yylval.ival, yylloc.first_line);
286 return (int) input[counter++];
9280d3ef
AD
287 }
288 else
5719c109
AD
289 {
290 printf ("sending: EOF\n");
291 return EOF;
292 }
9280d3ef
AD
293}
294
295static void
296yyerror (const char *msg)
297{
93b68a0e 298 fprintf (stdout, "%d: %s\n", yylloc.first_line, msg);
9280d3ef
AD
299}
300
9280d3ef
AD
301int
302main (void)
303{
304 yydebug = !!getenv ("YYDEBUG");
305 if (yyparse ())
306 {
307 fprintf (stdout, "Parsing FAILED.\n");
308 exit (1);
309 }
310 fprintf (stdout, "Successful parse.\n");
311 return 0;
312}
313]])
314
7bd6c77e 315AT_CHECK([bison -o input.c input.y])
1154cced
AD
316AT_COMPILE([input])
317AT_PARSER_CHECK([./input], 1,
5a08f1ce 318[[sending: 'x' (value = 0, line 0)
283f1e64 319thing(0@0): 'x'(0@0)
5a08f1ce 320sending: 'x' (value = 1, line 10)
283f1e64 321thing(1@10): 'x'(1@10)
5a08f1ce 322sending: 'x' (value = 2, line 20)
283f1e64 323thing(2@20): 'x'(2@20)
5a08f1ce 324sending: 'x' (value = 3, line 30)
93b68a0e 32530: parse error, unexpected 'x', expecting ';'
283f1e64
AD
326Freeing nterm thing (2@20)
327Freeing nterm thing (1@10)
328Freeing nterm thing (0@0)
329Freeing token 'x' (3@30)
5a08f1ce 330sending: 'x' (value = 4, line 40)
283f1e64 331Freeing token 'x' (4@40)
5a08f1ce 332sending: 'x' (value = 5, line 50)
283f1e64 333Freeing token 'x' (5@50)
5a08f1ce 334sending: ';' (value = 6, line 60)
283f1e64 335line(-1@50): error(@50) ';'
5a08f1ce 336sending: 'x' (value = 7, line 70)
283f1e64 337thing(7@70): 'x'(7@70)
5a08f1ce 338sending: 'x' (value = 8, line 80)
283f1e64 339thing(8@80): 'x'(8@80)
5a08f1ce 340sending: ';' (value = 9, line 90)
283f1e64 341line(7@70): thing(7@70) thing(8@80) ';'
5a08f1ce 342sending: 'x' (value = 10, line 100)
283f1e64 343thing(10@100): 'x'(10@100)
5a08f1ce 344sending: ';' (value = 11, line 110)
283f1e64 345line(10@100): thing(10@100) ';'
5a08f1ce 346sending: 'y' (value = 12, line 120)
217598da 347120: parse error, unexpected $undefined, expecting $end or 'x'
5719c109 348sending: EOF
283f1e64
AD
349Freeing nterm line (10@100)
350Freeing nterm line (7@70)
351Freeing nterm line (-1@50)
5719c109 352Parsing FAILED.
9280d3ef
AD
353]])
354
355AT_CLEANUP
3df37415
AD
356])
357
358
359# AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS])
360# ------------------------------------------------
361# Produce `calc.y'.
362m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
363[_AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], [$1])
364])
365
366
367AT_CHECK_PRINTER_AND_DESTRUCTOR()
368AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])