1 # Executing Actions. -*- Autotest -*-
2 # Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
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)
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.
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
19 AT_BANNER([[User Actions.]])
21 ## ------------------ ##
22 ## Mid-rule actions. ##
23 ## ------------------ ##
25 AT_SETUP([Mid-rule actions])
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
32 AT_DATA_GRAMMAR([[input.y]],
36 static void yyerror (const char *msg);
37 static int yylex (void);
39 # define YYERROR_VERBOSE 1
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'); }
58 static const char *input = "123456789";
63 yyerror (const char *msg)
65 fprintf (stderr, "%s\n", msg);
75 AT_CHECK([bison -d -v -o input.c input.y])
77 AT_PARSER_CHECK([./input], 0,
87 ## ---------------- ##
89 ## ---------------- ##
91 AT_SETUP([Exotic Dollars])
93 AT_DATA_GRAMMAR([[input.y]],
97 static void yyerror (const char *msg);
98 static int yylex (void);
100 # define YYERROR_VERBOSE 1
108 %type <val> a_1 a_2 a_5
109 sum_of_the_five_previous_values
112 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
113 sum_of_the_five_previous_values
122 sum_of_the_five_previous_values:
124 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
136 yyerror (const char *msg)
138 fprintf (stderr, "%s\n", msg);
148 AT_CHECK([bison -d -v -o input.c input.y])
150 AT_PARSER_CHECK([./input], 0,
158 ## -------------------------- ##
159 ## Printers and Destructors. ##
160 ## -------------------------- ##
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
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]],
178 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
179 [#define RANGE(Location) (Location).first_line, (Location).last_line])
183 m4_ifval([$6], [%union
189 ]AT_LALR1_CC_IF([typedef yy::Location YYLTYPE;
190 m4_ifval([$6], , [#define YYSTYPE int])])
191 [static int yylex (]AT_LEX_FORMALS[);
192 ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
195 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input])[
199 ]AT_LALR1_CC_IF([cdebug_ << $$;],
200 [fprintf (yyoutput, "%d", $$)])[;
202 input line thing 'x' 'y'
205 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
209 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
213 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
217 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
221 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
226 This grammar is made to exercise error recovery.
227 "Lines" starting with `(' support error recovery, with
228 ')' as synchronizing token. Lines starting with 'x' can never
229 be recovered from if in error.
236 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
238 | line input /* Right recursive to load the stack so that popping at
239 EOF can be exercised. */
242 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
243 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
248 thing thing thing ';'
251 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
252 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
253 $3, RANGE (@3), $4, RANGE (@4));
255 | '(' thing thing ')'
258 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
259 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
260 $3, RANGE (@3), $4, RANGE (@4));
265 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
266 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
271 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
272 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
280 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
281 $$, RANGE (@$), $1, RANGE (@1));
285 /* Alias to ARGV[1]. */
286 const char *yysource = 0;
289 yylex (]AT_LEX_FORMALS[)
291 static unsigned int counter = 0;
293 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
294 /* As in BASIC, line numbers go from 10 to 10. */
296 [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
297 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
299 [ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
300 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
304 printf ("sending: '%c'", yysource[c]);
306 printf ("sending: EOF");
307 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
312 [/* A C++ error reporting function. */
314 yy::Parser::error_ ()
316 printf ("%d-%d: %s\n", RANGE (location), message.c_str());
323 yy::Parser parser (yydebug);
324 return parser.parse ();
328 yyerror (const char *msg)
330 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
334 main (int argc, const char *argv[])
336 yydebug = !!getenv ("YYDEBUG");
341 printf ("Parsing FAILED.\n");
344 printf ("Successful parse.\n");
350 [AT_CHECK([bison -o input.cc input.y])
351 AT_COMPILE_CXX([input])],
352 [AT_CHECK([bison -o input.c input.y])
353 AT_COMPILE([input])])
356 # Check the location of "empty"
357 # -----------------------------
358 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
359 # an empty "line" nterm.
360 # FIXME: This location is not satisfying. Depend on the lookahead?
361 AT_PARSER_CHECK([./input '(x)'], 0,
362 [[sending: '(' (0@0-9)
363 sending: 'x' (1@10-19)
364 thing (1@10-19): 'x' (1@10-19)
365 sending: ')' (2@20-29)
366 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
367 sending: EOF (3@30-39)
368 input (0@29-29): /* Nothing */
369 input (2@0-29): line (0@0-29) input (0@29-29)
374 # Check locations in error recovery
375 # ---------------------------------
376 # '(y)' is an error, but can be recovered from. But what's the location
377 # of the error itself ('y'), and of the resulting reduction ('(error)').
378 AT_PARSER_CHECK([./input '(y)'], 0,
379 [[sending: '(' (0@0-9)
380 sending: 'y' (1@10-19)
381 10-19: syntax error, unexpected 'y', expecting 'x'
382 Freeing token 'y' (1@10-19)
383 sending: ')' (2@20-29)
384 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
385 sending: EOF (3@30-39)
386 input (0@29-29): /* Nothing */
387 input (2@0-29): line (-1@0-29) input (0@29-29)
392 # Syntax errors caught by the parser
393 # ----------------------------------
394 # Exercise the discarding of stack top and input until `error'
397 # '(', 'x', 'x', 'x', 'x', 'x', ')',
399 # Load the stack and provoke an error that cannot be caught by the
400 # grammar, to check that the stack is cleared. And make sure the
401 # lookahead is freed.
406 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
407 [[sending: '(' (0@0-9)
408 sending: 'x' (1@10-19)
409 thing (1@10-19): 'x' (1@10-19)
410 sending: 'x' (2@20-29)
411 thing (2@20-29): 'x' (2@20-29)
412 sending: 'x' (3@30-39)
413 30-39: syntax error, unexpected 'x', expecting ')'
414 Freeing nterm thing (2@20-29)
415 Freeing nterm thing (1@10-19)
416 Freeing token 'x' (3@30-39)
417 sending: 'x' (4@40-49)
418 Freeing token 'x' (4@40-49)
419 sending: 'x' (5@50-59)
420 Freeing token 'x' (5@50-59)
421 sending: ')' (6@60-69)
422 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
423 sending: '(' (7@70-79)
424 sending: 'x' (8@80-89)
425 thing (8@80-89): 'x' (8@80-89)
426 sending: ')' (9@90-99)
427 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
428 sending: '(' (10@100-109)
429 sending: 'x' (11@110-119)
430 thing (11@110-119): 'x' (11@110-119)
431 sending: ')' (12@120-129)
432 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
433 sending: 'y' (13@130-139)
434 input (0@129-129): /* Nothing */
435 input (2@100-129): line (10@100-129) input (0@129-129)
436 input (2@70-129): line (7@70-99) input (2@100-129)
437 input (2@0-129): line (-1@0-69) input (2@70-129)
438 130-139: syntax error, unexpected 'y', expecting $end
439 Freeing nterm input (2@0-129)
440 Freeing token 'y' (13@130-139)
447 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG])
448 # --------------------------------------------------------------
449 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
450 [AT_SETUP([Printers and Destructors $2: $1])
452 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
463 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
464 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
465 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
466 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
467 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
468 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])