]>
Commit | Line | Data |
---|---|---|
1 | # Bison Regressions. -*- Autotest -*- | |
2 | ||
3 | # Copyright (C) 2001-2011 Free Software Foundation, Inc. | |
4 | ||
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU General Public License as published by | |
7 | # the Free Software Foundation, either version 3 of the License, or | |
8 | # (at your option) any later version. | |
9 | # | |
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. | |
14 | # | |
15 | # You should have received a copy of the GNU General Public License | |
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | AT_BANNER([[Regression tests.]]) | |
19 | ||
20 | ||
21 | ## ------------------ ## | |
22 | ## Trivial grammars. ## | |
23 | ## ------------------ ## | |
24 | ||
25 | AT_SETUP([Trivial grammars]) | |
26 | ||
27 | AT_DATA_GRAMMAR([input.y], | |
28 | [[%{ | |
29 | void yyerror (char const *); | |
30 | int yylex (void); | |
31 | #define YYSTYPE int * | |
32 | %} | |
33 | ||
34 | %error-verbose | |
35 | ||
36 | %% | |
37 | ||
38 | program: 'x'; | |
39 | ]]) | |
40 | ||
41 | AT_BISON_CHECK([-o input.c input.y]) | |
42 | AT_COMPILE([input.o], [-c input.c]) | |
43 | AT_COMPILE([input.o], [-DYYDEBUG -c input.c]) | |
44 | ||
45 | AT_CLEANUP | |
46 | ||
47 | ||
48 | ||
49 | ## ----------------- ## | |
50 | ## YYSTYPE typedef. ## | |
51 | ## ----------------- ## | |
52 | ||
53 | AT_SETUP([YYSTYPE typedef]) | |
54 | ||
55 | AT_DATA_GRAMMAR([input.y], | |
56 | [[%{ | |
57 | void yyerror (char const *); | |
58 | int yylex (void); | |
59 | typedef union { char const *val; } YYSTYPE; | |
60 | %} | |
61 | ||
62 | %type <val> program | |
63 | ||
64 | %% | |
65 | ||
66 | program: { $$ = ""; }; | |
67 | ]]) | |
68 | ||
69 | AT_BISON_CHECK([-o input.c input.y]) | |
70 | AT_COMPILE([input.o], [-c input.c]) | |
71 | ||
72 | AT_CLEANUP | |
73 | ||
74 | ||
75 | ||
76 | ## ------------------------------------- ## | |
77 | ## Early token definitions with --yacc. ## | |
78 | ## ------------------------------------- ## | |
79 | ||
80 | ||
81 | AT_SETUP([Early token definitions with --yacc]) | |
82 | ||
83 | # Found in GCJ: they expect the tokens to be defined before the user | |
84 | # prologue, so that they can use the token definitions in it. | |
85 | ||
86 | AT_DATA_GRAMMAR([input.y], | |
87 | [[%{ | |
88 | void yyerror (const char *s); | |
89 | int yylex (void); | |
90 | %} | |
91 | ||
92 | %union | |
93 | { | |
94 | int val; | |
95 | }; | |
96 | %{ | |
97 | #ifndef MY_TOKEN | |
98 | # error "MY_TOKEN not defined." | |
99 | #endif | |
100 | %} | |
101 | %token MY_TOKEN | |
102 | %% | |
103 | exp: MY_TOKEN; | |
104 | %% | |
105 | ]]) | |
106 | ||
107 | AT_BISON_CHECK([-y -o input.c input.y]) | |
108 | AT_COMPILE([input.o], [-c input.c]) | |
109 | ||
110 | AT_CLEANUP | |
111 | ||
112 | ||
113 | ||
114 | ## ---------------------------------------- ## | |
115 | ## Early token definitions without --yacc. ## | |
116 | ## ---------------------------------------- ## | |
117 | ||
118 | ||
119 | AT_SETUP([Early token definitions without --yacc]) | |
120 | ||
121 | # Found in GCJ: they expect the tokens to be defined before the user | |
122 | # prologue, so that they can use the token definitions in it. | |
123 | ||
124 | AT_DATA_GRAMMAR([input.y], | |
125 | [[%{ | |
126 | #include <stdio.h> | |
127 | void yyerror (const char *s); | |
128 | int yylex (void); | |
129 | void print_my_token (void); | |
130 | %} | |
131 | ||
132 | %union | |
133 | { | |
134 | int val; | |
135 | }; | |
136 | %{ | |
137 | void | |
138 | print_my_token (void) | |
139 | { | |
140 | enum yytokentype my_token = MY_TOKEN; | |
141 | printf ("%d\n", my_token); | |
142 | } | |
143 | %} | |
144 | %token MY_TOKEN | |
145 | %% | |
146 | exp: MY_TOKEN; | |
147 | %% | |
148 | ]]) | |
149 | ||
150 | AT_BISON_CHECK([-o input.c input.y]) | |
151 | AT_COMPILE([input.o], [-c input.c]) | |
152 | ||
153 | AT_CLEANUP | |
154 | ||
155 | ||
156 | ||
157 | ## ---------------- ## | |
158 | ## Braces parsing. ## | |
159 | ## ---------------- ## | |
160 | ||
161 | ||
162 | AT_SETUP([Braces parsing]) | |
163 | ||
164 | AT_DATA([input.y], | |
165 | [[/* Bison used to swallow the character after `}'. */ | |
166 | ||
167 | %% | |
168 | exp: { tests = {{{{{{{{{{}}}}}}}}}}; }; | |
169 | %% | |
170 | ]]) | |
171 | ||
172 | AT_BISON_CHECK([-v -o input.c input.y]) | |
173 | ||
174 | AT_CHECK([grep 'tests = {{{{{{{{{{}}}}}}}}}};' input.c], 0, [ignore]) | |
175 | ||
176 | AT_CLEANUP | |
177 | ||
178 | ||
179 | ## ------------------ ## | |
180 | ## Duplicate string. ## | |
181 | ## ------------------ ## | |
182 | ||
183 | ||
184 | AT_SETUP([Duplicate string]) | |
185 | ||
186 | AT_DATA([input.y], | |
187 | [[/* `Bison -v' used to dump core when two tokens are defined with the same | |
188 | string, as LE and GE below. */ | |
189 | ||
190 | %token NUM | |
191 | %token LE "<=" | |
192 | %token GE "<=" | |
193 | ||
194 | %% | |
195 | exp: '(' exp ')' | NUM ; | |
196 | %% | |
197 | ]]) | |
198 | ||
199 | AT_BISON_CHECK([-v -o input.c input.y], 0, [], | |
200 | [[input.y:6.8-14: warning: symbol `"<="' used more than once as a literal string | |
201 | ]]) | |
202 | ||
203 | AT_CLEANUP | |
204 | ||
205 | ||
206 | ## ------------------- ## | |
207 | ## Rule Line Numbers. ## | |
208 | ## ------------------- ## | |
209 | ||
210 | AT_SETUP([Rule Line Numbers]) | |
211 | ||
212 | AT_KEYWORDS([report]) | |
213 | ||
214 | AT_DATA([input.y], | |
215 | [[%% | |
216 | expr: | |
217 | 'a' | |
218 | ||
219 | { | |
220 | ||
221 | } | |
222 | ||
223 | 'b' | |
224 | ||
225 | { | |
226 | ||
227 | } | |
228 | ||
229 | | | |
230 | ||
231 | ||
232 | { | |
233 | ||
234 | ||
235 | } | |
236 | ||
237 | 'c' | |
238 | ||
239 | { | |
240 | ||
241 | }; | |
242 | ]]) | |
243 | ||
244 | AT_BISON_CHECK([-o input.c -v input.y]) | |
245 | ||
246 | # Check the contents of the report. | |
247 | AT_CHECK([cat input.output], [], | |
248 | [[Grammar | |
249 | ||
250 | 0 $accept: expr $end | |
251 | ||
252 | 1 $@1: /* empty */ | |
253 | ||
254 | 2 expr: 'a' $@1 'b' | |
255 | ||
256 | 3 $@2: /* empty */ | |
257 | ||
258 | 4 expr: $@2 'c' | |
259 | ||
260 | ||
261 | Terminals, with rules where they appear | |
262 | ||
263 | $end (0) 0 | |
264 | 'a' (97) 2 | |
265 | 'b' (98) 2 | |
266 | 'c' (99) 4 | |
267 | error (256) | |
268 | ||
269 | ||
270 | Nonterminals, with rules where they appear | |
271 | ||
272 | $accept (6) | |
273 | on left: 0 | |
274 | expr (7) | |
275 | on left: 2 4, on right: 0 | |
276 | $@1 (8) | |
277 | on left: 1, on right: 2 | |
278 | $@2 (9) | |
279 | on left: 3, on right: 4 | |
280 | ||
281 | ||
282 | state 0 | |
283 | ||
284 | 0 $accept: . expr $end | |
285 | ||
286 | 'a' shift, and go to state 1 | |
287 | ||
288 | $default reduce using rule 3 ($@2) | |
289 | ||
290 | expr go to state 2 | |
291 | $@2 go to state 3 | |
292 | ||
293 | ||
294 | state 1 | |
295 | ||
296 | 2 expr: 'a' . $@1 'b' | |
297 | ||
298 | $default reduce using rule 1 ($@1) | |
299 | ||
300 | $@1 go to state 4 | |
301 | ||
302 | ||
303 | state 2 | |
304 | ||
305 | 0 $accept: expr . $end | |
306 | ||
307 | $end shift, and go to state 5 | |
308 | ||
309 | ||
310 | state 3 | |
311 | ||
312 | 4 expr: $@2 . 'c' | |
313 | ||
314 | 'c' shift, and go to state 6 | |
315 | ||
316 | ||
317 | state 4 | |
318 | ||
319 | 2 expr: 'a' $@1 . 'b' | |
320 | ||
321 | 'b' shift, and go to state 7 | |
322 | ||
323 | ||
324 | state 5 | |
325 | ||
326 | 0 $accept: expr $end . | |
327 | ||
328 | $default accept | |
329 | ||
330 | ||
331 | state 6 | |
332 | ||
333 | 4 expr: $@2 'c' . | |
334 | ||
335 | $default reduce using rule 4 (expr) | |
336 | ||
337 | ||
338 | state 7 | |
339 | ||
340 | 2 expr: 'a' $@1 'b' . | |
341 | ||
342 | $default reduce using rule 2 (expr) | |
343 | ]]) | |
344 | ||
345 | AT_CLEANUP | |
346 | ||
347 | ||
348 | ||
349 | ## ---------------------- ## | |
350 | ## Mixing %token styles. ## | |
351 | ## ---------------------- ## | |
352 | ||
353 | ||
354 | AT_SETUP([Mixing %token styles]) | |
355 | ||
356 | # Taken from the documentation. | |
357 | AT_DATA([input.y], | |
358 | [[%token <operator> OR "||" | |
359 | %token <operator> LE 134 "<=" | |
360 | %left OR "<=" | |
361 | %% | |
362 | exp: ; | |
363 | %% | |
364 | ]]) | |
365 | ||
366 | AT_BISON_CHECK([-v -o input.c input.y]) | |
367 | ||
368 | AT_CLEANUP | |
369 | ||
370 | ||
371 | ||
372 | ## ---------------- ## | |
373 | ## Invalid inputs. ## | |
374 | ## ---------------- ## | |
375 | ||
376 | ||
377 | AT_SETUP([Invalid inputs]) | |
378 | ||
379 | AT_DATA([input.y], | |
380 | [[%% | |
381 | ? | |
382 | default: 'a' } | |
383 | %& | |
384 | %a-does-not-exist | |
385 | %- | |
386 | %{ | |
387 | ]]) | |
388 | ||
389 | AT_BISON_CHECK([input.y], [1], [], | |
390 | [[input.y:2.1: invalid character: `?' | |
391 | input.y:3.14: invalid character: `}' | |
392 | input.y:4.1: invalid character: `%' | |
393 | input.y:4.2: invalid character: `&' | |
394 | input.y:5.1-17: invalid directive: `%a-does-not-exist' | |
395 | input.y:6.1: invalid character: `%' | |
396 | input.y:6.2: invalid character: `-' | |
397 | input.y:7.1-8.0: missing `%}' at end of file | |
398 | input.y:7.1-8.0: syntax error, unexpected %{...%} | |
399 | ]]) | |
400 | ||
401 | AT_CLEANUP | |
402 | ||
403 | ||
404 | AT_SETUP([Invalid inputs with {}]) | |
405 | ||
406 | AT_DATA([input.y], | |
407 | [[ | |
408 | %destructor | |
409 | %initial-action | |
410 | %lex-param | |
411 | %parse-param | |
412 | %printer | |
413 | %union | |
414 | ]]) | |
415 | ||
416 | AT_BISON_CHECK([input.y], [1], [], | |
417 | [[input.y:3.1-15: syntax error, unexpected %initial-action, expecting {...} | |
418 | ]]) | |
419 | ||
420 | AT_CLEANUP | |
421 | ||
422 | ||
423 | ||
424 | ## ------------------- ## | |
425 | ## Token definitions. ## | |
426 | ## ------------------- ## | |
427 | ||
428 | ||
429 | AT_SETUP([Token definitions]) | |
430 | ||
431 | # Bison managed, when fed with `%token 'f' "f"' to #define 'f'! | |
432 | AT_DATA_GRAMMAR([input.y], | |
433 | [%{ | |
434 | #include <stdlib.h> | |
435 | #include <stdio.h> | |
436 | void yyerror (const char *s); | |
437 | int yylex (void); | |
438 | %} | |
439 | [%error-verbose | |
440 | %token MYEOF 0 "end of file" | |
441 | %token 'a' "a" | |
442 | %token B_TOKEN "b" | |
443 | %token C_TOKEN 'c' | |
444 | %token 'd' D_TOKEN | |
445 | %token SPECIAL "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!" | |
446 | %token SPECIAL "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!" | |
447 | %% | |
448 | exp: "a" "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!"; | |
449 | %% | |
450 | void | |
451 | yyerror (char const *s) | |
452 | { | |
453 | fprintf (stderr, "%s\n", s); | |
454 | } | |
455 | ||
456 | int | |
457 | yylex (void) | |
458 | { | |
459 | static int called; | |
460 | if (called++) | |
461 | abort (); | |
462 | return SPECIAL; | |
463 | } | |
464 | ||
465 | int | |
466 | main (void) | |
467 | { | |
468 | return yyparse (); | |
469 | } | |
470 | ]]) | |
471 | ||
472 | # Checking the warning message guarantees that the trigraph "??!" isn't | |
473 | # unnecessarily escaped here even though it would need to be if encoded in a | |
474 | # C-string literal. Also notice that unnecessary escaping, such as "\?", from | |
475 | # the user specification is eliminated. | |
476 | AT_BISON_CHECK([-o input.c input.y], [[0]], [[]], | |
477 | [[input.y:22.8-14: warning: symbol SPECIAL redeclared | |
478 | input.y:22.8-63: warning: symbol `"\\'?\"\a\b\f\n\r\t\v\001\201\001\201??!"' used more than once as a literal string | |
479 | ]]) | |
480 | AT_COMPILE([input]) | |
481 | ||
482 | # Checking the error message here guarantees that yytname, which does contain | |
483 | # C-string literals, does have the trigraph escaped correctly. Thus, the | |
484 | # symbol name reported by the parser is exactly the same as that reported by | |
485 | # Bison itself. | |
486 | AT_DATA([experr], | |
487 | [[syntax error, unexpected "\\'?\"\a\b\f\n\r\t\v\001\201\001\201??!", expecting a | |
488 | ]]) | |
489 | AT_PARSER_CHECK([./input], 1, [], [experr]) | |
490 | AT_CLEANUP | |
491 | ||
492 | ||
493 | ||
494 | ## -------------------- ## | |
495 | ## Characters Escapes. ## | |
496 | ## -------------------- ## | |
497 | ||
498 | ||
499 | AT_SETUP([Characters Escapes]) | |
500 | ||
501 | AT_DATA_GRAMMAR([input.y], | |
502 | [%{ | |
503 | void yyerror (const char *s); | |
504 | int yylex (void); | |
505 | %} | |
506 | [%% | |
507 | exp: | |
508 | '\'' "\'" | |
509 | | '\"' "\"" | |
510 | | '"' "'" | |
511 | ; | |
512 | ]]) | |
513 | # Pacify font-lock-mode: " | |
514 | ||
515 | AT_BISON_CHECK([-o input.c input.y]) | |
516 | AT_COMPILE([input.o], [-c input.c]) | |
517 | AT_CLEANUP | |
518 | ||
519 | ||
520 | ||
521 | ## -------------- ## | |
522 | ## Web2c Report. ## | |
523 | ## -------------- ## | |
524 | ||
525 | # The generation of the reduction was once wrong in Bison, and made it | |
526 | # miss some reductions. In the following test case, the reduction on | |
527 | # `undef_id_tok' in state 1 was missing. This is stripped down from | |
528 | # the actual web2c.y. | |
529 | ||
530 | AT_SETUP([Web2c Report]) | |
531 | ||
532 | AT_KEYWORDS([report]) | |
533 | ||
534 | AT_DATA([input.y], | |
535 | [[%token undef_id_tok const_id_tok | |
536 | ||
537 | %start CONST_DEC_PART | |
538 | \f | |
539 | %% | |
540 | CONST_DEC_PART: | |
541 | CONST_DEC_LIST | |
542 | ; | |
543 | ||
544 | CONST_DEC_LIST: | |
545 | CONST_DEC | |
546 | | CONST_DEC_LIST CONST_DEC | |
547 | ; | |
548 | ||
549 | CONST_DEC: | |
550 | { } undef_id_tok '=' const_id_tok ';' | |
551 | ; | |
552 | %% | |
553 | ]]) | |
554 | ||
555 | AT_BISON_CHECK([-v input.y]) | |
556 | AT_CHECK([cat input.output], 0, | |
557 | [[Grammar | |
558 | ||
559 | 0 $accept: CONST_DEC_PART $end | |
560 | ||
561 | 1 CONST_DEC_PART: CONST_DEC_LIST | |
562 | ||
563 | 2 CONST_DEC_LIST: CONST_DEC | |
564 | 3 | CONST_DEC_LIST CONST_DEC | |
565 | ||
566 | 4 $@1: /* empty */ | |
567 | ||
568 | 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok ';' | |
569 | ||
570 | ||
571 | Terminals, with rules where they appear | |
572 | ||
573 | $end (0) 0 | |
574 | ';' (59) 5 | |
575 | '=' (61) 5 | |
576 | error (256) | |
577 | undef_id_tok (258) 5 | |
578 | const_id_tok (259) 5 | |
579 | ||
580 | ||
581 | Nonterminals, with rules where they appear | |
582 | ||
583 | $accept (7) | |
584 | on left: 0 | |
585 | CONST_DEC_PART (8) | |
586 | on left: 1, on right: 0 | |
587 | CONST_DEC_LIST (9) | |
588 | on left: 2 3, on right: 1 3 | |
589 | CONST_DEC (10) | |
590 | on left: 5, on right: 2 3 | |
591 | $@1 (11) | |
592 | on left: 4, on right: 5 | |
593 | ||
594 | ||
595 | state 0 | |
596 | ||
597 | 0 $accept: . CONST_DEC_PART $end | |
598 | ||
599 | $default reduce using rule 4 ($@1) | |
600 | ||
601 | CONST_DEC_PART go to state 1 | |
602 | CONST_DEC_LIST go to state 2 | |
603 | CONST_DEC go to state 3 | |
604 | $@1 go to state 4 | |
605 | ||
606 | ||
607 | state 1 | |
608 | ||
609 | 0 $accept: CONST_DEC_PART . $end | |
610 | ||
611 | $end shift, and go to state 5 | |
612 | ||
613 | ||
614 | state 2 | |
615 | ||
616 | 1 CONST_DEC_PART: CONST_DEC_LIST . | |
617 | 3 CONST_DEC_LIST: CONST_DEC_LIST . CONST_DEC | |
618 | ||
619 | undef_id_tok reduce using rule 4 ($@1) | |
620 | $default reduce using rule 1 (CONST_DEC_PART) | |
621 | ||
622 | CONST_DEC go to state 6 | |
623 | $@1 go to state 4 | |
624 | ||
625 | ||
626 | state 3 | |
627 | ||
628 | 2 CONST_DEC_LIST: CONST_DEC . | |
629 | ||
630 | $default reduce using rule 2 (CONST_DEC_LIST) | |
631 | ||
632 | ||
633 | state 4 | |
634 | ||
635 | 5 CONST_DEC: $@1 . undef_id_tok '=' const_id_tok ';' | |
636 | ||
637 | undef_id_tok shift, and go to state 7 | |
638 | ||
639 | ||
640 | state 5 | |
641 | ||
642 | 0 $accept: CONST_DEC_PART $end . | |
643 | ||
644 | $default accept | |
645 | ||
646 | ||
647 | state 6 | |
648 | ||
649 | 3 CONST_DEC_LIST: CONST_DEC_LIST CONST_DEC . | |
650 | ||
651 | $default reduce using rule 3 (CONST_DEC_LIST) | |
652 | ||
653 | ||
654 | state 7 | |
655 | ||
656 | 5 CONST_DEC: $@1 undef_id_tok . '=' const_id_tok ';' | |
657 | ||
658 | '=' shift, and go to state 8 | |
659 | ||
660 | ||
661 | state 8 | |
662 | ||
663 | 5 CONST_DEC: $@1 undef_id_tok '=' . const_id_tok ';' | |
664 | ||
665 | const_id_tok shift, and go to state 9 | |
666 | ||
667 | ||
668 | state 9 | |
669 | ||
670 | 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok . ';' | |
671 | ||
672 | ';' shift, and go to state 10 | |
673 | ||
674 | ||
675 | state 10 | |
676 | ||
677 | 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok ';' . | |
678 | ||
679 | $default reduce using rule 5 (CONST_DEC) | |
680 | ]]) | |
681 | ||
682 | AT_CLEANUP | |
683 | ||
684 | ||
685 | ## --------------- ## | |
686 | ## Web2c Actions. ## | |
687 | ## --------------- ## | |
688 | ||
689 | # The generation of the mapping `state -> action' was once wrong in | |
690 | # extremely specific situations. web2c.y exhibits this situation. | |
691 | # Below is a stripped version of the grammar. It looks like one can | |
692 | # simplify it further, but just don't: it is tuned to exhibit a bug, | |
693 | # which disapears when applying sane grammar transformations. | |
694 | # | |
695 | # It used to be wrong on yydefact only: | |
696 | # | |
697 | # static const yytype_uint8 yydefact[] = | |
698 | # { | |
699 | # - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4, | |
700 | # + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4, | |
701 | # 0, 0 | |
702 | # }; | |
703 | # | |
704 | # but let's check all the tables. | |
705 | ||
706 | ||
707 | AT_SETUP([Web2c Actions]) | |
708 | ||
709 | AT_KEYWORDS([report]) | |
710 | ||
711 | AT_DATA([input.y], | |
712 | [[%% | |
713 | statement: struct_stat; | |
714 | struct_stat: /* empty. */ | if else; | |
715 | if: "if" "const" "then" statement; | |
716 | else: "else" statement; | |
717 | %% | |
718 | ]]) | |
719 | ||
720 | AT_BISON_CHECK([-v -o input.c input.y]) | |
721 | ||
722 | # Check only the tables. | |
723 | [sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c >tables.c] | |
724 | ||
725 | AT_CHECK([[cat tables.c]], 0, | |
726 | [[static const yytype_uint8 yytranslate[] = | |
727 | { | |
728 | 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
729 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
730 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
731 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
732 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
733 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
734 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
735 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
736 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
737 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
738 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
739 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
740 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
741 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
742 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
743 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
744 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
745 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
746 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
747 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
748 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
749 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
750 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
751 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
752 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
753 | 2, 2, 2, 2, 2, 2, 1, 2, 3, 4, | |
754 | 5, 6 | |
755 | }; | |
756 | static const yytype_uint8 yyrline[] = | |
757 | { | |
758 | 0, 2, 2, 3, 3, 4, 5 | |
759 | }; | |
760 | static const char *const yytname[] = | |
761 | { | |
762 | "$end", "error", "$undefined", "\"if\"", "\"const\"", "\"then\"", | |
763 | "\"else\"", "$accept", "statement", "struct_stat", "if", "else", 0 | |
764 | }; | |
765 | static const yytype_uint16 yytoknum[] = | |
766 | { | |
767 | 0, 256, 257, 258, 259, 260, 261 | |
768 | }; | |
769 | static const yytype_int8 yypact[] = | |
770 | { | |
771 | -2, -1, 4, -8, 0, 2, -8, -2, -8, -2, | |
772 | -8, -8 | |
773 | }; | |
774 | static const yytype_uint8 yydefact[] = | |
775 | { | |
776 | 3, 0, 0, 2, 0, 0, 1, 3, 4, 3, | |
777 | 6, 5 | |
778 | }; | |
779 | static const yytype_int8 yypgoto[] = | |
780 | { | |
781 | -8, -7, -8, -8, -8 | |
782 | }; | |
783 | static const yytype_int8 yydefgoto[] = | |
784 | { | |
785 | -1, 2, 3, 4, 8 | |
786 | }; | |
787 | static const yytype_uint8 yytable[] = | |
788 | { | |
789 | 10, 1, 11, 5, 6, 0, 7, 9 | |
790 | }; | |
791 | static const yytype_int8 yycheck[] = | |
792 | { | |
793 | 7, 3, 9, 4, 0, -1, 6, 5 | |
794 | }; | |
795 | static const yytype_uint8 yystos[] = | |
796 | { | |
797 | 0, 3, 8, 9, 10, 4, 0, 6, 11, 5, | |
798 | 8, 8 | |
799 | }; | |
800 | static const yytype_uint8 yyr1[] = | |
801 | { | |
802 | 0, 7, 8, 9, 9, 10, 11 | |
803 | }; | |
804 | static const yytype_uint8 yyr2[] = | |
805 | { | |
806 | 0, 2, 1, 0, 2, 4, 2 | |
807 | }; | |
808 | ]]) | |
809 | ||
810 | AT_CLEANUP | |
811 | ||
812 | ||
813 | ## ------------------------- ## | |
814 | ## yycheck Bound Violation. ## | |
815 | ## ------------------------- ## | |
816 | ||
817 | ||
818 | # _AT_DATA_DANCER_Y(BISON-OPTIONS) | |
819 | # -------------------------------- | |
820 | # The following grammar, taken from Andrew Suffield's GPL'd implementation | |
821 | # of DGMTP, the Dancer Generic Message Transport Protocol, used to violate | |
822 | # yycheck's bounds where issuing a verbose error message. Keep this test | |
823 | # so that possible bound checking compilers could check all the skeletons. | |
824 | m4_define([_AT_DATA_DANCER_Y], | |
825 | [AT_DATA_GRAMMAR([dancer.y], | |
826 | [%{ | |
827 | static int yylex (AT_LALR1_CC_IF([int *], [void])); | |
828 | AT_LALR1_CC_IF([#include <cstdlib>], | |
829 | [#include <stdlib.h> | |
830 | #include <stdio.h> | |
831 | static void yyerror (const char *);]) | |
832 | %} | |
833 | $1 | |
834 | %token ARROW INVALID NUMBER STRING DATA | |
835 | %defines | |
836 | %verbose | |
837 | %error-verbose | |
838 | /* Grammar follows */ | |
839 | %% | |
840 | line: header body | |
841 | ; | |
842 | ||
843 | header: '<' from ARROW to '>' type ':' | |
844 | | '<' ARROW to '>' type ':' | |
845 | | ARROW to type ':' | |
846 | | type ':' | |
847 | | '<' '>' | |
848 | ; | |
849 | ||
850 | from: DATA | |
851 | | STRING | |
852 | | INVALID | |
853 | ; | |
854 | ||
855 | to: DATA | |
856 | | STRING | |
857 | | INVALID | |
858 | ; | |
859 | ||
860 | type: DATA | |
861 | | STRING | |
862 | | INVALID | |
863 | ; | |
864 | ||
865 | body: /* empty */ | |
866 | | body member | |
867 | ; | |
868 | ||
869 | member: STRING | |
870 | | DATA | |
871 | | '+' NUMBER | |
872 | | '-' NUMBER | |
873 | | NUMBER | |
874 | | INVALID | |
875 | ; | |
876 | %% | |
877 | AT_LALR1_CC_IF( | |
878 | [/* A C++ error reporting function. */ | |
879 | void | |
880 | yy::parser::error (const std::string& m) | |
881 | { | |
882 | std::cerr << m << std::endl; | |
883 | } | |
884 | ||
885 | int | |
886 | yyparse () | |
887 | { | |
888 | yy::parser parser; | |
889 | #if YYDEBUG | |
890 | parser.set_debug_level (YYDEBUG); | |
891 | #endif | |
892 | return parser.parse (); | |
893 | } | |
894 | ], | |
895 | [static void | |
896 | yyerror (const char *s) | |
897 | { | |
898 | fprintf (stderr, "%s\n", s); | |
899 | }]) | |
900 | ||
901 | static int | |
902 | yylex (AT_LALR1_CC_IF([int *lval], [void])) | |
903 | [{ | |
904 | static int const tokens[] = | |
905 | { | |
906 | ':', -1 | |
907 | }; | |
908 | static size_t toknum; | |
909 | ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[ | |
910 | if (! (toknum < sizeof tokens / sizeof *tokens)) | |
911 | abort (); | |
912 | return tokens[toknum++]; | |
913 | }] | |
914 | ||
915 | int | |
916 | main (void) | |
917 | { | |
918 | return yyparse (); | |
919 | } | |
920 | ]) | |
921 | ])# _AT_DATA_DANCER_Y | |
922 | ||
923 | ||
924 | # AT_CHECK_DANCER(BISON-OPTIONS) | |
925 | # ------------------------------ | |
926 | # Generate the grammar, compile it, run it. | |
927 | m4_define([AT_CHECK_DANCER], | |
928 | [AT_SETUP([Dancer $1]) | |
929 | AT_BISON_OPTION_PUSHDEFS([$1]) | |
930 | _AT_DATA_DANCER_Y([$1]) | |
931 | AT_BISON_CHECK([-o dancer.c dancer.y]) | |
932 | AT_FULL_COMPILE([dancer]) | |
933 | AT_PARSER_CHECK([./dancer], 1, [], | |
934 | [syntax error, unexpected ':' | |
935 | ]) | |
936 | AT_BISON_OPTION_POPDEFS | |
937 | AT_CLEANUP | |
938 | ]) | |
939 | ||
940 | AT_CHECK_DANCER() | |
941 | AT_CHECK_DANCER([%glr-parser]) | |
942 | AT_CHECK_DANCER([%skeleton "lalr1.cc"]) | |
943 | ||
944 | ||
945 | ## ------------------------------------------ ## | |
946 | ## Diagnostic that expects two alternatives. ## | |
947 | ## ------------------------------------------ ## | |
948 | ||
949 | ||
950 | # _AT_DATA_EXPECT2_Y(BISON-OPTIONS) | |
951 | # -------------------------------- | |
952 | m4_define([_AT_DATA_EXPECT2_Y], | |
953 | [AT_DATA_GRAMMAR([expect2.y], | |
954 | [%{ | |
955 | static int yylex (AT_LALR1_CC_IF([int *], [void])); | |
956 | AT_LALR1_CC_IF([#include <cstdlib>], | |
957 | [#include <stdio.h> | |
958 | #include <stdlib.h> | |
959 | static void yyerror (const char *);]) | |
960 | %} | |
961 | $1 | |
962 | %defines | |
963 | %error-verbose | |
964 | %token A 1000 | |
965 | %token B | |
966 | ||
967 | %% | |
968 | program: /* empty */ | |
969 | | program e ';' | |
970 | | program error ';'; | |
971 | ||
972 | e: e '+' t | t; | |
973 | t: A | B; | |
974 | ||
975 | %% | |
976 | AT_LALR1_CC_IF( | |
977 | [/* A C++ error reporting function. */ | |
978 | void | |
979 | yy::parser::error (const std::string& m) | |
980 | { | |
981 | std::cerr << m << std::endl; | |
982 | } | |
983 | ||
984 | int | |
985 | yyparse () | |
986 | { | |
987 | yy::parser parser; | |
988 | return parser.parse (); | |
989 | } | |
990 | ], | |
991 | [static void | |
992 | yyerror (const char *s) | |
993 | { | |
994 | fprintf (stderr, "%s\n", s); | |
995 | }]) | |
996 | ||
997 | static int | |
998 | yylex (AT_LALR1_CC_IF([int *lval], [void])) | |
999 | [{ | |
1000 | static int const tokens[] = | |
1001 | { | |
1002 | 1000, '+', '+', -1 | |
1003 | }; | |
1004 | static size_t toknum; | |
1005 | ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[ | |
1006 | if (! (toknum < sizeof tokens / sizeof *tokens)) | |
1007 | abort (); | |
1008 | return tokens[toknum++]; | |
1009 | }] | |
1010 | ||
1011 | int | |
1012 | main (void) | |
1013 | { | |
1014 | return yyparse (); | |
1015 | } | |
1016 | ]) | |
1017 | ])# _AT_DATA_EXPECT2_Y | |
1018 | ||
1019 | ||
1020 | # AT_CHECK_EXPECT2(BISON-OPTIONS) | |
1021 | # ------------------------------ | |
1022 | # Generate the grammar, compile it, run it. | |
1023 | m4_define([AT_CHECK_EXPECT2], | |
1024 | [AT_SETUP([Expecting two tokens $1]) | |
1025 | AT_BISON_OPTION_PUSHDEFS([$1]) | |
1026 | _AT_DATA_EXPECT2_Y([$1]) | |
1027 | AT_BISON_CHECK([-o expect2.c expect2.y]) | |
1028 | AT_FULL_COMPILE([expect2]) | |
1029 | AT_PARSER_CHECK([./expect2], 1, [], | |
1030 | [syntax error, unexpected '+', expecting A or B | |
1031 | ]) | |
1032 | AT_BISON_OPTION_POPDEFS | |
1033 | AT_CLEANUP | |
1034 | ]) | |
1035 | ||
1036 | AT_CHECK_EXPECT2() | |
1037 | AT_CHECK_EXPECT2([%glr-parser]) | |
1038 | AT_CHECK_EXPECT2([%skeleton "lalr1.cc"]) | |
1039 | ||
1040 | ||
1041 | ||
1042 | ## --------------------------------------------- ## | |
1043 | ## Braced code in declaration in rules section. ## | |
1044 | ## --------------------------------------------- ## | |
1045 | ||
1046 | AT_SETUP([Braced code in declaration in rules section]) | |
1047 | ||
1048 | # Bison once mistook braced code in a declaration in the rules section to be a | |
1049 | # rule action. | |
1050 | ||
1051 | AT_DATA_GRAMMAR([input.y], | |
1052 | [[%{ | |
1053 | #include <stdio.h> | |
1054 | static void yyerror (char const *msg); | |
1055 | static int yylex (void); | |
1056 | %} | |
1057 | ||
1058 | %error-verbose | |
1059 | ||
1060 | %% | |
1061 | ||
1062 | start: | |
1063 | { | |
1064 | printf ("Bison would once convert this action to a midrule because of the" | |
1065 | " subsequent braced code.\n"); | |
1066 | } | |
1067 | ; | |
1068 | ||
1069 | %destructor { fprintf (stderr, "DESTRUCTOR\n"); } 'a'; | |
1070 | %printer { fprintf (yyoutput, "PRINTER"); } 'a'; | |
1071 | ||
1072 | %% | |
1073 | ||
1074 | static void | |
1075 | yyerror (char const *msg) | |
1076 | { | |
1077 | fprintf (stderr, "%s\n", msg); | |
1078 | } | |
1079 | ||
1080 | static int | |
1081 | yylex (void) | |
1082 | { | |
1083 | return 'a'; | |
1084 | } | |
1085 | ||
1086 | int | |
1087 | main (void) | |
1088 | { | |
1089 | yydebug = 1; | |
1090 | return !yyparse (); | |
1091 | } | |
1092 | ]]) | |
1093 | ||
1094 | AT_BISON_CHECK([-t -o input.c input.y]) | |
1095 | AT_COMPILE([input]) | |
1096 | AT_PARSER_CHECK([./input], 0, | |
1097 | [[Bison would once convert this action to a midrule because of the subsequent braced code. | |
1098 | ]], | |
1099 | [[Starting parse | |
1100 | Entering state 0 | |
1101 | Reducing stack by rule 1 (line 20): | |
1102 | -> $$ = nterm start () | |
1103 | Stack now 0 | |
1104 | Entering state 1 | |
1105 | Reading a token: Next token is token 'a' (PRINTER) | |
1106 | syntax error, unexpected 'a', expecting $end | |
1107 | Error: popping nterm start () | |
1108 | Stack now 0 | |
1109 | Cleanup: discarding lookahead token 'a' (PRINTER) | |
1110 | DESTRUCTOR | |
1111 | Stack now 0 | |
1112 | ]]) | |
1113 | ||
1114 | AT_CLEANUP | |
1115 | ||
1116 | ||
1117 | ||
1118 | ## --------------------------------- ## | |
1119 | ## String alias declared after use. ## | |
1120 | ## --------------------------------- ## | |
1121 | ||
1122 | AT_SETUP([String alias declared after use]) | |
1123 | ||
1124 | # Bison once incorrectly asserted that the symbol number for either a token or | |
1125 | # its alias was the highest symbol number so far at the point of the alias | |
1126 | # declaration. That was true unless the declaration appeared after their first | |
1127 | # uses and other tokens appeared in between. | |
1128 | ||
1129 | AT_DATA([input.y], | |
1130 | [[%% | |
1131 | start: 'a' "A" 'b'; | |
1132 | %token 'a' "A"; | |
1133 | ]]) | |
1134 | ||
1135 | AT_BISON_CHECK([-t -o input.c input.y]) | |
1136 | ||
1137 | AT_CLEANUP | |
1138 | ||
1139 | ||
1140 | ||
1141 | ## -------------------------------- ## | |
1142 | ## Extra lookahead sets in report. ## | |
1143 | ## -------------------------------- ## | |
1144 | ||
1145 | AT_SETUP([[Extra lookahead sets in report]]) | |
1146 | ||
1147 | # Bison prints each reduction's lookahead set only next to the associated | |
1148 | # state's one item that (1) is associated with the same rule as the reduction | |
1149 | # and (2) has its dot at the end of its RHS. Previously, Bison also | |
1150 | # erroneously printed the lookahead set next to all of the state's other items | |
1151 | # associated with the same rule. This bug affected only the `.output' file and | |
1152 | # not the generated parser source code. | |
1153 | ||
1154 | AT_DATA([[input.y]], | |
1155 | [[%% | |
1156 | start: a | 'a' a 'a' ; | |
1157 | a: 'a' ; | |
1158 | ]]) | |
1159 | ||
1160 | AT_BISON_CHECK([[--report=all input.y]]) | |
1161 | AT_CHECK([[sed -n '/^state 1$/,/^state 2$/p' input.output]], [[0]], | |
1162 | [[state 1 | |
1163 | ||
1164 | 2 start: 'a' . a 'a' | |
1165 | 3 a: . 'a' | |
1166 | 3 | 'a' . [$end] | |
1167 | ||
1168 | 'a' shift, and go to state 4 | |
1169 | ||
1170 | $default reduce using rule 3 (a) | |
1171 | ||
1172 | a go to state 5 | |
1173 | ||
1174 | ||
1175 | state 2 | |
1176 | ]]) | |
1177 | ||
1178 | AT_CLEANUP | |
1179 | ||
1180 | ||
1181 | ||
1182 | ## ---------------------------------------- ## | |
1183 | ## Token number in precedence declaration. ## | |
1184 | ## ---------------------------------------- ## | |
1185 | ||
1186 | AT_SETUP([[Token number in precedence declaration]]) | |
1187 | ||
1188 | # POSIX says token numbers can be declared in %left, %right, and %nonassoc, but | |
1189 | # we lost this in Bison 1.50. | |
1190 | ||
1191 | AT_DATA_GRAMMAR([input.y], | |
1192 | [[%{ | |
1193 | #include <stdio.h> | |
1194 | void yyerror (char const *); | |
1195 | int yylex (void); | |
1196 | %} | |
1197 | ||
1198 | %error-verbose | |
1199 | %left TK1 1 TK2 2 "tok alias" 3 | |
1200 | ||
1201 | %% | |
1202 | ||
1203 | start: TK1 sr_conflict "tok alias" ; | |
1204 | ||
1205 | sr_conflict: | |
1206 | TK2 | |
1207 | | TK2 "tok alias" | |
1208 | ; | |
1209 | ||
1210 | %% | |
1211 | ||
1212 | void | |
1213 | yyerror (char const *msg) | |
1214 | { | |
1215 | fprintf (stderr, "%s\n", msg); | |
1216 | } | |
1217 | ||
1218 | int | |
1219 | yylex (void) | |
1220 | { | |
1221 | static int const input[] = { 1, 2, 3, 0 }; | |
1222 | static int const *inputp = input; | |
1223 | return *inputp++; | |
1224 | } | |
1225 | ||
1226 | int | |
1227 | main (void) | |
1228 | { | |
1229 | return yyparse (); | |
1230 | } | |
1231 | ]]) | |
1232 | ||
1233 | AT_BISON_CHECK([[-o input.c input.y]], [[0]],, | |
1234 | [[input.y:24.5-19: warning: rule useless in parser due to conflicts: sr_conflict: TK2 "tok alias" | |
1235 | ]]) | |
1236 | AT_COMPILE([[input]]) | |
1237 | AT_PARSER_CHECK([[./input]]) | |
1238 | ||
1239 | AT_CLEANUP | |
1240 | ||
1241 | ||
1242 | ||
1243 | ## --------------------------- ## | |
1244 | ## parse-gram.y: LALR = IELR. ## | |
1245 | ## --------------------------- ## | |
1246 | ||
1247 | # If parse-gram.y's LALR and IELR parser tables ever begin to differ, we | |
1248 | # need to fix parse-gram.y or start using IELR. | |
1249 | ||
1250 | AT_SETUP([[parse-gram.y: LALR = IELR]]) | |
1251 | ||
1252 | # Avoid tests/bison's dark magic by processing a local copy of the | |
1253 | # grammar. Avoid differences in synclines by telling bison that the | |
1254 | # output files have the same name. | |
1255 | [cp $abs_top_srcdir/src/parse-gram.y input.y] | |
1256 | AT_BISON_CHECK([[-o input.c -Dlr.type=lalr input.y]]) | |
1257 | [mv input.c lalr.c] | |
1258 | AT_BISON_CHECK([[-o input.c -Dlr.type=ielr input.y]]) | |
1259 | [mv input.c ielr.c] | |
1260 | AT_CHECK([[diff -u lalr.c ielr.c]]) | |
1261 | ||
1262 | AT_CLEANUP | |
1263 | ||
1264 | ||
1265 | ||
1266 | ## -------------------------------------------- ## | |
1267 | ## parse.error=verbose and YYSTACK_USE_ALLOCA. ## | |
1268 | ## -------------------------------------------- ## | |
1269 | ||
1270 | AT_SETUP([[parse.error=verbose and YYSTACK_USE_ALLOCA]]) | |
1271 | ||
1272 | AT_DATA_GRAMMAR([input.y], | |
1273 | [[%code { | |
1274 | #include <stdio.h> | |
1275 | void yyerror (char const *); | |
1276 | int yylex (void); | |
1277 | #define YYSTACK_USE_ALLOCA 1 | |
1278 | } | |
1279 | ||
1280 | %define parse.error verbose | |
1281 | ||
1282 | %% | |
1283 | ||
1284 | start: check syntax_error syntax_error ; | |
1285 | ||
1286 | check: | |
1287 | { | |
1288 | if (128 < sizeof yymsgbuf) | |
1289 | { | |
1290 | fprintf (stderr, | |
1291 | "The initial size of yymsgbuf in yyparse has increased\n" | |
1292 | "since this test group was last updated. As a result,\n" | |
1293 | "this test group may no longer manage to induce a\n" | |
1294 | "reallocation of the syntax error message buffer.\n" | |
1295 | "This test group must be adjusted to produce a longer\n" | |
1296 | "error message.\n"); | |
1297 | YYABORT; | |
1298 | } | |
1299 | } | |
1300 | ; | |
1301 | ||
1302 | // Induce a syntax error message whose total length is more than | |
1303 | // sizeof yymsgbuf in yyparse. Each token here is 64 bytes. | |
1304 | syntax_error: | |
1305 | "123456789112345678921234567893123456789412345678951234567896123A" | |
1306 | | "123456789112345678921234567893123456789412345678951234567896123B" | |
1307 | | error 'a' 'b' 'c' | |
1308 | ; | |
1309 | ||
1310 | %% | |
1311 | ||
1312 | void | |
1313 | yyerror (char const *msg) | |
1314 | { | |
1315 | fprintf (stderr, "%s\n", msg); | |
1316 | } | |
1317 | ||
1318 | int | |
1319 | yylex (void) | |
1320 | { | |
1321 | /* Induce two syntax error messages (which requires full error | |
1322 | recovery by shifting 3 tokens) in order to detect any loss of the | |
1323 | reallocated buffer. */ | |
1324 | static char const *input = "abc"; | |
1325 | return *input++; | |
1326 | } | |
1327 | ||
1328 | int | |
1329 | main (void) | |
1330 | { | |
1331 | return yyparse (); | |
1332 | } | |
1333 | ]]) | |
1334 | ||
1335 | AT_BISON_CHECK([[-o input.c input.y]]) | |
1336 | AT_COMPILE([[input]]) | |
1337 | AT_PARSER_CHECK([[./input]], [[1]], [], | |
1338 | [[syntax error, unexpected 'a', expecting 123456789112345678921234567893123456789412345678951234567896123A or 123456789112345678921234567893123456789412345678951234567896123B | |
1339 | syntax error, unexpected $end, expecting 123456789112345678921234567893123456789412345678951234567896123A or 123456789112345678921234567893123456789412345678951234567896123B | |
1340 | ]]) | |
1341 | ||
1342 | AT_CLEANUP | |
1343 | ||
1344 | ||
1345 | ||
1346 | ## ------------------------------ ## | |
1347 | ## parse.error=verbose overflow. ## | |
1348 | ## ------------------------------ ## | |
1349 | ||
1350 | # Imagine the case where YYSTACK_ALLOC_MAXIMUM = YYSIZE_MAXIMUM and an | |
1351 | # invocation of yysyntax_error has caused yymsg_alloc to grow to exactly | |
1352 | # YYSTACK_ALLOC_MAXIMUM (perhaps because the normal doubling of size had | |
1353 | # to be clipped to YYSTACK_ALLOC_MAXIMUM). In an old version of yacc.c, | |
1354 | # a subsequent invocation of yysyntax_error that overflows during its | |
1355 | # size calculation would return YYSIZE_MAXIMUM to yyparse. Then, | |
1356 | # yyparse would invoke yyerror using the old contents of yymsg. | |
1357 | ||
1358 | AT_SETUP([[parse.error=verbose overflow]]) | |
1359 | ||
1360 | AT_DATA_GRAMMAR([input.y], | |
1361 | [[%code { | |
1362 | #include <stdio.h> | |
1363 | void yyerror (char const *); | |
1364 | int yylex (void); | |
1365 | ||
1366 | /* This prevents this test case from having to induce error messages | |
1367 | large enough to overflow size_t. */ | |
1368 | #define YYSIZE_T unsigned char | |
1369 | ||
1370 | /* Bring in malloc and set EXIT_SUCCESS so yacc.c doesn't try to | |
1371 | provide a malloc prototype using our YYSIZE_T. */ | |
1372 | #include <stdlib.h> | |
1373 | #ifndef EXIT_SUCCESS | |
1374 | # define EXIT_SUCCESS 0 | |
1375 | #endif | |
1376 | ||
1377 | /* Max depth is usually much smaller than YYSTACK_ALLOC_MAXIMUM, and | |
1378 | we don't want gcc to warn everywhere this constant would be too big | |
1379 | to make sense for our YYSIZE_T. */ | |
1380 | #define YYMAXDEPTH 100 | |
1381 | } | |
1382 | ||
1383 | %define parse.error verbose | |
1384 | ||
1385 | %% | |
1386 | ||
1387 | start: syntax_error1 check syntax_error2 ; | |
1388 | ||
1389 | // Induce a syntax error message whose total length causes yymsg in | |
1390 | // yyparse to be reallocated to size YYSTACK_ALLOC_MAXIMUM, which | |
1391 | // should be 255. Each token here is 64 bytes. | |
1392 | syntax_error1: | |
1393 | "123456789112345678921234567893123456789412345678951234567896123A" | |
1394 | | "123456789112345678921234567893123456789412345678951234567896123B" | |
1395 | | "123456789112345678921234567893123456789412345678951234567896123C" | |
1396 | | error 'a' 'b' 'c' | |
1397 | ; | |
1398 | ||
1399 | check: | |
1400 | { | |
1401 | if (yymsg_alloc != YYSTACK_ALLOC_MAXIMUM | |
1402 | || YYSTACK_ALLOC_MAXIMUM != YYSIZE_MAXIMUM | |
1403 | || YYSIZE_MAXIMUM != 255) | |
1404 | { | |
1405 | fprintf (stderr, | |
1406 | "The assumptions of this test group are no longer\n" | |
1407 | "valid, so it may no longer catch the error it was\n" | |
1408 | "designed to catch. Specifically, the following\n" | |
1409 | "values should all be 255:\n\n"); | |
1410 | fprintf (stderr, " yymsg_alloc = %d\n", yymsg_alloc); | |
1411 | fprintf (stderr, " YYSTACK_ALLOC_MAXIMUM = %d\n", | |
1412 | YYSTACK_ALLOC_MAXIMUM); | |
1413 | fprintf (stderr, " YYSIZE_MAXIMUM = %d\n", YYSIZE_MAXIMUM); | |
1414 | YYABORT; | |
1415 | } | |
1416 | } | |
1417 | ; | |
1418 | ||
1419 | // Now overflow. | |
1420 | syntax_error2: | |
1421 | "123456789112345678921234567893123456789412345678951234567896123A" | |
1422 | | "123456789112345678921234567893123456789412345678951234567896123B" | |
1423 | | "123456789112345678921234567893123456789412345678951234567896123C" | |
1424 | | "123456789112345678921234567893123456789412345678951234567896123D" | |
1425 | | "123456789112345678921234567893123456789412345678951234567896123E" | |
1426 | ; | |
1427 | ||
1428 | %% | |
1429 | ||
1430 | void | |
1431 | yyerror (char const *msg) | |
1432 | { | |
1433 | fprintf (stderr, "%s\n", msg); | |
1434 | } | |
1435 | ||
1436 | int | |
1437 | yylex (void) | |
1438 | { | |
1439 | /* Induce two syntax error messages (which requires full error | |
1440 | recovery by shifting 3 tokens). */ | |
1441 | static char const *input = "abc"; | |
1442 | return *input++; | |
1443 | } | |
1444 | ||
1445 | int | |
1446 | main (void) | |
1447 | { | |
1448 | /* Push parsers throw away the message buffer between tokens, so skip | |
1449 | this test under maintainer-push-check. */ | |
1450 | if (YYPUSH) | |
1451 | return 77; | |
1452 | return yyparse (); | |
1453 | } | |
1454 | ]]) | |
1455 | ||
1456 | AT_BISON_CHECK([[-o input.c input.y]]) | |
1457 | ||
1458 | # gcc warns about tautologies and fallacies involving comparisons for | |
1459 | # unsigned char. However, it doesn't produce these same warnings for | |
1460 | # size_t and many other types when the warnings would seem to make just | |
1461 | # as much sense. We ignore the warnings. | |
1462 | [CFLAGS="$NO_WERROR_CFLAGS"] | |
1463 | AT_COMPILE([[input]]) | |
1464 | ||
1465 | AT_PARSER_CHECK([[./input]], [[2]], [], | |
1466 | [[syntax error, unexpected 'a', expecting 123456789112345678921234567893123456789412345678951234567896123A or 123456789112345678921234567893123456789412345678951234567896123B or 123456789112345678921234567893123456789412345678951234567896123C | |
1467 | syntax error | |
1468 | memory exhausted | |
1469 | ]]) | |
1470 | ||
1471 | AT_CLEANUP | |
1472 | ||
1473 | ||
1474 | ||
1475 | ## ------------------------ ## | |
1476 | ## LAC: Exploratory stack. ## | |
1477 | ## ------------------------ ## | |
1478 | ||
1479 | AT_SETUP([[LAC: Exploratory stack]]) | |
1480 | ||
1481 | m4_pushdef([AT_LAC_CHECK], [ | |
1482 | ||
1483 | AT_BISON_OPTION_PUSHDEFS([$1]) | |
1484 | ||
1485 | AT_DATA_GRAMMAR([input.y], | |
1486 | [[%code { | |
1487 | #include <stdio.h> | |
1488 | void yyerror (char const *); | |
1489 | int yylex (]AT_PURE_IF([[YYSTYPE *]], [[void]])[); | |
1490 | } | |
1491 | ||
1492 | ]$1[ | |
1493 | %define parse.error verbose | |
1494 | %token 'c' | |
1495 | ||
1496 | %% | |
1497 | ||
1498 | // default reductions in inconsistent states | |
1499 | // v v v v v v v v v v v v v v | |
1500 | S: A B A A B A A A A B A A A A A A A B C C A A A A A A A A A A A A B ; | |
1501 | // ^ ^ ^ | |
1502 | // LAC reallocs | |
1503 | ||
1504 | A: 'a' | /*empty*/ { printf ("inconsistent default reduction\n"); } ; | |
1505 | B: 'b' ; | |
1506 | C: /*empty*/ { printf ("consistent default reduction\n"); } ; | |
1507 | ||
1508 | %% | |
1509 | ||
1510 | void | |
1511 | yyerror (char const *msg) | |
1512 | { | |
1513 | fprintf (stderr, "%s\n", msg); | |
1514 | } | |
1515 | ||
1516 | int | |
1517 | yylex (]AT_PURE_IF([[YYSTYPE *v]], [[void]])[) | |
1518 | { | |
1519 | static char const *input = "bbbbc";]AT_PURE_IF([[ | |
1520 | *v = 0;]])[ | |
1521 | return *input++; | |
1522 | } | |
1523 | ||
1524 | int | |
1525 | main (void) | |
1526 | { | |
1527 | yydebug = 1; | |
1528 | return yyparse (); | |
1529 | } | |
1530 | ]]) | |
1531 | ||
1532 | AT_BISON_CHECK([[-Dparse.lac=full -Dparse.lac.es-capacity-initial=1 \ | |
1533 | -Dparse.lac.memory-trace=full \ | |
1534 | -t -o input.c input.y]], [[0]], [], | |
1535 | [[input.y: conflicts: 21 shift/reduce | |
1536 | ]]) | |
1537 | AT_COMPILE([[input]]) | |
1538 | AT_PARSER_CHECK([[./input > stdout.txt 2> stderr.txt]], [[1]]) | |
1539 | ||
1540 | # Make sure syntax error doesn't forget that 'a' is expected. It would | |
1541 | # be forgotten without lookahead correction. | |
1542 | AT_CHECK([[grep 'syntax error,' stderr.txt]], [[0]], | |
1543 | [[syntax error, unexpected 'c', expecting 'a' or 'b' | |
1544 | ]]) | |
1545 | ||
1546 | # Check number of default reductions in inconsistent states to be sure | |
1547 | # syntax error is detected before unnecessary reductions are performed. | |
1548 | AT_CHECK([[perl -0777 -ne 'print s/inconsistent default reduction//g;' \ | |
1549 | < stdout.txt || exit 77]], [[0]], [[14]]) | |
1550 | ||
1551 | # Check number of default reductions in consistent states to be sure | |
1552 | # it is performed before the syntax error is detected. | |
1553 | AT_CHECK([[perl -0777 -ne 'print s/\bconsistent default reduction//g;' \ | |
1554 | < stdout.txt || exit 77]], [[0]], [[2]]) | |
1555 | ||
1556 | # Check number of reallocs to be sure reallocated memory isn't somehow | |
1557 | # lost between LAC invocations. | |
1558 | AT_CHECK([[perl -0777 -ne 'print s/\(realloc//g;' < stderr.txt \ | |
1559 | || exit 77]], [[0]], [[3]]) | |
1560 | ||
1561 | AT_BISON_OPTION_POPDEFS | |
1562 | ]) | |
1563 | ||
1564 | AT_LAC_CHECK([[%define api.push-pull pull]]) | |
1565 | AT_LAC_CHECK([[%define api.push-pull pull %define api.pure]]) | |
1566 | AT_LAC_CHECK([[%define api.push-pull both]]) | |
1567 | AT_LAC_CHECK([[%define api.push-pull both %define api.pure]]) | |
1568 | ||
1569 | m4_popdef([AT_LAC_CHECK]) | |
1570 | ||
1571 | AT_CLEANUP | |
1572 | ||
1573 | ||
1574 | ||
1575 | ## ------------------------ ## | |
1576 | ## LAC: Memory exhaustion. ## | |
1577 | ## ------------------------ ## | |
1578 | ||
1579 | AT_SETUP([[LAC: Memory exhaustion]]) | |
1580 | ||
1581 | m4_pushdef([AT_LAC_CHECK], [ | |
1582 | ||
1583 | AT_DATA_GRAMMAR([input.y], | |
1584 | [[%code { | |
1585 | #include <stdio.h> | |
1586 | void yyerror (char const *); | |
1587 | int yylex (void); | |
1588 | #define YYMAXDEPTH 8 | |
1589 | } | |
1590 | ||
1591 | %error-verbose | |
1592 | ||
1593 | %% | |
1594 | ||
1595 | S: A A A A A A A A A ; | |
1596 | A: /*empty*/ | 'a' ; | |
1597 | ||
1598 | %% | |
1599 | ||
1600 | void | |
1601 | yyerror (char const *msg) | |
1602 | { | |
1603 | fprintf (stderr, "%s\n", msg); | |
1604 | } | |
1605 | ||
1606 | int | |
1607 | yylex (void) | |
1608 | { | |
1609 | static char const *input = "]$1["; | |
1610 | return *input++; | |
1611 | } | |
1612 | ||
1613 | int | |
1614 | main (void) | |
1615 | { | |
1616 | yydebug = 1; | |
1617 | return yyparse (); | |
1618 | } | |
1619 | ]]) | |
1620 | ||
1621 | AT_BISON_CHECK([[-Dparse.lac=full -Dparse.lac.es-capacity-initial=1 \ | |
1622 | -t -o input.c input.y]], [[0]], [], | |
1623 | [[input.y: conflicts: 8 shift/reduce | |
1624 | ]]) | |
1625 | AT_COMPILE([[input]]) | |
1626 | ||
1627 | ]) | |
1628 | ||
1629 | # Check for memory exhaustion during parsing. | |
1630 | AT_LAC_CHECK([[]]) | |
1631 | AT_PARSER_CHECK([[./input]], [[2]], [[]], | |
1632 | [[Starting parse | |
1633 | Entering state 0 | |
1634 | Reading a token: Now at end of input. | |
1635 | LAC: initial context established for $end | |
1636 | LAC: checking lookahead $end: R2 G3 R2 G5 R2 G6 R2 G7 R2 G8 R2 G9 R2 G10 R2 G11 R2 (max size exceeded) | |
1637 | memory exhausted | |
1638 | Cleanup: discarding lookahead token $end () | |
1639 | Stack now 0 | |
1640 | ]]) | |
1641 | ||
1642 | # Induce an immediate syntax error with an undefined token, and check | |
1643 | # for memory exhaustion while building syntax error message. | |
1644 | AT_LAC_CHECK([[z]], [[0]]) | |
1645 | AT_PARSER_CHECK([[./input]], [[2]], [[]], | |
1646 | [[Starting parse | |
1647 | Entering state 0 | |
1648 | Reading a token: Next token is token $undefined () | |
1649 | LAC: initial context established for $undefined | |
1650 | LAC: checking lookahead $undefined: Always Err | |
1651 | Constructing syntax error message | |
1652 | LAC: checking lookahead $end: R2 G3 R2 G5 R2 G6 R2 G7 R2 G8 R2 G9 R2 G10 R2 G11 R2 (max size exceeded) | |
1653 | syntax error | |
1654 | memory exhausted | |
1655 | Cleanup: discarding lookahead token $undefined () | |
1656 | Stack now 0 | |
1657 | ]]) | |
1658 | ||
1659 | m4_popdef([AT_LAC_CHECK]) | |
1660 | ||
1661 | AT_CLEANUP |