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