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