]>
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 | { | |
cf806753 PE |
58 | static char const input[] = "123456789"; |
59 | static size_t toknum; | |
60 | if (! (toknum < sizeof input)) | |
61 | abort (); | |
62 | return input[toknum++]; | |
82c035a8 AD |
63 | } |
64 | ||
65 | static void | |
66 | yyerror (const char *msg) | |
67 | { | |
68 | fprintf (stderr, "%s\n", msg); | |
69 | } | |
70 | ||
71 | int | |
72 | main (void) | |
73 | { | |
74 | return yyparse (); | |
75 | } | |
76 | ]]) | |
77 | ||
b56471a6 | 78 | AT_CHECK([bison -d -v -o input.c input.y]) |
1154cced AD |
79 | AT_COMPILE([input]) |
80 | AT_PARSER_CHECK([./input], 0, | |
931394cb | 81 | [[0123456789 |
82c035a8 AD |
82 | ]]) |
83 | ||
84 | AT_CLEANUP | |
75d1fe16 AD |
85 | |
86 | ||
87 | ||
5dac0025 PE |
88 | |
89 | ||
75d1fe16 AD |
90 | ## ---------------- ## |
91 | ## Exotic Dollars. ## | |
92 | ## ---------------- ## | |
93 | ||
94 | AT_SETUP([Exotic Dollars]) | |
95 | ||
9501dc6e | 96 | AT_DATA_GRAMMAR([[input.y]], |
8f3596a6 AD |
97 | [[%error-verbose |
98 | %debug | |
99 | %{ | |
75d1fe16 AD |
100 | # include <stdio.h> |
101 | # include <stdlib.h> | |
102 | static void yyerror (const char *msg); | |
103 | static int yylex (void); | |
affac613 | 104 | # define USE(Var) |
75d1fe16 AD |
105 | %} |
106 | ||
107 | %union | |
108 | { | |
109 | int val; | |
110 | }; | |
111 | ||
0ff67d71 | 112 | %type <val> a_1 a_2 a_5 |
378f4bd8 | 113 | sum_of_the_five_previous_values |
75d1fe16 AD |
114 | |
115 | %% | |
0ff67d71 PE |
116 | exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5 |
117 | sum_of_the_five_previous_values | |
75d1fe16 | 118 | { |
84866159 | 119 | USE (($1, $2, $<foo>3, $<foo>4, $5)); |
75d1fe16 AD |
120 | printf ("%d\n", $6); |
121 | } | |
122 | ; | |
123 | a_1: { $$ = 1; }; | |
124 | a_2: { $$ = 2; }; | |
75d1fe16 AD |
125 | a_5: { $$ = 5; }; |
126 | ||
127 | sum_of_the_five_previous_values: | |
128 | { | |
129 | $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4; | |
130 | } | |
131 | ; | |
132 | ||
133 | %% | |
134 | static int | |
135 | yylex (void) | |
136 | { | |
cf806753 PE |
137 | static int called; |
138 | if (called++) | |
139 | abort (); | |
75d1fe16 AD |
140 | return EOF; |
141 | } | |
142 | ||
143 | static void | |
144 | yyerror (const char *msg) | |
145 | { | |
146 | fprintf (stderr, "%s\n", msg); | |
147 | } | |
148 | ||
149 | int | |
150 | main (void) | |
151 | { | |
152 | return yyparse (); | |
153 | } | |
154 | ]]) | |
155 | ||
84866159 | 156 | AT_CHECK([bison -d -v -o input.c input.y], 0) |
1154cced AD |
157 | AT_COMPILE([input]) |
158 | AT_PARSER_CHECK([./input], 0, | |
75d1fe16 AD |
159 | [[15 |
160 | ]]) | |
161 | ||
162 | AT_CLEANUP | |
9280d3ef AD |
163 | |
164 | ||
165 | ||
e776192e AD |
166 | ## -------------------------- ## |
167 | ## Printers and Destructors. ## | |
168 | ## -------------------------- ## | |
9280d3ef | 169 | |
ac700aa6 PE |
170 | # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG) |
171 | # ----------------------------------------------------------------------------- | |
3df37415 | 172 | m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR], |
9c66f418 AD |
173 | [# Make sure complex $n work. |
174 | m4_if([$1$2$3], $[1]$[2]$[3], [], | |
3df37415 AD |
175 | [m4_fatal([$0: Invalid arguments: $@])])dnl |
176 | ||
9c66f418 AD |
177 | # Be sure to pass all the %directives to this macro to have correct |
178 | # helping macros. So don't put any directly in the Bison file. | |
c2729758 | 179 | AT_BISON_OPTION_PUSHDEFS([$5]) |
9501dc6e | 180 | AT_DATA_GRAMMAR([[input.y]], |
16dc6a9e | 181 | [[%code requires { |
9280d3ef AD |
182 | #include <stdio.h> |
183 | #include <stdlib.h> | |
cf806753 | 184 | #include <string.h> |
9c66f418 | 185 | #include <assert.h> |
80ce3401 PE |
186 | |
187 | #define YYINITDEPTH 10 | |
188 | #define YYMAXDEPTH 10 | |
c2729758 ADL |
189 | ]AT_LALR1_CC_IF( |
190 | [#define RANGE(Location) (Location).begin.line, (Location).end.line], | |
191 | [#define RANGE(Location) (Location).first_line, (Location).last_line]) | |
9bc0dd67 | 192 | [} |
9c66f418 AD |
193 | |
194 | $5] | |
195 | m4_ifval([$6], [%union | |
9280d3ef AD |
196 | { |
197 | int ival; | |
ac700aa6 | 198 | }]) |
16dc6a9e JD |
199 | AT_LALR1_CC_IF([%define global_tokens_and_yystype]) |
200 | m4_ifval([$6], [[%code provides {]], [[%code {]]) | |
e8ec4d9b | 201 | AT_LALR1_CC_IF([typedef yy::location YYLTYPE;]) |
c2729758 | 202 | [static int yylex (]AT_LEX_FORMALS[); |
ca6f187f | 203 | ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);]) |
9bc0dd67 | 204 | [} |
c2729758 | 205 | |
868d2d96 | 206 | ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[ |
e3170060 | 207 | |
868d2d96 | 208 | /* FIXME: This %printer isn't actually tested. */ |
a5eb1ed2 AD |
209 | %printer |
210 | { | |
9a1e9989 | 211 | ]AT_LALR1_CC_IF([debug_stream () << $$;], |
3fc16193 | 212 | [fprintf (yyoutput, "%d", $$)])[; |
a5eb1ed2 | 213 | } |
9c66f418 | 214 | input line thing 'x' 'y' |
e3170060 AD |
215 | |
216 | %destructor | |
4c6cc1db | 217 | { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e | 218 | input |
5719c109 | 219 | |
7bd6c77e | 220 | %destructor |
4c6cc1db | 221 | { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e AD |
222 | line |
223 | ||
224 | %destructor | |
4c6cc1db | 225 | { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e AD |
226 | thing |
227 | ||
228 | %destructor | |
4c6cc1db | 229 | { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e | 230 | 'x' |
5719c109 | 231 | |
9c66f418 AD |
232 | %destructor |
233 | { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); } | |
234 | 'y' | |
235 | ||
868d2d96 JD |
236 | %token END 0 |
237 | %destructor | |
238 | { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); } | |
239 | END | |
240 | ||
9280d3ef | 241 | %% |
9c66f418 AD |
242 | /* |
243 | This grammar is made to exercise error recovery. | |
244 | "Lines" starting with `(' support error recovery, with | |
245 | ')' as synchronizing token. Lines starting with 'x' can never | |
246 | be recovered from if in error. | |
247 | */ | |
248 | ||
9280d3ef AD |
249 | input: |
250 | /* Nothing. */ | |
5719c109 AD |
251 | { |
252 | $$ = 0; | |
16f37b35 | 253 | printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$)); |
5719c109 AD |
254 | } |
255 | | line input /* Right recursive to load the stack so that popping at | |
868d2d96 | 256 | END can be exercised. */ |
5719c109 AD |
257 | { |
258 | $$ = 2; | |
16f37b35 | 259 | printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n", |
4c6cc1db | 260 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2)); |
5719c109 | 261 | } |
9280d3ef AD |
262 | ; |
263 | ||
264 | line: | |
265 | thing thing thing ';' | |
5719c109 AD |
266 | { |
267 | $$ = $1; | |
16f37b35 | 268 | printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n", |
4c6cc1db | 269 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), |
16f37b35 | 270 | $3, RANGE (@3), $4, RANGE (@4)); |
5719c109 | 271 | } |
9c66f418 | 272 | | '(' thing thing ')' |
5719c109 AD |
273 | { |
274 | $$ = $1; | |
9c66f418 AD |
275 | printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n", |
276 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), | |
277 | $3, RANGE (@3), $4, RANGE (@4)); | |
5719c109 | 278 | } |
9c66f418 | 279 | | '(' thing ')' |
5719c109 AD |
280 | { |
281 | $$ = $1; | |
9c66f418 AD |
282 | printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n", |
283 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3)); | |
5719c109 | 284 | } |
9c66f418 | 285 | | '(' error ')' |
5719c109 AD |
286 | { |
287 | $$ = -1; | |
9c66f418 AD |
288 | printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n", |
289 | $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3)); | |
5719c109 | 290 | } |
9280d3ef AD |
291 | ; |
292 | ||
293 | thing: | |
5719c109 AD |
294 | 'x' |
295 | { | |
296 | $$ = $1; | |
4c6cc1db AD |
297 | printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n", |
298 | $$, RANGE (@$), $1, RANGE (@1)); | |
5719c109 | 299 | } |
9280d3ef AD |
300 | ; |
301 | %% | |
9c66f418 | 302 | /* Alias to ARGV[1]. */ |
a9739e7c | 303 | const char *source = 0; |
9c66f418 | 304 | |
9280d3ef | 305 | static int |
c2729758 | 306 | yylex (]AT_LEX_FORMALS[) |
9280d3ef | 307 | { |
5a08f1ce | 308 | static unsigned int counter = 0; |
9280d3ef | 309 | |
9c66f418 AD |
310 | int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++; |
311 | /* As in BASIC, line numbers go from 10 to 10. */ | |
c2729758 | 312 | ]AT_LALR1_CC_IF( |
9c66f418 AD |
313 | [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c; |
314 | AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9; | |
c2729758 | 315 | ], |
9c66f418 AD |
316 | [ AT_LOC.first_line = AT_LOC.first_column = 10 * c; |
317 | AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9; | |
c2729758 | 318 | ])[ |
9c66f418 | 319 | |
cf806753 PE |
320 | if (! (0 <= c && c <= strlen (source))) |
321 | abort (); | |
a9739e7c PE |
322 | if (source[c]) |
323 | printf ("sending: '%c'", source[c]); | |
9280d3ef | 324 | else |
868d2d96 | 325 | printf ("sending: END"); |
9c66f418 | 326 | printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[)); |
a9739e7c | 327 | return source[c]; |
9280d3ef AD |
328 | } |
329 | ||
c2729758 | 330 | ]AT_LALR1_CC_IF( |
68e11668 | 331 | [/* A C++ error reporting function. */ |
c2729758 | 332 | void |
99880de5 | 333 | yy::parser::error (const location& l, const std::string& m) |
c2729758 | 334 | { |
efeed023 | 335 | printf ("%d-%d: %s\n", RANGE (l), m.c_str()); |
c2729758 ADL |
336 | } |
337 | ||
338 | static bool yydebug; | |
339 | int | |
340 | yyparse () | |
341 | { | |
99880de5 | 342 | yy::parser parser; |
a3cb6248 | 343 | parser.set_debug_level (yydebug); |
c2729758 ADL |
344 | return parser.parse (); |
345 | } | |
346 | ], | |
347 | [static void | |
9280d3ef AD |
348 | yyerror (const char *msg) |
349 | { | |
4c6cc1db | 350 | printf ("%d-%d: %s\n", RANGE (yylloc), msg); |
c2729758 | 351 | }])[ |
9280d3ef | 352 | |
9280d3ef | 353 | int |
9c66f418 | 354 | main (int argc, const char *argv[]) |
9280d3ef | 355 | { |
6100a9aa | 356 | int status; |
9280d3ef | 357 | yydebug = !!getenv ("YYDEBUG"); |
9c66f418 | 358 | assert (argc == 2); |
a9739e7c | 359 | source = argv[1]; |
6100a9aa PE |
360 | status = yyparse (); |
361 | switch (status) | |
9280d3ef | 362 | { |
6100a9aa PE |
363 | case 0: printf ("Successful parse.\n"); break; |
364 | case 1: printf ("Parsing FAILED.\n"); break; | |
365 | default: printf ("Parsing FAILED (status %d).\n", status); break; | |
9280d3ef | 366 | } |
6100a9aa | 367 | return status; |
9280d3ef AD |
368 | } |
369 | ]]) | |
370 | ||
07971983 PE |
371 | AT_LALR1_CC_IF( |
372 | [AT_CHECK([bison -o input.cc input.y]) | |
373 | AT_COMPILE_CXX([input])], | |
374 | [AT_CHECK([bison -o input.c input.y]) | |
375 | AT_COMPILE([input])]) | |
9c66f418 AD |
376 | |
377 | ||
378 | # Check the location of "empty" | |
379 | # ----------------------------- | |
380 | # I.e., epsilon-reductions, as in "(x)" which ends by reducing | |
381 | # an empty "line" nterm. | |
382 | # FIXME: This location is not satisfying. Depend on the lookahead? | |
383 | AT_PARSER_CHECK([./input '(x)'], 0, | |
384 | [[sending: '(' (0@0-9) | |
385 | sending: 'x' (1@10-19) | |
386 | thing (1@10-19): 'x' (1@10-19) | |
387 | sending: ')' (2@20-29) | |
388 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
868d2d96 | 389 | sending: END (3@30-39) |
b4a20338 AD |
390 | input (0@29-29): /* Nothing */ |
391 | input (2@0-29): line (0@0-29) input (0@29-29) | |
868d2d96 | 392 | Freeing token END (3@30-39) |
258b75ca | 393 | Freeing nterm input (2@0-29) |
9c66f418 AD |
394 | Successful parse. |
395 | ]]) | |
396 | ||
397 | ||
398 | # Check locations in error recovery | |
399 | # --------------------------------- | |
400 | # '(y)' is an error, but can be recovered from. But what's the location | |
401 | # of the error itself ('y'), and of the resulting reduction ('(error)'). | |
402 | AT_PARSER_CHECK([./input '(y)'], 0, | |
403 | [[sending: '(' (0@0-9) | |
404 | sending: 'y' (1@10-19) | |
405 | 10-19: syntax error, unexpected 'y', expecting 'x' | |
406 | Freeing token 'y' (1@10-19) | |
407 | sending: ')' (2@20-29) | |
408 | line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29) | |
868d2d96 | 409 | sending: END (3@30-39) |
b4a20338 AD |
410 | input (0@29-29): /* Nothing */ |
411 | input (2@0-29): line (-1@0-29) input (0@29-29) | |
868d2d96 | 412 | Freeing token END (3@30-39) |
258b75ca | 413 | Freeing nterm input (2@0-29) |
9c66f418 AD |
414 | Successful parse. |
415 | ]]) | |
416 | ||
417 | ||
418 | # Syntax errors caught by the parser | |
419 | # ---------------------------------- | |
420 | # Exercise the discarding of stack top and input until `error' | |
421 | # can be reduced. | |
422 | # | |
423 | # '(', 'x', 'x', 'x', 'x', 'x', ')', | |
424 | # | |
425 | # Load the stack and provoke an error that cannot be caught by the | |
426 | # grammar, to check that the stack is cleared. And make sure the | |
427 | # lookahead is freed. | |
428 | # | |
429 | # '(', 'x', ')', | |
430 | # '(', 'x', ')', | |
431 | # 'y' | |
432 | AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1, | |
433 | [[sending: '(' (0@0-9) | |
4c6cc1db AD |
434 | sending: 'x' (1@10-19) |
435 | thing (1@10-19): 'x' (1@10-19) | |
436 | sending: 'x' (2@20-29) | |
437 | thing (2@20-29): 'x' (2@20-29) | |
438 | sending: 'x' (3@30-39) | |
9c66f418 | 439 | 30-39: syntax error, unexpected 'x', expecting ')' |
4c6cc1db AD |
440 | Freeing nterm thing (2@20-29) |
441 | Freeing nterm thing (1@10-19) | |
4c6cc1db AD |
442 | Freeing token 'x' (3@30-39) |
443 | sending: 'x' (4@40-49) | |
444 | Freeing token 'x' (4@40-49) | |
445 | sending: 'x' (5@50-59) | |
446 | Freeing token 'x' (5@50-59) | |
9c66f418 AD |
447 | sending: ')' (6@60-69) |
448 | line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69) | |
449 | sending: '(' (7@70-79) | |
4c6cc1db AD |
450 | sending: 'x' (8@80-89) |
451 | thing (8@80-89): 'x' (8@80-89) | |
9c66f418 AD |
452 | sending: ')' (9@90-99) |
453 | line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99) | |
454 | sending: '(' (10@100-109) | |
455 | sending: 'x' (11@110-119) | |
456 | thing (11@110-119): 'x' (11@110-119) | |
457 | sending: ')' (12@120-129) | |
458 | line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129) | |
459 | sending: 'y' (13@130-139) | |
b4a20338 AD |
460 | input (0@129-129): /* Nothing */ |
461 | input (2@100-129): line (10@100-129) input (0@129-129) | |
9c66f418 AD |
462 | input (2@70-129): line (7@70-99) input (2@100-129) |
463 | input (2@0-129): line (-1@0-69) input (2@70-129) | |
868d2d96 | 464 | 130-139: syntax error, unexpected 'y', expecting END |
9c66f418 AD |
465 | Freeing nterm input (2@0-129) |
466 | Freeing token 'y' (13@130-139) | |
5719c109 | 467 | Parsing FAILED. |
9280d3ef AD |
468 | ]]) |
469 | ||
868d2d96 JD |
470 | |
471 | # Syntax error caught by the parser where lookahead = END | |
472 | # -------------------------------------------------------- | |
473 | # Load the stack and provoke an error that cannot be caught by the | |
474 | # grammar, to check that the stack is cleared. And make sure the | |
475 | # lookahead is freed. | |
476 | # | |
477 | # '(', 'x', ')', | |
478 | # '(', 'x', ')', | |
479 | # 'x' | |
480 | AT_PARSER_CHECK([./input '(x)(x)x'], 1, | |
481 | [[sending: '(' (0@0-9) | |
482 | sending: 'x' (1@10-19) | |
483 | thing (1@10-19): 'x' (1@10-19) | |
484 | sending: ')' (2@20-29) | |
485 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
486 | sending: '(' (3@30-39) | |
487 | sending: 'x' (4@40-49) | |
488 | thing (4@40-49): 'x' (4@40-49) | |
489 | sending: ')' (5@50-59) | |
490 | line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59) | |
491 | sending: 'x' (6@60-69) | |
492 | thing (6@60-69): 'x' (6@60-69) | |
493 | sending: END (7@70-79) | |
494 | 70-79: syntax error, unexpected END, expecting 'x' | |
495 | Freeing nterm thing (6@60-69) | |
496 | Freeing nterm line (3@30-59) | |
497 | Freeing nterm line (0@0-29) | |
498 | Freeing token END (7@70-79) | |
499 | Parsing FAILED. | |
500 | ]]) | |
501 | ||
502 | ||
80ce3401 PE |
503 | # Check destruction upon stack overflow |
504 | # ------------------------------------- | |
505 | # Upon stack overflow, all symbols on the stack should be destroyed. | |
506 | # Only check for yacc.c. | |
507 | AT_YACC_IF([ | |
6100a9aa | 508 | AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2, |
80ce3401 PE |
509 | [[sending: '(' (0@0-9) |
510 | sending: 'x' (1@10-19) | |
511 | thing (1@10-19): 'x' (1@10-19) | |
512 | sending: ')' (2@20-29) | |
513 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
514 | sending: '(' (3@30-39) | |
515 | sending: 'x' (4@40-49) | |
516 | thing (4@40-49): 'x' (4@40-49) | |
517 | sending: ')' (5@50-59) | |
518 | line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59) | |
519 | sending: '(' (6@60-69) | |
520 | sending: 'x' (7@70-79) | |
521 | thing (7@70-79): 'x' (7@70-79) | |
522 | sending: ')' (8@80-89) | |
523 | line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89) | |
524 | sending: '(' (9@90-99) | |
525 | sending: 'x' (10@100-109) | |
526 | thing (10@100-109): 'x' (10@100-109) | |
527 | sending: ')' (11@110-119) | |
528 | line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119) | |
529 | sending: '(' (12@120-129) | |
530 | sending: 'x' (13@130-139) | |
531 | thing (13@130-139): 'x' (13@130-139) | |
532 | sending: ')' (14@140-149) | |
533 | line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149) | |
534 | sending: '(' (15@150-159) | |
535 | sending: 'x' (16@160-169) | |
536 | thing (16@160-169): 'x' (16@160-169) | |
537 | sending: ')' (17@170-179) | |
538 | line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179) | |
539 | sending: '(' (18@180-189) | |
540 | sending: 'x' (19@190-199) | |
541 | thing (19@190-199): 'x' (19@190-199) | |
542 | sending: ')' (20@200-209) | |
1a059451 | 543 | 200-209: memory exhausted |
80ce3401 PE |
544 | Freeing nterm thing (19@190-199) |
545 | Freeing nterm line (15@150-179) | |
546 | Freeing nterm line (12@120-149) | |
547 | Freeing nterm line (9@90-119) | |
548 | Freeing nterm line (6@60-89) | |
549 | Freeing nterm line (3@30-59) | |
550 | Freeing nterm line (0@0-29) | |
6100a9aa | 551 | Parsing FAILED (status 2). |
80ce3401 PE |
552 | ]]) |
553 | ]) | |
554 | ||
3df37415 AD |
555 | ]) |
556 | ||
557 | ||
a14a26fa | 558 | # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG]) |
046ac74e | 559 | # --------------------------------------------------------------------------- |
3df37415 | 560 | m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR], |
9c66f418 AD |
561 | [AT_SETUP([Printers and Destructors $2: $1]) |
562 | ||
a14a26fa | 563 | $3 |
9c66f418 AD |
564 | _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4], |
565 | [%error-verbose | |
566 | %debug | |
567 | %verbose | |
568 | %locations | |
569 | $1], [$2]) | |
570 | ||
571 | AT_CLEANUP | |
3df37415 AD |
572 | ]) |
573 | ||
574 | ||
ac700aa6 PE |
575 | AT_CHECK_PRINTER_AND_DESTRUCTOR([]) |
576 | AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union]) | |
046ac74e | 577 | |
fd19f271 AD |
578 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"]) |
579 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union]) | |
046ac74e | 580 | |
1576d44d AD |
581 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser]) |
582 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union]) | |
ec5479ce JD |
583 | |
584 | ||
585 | ||
12e35840 JD |
586 | ## ----------------------------------------- ## |
587 | ## Default tagless %printer and %destructor. ## | |
588 | ## ----------------------------------------- ## | |
ec5479ce JD |
589 | |
590 | # Check that the right %printer and %destructor are called, that they're not | |
591 | # called for $end, and that $$ and @$ work correctly. | |
592 | ||
12e35840 | 593 | AT_SETUP([Default tagless %printer and %destructor]) |
ec5479ce JD |
594 | |
595 | AT_DATA_GRAMMAR([[input.y]], | |
596 | [[%error-verbose | |
597 | %debug | |
598 | %locations | |
599 | %initial-action { | |
600 | @$.first_line = @$.last_line = 1; | |
601 | @$.first_column = @$.last_column = 1; | |
602 | } | |
603 | ||
604 | %{ | |
605 | # include <stdio.h> | |
606 | # include <stdlib.h> | |
607 | static void yyerror (const char *msg); | |
608 | static int yylex (void); | |
609 | # define USE(SYM) | |
610 | %} | |
611 | ||
612 | %printer { | |
12e35840 JD |
613 | fprintf (yyoutput, "<*> printer should not be called.\n"); |
614 | } <*> | |
615 | ||
616 | %printer { | |
3ebecc24 JD |
617 | fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column); |
618 | } <> | |
ec5479ce | 619 | %destructor { |
3ebecc24 JD |
620 | fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column); |
621 | } <> | |
ec5479ce JD |
622 | |
623 | %printer { | |
624 | fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column); | |
625 | } 'b' 'c' | |
626 | %destructor { | |
627 | fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column); | |
628 | } 'b' 'c' | |
629 | ||
12e35840 JD |
630 | %destructor { |
631 | fprintf (yyoutput, "<*> destructor should not be called.\n"); | |
632 | } <*> | |
633 | ||
ec5479ce JD |
634 | %% |
635 | ||
636 | start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ; | |
637 | ||
638 | %% | |
639 | ||
640 | static int | |
641 | yylex (void) | |
642 | { | |
cf806753 PE |
643 | static char const input[] = "abcd"; |
644 | static size_t toknum; | |
645 | if (! (toknum < sizeof input)) | |
646 | abort (); | |
647 | yylval = input[toknum++]; | |
ec5479ce | 648 | yylloc.first_line = yylloc.last_line = 1; |
cf806753 | 649 | yylloc.first_column = yylloc.last_column = toknum; |
ec5479ce JD |
650 | return yylval; |
651 | } | |
652 | ||
653 | static void | |
654 | yyerror (const char *msg) | |
655 | { | |
656 | fprintf (stderr, "%s\n", msg); | |
657 | } | |
658 | ||
659 | int | |
660 | main (void) | |
661 | { | |
662 | yydebug = 1; | |
663 | return yyparse (); | |
664 | } | |
665 | ]]) | |
666 | ||
667 | AT_CHECK([bison -o input.c input.y]) | |
668 | AT_COMPILE([input]) | |
669 | AT_PARSER_CHECK([./input], 1, | |
3ebecc24 | 670 | [[<> destructor for 'd' @ 4. |
ec5479ce JD |
671 | 'b'/'c' destructor for 'c' @ 3. |
672 | 'b'/'c' destructor for 'b' @ 2. | |
3ebecc24 | 673 | <> destructor for 'a' @ 1. |
ec5479ce JD |
674 | ]], |
675 | [[Starting parse | |
676 | Entering state 0 | |
3ebecc24 JD |
677 | Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1) |
678 | Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1) | |
ec5479ce JD |
679 | Entering state 1 |
680 | Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) | |
681 | Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) | |
682 | Entering state 3 | |
683 | Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) | |
684 | Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) | |
685 | Entering state 5 | |
3ebecc24 JD |
686 | Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4) |
687 | Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4) | |
ec5479ce JD |
688 | Entering state 6 |
689 | Reading a token: Now at end of input. | |
690 | syntax error, unexpected $end, expecting 'e' | |
3ebecc24 | 691 | Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4) |
ec5479ce JD |
692 | Stack now 0 1 3 5 |
693 | Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) | |
694 | Stack now 0 1 3 | |
695 | Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) | |
696 | Stack now 0 1 | |
3ebecc24 | 697 | Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1) |
ec5479ce JD |
698 | Stack now 0 |
699 | Cleanup: discarding lookahead token $end (1.5-1.5: ) | |
700 | Stack now 0 | |
701 | ]]) | |
702 | ||
703 | AT_CLEANUP | |
704 | ||
705 | ||
706 | ||
12e35840 JD |
707 | ## ------------------------------------------------------ ## |
708 | ## Default tagged and per-type %printer and %destructor. ## | |
709 | ## ------------------------------------------------------ ## | |
b2a0b7ca | 710 | |
12e35840 | 711 | AT_SETUP([Default tagged and per-type %printer and %destructor]) |
b2a0b7ca JD |
712 | |
713 | AT_DATA_GRAMMAR([[input.y]], | |
714 | [[%error-verbose | |
715 | %debug | |
716 | ||
717 | %{ | |
718 | # include <stdio.h> | |
719 | # include <stdlib.h> | |
720 | static void yyerror (const char *msg); | |
721 | static int yylex (void); | |
722 | # define USE(SYM) | |
723 | %} | |
724 | ||
12e35840 | 725 | %printer { |
3ebecc24 JD |
726 | fprintf (yyoutput, "<> printer should not be called.\n"); |
727 | } <> | |
12e35840 | 728 | |
b2a0b7ca JD |
729 | %union { int field0; int field1; int field2; } |
730 | %type <field0> start 'a' 'g' | |
731 | %type <field1> 'e' | |
732 | %type <field2> 'f' | |
733 | %printer { | |
12e35840 JD |
734 | fprintf (yyoutput, "<*>/<field2>/e printer"); |
735 | } <*> 'e' <field2> | |
b2a0b7ca | 736 | %destructor { |
12e35840 JD |
737 | fprintf (stdout, "<*>/<field2>/e destructor.\n"); |
738 | } <*> 'e' <field2> | |
b2a0b7ca JD |
739 | |
740 | %type <field1> 'b' | |
741 | %printer { fprintf (yyoutput, "<field1> printer"); } <field1> | |
742 | %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1> | |
743 | ||
744 | %type <field0> 'c' | |
745 | %printer { fprintf (yyoutput, "'c' printer"); } 'c' | |
746 | %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c' | |
747 | ||
748 | %type <field1> 'd' | |
749 | %printer { fprintf (yyoutput, "'d' printer"); } 'd' | |
750 | %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd' | |
751 | ||
12e35840 | 752 | %destructor { |
3ebecc24 JD |
753 | fprintf (yyoutput, "<> destructor should not be called.\n"); |
754 | } <> | |
12e35840 | 755 | |
b2a0b7ca JD |
756 | %% |
757 | ||
758 | start: | |
759 | 'a' 'b' 'c' 'd' 'e' 'f' 'g' | |
760 | { | |
761 | USE(($1, $2, $3, $4, $5, $6, $7)); | |
762 | $$ = 'S'; | |
763 | } | |
764 | ; | |
765 | ||
766 | %% | |
767 | ||
768 | static int | |
769 | yylex (void) | |
770 | { | |
cf806753 PE |
771 | static char const input[] = "abcdef"; |
772 | static size_t toknum; | |
773 | if (! (toknum < sizeof input)) | |
774 | abort (); | |
775 | return input[toknum++]; | |
b2a0b7ca JD |
776 | } |
777 | ||
778 | static void | |
779 | yyerror (const char *msg) | |
780 | { | |
781 | fprintf (stderr, "%s\n", msg); | |
782 | } | |
783 | ||
784 | int | |
785 | main (void) | |
786 | { | |
787 | yydebug = 1; | |
788 | return yyparse (); | |
789 | } | |
790 | ]]) | |
791 | ||
792 | AT_CHECK([bison -o input.c input.y]) | |
793 | AT_COMPILE([input]) | |
794 | AT_PARSER_CHECK([./input], 1, | |
12e35840 JD |
795 | [[<*>/<field2>/e destructor. |
796 | <*>/<field2>/e destructor. | |
b2a0b7ca JD |
797 | 'd' destructor. |
798 | 'c' destructor. | |
799 | <field1> destructor. | |
12e35840 | 800 | <*>/<field2>/e destructor. |
b2a0b7ca JD |
801 | ]], |
802 | [[Starting parse | |
803 | Entering state 0 | |
12e35840 JD |
804 | Reading a token: Next token is token 'a' (<*>/<field2>/e printer) |
805 | Shifting token 'a' (<*>/<field2>/e printer) | |
b2a0b7ca JD |
806 | Entering state 1 |
807 | Reading a token: Next token is token 'b' (<field1> printer) | |
808 | Shifting token 'b' (<field1> printer) | |
809 | Entering state 3 | |
810 | Reading a token: Next token is token 'c' ('c' printer) | |
811 | Shifting token 'c' ('c' printer) | |
812 | Entering state 5 | |
813 | Reading a token: Next token is token 'd' ('d' printer) | |
814 | Shifting token 'd' ('d' printer) | |
815 | Entering state 6 | |
12e35840 JD |
816 | Reading a token: Next token is token 'e' (<*>/<field2>/e printer) |
817 | Shifting token 'e' (<*>/<field2>/e printer) | |
b2a0b7ca | 818 | Entering state 7 |
12e35840 JD |
819 | Reading a token: Next token is token 'f' (<*>/<field2>/e printer) |
820 | Shifting token 'f' (<*>/<field2>/e printer) | |
b2a0b7ca JD |
821 | Entering state 8 |
822 | Reading a token: Now at end of input. | |
823 | syntax error, unexpected $end, expecting 'g' | |
12e35840 | 824 | Error: popping token 'f' (<*>/<field2>/e printer) |
b2a0b7ca | 825 | Stack now 0 1 3 5 6 7 |
12e35840 | 826 | Error: popping token 'e' (<*>/<field2>/e printer) |
b2a0b7ca JD |
827 | Stack now 0 1 3 5 6 |
828 | Error: popping token 'd' ('d' printer) | |
829 | Stack now 0 1 3 5 | |
830 | Error: popping token 'c' ('c' printer) | |
831 | Stack now 0 1 3 | |
832 | Error: popping token 'b' (<field1> printer) | |
833 | Stack now 0 1 | |
12e35840 | 834 | Error: popping token 'a' (<*>/<field2>/e printer) |
b2a0b7ca JD |
835 | Stack now 0 |
836 | Cleanup: discarding lookahead token $end () | |
837 | Stack now 0 | |
838 | ]]) | |
839 | ||
840 | AT_CLEANUP | |
841 | ||
842 | ||
843 | ||
ec5479ce | 844 | ## ------------------------------------------------------------- ## |
12e35840 | 845 | ## Default %printer and %destructor for user-defined end token. ## |
ec5479ce JD |
846 | ## ------------------------------------------------------------- ## |
847 | ||
3508ce36 | 848 | AT_SETUP([Default %printer and %destructor for user-defined end token]) |
ec5479ce | 849 | |
12e35840 JD |
850 | # _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED) |
851 | # ----------------------------------------------------------------------------- | |
852 | m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN], | |
853 | [m4_if($1, 0, | |
3ebecc24 JD |
854 | [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])], |
855 | [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])]) | |
12e35840 JD |
856 | |
857 | AT_DATA_GRAMMAR([[input]]$1[[.y]], | |
ec5479ce JD |
858 | [[%error-verbose |
859 | %debug | |
860 | %locations | |
861 | %initial-action { | |
862 | @$.first_line = @$.last_line = 1; | |
863 | @$.first_column = @$.last_column = 1; | |
864 | } | |
865 | ||
866 | %{ | |
867 | # include <stdio.h> | |
868 | # include <stdlib.h> | |
869 | static void yyerror (const char *msg); | |
870 | static int yylex (void); | |
871 | # define USE(SYM) | |
872 | %} | |
873 | ||
12e35840 JD |
874 | %destructor { |
875 | fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n"); | |
876 | } <]]not_kind[[> | |
877 | ||
ec5479ce JD |
878 | %token END 0 |
879 | %printer { | |
12e35840 JD |
880 | fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column); |
881 | } <]]kind[[> | |
ec5479ce | 882 | %destructor { |
12e35840 JD |
883 | fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column); |
884 | } <]]kind[[> | |
885 | ||
886 | %printer { | |
887 | fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n"); | |
888 | } <]]not_kind[[> | |
889 | ||
890 | ]]m4_if($1, 0, [[[ | |
891 | ]]], | |
892 | [[[%union { char tag; } | |
893 | %type <tag> start END]]])[[ | |
ec5479ce JD |
894 | |
895 | %% | |
896 | ||
897 | start: { $$ = 'S'; } ; | |
898 | ||
899 | %% | |
900 | ||
901 | static int | |
902 | yylex (void) | |
903 | { | |
cf806753 PE |
904 | static int called; |
905 | if (called++) | |
906 | abort (); | |
12e35840 | 907 | yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E'; |
ec5479ce JD |
908 | yylloc.first_line = yylloc.last_line = 1; |
909 | yylloc.first_column = yylloc.last_column = 1; | |
910 | return 0; | |
911 | } | |
912 | ||
913 | static void | |
914 | yyerror (const char *msg) | |
915 | { | |
916 | fprintf (stderr, "%s\n", msg); | |
917 | } | |
918 | ||
919 | int | |
920 | main (void) | |
921 | { | |
922 | yydebug = 1; | |
923 | return yyparse (); | |
924 | } | |
925 | ]]) | |
926 | ||
12e35840 JD |
927 | AT_CHECK([bison -o input$1.c input$1.y]) |
928 | AT_COMPILE([input$1]) | |
929 | AT_PARSER_CHECK([./input$1], 0, | |
930 | [[<]]kind[[> for 'E' @ 1. | |
931 | <]]kind[[> for 'S' @ 1. | |
ec5479ce JD |
932 | ]], |
933 | [[Starting parse | |
934 | Entering state 0 | |
12e35840 JD |
935 | Reducing stack by rule 1 (line 46): |
936 | -> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1) | |
ec5479ce JD |
937 | Stack now 0 |
938 | Entering state 1 | |
939 | Reading a token: Now at end of input. | |
12e35840 | 940 | Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1) |
ec5479ce JD |
941 | Entering state 2 |
942 | Stack now 0 1 2 | |
12e35840 JD |
943 | Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1) |
944 | Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1) | |
ec5479ce JD |
945 | ]]) |
946 | ||
12e35840 JD |
947 | m4_popdef([kind]) |
948 | m4_popdef([not_kind]) | |
949 | ]) | |
950 | ||
951 | _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0) | |
952 | _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1) | |
953 | ||
ec5479ce | 954 | AT_CLEANUP |
9350499c JD |
955 | |
956 | ||
957 | ||
958 | ## ------------------------------------------------------------------ ## | |
959 | ## Default %printer and %destructor are not for error or $undefined. ## | |
960 | ## ------------------------------------------------------------------ ## | |
961 | ||
287b314e | 962 | AT_SETUP([Default %printer and %destructor are not for error or $undefined]) |
9350499c JD |
963 | |
964 | # If Bison were to apply the default %printer and %destructor to the error | |
965 | # token or to $undefined: | |
966 | # - For the error token: | |
967 | # - It would generate warnings for unused $n. | |
968 | # - It would invoke the %printer and %destructor on the error token's | |
969 | # semantic value, which would be initialized from the lookahead, which | |
970 | # would be destroyed separately. | |
971 | # - For $undefined, who knows what the semantic value would be. | |
972 | ||
973 | AT_DATA_GRAMMAR([[input.y]], | |
974 | [[%debug | |
975 | ||
976 | %{ | |
977 | # include <stdio.h> | |
cf806753 | 978 | # include <stdlib.h> |
9350499c JD |
979 | static void yyerror (const char *msg); |
980 | static int yylex (void); | |
981 | # define USE(SYM) | |
982 | %} | |
983 | ||
984 | %printer { | |
985 | fprintf (yyoutput, "'%c'", $$); | |
3ebecc24 | 986 | } <> <*> |
9350499c JD |
987 | %destructor { |
988 | fprintf (stderr, "DESTROY '%c'\n", $$); | |
3ebecc24 | 989 | } <> <*> |
9350499c JD |
990 | |
991 | %% | |
992 | ||
993 | start: | |
994 | { $$ = 'S'; } | |
995 | /* In order to reveal the problems that this bug caused during parsing, add | |
996 | * $2 to USE. */ | |
997 | | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; } | |
998 | ; | |
999 | ||
1000 | %% | |
1001 | ||
1002 | static int | |
1003 | yylex (void) | |
1004 | { | |
cf806753 PE |
1005 | static char const input[] = "abd"; |
1006 | static size_t toknum; | |
1007 | if (! (toknum < sizeof input)) | |
1008 | abort (); | |
1009 | yylval = input[toknum++]; | |
9350499c JD |
1010 | return yylval; |
1011 | } | |
1012 | ||
1013 | static void | |
1014 | yyerror (const char *msg) | |
1015 | { | |
1016 | fprintf (stderr, "%s\n", msg); | |
1017 | } | |
1018 | ||
1019 | int | |
1020 | main (void) | |
1021 | { | |
1022 | yydebug = 1; | |
1023 | return yyparse (); | |
1024 | } | |
1025 | ]]) | |
1026 | ||
1027 | AT_CHECK([bison -o input.c input.y]) | |
1028 | AT_COMPILE([input]) | |
1029 | AT_PARSER_CHECK([./input], [1], [], | |
1030 | [[Starting parse | |
1031 | Entering state 0 | |
1032 | Reading a token: Next token is token 'a' ('a') | |
1033 | Shifting token 'a' ('a') | |
1034 | Entering state 1 | |
1035 | Reading a token: Next token is token 'b' ('b') | |
1036 | syntax error | |
1037 | Shifting token error () | |
1038 | Entering state 3 | |
1039 | Next token is token 'b' ('b') | |
1040 | Shifting token 'b' ('b') | |
1041 | Entering state 5 | |
1042 | Reading a token: Next token is token $undefined () | |
1043 | Error: popping token 'b' ('b') | |
1044 | DESTROY 'b' | |
1045 | Stack now 0 1 3 | |
1046 | Error: popping token error () | |
1047 | Stack now 0 1 | |
1048 | Shifting token error () | |
1049 | Entering state 3 | |
1050 | Next token is token $undefined () | |
1051 | Error: discarding token $undefined () | |
1052 | Error: popping token error () | |
1053 | Stack now 0 1 | |
1054 | Shifting token error () | |
1055 | Entering state 3 | |
1056 | Reading a token: Now at end of input. | |
1057 | Cleanup: discarding lookahead token $end () | |
1058 | Stack now 0 1 3 | |
1059 | Cleanup: popping token error () | |
1060 | Cleanup: popping token 'a' ('a') | |
1061 | DESTROY 'a' | |
1062 | ]]) | |
1063 | ||
1064 | AT_CLEANUP | |
1065 | ||
1066 | ||
1067 | ||
1068 | ## ------------------------------------------------------ ## | |
1069 | ## Default %printer and %destructor are not for $accept. ## | |
1070 | ## ------------------------------------------------------ ## | |
1071 | ||
287b314e | 1072 | AT_SETUP([Default %printer and %destructor are not for $accept]) |
9350499c JD |
1073 | |
1074 | # If YYSTYPE is a union and Bison were to apply the default %printer and | |
1075 | # %destructor to $accept: | |
1076 | # - The %printer and %destructor code generated for $accept would always be | |
1077 | # dead code because $accept is currently never shifted onto the stack. | |
1078 | # - $$ for $accept would always be of type YYSTYPE because it's not possible | |
1079 | # to declare `%type <field> $accept'. (Also true for $undefined.) | |
1080 | # - Thus, the compiler might complain that the user code assumes the wrong | |
1081 | # type for $$ since the code might assume the type associated with a | |
1082 | # specific union field, which is especially reasonable in C++ since that | |
1083 | # type may be a base type. This test case checks for this problem. (Also | |
1084 | # true for $undefined and the error token, so there are three warnings for | |
1085 | # %printer and three for %destructor.) | |
1086 | ||
1087 | AT_DATA_GRAMMAR([[input.y]], | |
1088 | [[%debug /* So that %printer is actually compiled. */ | |
1089 | ||
1090 | %{ | |
1091 | # include <stdio.h> | |
cf806753 | 1092 | # include <stdlib.h> |
9350499c JD |
1093 | static void yyerror (const char *msg); |
1094 | static int yylex (void); | |
1095 | # define USE(SYM) | |
1096 | %} | |
1097 | ||
1098 | %printer { | |
1099 | char chr = $$; | |
1100 | fprintf (yyoutput, "'%c'", chr); | |
3ebecc24 | 1101 | } <> <*> |
9350499c JD |
1102 | %destructor { |
1103 | char chr = $$; | |
1104 | fprintf (stderr, "DESTROY '%c'\n", chr); | |
3ebecc24 | 1105 | } <> <*> |
9350499c JD |
1106 | |
1107 | %union { char chr; } | |
1108 | %type <chr> start | |
1109 | ||
1110 | %% | |
1111 | ||
1112 | start: { USE($$); } ; | |
1113 | ||
1114 | %% | |
1115 | ||
1116 | static int | |
1117 | yylex (void) | |
1118 | { | |
cf806753 PE |
1119 | static int called; |
1120 | if (called++) | |
1121 | abort (); | |
9350499c JD |
1122 | return 0; |
1123 | } | |
1124 | ||
1125 | static void | |
1126 | yyerror (const char *msg) | |
1127 | { | |
1128 | fprintf (stderr, "%s\n", msg); | |
1129 | } | |
1130 | ||
1131 | int | |
1132 | main (void) | |
1133 | { | |
1134 | return yyparse (); | |
1135 | } | |
1136 | ]]) | |
1137 | ||
1138 | AT_CHECK([bison -o input.c input.y]) | |
1139 | AT_COMPILE([input]) | |
1140 | ||
1141 | AT_CLEANUP | |
f91b1629 JD |
1142 | |
1143 | ||
1144 | ||
1145 | ## ------------------------------------------------------ ## | |
1146 | ## Default %printer and %destructor for mid-rule values. ## | |
1147 | ## ------------------------------------------------------ ## | |
1148 | ||
1149 | AT_SETUP([Default %printer and %destructor for mid-rule values]) | |
1150 | ||
1151 | AT_DATA_GRAMMAR([[input.y]], | |
1152 | [[%debug /* So that %printer is actually compiled. */ | |
1153 | ||
1154 | %{ | |
1155 | # include <stdio.h> | |
1156 | # include <stdlib.h> | |
1157 | static void yyerror (const char *msg); | |
1158 | static int yylex (void); | |
1159 | # define USE(SYM) | |
1160 | # define YYLTYPE int | |
1161 | # define YYLLOC_DEFAULT(Current, Rhs, N) | |
1162 | # define YY_LOCATION_PRINT(File, Loc) | |
1163 | %} | |
1164 | ||
3ebecc24 JD |
1165 | %printer { fprintf (yyoutput, "%d", @$); } <> |
1166 | %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <> | |
12e35840 JD |
1167 | %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*> |
1168 | %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*> | |
f91b1629 JD |
1169 | |
1170 | %% | |
1171 | ||
1172 | start: | |
1173 | { @$ = 1; } // Not set or used. | |
1174 | { USE ($$); @$ = 2; } // Both set and used. | |
1175 | { USE ($$); @$ = 3; } // Only set. | |
1176 | { @$ = 4; } // Only used. | |
1177 | 'c' | |
1178 | { USE (($$, $2, $4, $5)); @$ = 0; } | |
1179 | ; | |
1180 | ||
1181 | %% | |
1182 | ||
1183 | static int | |
1184 | yylex (void) | |
1185 | { | |
1186 | static int called; | |
1187 | if (called++) | |
1188 | abort (); | |
1189 | return 0; | |
1190 | } | |
1191 | ||
1192 | static void | |
1193 | yyerror (const char *msg) | |
1194 | { | |
1195 | fprintf (stderr, "%s\n", msg); | |
1196 | } | |
1197 | ||
1198 | int | |
1199 | main (void) | |
1200 | { | |
1201 | yydebug = 1; | |
1202 | return yyparse (); | |
1203 | } | |
1204 | ]]) | |
1205 | ||
1206 | AT_CHECK([bison -o input.c input.y], 0,, | |
12e35840 JD |
1207 | [[input.y:33.3-23: warning: unset value: $$ |
1208 | input.y:30.3-35.37: warning: unused value: $3 | |
f91b1629 JD |
1209 | ]]) |
1210 | ||
1211 | AT_COMPILE([input]) | |
1212 | AT_PARSER_CHECK([./input], 1,, | |
1213 | [[Starting parse | |
1214 | Entering state 0 | |
12e35840 | 1215 | Reducing stack by rule 1 (line 30): |
f91b1629 JD |
1216 | -> $$ = nterm $@1 (: ) |
1217 | Stack now 0 | |
1218 | Entering state 2 | |
12e35840 | 1219 | Reducing stack by rule 2 (line 31): |
f91b1629 JD |
1220 | -> $$ = nterm @2 (: 2) |
1221 | Stack now 0 2 | |
1222 | Entering state 4 | |
12e35840 | 1223 | Reducing stack by rule 3 (line 32): |
f91b1629 JD |
1224 | -> $$ = nterm @3 (: 3) |
1225 | Stack now 0 2 4 | |
1226 | Entering state 5 | |
12e35840 | 1227 | Reducing stack by rule 4 (line 33): |
f91b1629 JD |
1228 | -> $$ = nterm @4 (: 4) |
1229 | Stack now 0 2 4 5 | |
1230 | Entering state 6 | |
1231 | Reading a token: Now at end of input. | |
1232 | syntax error | |
1233 | Error: popping nterm @4 (: 4) | |
1234 | DESTROY 4 | |
1235 | Stack now 0 2 4 5 | |
1236 | Error: popping nterm @3 (: 3) | |
1237 | DESTROY 3 | |
1238 | Stack now 0 2 4 | |
1239 | Error: popping nterm @2 (: 2) | |
1240 | DESTROY 2 | |
1241 | Stack now 0 2 | |
1242 | Error: popping nterm $@1 (: ) | |
1243 | Stack now 0 | |
1244 | Cleanup: discarding lookahead token $end (: ) | |
1245 | Stack now 0 | |
1246 | ]]) | |
1247 | ||
1248 | AT_CLEANUP |