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