]> git.saurik.com Git - bison.git/blob - tests/regression.at
Update to the current gnulib CVS repository, and fix trigraph handling
[bison.git] / tests / regression.at
1 # Bison Regressions. -*- Autotest -*-
2
3 # Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software
4 # Foundation, Inc.
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 AT_BANNER([[Regression tests.]])
20
21
22 ## ------------------ ##
23 ## Trivial grammars. ##
24 ## ------------------ ##
25
26 AT_SETUP([Trivial grammars])
27
28 AT_DATA_GRAMMAR([input.y],
29 [[%{
30 void yyerror (char const *);
31 int yylex (void);
32 #define YYSTYPE int *
33 %}
34
35 %error-verbose
36
37 %%
38
39 program: 'x';
40 ]])
41
42 AT_BISON_CHECK([-o input.c input.y])
43 AT_COMPILE([input.o], [-c input.c])
44 AT_COMPILE([input.o], [-DYYDEBUG -c input.c])
45
46 AT_CLEANUP
47
48
49
50 ## ----------------- ##
51 ## YYSTYPE typedef. ##
52 ## ----------------- ##
53
54 AT_SETUP([YYSTYPE typedef])
55
56 AT_DATA_GRAMMAR([input.y],
57 [[%{
58 void yyerror (char const *);
59 int yylex (void);
60 typedef union { char const *val; } YYSTYPE;
61 %}
62
63 %type <val> program
64
65 %%
66
67 program: { $$ = ""; };
68 ]])
69
70 AT_BISON_CHECK([-o input.c input.y])
71 AT_COMPILE([input.o], [-c input.c])
72
73 AT_CLEANUP
74
75
76
77 ## ------------------------------------- ##
78 ## Early token definitions with --yacc. ##
79 ## ------------------------------------- ##
80
81
82 AT_SETUP([Early token definitions with --yacc])
83
84 # Found in GCJ: they expect the tokens to be defined before the user
85 # prologue, so that they can use the token definitions in it.
86
87 AT_DATA_GRAMMAR([input.y],
88 [[%{
89 void yyerror (const char *s);
90 int yylex (void);
91 %}
92
93 %union
94 {
95 int val;
96 };
97 %{
98 #ifndef MY_TOKEN
99 # error "MY_TOKEN not defined."
100 #endif
101 %}
102 %token MY_TOKEN
103 %%
104 exp: MY_TOKEN;
105 %%
106 ]])
107
108 AT_BISON_CHECK([-y -o input.c input.y])
109 AT_COMPILE([input.o], [-c input.c])
110
111 AT_CLEANUP
112
113
114
115 ## ---------------------------------------- ##
116 ## Early token definitions without --yacc. ##
117 ## ---------------------------------------- ##
118
119
120 AT_SETUP([Early token definitions without --yacc])
121
122 # Found in GCJ: they expect the tokens to be defined before the user
123 # prologue, so that they can use the token definitions in it.
124
125 AT_DATA_GRAMMAR([input.y],
126 [[%{
127 #include <stdio.h>
128 void yyerror (const char *s);
129 int yylex (void);
130 void print_my_token (void);
131 %}
132
133 %union
134 {
135 int val;
136 };
137 %{
138 void
139 print_my_token (void)
140 {
141 enum yytokentype my_token = MY_TOKEN;
142 printf ("%d\n", my_token);
143 }
144 %}
145 %token MY_TOKEN
146 %%
147 exp: MY_TOKEN;
148 %%
149 ]])
150
151 AT_BISON_CHECK([-o input.c input.y])
152 AT_COMPILE([input.o], [-c input.c])
153
154 AT_CLEANUP
155
156
157
158 ## ---------------- ##
159 ## Braces parsing. ##
160 ## ---------------- ##
161
162
163 AT_SETUP([Braces parsing])
164
165 AT_DATA([input.y],
166 [[/* Bison used to swallow the character after `}'. */
167
168 %%
169 exp: { tests = {{{{{{{{{{}}}}}}}}}}; };
170 %%
171 ]])
172
173 AT_BISON_CHECK([-v -o input.c input.y])
174
175 AT_CHECK([grep 'tests = {{{{{{{{{{}}}}}}}}}};' input.c], 0, [ignore])
176
177 AT_CLEANUP
178
179
180 ## ------------------ ##
181 ## Duplicate string. ##
182 ## ------------------ ##
183
184
185 AT_SETUP([Duplicate string])
186
187 AT_DATA([input.y],
188 [[/* `Bison -v' used to dump core when two tokens are defined with the same
189 string, as LE and GE below. */
190
191 %token NUM
192 %token LE "<="
193 %token GE "<="
194
195 %%
196 exp: '(' exp ')' | NUM ;
197 %%
198 ]])
199
200 AT_BISON_CHECK([-v -o input.c input.y], 0, [],
201 [[input.y:6.8-14: warning: symbol `"<="' used more than once as a literal string
202 ]])
203
204 AT_CLEANUP
205
206
207 ## ------------------- ##
208 ## Rule Line Numbers. ##
209 ## ------------------- ##
210
211 AT_SETUP([Rule Line Numbers])
212
213 AT_KEYWORDS([report])
214
215 AT_DATA([input.y],
216 [[%%
217 expr:
218 'a'
219
220 {
221
222 }
223
224 'b'
225
226 {
227
228 }
229
230 |
231
232
233 {
234
235
236 }
237
238 'c'
239
240 {
241
242 };
243 ]])
244
245 AT_BISON_CHECK([-o input.c -v input.y])
246
247 # Check the contents of the report.
248 AT_CHECK([cat input.output], [],
249 [[Grammar
250
251 0 $accept: expr $end
252
253 1 $@1: /* empty */
254
255 2 expr: 'a' $@1 'b'
256
257 3 $@2: /* empty */
258
259 4 expr: $@2 'c'
260
261
262 Terminals, with rules where they appear
263
264 $end (0) 0
265 'a' (97) 2
266 'b' (98) 2
267 'c' (99) 4
268 error (256)
269
270
271 Nonterminals, with rules where they appear
272
273 $accept (6)
274 on left: 0
275 expr (7)
276 on left: 2 4, on right: 0
277 $@1 (8)
278 on left: 1, on right: 2
279 $@2 (9)
280 on left: 3, on right: 4
281
282
283 state 0
284
285 0 $accept: . expr $end
286
287 'a' shift, and go to state 1
288
289 $default reduce using rule 3 ($@2)
290
291 expr go to state 2
292 $@2 go to state 3
293
294
295 state 1
296
297 2 expr: 'a' . $@1 'b'
298
299 $default reduce using rule 1 ($@1)
300
301 $@1 go to state 4
302
303
304 state 2
305
306 0 $accept: expr . $end
307
308 $end shift, and go to state 5
309
310
311 state 3
312
313 4 expr: $@2 . 'c'
314
315 'c' shift, and go to state 6
316
317
318 state 4
319
320 2 expr: 'a' $@1 . 'b'
321
322 'b' shift, and go to state 7
323
324
325 state 5
326
327 0 $accept: expr $end .
328
329 $default accept
330
331
332 state 6
333
334 4 expr: $@2 'c' .
335
336 $default reduce using rule 4 (expr)
337
338
339 state 7
340
341 2 expr: 'a' $@1 'b' .
342
343 $default reduce using rule 2 (expr)
344 ]])
345
346 AT_CLEANUP
347
348
349
350 ## ---------------------- ##
351 ## Mixing %token styles. ##
352 ## ---------------------- ##
353
354
355 AT_SETUP([Mixing %token styles])
356
357 # Taken from the documentation.
358 AT_DATA([input.y],
359 [[%token <operator> OR "||"
360 %token <operator> LE 134 "<="
361 %left OR "<="
362 %%
363 exp: ;
364 %%
365 ]])
366
367 AT_BISON_CHECK([-v -o input.c input.y])
368
369 AT_CLEANUP
370
371
372
373 ## ---------------- ##
374 ## Invalid inputs. ##
375 ## ---------------- ##
376
377
378 AT_SETUP([Invalid inputs])
379
380 AT_DATA([input.y],
381 [[%%
382 ?
383 default: 'a' }
384 %&
385 %a-does-not-exist
386 %-
387 %{
388 ]])
389
390 AT_BISON_CHECK([input.y], [1], [],
391 [[input.y:2.1: invalid character: `?'
392 input.y:3.14: invalid character: `}'
393 input.y:4.1: invalid character: `%'
394 input.y:4.2: invalid character: `&'
395 input.y:5.1-17: invalid directive: `%a-does-not-exist'
396 input.y:6.1: invalid character: `%'
397 input.y:6.2: invalid character: `-'
398 input.y:7.1-8.0: missing `%}' at end of file
399 input.y:7.1-8.0: syntax error, unexpected %{...%}
400 ]])
401
402 AT_CLEANUP
403
404
405 AT_SETUP([Invalid inputs with {}])
406
407 AT_DATA([input.y],
408 [[
409 %destructor
410 %initial-action
411 %lex-param
412 %parse-param
413 %printer
414 %union
415 ]])
416
417 AT_BISON_CHECK([input.y], [1], [],
418 [[input.y:3.1-15: syntax error, unexpected %initial-action, expecting {...}
419 ]])
420
421 AT_CLEANUP
422
423
424
425 ## ------------------- ##
426 ## Token definitions. ##
427 ## ------------------- ##
428
429
430 AT_SETUP([Token definitions])
431
432 # Bison managed, when fed with `%token 'f' "f"' to #define 'f'!
433 AT_DATA_GRAMMAR([input.y],
434 [%{
435 #include <stdlib.h>
436 #include <stdio.h>
437 void yyerror (const char *s);
438 int yylex (void);
439 %}
440 [%error-verbose
441 %token MYEOF 0 "end of file"
442 %token 'a' "a"
443 %token B_TOKEN "b"
444 %token C_TOKEN 'c'
445 %token 'd' D_TOKEN
446 %token SPECIAL "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!"
447 %token SPECIAL "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!"
448 %%
449 exp: "a" "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!";
450 %%
451 void
452 yyerror (char const *s)
453 {
454 fprintf (stderr, "%s\n", s);
455 }
456
457 int
458 yylex (void)
459 {
460 static int called;
461 if (called++)
462 abort ();
463 return SPECIAL;
464 }
465
466 int
467 main (void)
468 {
469 return yyparse ();
470 }
471 ]])
472
473 # Checking the warning message guarantees that the trigraph "??!" isn't
474 # unnecessarily escaped here even though it would need to be if encoded in a
475 # C-string literal. Also notice that unnecessary escaping, such as "\?", from
476 # the user specification is eliminated.
477 AT_BISON_CHECK([-o input.c input.y], [[0]], [[]],
478 [[input.y:22.8-14: warning: symbol SPECIAL redeclared
479 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
480 ]])
481 AT_COMPILE([input])
482
483 # Checking the error message here guarantees that yytname, which does contain
484 # C-string literals, does have the trigraph escaped correctly. Thus, the
485 # symbol name reported by the parser is exactly the same as that reported by
486 # Bison itself.
487 AT_DATA([experr],
488 [[syntax error, unexpected "\\'?\"\a\b\f\n\r\t\v\001\201\001\201??!", expecting a
489 ]])
490 AT_PARSER_CHECK([./input], 1, [], [experr])
491 AT_CLEANUP
492
493
494
495 ## -------------------- ##
496 ## Characters Escapes. ##
497 ## -------------------- ##
498
499
500 AT_SETUP([Characters Escapes])
501
502 AT_DATA_GRAMMAR([input.y],
503 [%{
504 void yyerror (const char *s);
505 int yylex (void);
506 %}
507 [%%
508 exp:
509 '\'' "\'"
510 | '\"' "\""
511 | '"' "'"
512 ;
513 ]])
514 # Pacify font-lock-mode: "
515
516 AT_BISON_CHECK([-o input.c input.y])
517 AT_COMPILE([input.o], [-c input.c])
518 AT_CLEANUP
519
520
521
522 ## -------------- ##
523 ## Web2c Report. ##
524 ## -------------- ##
525
526 # The generation of the reduction was once wrong in Bison, and made it
527 # miss some reductions. In the following test case, the reduction on
528 # `undef_id_tok' in state 1 was missing. This is stripped down from
529 # the actual web2c.y.
530
531 AT_SETUP([Web2c Report])
532
533 AT_KEYWORDS([report])
534
535 AT_DATA([input.y],
536 [[%token undef_id_tok const_id_tok
537
538 %start CONST_DEC_PART
539 \f
540 %%
541 CONST_DEC_PART:
542 CONST_DEC_LIST
543 ;
544
545 CONST_DEC_LIST:
546 CONST_DEC
547 | CONST_DEC_LIST CONST_DEC
548 ;
549
550 CONST_DEC:
551 { } undef_id_tok '=' const_id_tok ';'
552 ;
553 %%
554 ]])
555
556 AT_BISON_CHECK([-v input.y])
557 AT_CHECK([cat input.output], 0,
558 [[Grammar
559
560 0 $accept: CONST_DEC_PART $end
561
562 1 CONST_DEC_PART: CONST_DEC_LIST
563
564 2 CONST_DEC_LIST: CONST_DEC
565 3 | CONST_DEC_LIST CONST_DEC
566
567 4 $@1: /* empty */
568
569 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok ';'
570
571
572 Terminals, with rules where they appear
573
574 $end (0) 0
575 ';' (59) 5
576 '=' (61) 5
577 error (256)
578 undef_id_tok (258) 5
579 const_id_tok (259) 5
580
581
582 Nonterminals, with rules where they appear
583
584 $accept (7)
585 on left: 0
586 CONST_DEC_PART (8)
587 on left: 1, on right: 0
588 CONST_DEC_LIST (9)
589 on left: 2 3, on right: 1 3
590 CONST_DEC (10)
591 on left: 5, on right: 2 3
592 $@1 (11)
593 on left: 4, on right: 5
594
595
596 state 0
597
598 0 $accept: . CONST_DEC_PART $end
599
600 $default reduce using rule 4 ($@1)
601
602 CONST_DEC_PART go to state 1
603 CONST_DEC_LIST go to state 2
604 CONST_DEC go to state 3
605 $@1 go to state 4
606
607
608 state 1
609
610 0 $accept: CONST_DEC_PART . $end
611
612 $end shift, and go to state 5
613
614
615 state 2
616
617 1 CONST_DEC_PART: CONST_DEC_LIST .
618 3 CONST_DEC_LIST: CONST_DEC_LIST . CONST_DEC
619
620 undef_id_tok reduce using rule 4 ($@1)
621 $default reduce using rule 1 (CONST_DEC_PART)
622
623 CONST_DEC go to state 6
624 $@1 go to state 4
625
626
627 state 3
628
629 2 CONST_DEC_LIST: CONST_DEC .
630
631 $default reduce using rule 2 (CONST_DEC_LIST)
632
633
634 state 4
635
636 5 CONST_DEC: $@1 . undef_id_tok '=' const_id_tok ';'
637
638 undef_id_tok shift, and go to state 7
639
640
641 state 5
642
643 0 $accept: CONST_DEC_PART $end .
644
645 $default accept
646
647
648 state 6
649
650 3 CONST_DEC_LIST: CONST_DEC_LIST CONST_DEC .
651
652 $default reduce using rule 3 (CONST_DEC_LIST)
653
654
655 state 7
656
657 5 CONST_DEC: $@1 undef_id_tok . '=' const_id_tok ';'
658
659 '=' shift, and go to state 8
660
661
662 state 8
663
664 5 CONST_DEC: $@1 undef_id_tok '=' . const_id_tok ';'
665
666 const_id_tok shift, and go to state 9
667
668
669 state 9
670
671 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok . ';'
672
673 ';' shift, and go to state 10
674
675
676 state 10
677
678 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok ';' .
679
680 $default reduce using rule 5 (CONST_DEC)
681 ]])
682
683 AT_CLEANUP
684
685
686 ## --------------- ##
687 ## Web2c Actions. ##
688 ## --------------- ##
689
690 # The generation of the mapping `state -> action' was once wrong in
691 # extremely specific situations. web2c.y exhibits this situation.
692 # Below is a stripped version of the grammar. It looks like one can
693 # simplify it further, but just don't: it is tuned to exhibit a bug,
694 # which disapears when applying sane grammar transformations.
695 #
696 # It used to be wrong on yydefact only:
697 #
698 # static const yytype_uint8 yydefact[] =
699 # {
700 # - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
701 # + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4,
702 # 0, 0
703 # };
704 #
705 # but let's check all the tables.
706
707
708 AT_SETUP([Web2c Actions])
709
710 AT_KEYWORDS([report])
711
712 AT_DATA([input.y],
713 [[%%
714 statement: struct_stat;
715 struct_stat: /* empty. */ | if else;
716 if: "if" "const" "then" statement;
717 else: "else" statement;
718 %%
719 ]])
720
721 AT_BISON_CHECK([-v -o input.c input.y])
722
723 # Check only the tables.
724 [sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c >tables.c]
725
726 AT_CHECK([[cat tables.c]], 0,
727 [[static const yytype_uint8 yytranslate[] =
728 {
729 0, 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, 2, 2, 2, 2,
754 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
755 5, 6
756 };
757 static const yytype_uint8 yyprhs[] =
758 {
759 0, 0, 3, 5, 6, 9, 14
760 };
761 static const yytype_int8 yyrhs[] =
762 {
763 8, 0, -1, 9, -1, -1, 10, 11, -1, 3,
764 4, 5, 8, -1, 6, 8, -1
765 };
766 static const yytype_uint8 yyrline[] =
767 {
768 0, 2, 2, 3, 3, 4, 5
769 };
770 static const char *const yytname[] =
771 {
772 "$end", "error", "$undefined", "\"if\"", "\"const\"", "\"then\"",
773 "\"else\"", "$accept", "statement", "struct_stat", "if", "else", 0
774 };
775 static const yytype_uint16 yytoknum[] =
776 {
777 0, 256, 257, 258, 259, 260, 261
778 };
779 static const yytype_uint8 yyr1[] =
780 {
781 0, 7, 8, 9, 9, 10, 11
782 };
783 static const yytype_uint8 yyr2[] =
784 {
785 0, 2, 1, 0, 2, 4, 2
786 };
787 static const yytype_uint8 yydefact[] =
788 {
789 3, 0, 0, 2, 0, 0, 1, 3, 4, 3,
790 6, 5
791 };
792 static const yytype_int8 yydefgoto[] =
793 {
794 -1, 2, 3, 4, 8
795 };
796 static const yytype_int8 yypact[] =
797 {
798 -2, -1, 4, -8, 0, 2, -8, -2, -8, -2,
799 -8, -8
800 };
801 static const yytype_int8 yypgoto[] =
802 {
803 -8, -7, -8, -8, -8
804 };
805 static const yytype_uint8 yytable[] =
806 {
807 10, 1, 11, 5, 6, 0, 7, 9
808 };
809 static const yytype_int8 yycheck[] =
810 {
811 7, 3, 9, 4, 0, -1, 6, 5
812 };
813 static const yytype_uint8 yystos[] =
814 {
815 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
816 8, 8
817 };
818 ]])
819
820 AT_CLEANUP
821
822
823 ## ------------------------- ##
824 ## yycheck Bound Violation. ##
825 ## ------------------------- ##
826
827
828 # _AT_DATA_DANCER_Y(BISON-OPTIONS)
829 # --------------------------------
830 # The following grammar, taken from Andrew Suffield's GPL'd implementation
831 # of DGMTP, the Dancer Generic Message Transport Protocol, used to violate
832 # yycheck's bounds where issuing a verbose error message. Keep this test
833 # so that possible bound checking compilers could check all the skeletons.
834 m4_define([_AT_DATA_DANCER_Y],
835 [AT_DATA_GRAMMAR([dancer.y],
836 [%{
837 static int yylex (AT_LALR1_CC_IF([int *], [void]));
838 AT_LALR1_CC_IF([],
839 [#include <stdlib.h>
840 #include <stdio.h>
841 static void yyerror (const char *);])
842 %}
843 $1
844 %token ARROW INVALID NUMBER STRING DATA
845 %defines
846 %verbose
847 %error-verbose
848 /* Grammar follows */
849 %%
850 line: header body
851 ;
852
853 header: '<' from ARROW to '>' type ':'
854 | '<' ARROW to '>' type ':'
855 | ARROW to type ':'
856 | type ':'
857 | '<' '>'
858 ;
859
860 from: DATA
861 | STRING
862 | INVALID
863 ;
864
865 to: DATA
866 | STRING
867 | INVALID
868 ;
869
870 type: DATA
871 | STRING
872 | INVALID
873 ;
874
875 body: /* empty */
876 | body member
877 ;
878
879 member: STRING
880 | DATA
881 | '+' NUMBER
882 | '-' NUMBER
883 | NUMBER
884 | INVALID
885 ;
886 %%
887 AT_LALR1_CC_IF(
888 [/* A C++ error reporting function. */
889 void
890 yy::parser::error (const location&, const std::string& m)
891 {
892 std::cerr << m << std::endl;
893 }
894
895 int
896 yyparse ()
897 {
898 yy::parser parser;
899 #if YYDEBUG
900 parser.set_debug_level (YYDEBUG);
901 #endif
902 return parser.parse ();
903 }
904 ],
905 [static void
906 yyerror (const char *s)
907 {
908 fprintf (stderr, "%s\n", s);
909 }])
910
911 static int
912 yylex (AT_LALR1_CC_IF([int *lval], [void]))
913 [{
914 static int const tokens[] =
915 {
916 ':', -1
917 };
918 static size_t toknum;
919 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
920 if (! (toknum < sizeof tokens / sizeof *tokens))
921 abort ();
922 return tokens[toknum++];
923 }]
924
925 int
926 main (void)
927 {
928 return yyparse ();
929 }
930 ])
931 ])# _AT_DATA_DANCER_Y
932
933
934 # AT_CHECK_DANCER(BISON-OPTIONS)
935 # ------------------------------
936 # Generate the grammar, compile it, run it.
937 m4_define([AT_CHECK_DANCER],
938 [AT_SETUP([Dancer $1])
939 AT_BISON_OPTION_PUSHDEFS([$1])
940 _AT_DATA_DANCER_Y([$1])
941 AT_BISON_CHECK([-o dancer.c dancer.y])
942 AT_LALR1_CC_IF(
943 [AT_BISON_CHECK([-o dancer.cc dancer.y])
944 AT_COMPILE_CXX([dancer])],
945 [AT_BISON_CHECK([-o dancer.c dancer.y])
946 AT_COMPILE([dancer])])
947 AT_PARSER_CHECK([./dancer], 1, [],
948 [syntax error, unexpected ':'
949 ])
950 AT_BISON_OPTION_POPDEFS
951 AT_CLEANUP
952 ])
953
954 AT_CHECK_DANCER()
955 AT_CHECK_DANCER([%glr-parser])
956 AT_CHECK_DANCER([%skeleton "lalr1.cc"])
957
958
959 ## ------------------------------------------ ##
960 ## Diagnostic that expects two alternatives. ##
961 ## ------------------------------------------ ##
962
963
964 # _AT_DATA_EXPECT2_Y(BISON-OPTIONS)
965 # --------------------------------
966 m4_define([_AT_DATA_EXPECT2_Y],
967 [AT_DATA_GRAMMAR([expect2.y],
968 [%{
969 static int yylex (AT_LALR1_CC_IF([int *], [void]));
970 AT_LALR1_CC_IF([],
971 [#include <stdio.h>
972 #include <stdlib.h>
973 static void yyerror (const char *);])
974 %}
975 $1
976 %defines
977 %error-verbose
978 %token A 1000
979 %token B
980
981 %%
982 program: /* empty */
983 | program e ';'
984 | program error ';';
985
986 e: e '+' t | t;
987 t: A | B;
988
989 %%
990 AT_LALR1_CC_IF(
991 [/* A C++ error reporting function. */
992 void
993 yy::parser::error (const location&, const std::string& m)
994 {
995 std::cerr << m << std::endl;
996 }
997
998 int
999 yyparse ()
1000 {
1001 yy::parser parser;
1002 return parser.parse ();
1003 }
1004 ],
1005 [static void
1006 yyerror (const char *s)
1007 {
1008 fprintf (stderr, "%s\n", s);
1009 }])
1010
1011 static int
1012 yylex (AT_LALR1_CC_IF([int *lval], [void]))
1013 [{
1014 static int const tokens[] =
1015 {
1016 1000, '+', '+', -1
1017 };
1018 static size_t toknum;
1019 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
1020 if (! (toknum < sizeof tokens / sizeof *tokens))
1021 abort ();
1022 return tokens[toknum++];
1023 }]
1024
1025 int
1026 main (void)
1027 {
1028 return yyparse ();
1029 }
1030 ])
1031 ])# _AT_DATA_EXPECT2_Y
1032
1033
1034 # AT_CHECK_EXPECT2(BISON-OPTIONS)
1035 # ------------------------------
1036 # Generate the grammar, compile it, run it.
1037 m4_define([AT_CHECK_EXPECT2],
1038 [AT_SETUP([Expecting two tokens $1])
1039 AT_BISON_OPTION_PUSHDEFS([$1])
1040 _AT_DATA_EXPECT2_Y([$1])
1041 AT_BISON_CHECK([-o expect2.c expect2.y])
1042 AT_LALR1_CC_IF(
1043 [AT_BISON_CHECK([-o expect2.cc expect2.y])
1044 AT_COMPILE_CXX([expect2])],
1045 [AT_BISON_CHECK([-o expect2.c expect2.y])
1046 AT_COMPILE([expect2])])
1047 AT_PARSER_CHECK([./expect2], 1, [],
1048 [syntax error, unexpected '+', expecting A or B
1049 ])
1050 AT_BISON_OPTION_POPDEFS
1051 AT_CLEANUP
1052 ])
1053
1054 AT_CHECK_EXPECT2()
1055 AT_CHECK_EXPECT2([%glr-parser])
1056 AT_CHECK_EXPECT2([%skeleton "lalr1.cc"])
1057
1058
1059
1060 ## --------------------------------------------- ##
1061 ## Braced code in declaration in rules section. ##
1062 ## --------------------------------------------- ##
1063
1064 AT_SETUP([Braced code in declaration in rules section])
1065
1066 # Bison once mistook braced code in a declaration in the rules section to be a
1067 # rule action.
1068
1069 AT_DATA_GRAMMAR([input.y],
1070 [[%{
1071 #include <stdio.h>
1072 static void yyerror (char const *msg);
1073 static int yylex (void);
1074 %}
1075
1076 %error-verbose
1077
1078 %%
1079
1080 start:
1081 {
1082 printf ("Bison would once convert this action to a midrule because of the"
1083 " subsequent braced code.\n");
1084 }
1085 ;
1086
1087 %destructor { fprintf (stderr, "DESTRUCTOR\n"); } 'a';
1088 %printer { fprintf (yyoutput, "PRINTER"); } 'a';
1089
1090 %%
1091
1092 static void
1093 yyerror (char const *msg)
1094 {
1095 fprintf (stderr, "%s\n", msg);
1096 }
1097
1098 static int
1099 yylex (void)
1100 {
1101 return 'a';
1102 }
1103
1104 int
1105 main (void)
1106 {
1107 yydebug = 1;
1108 return !yyparse ();
1109 }
1110 ]])
1111
1112 AT_BISON_CHECK([-t -o input.c input.y])
1113 AT_COMPILE([input])
1114 AT_PARSER_CHECK([./input], 0,
1115 [[Bison would once convert this action to a midrule because of the subsequent braced code.
1116 ]],
1117 [[Starting parse
1118 Entering state 0
1119 Reducing stack by rule 1 (line 20):
1120 -> $$ = nterm start ()
1121 Stack now 0
1122 Entering state 1
1123 Reading a token: Next token is token 'a' (PRINTER)
1124 syntax error, unexpected 'a', expecting $end
1125 Error: popping nterm start ()
1126 Stack now 0
1127 Cleanup: discarding lookahead token 'a' (PRINTER)
1128 DESTRUCTOR
1129 Stack now 0
1130 ]])
1131
1132 AT_CLEANUP
1133
1134
1135
1136 ## --------------------------------- ##
1137 ## String alias declared after use. ##
1138 ## --------------------------------- ##
1139
1140 AT_SETUP([String alias declared after use])
1141
1142 # Bison once incorrectly asserted that the symbol number for either a token or
1143 # its alias was the highest symbol number so far at the point of the alias
1144 # declaration. That was true unless the declaration appeared after their first
1145 # uses and other tokens appeared in between.
1146
1147 AT_DATA([input.y],
1148 [[%%
1149 start: 'a' "A" 'b';
1150 %token 'a' "A";
1151 ]])
1152
1153 AT_BISON_CHECK([-t -o input.c input.y])
1154
1155 AT_CLEANUP
1156
1157
1158
1159 ## -------------------------------- ##
1160 ## Extra lookahead sets in report. ##
1161 ## -------------------------------- ##
1162
1163 AT_SETUP([[Extra lookahead sets in report]])
1164
1165 # Bison prints each reduction's lookahead set only next to the associated
1166 # state's one item that (1) is associated with the same rule as the reduction
1167 # and (2) has its dot at the end of its RHS. Previously, Bison also
1168 # erroneously printed the lookahead set next to all of the state's other items
1169 # associated with the same rule. This bug affected only the `.output' file and
1170 # not the generated parser source code.
1171
1172 AT_DATA([[input.y]],
1173 [[%%
1174 start: a | 'a' a 'a' ;
1175 a: 'a' ;
1176 ]])
1177
1178 AT_BISON_CHECK([[--report=all input.y]])
1179 AT_CHECK([[sed -n '/^state 1$/,/^state 2$/p' input.output]], [[0]],
1180 [[state 1
1181
1182 2 start: 'a' . a 'a'
1183 3 a: . 'a'
1184 3 | 'a' . [$end]
1185
1186 'a' shift, and go to state 4
1187
1188 $default reduce using rule 3 (a)
1189
1190 a go to state 5
1191
1192
1193 state 2
1194 ]])
1195
1196 AT_CLEANUP
1197
1198
1199
1200 ## ---------------------------------------- ##
1201 ## Token number in precedence declaration. ##
1202 ## ---------------------------------------- ##
1203
1204 AT_SETUP([[Token number in precedence declaration.]])
1205
1206 # POSIX says token numbers can be declared in %left, %right, and %nonassoc, but
1207 # we lost this in Bison 1.50.
1208
1209 AT_DATA_GRAMMAR([input.y],
1210 [[%{
1211 #include <stdio.h>
1212 void yyerror (char const *);
1213 int yylex (void);
1214 %}
1215
1216 %error-verbose
1217 %left TK1 1 TK2 2 "tok alias" 3
1218
1219 %%
1220
1221 start: TK1 sr_conflict "tok alias" ;
1222
1223 sr_conflict:
1224 TK2
1225 | TK2 "tok alias"
1226 ;
1227
1228 %%
1229
1230 void
1231 yyerror (char const *msg)
1232 {
1233 fprintf (stderr, "%s\n", msg);
1234 }
1235
1236 int
1237 yylex (void)
1238 {
1239 static int const input[] = { 1, 2, 3, 0 };
1240 static int const *inputp = input;
1241 return *inputp++;
1242 }
1243
1244 int
1245 main (void)
1246 {
1247 return yyparse ();
1248 }
1249 ]])
1250
1251 AT_BISON_CHECK([[-o input.c input.y]], [[0]],,
1252 [[input.y:24.5-19: warning: rule useless in parser due to conflicts: sr_conflict: TK2 "tok alias"
1253 ]])
1254 AT_COMPILE([[input]])
1255 AT_PARSER_CHECK([[./input]])
1256
1257 AT_CLEANUP