]> git.saurik.com Git - bison.git/blob - tests/regression.at
Regenerate.
[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 ]])
402
403 AT_CLEANUP
404
405
406 AT_SETUP([Invalid inputs with {}])
407
408 AT_DATA([input.y],
409 [[
410 %destructor
411 %initial-action
412 %lex-param
413 %parse-param
414 %printer
415 %union
416 ]])
417
418 AT_CHECK([bison input.y], [1], [],
419 [[input.y:3.1-15: syntax error, unexpected %initial-action, expecting {...}
420 ]])
421
422 AT_CLEANUP
423
424
425
426 ## ------------------- ##
427 ## Token definitions. ##
428 ## ------------------- ##
429
430
431 AT_SETUP([Token definitions])
432
433 # Bison managed, when fed with `%token 'f' "f"' to #define 'f'!
434 AT_DATA_GRAMMAR([input.y],
435 [%{
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 %%
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 return SPECIAL;
460 }
461
462 int
463 main (void)
464 {
465 return yyparse ();
466 }
467 ]])
468
469 AT_CHECK([bison -o input.c input.y])
470 AT_COMPILE([input])
471 AT_DATA([experr],
472 [[syntax error, unexpected "\\'?\"\a\b\f\n\r\t\v\001\201\001\201?\?!", expecting a
473 ]])
474 AT_PARSER_CHECK([./input], 1, [], [experr])
475 AT_CLEANUP
476
477
478
479 ## -------------------- ##
480 ## Characters Escapes. ##
481 ## -------------------- ##
482
483
484 AT_SETUP([Characters Escapes])
485
486 AT_DATA_GRAMMAR([input.y],
487 [%{
488 void yyerror (const char *s);
489 int yylex (void);
490 %}
491 [%%
492 exp:
493 '\'' "\'"
494 | '\"' "\""
495 | '"' "'"
496 ;
497 ]])
498 # Pacify font-lock-mode: "
499
500 AT_CHECK([bison -o input.c input.y])
501 AT_COMPILE([input.o], [-c input.c])
502 AT_CLEANUP
503
504
505
506 ## -------------- ##
507 ## Web2c Report. ##
508 ## -------------- ##
509
510 # The generation of the reduction was once wrong in Bison, and made it
511 # miss some reductions. In the following test case, the reduction on
512 # `undef_id_tok' in state 1 was missing. This is stripped down from
513 # the actual web2c.y.
514
515 AT_SETUP([Web2c Report])
516
517 AT_KEYWORDS([report])
518
519 AT_DATA([input.y],
520 [[%token undef_id_tok const_id_tok
521
522 %start CONST_DEC_PART
523 \f
524 %%
525 CONST_DEC_PART:
526 CONST_DEC_LIST
527 ;
528
529 CONST_DEC_LIST:
530 CONST_DEC
531 | CONST_DEC_LIST CONST_DEC
532 ;
533
534 CONST_DEC:
535 { } undef_id_tok '=' const_id_tok ';'
536 ;
537 %%
538 ]])
539
540 AT_CHECK([bison -v input.y])
541 AT_CHECK([cat input.output], 0,
542 [[Grammar
543
544 0 $accept: CONST_DEC_PART $end
545
546 1 CONST_DEC_PART: CONST_DEC_LIST
547
548 2 CONST_DEC_LIST: CONST_DEC
549 3 | CONST_DEC_LIST CONST_DEC
550
551 4 @1: /* empty */
552
553 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';'
554
555
556 Terminals, with rules where they appear
557
558 $end (0) 0
559 ';' (59) 5
560 '=' (61) 5
561 error (256)
562 undef_id_tok (258) 5
563 const_id_tok (259) 5
564
565
566 Nonterminals, with rules where they appear
567
568 $accept (7)
569 on left: 0
570 CONST_DEC_PART (8)
571 on left: 1, on right: 0
572 CONST_DEC_LIST (9)
573 on left: 2 3, on right: 1 3
574 CONST_DEC (10)
575 on left: 5, on right: 2 3
576 @1 (11)
577 on left: 4, on right: 5
578
579
580 state 0
581
582 0 $accept: . CONST_DEC_PART $end
583
584 $default reduce using rule 4 (@1)
585
586 CONST_DEC_PART go to state 1
587 CONST_DEC_LIST go to state 2
588 CONST_DEC go to state 3
589 @1 go to state 4
590
591
592 state 1
593
594 0 $accept: CONST_DEC_PART . $end
595
596 $end shift, and go to state 5
597
598
599 state 2
600
601 1 CONST_DEC_PART: CONST_DEC_LIST .
602 3 CONST_DEC_LIST: CONST_DEC_LIST . CONST_DEC
603
604 undef_id_tok reduce using rule 4 (@1)
605 $default reduce using rule 1 (CONST_DEC_PART)
606
607 CONST_DEC go to state 6
608 @1 go to state 4
609
610
611 state 3
612
613 2 CONST_DEC_LIST: CONST_DEC .
614
615 $default reduce using rule 2 (CONST_DEC_LIST)
616
617
618 state 4
619
620 5 CONST_DEC: @1 . undef_id_tok '=' const_id_tok ';'
621
622 undef_id_tok shift, and go to state 7
623
624
625 state 5
626
627 0 $accept: CONST_DEC_PART $end .
628
629 $default accept
630
631
632 state 6
633
634 3 CONST_DEC_LIST: CONST_DEC_LIST CONST_DEC .
635
636 $default reduce using rule 3 (CONST_DEC_LIST)
637
638
639 state 7
640
641 5 CONST_DEC: @1 undef_id_tok . '=' const_id_tok ';'
642
643 '=' shift, and go to state 8
644
645
646 state 8
647
648 5 CONST_DEC: @1 undef_id_tok '=' . const_id_tok ';'
649
650 const_id_tok shift, and go to state 9
651
652
653 state 9
654
655 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok . ';'
656
657 ';' shift, and go to state 10
658
659
660 state 10
661
662 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';' .
663
664 $default reduce using rule 5 (CONST_DEC)
665 ]])
666
667 AT_CLEANUP
668
669
670 ## --------------- ##
671 ## Web2c Actions. ##
672 ## --------------- ##
673
674 # The generation of the mapping `state -> action' was once wrong in
675 # extremely specific situations. web2c.y exhibits this situation.
676 # Below is a stripped version of the grammar. It looks like one can
677 # simplify it further, but just don't: it is tuned to exhibit a bug,
678 # which disapears when applying sane grammar transformations.
679 #
680 # It used to be wrong on yydefact only:
681 #
682 # static const yytype_uint8 yydefact[] =
683 # {
684 # - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
685 # + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4,
686 # 0, 0
687 # };
688 #
689 # but let's check all the tables.
690
691
692 AT_SETUP([Web2c Actions])
693
694 AT_KEYWORDS([report])
695
696 AT_DATA([input.y],
697 [[%%
698 statement: struct_stat;
699 struct_stat: /* empty. */ | if else;
700 if: "if" "const" "then" statement;
701 else: "else" statement;
702 %%
703 ]])
704
705 AT_CHECK([bison -v -o input.c input.y])
706
707 # Check only the tables. We don't use --no-parser, because it is
708 # still to be implemented in the experimental branch of Bison.
709 [sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c >tables.c]
710
711 AT_CHECK([[cat tables.c]], 0,
712 [[static const yytype_uint8 yytranslate[] =
713 {
714 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
715 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
716 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
717 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
718 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
719 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
720 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
721 2, 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, 1, 2, 3, 4,
740 5, 6
741 };
742 static const yytype_uint8 yyprhs[] =
743 {
744 0, 0, 3, 5, 6, 9, 14
745 };
746 static const yytype_int8 yyrhs[] =
747 {
748 8, 0, -1, 9, -1, -1, 10, 11, -1, 3,
749 4, 5, 8, -1, 6, 8, -1
750 };
751 static const yytype_uint8 yyrline[] =
752 {
753 0, 2, 2, 3, 3, 4, 5
754 };
755 static const char *const yytname[] =
756 {
757 "$end", "error", "$undefined", "\"if\"", "\"const\"", "\"then\"",
758 "\"else\"", "$accept", "statement", "struct_stat", "if", "else", 0
759 };
760 static const yytype_uint16 yytoknum[] =
761 {
762 0, 256, 257, 258, 259, 260, 261
763 };
764 static const yytype_uint8 yyr1[] =
765 {
766 0, 7, 8, 9, 9, 10, 11
767 };
768 static const yytype_uint8 yyr2[] =
769 {
770 0, 2, 1, 0, 2, 4, 2
771 };
772 static const yytype_uint8 yydefact[] =
773 {
774 3, 0, 0, 2, 0, 0, 1, 3, 4, 3,
775 6, 5
776 };
777 static const yytype_int8 yydefgoto[] =
778 {
779 -1, 2, 3, 4, 8
780 };
781 static const yytype_int8 yypact[] =
782 {
783 -2, -1, 4, -8, 0, 2, -8, -2, -8, -2,
784 -8, -8
785 };
786 static const yytype_int8 yypgoto[] =
787 {
788 -8, -7, -8, -8, -8
789 };
790 static const yytype_uint8 yytable[] =
791 {
792 10, 1, 11, 5, 6, 0, 7, 9
793 };
794 static const yytype_int8 yycheck[] =
795 {
796 7, 3, 9, 4, 0, -1, 6, 5
797 };
798 static const yytype_uint8 yystos[] =
799 {
800 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
801 8, 8
802 };
803 ]])
804
805 AT_CLEANUP
806
807
808 ## ------------------------- ##
809 ## yycheck Bound Violation. ##
810 ## ------------------------- ##
811
812
813 # _AT_DATA_DANCER_Y(BISON-OPTIONS)
814 # --------------------------------
815 # The following grammar, taken from Andrew Suffield's GPL'd implementation
816 # of DGMTP, the Dancer Generic Message Transport Protocol, used to violate
817 # yycheck's bounds where issuing a verbose error message. Keep this test
818 # so that possible bound checking compilers could check all the skeletons.
819 m4_define([_AT_DATA_DANCER_Y],
820 [AT_DATA_GRAMMAR([dancer.y],
821 [%{
822 static int yylex (AT_LALR1_CC_IF([int *], [void]));
823 AT_LALR1_CC_IF([],
824 [#include <stdio.h>
825 static void yyerror (const char *);])
826 %}
827 $1
828 %token ARROW INVALID NUMBER STRING DATA
829 %defines
830 %verbose
831 %error-verbose
832 /* Grammar follows */
833 %%
834 line: header body
835 ;
836
837 header: '<' from ARROW to '>' type ':'
838 | '<' ARROW to '>' type ':'
839 | ARROW to type ':'
840 | type ':'
841 | '<' '>'
842 ;
843
844 from: DATA
845 | STRING
846 | INVALID
847 ;
848
849 to: DATA
850 | STRING
851 | INVALID
852 ;
853
854 type: DATA
855 | STRING
856 | INVALID
857 ;
858
859 body: /* empty */
860 | body member
861 ;
862
863 member: STRING
864 | DATA
865 | '+' NUMBER
866 | '-' NUMBER
867 | NUMBER
868 | INVALID
869 ;
870 %%
871 AT_LALR1_CC_IF(
872 [/* A C++ error reporting function. */
873 void
874 yy::parser::error (const location&, const std::string& m)
875 {
876 std::cerr << m << std::endl;
877 }
878
879 int
880 yyparse ()
881 {
882 yy::parser parser;
883 parser.set_debug_level (!!YYDEBUG);
884 return parser.parse ();
885 }
886 ],
887 [static void
888 yyerror (const char *s)
889 {
890 fprintf (stderr, "%s\n", s);
891 }])
892
893 static int
894 yylex (AT_LALR1_CC_IF([int *lval], [void]))
895 [{
896 static int toknum = 0;
897 static int tokens[] =
898 {
899 ':', -1
900 };
901 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
902 return tokens[toknum++];
903 }]
904
905 int
906 main (void)
907 {
908 return yyparse ();
909 }
910 ])
911 ])# _AT_DATA_DANCER_Y
912
913
914 # AT_CHECK_DANCER(BISON-OPTIONS)
915 # ------------------------------
916 # Generate the grammar, compile it, run it.
917 m4_define([AT_CHECK_DANCER],
918 [AT_SETUP([Dancer $1])
919 AT_BISON_OPTION_PUSHDEFS([$1])
920 _AT_DATA_DANCER_Y([$1])
921 AT_CHECK([bison -o dancer.c dancer.y])
922 AT_LALR1_CC_IF(
923 [AT_CHECK([bison -o dancer.cc dancer.y])
924 AT_COMPILE_CXX([dancer])],
925 [AT_CHECK([bison -o dancer.c dancer.y])
926 AT_COMPILE([dancer])])
927 AT_PARSER_CHECK([./dancer], 1, [],
928 [syntax error, unexpected ':'
929 ])
930 AT_BISON_OPTION_POPDEFS
931 AT_CLEANUP
932 ])
933
934 AT_CHECK_DANCER()
935 AT_CHECK_DANCER([%glr-parser])
936 AT_CHECK_DANCER([%skeleton "lalr1.cc"])
937
938
939 ## ------------------------------------------ ##
940 ## Diagnostic that expects two alternatives. ##
941 ## ------------------------------------------ ##
942
943
944 # _AT_DATA_EXPECT2_Y(BISON-OPTIONS)
945 # --------------------------------
946 m4_define([_AT_DATA_EXPECT2_Y],
947 [AT_DATA_GRAMMAR([expect2.y],
948 [%{
949 static int yylex (AT_LALR1_CC_IF([int *], [void]));
950 AT_LALR1_CC_IF([],
951 [#include <stdio.h>
952 static void yyerror (const char *);])
953 %}
954 $1
955 %defines
956 %error-verbose
957 %token A 1000
958 %token B
959
960 %%
961 program: /* empty */
962 | program e ';'
963 | program error ';';
964
965 e: e '+' t | t;
966 t: A | B;
967
968 %%
969 AT_LALR1_CC_IF(
970 [/* A C++ error reporting function. */
971 void
972 yy::parser::error (const location&, const std::string& m)
973 {
974 std::cerr << m << std::endl;
975 }
976
977 int
978 yyparse ()
979 {
980 yy::parser parser;
981 return parser.parse ();
982 }
983 ],
984 [static void
985 yyerror (const char *s)
986 {
987 fprintf (stderr, "%s\n", s);
988 }])
989
990 static int
991 yylex (AT_LALR1_CC_IF([int *lval], [void]))
992 [{
993 static int toknum = 0;
994 static int tokens[] =
995 {
996 1000, '+', '+', -1
997 };
998 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
999 return tokens[toknum++];
1000 }]
1001
1002 int
1003 main (void)
1004 {
1005 return yyparse ();
1006 }
1007 ])
1008 ])# _AT_DATA_EXPECT2_Y
1009
1010
1011 # AT_CHECK_EXPECT2(BISON-OPTIONS)
1012 # ------------------------------
1013 # Generate the grammar, compile it, run it.
1014 m4_define([AT_CHECK_EXPECT2],
1015 [AT_SETUP([Expecting two tokens $1])
1016 AT_BISON_OPTION_PUSHDEFS([$1])
1017 _AT_DATA_EXPECT2_Y([$1])
1018 AT_CHECK([bison -o expect2.c expect2.y])
1019 AT_LALR1_CC_IF(
1020 [AT_CHECK([bison -o expect2.cc expect2.y])
1021 AT_COMPILE_CXX([expect2])],
1022 [AT_CHECK([bison -o expect2.c expect2.y])
1023 AT_COMPILE([expect2])])
1024 AT_PARSER_CHECK([./expect2], 1, [],
1025 [syntax error, unexpected '+', expecting A or B
1026 ])
1027 AT_BISON_OPTION_POPDEFS
1028 AT_CLEANUP
1029 ])
1030
1031 AT_CHECK_EXPECT2()
1032 AT_CHECK_EXPECT2([%glr-parser])
1033 AT_CHECK_EXPECT2([%skeleton "lalr1.cc"])
1034
1035
1036
1037 ## --------------------------------------------- ##
1038 ## Braced code in declaration in rules section. ##
1039 ## --------------------------------------------- ##
1040
1041 AT_SETUP([Braced code in declaration in rules section])
1042
1043 # Bison once mistook braced code in a declaration in the rules section to be a
1044 # rule action.
1045
1046 AT_DATA_GRAMMAR([input.y],
1047 [[%{
1048 #include <stdio.h>
1049 static void yyerror (char const *msg);
1050 static int yylex (void);
1051 %}
1052
1053 %error-verbose
1054
1055 %%
1056
1057 start:
1058 {
1059 printf ("Bison would once convert this action to a midrule because of the"
1060 " subsequent braced code.\n");
1061 }
1062 ;
1063
1064 %destructor { fprintf (stderr, "DESTRUCTOR\n"); } 'a';
1065 %printer { fprintf (yyoutput, "PRINTER"); } 'a';
1066
1067 %%
1068
1069 static void
1070 yyerror (char const *msg)
1071 {
1072 fprintf (stderr, "%s\n", msg);
1073 }
1074
1075 static int
1076 yylex (void)
1077 {
1078 return 'a';
1079 }
1080
1081 int
1082 main (void)
1083 {
1084 yydebug = 1;
1085 return !yyparse ();
1086 }
1087 ]])
1088
1089 AT_CHECK([bison -t -o input.c input.y])
1090 AT_COMPILE([input])
1091 AT_PARSER_CHECK([./input], 0,
1092 [[Bison would once convert this action to a midrule because of the subsequent braced code.
1093 ]],
1094 [[Starting parse
1095 Entering state 0
1096 Reducing stack by rule 1 (line 22):
1097 -> $$ = nterm start ()
1098 Stack now 0
1099 Entering state 1
1100 Reading a token: Next token is token 'a' (PRINTER)
1101 syntax error, unexpected 'a', expecting $end
1102 Error: popping nterm start ()
1103 Stack now 0
1104 Cleanup: discarding lookahead token 'a' (PRINTER)
1105 DESTRUCTOR
1106 Stack now 0
1107 ]])
1108
1109 AT_CLEANUP