]> git.saurik.com Git - bison.git/blob - tests/actions.at
* doc/bison.texinfo (C++ Parser Interface): Use defcv to define
[bison.git] / tests / actions.at
1 # Executing Actions. -*- Autotest -*-
2 # Copyright (C) 2001, 2002, 2003, 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
17 # 02110-1301, USA.
18
19 AT_BANNER([[User Actions.]])
20
21 ## ------------------ ##
22 ## Mid-rule actions. ##
23 ## ------------------ ##
24
25 AT_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
32 AT_DATA_GRAMMAR([[input.y]],
33 [[%{
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
40 %}
41 %%
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'); }
52 { putchar ('\n'); }
53 ;
54 %%
55 static int
56 yylex (void)
57 {
58 static const char *input = "123456789";
59 return *input++;
60 }
61
62 static void
63 yyerror (const char *msg)
64 {
65 fprintf (stderr, "%s\n", msg);
66 }
67
68 int
69 main (void)
70 {
71 return yyparse ();
72 }
73 ]])
74
75 AT_CHECK([bison -d -v -o input.c input.y])
76 AT_COMPILE([input])
77 AT_PARSER_CHECK([./input], 0,
78 [[0123456789
79 ]])
80
81 AT_CLEANUP
82
83
84
85
86
87 ## ---------------- ##
88 ## Exotic Dollars. ##
89 ## ---------------- ##
90
91 AT_SETUP([Exotic Dollars])
92
93 AT_DATA_GRAMMAR([[input.y]],
94 [[%{
95 # include <stdio.h>
96 # include <stdlib.h>
97 static void yyerror (const char *msg);
98 static int yylex (void);
99 # define YYDEBUG 1
100 # define YYERROR_VERBOSE 1
101 %}
102
103 %union
104 {
105 int val;
106 };
107
108 %type <val> a_1 a_2 a_5
109 sum_of_the_five_previous_values
110
111 %%
112 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
113 sum_of_the_five_previous_values
114 {
115 printf ("%d\n", $6);
116 }
117 ;
118 a_1: { $$ = 1; };
119 a_2: { $$ = 2; };
120 a_5: { $$ = 5; };
121
122 sum_of_the_five_previous_values:
123 {
124 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
125 }
126 ;
127
128 %%
129 static int
130 yylex (void)
131 {
132 return EOF;
133 }
134
135 static void
136 yyerror (const char *msg)
137 {
138 fprintf (stderr, "%s\n", msg);
139 }
140
141 int
142 main (void)
143 {
144 return yyparse ();
145 }
146 ]])
147
148 AT_CHECK([bison -d -v -o input.c input.y])
149 AT_COMPILE([input])
150 AT_PARSER_CHECK([./input], 0,
151 [[15
152 ]])
153
154 AT_CLEANUP
155
156
157
158 ## -------------------------- ##
159 ## Printers and Destructors. ##
160 ## -------------------------- ##
161
162 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
163 # -----------------------------------------------------------------------------
164 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
165 [# Make sure complex $n work.
166 m4_if([$1$2$3], $[1]$[2]$[3], [],
167 [m4_fatal([$0: Invalid arguments: $@])])dnl
168
169 # Be sure to pass all the %directives to this macro to have correct
170 # helping macros. So don't put any directly in the Bison file.
171 AT_BISON_OPTION_PUSHDEFS([$5])
172 AT_DATA_GRAMMAR([[input.y]],
173 [[%{
174 #include <stdio.h>
175 #include <stdlib.h>
176 #include <assert.h>
177
178 #define YYINITDEPTH 10
179 #define YYMAXDEPTH 10
180 ]AT_LALR1_CC_IF(
181 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
182 [#define RANGE(Location) (Location).first_line, (Location).last_line])
183 [%}
184
185 $5]
186 m4_ifval([$6], [%union
187 {
188 int ival;
189 }])
190 [
191 %{
192 ]AT_LALR1_CC_IF([typedef yy::location YYLTYPE;
193 m4_ifval([$6], , [#define YYSTYPE int])])
194 [static int yylex (]AT_LEX_FORMALS[);
195 ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
196 [%}
197
198 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input])[
199
200 %printer
201 {
202 ]AT_LALR1_CC_IF([debug_stream () << $$;],
203 [fprintf (yyoutput, "%d", $$)])[;
204 }
205 input line thing 'x' 'y'
206
207 %destructor
208 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
209 input
210
211 %destructor
212 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
213 line
214
215 %destructor
216 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
217 thing
218
219 %destructor
220 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
221 'x'
222
223 %destructor
224 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
225 'y'
226
227 %%
228 /*
229 This grammar is made to exercise error recovery.
230 "Lines" starting with `(' support error recovery, with
231 ')' as synchronizing token. Lines starting with 'x' can never
232 be recovered from if in error.
233 */
234
235 input:
236 /* Nothing. */
237 {
238 $$ = 0;
239 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
240 }
241 | line input /* Right recursive to load the stack so that popping at
242 EOF can be exercised. */
243 {
244 $$ = 2;
245 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
246 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
247 }
248 ;
249
250 line:
251 thing thing thing ';'
252 {
253 $$ = $1;
254 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
255 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
256 $3, RANGE (@3), $4, RANGE (@4));
257 }
258 | '(' thing thing ')'
259 {
260 $$ = $1;
261 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
262 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
263 $3, RANGE (@3), $4, RANGE (@4));
264 }
265 | '(' thing ')'
266 {
267 $$ = $1;
268 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
269 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
270 }
271 | '(' error ')'
272 {
273 $$ = -1;
274 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
275 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
276 }
277 ;
278
279 thing:
280 'x'
281 {
282 $$ = $1;
283 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
284 $$, RANGE (@$), $1, RANGE (@1));
285 }
286 ;
287 %%
288 /* Alias to ARGV[1]. */
289 const char *yysource = 0;
290
291 static int
292 yylex (]AT_LEX_FORMALS[)
293 {
294 static unsigned int counter = 0;
295
296 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
297 /* As in BASIC, line numbers go from 10 to 10. */
298 ]AT_LALR1_CC_IF(
299 [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
300 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
301 ],
302 [ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
303 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
304 ])[
305
306 if (yysource[c])
307 printf ("sending: '%c'", yysource[c]);
308 else
309 printf ("sending: EOF");
310 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
311 return yysource[c];
312 }
313
314 ]AT_LALR1_CC_IF(
315 [/* A C++ error reporting function. */
316 void
317 yy::parser::error (const location& l, const std::string& m)
318 {
319 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
320 }
321
322 static bool yydebug;
323 int
324 yyparse ()
325 {
326 yy::parser parser;
327 parser.set_debug_level (yydebug);
328 return parser.parse ();
329 }
330 ],
331 [static void
332 yyerror (const char *msg)
333 {
334 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
335 }])[
336
337 int
338 main (int argc, const char *argv[])
339 {
340 yydebug = !!getenv ("YYDEBUG");
341 assert (argc == 2);
342 yysource = argv[1];
343 if (yyparse ())
344 {
345 printf ("Parsing FAILED.\n");
346 exit (1);
347 }
348 printf ("Successful parse.\n");
349 return 0;
350 }
351 ]])
352
353 AT_LALR1_CC_IF(
354 [AT_CHECK([bison -o input.cc input.y])
355 AT_COMPILE_CXX([input])],
356 [AT_CHECK([bison -o input.c input.y])
357 AT_COMPILE([input])])
358
359
360 # Check the location of "empty"
361 # -----------------------------
362 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
363 # an empty "line" nterm.
364 # FIXME: This location is not satisfying. Depend on the lookahead?
365 AT_PARSER_CHECK([./input '(x)'], 0,
366 [[sending: '(' (0@0-9)
367 sending: 'x' (1@10-19)
368 thing (1@10-19): 'x' (1@10-19)
369 sending: ')' (2@20-29)
370 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
371 sending: EOF (3@30-39)
372 input (0@29-29): /* Nothing */
373 input (2@0-29): line (0@0-29) input (0@29-29)
374 Successful parse.
375 ]])
376
377
378 # Check locations in error recovery
379 # ---------------------------------
380 # '(y)' is an error, but can be recovered from. But what's the location
381 # of the error itself ('y'), and of the resulting reduction ('(error)').
382 AT_PARSER_CHECK([./input '(y)'], 0,
383 [[sending: '(' (0@0-9)
384 sending: 'y' (1@10-19)
385 10-19: syntax error, unexpected 'y', expecting 'x'
386 Freeing token 'y' (1@10-19)
387 sending: ')' (2@20-29)
388 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
389 sending: EOF (3@30-39)
390 input (0@29-29): /* Nothing */
391 input (2@0-29): line (-1@0-29) input (0@29-29)
392 Successful parse.
393 ]])
394
395
396 # Syntax errors caught by the parser
397 # ----------------------------------
398 # Exercise the discarding of stack top and input until `error'
399 # can be reduced.
400 #
401 # '(', 'x', 'x', 'x', 'x', 'x', ')',
402 #
403 # Load the stack and provoke an error that cannot be caught by the
404 # grammar, to check that the stack is cleared. And make sure the
405 # lookahead is freed.
406 #
407 # '(', 'x', ')',
408 # '(', 'x', ')',
409 # 'y'
410 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
411 [[sending: '(' (0@0-9)
412 sending: 'x' (1@10-19)
413 thing (1@10-19): 'x' (1@10-19)
414 sending: 'x' (2@20-29)
415 thing (2@20-29): 'x' (2@20-29)
416 sending: 'x' (3@30-39)
417 30-39: syntax error, unexpected 'x', expecting ')'
418 Freeing nterm thing (2@20-29)
419 Freeing nterm thing (1@10-19)
420 Freeing token 'x' (3@30-39)
421 sending: 'x' (4@40-49)
422 Freeing token 'x' (4@40-49)
423 sending: 'x' (5@50-59)
424 Freeing token 'x' (5@50-59)
425 sending: ')' (6@60-69)
426 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
427 sending: '(' (7@70-79)
428 sending: 'x' (8@80-89)
429 thing (8@80-89): 'x' (8@80-89)
430 sending: ')' (9@90-99)
431 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
432 sending: '(' (10@100-109)
433 sending: 'x' (11@110-119)
434 thing (11@110-119): 'x' (11@110-119)
435 sending: ')' (12@120-129)
436 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
437 sending: 'y' (13@130-139)
438 input (0@129-129): /* Nothing */
439 input (2@100-129): line (10@100-129) input (0@129-129)
440 input (2@70-129): line (7@70-99) input (2@100-129)
441 input (2@0-129): line (-1@0-69) input (2@70-129)
442 130-139: syntax error, unexpected 'y', expecting $end
443 Freeing nterm input (2@0-129)
444 Freeing token 'y' (13@130-139)
445 Parsing FAILED.
446 ]])
447
448 # Check destruction upon stack overflow
449 # -------------------------------------
450 # Upon stack overflow, all symbols on the stack should be destroyed.
451 # Only check for yacc.c.
452 AT_YACC_IF([
453 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 1,
454 [[sending: '(' (0@0-9)
455 sending: 'x' (1@10-19)
456 thing (1@10-19): 'x' (1@10-19)
457 sending: ')' (2@20-29)
458 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
459 sending: '(' (3@30-39)
460 sending: 'x' (4@40-49)
461 thing (4@40-49): 'x' (4@40-49)
462 sending: ')' (5@50-59)
463 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
464 sending: '(' (6@60-69)
465 sending: 'x' (7@70-79)
466 thing (7@70-79): 'x' (7@70-79)
467 sending: ')' (8@80-89)
468 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
469 sending: '(' (9@90-99)
470 sending: 'x' (10@100-109)
471 thing (10@100-109): 'x' (10@100-109)
472 sending: ')' (11@110-119)
473 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
474 sending: '(' (12@120-129)
475 sending: 'x' (13@130-139)
476 thing (13@130-139): 'x' (13@130-139)
477 sending: ')' (14@140-149)
478 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
479 sending: '(' (15@150-159)
480 sending: 'x' (16@160-169)
481 thing (16@160-169): 'x' (16@160-169)
482 sending: ')' (17@170-179)
483 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
484 sending: '(' (18@180-189)
485 sending: 'x' (19@190-199)
486 thing (19@190-199): 'x' (19@190-199)
487 sending: ')' (20@200-209)
488 200-209: parser stack overflow
489 Freeing nterm thing (19@190-199)
490 Freeing nterm line (15@150-179)
491 Freeing nterm line (12@120-149)
492 Freeing nterm line (9@90-119)
493 Freeing nterm line (6@60-89)
494 Freeing nterm line (3@30-59)
495 Freeing nterm line (0@0-29)
496 Parsing FAILED.
497 ]])
498 ])
499
500 ])
501
502
503 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
504 # ---------------------------------------------------------------------------
505 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
506 [AT_SETUP([Printers and Destructors $2: $1])
507
508 $3
509 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
510 [%error-verbose
511 %debug
512 %verbose
513 %locations
514 $1], [$2])
515
516 AT_CLEANUP
517 ])
518
519
520 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
521 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
522
523 # These tests currently fail on a Debian GNU/Linux 3.0r2 x86 host,
524 # but the 2nd test succeeds on a Solaris 9 sparc hosts (Forte 7 cc).
525 # Skip them until we figure out what the problem is.
526 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
527 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
528
529 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
530 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])