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