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