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