]>
Commit | Line | Data |
---|---|---|
82c035a8 | 1 | # Executing Actions. -*- Autotest -*- |
6e30ede8 PE |
2 | |
3 | # Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, | |
4 | # 2010 Free Software Foundation, Inc. | |
82c035a8 | 5 | |
f16b0819 | 6 | # This program is free software: you can redistribute it and/or modify |
82c035a8 | 7 | # it under the terms of the GNU General Public License as published by |
f16b0819 PE |
8 | # the Free Software Foundation, either version 3 of the License, or |
9 | # (at your option) any later version. | |
10 | # | |
82c035a8 AD |
11 | # This program is distributed in the hope that it will be useful, |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU General Public License for more details. | |
f16b0819 | 15 | # |
82c035a8 | 16 | # You should have received a copy of the GNU General Public License |
f16b0819 | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
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 | ||
da730230 | 78 | AT_BISON_CHECK([-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 | ||
da730230 | 156 | AT_BISON_CHECK([-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 | ||
11c4e57d | 371 | AT_FULL_COMPILE([input]) |
9c66f418 AD |
372 | |
373 | ||
374 | # Check the location of "empty" | |
375 | # ----------------------------- | |
376 | # I.e., epsilon-reductions, as in "(x)" which ends by reducing | |
377 | # an empty "line" nterm. | |
378 | # FIXME: This location is not satisfying. Depend on the lookahead? | |
379 | AT_PARSER_CHECK([./input '(x)'], 0, | |
380 | [[sending: '(' (0@0-9) | |
381 | sending: 'x' (1@10-19) | |
382 | thing (1@10-19): 'x' (1@10-19) | |
383 | sending: ')' (2@20-29) | |
384 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
868d2d96 | 385 | sending: END (3@30-39) |
b4a20338 AD |
386 | input (0@29-29): /* Nothing */ |
387 | input (2@0-29): line (0@0-29) input (0@29-29) | |
868d2d96 | 388 | Freeing token END (3@30-39) |
258b75ca | 389 | Freeing nterm input (2@0-29) |
9c66f418 AD |
390 | Successful parse. |
391 | ]]) | |
392 | ||
393 | ||
394 | # Check locations in error recovery | |
395 | # --------------------------------- | |
396 | # '(y)' is an error, but can be recovered from. But what's the location | |
397 | # of the error itself ('y'), and of the resulting reduction ('(error)'). | |
398 | AT_PARSER_CHECK([./input '(y)'], 0, | |
399 | [[sending: '(' (0@0-9) | |
400 | sending: 'y' (1@10-19) | |
401 | 10-19: syntax error, unexpected 'y', expecting 'x' | |
402 | Freeing token 'y' (1@10-19) | |
403 | sending: ')' (2@20-29) | |
404 | line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29) | |
868d2d96 | 405 | sending: END (3@30-39) |
b4a20338 AD |
406 | input (0@29-29): /* Nothing */ |
407 | input (2@0-29): line (-1@0-29) input (0@29-29) | |
868d2d96 | 408 | Freeing token END (3@30-39) |
258b75ca | 409 | Freeing nterm input (2@0-29) |
9c66f418 AD |
410 | Successful parse. |
411 | ]]) | |
412 | ||
413 | ||
414 | # Syntax errors caught by the parser | |
415 | # ---------------------------------- | |
416 | # Exercise the discarding of stack top and input until `error' | |
417 | # can be reduced. | |
418 | # | |
419 | # '(', 'x', 'x', 'x', 'x', 'x', ')', | |
420 | # | |
421 | # Load the stack and provoke an error that cannot be caught by the | |
422 | # grammar, to check that the stack is cleared. And make sure the | |
423 | # lookahead is freed. | |
424 | # | |
425 | # '(', 'x', ')', | |
426 | # '(', 'x', ')', | |
427 | # 'y' | |
428 | AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1, | |
429 | [[sending: '(' (0@0-9) | |
4c6cc1db AD |
430 | sending: 'x' (1@10-19) |
431 | thing (1@10-19): 'x' (1@10-19) | |
432 | sending: 'x' (2@20-29) | |
433 | thing (2@20-29): 'x' (2@20-29) | |
434 | sending: 'x' (3@30-39) | |
9c66f418 | 435 | 30-39: syntax error, unexpected 'x', expecting ')' |
4c6cc1db AD |
436 | Freeing nterm thing (2@20-29) |
437 | Freeing nterm thing (1@10-19) | |
4c6cc1db AD |
438 | Freeing token 'x' (3@30-39) |
439 | sending: 'x' (4@40-49) | |
440 | Freeing token 'x' (4@40-49) | |
441 | sending: 'x' (5@50-59) | |
442 | Freeing token 'x' (5@50-59) | |
9c66f418 AD |
443 | sending: ')' (6@60-69) |
444 | line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69) | |
445 | sending: '(' (7@70-79) | |
4c6cc1db AD |
446 | sending: 'x' (8@80-89) |
447 | thing (8@80-89): 'x' (8@80-89) | |
9c66f418 AD |
448 | sending: ')' (9@90-99) |
449 | line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99) | |
450 | sending: '(' (10@100-109) | |
451 | sending: 'x' (11@110-119) | |
452 | thing (11@110-119): 'x' (11@110-119) | |
453 | sending: ')' (12@120-129) | |
454 | line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129) | |
455 | sending: 'y' (13@130-139) | |
b4a20338 AD |
456 | input (0@129-129): /* Nothing */ |
457 | input (2@100-129): line (10@100-129) input (0@129-129) | |
9c66f418 AD |
458 | input (2@70-129): line (7@70-99) input (2@100-129) |
459 | input (2@0-129): line (-1@0-69) input (2@70-129) | |
868d2d96 | 460 | 130-139: syntax error, unexpected 'y', expecting END |
9c66f418 AD |
461 | Freeing nterm input (2@0-129) |
462 | Freeing token 'y' (13@130-139) | |
5719c109 | 463 | Parsing FAILED. |
9280d3ef AD |
464 | ]]) |
465 | ||
868d2d96 JD |
466 | |
467 | # Syntax error caught by the parser where lookahead = END | |
468 | # -------------------------------------------------------- | |
469 | # Load the stack and provoke an error that cannot be caught by the | |
470 | # grammar, to check that the stack is cleared. And make sure the | |
471 | # lookahead is freed. | |
472 | # | |
473 | # '(', 'x', ')', | |
474 | # '(', 'x', ')', | |
475 | # 'x' | |
476 | AT_PARSER_CHECK([./input '(x)(x)x'], 1, | |
477 | [[sending: '(' (0@0-9) | |
478 | sending: 'x' (1@10-19) | |
479 | thing (1@10-19): 'x' (1@10-19) | |
480 | sending: ')' (2@20-29) | |
481 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
482 | sending: '(' (3@30-39) | |
483 | sending: 'x' (4@40-49) | |
484 | thing (4@40-49): 'x' (4@40-49) | |
485 | sending: ')' (5@50-59) | |
486 | line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59) | |
487 | sending: 'x' (6@60-69) | |
488 | thing (6@60-69): 'x' (6@60-69) | |
489 | sending: END (7@70-79) | |
490 | 70-79: syntax error, unexpected END, expecting 'x' | |
491 | Freeing nterm thing (6@60-69) | |
492 | Freeing nterm line (3@30-59) | |
493 | Freeing nterm line (0@0-29) | |
494 | Freeing token END (7@70-79) | |
495 | Parsing FAILED. | |
496 | ]]) | |
497 | ||
498 | ||
80ce3401 PE |
499 | # Check destruction upon stack overflow |
500 | # ------------------------------------- | |
501 | # Upon stack overflow, all symbols on the stack should be destroyed. | |
502 | # Only check for yacc.c. | |
503 | AT_YACC_IF([ | |
6100a9aa | 504 | AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2, |
80ce3401 PE |
505 | [[sending: '(' (0@0-9) |
506 | sending: 'x' (1@10-19) | |
507 | thing (1@10-19): 'x' (1@10-19) | |
508 | sending: ')' (2@20-29) | |
509 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
510 | sending: '(' (3@30-39) | |
511 | sending: 'x' (4@40-49) | |
512 | thing (4@40-49): 'x' (4@40-49) | |
513 | sending: ')' (5@50-59) | |
514 | line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59) | |
515 | sending: '(' (6@60-69) | |
516 | sending: 'x' (7@70-79) | |
517 | thing (7@70-79): 'x' (7@70-79) | |
518 | sending: ')' (8@80-89) | |
519 | line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89) | |
520 | sending: '(' (9@90-99) | |
521 | sending: 'x' (10@100-109) | |
522 | thing (10@100-109): 'x' (10@100-109) | |
523 | sending: ')' (11@110-119) | |
524 | line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119) | |
525 | sending: '(' (12@120-129) | |
526 | sending: 'x' (13@130-139) | |
527 | thing (13@130-139): 'x' (13@130-139) | |
528 | sending: ')' (14@140-149) | |
529 | line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149) | |
530 | sending: '(' (15@150-159) | |
531 | sending: 'x' (16@160-169) | |
532 | thing (16@160-169): 'x' (16@160-169) | |
533 | sending: ')' (17@170-179) | |
534 | line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179) | |
535 | sending: '(' (18@180-189) | |
536 | sending: 'x' (19@190-199) | |
537 | thing (19@190-199): 'x' (19@190-199) | |
538 | sending: ')' (20@200-209) | |
1a059451 | 539 | 200-209: memory exhausted |
80ce3401 PE |
540 | Freeing nterm thing (19@190-199) |
541 | Freeing nterm line (15@150-179) | |
542 | Freeing nterm line (12@120-149) | |
543 | Freeing nterm line (9@90-119) | |
544 | Freeing nterm line (6@60-89) | |
545 | Freeing nterm line (3@30-59) | |
546 | Freeing nterm line (0@0-29) | |
6100a9aa | 547 | Parsing FAILED (status 2). |
80ce3401 PE |
548 | ]]) |
549 | ]) | |
550 | ||
3df37415 AD |
551 | ]) |
552 | ||
553 | ||
a14a26fa | 554 | # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG]) |
046ac74e | 555 | # --------------------------------------------------------------------------- |
3df37415 | 556 | m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR], |
9c66f418 AD |
557 | [AT_SETUP([Printers and Destructors $2: $1]) |
558 | ||
a14a26fa | 559 | $3 |
9c66f418 AD |
560 | _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4], |
561 | [%error-verbose | |
562 | %debug | |
563 | %verbose | |
564 | %locations | |
565 | $1], [$2]) | |
566 | ||
567 | AT_CLEANUP | |
3df37415 AD |
568 | ]) |
569 | ||
570 | ||
ac700aa6 PE |
571 | AT_CHECK_PRINTER_AND_DESTRUCTOR([]) |
572 | AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union]) | |
046ac74e | 573 | |
fd19f271 AD |
574 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"]) |
575 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union]) | |
046ac74e | 576 | |
1576d44d AD |
577 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser]) |
578 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union]) | |
ec5479ce JD |
579 | |
580 | ||
581 | ||
12e35840 JD |
582 | ## ----------------------------------------- ## |
583 | ## Default tagless %printer and %destructor. ## | |
584 | ## ----------------------------------------- ## | |
ec5479ce JD |
585 | |
586 | # Check that the right %printer and %destructor are called, that they're not | |
587 | # called for $end, and that $$ and @$ work correctly. | |
588 | ||
12e35840 | 589 | AT_SETUP([Default tagless %printer and %destructor]) |
ec5479ce JD |
590 | |
591 | AT_DATA_GRAMMAR([[input.y]], | |
592 | [[%error-verbose | |
593 | %debug | |
594 | %locations | |
595 | %initial-action { | |
596 | @$.first_line = @$.last_line = 1; | |
597 | @$.first_column = @$.last_column = 1; | |
598 | } | |
599 | ||
600 | %{ | |
601 | # include <stdio.h> | |
602 | # include <stdlib.h> | |
603 | static void yyerror (const char *msg); | |
604 | static int yylex (void); | |
605 | # define USE(SYM) | |
606 | %} | |
607 | ||
608 | %printer { | |
12e35840 JD |
609 | fprintf (yyoutput, "<*> printer should not be called.\n"); |
610 | } <*> | |
611 | ||
612 | %printer { | |
3ebecc24 JD |
613 | fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column); |
614 | } <> | |
ec5479ce | 615 | %destructor { |
3ebecc24 JD |
616 | fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column); |
617 | } <> | |
ec5479ce JD |
618 | |
619 | %printer { | |
620 | fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column); | |
621 | } 'b' 'c' | |
622 | %destructor { | |
623 | fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column); | |
624 | } 'b' 'c' | |
625 | ||
12e35840 JD |
626 | %destructor { |
627 | fprintf (yyoutput, "<*> destructor should not be called.\n"); | |
628 | } <*> | |
629 | ||
ec5479ce JD |
630 | %% |
631 | ||
632 | start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ; | |
633 | ||
634 | %% | |
635 | ||
636 | static int | |
637 | yylex (void) | |
638 | { | |
cf806753 PE |
639 | static char const input[] = "abcd"; |
640 | static size_t toknum; | |
641 | if (! (toknum < sizeof input)) | |
642 | abort (); | |
643 | yylval = input[toknum++]; | |
ec5479ce | 644 | yylloc.first_line = yylloc.last_line = 1; |
cf806753 | 645 | yylloc.first_column = yylloc.last_column = toknum; |
ec5479ce JD |
646 | return yylval; |
647 | } | |
648 | ||
649 | static void | |
650 | yyerror (const char *msg) | |
651 | { | |
652 | fprintf (stderr, "%s\n", msg); | |
653 | } | |
654 | ||
655 | int | |
656 | main (void) | |
657 | { | |
658 | yydebug = 1; | |
659 | return yyparse (); | |
660 | } | |
661 | ]]) | |
662 | ||
da730230 | 663 | AT_BISON_CHECK([-o input.c input.y]) |
ec5479ce JD |
664 | AT_COMPILE([input]) |
665 | AT_PARSER_CHECK([./input], 1, | |
3ebecc24 | 666 | [[<> destructor for 'd' @ 4. |
ec5479ce JD |
667 | 'b'/'c' destructor for 'c' @ 3. |
668 | 'b'/'c' destructor for 'b' @ 2. | |
3ebecc24 | 669 | <> destructor for 'a' @ 1. |
ec5479ce JD |
670 | ]], |
671 | [[Starting parse | |
672 | Entering state 0 | |
3ebecc24 JD |
673 | Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1) |
674 | Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1) | |
ec5479ce JD |
675 | Entering state 1 |
676 | Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) | |
677 | Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) | |
678 | Entering state 3 | |
679 | Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) | |
680 | Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) | |
681 | Entering state 5 | |
3ebecc24 JD |
682 | Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4) |
683 | Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4) | |
ec5479ce JD |
684 | Entering state 6 |
685 | Reading a token: Now at end of input. | |
686 | syntax error, unexpected $end, expecting 'e' | |
3ebecc24 | 687 | Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4) |
ec5479ce JD |
688 | Stack now 0 1 3 5 |
689 | Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) | |
690 | Stack now 0 1 3 | |
691 | Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) | |
692 | Stack now 0 1 | |
3ebecc24 | 693 | Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1) |
ec5479ce JD |
694 | Stack now 0 |
695 | Cleanup: discarding lookahead token $end (1.5-1.5: ) | |
696 | Stack now 0 | |
697 | ]]) | |
698 | ||
699 | AT_CLEANUP | |
700 | ||
701 | ||
702 | ||
12e35840 JD |
703 | ## ------------------------------------------------------ ## |
704 | ## Default tagged and per-type %printer and %destructor. ## | |
705 | ## ------------------------------------------------------ ## | |
b2a0b7ca | 706 | |
12e35840 | 707 | AT_SETUP([Default tagged and per-type %printer and %destructor]) |
b2a0b7ca JD |
708 | |
709 | AT_DATA_GRAMMAR([[input.y]], | |
710 | [[%error-verbose | |
711 | %debug | |
712 | ||
713 | %{ | |
714 | # include <stdio.h> | |
715 | # include <stdlib.h> | |
716 | static void yyerror (const char *msg); | |
717 | static int yylex (void); | |
718 | # define USE(SYM) | |
719 | %} | |
720 | ||
12e35840 | 721 | %printer { |
3ebecc24 JD |
722 | fprintf (yyoutput, "<> printer should not be called.\n"); |
723 | } <> | |
12e35840 | 724 | |
b2a0b7ca JD |
725 | %union { int field0; int field1; int field2; } |
726 | %type <field0> start 'a' 'g' | |
727 | %type <field1> 'e' | |
728 | %type <field2> 'f' | |
729 | %printer { | |
12e35840 JD |
730 | fprintf (yyoutput, "<*>/<field2>/e printer"); |
731 | } <*> 'e' <field2> | |
b2a0b7ca | 732 | %destructor { |
12e35840 JD |
733 | fprintf (stdout, "<*>/<field2>/e destructor.\n"); |
734 | } <*> 'e' <field2> | |
b2a0b7ca JD |
735 | |
736 | %type <field1> 'b' | |
737 | %printer { fprintf (yyoutput, "<field1> printer"); } <field1> | |
738 | %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1> | |
739 | ||
740 | %type <field0> 'c' | |
741 | %printer { fprintf (yyoutput, "'c' printer"); } 'c' | |
742 | %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c' | |
743 | ||
744 | %type <field1> 'd' | |
745 | %printer { fprintf (yyoutput, "'d' printer"); } 'd' | |
746 | %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd' | |
747 | ||
12e35840 | 748 | %destructor { |
3ebecc24 JD |
749 | fprintf (yyoutput, "<> destructor should not be called.\n"); |
750 | } <> | |
12e35840 | 751 | |
b2a0b7ca JD |
752 | %% |
753 | ||
754 | start: | |
755 | 'a' 'b' 'c' 'd' 'e' 'f' 'g' | |
756 | { | |
757 | USE(($1, $2, $3, $4, $5, $6, $7)); | |
758 | $$ = 'S'; | |
759 | } | |
760 | ; | |
761 | ||
762 | %% | |
763 | ||
764 | static int | |
765 | yylex (void) | |
766 | { | |
cf806753 PE |
767 | static char const input[] = "abcdef"; |
768 | static size_t toknum; | |
769 | if (! (toknum < sizeof input)) | |
770 | abort (); | |
771 | return input[toknum++]; | |
b2a0b7ca JD |
772 | } |
773 | ||
774 | static void | |
775 | yyerror (const char *msg) | |
776 | { | |
777 | fprintf (stderr, "%s\n", msg); | |
778 | } | |
779 | ||
780 | int | |
781 | main (void) | |
782 | { | |
783 | yydebug = 1; | |
784 | return yyparse (); | |
785 | } | |
786 | ]]) | |
787 | ||
da730230 | 788 | AT_BISON_CHECK([-o input.c input.y]) |
b2a0b7ca JD |
789 | AT_COMPILE([input]) |
790 | AT_PARSER_CHECK([./input], 1, | |
12e35840 JD |
791 | [[<*>/<field2>/e destructor. |
792 | <*>/<field2>/e destructor. | |
b2a0b7ca JD |
793 | 'd' destructor. |
794 | 'c' destructor. | |
795 | <field1> destructor. | |
12e35840 | 796 | <*>/<field2>/e destructor. |
b2a0b7ca JD |
797 | ]], |
798 | [[Starting parse | |
799 | Entering state 0 | |
12e35840 JD |
800 | Reading a token: Next token is token 'a' (<*>/<field2>/e printer) |
801 | Shifting token 'a' (<*>/<field2>/e printer) | |
b2a0b7ca JD |
802 | Entering state 1 |
803 | Reading a token: Next token is token 'b' (<field1> printer) | |
804 | Shifting token 'b' (<field1> printer) | |
805 | Entering state 3 | |
806 | Reading a token: Next token is token 'c' ('c' printer) | |
807 | Shifting token 'c' ('c' printer) | |
808 | Entering state 5 | |
809 | Reading a token: Next token is token 'd' ('d' printer) | |
810 | Shifting token 'd' ('d' printer) | |
811 | Entering state 6 | |
12e35840 JD |
812 | Reading a token: Next token is token 'e' (<*>/<field2>/e printer) |
813 | Shifting token 'e' (<*>/<field2>/e printer) | |
b2a0b7ca | 814 | Entering state 7 |
12e35840 JD |
815 | Reading a token: Next token is token 'f' (<*>/<field2>/e printer) |
816 | Shifting token 'f' (<*>/<field2>/e printer) | |
b2a0b7ca JD |
817 | Entering state 8 |
818 | Reading a token: Now at end of input. | |
819 | syntax error, unexpected $end, expecting 'g' | |
12e35840 | 820 | Error: popping token 'f' (<*>/<field2>/e printer) |
b2a0b7ca | 821 | Stack now 0 1 3 5 6 7 |
12e35840 | 822 | Error: popping token 'e' (<*>/<field2>/e printer) |
b2a0b7ca JD |
823 | Stack now 0 1 3 5 6 |
824 | Error: popping token 'd' ('d' printer) | |
825 | Stack now 0 1 3 5 | |
826 | Error: popping token 'c' ('c' printer) | |
827 | Stack now 0 1 3 | |
828 | Error: popping token 'b' (<field1> printer) | |
829 | Stack now 0 1 | |
12e35840 | 830 | Error: popping token 'a' (<*>/<field2>/e printer) |
b2a0b7ca JD |
831 | Stack now 0 |
832 | Cleanup: discarding lookahead token $end () | |
833 | Stack now 0 | |
834 | ]]) | |
835 | ||
836 | AT_CLEANUP | |
837 | ||
838 | ||
839 | ||
ec5479ce | 840 | ## ------------------------------------------------------------- ## |
12e35840 | 841 | ## Default %printer and %destructor for user-defined end token. ## |
ec5479ce JD |
842 | ## ------------------------------------------------------------- ## |
843 | ||
3508ce36 | 844 | AT_SETUP([Default %printer and %destructor for user-defined end token]) |
ec5479ce | 845 | |
12e35840 JD |
846 | # _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED) |
847 | # ----------------------------------------------------------------------------- | |
848 | m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN], | |
849 | [m4_if($1, 0, | |
3ebecc24 JD |
850 | [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])], |
851 | [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])]) | |
12e35840 JD |
852 | |
853 | AT_DATA_GRAMMAR([[input]]$1[[.y]], | |
ec5479ce JD |
854 | [[%error-verbose |
855 | %debug | |
856 | %locations | |
857 | %initial-action { | |
858 | @$.first_line = @$.last_line = 1; | |
859 | @$.first_column = @$.last_column = 1; | |
860 | } | |
861 | ||
862 | %{ | |
863 | # include <stdio.h> | |
864 | # include <stdlib.h> | |
865 | static void yyerror (const char *msg); | |
866 | static int yylex (void); | |
867 | # define USE(SYM) | |
868 | %} | |
869 | ||
12e35840 JD |
870 | %destructor { |
871 | fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n"); | |
872 | } <]]not_kind[[> | |
873 | ||
ec5479ce JD |
874 | %token END 0 |
875 | %printer { | |
12e35840 JD |
876 | fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column); |
877 | } <]]kind[[> | |
ec5479ce | 878 | %destructor { |
12e35840 JD |
879 | fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column); |
880 | } <]]kind[[> | |
881 | ||
882 | %printer { | |
883 | fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n"); | |
884 | } <]]not_kind[[> | |
885 | ||
886 | ]]m4_if($1, 0, [[[ | |
887 | ]]], | |
888 | [[[%union { char tag; } | |
889 | %type <tag> start END]]])[[ | |
ec5479ce JD |
890 | |
891 | %% | |
892 | ||
893 | start: { $$ = 'S'; } ; | |
894 | ||
895 | %% | |
896 | ||
897 | static int | |
898 | yylex (void) | |
899 | { | |
cf806753 PE |
900 | static int called; |
901 | if (called++) | |
902 | abort (); | |
12e35840 | 903 | yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E'; |
ec5479ce JD |
904 | yylloc.first_line = yylloc.last_line = 1; |
905 | yylloc.first_column = yylloc.last_column = 1; | |
906 | return 0; | |
907 | } | |
908 | ||
909 | static void | |
910 | yyerror (const char *msg) | |
911 | { | |
912 | fprintf (stderr, "%s\n", msg); | |
913 | } | |
914 | ||
915 | int | |
916 | main (void) | |
917 | { | |
918 | yydebug = 1; | |
919 | return yyparse (); | |
920 | } | |
921 | ]]) | |
922 | ||
da730230 | 923 | AT_BISON_CHECK([-o input$1.c input$1.y]) |
12e35840 JD |
924 | AT_COMPILE([input$1]) |
925 | AT_PARSER_CHECK([./input$1], 0, | |
926 | [[<]]kind[[> for 'E' @ 1. | |
927 | <]]kind[[> for 'S' @ 1. | |
ec5479ce JD |
928 | ]], |
929 | [[Starting parse | |
930 | Entering state 0 | |
12e35840 JD |
931 | Reducing stack by rule 1 (line 46): |
932 | -> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1) | |
ec5479ce JD |
933 | Stack now 0 |
934 | Entering state 1 | |
935 | Reading a token: Now at end of input. | |
12e35840 | 936 | Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1) |
ec5479ce JD |
937 | Entering state 2 |
938 | Stack now 0 1 2 | |
12e35840 JD |
939 | Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1) |
940 | Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1) | |
ec5479ce JD |
941 | ]]) |
942 | ||
12e35840 JD |
943 | m4_popdef([kind]) |
944 | m4_popdef([not_kind]) | |
945 | ]) | |
946 | ||
947 | _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0) | |
948 | _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1) | |
949 | ||
ec5479ce | 950 | AT_CLEANUP |
9350499c JD |
951 | |
952 | ||
953 | ||
954 | ## ------------------------------------------------------------------ ## | |
955 | ## Default %printer and %destructor are not for error or $undefined. ## | |
956 | ## ------------------------------------------------------------------ ## | |
957 | ||
287b314e | 958 | AT_SETUP([Default %printer and %destructor are not for error or $undefined]) |
9350499c JD |
959 | |
960 | # If Bison were to apply the default %printer and %destructor to the error | |
961 | # token or to $undefined: | |
962 | # - For the error token: | |
963 | # - It would generate warnings for unused $n. | |
964 | # - It would invoke the %printer and %destructor on the error token's | |
965 | # semantic value, which would be initialized from the lookahead, which | |
966 | # would be destroyed separately. | |
967 | # - For $undefined, who knows what the semantic value would be. | |
968 | ||
969 | AT_DATA_GRAMMAR([[input.y]], | |
970 | [[%debug | |
971 | ||
972 | %{ | |
973 | # include <stdio.h> | |
cf806753 | 974 | # include <stdlib.h> |
9350499c JD |
975 | static void yyerror (const char *msg); |
976 | static int yylex (void); | |
977 | # define USE(SYM) | |
978 | %} | |
979 | ||
980 | %printer { | |
981 | fprintf (yyoutput, "'%c'", $$); | |
3ebecc24 | 982 | } <> <*> |
9350499c JD |
983 | %destructor { |
984 | fprintf (stderr, "DESTROY '%c'\n", $$); | |
3ebecc24 | 985 | } <> <*> |
9350499c JD |
986 | |
987 | %% | |
988 | ||
989 | start: | |
990 | { $$ = 'S'; } | |
991 | /* In order to reveal the problems that this bug caused during parsing, add | |
992 | * $2 to USE. */ | |
993 | | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; } | |
994 | ; | |
995 | ||
996 | %% | |
997 | ||
998 | static int | |
999 | yylex (void) | |
1000 | { | |
cf806753 PE |
1001 | static char const input[] = "abd"; |
1002 | static size_t toknum; | |
1003 | if (! (toknum < sizeof input)) | |
1004 | abort (); | |
1005 | yylval = input[toknum++]; | |
9350499c JD |
1006 | return yylval; |
1007 | } | |
1008 | ||
1009 | static void | |
1010 | yyerror (const char *msg) | |
1011 | { | |
1012 | fprintf (stderr, "%s\n", msg); | |
1013 | } | |
1014 | ||
1015 | int | |
1016 | main (void) | |
1017 | { | |
1018 | yydebug = 1; | |
1019 | return yyparse (); | |
1020 | } | |
1021 | ]]) | |
1022 | ||
da730230 | 1023 | AT_BISON_CHECK([-o input.c input.y]) |
9350499c JD |
1024 | AT_COMPILE([input]) |
1025 | AT_PARSER_CHECK([./input], [1], [], | |
1026 | [[Starting parse | |
1027 | Entering state 0 | |
1028 | Reading a token: Next token is token 'a' ('a') | |
1029 | Shifting token 'a' ('a') | |
1030 | Entering state 1 | |
1031 | Reading a token: Next token is token 'b' ('b') | |
1032 | syntax error | |
1033 | Shifting token error () | |
1034 | Entering state 3 | |
1035 | Next token is token 'b' ('b') | |
1036 | Shifting token 'b' ('b') | |
1037 | Entering state 5 | |
1038 | Reading a token: Next token is token $undefined () | |
1039 | Error: popping token 'b' ('b') | |
1040 | DESTROY 'b' | |
1041 | Stack now 0 1 3 | |
1042 | Error: popping token error () | |
1043 | Stack now 0 1 | |
1044 | Shifting token error () | |
1045 | Entering state 3 | |
1046 | Next token is token $undefined () | |
1047 | Error: discarding token $undefined () | |
1048 | Error: popping token error () | |
1049 | Stack now 0 1 | |
1050 | Shifting token error () | |
1051 | Entering state 3 | |
1052 | Reading a token: Now at end of input. | |
1053 | Cleanup: discarding lookahead token $end () | |
1054 | Stack now 0 1 3 | |
1055 | Cleanup: popping token error () | |
1056 | Cleanup: popping token 'a' ('a') | |
1057 | DESTROY 'a' | |
1058 | ]]) | |
1059 | ||
1060 | AT_CLEANUP | |
1061 | ||
1062 | ||
1063 | ||
1064 | ## ------------------------------------------------------ ## | |
1065 | ## Default %printer and %destructor are not for $accept. ## | |
1066 | ## ------------------------------------------------------ ## | |
1067 | ||
287b314e | 1068 | AT_SETUP([Default %printer and %destructor are not for $accept]) |
9350499c JD |
1069 | |
1070 | # If YYSTYPE is a union and Bison were to apply the default %printer and | |
1071 | # %destructor to $accept: | |
1072 | # - The %printer and %destructor code generated for $accept would always be | |
1073 | # dead code because $accept is currently never shifted onto the stack. | |
1074 | # - $$ for $accept would always be of type YYSTYPE because it's not possible | |
1075 | # to declare `%type <field> $accept'. (Also true for $undefined.) | |
1076 | # - Thus, the compiler might complain that the user code assumes the wrong | |
1077 | # type for $$ since the code might assume the type associated with a | |
1078 | # specific union field, which is especially reasonable in C++ since that | |
1079 | # type may be a base type. This test case checks for this problem. (Also | |
1080 | # true for $undefined and the error token, so there are three warnings for | |
1081 | # %printer and three for %destructor.) | |
1082 | ||
1083 | AT_DATA_GRAMMAR([[input.y]], | |
1084 | [[%debug /* So that %printer is actually compiled. */ | |
1085 | ||
1086 | %{ | |
1087 | # include <stdio.h> | |
cf806753 | 1088 | # include <stdlib.h> |
9350499c JD |
1089 | static void yyerror (const char *msg); |
1090 | static int yylex (void); | |
1091 | # define USE(SYM) | |
1092 | %} | |
1093 | ||
1094 | %printer { | |
1095 | char chr = $$; | |
1096 | fprintf (yyoutput, "'%c'", chr); | |
3ebecc24 | 1097 | } <> <*> |
9350499c JD |
1098 | %destructor { |
1099 | char chr = $$; | |
1100 | fprintf (stderr, "DESTROY '%c'\n", chr); | |
3ebecc24 | 1101 | } <> <*> |
9350499c JD |
1102 | |
1103 | %union { char chr; } | |
1104 | %type <chr> start | |
1105 | ||
1106 | %% | |
1107 | ||
1108 | start: { USE($$); } ; | |
1109 | ||
1110 | %% | |
1111 | ||
1112 | static int | |
1113 | yylex (void) | |
1114 | { | |
cf806753 PE |
1115 | static int called; |
1116 | if (called++) | |
1117 | abort (); | |
9350499c JD |
1118 | return 0; |
1119 | } | |
1120 | ||
1121 | static void | |
1122 | yyerror (const char *msg) | |
1123 | { | |
1124 | fprintf (stderr, "%s\n", msg); | |
1125 | } | |
1126 | ||
1127 | int | |
1128 | main (void) | |
1129 | { | |
1130 | return yyparse (); | |
1131 | } | |
1132 | ]]) | |
1133 | ||
da730230 | 1134 | AT_BISON_CHECK([-o input.c input.y]) |
9350499c JD |
1135 | AT_COMPILE([input]) |
1136 | ||
1137 | AT_CLEANUP | |
f91b1629 JD |
1138 | |
1139 | ||
1140 | ||
1141 | ## ------------------------------------------------------ ## | |
1142 | ## Default %printer and %destructor for mid-rule values. ## | |
1143 | ## ------------------------------------------------------ ## | |
1144 | ||
1145 | AT_SETUP([Default %printer and %destructor for mid-rule values]) | |
1146 | ||
1147 | AT_DATA_GRAMMAR([[input.y]], | |
1148 | [[%debug /* So that %printer is actually compiled. */ | |
1149 | ||
1150 | %{ | |
1151 | # include <stdio.h> | |
1152 | # include <stdlib.h> | |
1153 | static void yyerror (const char *msg); | |
1154 | static int yylex (void); | |
1155 | # define USE(SYM) | |
1156 | # define YYLTYPE int | |
1157 | # define YYLLOC_DEFAULT(Current, Rhs, N) | |
1158 | # define YY_LOCATION_PRINT(File, Loc) | |
1159 | %} | |
1160 | ||
3ebecc24 JD |
1161 | %printer { fprintf (yyoutput, "%d", @$); } <> |
1162 | %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <> | |
12e35840 JD |
1163 | %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*> |
1164 | %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*> | |
f91b1629 JD |
1165 | |
1166 | %% | |
1167 | ||
1168 | start: | |
1169 | { @$ = 1; } // Not set or used. | |
1170 | { USE ($$); @$ = 2; } // Both set and used. | |
1171 | { USE ($$); @$ = 3; } // Only set. | |
1172 | { @$ = 4; } // Only used. | |
1173 | 'c' | |
1174 | { USE (($$, $2, $4, $5)); @$ = 0; } | |
1175 | ; | |
1176 | ||
1177 | %% | |
1178 | ||
1179 | static int | |
1180 | yylex (void) | |
1181 | { | |
1182 | static int called; | |
1183 | if (called++) | |
1184 | abort (); | |
1185 | return 0; | |
1186 | } | |
1187 | ||
1188 | static void | |
1189 | yyerror (const char *msg) | |
1190 | { | |
1191 | fprintf (stderr, "%s\n", msg); | |
1192 | } | |
1193 | ||
1194 | int | |
1195 | main (void) | |
1196 | { | |
1197 | yydebug = 1; | |
1198 | return yyparse (); | |
1199 | } | |
1200 | ]]) | |
1201 | ||
da730230 | 1202 | AT_BISON_CHECK([-o input.c input.y], 0,, |
12e35840 JD |
1203 | [[input.y:33.3-23: warning: unset value: $$ |
1204 | input.y:30.3-35.37: warning: unused value: $3 | |
f91b1629 JD |
1205 | ]]) |
1206 | ||
1207 | AT_COMPILE([input]) | |
1208 | AT_PARSER_CHECK([./input], 1,, | |
1209 | [[Starting parse | |
1210 | Entering state 0 | |
12e35840 | 1211 | Reducing stack by rule 1 (line 30): |
f91b1629 JD |
1212 | -> $$ = nterm $@1 (: ) |
1213 | Stack now 0 | |
1214 | Entering state 2 | |
12e35840 | 1215 | Reducing stack by rule 2 (line 31): |
f91b1629 JD |
1216 | -> $$ = nterm @2 (: 2) |
1217 | Stack now 0 2 | |
1218 | Entering state 4 | |
12e35840 | 1219 | Reducing stack by rule 3 (line 32): |
f91b1629 JD |
1220 | -> $$ = nterm @3 (: 3) |
1221 | Stack now 0 2 4 | |
1222 | Entering state 5 | |
12e35840 | 1223 | Reducing stack by rule 4 (line 33): |
f91b1629 JD |
1224 | -> $$ = nterm @4 (: 4) |
1225 | Stack now 0 2 4 5 | |
1226 | Entering state 6 | |
1227 | Reading a token: Now at end of input. | |
1228 | syntax error | |
1229 | Error: popping nterm @4 (: 4) | |
1230 | DESTROY 4 | |
1231 | Stack now 0 2 4 5 | |
1232 | Error: popping nterm @3 (: 3) | |
1233 | DESTROY 3 | |
1234 | Stack now 0 2 4 | |
1235 | Error: popping nterm @2 (: 2) | |
1236 | DESTROY 2 | |
1237 | Stack now 0 2 | |
1238 | Error: popping nterm $@1 (: ) | |
1239 | Stack now 0 | |
1240 | Cleanup: discarding lookahead token $end (: ) | |
1241 | Stack now 0 | |
1242 | ]]) | |
1243 | ||
1244 | AT_CLEANUP | |
e785ccf7 JD |
1245 | |
1246 | ||
1247 | ## ----------------------- ## | |
1248 | ## @$ implies %locations. ## | |
1249 | ## ----------------------- ## | |
1250 | ||
1251 | # Bison once forgot to check for @$ in actions other than semantic actions. | |
1252 | ||
1253 | # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE) | |
1254 | # ------------------------------------------------------- | |
1255 | m4_define([AT_CHECK_ACTION_LOCATIONS], | |
1256 | [AT_SETUP([[@$ in ]$1[ implies %locations]]) | |
1257 | ||
1258 | AT_DATA_GRAMMAR([[input.y]], | |
1259 | [[%code { | |
1260 | #include <stdio.h> | |
1261 | static int yylex (void); | |
1262 | static void yyerror (char const *msg); | |
1263 | } | |
1264 | ||
1265 | %debug | |
1266 | ||
1267 | ]$1[ { | |
1268 | printf ("%d\n", @$.first_line); | |
1269 | } ]m4_if($1, [%initial-action], [], [[start]])[ | |
1270 | ||
1271 | %% | |
1272 | ||
1273 | start: ; | |
1274 | ||
1275 | %% | |
1276 | ||
1277 | static int | |
1278 | yylex (void) | |
1279 | { | |
1280 | return 0; | |
1281 | } | |
1282 | ||
1283 | static void | |
1284 | yyerror (char const *msg) | |
1285 | { | |
1286 | fprintf (stderr, "%s\n", msg); | |
1287 | } | |
1288 | ||
1289 | int | |
1290 | main (void) | |
1291 | { | |
1292 | return yyparse (); | |
1293 | } | |
1294 | ]]) | |
1295 | ||
da730230 | 1296 | AT_BISON_CHECK([[-o input.c input.y]]) |
e785ccf7 JD |
1297 | AT_COMPILE([[input]]) |
1298 | ||
1299 | AT_CLEANUP]) | |
1300 | ||
1301 | AT_CHECK_ACTION_LOCATIONS([[%initial-action]]) | |
1302 | AT_CHECK_ACTION_LOCATIONS([[%destructor]]) | |
1303 | AT_CHECK_ACTION_LOCATIONS([[%printer]]) | |
42f4393a DJ |
1304 | |
1305 | ||
1306 | ## ----------------------------------------------- ## | |
1307 | ## Fix user actions without a trailing semicolon. ## | |
1308 | ## ----------------------------------------------- ## | |
1309 | ||
1310 | AT_SETUP([[Fix user actions without a trailing semicolon]]) | |
1311 | ||
1312 | # This feature is undocumented, but we accidentally broke it in 2.3a, | |
1313 | # and there was a complaint at: | |
1314 | # <http://lists.gnu.org/archive/html/bug-bison/2008-11/msg00001.html>. | |
1315 | ||
1316 | AT_DATA([input.y], | |
1317 | [[%% | |
1318 | start: test2 test1 test0 testc; | |
1319 | ||
1320 | test2 | |
1321 | : 'a' { semi; /* TEST:N:2 */ } | |
1322 | | 'b' { if (0) {no_semi} /* TEST:N:2 */ } | |
1323 | | 'c' { if (0) {semi;} /* TEST:N:2 */ } | |
1324 | | 'd' { semi; no_semi /* TEST:Y:2 */ } | |
1325 | | 'e' { semi(); no_semi() /* TEST:Y:2 */ } | |
1326 | | 'f' { semi[]; no_semi[] /* TEST:Y:2 */ } | |
1327 | | 'g' { semi++; no_semi++ /* TEST:Y:2 */ } | |
1328 | | 'h' { {no_semi} no_semi /* TEST:Y:2 */ } | |
1329 | | 'i' { {semi;} no_semi /* TEST:Y:2 */ } | |
1330 | ; | |
1331 | test1 | |
1332 | : 'a' { semi; // TEST:N:1 ; | |
1333 | } | 'b' { if (0) {no_semi} // TEST:N:1 ; | |
1334 | } | 'c' { if (0) {semi;} // TEST:N:1 ; | |
1335 | } | 'd' { semi; no_semi // TEST:Y:1 ; | |
1336 | } | 'e' { semi(); no_semi() // TEST:Y:1 ; | |
1337 | } | 'f' { semi[]; no_semi[] // TEST:Y:1 ; | |
1338 | } | 'g' { semi++; no_semi++ // TEST:Y:1 ; | |
1339 | } | 'h' { {no_semi} no_semi // TEST:Y:1 ; | |
1340 | } | 'i' { {semi;} no_semi // TEST:Y:1 ; | |
1341 | } ; | |
1342 | test0 | |
1343 | : 'a' { semi; // TEST:N:1 {} | |
1344 | } | 'b' { if (0) {no_semi} // TEST:N:1 {} | |
1345 | } | 'c' { if (0) {semi;} // TEST:N:1 {} | |
1346 | } | 'd' { semi; no_semi // TEST:Y:1 {} | |
1347 | } | 'e' { semi(); no_semi() // TEST:Y:1 {} | |
1348 | } | 'f' { semi[]; no_semi[] // TEST:Y:1 {} | |
1349 | } | 'g' { semi++; no_semi++ // TEST:Y:1 {} | |
1350 | } | 'h' { {no_semi} no_semi // TEST:Y:1 {} | |
1351 | } | 'i' { {semi;} no_semi // TEST:Y:1 {} | |
1352 | } ; | |
1353 | ||
1354 | testc | |
1355 | : 'a' { | |
1356 | #define TEST_MACRO_N \ | |
1357 | []"broken\" $ @ $$ @$ [];\ | |
1358 | string;"} | |
1359 | | 'b' { | |
1360 | no_semi | |
1361 | #define TEST_MACRO_N \ | |
1362 | []"broken\" $ @ $$ @$ [];\ | |
1363 | string;"} | |
1364 | ]]) | |
1365 | ||
1366 | AT_BISON_CHECK([[-o input.c input.y]], [0], [], | |
1367 | [[input.y:8.48: warning: a `;' might be needed at the end of action code | |
1368 | input.y:8.48: warning: future versions of Bison will not add the `;' | |
1369 | input.y:9.48: warning: a `;' might be needed at the end of action code | |
1370 | input.y:9.48: warning: future versions of Bison will not add the `;' | |
1371 | input.y:10.48: warning: a `;' might be needed at the end of action code | |
1372 | input.y:10.48: warning: future versions of Bison will not add the `;' | |
1373 | input.y:11.48: warning: a `;' might be needed at the end of action code | |
1374 | input.y:11.48: warning: future versions of Bison will not add the `;' | |
1375 | input.y:12.48: warning: a `;' might be needed at the end of action code | |
1376 | input.y:12.48: warning: future versions of Bison will not add the `;' | |
1377 | input.y:13.48: warning: a `;' might be needed at the end of action code | |
1378 | input.y:13.48: warning: future versions of Bison will not add the `;' | |
1379 | input.y:20.1: warning: a `;' might be needed at the end of action code | |
1380 | input.y:20.1: warning: future versions of Bison will not add the `;' | |
1381 | input.y:21.1: warning: a `;' might be needed at the end of action code | |
1382 | input.y:21.1: warning: future versions of Bison will not add the `;' | |
1383 | input.y:22.1: warning: a `;' might be needed at the end of action code | |
1384 | input.y:22.1: warning: future versions of Bison will not add the `;' | |
1385 | input.y:23.1: warning: a `;' might be needed at the end of action code | |
1386 | input.y:23.1: warning: future versions of Bison will not add the `;' | |
1387 | input.y:24.1: warning: a `;' might be needed at the end of action code | |
1388 | input.y:24.1: warning: future versions of Bison will not add the `;' | |
1389 | input.y:25.1: warning: a `;' might be needed at the end of action code | |
1390 | input.y:25.1: warning: future versions of Bison will not add the `;' | |
1391 | input.y:31.1: warning: a `;' might be needed at the end of action code | |
1392 | input.y:31.1: warning: future versions of Bison will not add the `;' | |
1393 | input.y:32.1: warning: a `;' might be needed at the end of action code | |
1394 | input.y:32.1: warning: future versions of Bison will not add the `;' | |
1395 | input.y:33.1: warning: a `;' might be needed at the end of action code | |
1396 | input.y:33.1: warning: future versions of Bison will not add the `;' | |
1397 | input.y:34.1: warning: a `;' might be needed at the end of action code | |
1398 | input.y:34.1: warning: future versions of Bison will not add the `;' | |
1399 | input.y:35.1: warning: a `;' might be needed at the end of action code | |
1400 | input.y:35.1: warning: future versions of Bison will not add the `;' | |
1401 | input.y:36.1: warning: a `;' might be needed at the end of action code | |
1402 | input.y:36.1: warning: future versions of Bison will not add the `;' | |
1403 | ]]) | |
c4fae1ef AD |
1404 | |
1405 | AT_MATCHES_CHECK([input.c], [[/\* TEST:N:2 \*/ \}$]], [[3]]) | |
1406 | AT_MATCHES_CHECK([input.c], [[/\* TEST:Y:2 \*/ ;\}$]], [[6]]) | |
1407 | AT_MATCHES_CHECK([input.c], [[// TEST:N:1 [;{}]*\n\}$]], [[6]]) | |
1408 | AT_MATCHES_CHECK([input.c], [[// TEST:Y:1 [;{}]*\n;\}$]], [[12]]) | |
1409 | AT_MATCHES_CHECK([input.c], [[#define TEST_MACRO_N \\\n\[\]"broken\\" \$ \@ \$\$ \@\$ \[\];\\\nstring;"\}]], [[2]]) | |
42f4393a DJ |
1410 | |
1411 | AT_CLEANUP | |
abcc7c03 JD |
1412 | |
1413 | ||
1414 | ## -------------------------------------------------- ## | |
1415 | ## Destroying lookahead assigned by semantic action. ## | |
1416 | ## -------------------------------------------------- ## | |
1417 | ||
1418 | AT_SETUP([[Destroying lookahead assigned by semantic action]]) | |
1419 | ||
1420 | AT_DATA_GRAMMAR([input.y], | |
1421 | [[ | |
1422 | %code { | |
1423 | #include <assert.h> | |
1424 | #include <stdio.h> | |
1425 | static void yyerror (char const *); | |
1426 | static int yylex (void); | |
1427 | #define USE(Var) | |
1428 | } | |
1429 | ||
1430 | %destructor { fprintf (stderr, "'a' destructor\n"); } 'a' | |
1431 | %destructor { fprintf (stderr, "'b' destructor\n"); } 'b' | |
1432 | ||
1433 | %% | |
1434 | ||
1435 | // In a previous version of Bison, yychar assigned by the semantic | |
1436 | // action below was not translated into yytoken before the lookahead was | |
1437 | // discarded and thus before its destructor (selected according to | |
1438 | // yytoken) was called in order to return from yyparse. This would | |
1439 | // happen even if YYACCEPT was performed in a later semantic action as | |
1440 | // long as only consistent states with default reductions were visited | |
1441 | // in between. However, we leave YYACCEPT in the same semantic action | |
1442 | // for this test in order to show that skeletons cannot simply translate | |
1443 | // immediately after every semantic action because a semantic action | |
1444 | // that has set yychar might not always return normally. Instead, | |
1445 | // skeletons must translate before every use of yytoken. | |
1446 | start: 'a' accept { USE($1); } ; | |
1447 | accept: /*empty*/ { | |
1448 | assert (yychar == YYEMPTY); | |
1449 | yychar = 'b'; | |
1450 | YYACCEPT; | |
1451 | } ; | |
1452 | ||
1453 | %% | |
1454 | ||
1455 | static void | |
1456 | yyerror (char const *msg) | |
1457 | { | |
1458 | fprintf (stderr, "%s\n", msg); | |
1459 | } | |
1460 | ||
1461 | static int | |
1462 | yylex (void) | |
1463 | { | |
1464 | static char const *input = "a"; | |
1465 | return *input++; | |
1466 | } | |
1467 | ||
1468 | int | |
1469 | main (void) | |
1470 | { | |
1471 | return yyparse (); | |
1472 | } | |
1473 | ]]) | |
1474 | ||
1475 | AT_BISON_CHECK([[-o input.c input.y]]) | |
1476 | AT_COMPILE([[input]]) | |
1477 | AT_PARSER_CHECK([[./input]], [[0]], [], | |
1478 | [['b' destructor | |
1479 | 'a' destructor | |
1480 | ]]) | |
1481 | ||
1482 | AT_CLEANUP |