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