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