]>
Commit | Line | Data |
---|---|---|
9534d2be | 1 | e# Executing Actions. -*- Autotest -*- |
7d424de1 | 2 | |
34136e65 | 3 | # Copyright (C) 2001-2012 Free Software 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 | ||
55f48c48 | 31 | AT_BISON_OPTION_PUSHDEFS |
9501dc6e | 32 | AT_DATA_GRAMMAR([[input.y]], |
cf499cff | 33 | [[%define parse.error verbose |
8f3596a6 AD |
34 | %debug |
35 | %{ | |
55f48c48 AD |
36 | ]AT_YYERROR_DECLARE[ |
37 | ]AT_YYLEX_DECLARE[ | |
82c035a8 AD |
38 | %} |
39 | %% | |
931394cb AD |
40 | exp: { putchar ('0'); } |
41 | '1' { putchar ('1'); } | |
42 | '2' { putchar ('2'); } | |
43 | '3' { putchar ('3'); } | |
44 | '4' { putchar ('4'); } | |
45 | '5' { putchar ('5'); } | |
46 | '6' { putchar ('6'); } | |
47 | '7' { putchar ('7'); } | |
48 | '8' { putchar ('8'); } | |
49 | '9' { putchar ('9'); } | |
50 | { putchar ('\n'); } | |
82c035a8 AD |
51 | ; |
52 | %% | |
55f48c48 | 53 | ]AT_YYERROR_DEFINE[ |
95361618 | 54 | ]AT_YYLEX_DEFINE(["123456789"])[ |
82c035a8 AD |
55 | int |
56 | main (void) | |
57 | { | |
58 | return yyparse (); | |
59 | } | |
60 | ]]) | |
55f48c48 | 61 | AT_BISON_OPTION_POPDEFS |
82c035a8 | 62 | |
da730230 | 63 | AT_BISON_CHECK([-d -v -o input.c input.y]) |
1154cced AD |
64 | AT_COMPILE([input]) |
65 | AT_PARSER_CHECK([./input], 0, | |
931394cb | 66 | [[0123456789 |
82c035a8 AD |
67 | ]]) |
68 | ||
69 | AT_CLEANUP | |
75d1fe16 AD |
70 | |
71 | ||
a1d1ab50 AD |
72 | ## ------------------ ## |
73 | ## Initial location. ## | |
74 | ## ------------------ ## | |
75 | ||
fb4c8a7c TR |
76 | # AT_TEST(SKELETON-NAME, DIRECTIVES, [MORE-DIRECTIVES], [LOCATION = 1.1]) |
77 | # ----------------------------------------------------------------------- | |
63951be2 | 78 | # Check that the initial location is correct. |
a1d1ab50 AD |
79 | m4_pushdef([AT_TEST], |
80 | [AT_SETUP([Initial location: $1 $2]) | |
81 | ||
6428a8a4 | 82 | AT_BISON_OPTION_PUSHDEFS([%locations %skeleton "$1" $2]) |
a1d1ab50 AD |
83 | AT_DATA_GRAMMAR([[input.y]], |
84 | [[%defines /* FIXME: Required by lalr1.cc in Bison 2.6. */ | |
85 | %locations | |
86 | %debug | |
87 | %skeleton "$1" | |
fb4c8a7c TR |
88 | ]$2[ |
89 | ]$3[ | |
a1d1ab50 AD |
90 | %code |
91 | { | |
92 | # include <stdio.h> | |
93 | # include <stdlib.h> // getenv | |
94 | ]AT_YYERROR_DECLARE[ | |
95 | ]AT_YYLEX_DECLARE[ | |
96 | } | |
97 | %% | |
98 | exp: { ]AT_SKEL_CC_IF([[std::cerr << @$ << std::endl]], | |
99 | [[YY_LOCATION_PRINT(stderr, @$); fputc ('\n', stderr)]])[; } | |
100 | %% | |
101 | ]AT_YYERROR_DEFINE[ | |
102 | ||
103 | ]AT_YYLEX_PROTOTYPE[ | |
104 | {]AT_PURE_IF([ | |
105 | YYUSE(lvalp); | |
106 | YYUSE(llocp);], [AT_SKEL_CC_IF([ | |
107 | YYUSE(lvalp); | |
108 | YYUSE(llocp);])])[ | |
109 | return 'x'; | |
110 | } | |
111 | ||
112 | int | |
113 | main (void) | |
114 | {]AT_SKEL_CC_IF([[ | |
6428a8a4 | 115 | yy::parser p; |
a1d1ab50 AD |
116 | p.set_debug_level (!!getenv("YYDEBUG")); |
117 | return p.parse ();]], [[ | |
118 | yydebug = !!getenv("YYDEBUG"); | |
6428a8a4 | 119 | return !!yyparse (]AT_PARAM_IF([0])[);]])[ |
a1d1ab50 AD |
120 | } |
121 | ]]) | |
122 | ||
123 | AT_FULL_COMPILE([input]) | |
124 | AT_PARSER_CHECK([./input], 1, [], | |
fb4c8a7c TR |
125 | [m4_default([$4], [1.1]) |
126 | m4_default([$4], [1.1])[: syntax error | |
a1d1ab50 AD |
127 | ]]) |
128 | AT_BISON_OPTION_POPDEFS | |
129 | AT_CLEANUP | |
130 | ]) | |
131 | ||
132 | ## FIXME: test Java, and iterate over skeletons. | |
133 | AT_TEST([yacc.c]) | |
6428a8a4 TR |
134 | AT_TEST([yacc.c], [%define api.pure full]) |
135 | AT_TEST([yacc.c], [%define api.pure %parse-param { int x }]) | |
a1d1ab50 | 136 | AT_TEST([yacc.c], [%define api.push-pull both]) |
6428a8a4 | 137 | AT_TEST([yacc.c], [%define api.push-pull both %define api.pure full]) |
a1d1ab50 | 138 | AT_TEST([glr.c]) |
6428a8a4 | 139 | AT_TEST([glr.c], [%define api.pure]) |
a1d1ab50 AD |
140 | AT_TEST([lalr1.cc]) |
141 | AT_TEST([glr.cc]) | |
142 | ||
fb4c8a7c TR |
143 | ## A very different test, based on PostgreSQL's implementation of the |
144 | ## locations. See | |
145 | ## http://lists.gnu.org/archive/html/bug-bison/2012-11/msg00023.html | |
146 | ## | |
147 | ## Weirdly enough, to trigger the warning with GCC 4.7, we must not | |
148 | ## use fprintf, so run the test twice: once to check the warning | |
149 | ## (absence thereof), and another time to check the value. | |
6428a8a4 | 150 | AT_TEST([yacc.c], [%define api.pure full], |
fb4c8a7c TR |
151 | [[%{ |
152 | # define YYLTYPE int | |
153 | # define YY_LOCATION_PRINT(Stream, Loc) \ | |
154 | (void) (Loc) | |
155 | # define YYLLOC_DEFAULT(Current, Rhs, N) \ | |
156 | (Current) = ((Rhs)[N ? 1 : 0]) | |
157 | %} | |
158 | ]], | |
159 | [@&t@]) | |
160 | ||
6428a8a4 | 161 | AT_TEST([yacc.c], [%define api.pure full], |
fb4c8a7c TR |
162 | [[%{ |
163 | # define YYLTYPE int | |
164 | # define YY_LOCATION_PRINT(Stream, Loc) \ | |
165 | fprintf ((Stream), "%d", (Loc)) | |
166 | # define YYLLOC_DEFAULT(Current, Rhs, N) \ | |
167 | (Current) = ((Rhs)[N ? 1 : 0]) | |
168 | %} | |
169 | ]], | |
170 | [0]) | |
171 | ||
172 | ||
a1d1ab50 | 173 | m4_popdef([AT_TEST]) |
75d1fe16 | 174 | |
5dac0025 PE |
175 | |
176 | ||
3804aa26 AD |
177 | ## ---------------- ## |
178 | ## Location Print. ## | |
179 | ## ---------------- ## | |
180 | ||
181 | # AT_TEST(SKELETON-NAME, DIRECTIVES, [MORE-DIRECTIVES], [LOCATION = 1.1]) | |
182 | # ----------------------------------------------------------------------- | |
183 | # Check that the initial location is correct. | |
184 | m4_pushdef([AT_TEST], | |
185 | [AT_SETUP([Location print: $1 $2]) | |
186 | ||
187 | AT_BISON_OPTION_PUSHDEFS([%locations %skeleton "$1" $2]) | |
188 | AT_DATA_GRAMMAR([[input.y]], | |
189 | [[%defines /* FIXME: Required by lalr1.cc in Bison 2.6. */ | |
190 | %locations | |
191 | %debug | |
192 | %skeleton "$1" | |
193 | ]$2[ | |
194 | ]$3[ | |
195 | %code | |
196 | { | |
197 | # include <stdio.h> | |
198 | # include <stdlib.h> // getenv | |
199 | ]AT_YYERROR_DECLARE[ | |
200 | ]AT_YYLEX_DECLARE[ | |
201 | } | |
202 | %% | |
203 | exp:; | |
204 | %% | |
205 | ]AT_YYERROR_DEFINE[ | |
206 | ]AT_YYLEX_DEFINE[ | |
207 | ||
208 | int | |
209 | main (void) | |
210 | { | |
211 | #define TEST(L1, C1, L2, C2) \ | |
212 | ]AT_LOC_FIRST_LINE[ = L1; \ | |
213 | ]AT_LOC_FIRST_COLUMN[ = C1; \ | |
214 | ]AT_LOC_LAST_LINE[ = L2; \ | |
215 | ]AT_LOC_LAST_COLUMN[ = C2; \ | |
216 | ]YY_LOCATION_PRINT(stdout, AT_LOC)[;\ | |
217 | putchar ('\n'); | |
218 | ||
219 | TEST(1, 1, 1, 1); | |
220 | TEST(2, 1, 2, 10); | |
221 | TEST(3, 1, 4, 1); | |
222 | TEST(5, 1, 6, 10); | |
223 | ||
224 | TEST(7, 2, 0, 2); | |
225 | TEST(8, 0, 8, 0); | |
226 | } | |
227 | ]]) | |
228 | ||
229 | AT_FULL_COMPILE([input]) | |
230 | AT_PARSER_CHECK([./input], 0, | |
231 | [[1.1 | |
232 | 2.1-9 | |
233 | 3.1-4.0 | |
234 | 5.1-6.9 | |
235 | 7.2 | |
236 | 8.0 | |
237 | ]]) | |
238 | AT_BISON_OPTION_POPDEFS | |
239 | AT_CLEANUP | |
240 | ]) | |
241 | ||
242 | ## FIXME: test Java, and iterate over skeletons. | |
243 | AT_TEST([yacc.c]) | |
244 | AT_TEST([glr.c]) | |
245 | #AT_TEST([lalr1.cc]) | |
246 | #AT_TEST([glr.cc]) | |
247 | ||
248 | m4_popdef([AT_TEST]) | |
249 | ||
250 | ||
251 | ||
75d1fe16 AD |
252 | ## ---------------- ## |
253 | ## Exotic Dollars. ## | |
254 | ## ---------------- ## | |
255 | ||
256 | AT_SETUP([Exotic Dollars]) | |
257 | ||
55f48c48 | 258 | AT_BISON_OPTION_PUSHDEFS |
9501dc6e | 259 | AT_DATA_GRAMMAR([[input.y]], |
cf499cff | 260 | [[%define parse.error verbose |
8f3596a6 AD |
261 | %debug |
262 | %{ | |
55f48c48 AD |
263 | ]AT_YYERROR_DECLARE[ |
264 | ]AT_YYLEX_DECLARE[ | |
affac613 | 265 | # define USE(Var) |
75d1fe16 AD |
266 | %} |
267 | ||
268 | %union | |
269 | { | |
270 | int val; | |
271 | }; | |
272 | ||
0ff67d71 | 273 | %type <val> a_1 a_2 a_5 |
378f4bd8 | 274 | sum_of_the_five_previous_values |
75d1fe16 AD |
275 | |
276 | %% | |
0ff67d71 PE |
277 | exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5 |
278 | sum_of_the_five_previous_values | |
75d1fe16 | 279 | { |
84866159 | 280 | USE (($1, $2, $<foo>3, $<foo>4, $5)); |
75d1fe16 AD |
281 | printf ("%d\n", $6); |
282 | } | |
283 | ; | |
284 | a_1: { $$ = 1; }; | |
285 | a_2: { $$ = 2; }; | |
75d1fe16 AD |
286 | a_5: { $$ = 5; }; |
287 | ||
288 | sum_of_the_five_previous_values: | |
289 | { | |
290 | $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4; | |
291 | } | |
292 | ; | |
293 | ||
294 | %% | |
55f48c48 | 295 | ]AT_YYERROR_DEFINE[ |
95361618 | 296 | ]AT_YYLEX_DEFINE[ |
75d1fe16 AD |
297 | int |
298 | main (void) | |
299 | { | |
300 | return yyparse (); | |
301 | } | |
302 | ]]) | |
303 | ||
da730230 | 304 | AT_BISON_CHECK([-d -v -o input.c input.y], 0) |
1154cced AD |
305 | AT_COMPILE([input]) |
306 | AT_PARSER_CHECK([./input], 0, | |
75d1fe16 AD |
307 | [[15 |
308 | ]]) | |
309 | ||
82f3355e JD |
310 | # Make sure that fields after $n or $-n are parsed correctly. At one |
311 | # point while implementing dashes in symbol names, we were dropping | |
312 | # fields after $-n. | |
313 | AT_DATA_GRAMMAR([[input.y]], | |
314 | [[ | |
315 | %{ | |
316 | # include <stdio.h> | |
55f48c48 AD |
317 | ]AT_YYERROR_DECLARE[ |
318 | ]AT_YYLEX_DECLARE[ | |
82f3355e JD |
319 | typedef struct { int val; } stype; |
320 | # define YYSTYPE stype | |
321 | %} | |
322 | ||
323 | %% | |
324 | start: one two { $$.val = $1.val + $2.val; } sum ; | |
325 | one: { $$.val = 1; } ; | |
326 | two: { $$.val = 2; } ; | |
327 | sum: { printf ("%d\n", $0.val + $-1.val + $-2.val); } ; | |
328 | ||
329 | %% | |
55f48c48 | 330 | ]AT_YYERROR_DEFINE[ |
95361618 | 331 | ]AT_YYLEX_DEFINE[ |
82f3355e JD |
332 | int |
333 | main (void) | |
334 | { | |
335 | return yyparse (); | |
336 | } | |
337 | ]]) | |
338 | ||
3112e7a8 | 339 | AT_FULL_COMPILE([input]) |
82f3355e JD |
340 | AT_PARSER_CHECK([[./input]], [[0]], |
341 | [[6 | |
342 | ]]) | |
343 | ||
55f48c48 | 344 | AT_BISON_OPTION_POPDEFS |
75d1fe16 | 345 | AT_CLEANUP |
9280d3ef AD |
346 | |
347 | ||
348 | ||
e776192e AD |
349 | ## -------------------------- ## |
350 | ## Printers and Destructors. ## | |
351 | ## -------------------------- ## | |
9280d3ef | 352 | |
8d6c1b5e AD |
353 | # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, |
354 | # BISON-DIRECTIVE, UNION-FLAG) | |
355 | # ------------------------------------------------------------- | |
3df37415 | 356 | m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR], |
9c66f418 | 357 | [# Make sure complex $n work. |
8d6c1b5e | 358 | m4_if([$1$2$3$4], $[1]$[2]$[3]$[4], [], |
3df37415 AD |
359 | [m4_fatal([$0: Invalid arguments: $@])])dnl |
360 | ||
9c66f418 AD |
361 | # Be sure to pass all the %directives to this macro to have correct |
362 | # helping macros. So don't put any directly in the Bison file. | |
c2729758 | 363 | AT_BISON_OPTION_PUSHDEFS([$5]) |
9501dc6e | 364 | AT_DATA_GRAMMAR([[input.y]], |
16dc6a9e | 365 | [[%code requires { |
9280d3ef AD |
366 | #include <stdio.h> |
367 | #include <stdlib.h> | |
cf806753 | 368 | #include <string.h> |
9c66f418 | 369 | #include <assert.h> |
80ce3401 PE |
370 | |
371 | #define YYINITDEPTH 10 | |
372 | #define YYMAXDEPTH 10 | |
8d6c1b5e AD |
373 | #define RANGE(Location) ]AT_LALR1_CC_IF([(Location).begin.line, (Location).end.line], |
374 | [(Location).first_line, (Location).last_line])[ | |
375 | ||
376 | /* Display the symbol type Symbol. */ | |
377 | #define V(Symbol, Value, Location, Sep) \ | |
378 | fprintf (stderr, #Symbol " (%d@%d-%d)" Sep, Value, RANGE(Location)) | |
379 | } | |
9c66f418 | 380 | |
8d6c1b5e AD |
381 | $5 |
382 | ]m4_ifval([$6], [%union | |
9280d3ef AD |
383 | { |
384 | int ival; | |
ac700aa6 | 385 | }]) |
16dc6a9e JD |
386 | AT_LALR1_CC_IF([%define global_tokens_and_yystype]) |
387 | m4_ifval([$6], [[%code provides {]], [[%code {]]) | |
230a3db4 AD |
388 | AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])[ |
389 | ]AT_YYLEX_DECLARE[ | |
55f48c48 | 390 | ]AT_LALR1_CC_IF([], [AT_YYERROR_DECLARE]) |
9bc0dd67 | 391 | [} |
c2729758 | 392 | |
868d2d96 | 393 | ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[ |
e3170060 | 394 | |
868d2d96 | 395 | /* FIXME: This %printer isn't actually tested. */ |
a5eb1ed2 AD |
396 | %printer |
397 | { | |
9a1e9989 | 398 | ]AT_LALR1_CC_IF([debug_stream () << $$;], |
3fc16193 | 399 | [fprintf (yyoutput, "%d", $$)])[; |
a5eb1ed2 | 400 | } |
9c66f418 | 401 | input line thing 'x' 'y' |
e3170060 AD |
402 | |
403 | %destructor | |
8d6c1b5e | 404 | { fprintf (stderr, "Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e | 405 | input |
5719c109 | 406 | |
7bd6c77e | 407 | %destructor |
8d6c1b5e | 408 | { fprintf (stderr, "Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e AD |
409 | line |
410 | ||
411 | %destructor | |
8d6c1b5e | 412 | { fprintf (stderr, "Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e AD |
413 | thing |
414 | ||
415 | %destructor | |
8d6c1b5e | 416 | { fprintf (stderr, "Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e | 417 | 'x' |
5719c109 | 418 | |
9c66f418 | 419 | %destructor |
8d6c1b5e | 420 | { fprintf (stderr, "Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); } |
9c66f418 AD |
421 | 'y' |
422 | ||
868d2d96 JD |
423 | %token END 0 |
424 | %destructor | |
8d6c1b5e | 425 | { fprintf (stderr, "Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); } |
868d2d96 JD |
426 | END |
427 | ||
9280d3ef | 428 | %% |
9c66f418 AD |
429 | /* |
430 | This grammar is made to exercise error recovery. | |
431 | "Lines" starting with `(' support error recovery, with | |
432 | ')' as synchronizing token. Lines starting with 'x' can never | |
433 | be recovered from if in error. | |
434 | */ | |
435 | ||
9280d3ef AD |
436 | input: |
437 | /* Nothing. */ | |
5719c109 AD |
438 | { |
439 | $$ = 0; | |
8d6c1b5e | 440 | V(input, $$, @$, ": /* Nothing */\n"); |
5719c109 AD |
441 | } |
442 | | line input /* Right recursive to load the stack so that popping at | |
e9690142 | 443 | END can be exercised. */ |
5719c109 AD |
444 | { |
445 | $$ = 2; | |
8d6c1b5e AD |
446 | V(input, $$, @$, ": "); |
447 | V(line, $1, @1, " "); | |
448 | V(input, $2, @2, "\n"); | |
5719c109 | 449 | } |
9280d3ef AD |
450 | ; |
451 | ||
452 | line: | |
453 | thing thing thing ';' | |
5719c109 AD |
454 | { |
455 | $$ = $1; | |
8d6c1b5e AD |
456 | V(line, $$, @$, ": "); |
457 | V(thing, $1, @1, " "); | |
458 | V(thing, $2, @2, " "); | |
459 | V(thing, $3, @3, " "); | |
460 | V(;, $4, @4, "\n"); | |
5719c109 | 461 | } |
9c66f418 | 462 | | '(' thing thing ')' |
5719c109 AD |
463 | { |
464 | $$ = $1; | |
8d6c1b5e AD |
465 | V(line, $$, @$, ": "); |
466 | V('(', $1, @1, " "); | |
467 | V(thing, $2, @2, " "); | |
468 | V(thing, $3, @3, " "); | |
469 | V(')', $4, @4, "\n"); | |
5719c109 | 470 | } |
9c66f418 | 471 | | '(' thing ')' |
5719c109 AD |
472 | { |
473 | $$ = $1; | |
8d6c1b5e AD |
474 | V(line, $$, @$, ": "); |
475 | V('(', $1, @1, " "); | |
476 | V(thing, $2, @2, " "); | |
477 | V(')', $3, @3, "\n"); | |
5719c109 | 478 | } |
9c66f418 | 479 | | '(' error ')' |
5719c109 AD |
480 | { |
481 | $$ = -1; | |
8d6c1b5e AD |
482 | V(line, $$, @$, ": "); |
483 | V('(', $1, @1, " "); | |
484 | fprintf (stderr, "error (@%d-%d) ", RANGE(@2)); | |
485 | V(')', $3, @3, "\n"); | |
5719c109 | 486 | } |
9280d3ef AD |
487 | ; |
488 | ||
489 | thing: | |
5719c109 AD |
490 | 'x' |
491 | { | |
492 | $$ = $1; | |
8d6c1b5e AD |
493 | V(thing, $$, @$, ": "); |
494 | V('x', $1, @1, "\n"); | |
5719c109 | 495 | } |
9280d3ef AD |
496 | ; |
497 | %% | |
9c66f418 | 498 | /* Alias to ARGV[1]. */ |
ef51bfa7 | 499 | const char *source = YY_NULL; |
9c66f418 | 500 | |
8d6c1b5e AD |
501 | ]AT_YYERROR_DEFINE[ |
502 | ||
230a3db4 AD |
503 | static |
504 | ]AT_YYLEX_PROTOTYPE[ | |
9280d3ef | 505 | { |
5a08f1ce | 506 | static unsigned int counter = 0; |
9280d3ef | 507 | |
9c66f418 AD |
508 | int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++; |
509 | /* As in BASIC, line numbers go from 10 to 10. */ | |
8d6c1b5e AD |
510 | ]AT_LOC_FIRST_LINE[ = ]AT_LOC_FIRST_COLUMN[ = 10 * c; |
511 | ]AT_LOC_LAST_LINE[ = ]AT_LOC_LAST_COLUMN[ = ]AT_LOC_FIRST_LINE[ + 9; | |
77519a7d | 512 | assert (0 <= c && c <= strlen (source)); |
a9739e7c | 513 | if (source[c]) |
8d6c1b5e | 514 | fprintf (stderr, "sending: '%c'", source[c]); |
9280d3ef | 515 | else |
8d6c1b5e AD |
516 | fprintf (stderr, "sending: END"); |
517 | fprintf (stderr, " (%d@%d-%d)\n", c, RANGE (]AT_LOC[)); | |
a9739e7c | 518 | return source[c]; |
9280d3ef | 519 | } |
c2729758 | 520 | ]AT_LALR1_CC_IF( |
8d6c1b5e | 521 | [static bool yydebug; |
c2729758 ADL |
522 | int |
523 | yyparse () | |
524 | { | |
99880de5 | 525 | yy::parser parser; |
a3cb6248 | 526 | parser.set_debug_level (yydebug); |
c2729758 ADL |
527 | return parser.parse (); |
528 | } | |
8d6c1b5e | 529 | ])[ |
9280d3ef | 530 | |
9280d3ef | 531 | int |
9c66f418 | 532 | main (int argc, const char *argv[]) |
9280d3ef | 533 | { |
6100a9aa | 534 | int status; |
9280d3ef | 535 | yydebug = !!getenv ("YYDEBUG"); |
9c66f418 | 536 | assert (argc == 2); |
a9739e7c | 537 | source = argv[1]; |
6100a9aa PE |
538 | status = yyparse (); |
539 | switch (status) | |
9280d3ef | 540 | { |
8d6c1b5e AD |
541 | case 0: fprintf (stderr, "Successful parse.\n"); break; |
542 | case 1: fprintf (stderr, "Parsing FAILED.\n"); break; | |
543 | default: fprintf (stderr, "Parsing FAILED (status %d).\n", status); break; | |
9280d3ef | 544 | } |
6100a9aa | 545 | return status; |
9280d3ef AD |
546 | } |
547 | ]]) | |
548 | ||
7ca2266a | 549 | AT_FULL_COMPILE([input]) |
9c66f418 AD |
550 | |
551 | ||
552 | # Check the location of "empty" | |
553 | # ----------------------------- | |
554 | # I.e., epsilon-reductions, as in "(x)" which ends by reducing | |
555 | # an empty "line" nterm. | |
556 | # FIXME: This location is not satisfying. Depend on the lookahead? | |
8d6c1b5e | 557 | AT_PARSER_CHECK([./input '(x)'], 0, [], |
9c66f418 AD |
558 | [[sending: '(' (0@0-9) |
559 | sending: 'x' (1@10-19) | |
560 | thing (1@10-19): 'x' (1@10-19) | |
561 | sending: ')' (2@20-29) | |
562 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
868d2d96 | 563 | sending: END (3@30-39) |
b4a20338 AD |
564 | input (0@29-29): /* Nothing */ |
565 | input (2@0-29): line (0@0-29) input (0@29-29) | |
868d2d96 | 566 | Freeing token END (3@30-39) |
258b75ca | 567 | Freeing nterm input (2@0-29) |
9c66f418 AD |
568 | Successful parse. |
569 | ]]) | |
570 | ||
571 | ||
572 | # Check locations in error recovery | |
573 | # --------------------------------- | |
574 | # '(y)' is an error, but can be recovered from. But what's the location | |
575 | # of the error itself ('y'), and of the resulting reduction ('(error)'). | |
8d6c1b5e | 576 | AT_PARSER_CHECK([./input '(y)'], 0, [], |
9c66f418 AD |
577 | [[sending: '(' (0@0-9) |
578 | sending: 'y' (1@10-19) | |
8d6c1b5e | 579 | 10.10-19.18: syntax error, unexpected 'y', expecting 'x' |
9c66f418 AD |
580 | Freeing token 'y' (1@10-19) |
581 | sending: ')' (2@20-29) | |
582 | line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29) | |
868d2d96 | 583 | sending: END (3@30-39) |
b4a20338 AD |
584 | input (0@29-29): /* Nothing */ |
585 | input (2@0-29): line (-1@0-29) input (0@29-29) | |
868d2d96 | 586 | Freeing token END (3@30-39) |
258b75ca | 587 | Freeing nterm input (2@0-29) |
9c66f418 AD |
588 | Successful parse. |
589 | ]]) | |
590 | ||
591 | ||
592 | # Syntax errors caught by the parser | |
593 | # ---------------------------------- | |
594 | # Exercise the discarding of stack top and input until `error' | |
595 | # can be reduced. | |
596 | # | |
597 | # '(', 'x', 'x', 'x', 'x', 'x', ')', | |
598 | # | |
599 | # Load the stack and provoke an error that cannot be caught by the | |
600 | # grammar, to check that the stack is cleared. And make sure the | |
601 | # lookahead is freed. | |
602 | # | |
603 | # '(', 'x', ')', | |
604 | # '(', 'x', ')', | |
605 | # 'y' | |
8d6c1b5e | 606 | AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1, [], |
9c66f418 | 607 | [[sending: '(' (0@0-9) |
4c6cc1db AD |
608 | sending: 'x' (1@10-19) |
609 | thing (1@10-19): 'x' (1@10-19) | |
610 | sending: 'x' (2@20-29) | |
611 | thing (2@20-29): 'x' (2@20-29) | |
612 | sending: 'x' (3@30-39) | |
8d6c1b5e | 613 | 30.30-39.38: syntax error, unexpected 'x', expecting ')' |
4c6cc1db AD |
614 | Freeing nterm thing (2@20-29) |
615 | Freeing nterm thing (1@10-19) | |
4c6cc1db AD |
616 | Freeing token 'x' (3@30-39) |
617 | sending: 'x' (4@40-49) | |
618 | Freeing token 'x' (4@40-49) | |
619 | sending: 'x' (5@50-59) | |
620 | Freeing token 'x' (5@50-59) | |
9c66f418 AD |
621 | sending: ')' (6@60-69) |
622 | line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69) | |
623 | sending: '(' (7@70-79) | |
4c6cc1db AD |
624 | sending: 'x' (8@80-89) |
625 | thing (8@80-89): 'x' (8@80-89) | |
9c66f418 AD |
626 | sending: ')' (9@90-99) |
627 | line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99) | |
628 | sending: '(' (10@100-109) | |
629 | sending: 'x' (11@110-119) | |
630 | thing (11@110-119): 'x' (11@110-119) | |
631 | sending: ')' (12@120-129) | |
632 | line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129) | |
633 | sending: 'y' (13@130-139) | |
b4a20338 AD |
634 | input (0@129-129): /* Nothing */ |
635 | input (2@100-129): line (10@100-129) input (0@129-129) | |
9c66f418 AD |
636 | input (2@70-129): line (7@70-99) input (2@100-129) |
637 | input (2@0-129): line (-1@0-69) input (2@70-129) | |
8d6c1b5e | 638 | 130.130-139.138: syntax error, unexpected 'y', expecting END |
9c66f418 AD |
639 | Freeing nterm input (2@0-129) |
640 | Freeing token 'y' (13@130-139) | |
5719c109 | 641 | Parsing FAILED. |
9280d3ef AD |
642 | ]]) |
643 | ||
868d2d96 JD |
644 | |
645 | # Syntax error caught by the parser where lookahead = END | |
646 | # -------------------------------------------------------- | |
647 | # Load the stack and provoke an error that cannot be caught by the | |
648 | # grammar, to check that the stack is cleared. And make sure the | |
649 | # lookahead is freed. | |
650 | # | |
651 | # '(', 'x', ')', | |
652 | # '(', 'x', ')', | |
653 | # 'x' | |
8d6c1b5e | 654 | AT_PARSER_CHECK([./input '(x)(x)x'], 1, [], |
868d2d96 JD |
655 | [[sending: '(' (0@0-9) |
656 | sending: 'x' (1@10-19) | |
657 | thing (1@10-19): 'x' (1@10-19) | |
658 | sending: ')' (2@20-29) | |
659 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
660 | sending: '(' (3@30-39) | |
661 | sending: 'x' (4@40-49) | |
662 | thing (4@40-49): 'x' (4@40-49) | |
663 | sending: ')' (5@50-59) | |
664 | line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59) | |
665 | sending: 'x' (6@60-69) | |
666 | thing (6@60-69): 'x' (6@60-69) | |
667 | sending: END (7@70-79) | |
8d6c1b5e | 668 | 70.70-79.78: syntax error, unexpected END, expecting 'x' |
868d2d96 JD |
669 | Freeing nterm thing (6@60-69) |
670 | Freeing nterm line (3@30-59) | |
671 | Freeing nterm line (0@0-29) | |
672 | Freeing token END (7@70-79) | |
673 | Parsing FAILED. | |
674 | ]]) | |
675 | ||
676 | ||
80ce3401 PE |
677 | # Check destruction upon stack overflow |
678 | # ------------------------------------- | |
679 | # Upon stack overflow, all symbols on the stack should be destroyed. | |
680 | # Only check for yacc.c. | |
681 | AT_YACC_IF([ | |
8d6c1b5e | 682 | AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2, [], |
80ce3401 PE |
683 | [[sending: '(' (0@0-9) |
684 | sending: 'x' (1@10-19) | |
685 | thing (1@10-19): 'x' (1@10-19) | |
686 | sending: ')' (2@20-29) | |
687 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
688 | sending: '(' (3@30-39) | |
689 | sending: 'x' (4@40-49) | |
690 | thing (4@40-49): 'x' (4@40-49) | |
691 | sending: ')' (5@50-59) | |
692 | line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59) | |
693 | sending: '(' (6@60-69) | |
694 | sending: 'x' (7@70-79) | |
695 | thing (7@70-79): 'x' (7@70-79) | |
696 | sending: ')' (8@80-89) | |
697 | line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89) | |
698 | sending: '(' (9@90-99) | |
699 | sending: 'x' (10@100-109) | |
700 | thing (10@100-109): 'x' (10@100-109) | |
701 | sending: ')' (11@110-119) | |
702 | line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119) | |
703 | sending: '(' (12@120-129) | |
704 | sending: 'x' (13@130-139) | |
705 | thing (13@130-139): 'x' (13@130-139) | |
706 | sending: ')' (14@140-149) | |
707 | line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149) | |
708 | sending: '(' (15@150-159) | |
709 | sending: 'x' (16@160-169) | |
710 | thing (16@160-169): 'x' (16@160-169) | |
711 | sending: ')' (17@170-179) | |
712 | line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179) | |
713 | sending: '(' (18@180-189) | |
714 | sending: 'x' (19@190-199) | |
715 | thing (19@190-199): 'x' (19@190-199) | |
716 | sending: ')' (20@200-209) | |
8d6c1b5e | 717 | 200.200-209.208: memory exhausted |
80ce3401 PE |
718 | Freeing nterm thing (19@190-199) |
719 | Freeing nterm line (15@150-179) | |
720 | Freeing nterm line (12@120-149) | |
721 | Freeing nterm line (9@90-119) | |
722 | Freeing nterm line (6@60-89) | |
723 | Freeing nterm line (3@30-59) | |
724 | Freeing nterm line (0@0-29) | |
6100a9aa | 725 | Parsing FAILED (status 2). |
80ce3401 PE |
726 | ]]) |
727 | ]) | |
728 | ||
55f48c48 AD |
729 | AT_BISON_OPTION_POPDEFS |
730 | ])# _AT_CHECK_PRINTER_AND_DESTRUCTOR | |
3df37415 AD |
731 | |
732 | ||
a14a26fa | 733 | # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG]) |
046ac74e | 734 | # --------------------------------------------------------------------------- |
3df37415 | 735 | m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR], |
623a5147 | 736 | [AT_SETUP([Printers and Destructors$2]m4_ifval([$1], [[: $1]])) |
9c66f418 | 737 | |
a14a26fa | 738 | $3 |
9c66f418 | 739 | _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4], |
cf499cff | 740 | [%define parse.error verbose |
9c66f418 AD |
741 | %debug |
742 | %verbose | |
743 | %locations | |
744 | $1], [$2]) | |
745 | ||
746 | AT_CLEANUP | |
3df37415 AD |
747 | ]) |
748 | ||
749 | ||
ac700aa6 PE |
750 | AT_CHECK_PRINTER_AND_DESTRUCTOR([]) |
751 | AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union]) | |
046ac74e | 752 | |
fd19f271 AD |
753 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"]) |
754 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union]) | |
046ac74e | 755 | |
1576d44d AD |
756 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser]) |
757 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union]) | |
ec5479ce JD |
758 | |
759 | ||
760 | ||
12e35840 JD |
761 | ## ----------------------------------------- ## |
762 | ## Default tagless %printer and %destructor. ## | |
763 | ## ----------------------------------------- ## | |
ec5479ce JD |
764 | |
765 | # Check that the right %printer and %destructor are called, that they're not | |
766 | # called for $end, and that $$ and @$ work correctly. | |
767 | ||
12e35840 | 768 | AT_SETUP([Default tagless %printer and %destructor]) |
55f48c48 | 769 | AT_BISON_OPTION_PUSHDEFS([%locations]) |
ec5479ce | 770 | AT_DATA_GRAMMAR([[input.y]], |
cf499cff | 771 | [[%define parse.error verbose |
ec5479ce JD |
772 | %debug |
773 | %locations | |
ec5479ce JD |
774 | |
775 | %{ | |
776 | # include <stdio.h> | |
777 | # include <stdlib.h> | |
55f48c48 AD |
778 | ]AT_YYLEX_DECLARE[ |
779 | ]AT_YYERROR_DECLARE[ | |
ec5479ce JD |
780 | # define USE(SYM) |
781 | %} | |
782 | ||
783 | %printer { | |
12e35840 JD |
784 | fprintf (yyoutput, "<*> printer should not be called.\n"); |
785 | } <*> | |
786 | ||
787 | %printer { | |
3ebecc24 JD |
788 | fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column); |
789 | } <> | |
ec5479ce | 790 | %destructor { |
3ebecc24 JD |
791 | fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column); |
792 | } <> | |
ec5479ce JD |
793 | |
794 | %printer { | |
795 | fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column); | |
796 | } 'b' 'c' | |
797 | %destructor { | |
798 | fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column); | |
799 | } 'b' 'c' | |
800 | ||
12e35840 JD |
801 | %destructor { |
802 | fprintf (yyoutput, "<*> destructor should not be called.\n"); | |
803 | } <*> | |
804 | ||
ec5479ce JD |
805 | %% |
806 | ||
807 | start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ; | |
808 | ||
809 | %% | |
55f48c48 | 810 | ]AT_YYERROR_DEFINE[ |
95361618 | 811 | ]AT_YYLEX_DEFINE(["abcd"], [[yylval = res]])[ |
ec5479ce JD |
812 | |
813 | int | |
814 | main (void) | |
815 | { | |
816 | yydebug = 1; | |
817 | return yyparse (); | |
818 | } | |
819 | ]]) | |
820 | ||
9534d2be | 821 | AT_BISON_CHECK([-o input.c input.y], [], [], |
e42906f7 AD |
822 | [[input.y:23.3-5: warning: useless %destructor for type <*> [-Wother] |
823 | input.y:23.3-5: warning: useless %printer for type <*> [-Wother] | |
9534d2be | 824 | ]]) |
ec5479ce JD |
825 | AT_COMPILE([input]) |
826 | AT_PARSER_CHECK([./input], 1, | |
3ebecc24 | 827 | [[<> destructor for 'd' @ 4. |
ec5479ce JD |
828 | 'b'/'c' destructor for 'c' @ 3. |
829 | 'b'/'c' destructor for 'b' @ 2. | |
3ebecc24 | 830 | <> destructor for 'a' @ 1. |
ec5479ce JD |
831 | ]], |
832 | [[Starting parse | |
833 | Entering state 0 | |
3237f570 AD |
834 | Reading a token: Next token is token 'a' (1.1: <> printer for 'a' @ 1) |
835 | Shifting token 'a' (1.1: <> printer for 'a' @ 1) | |
ec5479ce | 836 | Entering state 1 |
3237f570 AD |
837 | Reading a token: Next token is token 'b' (1.2: 'b'/'c' printer for 'b' @ 2) |
838 | Shifting token 'b' (1.2: 'b'/'c' printer for 'b' @ 2) | |
ec5479ce | 839 | Entering state 3 |
3237f570 AD |
840 | Reading a token: Next token is token 'c' (1.3: 'b'/'c' printer for 'c' @ 3) |
841 | Shifting token 'c' (1.3: 'b'/'c' printer for 'c' @ 3) | |
ec5479ce | 842 | Entering state 5 |
3237f570 AD |
843 | Reading a token: Next token is token 'd' (1.4: <> printer for 'd' @ 4) |
844 | Shifting token 'd' (1.4: <> printer for 'd' @ 4) | |
ec5479ce JD |
845 | Entering state 6 |
846 | Reading a token: Now at end of input. | |
7dbb8d8a | 847 | 1.5: syntax error, unexpected $end, expecting 'e' |
3237f570 | 848 | Error: popping token 'd' (1.4: <> printer for 'd' @ 4) |
ec5479ce | 849 | Stack now 0 1 3 5 |
3237f570 | 850 | Error: popping token 'c' (1.3: 'b'/'c' printer for 'c' @ 3) |
ec5479ce | 851 | Stack now 0 1 3 |
3237f570 | 852 | Error: popping token 'b' (1.2: 'b'/'c' printer for 'b' @ 2) |
ec5479ce | 853 | Stack now 0 1 |
3237f570 | 854 | Error: popping token 'a' (1.1: <> printer for 'a' @ 1) |
ec5479ce | 855 | Stack now 0 |
3237f570 | 856 | Cleanup: discarding lookahead token $end (1.5: ) |
ec5479ce JD |
857 | Stack now 0 |
858 | ]]) | |
859 | ||
55f48c48 | 860 | AT_BISON_OPTION_POPDEFS |
ec5479ce JD |
861 | AT_CLEANUP |
862 | ||
863 | ||
864 | ||
12e35840 JD |
865 | ## ------------------------------------------------------ ## |
866 | ## Default tagged and per-type %printer and %destructor. ## | |
867 | ## ------------------------------------------------------ ## | |
b2a0b7ca | 868 | |
12e35840 | 869 | AT_SETUP([Default tagged and per-type %printer and %destructor]) |
55f48c48 | 870 | AT_BISON_OPTION_PUSHDEFS |
b2a0b7ca | 871 | AT_DATA_GRAMMAR([[input.y]], |
cf499cff | 872 | [[%define parse.error verbose |
b2a0b7ca JD |
873 | %debug |
874 | ||
875 | %{ | |
876 | # include <stdio.h> | |
877 | # include <stdlib.h> | |
55f48c48 AD |
878 | ]AT_YYERROR_DECLARE[ |
879 | ]AT_YYLEX_DECLARE[ | |
b2a0b7ca JD |
880 | # define USE(SYM) |
881 | %} | |
882 | ||
12e35840 | 883 | %printer { |
3ebecc24 JD |
884 | fprintf (yyoutput, "<> printer should not be called.\n"); |
885 | } <> | |
12e35840 | 886 | |
b2a0b7ca JD |
887 | %union { int field0; int field1; int field2; } |
888 | %type <field0> start 'a' 'g' | |
889 | %type <field1> 'e' | |
890 | %type <field2> 'f' | |
891 | %printer { | |
12e35840 JD |
892 | fprintf (yyoutput, "<*>/<field2>/e printer"); |
893 | } <*> 'e' <field2> | |
b2a0b7ca | 894 | %destructor { |
12e35840 JD |
895 | fprintf (stdout, "<*>/<field2>/e destructor.\n"); |
896 | } <*> 'e' <field2> | |
b2a0b7ca JD |
897 | |
898 | %type <field1> 'b' | |
899 | %printer { fprintf (yyoutput, "<field1> printer"); } <field1> | |
900 | %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1> | |
901 | ||
902 | %type <field0> 'c' | |
903 | %printer { fprintf (yyoutput, "'c' printer"); } 'c' | |
904 | %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c' | |
905 | ||
906 | %type <field1> 'd' | |
907 | %printer { fprintf (yyoutput, "'d' printer"); } 'd' | |
908 | %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd' | |
909 | ||
12e35840 | 910 | %destructor { |
3ebecc24 JD |
911 | fprintf (yyoutput, "<> destructor should not be called.\n"); |
912 | } <> | |
12e35840 | 913 | |
b2a0b7ca JD |
914 | %% |
915 | ||
916 | start: | |
917 | 'a' 'b' 'c' 'd' 'e' 'f' 'g' | |
918 | { | |
919 | USE(($1, $2, $3, $4, $5, $6, $7)); | |
920 | $$ = 'S'; | |
921 | } | |
922 | ; | |
923 | ||
924 | %% | |
55f48c48 | 925 | ]AT_YYERROR_DEFINE[ |
95361618 | 926 | ]AT_YYLEX_DEFINE(["abcdef"])[ |
b2a0b7ca JD |
927 | |
928 | int | |
929 | main (void) | |
930 | { | |
931 | yydebug = 1; | |
932 | return yyparse (); | |
933 | } | |
934 | ]]) | |
935 | ||
9534d2be AD |
936 | AT_BISON_CHECK([-o input.c input.y], [], [], |
937 | [[input.y:22.3-4: warning: useless %destructor for type <> [-Wother] | |
938 | input.y:22.3-4: warning: useless %printer for type <> [-Wother] | |
939 | ]]) | |
b2a0b7ca JD |
940 | AT_COMPILE([input]) |
941 | AT_PARSER_CHECK([./input], 1, | |
12e35840 JD |
942 | [[<*>/<field2>/e destructor. |
943 | <*>/<field2>/e destructor. | |
b2a0b7ca JD |
944 | 'd' destructor. |
945 | 'c' destructor. | |
946 | <field1> destructor. | |
12e35840 | 947 | <*>/<field2>/e destructor. |
b2a0b7ca JD |
948 | ]], |
949 | [[Starting parse | |
950 | Entering state 0 | |
12e35840 JD |
951 | Reading a token: Next token is token 'a' (<*>/<field2>/e printer) |
952 | Shifting token 'a' (<*>/<field2>/e printer) | |
b2a0b7ca JD |
953 | Entering state 1 |
954 | Reading a token: Next token is token 'b' (<field1> printer) | |
955 | Shifting token 'b' (<field1> printer) | |
956 | Entering state 3 | |
957 | Reading a token: Next token is token 'c' ('c' printer) | |
958 | Shifting token 'c' ('c' printer) | |
959 | Entering state 5 | |
960 | Reading a token: Next token is token 'd' ('d' printer) | |
961 | Shifting token 'd' ('d' printer) | |
962 | Entering state 6 | |
12e35840 JD |
963 | Reading a token: Next token is token 'e' (<*>/<field2>/e printer) |
964 | Shifting token 'e' (<*>/<field2>/e printer) | |
b2a0b7ca | 965 | Entering state 7 |
12e35840 JD |
966 | Reading a token: Next token is token 'f' (<*>/<field2>/e printer) |
967 | Shifting token 'f' (<*>/<field2>/e printer) | |
b2a0b7ca JD |
968 | Entering state 8 |
969 | Reading a token: Now at end of input. | |
970 | syntax error, unexpected $end, expecting 'g' | |
12e35840 | 971 | Error: popping token 'f' (<*>/<field2>/e printer) |
b2a0b7ca | 972 | Stack now 0 1 3 5 6 7 |
12e35840 | 973 | Error: popping token 'e' (<*>/<field2>/e printer) |
b2a0b7ca JD |
974 | Stack now 0 1 3 5 6 |
975 | Error: popping token 'd' ('d' printer) | |
976 | Stack now 0 1 3 5 | |
977 | Error: popping token 'c' ('c' printer) | |
978 | Stack now 0 1 3 | |
979 | Error: popping token 'b' (<field1> printer) | |
980 | Stack now 0 1 | |
12e35840 | 981 | Error: popping token 'a' (<*>/<field2>/e printer) |
b2a0b7ca JD |
982 | Stack now 0 |
983 | Cleanup: discarding lookahead token $end () | |
984 | Stack now 0 | |
985 | ]]) | |
986 | ||
55f48c48 | 987 | AT_BISON_OPTION_POPDEFS |
b2a0b7ca JD |
988 | AT_CLEANUP |
989 | ||
990 | ||
991 | ||
ec5479ce | 992 | ## ------------------------------------------------------------- ## |
12e35840 | 993 | ## Default %printer and %destructor for user-defined end token. ## |
ec5479ce JD |
994 | ## ------------------------------------------------------------- ## |
995 | ||
3508ce36 | 996 | AT_SETUP([Default %printer and %destructor for user-defined end token]) |
ec5479ce | 997 | |
9534d2be AD |
998 | # AT_TEST(TYPED) |
999 | # -------------- | |
1000 | m4_pushdef([AT_TEST], | |
12e35840 | 1001 | [m4_if($1, 0, |
3ebecc24 JD |
1002 | [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])], |
1003 | [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])]) | |
12e35840 | 1004 | |
55f48c48 | 1005 | AT_BISON_OPTION_PUSHDEFS([%locations]) |
12e35840 | 1006 | AT_DATA_GRAMMAR([[input]]$1[[.y]], |
cf499cff | 1007 | [[%define parse.error verbose |
ec5479ce JD |
1008 | %debug |
1009 | %locations | |
ec5479ce JD |
1010 | |
1011 | %{ | |
1012 | # include <stdio.h> | |
1013 | # include <stdlib.h> | |
55f48c48 AD |
1014 | ]AT_YYERROR_DECLARE[ |
1015 | ]AT_YYLEX_DECLARE[ | |
ec5479ce JD |
1016 | # define USE(SYM) |
1017 | %} | |
1018 | ||
12e35840 JD |
1019 | %destructor { |
1020 | fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n"); | |
1021 | } <]]not_kind[[> | |
1022 | ||
ec5479ce JD |
1023 | %token END 0 |
1024 | %printer { | |
12e35840 JD |
1025 | fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column); |
1026 | } <]]kind[[> | |
ec5479ce | 1027 | %destructor { |
12e35840 JD |
1028 | fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column); |
1029 | } <]]kind[[> | |
1030 | ||
1031 | %printer { | |
1032 | fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n"); | |
1033 | } <]]not_kind[[> | |
1034 | ||
1035 | ]]m4_if($1, 0, [[[ | |
1036 | ]]], | |
1037 | [[[%union { char tag; } | |
1038 | %type <tag> start END]]])[[ | |
ec5479ce JD |
1039 | |
1040 | %% | |
1041 | ||
1042 | start: { $$ = 'S'; } ; | |
1043 | ||
1044 | %% | |
1045 | ||
1046 | static int | |
1047 | yylex (void) | |
1048 | { | |
cf806753 PE |
1049 | static int called; |
1050 | if (called++) | |
1051 | abort (); | |
12e35840 | 1052 | yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E'; |
ec5479ce JD |
1053 | yylloc.first_line = yylloc.last_line = 1; |
1054 | yylloc.first_column = yylloc.last_column = 1; | |
1055 | return 0; | |
1056 | } | |
55f48c48 | 1057 | ]AT_YYERROR_DEFINE[ |
ec5479ce JD |
1058 | |
1059 | int | |
1060 | main (void) | |
1061 | { | |
1062 | yydebug = 1; | |
1063 | return yyparse (); | |
1064 | } | |
1065 | ]]) | |
55f48c48 | 1066 | AT_BISON_OPTION_POPDEFS |
ec5479ce | 1067 | |
9534d2be AD |
1068 | AT_BISON_CHECK([-o input$1.c input$1.y], [], [], |
1069 | [m4_if([$1], [0], | |
e42906f7 AD |
1070 | [[input0.y:23.3-5: warning: useless %destructor for type <*> [-Wother] |
1071 | input0.y:23.3-5: warning: useless %printer for type <*> [-Wother] | |
9534d2be | 1072 | ]], |
e42906f7 AD |
1073 | [[input1.y:23.3-4: warning: useless %destructor for type <> [-Wother] |
1074 | input1.y:23.3-4: warning: useless %printer for type <> [-Wother] | |
9534d2be AD |
1075 | ]])]) |
1076 | ||
12e35840 | 1077 | AT_COMPILE([input$1]) |
9534d2be | 1078 | |
12e35840 JD |
1079 | AT_PARSER_CHECK([./input$1], 0, |
1080 | [[<]]kind[[> for 'E' @ 1. | |
1081 | <]]kind[[> for 'S' @ 1. | |
ec5479ce JD |
1082 | ]], |
1083 | [[Starting parse | |
1084 | Entering state 0 | |
d03c0413 | 1085 | Reducing stack by rule 1 (line 42): |
3237f570 | 1086 | -> $$ = nterm start (1.1: <]]kind[[> for 'S' @ 1) |
ec5479ce JD |
1087 | Stack now 0 |
1088 | Entering state 1 | |
1089 | Reading a token: Now at end of input. | |
3237f570 | 1090 | Shifting token END (1.1: <]]kind[[> for 'E' @ 1) |
ec5479ce JD |
1091 | Entering state 2 |
1092 | Stack now 0 1 2 | |
3237f570 AD |
1093 | Cleanup: popping token END (1.1: <]]kind[[> for 'E' @ 1) |
1094 | Cleanup: popping nterm start (1.1: <]]kind[[> for 'S' @ 1) | |
ec5479ce JD |
1095 | ]]) |
1096 | ||
12e35840 JD |
1097 | m4_popdef([kind]) |
1098 | m4_popdef([not_kind]) | |
1099 | ]) | |
1100 | ||
9534d2be AD |
1101 | AT_TEST(0) |
1102 | AT_TEST(1) | |
1103 | ||
1104 | m4_popdef([AT_TEST]) | |
12e35840 | 1105 | |
ec5479ce | 1106 | AT_CLEANUP |
9350499c JD |
1107 | |
1108 | ||
1109 | ||
1110 | ## ------------------------------------------------------------------ ## | |
1111 | ## Default %printer and %destructor are not for error or $undefined. ## | |
1112 | ## ------------------------------------------------------------------ ## | |
1113 | ||
287b314e | 1114 | AT_SETUP([Default %printer and %destructor are not for error or $undefined]) |
9350499c JD |
1115 | |
1116 | # If Bison were to apply the default %printer and %destructor to the error | |
1117 | # token or to $undefined: | |
1118 | # - For the error token: | |
1119 | # - It would generate warnings for unused $n. | |
1120 | # - It would invoke the %printer and %destructor on the error token's | |
1121 | # semantic value, which would be initialized from the lookahead, which | |
1122 | # would be destroyed separately. | |
1123 | # - For $undefined, who knows what the semantic value would be. | |
55f48c48 | 1124 | AT_BISON_OPTION_PUSHDEFS |
9350499c JD |
1125 | AT_DATA_GRAMMAR([[input.y]], |
1126 | [[%debug | |
1127 | ||
1128 | %{ | |
1129 | # include <stdio.h> | |
cf806753 | 1130 | # include <stdlib.h> |
55f48c48 AD |
1131 | ]AT_YYERROR_DECLARE[ |
1132 | ]AT_YYLEX_DECLARE[ | |
9350499c JD |
1133 | # define USE(SYM) |
1134 | %} | |
1135 | ||
1136 | %printer { | |
1137 | fprintf (yyoutput, "'%c'", $$); | |
3ebecc24 | 1138 | } <> <*> |
9350499c JD |
1139 | %destructor { |
1140 | fprintf (stderr, "DESTROY '%c'\n", $$); | |
3ebecc24 | 1141 | } <> <*> |
9350499c JD |
1142 | |
1143 | %% | |
1144 | ||
1145 | start: | |
1146 | { $$ = 'S'; } | |
1147 | /* In order to reveal the problems that this bug caused during parsing, add | |
1148 | * $2 to USE. */ | |
1149 | | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; } | |
1150 | ; | |
1151 | ||
1152 | %% | |
55f48c48 | 1153 | ]AT_YYERROR_DEFINE[ |
95361618 | 1154 | ]AT_YYLEX_DEFINE(["abd"], [yylval = res])[ |
9350499c JD |
1155 | int |
1156 | main (void) | |
1157 | { | |
1158 | yydebug = 1; | |
1159 | return yyparse (); | |
1160 | } | |
1161 | ]]) | |
55f48c48 | 1162 | AT_BISON_OPTION_POPDEFS |
9350499c | 1163 | |
9534d2be AD |
1164 | AT_BISON_CHECK([-o input.c input.y], [], [], |
1165 | [[input.y:21.6-8: warning: useless %destructor for type <*> [-Wother] | |
1166 | input.y:21.6-8: warning: useless %printer for type <*> [-Wother] | |
1167 | ]]) | |
9350499c JD |
1168 | AT_COMPILE([input]) |
1169 | AT_PARSER_CHECK([./input], [1], [], | |
1170 | [[Starting parse | |
1171 | Entering state 0 | |
1172 | Reading a token: Next token is token 'a' ('a') | |
1173 | Shifting token 'a' ('a') | |
1174 | Entering state 1 | |
1175 | Reading a token: Next token is token 'b' ('b') | |
1176 | syntax error | |
1177 | Shifting token error () | |
1178 | Entering state 3 | |
1179 | Next token is token 'b' ('b') | |
1180 | Shifting token 'b' ('b') | |
1181 | Entering state 5 | |
1182 | Reading a token: Next token is token $undefined () | |
1183 | Error: popping token 'b' ('b') | |
1184 | DESTROY 'b' | |
1185 | Stack now 0 1 3 | |
1186 | Error: popping token error () | |
1187 | Stack now 0 1 | |
1188 | Shifting token error () | |
1189 | Entering state 3 | |
1190 | Next token is token $undefined () | |
1191 | Error: discarding token $undefined () | |
1192 | Error: popping token error () | |
1193 | Stack now 0 1 | |
1194 | Shifting token error () | |
1195 | Entering state 3 | |
1196 | Reading a token: Now at end of input. | |
1197 | Cleanup: discarding lookahead token $end () | |
1198 | Stack now 0 1 3 | |
1199 | Cleanup: popping token error () | |
1200 | Cleanup: popping token 'a' ('a') | |
1201 | DESTROY 'a' | |
1202 | ]]) | |
1203 | ||
1204 | AT_CLEANUP | |
1205 | ||
1206 | ||
1207 | ||
1208 | ## ------------------------------------------------------ ## | |
1209 | ## Default %printer and %destructor are not for $accept. ## | |
1210 | ## ------------------------------------------------------ ## | |
1211 | ||
287b314e | 1212 | AT_SETUP([Default %printer and %destructor are not for $accept]) |
9350499c JD |
1213 | |
1214 | # If YYSTYPE is a union and Bison were to apply the default %printer and | |
1215 | # %destructor to $accept: | |
1216 | # - The %printer and %destructor code generated for $accept would always be | |
1217 | # dead code because $accept is currently never shifted onto the stack. | |
1218 | # - $$ for $accept would always be of type YYSTYPE because it's not possible | |
1219 | # to declare `%type <field> $accept'. (Also true for $undefined.) | |
1220 | # - Thus, the compiler might complain that the user code assumes the wrong | |
1221 | # type for $$ since the code might assume the type associated with a | |
1222 | # specific union field, which is especially reasonable in C++ since that | |
1223 | # type may be a base type. This test case checks for this problem. (Also | |
1224 | # true for $undefined and the error token, so there are three warnings for | |
1225 | # %printer and three for %destructor.) | |
1226 | ||
55f48c48 | 1227 | AT_BISON_OPTION_PUSHDEFS |
9350499c JD |
1228 | AT_DATA_GRAMMAR([[input.y]], |
1229 | [[%debug /* So that %printer is actually compiled. */ | |
1230 | ||
1231 | %{ | |
1232 | # include <stdio.h> | |
cf806753 | 1233 | # include <stdlib.h> |
55f48c48 AD |
1234 | ]AT_YYERROR_DECLARE[ |
1235 | ]AT_YYLEX_DECLARE[ | |
9350499c JD |
1236 | # define USE(SYM) |
1237 | %} | |
1238 | ||
1239 | %printer { | |
1240 | char chr = $$; | |
1241 | fprintf (yyoutput, "'%c'", chr); | |
3ebecc24 | 1242 | } <> <*> |
9350499c JD |
1243 | %destructor { |
1244 | char chr = $$; | |
1245 | fprintf (stderr, "DESTROY '%c'\n", chr); | |
3ebecc24 | 1246 | } <> <*> |
9350499c JD |
1247 | |
1248 | %union { char chr; } | |
1249 | %type <chr> start | |
1250 | ||
1251 | %% | |
1252 | ||
1253 | start: { USE($$); } ; | |
1254 | ||
1255 | %% | |
55f48c48 | 1256 | ]AT_YYERROR_DEFINE[ |
95361618 | 1257 | ]AT_YYLEX_DEFINE[ |
9350499c JD |
1258 | int |
1259 | main (void) | |
1260 | { | |
1261 | return yyparse (); | |
1262 | } | |
1263 | ]]) | |
55f48c48 | 1264 | AT_BISON_OPTION_POPDEFS |
9350499c | 1265 | |
9534d2be AD |
1266 | AT_BISON_CHECK([-o input.c input.y], [], [], |
1267 | [[input.y:22.3-4: warning: useless %destructor for type <> [-Wother] | |
1268 | input.y:22.3-4: warning: useless %printer for type <> [-Wother] | |
1269 | ]]) | |
9350499c JD |
1270 | AT_COMPILE([input]) |
1271 | ||
1272 | AT_CLEANUP | |
f91b1629 JD |
1273 | |
1274 | ||
1275 | ||
1276 | ## ------------------------------------------------------ ## | |
1277 | ## Default %printer and %destructor for mid-rule values. ## | |
1278 | ## ------------------------------------------------------ ## | |
1279 | ||
1280 | AT_SETUP([Default %printer and %destructor for mid-rule values]) | |
1281 | ||
55f48c48 | 1282 | AT_BISON_OPTION_PUSHDEFS |
f91b1629 JD |
1283 | AT_DATA_GRAMMAR([[input.y]], |
1284 | [[%debug /* So that %printer is actually compiled. */ | |
1285 | ||
1286 | %{ | |
1287 | # include <stdio.h> | |
1288 | # include <stdlib.h> | |
55f48c48 AD |
1289 | ]AT_YYERROR_DECLARE[ |
1290 | ]AT_YYLEX_DECLARE[ | |
f91b1629 JD |
1291 | # define USE(SYM) |
1292 | # define YYLTYPE int | |
9fc4c025 | 1293 | # define YYLLOC_DEFAULT(Current, Rhs, N) (void)(Rhs) |
f91b1629 JD |
1294 | # define YY_LOCATION_PRINT(File, Loc) |
1295 | %} | |
1296 | ||
3ebecc24 JD |
1297 | %printer { fprintf (yyoutput, "%d", @$); } <> |
1298 | %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <> | |
12e35840 JD |
1299 | %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*> |
1300 | %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*> | |
f91b1629 JD |
1301 | |
1302 | %% | |
1303 | ||
1304 | start: | |
1305 | { @$ = 1; } // Not set or used. | |
1306 | { USE ($$); @$ = 2; } // Both set and used. | |
1307 | { USE ($$); @$ = 3; } // Only set. | |
1308 | { @$ = 4; } // Only used. | |
1309 | 'c' | |
1310 | { USE (($$, $2, $4, $5)); @$ = 0; } | |
1311 | ; | |
1312 | ||
1313 | %% | |
55f48c48 | 1314 | ]AT_YYERROR_DEFINE[ |
95361618 | 1315 | ]AT_YYLEX_DEFINE[ |
f91b1629 JD |
1316 | int |
1317 | main (void) | |
1318 | { | |
1319 | yydebug = 1; | |
1320 | return yyparse (); | |
1321 | } | |
1322 | ]]) | |
55f48c48 | 1323 | AT_BISON_OPTION_POPDEFS |
f91b1629 | 1324 | |
da730230 | 1325 | AT_BISON_CHECK([-o input.c input.y], 0,, |
9534d2be AD |
1326 | [[input.y:24.70-72: warning: useless %destructor for type <*> [-Wother] |
1327 | input.y:24.70-72: warning: useless %printer for type <*> [-Wother] | |
1328 | input.y:33.3-23: warning: unset value: $$ [-Wother] | |
0906b12c | 1329 | input.y:32.3-23: warning: unused value: $3 [-Wother] |
f91b1629 JD |
1330 | ]]) |
1331 | ||
505ece51 | 1332 | AT_BISON_CHECK([-fcaret -o input.c input.y], 0,, |
f3ead217 TR |
1333 | [[input.y:24.70-72: warning: useless %destructor for type <*> [-Wother] |
1334 | %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*> | |
1335 | ^^^ | |
1336 | input.y:24.70-72: warning: useless %printer for type <*> [-Wother] | |
1337 | %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*> | |
1338 | ^^^ | |
1339 | input.y:33.3-23: warning: unset value: $$ [-Wother] | |
505ece51 TR |
1340 | { @$ = 4; } // Only used. |
1341 | ^^^^^^^^^^^^^^^^^^^^^ | |
0906b12c | 1342 | input.y:32.3-23: warning: unused value: $3 [-Wother] |
23589235 AD |
1343 | { USE ($$); @$ = 3; } // Only set. |
1344 | ^^^^^^^^^^^^^^^^^^^^^ | |
505ece51 TR |
1345 | ]]) |
1346 | ||
f91b1629 JD |
1347 | AT_COMPILE([input]) |
1348 | AT_PARSER_CHECK([./input], 1,, | |
1349 | [[Starting parse | |
1350 | Entering state 0 | |
12e35840 | 1351 | Reducing stack by rule 1 (line 30): |
f91b1629 JD |
1352 | -> $$ = nterm $@1 (: ) |
1353 | Stack now 0 | |
1354 | Entering state 2 | |
12e35840 | 1355 | Reducing stack by rule 2 (line 31): |
f91b1629 JD |
1356 | -> $$ = nterm @2 (: 2) |
1357 | Stack now 0 2 | |
1358 | Entering state 4 | |
12e35840 | 1359 | Reducing stack by rule 3 (line 32): |
f91b1629 JD |
1360 | -> $$ = nterm @3 (: 3) |
1361 | Stack now 0 2 4 | |
1362 | Entering state 5 | |
12e35840 | 1363 | Reducing stack by rule 4 (line 33): |
f91b1629 JD |
1364 | -> $$ = nterm @4 (: 4) |
1365 | Stack now 0 2 4 5 | |
1366 | Entering state 6 | |
1367 | Reading a token: Now at end of input. | |
1368 | syntax error | |
1369 | Error: popping nterm @4 (: 4) | |
1370 | DESTROY 4 | |
1371 | Stack now 0 2 4 5 | |
1372 | Error: popping nterm @3 (: 3) | |
1373 | DESTROY 3 | |
1374 | Stack now 0 2 4 | |
1375 | Error: popping nterm @2 (: 2) | |
1376 | DESTROY 2 | |
1377 | Stack now 0 2 | |
1378 | Error: popping nterm $@1 (: ) | |
1379 | Stack now 0 | |
1380 | Cleanup: discarding lookahead token $end (: ) | |
1381 | Stack now 0 | |
1382 | ]]) | |
1383 | ||
1384 | AT_CLEANUP | |
e785ccf7 JD |
1385 | |
1386 | ||
1387 | ## ----------------------- ## | |
1388 | ## @$ implies %locations. ## | |
1389 | ## ----------------------- ## | |
1390 | ||
1391 | # Bison once forgot to check for @$ in actions other than semantic actions. | |
1392 | ||
1393 | # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE) | |
55f48c48 | 1394 | # ------------------------------------------- |
e785ccf7 JD |
1395 | m4_define([AT_CHECK_ACTION_LOCATIONS], |
1396 | [AT_SETUP([[@$ in ]$1[ implies %locations]]) | |
55f48c48 | 1397 | AT_BISON_OPTION_PUSHDEFS |
e785ccf7 JD |
1398 | AT_DATA_GRAMMAR([[input.y]], |
1399 | [[%code { | |
1400 | #include <stdio.h> | |
55f48c48 AD |
1401 | ]AT_YYERROR_DECLARE[ |
1402 | ]AT_YYLEX_DECLARE[ | |
e785ccf7 JD |
1403 | } |
1404 | ||
1405 | %debug | |
1406 | ||
1407 | ]$1[ { | |
8d6c1b5e | 1408 | fprintf (stderr, "%d\n", @$.first_line); |
e785ccf7 JD |
1409 | } ]m4_if($1, [%initial-action], [], [[start]])[ |
1410 | ||
1411 | %% | |
1412 | ||
1413 | start: ; | |
1414 | ||
1415 | %% | |
1416 | ||
1417 | static int | |
1418 | yylex (void) | |
1419 | { | |
1420 | return 0; | |
1421 | } | |
1422 | ||
55f48c48 | 1423 | ]AT_YYERROR_DEFINE[ |
e785ccf7 JD |
1424 | int |
1425 | main (void) | |
1426 | { | |
1427 | return yyparse (); | |
1428 | } | |
1429 | ]]) | |
1430 | ||
da730230 | 1431 | AT_BISON_CHECK([[-o input.c input.y]]) |
e785ccf7 | 1432 | AT_COMPILE([[input]]) |
55f48c48 | 1433 | AT_BISON_OPTION_POPDEFS |
e785ccf7 JD |
1434 | AT_CLEANUP]) |
1435 | ||
1436 | AT_CHECK_ACTION_LOCATIONS([[%initial-action]]) | |
1437 | AT_CHECK_ACTION_LOCATIONS([[%destructor]]) | |
1438 | AT_CHECK_ACTION_LOCATIONS([[%printer]]) | |
e8cd1ad6 DJ |
1439 | |
1440 | ||
4982f078 AD |
1441 | ## ------------------------- ## |
1442 | ## Qualified $$ in actions. ## | |
1443 | ## ------------------------- ## | |
1444 | ||
3edfae04 AD |
1445 | # Check that we can use qualified $$ (v.g., $<type>$) not only in rule |
1446 | # actions, but also where $$ is valid: %destructor/%printer and | |
1447 | # %initial-action. | |
4982f078 | 1448 | # |
3edfae04 | 1449 | # FIXME: Not actually checking %destructor, but it's the same code as |
4982f078 AD |
1450 | # %printer... |
1451 | # | |
1452 | # To do that, use a semantic value that has two fields (sem_type), | |
1453 | # declare symbols to have only one of these types (INT, float), and | |
1454 | # use $<type>$ to get the other one. Including for symbols that are | |
1455 | # not typed (UNTYPED). | |
1456 | ||
1457 | m4_pushdef([AT_TEST], | |
1458 | [AT_SETUP([[Qualified $$ in actions: $1]]) | |
1459 | ||
34904c57 | 1460 | AT_BISON_OPTION_PUSHDEFS([%skeleton "$1"]) |
4982f078 AD |
1461 | |
1462 | AT_DATA_GRAMMAR([[input.y]], | |
1463 | [[%skeleton "$1" | |
4982f078 AD |
1464 | %debug |
1465 | %code requires | |
1466 | { | |
4982f078 AD |
1467 | typedef struct sem_type |
1468 | { | |
1469 | int ival; | |
1470 | float fval; | |
1471 | } sem_type; | |
1472 | ||
1473 | # define YYSTYPE sem_type | |
1474 | ||
4acc22e5 | 1475 | ]AT_SKEL_CC_IF([[ |
4982f078 AD |
1476 | # include <iostream> |
1477 | static void | |
1478 | report (std::ostream& yyo, int ival, float fval) | |
1479 | { | |
1480 | yyo << "ival: " << ival << ", fval: " << fval; | |
1481 | } | |
4acc22e5 AD |
1482 | ]], [[ |
1483 | # include <stdio.h> | |
4982f078 AD |
1484 | static void |
1485 | report (FILE* yyo, int ival, float fval) | |
1486 | { | |
1487 | fprintf (yyo, "ival: %d, fval: %1.1f", ival, fval); | |
1488 | } | |
4acc22e5 | 1489 | ]])[ |
4982f078 AD |
1490 | } |
1491 | ||
1492 | %code | |
1493 | { | |
1494 | ]AT_YYERROR_DECLARE[ | |
1495 | ]AT_YYLEX_DECLARE[ | |
1496 | } | |
1497 | ||
1498 | %token UNTYPED | |
1499 | %token <ival> INT | |
1500 | %type <fval> float | |
1501 | %printer { report (yyo, $$, $<fval>$); } <ival>; | |
1502 | %printer { report (yyo, $<ival>$, $$ ); } <fval>; | |
1503 | %printer { report (yyo, $<ival>$, $<fval>$); } <>; | |
1504 | ||
cd735a8c AD |
1505 | %initial-action |
1506 | { | |
1507 | $<ival>$ = 42; | |
1508 | $<fval>$ = 4.2; | |
1509 | } | |
4982f078 AD |
1510 | |
1511 | %% | |
1512 | float: UNTYPED INT | |
1513 | { | |
1514 | $$ = $<fval>1 + $<fval>2; | |
1515 | $<ival>$ = $<ival>1 + $][2; | |
1516 | }; | |
1517 | %% | |
1518 | ]AT_YYERROR_DEFINE[ | |
1519 | ]AT_YYLEX_DEFINE(AT_SKEL_CC_IF([[{yy::parser::token::UNTYPED, | |
1520 | yy::parser::token::INT, | |
1521 | EOF}]], | |
1522 | [[{UNTYPED, INT, EOF}]]), | |
1523 | [AT_VAL.ival = toknum * 10; AT_VAL.fval = toknum / 10.0;])[ | |
1524 | int | |
1525 | main (void) | |
1526 | {]AT_SKEL_CC_IF([[ | |
1527 | yy::parser p; | |
1528 | p.set_debug_level(1); | |
1529 | return p.parse ();]], [[ | |
1530 | yydebug = 1; | |
1531 | return yyparse ();]])[ | |
1532 | } | |
1533 | ]]) | |
1534 | ||
1535 | AT_FULL_COMPILE([[input]]) | |
1536 | AT_PARSER_CHECK([./input], 0, [], [stderr]) | |
1537 | # Don't be too picky on the traces, GLR is not exactly the same. Keep | |
1538 | # only the lines from the printer. | |
34904c57 | 1539 | AT_CHECK([[sed -ne '/ival:/p' stderr]], 0, |
4982f078 AD |
1540 | [[Reading a token: Next token is token UNTYPED (ival: 10, fval: 0.1) |
1541 | Shifting token UNTYPED (ival: 10, fval: 0.1) | |
1542 | Reading a token: Next token is token INT (ival: 20, fval: 0.2) | |
1543 | Shifting token INT (ival: 20, fval: 0.2) | |
1544 | $][1 = token UNTYPED (ival: 10, fval: 0.1) | |
1545 | $][2 = token INT (ival: 20, fval: 0.2) | |
1546 | -> $$ = nterm float (ival: 30, fval: 0.3) | |
1547 | Cleanup: popping nterm float (ival: 30, fval: 0.3) | |
1548 | ]]) | |
1549 | ||
1550 | AT_BISON_OPTION_POPDEFS | |
1551 | ||
1552 | AT_CLEANUP | |
1553 | ]) | |
1554 | ||
1555 | AT_TEST([yacc.c]) | |
1556 | AT_TEST([glr.c]) | |
1557 | AT_TEST([lalr1.cc]) | |
1558 | AT_TEST([glr.cc]) | |
1559 | ||
1560 | m4_popdef([AT_TEST]) | |
1561 | ||
e8cd1ad6 DJ |
1562 | ## ----------------------------------------------- ## |
1563 | ## Fix user actions without a trailing semicolon. ## | |
1564 | ## ----------------------------------------------- ## | |
1565 | ||
1566 | AT_SETUP([[Fix user actions without a trailing semicolon]]) | |
1567 | ||
1568 | # This feature is undocumented, but we accidentally broke it in 2.3a, | |
1569 | # and there was a complaint at: | |
1570 | # <http://lists.gnu.org/archive/html/bug-bison/2008-11/msg00001.html>. | |
55f48c48 | 1571 | AT_BISON_OPTION_PUSHDEFS |
e8cd1ad6 DJ |
1572 | AT_DATA([input.y], |
1573 | [[%% | |
1574 | start: test2 test1 test0 testc; | |
1575 | ||
1576 | test2 | |
e9690142 JD |
1577 | : 'a' { semi; /* TEST:N:2 */ } |
1578 | | 'b' { if (0) {no_semi} /* TEST:N:2 */ } | |
1579 | | 'c' { if (0) {semi;} /* TEST:N:2 */ } | |
1580 | | 'd' { semi; no_semi /* TEST:Y:2 */ } | |
1581 | | 'e' { semi(); no_semi() /* TEST:Y:2 */ } | |
1582 | | 'f' { semi[]; no_semi[] /* TEST:Y:2 */ } | |
1583 | | 'g' { semi++; no_semi++ /* TEST:Y:2 */ } | |
1584 | | 'h' { {no_semi} no_semi /* TEST:Y:2 */ } | |
1585 | | 'i' { {semi;} no_semi /* TEST:Y:2 */ } | |
e8cd1ad6 DJ |
1586 | ; |
1587 | test1 | |
e9690142 JD |
1588 | : 'a' { semi; // TEST:N:1 ; |
1589 | } | 'b' { if (0) {no_semi} // TEST:N:1 ; | |
1590 | } | 'c' { if (0) {semi;} // TEST:N:1 ; | |
1591 | } | 'd' { semi; no_semi // TEST:Y:1 ; | |
1592 | } | 'e' { semi(); no_semi() // TEST:Y:1 ; | |
1593 | } | 'f' { semi[]; no_semi[] // TEST:Y:1 ; | |
1594 | } | 'g' { semi++; no_semi++ // TEST:Y:1 ; | |
1595 | } | 'h' { {no_semi} no_semi // TEST:Y:1 ; | |
1596 | } | 'i' { {semi;} no_semi // TEST:Y:1 ; | |
e8cd1ad6 DJ |
1597 | } ; |
1598 | test0 | |
e9690142 JD |
1599 | : 'a' { semi; // TEST:N:1 {} |
1600 | } | 'b' { if (0) {no_semi} // TEST:N:1 {} | |
1601 | } | 'c' { if (0) {semi;} // TEST:N:1 {} | |
1602 | } | 'd' { semi; no_semi // TEST:Y:1 {} | |
1603 | } | 'e' { semi(); no_semi() // TEST:Y:1 {} | |
1604 | } | 'f' { semi[]; no_semi[] // TEST:Y:1 {} | |
1605 | } | 'g' { semi++; no_semi++ // TEST:Y:1 {} | |
1606 | } | 'h' { {no_semi} no_semi // TEST:Y:1 {} | |
1607 | } | 'i' { {semi;} no_semi // TEST:Y:1 {} | |
e8cd1ad6 DJ |
1608 | } ; |
1609 | ||
1610 | testc | |
1611 | : 'a' { | |
1612 | #define TEST_MACRO_N \ | |
1613 | []"broken\" $ @ $$ @$ [];\ | |
1614 | string;"} | |
1615 | | 'b' { | |
1616 | no_semi | |
1617 | #define TEST_MACRO_N \ | |
1618 | []"broken\" $ @ $$ @$ [];\ | |
1619 | string;"} | |
1620 | ]]) | |
55f48c48 | 1621 | AT_BISON_OPTION_POPDEFS |
e8cd1ad6 DJ |
1622 | |
1623 | AT_BISON_CHECK([[-o input.c input.y]], [0], [], | |
518e8830 | 1624 | [[input.y:8.48: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1625 | input.y:8.48: future versions of Bison will not add the ';' |
518e8830 | 1626 | input.y:9.48: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1627 | input.y:9.48: future versions of Bison will not add the ';' |
518e8830 | 1628 | input.y:10.48: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1629 | input.y:10.48: future versions of Bison will not add the ';' |
518e8830 | 1630 | input.y:11.48: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1631 | input.y:11.48: future versions of Bison will not add the ';' |
518e8830 | 1632 | input.y:12.48: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1633 | input.y:12.48: future versions of Bison will not add the ';' |
518e8830 | 1634 | input.y:13.48: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1635 | input.y:13.48: future versions of Bison will not add the ';' |
518e8830 | 1636 | input.y:20.1: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1637 | input.y:20.1: future versions of Bison will not add the ';' |
518e8830 | 1638 | input.y:21.1: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1639 | input.y:21.1: future versions of Bison will not add the ';' |
518e8830 | 1640 | input.y:22.1: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1641 | input.y:22.1: future versions of Bison will not add the ';' |
518e8830 | 1642 | input.y:23.1: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1643 | input.y:23.1: future versions of Bison will not add the ';' |
518e8830 | 1644 | input.y:24.1: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1645 | input.y:24.1: future versions of Bison will not add the ';' |
518e8830 | 1646 | input.y:25.1: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1647 | input.y:25.1: future versions of Bison will not add the ';' |
518e8830 | 1648 | input.y:31.1: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1649 | input.y:31.1: future versions of Bison will not add the ';' |
518e8830 | 1650 | input.y:32.1: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1651 | input.y:32.1: future versions of Bison will not add the ';' |
518e8830 | 1652 | input.y:33.1: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1653 | input.y:33.1: future versions of Bison will not add the ';' |
518e8830 | 1654 | input.y:34.1: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1655 | input.y:34.1: future versions of Bison will not add the ';' |
518e8830 | 1656 | input.y:35.1: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1657 | input.y:35.1: future versions of Bison will not add the ';' |
518e8830 | 1658 | input.y:36.1: warning: a ';' might be needed at the end of action code [-Wdeprecated] |
d4e985d5 | 1659 | input.y:36.1: future versions of Bison will not add the ';' |
e8cd1ad6 | 1660 | ]]) |
6617622c AD |
1661 | |
1662 | AT_MATCHES_CHECK([input.c], [[/\* TEST:N:2 \*/ \}$]], [[3]]) | |
1663 | AT_MATCHES_CHECK([input.c], [[/\* TEST:Y:2 \*/ ;\}$]], [[6]]) | |
1664 | AT_MATCHES_CHECK([input.c], [[// TEST:N:1 [;{}]*\n\}$]], [[6]]) | |
1665 | AT_MATCHES_CHECK([input.c], [[// TEST:Y:1 [;{}]*\n;\}$]], [[12]]) | |
1666 | AT_MATCHES_CHECK([input.c], [[#define TEST_MACRO_N \\\n\[\]"broken\\" \$ \@ \$\$ \@\$ \[\];\\\nstring;"\}]], [[2]]) | |
e8cd1ad6 DJ |
1667 | |
1668 | AT_CLEANUP | |
df222dfa JD |
1669 | |
1670 | ||
1671 | ## -------------------------------------------------- ## | |
1672 | ## Destroying lookahead assigned by semantic action. ## | |
1673 | ## -------------------------------------------------- ## | |
1674 | ||
1675 | AT_SETUP([[Destroying lookahead assigned by semantic action]]) | |
1676 | ||
55f48c48 | 1677 | AT_BISON_OPTION_PUSHDEFS |
df222dfa JD |
1678 | AT_DATA_GRAMMAR([input.y], |
1679 | [[ | |
1680 | %code { | |
1681 | #include <assert.h> | |
1682 | #include <stdio.h> | |
55f48c48 AD |
1683 | ]AT_YYERROR_DECLARE[ |
1684 | ]AT_YYLEX_DECLARE[ | |
df222dfa JD |
1685 | #define USE(Var) |
1686 | } | |
1687 | ||
1688 | %destructor { fprintf (stderr, "'a' destructor\n"); } 'a' | |
1689 | %destructor { fprintf (stderr, "'b' destructor\n"); } 'b' | |
1690 | ||
1691 | %% | |
1692 | ||
1693 | // In a previous version of Bison, yychar assigned by the semantic | |
1694 | // action below was not translated into yytoken before the lookahead was | |
1695 | // discarded and thus before its destructor (selected according to | |
1696 | // yytoken) was called in order to return from yyparse. This would | |
1697 | // happen even if YYACCEPT was performed in a later semantic action as | |
1698 | // long as only consistent states with default reductions were visited | |
1699 | // in between. However, we leave YYACCEPT in the same semantic action | |
1700 | // for this test in order to show that skeletons cannot simply translate | |
1701 | // immediately after every semantic action because a semantic action | |
1702 | // that has set yychar might not always return normally. Instead, | |
1703 | // skeletons must translate before every use of yytoken. | |
1704 | start: 'a' accept { USE($1); } ; | |
1705 | accept: /*empty*/ { | |
1706 | assert (yychar == YYEMPTY); | |
1707 | yychar = 'b'; | |
1708 | YYACCEPT; | |
1709 | } ; | |
1710 | ||
1711 | %% | |
55f48c48 | 1712 | ]AT_YYERROR_DEFINE[ |
95361618 | 1713 | ]AT_YYLEX_DEFINE(["a"])[ |
df222dfa JD |
1714 | int |
1715 | main (void) | |
1716 | { | |
1717 | return yyparse (); | |
1718 | } | |
1719 | ]]) | |
55f48c48 | 1720 | AT_BISON_OPTION_POPDEFS |
df222dfa JD |
1721 | AT_BISON_CHECK([[-o input.c input.y]]) |
1722 | AT_COMPILE([[input]]) | |
1723 | AT_PARSER_CHECK([[./input]], [[0]], [], | |
1724 | [['b' destructor | |
1725 | 'a' destructor | |
1726 | ]]) | |
1727 | ||
1728 | AT_CLEANUP | |
d115aad9 AD |
1729 | |
1730 | ## ---------- ## | |
1731 | ## YYBACKUP. ## | |
1732 | ## ---------- ## | |
1733 | ||
1734 | AT_SETUP([[YYBACKUP]]) | |
1735 | ||
55f48c48 AD |
1736 | AT_BISON_OPTION_PUSHDEFS([%pure-parser]) |
1737 | ||
d115aad9 AD |
1738 | AT_DATA_GRAMMAR([input.y], |
1739 | [[ | |
1740 | %error-verbose | |
1741 | %debug | |
1742 | %pure-parser | |
1743 | %code { | |
1744 | # include <stdio.h> | |
1745 | # include <stdlib.h> | |
1746 | # include <assert.h> | |
1747 | ||
55f48c48 | 1748 | ]AT_YYERROR_DECLARE[ |
087dcd78 | 1749 | ]AT_YYLEX_DECLARE[ |
d115aad9 AD |
1750 | } |
1751 | %% | |
1752 | input: | |
1753 | exp exp {} | |
1754 | ; | |
1755 | ||
1756 | exp: | |
1757 | 'a' { printf ("a: %d\n", $1); } | |
1758 | | 'b' { YYBACKUP('a', 123); } | |
1759 | | 'c' 'd' { YYBACKUP('a', 456); } | |
1760 | ; | |
1761 | ||
1762 | %% | |
55f48c48 | 1763 | ]AT_YYERROR_DEFINE[ |
087dcd78 | 1764 | ]AT_YYLEX_DEFINE(["bcd"], [*lvalp = (toknum + 1) * 10])[ |
d115aad9 | 1765 | |
d115aad9 AD |
1766 | int |
1767 | main (void) | |
1768 | { | |
1769 | yydebug = !!getenv("YYDEBUG"); | |
1770 | return yyparse (); | |
1771 | } | |
1772 | ]]) | |
55f48c48 | 1773 | AT_BISON_OPTION_POPDEFS |
d115aad9 AD |
1774 | |
1775 | AT_BISON_CHECK([[-o input.c input.y]]) | |
1776 | AT_COMPILE([[input]]) | |
1777 | AT_PARSER_CHECK([[./input]], [[0]], | |
1778 | [[a: 123 | |
1779 | a: 456 | |
d115aad9 AD |
1780 | ]]) |
1781 | ||
1782 | AT_CLEANUP |