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