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