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