]> git.saurik.com Git - bison.git/blame_incremental - tests/regression.at
tests: headers.at: strengthen.
[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## Invalid inputs. ##
388## ---------------- ##
389
390
391AT_SETUP([Invalid inputs])
392
393AT_DATA([input.y],
394[[%%
395?
396default: 'a' }
397%&
398%a-does-not-exist
399%-
400%{
401]])
402
403AT_BISON_CHECK([input.y], [1], [],
404[[input.y:2.1: invalid character: '?'
405input.y:3.14: invalid character: '}'
406input.y:4.1: invalid character: '%'
407input.y:4.2: invalid character: '&'
408input.y:5.1-17: invalid directive: '%a-does-not-exist'
409input.y:6.1: invalid character: '%'
410input.y:6.2: invalid character: '-'
411input.y:7.1-8.0: missing '%}' at end of file
412input.y:7.1-8.0: syntax error, unexpected %{...%}
413]])
414
415AT_CLEANUP
416
417
418AT_SETUP([Invalid inputs with {}])
419
420AT_DATA([input.y],
421[[
422%destructor
423%initial-action
424%lex-param
425%parse-param
426%printer
427%union
428]])
429
430AT_BISON_CHECK([input.y], [1], [],
431[[input.y:3.1-15: syntax error, unexpected %initial-action, expecting {...}
432]])
433
434AT_CLEANUP
435
436
437
438## ------------------- ##
439## Token definitions. ##
440## ------------------- ##
441
442
443AT_SETUP([Token definitions])
444
445AT_BISON_OPTION_PUSHDEFS
446# Bison managed, when fed with '%token 'f' "f"' to #define 'f'!
447AT_DATA_GRAMMAR([input.y],
448[%{
449#include <stdlib.h>
450#include <stdio.h>
451]AT_YYERROR_DECLARE[
452]AT_YYLEX_DECLARE[
453%}
454[%error-verbose
455%token MYEOF 0 "end of file"
456%token 'a' "a"
457%token B_TOKEN "b"
458%token C_TOKEN 'c'
459%token 'd' D_TOKEN
460%token SPECIAL "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!"
461%token SPECIAL "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!"
462%%
463exp: "a" "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!";
464%%
465]AT_YYERROR_DEFINE[
466]AT_YYLEX_DEFINE([{ SPECIAL }])[
467
468int
469main (void)
470{
471 return yyparse ();
472}
473]])
474AT_BISON_OPTION_POPDEFS
475
476# Checking the warning message guarantees that the trigraph "??!" isn't
477# unnecessarily escaped here even though it would need to be if encoded in a
478# C-string literal. Also notice that unnecessary escaping, such as "\?", from
479# the user specification is eliminated.
480AT_BISON_CHECK([-o input.c input.y], [[0]], [[]],
481[[input.y:22.8-14: warning: symbol SPECIAL redeclared
482input.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
483]])
484AT_COMPILE([input])
485
486# Checking the error message here guarantees that yytname, which does contain
487# C-string literals, does have the trigraph escaped correctly. Thus, the
488# symbol name reported by the parser is exactly the same as that reported by
489# Bison itself.
490AT_DATA([experr],
491[[syntax error, unexpected "\\'?\"\a\b\f\n\r\t\v\001\201\001\201??!", expecting a
492]])
493AT_PARSER_CHECK([./input], 1, [], [experr])
494AT_CLEANUP
495
496
497
498## -------------------- ##
499## Characters Escapes. ##
500## -------------------- ##
501
502
503AT_SETUP([Characters Escapes])
504
505AT_BISON_OPTION_PUSHDEFS
506AT_DATA_GRAMMAR([input.y],
507[%{
508]AT_YYERROR_DECLARE_EXTERN[
509]AT_YYLEX_DECLARE_EXTERN[
510%}
511[%%
512exp:
513 '\'' "\'"
514| '\"' "\""
515| '"' "'" /* Pacify font-lock-mode: ". */
516;
517]])
518
519AT_BISON_OPTION_POPDEFS
520
521AT_BISON_CHECK([-o input.c input.y])
522AT_COMPILE([input.o])
523AT_CLEANUP
524
525
526
527## -------------- ##
528## Web2c Report. ##
529## -------------- ##
530
531# The generation of the reduction was once wrong in Bison, and made it
532# miss some reductions. In the following test case, the reduction on
533# 'undef_id_tok' in state 1 was missing. This is stripped down from
534# the actual web2c.y.
535
536AT_SETUP([Web2c Report])
537
538AT_KEYWORDS([report])
539
540AT_DATA([input.y],
541[[%token undef_id_tok const_id_tok
542
543%start CONST_DEC_PART
544\f
545%%
546CONST_DEC_PART:
547 CONST_DEC_LIST
548 ;
549
550CONST_DEC_LIST:
551 CONST_DEC
552 | CONST_DEC_LIST CONST_DEC
553 ;
554
555CONST_DEC:
556 { } undef_id_tok '=' const_id_tok ';'
557 ;
558%%
559]])
560
561AT_BISON_CHECK([-v input.y])
562AT_CHECK([cat input.output], 0,
563[[Grammar
564
565 0 $accept: CONST_DEC_PART $end
566
567 1 CONST_DEC_PART: CONST_DEC_LIST
568
569 2 CONST_DEC_LIST: CONST_DEC
570 3 | CONST_DEC_LIST CONST_DEC
571
572 4 $@1: /* empty */
573
574 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok ';'
575
576
577Terminals, with rules where they appear
578
579$end (0) 0
580';' (59) 5
581'=' (61) 5
582error (256)
583undef_id_tok (258) 5
584const_id_tok (259) 5
585
586
587Nonterminals, with rules where they appear
588
589$accept (7)
590 on left: 0
591CONST_DEC_PART (8)
592 on left: 1, on right: 0
593CONST_DEC_LIST (9)
594 on left: 2 3, on right: 1 3
595CONST_DEC (10)
596 on left: 5, on right: 2 3
597$@1 (11)
598 on left: 4, on right: 5
599
600
601state 0
602
603 0 $accept: . CONST_DEC_PART $end
604
605 $default reduce using rule 4 ($@1)
606
607 CONST_DEC_PART go to state 1
608 CONST_DEC_LIST go to state 2
609 CONST_DEC go to state 3
610 $@1 go to state 4
611
612
613state 1
614
615 0 $accept: CONST_DEC_PART . $end
616
617 $end shift, and go to state 5
618
619
620state 2
621
622 1 CONST_DEC_PART: CONST_DEC_LIST .
623 3 CONST_DEC_LIST: CONST_DEC_LIST . CONST_DEC
624
625 undef_id_tok reduce using rule 4 ($@1)
626 $default reduce using rule 1 (CONST_DEC_PART)
627
628 CONST_DEC go to state 6
629 $@1 go to state 4
630
631
632state 3
633
634 2 CONST_DEC_LIST: CONST_DEC .
635
636 $default reduce using rule 2 (CONST_DEC_LIST)
637
638
639state 4
640
641 5 CONST_DEC: $@1 . undef_id_tok '=' const_id_tok ';'
642
643 undef_id_tok shift, and go to state 7
644
645
646state 5
647
648 0 $accept: CONST_DEC_PART $end .
649
650 $default accept
651
652
653state 6
654
655 3 CONST_DEC_LIST: CONST_DEC_LIST CONST_DEC .
656
657 $default reduce using rule 3 (CONST_DEC_LIST)
658
659
660state 7
661
662 5 CONST_DEC: $@1 undef_id_tok . '=' const_id_tok ';'
663
664 '=' shift, and go to state 8
665
666
667state 8
668
669 5 CONST_DEC: $@1 undef_id_tok '=' . const_id_tok ';'
670
671 const_id_tok shift, and go to state 9
672
673
674state 9
675
676 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok . ';'
677
678 ';' shift, and go to state 10
679
680
681state 10
682
683 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok ';' .
684
685 $default reduce using rule 5 (CONST_DEC)
686]])
687
688AT_CLEANUP
689
690
691## --------------- ##
692## Web2c Actions. ##
693## --------------- ##
694
695# The generation of the mapping 'state -> action' was once wrong in
696# extremely specific situations. web2c.y exhibits this situation.
697# Below is a stripped version of the grammar. It looks like one can
698# simplify it further, but just don't: it is tuned to exhibit a bug,
699# which disapears when applying sane grammar transformations.
700#
701# It used to be wrong on yydefact only:
702#
703# static const yytype_uint8 yydefact[] =
704# {
705# - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
706# + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4,
707# 0, 0
708# };
709#
710# but let's check all the tables.
711
712
713AT_SETUP([Web2c Actions])
714
715AT_KEYWORDS([report])
716
717AT_DATA([input.y],
718[[%%
719statement: struct_stat;
720struct_stat: /* empty. */ | if else;
721if: "if" "const" "then" statement;
722else: "else" statement;
723%%
724]])
725
726AT_BISON_CHECK([-v -o input.c input.y])
727
728# Check only the tables.
729[sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c >tables.c]
730
731AT_CHECK([[cat tables.c]], 0,
732[[static const yytype_uint8 yytranslate[] =
733{
734 0, 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, 2, 2, 2, 2,
755 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
756 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
757 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
758 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
759 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
760 5, 6
761};
762static const yytype_uint8 yyprhs[] =
763{
764 0, 0, 3, 5, 6, 9, 14
765};
766static const yytype_int8 yyrhs[] =
767{
768 8, 0, -1, 9, -1, -1, 10, 11, -1, 3,
769 4, 5, 8, -1, 6, 8, -1
770};
771static const yytype_uint8 yyrline[] =
772{
773 0, 2, 2, 3, 3, 4, 5
774};
775static const char *const yytname[] =
776{
777 "$end", "error", "$undefined", "\"if\"", "\"const\"", "\"then\"",
778 "\"else\"", "$accept", "statement", "struct_stat", "if", "else", YY_NULL
779};
780static const yytype_uint16 yytoknum[] =
781{
782 0, 256, 257, 258, 259, 260, 261
783};
784static const yytype_uint8 yyr1[] =
785{
786 0, 7, 8, 9, 9, 10, 11
787};
788static const yytype_uint8 yyr2[] =
789{
790 0, 2, 1, 0, 2, 4, 2
791};
792static const yytype_uint8 yydefact[] =
793{
794 3, 0, 0, 2, 0, 0, 1, 3, 4, 3,
795 6, 5
796};
797static const yytype_int8 yydefgoto[] =
798{
799 -1, 2, 3, 4, 8
800};
801static const yytype_int8 yypact[] =
802{
803 -2, -1, 4, -8, 0, 2, -8, -2, -8, -2,
804 -8, -8
805};
806static const yytype_int8 yypgoto[] =
807{
808 -8, -7, -8, -8, -8
809};
810static const yytype_uint8 yytable[] =
811{
812 10, 1, 11, 5, 6, 0, 7, 9
813};
814static const yytype_int8 yycheck[] =
815{
816 7, 3, 9, 4, 0, -1, 6, 5
817};
818static const yytype_uint8 yystos[] =
819{
820 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
821 8, 8
822};
823]])
824
825AT_CLEANUP
826
827
828## ------------------------- ##
829## yycheck Bound Violation. ##
830## ------------------------- ##
831
832
833# _AT_DATA_DANCER_Y(BISON-OPTIONS)
834# --------------------------------
835# The following grammar, taken from Andrew Suffield's GPL'd implementation
836# of DGMTP, the Dancer Generic Message Transport Protocol, used to violate
837# yycheck's bounds where issuing a verbose error message. Keep this test
838# so that possible bound checking compilers could check all the skeletons.
839m4_define([_AT_DATA_DANCER_Y],
840[AT_DATA_GRAMMAR([dancer.y],
841[[%code provides
842{
843 ]AT_YYERROR_DECLARE[
844 ]AT_YYLEX_DECLARE[
845}
846$1
847%token ARROW INVALID NUMBER STRING DATA
848%defines
849%verbose
850%error-verbose
851/* Grammar follows */
852%%
853line: header body
854 ;
855
856header: '<' from ARROW to '>' type ':'
857 | '<' ARROW to '>' type ':'
858 | ARROW to type ':'
859 | type ':'
860 | '<' '>'
861 ;
862
863from: DATA
864 | STRING
865 | INVALID
866 ;
867
868to: DATA
869 | STRING
870 | INVALID
871 ;
872
873type: DATA
874 | STRING
875 | INVALID
876 ;
877
878body: /* empty */
879 | body member
880 ;
881
882member: STRING
883 | DATA
884 | '+' NUMBER
885 | '-' NUMBER
886 | NUMBER
887 | INVALID
888 ;
889%%
890]AT_YYERROR_DEFINE[
891]AT_YYLEX_DEFINE([":"])[
892]AT_LALR1_CC_IF(
893[int
894yyparse ()
895{
896 yy::parser parser;
897#if YYDEBUG
898 parser.set_debug_level (YYDEBUG);
899#endif
900 return parser.parse ();
901}
902])[
903
904int
905main (void)
906{
907 return yyparse ();
908}
909]])
910])# _AT_DATA_DANCER_Y
911
912
913# AT_CHECK_DANCER(BISON-OPTIONS)
914# ------------------------------
915# Generate the grammar, compile it, run it.
916m4_define([AT_CHECK_DANCER],
917[AT_SETUP([Dancer $1])
918AT_BISON_OPTION_PUSHDEFS([$1])
919_AT_DATA_DANCER_Y([$1])
920AT_BISON_CHECK([-o dancer.c dancer.y])
921AT_FULL_COMPILE([dancer])
922AT_PARSER_CHECK([./dancer], 1, [],
923[syntax error, unexpected ':'
924])
925AT_BISON_OPTION_POPDEFS
926AT_CLEANUP
927])
928
929AT_CHECK_DANCER()
930AT_CHECK_DANCER([%glr-parser])
931AT_CHECK_DANCER([%skeleton "lalr1.cc"])
932
933
934## ------------------------------------------ ##
935## Diagnostic that expects two alternatives. ##
936## ------------------------------------------ ##
937
938
939# _AT_DATA_EXPECT2_Y(BISON-OPTIONS)
940# --------------------------------
941m4_define([_AT_DATA_EXPECT2_Y],
942[AT_DATA_GRAMMAR([expect2.y],
943[[%{
944static int yylex (]AT_LALR1_CC_IF([int *], [void]));
945AT_LALR1_CC_IF([],
946[[#include <stdio.h>
947#include <stdlib.h>
948]AT_YYERROR_DECLARE])[
949%}
950$1
951%defines
952%error-verbose
953%token A 1000
954%token B
955
956%%
957program: /* empty */
958 | program e ';'
959 | program error ';';
960
961e: e '+' t | t;
962t: A | B;
963
964%%
965]AT_YYERROR_DEFINE[
966]AT_LALR1_CC_IF(
967[int
968yyparse ()
969{
970 yy::parser parser;
971 return parser.parse ();
972}
973])[
974
975#include <assert.h>
976static int
977yylex (]AT_LALR1_CC_IF([int *lval], [void])[)
978{
979 static int const tokens[] =
980 {
981 1000, '+', '+', -1
982 };
983 static size_t toknum;
984 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
985 assert (toknum < sizeof tokens / sizeof *tokens);
986 return tokens[toknum++];
987}
988
989int
990main (void)
991{
992 return yyparse ();
993}
994]])
995])# _AT_DATA_EXPECT2_Y
996
997
998# AT_CHECK_EXPECT2(BISON-OPTIONS)
999# -------------------------------
1000# Generate the grammar, compile it, run it.
1001m4_define([AT_CHECK_EXPECT2],
1002[AT_SETUP([Expecting two tokens $1])
1003AT_BISON_OPTION_PUSHDEFS([$1])
1004_AT_DATA_EXPECT2_Y([$1])
1005AT_BISON_CHECK([-o expect2.c expect2.y])
1006AT_FULL_COMPILE([expect2])
1007AT_PARSER_CHECK([./expect2], 1, [],
1008[syntax error, unexpected '+', expecting A or B
1009])
1010AT_BISON_OPTION_POPDEFS
1011AT_CLEANUP
1012])
1013
1014AT_CHECK_EXPECT2()
1015AT_CHECK_EXPECT2([%glr-parser])
1016AT_CHECK_EXPECT2([%skeleton "lalr1.cc"])
1017
1018
1019
1020## --------------------------------------------- ##
1021## Braced code in declaration in rules section. ##
1022## --------------------------------------------- ##
1023
1024AT_SETUP([Braced code in declaration in rules section])
1025
1026# Bison once mistook braced code in a declaration in the rules section to be a
1027# rule action.
1028AT_BISON_OPTION_PUSHDEFS
1029AT_DATA_GRAMMAR([input.y],
1030[[%{
1031#include <stdio.h>
1032]AT_YYERROR_DECLARE[
1033]AT_YYLEX_DECLARE[
1034%}
1035
1036%error-verbose
1037
1038%%
1039
1040start:
1041 {
1042 printf ("Bison would once convert this action to a midrule because of the"
1043 " subsequent braced code.\n");
1044 }
1045 ;
1046
1047%destructor { fprintf (stderr, "DESTRUCTOR\n"); } 'a';
1048%printer { fprintf (yyoutput, "PRINTER"); } 'a';
1049
1050%%
1051]AT_YYERROR_DEFINE[
1052]AT_YYLEX_DEFINE(["a"])[
1053
1054int
1055main (void)
1056{
1057 yydebug = 1;
1058 return !yyparse ();
1059}
1060]])
1061AT_BISON_OPTION_POPDEFS
1062
1063AT_BISON_CHECK([-t -o input.c input.y])
1064AT_COMPILE([input])
1065AT_PARSER_CHECK([./input], 0,
1066[[Bison would once convert this action to a midrule because of the subsequent braced code.
1067]],
1068[[Starting parse
1069Entering state 0
1070Reducing stack by rule 1 (line 20):
1071-> $$ = nterm start ()
1072Stack now 0
1073Entering state 1
1074Reading a token: Next token is token 'a' (PRINTER)
1075syntax error, unexpected 'a', expecting $end
1076Error: popping nterm start ()
1077Stack now 0
1078Cleanup: discarding lookahead token 'a' (PRINTER)
1079DESTRUCTOR
1080Stack now 0
1081]])
1082
1083AT_CLEANUP
1084
1085
1086
1087## --------------------------------- ##
1088## String alias declared after use. ##
1089## --------------------------------- ##
1090
1091AT_SETUP([String alias declared after use])
1092
1093# Bison once incorrectly asserted that the symbol number for either a token or
1094# its alias was the highest symbol number so far at the point of the alias
1095# declaration. That was true unless the declaration appeared after their first
1096# uses and other tokens appeared in between.
1097
1098AT_DATA([input.y],
1099[[%%
1100start: 'a' "A" 'b';
1101%token 'a' "A";
1102]])
1103
1104AT_BISON_CHECK([-t -o input.c input.y])
1105
1106AT_CLEANUP
1107
1108
1109
1110## -------------------------------- ##
1111## Extra lookahead sets in report. ##
1112## -------------------------------- ##
1113
1114AT_SETUP([[Extra lookahead sets in report]])
1115
1116# Bison prints each reduction's lookahead set only next to the associated
1117# state's one item that (1) is associated with the same rule as the reduction
1118# and (2) has its dot at the end of its RHS. Previously, Bison also
1119# erroneously printed the lookahead set next to all of the state's other items
1120# associated with the same rule. This bug affected only the '.output' file and
1121# not the generated parser source code.
1122
1123AT_DATA([[input.y]],
1124[[%%
1125start: a | 'a' a 'a' ;
1126a: 'a' ;
1127]])
1128
1129AT_BISON_CHECK([[--report=all input.y]])
1130AT_CHECK([[sed -n '/^state 1$/,/^state 2$/p' input.output]], [[0]],
1131[[state 1
1132
1133 2 start: 'a' . a 'a'
1134 3 a: . 'a'
1135 3 | 'a' . [$end]
1136
1137 'a' shift, and go to state 4
1138
1139 $default reduce using rule 3 (a)
1140
1141 a go to state 5
1142
1143
1144state 2
1145]])
1146
1147AT_CLEANUP
1148
1149
1150
1151## ---------------------------------------- ##
1152## Token number in precedence declaration. ##
1153## ---------------------------------------- ##
1154
1155AT_SETUP([[Token number in precedence declaration]])
1156
1157# POSIX says token numbers can be declared in %left, %right, and %nonassoc, but
1158# we lost this in Bison 1.50.
1159AT_BISON_OPTION_PUSHDEFS
1160AT_DATA_GRAMMAR([input.y],
1161[[%{
1162 #include <stdio.h>
1163 ]AT_YYERROR_DECLARE[
1164 ]AT_YYLEX_DECLARE[
1165%}
1166
1167%error-verbose
1168%right END 0
1169%left TK1 1 TK2 2 "tok alias" 3
1170
1171%%
1172
1173start:
1174 TK1 sr_conflict "tok alias"
1175 | start %prec END
1176 ;
1177sr_conflict:
1178 TK2
1179 | TK2 "tok alias"
1180 ;
1181
1182%%
1183
1184]AT_YYERROR_DEFINE[
1185]AT_YYLEX_DEFINE([{ 1, 2, 3, 0 }])[
1186
1187int
1188main (void)
1189{
1190 return yyparse ();
1191}
1192]])
1193AT_BISON_OPTION_POPDEFS
1194
1195AT_BISON_CHECK([[-o input.c input.y]], [[0]],,
1196[[input.y:23.5-19: warning: rule useless in parser due to conflicts: start: start
1197input.y:27.5-19: warning: rule useless in parser due to conflicts: sr_conflict: TK2 "tok alias"
1198]])
1199AT_COMPILE([[input]])
1200AT_PARSER_CHECK([[./input]])
1201
1202AT_CLEANUP
1203
1204
1205
1206## --------------------------- ##
1207## parse-gram.y: LALR = IELR. ##
1208## --------------------------- ##
1209
1210# If parse-gram.y's LALR and IELR parser tables ever begin to differ, we
1211# need to fix parse-gram.y or start using IELR.
1212
1213AT_SETUP([[parse-gram.y: LALR = IELR]])
1214
1215# Avoid differences in synclines by telling bison that the output files
1216# have the same name.
1217[cp $abs_top_srcdir/src/parse-gram.y input.y]
1218AT_BISON_CHECK([[-o input.c -Dlr.type=lalr input.y]])
1219[mv input.c lalr.c]
1220AT_CAPTURE_FILE([lalr.c])
1221AT_BISON_CHECK([[-o input.c -Dlr.type=ielr input.y]])
1222[mv input.c ielr.c]
1223AT_CAPTURE_FILE([ielr.c])
1224AT_CHECK([[diff lalr.c ielr.c]], [[0]])
1225
1226AT_CLEANUP
1227
1228
1229
1230## --------------------------------------- ##
1231## %error-verbose and YYSTACK_USE_ALLOCA. ##
1232## --------------------------------------- ##
1233
1234AT_SETUP([[%error-verbose and YYSTACK_USE_ALLOCA]])
1235
1236AT_BISON_OPTION_PUSHDEFS
1237AT_DATA_GRAMMAR([input.y],
1238[[%code {
1239 #include <stdio.h>
1240 ]AT_YYERROR_DECLARE[
1241 ]AT_YYLEX_DECLARE[
1242 #define YYSTACK_USE_ALLOCA 1
1243}
1244
1245%error-verbose
1246
1247%%
1248
1249start: check syntax_error syntax_error ;
1250
1251check:
1252{
1253 if (128 < sizeof yymsgbuf)
1254 {
1255 fprintf (stderr,
1256 "The initial size of yymsgbuf in yyparse has increased\n"
1257 "since this test group was last updated. As a result,\n"
1258 "this test group may no longer manage to induce a\n"
1259 "reallocation of the syntax error message buffer.\n"
1260 "This test group must be adjusted to produce a longer\n"
1261 "error message.\n");
1262 YYABORT;
1263 }
1264}
1265;
1266
1267// Induce a syntax error message whose total length is more than
1268// sizeof yymsgbuf in yyparse. Each token here is 64 bytes.
1269syntax_error:
1270 "123456789112345678921234567893123456789412345678951234567896123A"
1271| "123456789112345678921234567893123456789412345678951234567896123B"
1272| error 'a' 'b' 'c'
1273;
1274
1275%%
1276
1277]AT_YYERROR_DEFINE[
1278/* Induce two syntax error messages (which requires full error
1279 recovery by shifting 3 tokens) in order to detect any loss of the
1280 reallocated buffer. */
1281]AT_YYLEX_DEFINE(["abc"])[
1282int
1283main (void)
1284{
1285 return yyparse ();
1286}
1287]])
1288AT_BISON_OPTION_POPDEFS
1289
1290AT_BISON_CHECK([[-o input.c input.y]])
1291AT_COMPILE([[input]])
1292AT_PARSER_CHECK([[./input]], [[1]], [],
1293[[syntax error, unexpected 'a', expecting 123456789112345678921234567893123456789412345678951234567896123A or 123456789112345678921234567893123456789412345678951234567896123B
1294syntax error, unexpected $end, expecting 123456789112345678921234567893123456789412345678951234567896123A or 123456789112345678921234567893123456789412345678951234567896123B
1295]])
1296
1297AT_CLEANUP
1298
1299
1300
1301## ------------------------- ##
1302## %error-verbose overflow. ##
1303## ------------------------- ##
1304
1305# Imagine the case where YYSTACK_ALLOC_MAXIMUM = YYSIZE_MAXIMUM and an
1306# invocation of yysyntax_error has caused yymsg_alloc to grow to exactly
1307# YYSTACK_ALLOC_MAXIMUM (perhaps because the normal doubling of size had
1308# to be clipped to YYSTACK_ALLOC_MAXIMUM). In an old version of yacc.c,
1309# a subsequent invocation of yysyntax_error that overflows during its
1310# size calculation would return YYSIZE_MAXIMUM to yyparse. Then,
1311# yyparse would invoke yyerror using the old contents of yymsg.
1312
1313AT_SETUP([[%error-verbose overflow]])
1314AT_BISON_OPTION_PUSHDEFS
1315AT_DATA_GRAMMAR([input.y],
1316[[%code {
1317 #include <stdio.h>
1318 ]AT_YYERROR_DECLARE[
1319 ]AT_YYLEX_DECLARE[
1320
1321 /* This prevents this test case from having to induce error messages
1322 large enough to overflow size_t. */
1323 #define YYSIZE_T unsigned char
1324
1325 /* Bring in malloc and set EXIT_SUCCESS so yacc.c doesn't try to
1326 provide a malloc prototype using our YYSIZE_T. */
1327 #include <stdlib.h>
1328 #ifndef EXIT_SUCCESS
1329 # define EXIT_SUCCESS 0
1330 #endif
1331
1332 /* Max depth is usually much smaller than YYSTACK_ALLOC_MAXIMUM, and
1333 we don't want gcc to warn everywhere this constant would be too big
1334 to make sense for our YYSIZE_T. */
1335 #define YYMAXDEPTH 100
1336}
1337
1338%error-verbose
1339
1340%%
1341
1342start: syntax_error1 check syntax_error2 ;
1343
1344// Induce a syntax error message whose total length causes yymsg in
1345// yyparse to be reallocated to size YYSTACK_ALLOC_MAXIMUM, which
1346// should be 255. Each token here is 64 bytes.
1347syntax_error1:
1348 "123456789112345678921234567893123456789412345678951234567896123A"
1349| "123456789112345678921234567893123456789412345678951234567896123B"
1350| "123456789112345678921234567893123456789412345678951234567896123C"
1351| error 'a' 'b' 'c'
1352;
1353
1354check:
1355{
1356 if (yymsg_alloc != YYSTACK_ALLOC_MAXIMUM
1357 || YYSTACK_ALLOC_MAXIMUM != YYSIZE_MAXIMUM
1358 || YYSIZE_MAXIMUM != 255)
1359 {
1360 fprintf (stderr,
1361 "The assumptions of this test group are no longer\n"
1362 "valid, so it may no longer catch the error it was\n"
1363 "designed to catch. Specifically, the following\n"
1364 "values should all be 255:\n\n");
1365 fprintf (stderr, " yymsg_alloc = %d\n", yymsg_alloc);
1366 fprintf (stderr, " YYSTACK_ALLOC_MAXIMUM = %d\n",
1367 YYSTACK_ALLOC_MAXIMUM);
1368 fprintf (stderr, " YYSIZE_MAXIMUM = %d\n", YYSIZE_MAXIMUM);
1369 YYABORT;
1370 }
1371}
1372;
1373
1374// Now overflow.
1375syntax_error2:
1376 "123456789112345678921234567893123456789412345678951234567896123A"
1377| "123456789112345678921234567893123456789412345678951234567896123B"
1378| "123456789112345678921234567893123456789412345678951234567896123C"
1379| "123456789112345678921234567893123456789412345678951234567896123D"
1380| "123456789112345678921234567893123456789412345678951234567896123E"
1381;
1382
1383%%
1384
1385]AT_YYERROR_DEFINE[
1386/* Induce two syntax error messages (which requires full error
1387 recovery by shifting 3 tokens). */
1388]AT_YYLEX_DEFINE(["abc"])[
1389int
1390main (void)
1391{
1392 /* Push parsers throw away the message buffer between tokens, so skip
1393 this test under maintainer-push-check. */
1394 if (YYPUSH)
1395 return 77;
1396 return yyparse ();
1397}
1398]])
1399
1400AT_BISON_CHECK([[-o input.c input.y]])
1401
1402# gcc warns about tautologies and fallacies involving comparisons for
1403# unsigned char. However, it doesn't produce these same warnings for
1404# size_t and many other types when the warnings would seem to make just
1405# as much sense. We ignore the warnings.
1406[CFLAGS="$NO_WERROR_CFLAGS"]
1407AT_COMPILE([[input]])
1408
1409AT_PARSER_CHECK([[./input]], [[2]], [],
1410[[syntax error, unexpected 'a', expecting 123456789112345678921234567893123456789412345678951234567896123A or 123456789112345678921234567893123456789412345678951234567896123B or 123456789112345678921234567893123456789412345678951234567896123C
1411syntax error
1412memory exhausted
1413]])
1414AT_BISON_OPTION_POPDEFS
1415AT_CLEANUP
1416
1417
1418
1419## ------------------------ ##
1420## LAC: Exploratory stack. ##
1421## ------------------------ ##
1422
1423AT_SETUP([[LAC: Exploratory stack]])
1424
1425m4_pushdef([AT_LAC_CHECK], [
1426
1427AT_BISON_OPTION_PUSHDEFS([$1])
1428
1429AT_DATA_GRAMMAR([input.y],
1430[[%code {
1431 #include <stdio.h>
1432 ]AT_YYERROR_DECLARE[
1433 int yylex (]AT_PURE_IF([[YYSTYPE *]], [[void]])[);
1434}
1435
1436]$1[
1437%error-verbose
1438%token 'c'
1439
1440%%
1441
1442// default reductions in inconsistent states
1443// v v v v v v v v v v v v v v
1444S: 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 ;
1445// ^ ^ ^
1446// LAC reallocs
1447
1448A: 'a' | /*empty*/ { printf ("inconsistent default reduction\n"); } ;
1449B: 'b' ;
1450C: /*empty*/ { printf ("consistent default reduction\n"); } ;
1451
1452%%
1453]AT_YYERROR_DEFINE[
1454int
1455yylex (]AT_PURE_IF([[YYSTYPE *v]], [[void]])[)
1456{
1457 static char const *input = "bbbbc";]AT_PURE_IF([[
1458 *v = 0;]])[
1459 return *input++;
1460}
1461
1462int
1463main (void)
1464{
1465 yydebug = 1;
1466 return yyparse ();
1467}
1468]])
1469
1470AT_BISON_CHECK([[-Dparse.lac=full -Dparse.lac.es-capacity-initial=1 \
1471 -Dparse.lac.memory-trace=full \
1472 -t -o input.c input.y]], [[0]], [],
1473[[input.y: conflicts: 21 shift/reduce
1474]])
1475AT_COMPILE([[input]])
1476AT_PARSER_CHECK([[./input > stdout.txt 2> stderr.txt]], [[1]])
1477
1478# Make sure syntax error doesn't forget that 'a' is expected. It would
1479# be forgotten without lookahead correction.
1480AT_CHECK([[grep 'syntax error,' stderr.txt]], [[0]],
1481[[syntax error, unexpected 'c', expecting 'a' or 'b'
1482]])
1483
1484# Check number of default reductions in inconsistent states to be sure
1485# syntax error is detected before unnecessary reductions are performed.
1486AT_CHECK([[perl -0777 -ne 'print s/inconsistent default reduction//g;' \
1487 < stdout.txt || exit 77]], [[0]], [[14]])
1488
1489# Check number of default reductions in consistent states to be sure
1490# it is performed before the syntax error is detected.
1491AT_CHECK([[perl -0777 -ne 'print s/\bconsistent default reduction//g;' \
1492 < stdout.txt || exit 77]], [[0]], [[2]])
1493
1494# Check number of reallocs to be sure reallocated memory isn't somehow
1495# lost between LAC invocations.
1496AT_CHECK([[perl -0777 -ne 'print s/\(realloc//g;' < stderr.txt \
1497 || exit 77]], [[0]], [[3]])
1498
1499AT_BISON_OPTION_POPDEFS
1500])
1501
1502AT_LAC_CHECK([[%define api.push-pull pull]])
1503AT_LAC_CHECK([[%define api.push-pull pull %define api.pure]])
1504AT_LAC_CHECK([[%define api.push-pull both]])
1505AT_LAC_CHECK([[%define api.push-pull both %define api.pure]])
1506
1507m4_popdef([AT_LAC_CHECK])
1508
1509AT_CLEANUP
1510
1511
1512
1513## ------------------------ ##
1514## LAC: Memory exhaustion. ##
1515## ------------------------ ##
1516
1517AT_SETUP([[LAC: Memory exhaustion]])
1518
1519m4_pushdef([AT_LAC_CHECK],
1520[AT_BISON_OPTION_PUSHDEFS
1521AT_DATA_GRAMMAR([input.y],
1522[[%code {
1523 #include <stdio.h>
1524 ]AT_YYERROR_DECLARE[
1525 ]AT_YYLEX_DECLARE[
1526 #define YYMAXDEPTH 8
1527}
1528
1529%error-verbose
1530
1531%%
1532
1533S: A A A A A A A A A ;
1534A: /*empty*/ | 'a' ;
1535
1536%%
1537]AT_YYERROR_DEFINE[
1538]AT_YYLEX_DEFINE(["$1"])[
1539int
1540main (void)
1541{
1542 yydebug = 1;
1543 return yyparse ();
1544}
1545]])
1546
1547AT_BISON_CHECK([[-Dparse.lac=full -Dparse.lac.es-capacity-initial=1 \
1548 -t -o input.c input.y]], [[0]], [],
1549[[input.y: conflicts: 8 shift/reduce
1550]])
1551AT_COMPILE([[input]])
1552AT_BISON_OPTION_POPDEFS
1553])
1554
1555# Check for memory exhaustion during parsing.
1556AT_LAC_CHECK([])
1557AT_PARSER_CHECK([[./input]], [[2]], [],
1558[[Starting parse
1559Entering state 0
1560Reading a token: Now at end of input.
1561LAC: initial context established for $end
1562LAC: checking lookahead $end: R2 G3 R2 G5 R2 G6 R2 G7 R2 G8 R2 G9 R2 G10 R2 G11 R2 (max size exceeded)
1563memory exhausted
1564Cleanup: discarding lookahead token $end ()
1565Stack now 0
1566]])
1567
1568# Induce an immediate syntax error with an undefined token, and check
1569# for memory exhaustion while building syntax error message.
1570AT_LAC_CHECK([z], [[0]])
1571AT_PARSER_CHECK([[./input]], [[2]], [],
1572[[Starting parse
1573Entering state 0
1574Reading a token: Next token is token $undefined ()
1575LAC: initial context established for $undefined
1576LAC: checking lookahead $undefined: Always Err
1577Constructing syntax error message
1578LAC: checking lookahead $end: R2 G3 R2 G5 R2 G6 R2 G7 R2 G8 R2 G9 R2 G10 R2 G11 R2 (max size exceeded)
1579syntax error
1580memory exhausted
1581Cleanup: discarding lookahead token $undefined ()
1582Stack now 0
1583]])
1584
1585m4_popdef([AT_LAC_CHECK])
1586
1587AT_CLEANUP