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