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