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