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