]>
Commit | Line | Data |
---|---|---|
82c035a8 | 1 | # Executing Actions. -*- Autotest -*- |
378f4bd8 | 2 | # Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 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 | |
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 | ||
9501dc6e | 32 | AT_DATA_GRAMMAR([[input.y]], |
8f3596a6 AD |
33 | [[%error-verbose |
34 | %debug | |
35 | %{ | |
d99361e6 AD |
36 | # include <stdio.h> |
37 | # include <stdlib.h> | |
38 | static void yyerror (const char *msg); | |
39 | static int yylex (void); | |
82c035a8 AD |
40 | %} |
41 | %% | |
931394cb AD |
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'); } | |
82c035a8 AD |
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 | ||
b56471a6 | 75 | AT_CHECK([bison -d -v -o input.c input.y]) |
1154cced AD |
76 | AT_COMPILE([input]) |
77 | AT_PARSER_CHECK([./input], 0, | |
931394cb | 78 | [[0123456789 |
82c035a8 AD |
79 | ]]) |
80 | ||
81 | AT_CLEANUP | |
75d1fe16 AD |
82 | |
83 | ||
84 | ||
5dac0025 PE |
85 | |
86 | ||
75d1fe16 AD |
87 | ## ---------------- ## |
88 | ## Exotic Dollars. ## | |
89 | ## ---------------- ## | |
90 | ||
91 | AT_SETUP([Exotic Dollars]) | |
92 | ||
9501dc6e | 93 | AT_DATA_GRAMMAR([[input.y]], |
8f3596a6 AD |
94 | [[%error-verbose |
95 | %debug | |
96 | %{ | |
75d1fe16 AD |
97 | # include <stdio.h> |
98 | # include <stdlib.h> | |
99 | static void yyerror (const char *msg); | |
100 | static int yylex (void); | |
affac613 | 101 | # define USE(Var) |
75d1fe16 AD |
102 | %} |
103 | ||
104 | %union | |
105 | { | |
106 | int val; | |
107 | }; | |
108 | ||
0ff67d71 | 109 | %type <val> a_1 a_2 a_5 |
378f4bd8 | 110 | sum_of_the_five_previous_values |
75d1fe16 AD |
111 | |
112 | %% | |
0ff67d71 PE |
113 | exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5 |
114 | sum_of_the_five_previous_values | |
75d1fe16 | 115 | { |
84866159 | 116 | USE (($1, $2, $<foo>3, $<foo>4, $5)); |
75d1fe16 AD |
117 | printf ("%d\n", $6); |
118 | } | |
119 | ; | |
120 | a_1: { $$ = 1; }; | |
121 | a_2: { $$ = 2; }; | |
75d1fe16 AD |
122 | a_5: { $$ = 5; }; |
123 | ||
124 | sum_of_the_five_previous_values: | |
125 | { | |
126 | $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4; | |
127 | } | |
128 | ; | |
129 | ||
130 | %% | |
131 | static int | |
132 | yylex (void) | |
133 | { | |
134 | return EOF; | |
135 | } | |
136 | ||
137 | static void | |
138 | yyerror (const char *msg) | |
139 | { | |
140 | fprintf (stderr, "%s\n", msg); | |
141 | } | |
142 | ||
143 | int | |
144 | main (void) | |
145 | { | |
146 | return yyparse (); | |
147 | } | |
148 | ]]) | |
149 | ||
84866159 | 150 | AT_CHECK([bison -d -v -o input.c input.y], 0) |
1154cced AD |
151 | AT_COMPILE([input]) |
152 | AT_PARSER_CHECK([./input], 0, | |
75d1fe16 AD |
153 | [[15 |
154 | ]]) | |
155 | ||
156 | AT_CLEANUP | |
9280d3ef AD |
157 | |
158 | ||
159 | ||
e776192e AD |
160 | ## -------------------------- ## |
161 | ## Printers and Destructors. ## | |
162 | ## -------------------------- ## | |
9280d3ef | 163 | |
ac700aa6 PE |
164 | # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG) |
165 | # ----------------------------------------------------------------------------- | |
3df37415 | 166 | m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR], |
9c66f418 AD |
167 | [# Make sure complex $n work. |
168 | m4_if([$1$2$3], $[1]$[2]$[3], [], | |
3df37415 AD |
169 | [m4_fatal([$0: Invalid arguments: $@])])dnl |
170 | ||
9c66f418 AD |
171 | # Be sure to pass all the %directives to this macro to have correct |
172 | # helping macros. So don't put any directly in the Bison file. | |
c2729758 | 173 | AT_BISON_OPTION_PUSHDEFS([$5]) |
9501dc6e | 174 | AT_DATA_GRAMMAR([[input.y]], |
34f98f46 | 175 | [[%start-header { |
9280d3ef AD |
176 | #include <stdio.h> |
177 | #include <stdlib.h> | |
9c66f418 | 178 | #include <assert.h> |
80ce3401 PE |
179 | |
180 | #define YYINITDEPTH 10 | |
181 | #define YYMAXDEPTH 10 | |
c2729758 ADL |
182 | ]AT_LALR1_CC_IF( |
183 | [#define RANGE(Location) (Location).begin.line, (Location).end.line], | |
184 | [#define RANGE(Location) (Location).first_line, (Location).last_line]) | |
9bc0dd67 | 185 | [} |
9c66f418 AD |
186 | |
187 | $5] | |
188 | m4_ifval([$6], [%union | |
9280d3ef AD |
189 | { |
190 | int ival; | |
ac700aa6 | 191 | }]) |
fb9712a9 | 192 | AT_LALR1_CC_IF([%define "global_tokens_and_yystype"]) |
34f98f46 | 193 | m4_ifval([$6], [[%end-header {]], [[%start-header {]]) |
9bc0dd67 | 194 | AT_LALR1_CC_IF([typedef yy::location YYLTYPE; |
9c66f418 | 195 | m4_ifval([$6], , [#define YYSTYPE int])]) |
c2729758 | 196 | [static int yylex (]AT_LEX_FORMALS[); |
ca6f187f | 197 | ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);]) |
9bc0dd67 | 198 | [} |
c2729758 | 199 | |
868d2d96 | 200 | ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[ |
e3170060 | 201 | |
868d2d96 | 202 | /* FIXME: This %printer isn't actually tested. */ |
a5eb1ed2 AD |
203 | %printer |
204 | { | |
9a1e9989 | 205 | ]AT_LALR1_CC_IF([debug_stream () << $$;], |
3fc16193 | 206 | [fprintf (yyoutput, "%d", $$)])[; |
a5eb1ed2 | 207 | } |
9c66f418 | 208 | input line thing 'x' 'y' |
e3170060 AD |
209 | |
210 | %destructor | |
4c6cc1db | 211 | { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e | 212 | input |
5719c109 | 213 | |
7bd6c77e | 214 | %destructor |
4c6cc1db | 215 | { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e AD |
216 | line |
217 | ||
218 | %destructor | |
4c6cc1db | 219 | { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e AD |
220 | thing |
221 | ||
222 | %destructor | |
4c6cc1db | 223 | { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e | 224 | 'x' |
5719c109 | 225 | |
9c66f418 AD |
226 | %destructor |
227 | { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); } | |
228 | 'y' | |
229 | ||
868d2d96 JD |
230 | %token END 0 |
231 | %destructor | |
232 | { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); } | |
233 | END | |
234 | ||
9280d3ef | 235 | %% |
9c66f418 AD |
236 | /* |
237 | This grammar is made to exercise error recovery. | |
238 | "Lines" starting with `(' support error recovery, with | |
239 | ')' as synchronizing token. Lines starting with 'x' can never | |
240 | be recovered from if in error. | |
241 | */ | |
242 | ||
9280d3ef AD |
243 | input: |
244 | /* Nothing. */ | |
5719c109 AD |
245 | { |
246 | $$ = 0; | |
16f37b35 | 247 | printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$)); |
5719c109 AD |
248 | } |
249 | | line input /* Right recursive to load the stack so that popping at | |
868d2d96 | 250 | END can be exercised. */ |
5719c109 AD |
251 | { |
252 | $$ = 2; | |
16f37b35 | 253 | printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n", |
4c6cc1db | 254 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2)); |
5719c109 | 255 | } |
9280d3ef AD |
256 | ; |
257 | ||
258 | line: | |
259 | thing thing thing ';' | |
5719c109 AD |
260 | { |
261 | $$ = $1; | |
16f37b35 | 262 | printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n", |
4c6cc1db | 263 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), |
16f37b35 | 264 | $3, RANGE (@3), $4, RANGE (@4)); |
5719c109 | 265 | } |
9c66f418 | 266 | | '(' thing thing ')' |
5719c109 AD |
267 | { |
268 | $$ = $1; | |
9c66f418 AD |
269 | printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n", |
270 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), | |
271 | $3, RANGE (@3), $4, RANGE (@4)); | |
5719c109 | 272 | } |
9c66f418 | 273 | | '(' thing ')' |
5719c109 AD |
274 | { |
275 | $$ = $1; | |
9c66f418 AD |
276 | printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n", |
277 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3)); | |
5719c109 | 278 | } |
9c66f418 | 279 | | '(' error ')' |
5719c109 AD |
280 | { |
281 | $$ = -1; | |
9c66f418 AD |
282 | printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n", |
283 | $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3)); | |
5719c109 | 284 | } |
9280d3ef AD |
285 | ; |
286 | ||
287 | thing: | |
5719c109 AD |
288 | 'x' |
289 | { | |
290 | $$ = $1; | |
4c6cc1db AD |
291 | printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n", |
292 | $$, RANGE (@$), $1, RANGE (@1)); | |
5719c109 | 293 | } |
9280d3ef AD |
294 | ; |
295 | %% | |
9c66f418 | 296 | /* Alias to ARGV[1]. */ |
a9739e7c | 297 | const char *source = 0; |
9c66f418 | 298 | |
9280d3ef | 299 | static int |
c2729758 | 300 | yylex (]AT_LEX_FORMALS[) |
9280d3ef | 301 | { |
5a08f1ce | 302 | static unsigned int counter = 0; |
9280d3ef | 303 | |
9c66f418 AD |
304 | int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++; |
305 | /* As in BASIC, line numbers go from 10 to 10. */ | |
c2729758 | 306 | ]AT_LALR1_CC_IF( |
9c66f418 AD |
307 | [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c; |
308 | AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9; | |
c2729758 | 309 | ], |
9c66f418 AD |
310 | [ AT_LOC.first_line = AT_LOC.first_column = 10 * c; |
311 | AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9; | |
c2729758 | 312 | ])[ |
9c66f418 | 313 | |
a9739e7c PE |
314 | if (source[c]) |
315 | printf ("sending: '%c'", source[c]); | |
9280d3ef | 316 | else |
868d2d96 | 317 | printf ("sending: END"); |
9c66f418 | 318 | printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[)); |
a9739e7c | 319 | return source[c]; |
9280d3ef AD |
320 | } |
321 | ||
c2729758 | 322 | ]AT_LALR1_CC_IF( |
68e11668 | 323 | [/* A C++ error reporting function. */ |
c2729758 | 324 | void |
99880de5 | 325 | yy::parser::error (const location& l, const std::string& m) |
c2729758 | 326 | { |
efeed023 | 327 | printf ("%d-%d: %s\n", RANGE (l), m.c_str()); |
c2729758 ADL |
328 | } |
329 | ||
330 | static bool yydebug; | |
331 | int | |
332 | yyparse () | |
333 | { | |
99880de5 | 334 | yy::parser parser; |
a3cb6248 | 335 | parser.set_debug_level (yydebug); |
c2729758 ADL |
336 | return parser.parse (); |
337 | } | |
338 | ], | |
339 | [static void | |
9280d3ef AD |
340 | yyerror (const char *msg) |
341 | { | |
4c6cc1db | 342 | printf ("%d-%d: %s\n", RANGE (yylloc), msg); |
c2729758 | 343 | }])[ |
9280d3ef | 344 | |
9280d3ef | 345 | int |
9c66f418 | 346 | main (int argc, const char *argv[]) |
9280d3ef | 347 | { |
6100a9aa | 348 | int status; |
9280d3ef | 349 | yydebug = !!getenv ("YYDEBUG"); |
9c66f418 | 350 | assert (argc == 2); |
a9739e7c | 351 | source = argv[1]; |
6100a9aa PE |
352 | status = yyparse (); |
353 | switch (status) | |
9280d3ef | 354 | { |
6100a9aa PE |
355 | case 0: printf ("Successful parse.\n"); break; |
356 | case 1: printf ("Parsing FAILED.\n"); break; | |
357 | default: printf ("Parsing FAILED (status %d).\n", status); break; | |
9280d3ef | 358 | } |
6100a9aa | 359 | return status; |
9280d3ef AD |
360 | } |
361 | ]]) | |
362 | ||
07971983 PE |
363 | AT_LALR1_CC_IF( |
364 | [AT_CHECK([bison -o input.cc input.y]) | |
365 | AT_COMPILE_CXX([input])], | |
366 | [AT_CHECK([bison -o input.c input.y]) | |
367 | AT_COMPILE([input])]) | |
9c66f418 AD |
368 | |
369 | ||
370 | # Check the location of "empty" | |
371 | # ----------------------------- | |
372 | # I.e., epsilon-reductions, as in "(x)" which ends by reducing | |
373 | # an empty "line" nterm. | |
374 | # FIXME: This location is not satisfying. Depend on the lookahead? | |
375 | AT_PARSER_CHECK([./input '(x)'], 0, | |
376 | [[sending: '(' (0@0-9) | |
377 | sending: 'x' (1@10-19) | |
378 | thing (1@10-19): 'x' (1@10-19) | |
379 | sending: ')' (2@20-29) | |
380 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
868d2d96 | 381 | sending: END (3@30-39) |
b4a20338 AD |
382 | input (0@29-29): /* Nothing */ |
383 | input (2@0-29): line (0@0-29) input (0@29-29) | |
868d2d96 | 384 | Freeing token END (3@30-39) |
258b75ca | 385 | Freeing nterm input (2@0-29) |
9c66f418 AD |
386 | Successful parse. |
387 | ]]) | |
388 | ||
389 | ||
390 | # Check locations in error recovery | |
391 | # --------------------------------- | |
392 | # '(y)' is an error, but can be recovered from. But what's the location | |
393 | # of the error itself ('y'), and of the resulting reduction ('(error)'). | |
394 | AT_PARSER_CHECK([./input '(y)'], 0, | |
395 | [[sending: '(' (0@0-9) | |
396 | sending: 'y' (1@10-19) | |
397 | 10-19: syntax error, unexpected 'y', expecting 'x' | |
398 | Freeing token 'y' (1@10-19) | |
399 | sending: ')' (2@20-29) | |
400 | line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29) | |
868d2d96 | 401 | sending: END (3@30-39) |
b4a20338 AD |
402 | input (0@29-29): /* Nothing */ |
403 | input (2@0-29): line (-1@0-29) input (0@29-29) | |
868d2d96 | 404 | Freeing token END (3@30-39) |
258b75ca | 405 | Freeing nterm input (2@0-29) |
9c66f418 AD |
406 | Successful parse. |
407 | ]]) | |
408 | ||
409 | ||
410 | # Syntax errors caught by the parser | |
411 | # ---------------------------------- | |
412 | # Exercise the discarding of stack top and input until `error' | |
413 | # can be reduced. | |
414 | # | |
415 | # '(', 'x', 'x', 'x', 'x', 'x', ')', | |
416 | # | |
417 | # Load the stack and provoke an error that cannot be caught by the | |
418 | # grammar, to check that the stack is cleared. And make sure the | |
419 | # lookahead is freed. | |
420 | # | |
421 | # '(', 'x', ')', | |
422 | # '(', 'x', ')', | |
423 | # 'y' | |
424 | AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1, | |
425 | [[sending: '(' (0@0-9) | |
4c6cc1db AD |
426 | sending: 'x' (1@10-19) |
427 | thing (1@10-19): 'x' (1@10-19) | |
428 | sending: 'x' (2@20-29) | |
429 | thing (2@20-29): 'x' (2@20-29) | |
430 | sending: 'x' (3@30-39) | |
9c66f418 | 431 | 30-39: syntax error, unexpected 'x', expecting ')' |
4c6cc1db AD |
432 | Freeing nterm thing (2@20-29) |
433 | Freeing nterm thing (1@10-19) | |
4c6cc1db AD |
434 | Freeing token 'x' (3@30-39) |
435 | sending: 'x' (4@40-49) | |
436 | Freeing token 'x' (4@40-49) | |
437 | sending: 'x' (5@50-59) | |
438 | Freeing token 'x' (5@50-59) | |
9c66f418 AD |
439 | sending: ')' (6@60-69) |
440 | line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69) | |
441 | sending: '(' (7@70-79) | |
4c6cc1db AD |
442 | sending: 'x' (8@80-89) |
443 | thing (8@80-89): 'x' (8@80-89) | |
9c66f418 AD |
444 | sending: ')' (9@90-99) |
445 | line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99) | |
446 | sending: '(' (10@100-109) | |
447 | sending: 'x' (11@110-119) | |
448 | thing (11@110-119): 'x' (11@110-119) | |
449 | sending: ')' (12@120-129) | |
450 | line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129) | |
451 | sending: 'y' (13@130-139) | |
b4a20338 AD |
452 | input (0@129-129): /* Nothing */ |
453 | input (2@100-129): line (10@100-129) input (0@129-129) | |
9c66f418 AD |
454 | input (2@70-129): line (7@70-99) input (2@100-129) |
455 | input (2@0-129): line (-1@0-69) input (2@70-129) | |
868d2d96 | 456 | 130-139: syntax error, unexpected 'y', expecting END |
9c66f418 AD |
457 | Freeing nterm input (2@0-129) |
458 | Freeing token 'y' (13@130-139) | |
5719c109 | 459 | Parsing FAILED. |
9280d3ef AD |
460 | ]]) |
461 | ||
868d2d96 JD |
462 | |
463 | # Syntax error caught by the parser where lookahead = END | |
464 | # -------------------------------------------------------- | |
465 | # Load the stack and provoke an error that cannot be caught by the | |
466 | # grammar, to check that the stack is cleared. And make sure the | |
467 | # lookahead is freed. | |
468 | # | |
469 | # '(', 'x', ')', | |
470 | # '(', 'x', ')', | |
471 | # 'x' | |
472 | AT_PARSER_CHECK([./input '(x)(x)x'], 1, | |
473 | [[sending: '(' (0@0-9) | |
474 | sending: 'x' (1@10-19) | |
475 | thing (1@10-19): 'x' (1@10-19) | |
476 | sending: ')' (2@20-29) | |
477 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
478 | sending: '(' (3@30-39) | |
479 | sending: 'x' (4@40-49) | |
480 | thing (4@40-49): 'x' (4@40-49) | |
481 | sending: ')' (5@50-59) | |
482 | line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59) | |
483 | sending: 'x' (6@60-69) | |
484 | thing (6@60-69): 'x' (6@60-69) | |
485 | sending: END (7@70-79) | |
486 | 70-79: syntax error, unexpected END, expecting 'x' | |
487 | Freeing nterm thing (6@60-69) | |
488 | Freeing nterm line (3@30-59) | |
489 | Freeing nterm line (0@0-29) | |
490 | Freeing token END (7@70-79) | |
491 | Parsing FAILED. | |
492 | ]]) | |
493 | ||
494 | ||
80ce3401 PE |
495 | # Check destruction upon stack overflow |
496 | # ------------------------------------- | |
497 | # Upon stack overflow, all symbols on the stack should be destroyed. | |
498 | # Only check for yacc.c. | |
499 | AT_YACC_IF([ | |
6100a9aa | 500 | AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2, |
80ce3401 PE |
501 | [[sending: '(' (0@0-9) |
502 | sending: 'x' (1@10-19) | |
503 | thing (1@10-19): 'x' (1@10-19) | |
504 | sending: ')' (2@20-29) | |
505 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
506 | sending: '(' (3@30-39) | |
507 | sending: 'x' (4@40-49) | |
508 | thing (4@40-49): 'x' (4@40-49) | |
509 | sending: ')' (5@50-59) | |
510 | line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59) | |
511 | sending: '(' (6@60-69) | |
512 | sending: 'x' (7@70-79) | |
513 | thing (7@70-79): 'x' (7@70-79) | |
514 | sending: ')' (8@80-89) | |
515 | line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89) | |
516 | sending: '(' (9@90-99) | |
517 | sending: 'x' (10@100-109) | |
518 | thing (10@100-109): 'x' (10@100-109) | |
519 | sending: ')' (11@110-119) | |
520 | line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119) | |
521 | sending: '(' (12@120-129) | |
522 | sending: 'x' (13@130-139) | |
523 | thing (13@130-139): 'x' (13@130-139) | |
524 | sending: ')' (14@140-149) | |
525 | line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149) | |
526 | sending: '(' (15@150-159) | |
527 | sending: 'x' (16@160-169) | |
528 | thing (16@160-169): 'x' (16@160-169) | |
529 | sending: ')' (17@170-179) | |
530 | line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179) | |
531 | sending: '(' (18@180-189) | |
532 | sending: 'x' (19@190-199) | |
533 | thing (19@190-199): 'x' (19@190-199) | |
534 | sending: ')' (20@200-209) | |
1a059451 | 535 | 200-209: memory exhausted |
80ce3401 PE |
536 | Freeing nterm thing (19@190-199) |
537 | Freeing nterm line (15@150-179) | |
538 | Freeing nterm line (12@120-149) | |
539 | Freeing nterm line (9@90-119) | |
540 | Freeing nterm line (6@60-89) | |
541 | Freeing nterm line (3@30-59) | |
542 | Freeing nterm line (0@0-29) | |
6100a9aa | 543 | Parsing FAILED (status 2). |
80ce3401 PE |
544 | ]]) |
545 | ]) | |
546 | ||
3df37415 AD |
547 | ]) |
548 | ||
549 | ||
a14a26fa | 550 | # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG]) |
046ac74e | 551 | # --------------------------------------------------------------------------- |
3df37415 | 552 | m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR], |
9c66f418 AD |
553 | [AT_SETUP([Printers and Destructors $2: $1]) |
554 | ||
a14a26fa | 555 | $3 |
9c66f418 AD |
556 | _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4], |
557 | [%error-verbose | |
558 | %debug | |
559 | %verbose | |
560 | %locations | |
561 | $1], [$2]) | |
562 | ||
563 | AT_CLEANUP | |
3df37415 AD |
564 | ]) |
565 | ||
566 | ||
ac700aa6 PE |
567 | AT_CHECK_PRINTER_AND_DESTRUCTOR([]) |
568 | AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union]) | |
046ac74e | 569 | |
a14a26fa PE |
570 | # These tests currently fail on a Debian GNU/Linux 3.0r2 x86 host, |
571 | # but the 2nd test succeeds on a Solaris 9 sparc hosts (Forte 7 cc). | |
572 | # Skip them until we figure out what the problem is. | |
fd19f271 AD |
573 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"]) |
574 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union]) | |
046ac74e | 575 | |
1576d44d AD |
576 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser]) |
577 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union]) | |
ec5479ce JD |
578 | |
579 | ||
580 | ||
ec5479ce JD |
581 | ## --------------------------------- ## |
582 | ## Default %printer and %destructor. ## | |
583 | ## --------------------------------- ## | |
584 | ||
585 | # Check that the right %printer and %destructor are called, that they're not | |
586 | # called for $end, and that $$ and @$ work correctly. | |
587 | ||
588 | AT_SETUP([Default %printer and %destructor]) | |
589 | ||
590 | AT_DATA_GRAMMAR([[input.y]], | |
591 | [[%error-verbose | |
592 | %debug | |
593 | %locations | |
594 | %initial-action { | |
595 | @$.first_line = @$.last_line = 1; | |
596 | @$.first_column = @$.last_column = 1; | |
597 | } | |
598 | ||
599 | %{ | |
600 | # include <stdio.h> | |
601 | # include <stdlib.h> | |
602 | static void yyerror (const char *msg); | |
603 | static int yylex (void); | |
604 | # define USE(SYM) | |
605 | %} | |
606 | ||
607 | %printer { | |
608 | fprintf (yyoutput, "Default printer for '%c' @ %d", $$, @$.first_column); | |
609 | } | |
610 | %destructor { | |
611 | fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column); | |
612 | } | |
613 | ||
614 | %printer { | |
615 | fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column); | |
616 | } 'b' 'c' | |
617 | %destructor { | |
618 | fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column); | |
619 | } 'b' 'c' | |
620 | ||
621 | %% | |
622 | ||
623 | start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ; | |
624 | ||
625 | %% | |
626 | ||
627 | static int | |
628 | yylex (void) | |
629 | { | |
630 | static const char *input = "abcd"; | |
631 | static int column = 1; | |
632 | yylval = *input++; | |
633 | yylloc.first_line = yylloc.last_line = 1; | |
634 | yylloc.first_column = yylloc.last_column = column++; | |
635 | return yylval; | |
636 | } | |
637 | ||
638 | static void | |
639 | yyerror (const char *msg) | |
640 | { | |
641 | fprintf (stderr, "%s\n", msg); | |
642 | } | |
643 | ||
644 | int | |
645 | main (void) | |
646 | { | |
647 | yydebug = 1; | |
648 | return yyparse (); | |
649 | } | |
650 | ]]) | |
651 | ||
652 | AT_CHECK([bison -o input.c input.y]) | |
653 | AT_COMPILE([input]) | |
654 | AT_PARSER_CHECK([./input], 1, | |
655 | [[Default destructor for 'd' @ 4. | |
656 | 'b'/'c' destructor for 'c' @ 3. | |
657 | 'b'/'c' destructor for 'b' @ 2. | |
658 | Default destructor for 'a' @ 1. | |
659 | ]], | |
660 | [[Starting parse | |
661 | Entering state 0 | |
662 | Reading a token: Next token is token 'a' (1.1-1.1: Default printer for 'a' @ 1) | |
663 | Shifting token 'a' (1.1-1.1: Default printer for 'a' @ 1) | |
664 | Entering state 1 | |
665 | Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) | |
666 | Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) | |
667 | Entering state 3 | |
668 | Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) | |
669 | Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) | |
670 | Entering state 5 | |
671 | Reading a token: Next token is token 'd' (1.4-1.4: Default printer for 'd' @ 4) | |
672 | Shifting token 'd' (1.4-1.4: Default printer for 'd' @ 4) | |
673 | Entering state 6 | |
674 | Reading a token: Now at end of input. | |
675 | syntax error, unexpected $end, expecting 'e' | |
676 | Error: popping token 'd' (1.4-1.4: Default printer for 'd' @ 4) | |
677 | Stack now 0 1 3 5 | |
678 | Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) | |
679 | Stack now 0 1 3 | |
680 | Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) | |
681 | Stack now 0 1 | |
682 | Error: popping token 'a' (1.1-1.1: Default printer for 'a' @ 1) | |
683 | Stack now 0 | |
684 | Cleanup: discarding lookahead token $end (1.5-1.5: ) | |
685 | Stack now 0 | |
686 | ]]) | |
687 | ||
688 | AT_CLEANUP | |
689 | ||
690 | ||
691 | ||
ec5479ce | 692 | ## ------------------------------------------------------------- ## |
3508ce36 | 693 | ## Default %printer and %destructor for user-defined end token. ## |
ec5479ce JD |
694 | ## ------------------------------------------------------------- ## |
695 | ||
3508ce36 | 696 | AT_SETUP([Default %printer and %destructor for user-defined end token]) |
ec5479ce JD |
697 | |
698 | AT_DATA_GRAMMAR([[input.y]], | |
699 | [[%error-verbose | |
700 | %debug | |
701 | %locations | |
702 | %initial-action { | |
703 | @$.first_line = @$.last_line = 1; | |
704 | @$.first_column = @$.last_column = 1; | |
705 | } | |
706 | ||
707 | %{ | |
708 | # include <stdio.h> | |
709 | # include <stdlib.h> | |
710 | static void yyerror (const char *msg); | |
711 | static int yylex (void); | |
712 | # define USE(SYM) | |
713 | %} | |
714 | ||
715 | %token END 0 | |
716 | %printer { | |
717 | fprintf (yyoutput, "Default printer for '%c' @ %d", $$, @$.first_column); | |
718 | } | |
719 | %destructor { | |
720 | fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column); | |
721 | } | |
722 | ||
723 | %% | |
724 | ||
725 | start: { $$ = 'S'; } ; | |
726 | ||
727 | %% | |
728 | ||
729 | static int | |
730 | yylex (void) | |
731 | { | |
732 | yylval = 'E'; | |
733 | yylloc.first_line = yylloc.last_line = 1; | |
734 | yylloc.first_column = yylloc.last_column = 1; | |
735 | return 0; | |
736 | } | |
737 | ||
738 | static void | |
739 | yyerror (const char *msg) | |
740 | { | |
741 | fprintf (stderr, "%s\n", msg); | |
742 | } | |
743 | ||
744 | int | |
745 | main (void) | |
746 | { | |
747 | yydebug = 1; | |
748 | return yyparse (); | |
749 | } | |
750 | ]]) | |
751 | ||
752 | AT_CHECK([bison -o input.c input.y]) | |
753 | AT_COMPILE([input]) | |
754 | AT_PARSER_CHECK([./input], 0, | |
755 | [[Default destructor for 'E' @ 1. | |
756 | Default destructor for 'S' @ 1. | |
757 | ]], | |
758 | [[Starting parse | |
759 | Entering state 0 | |
760 | Reducing stack by rule 1 (line 37): | |
761 | -> $$ = nterm start (1.1-1.1: Default printer for 'S' @ 1) | |
762 | Stack now 0 | |
763 | Entering state 1 | |
764 | Reading a token: Now at end of input. | |
765 | Shifting token END (1.1-1.1: Default printer for 'E' @ 1) | |
766 | Entering state 2 | |
767 | Stack now 0 1 2 | |
768 | Cleanup: popping token END (1.1-1.1: Default printer for 'E' @ 1) | |
769 | Cleanup: popping nterm start (1.1-1.1: Default printer for 'S' @ 1) | |
770 | ]]) | |
771 | ||
772 | AT_CLEANUP | |
9350499c JD |
773 | |
774 | ||
775 | ||
776 | ## ------------------------------------------------------------------ ## | |
777 | ## Default %printer and %destructor are not for error or $undefined. ## | |
778 | ## ------------------------------------------------------------------ ## | |
779 | ||
780 | AT_SETUP([Default %printer and %destructor are not for error or \$undefined]) | |
781 | ||
782 | # If Bison were to apply the default %printer and %destructor to the error | |
783 | # token or to $undefined: | |
784 | # - For the error token: | |
785 | # - It would generate warnings for unused $n. | |
786 | # - It would invoke the %printer and %destructor on the error token's | |
787 | # semantic value, which would be initialized from the lookahead, which | |
788 | # would be destroyed separately. | |
789 | # - For $undefined, who knows what the semantic value would be. | |
790 | ||
791 | AT_DATA_GRAMMAR([[input.y]], | |
792 | [[%debug | |
793 | ||
794 | %{ | |
795 | # include <stdio.h> | |
796 | static void yyerror (const char *msg); | |
797 | static int yylex (void); | |
798 | # define USE(SYM) | |
799 | %} | |
800 | ||
801 | %printer { | |
802 | fprintf (yyoutput, "'%c'", $$); | |
803 | } | |
804 | %destructor { | |
805 | fprintf (stderr, "DESTROY '%c'\n", $$); | |
806 | } | |
807 | ||
808 | %% | |
809 | ||
810 | start: | |
811 | { $$ = 'S'; } | |
812 | /* In order to reveal the problems that this bug caused during parsing, add | |
813 | * $2 to USE. */ | |
814 | | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; } | |
815 | ; | |
816 | ||
817 | %% | |
818 | ||
819 | static int | |
820 | yylex (void) | |
821 | { | |
822 | static const char *input = "abd"; | |
823 | yylval = *input++; | |
824 | return yylval; | |
825 | } | |
826 | ||
827 | static void | |
828 | yyerror (const char *msg) | |
829 | { | |
830 | fprintf (stderr, "%s\n", msg); | |
831 | } | |
832 | ||
833 | int | |
834 | main (void) | |
835 | { | |
836 | yydebug = 1; | |
837 | return yyparse (); | |
838 | } | |
839 | ]]) | |
840 | ||
841 | AT_CHECK([bison -o input.c input.y]) | |
842 | AT_COMPILE([input]) | |
843 | AT_PARSER_CHECK([./input], [1], [], | |
844 | [[Starting parse | |
845 | Entering state 0 | |
846 | Reading a token: Next token is token 'a' ('a') | |
847 | Shifting token 'a' ('a') | |
848 | Entering state 1 | |
849 | Reading a token: Next token is token 'b' ('b') | |
850 | syntax error | |
851 | Shifting token error () | |
852 | Entering state 3 | |
853 | Next token is token 'b' ('b') | |
854 | Shifting token 'b' ('b') | |
855 | Entering state 5 | |
856 | Reading a token: Next token is token $undefined () | |
857 | Error: popping token 'b' ('b') | |
858 | DESTROY 'b' | |
859 | Stack now 0 1 3 | |
860 | Error: popping token error () | |
861 | Stack now 0 1 | |
862 | Shifting token error () | |
863 | Entering state 3 | |
864 | Next token is token $undefined () | |
865 | Error: discarding token $undefined () | |
866 | Error: popping token error () | |
867 | Stack now 0 1 | |
868 | Shifting token error () | |
869 | Entering state 3 | |
870 | Reading a token: Now at end of input. | |
871 | Cleanup: discarding lookahead token $end () | |
872 | Stack now 0 1 3 | |
873 | Cleanup: popping token error () | |
874 | Cleanup: popping token 'a' ('a') | |
875 | DESTROY 'a' | |
876 | ]]) | |
877 | ||
878 | AT_CLEANUP | |
879 | ||
880 | ||
881 | ||
882 | ## ------------------------------------------------------ ## | |
883 | ## Default %printer and %destructor are not for $accept. ## | |
884 | ## ------------------------------------------------------ ## | |
885 | ||
886 | AT_SETUP([Default %printer and %destructor are not for \$accept]) | |
887 | ||
888 | # If YYSTYPE is a union and Bison were to apply the default %printer and | |
889 | # %destructor to $accept: | |
890 | # - The %printer and %destructor code generated for $accept would always be | |
891 | # dead code because $accept is currently never shifted onto the stack. | |
892 | # - $$ for $accept would always be of type YYSTYPE because it's not possible | |
893 | # to declare `%type <field> $accept'. (Also true for $undefined.) | |
894 | # - Thus, the compiler might complain that the user code assumes the wrong | |
895 | # type for $$ since the code might assume the type associated with a | |
896 | # specific union field, which is especially reasonable in C++ since that | |
897 | # type may be a base type. This test case checks for this problem. (Also | |
898 | # true for $undefined and the error token, so there are three warnings for | |
899 | # %printer and three for %destructor.) | |
900 | ||
901 | AT_DATA_GRAMMAR([[input.y]], | |
902 | [[%debug /* So that %printer is actually compiled. */ | |
903 | ||
904 | %{ | |
905 | # include <stdio.h> | |
906 | static void yyerror (const char *msg); | |
907 | static int yylex (void); | |
908 | # define USE(SYM) | |
909 | %} | |
910 | ||
911 | %printer { | |
912 | char chr = $$; | |
913 | fprintf (yyoutput, "'%c'", chr); | |
914 | } | |
915 | %destructor { | |
916 | char chr = $$; | |
917 | fprintf (stderr, "DESTROY '%c'\n", chr); | |
918 | } | |
919 | ||
920 | %union { char chr; } | |
921 | %type <chr> start | |
922 | ||
923 | %% | |
924 | ||
925 | start: { USE($$); } ; | |
926 | ||
927 | %% | |
928 | ||
929 | static int | |
930 | yylex (void) | |
931 | { | |
932 | return 0; | |
933 | } | |
934 | ||
935 | static void | |
936 | yyerror (const char *msg) | |
937 | { | |
938 | fprintf (stderr, "%s\n", msg); | |
939 | } | |
940 | ||
941 | int | |
942 | main (void) | |
943 | { | |
944 | return yyparse (); | |
945 | } | |
946 | ]]) | |
947 | ||
948 | AT_CHECK([bison -o input.c input.y]) | |
949 | AT_COMPILE([input]) | |
950 | ||
951 | AT_CLEANUP |