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