]> git.saurik.com Git - bison.git/blame - tests/regression.at
maint: xfdopen, and scope reduction.
[bison.git] / tests / regression.at
CommitLineData
342b8b6e 1# Bison Regressions. -*- Autotest -*-
d42cf844 2
34136e65 3# Copyright (C) 2001-2012 Free Software Foundation, Inc.
c95f2d78 4
f16b0819 5# This program is free software: you can redistribute it and/or modify
342b8b6e 6# it under the terms of the GNU General Public License as published by
f16b0819
PE
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
342b8b6e
AD
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.
f16b0819 14#
342b8b6e 15# You should have received a copy of the GNU General Public License
f16b0819 16# along with this program. If not, see <http://www.gnu.org/licenses/>.
c95f2d78 17
342b8b6e 18AT_BANNER([[Regression tests.]])
c95f2d78 19
2b25d624 20
276f48df
PE
21## ------------------ ##
22## Trivial grammars. ##
23## ------------------ ##
24
25AT_SETUP([Trivial grammars])
26
27AT_DATA_GRAMMAR([input.y],
28[[%{
29void yyerror (char const *);
30int yylex (void);
50cce58e 31#define YYSTYPE int *
276f48df
PE
32%}
33
34%error-verbose
35
36%%
37
38program: 'x';
39]])
40
da730230 41AT_BISON_CHECK([-o input.c input.y])
276f48df 42AT_COMPILE([input.o], [-c input.c])
50cce58e 43AT_COMPILE([input.o], [-DYYDEBUG -c input.c])
276f48df
PE
44
45AT_CLEANUP
46
47
48
ddc8ede1
PE
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
da730230 69AT_BISON_CHECK([-o input.c input.y])
ddc8ede1
PE
70AT_COMPILE([input.o], [-c input.c])
71
72AT_CLEANUP
73
74
75
b931235e
JD
76## ------------------------------------- ##
77## Early token definitions with --yacc. ##
78## ------------------------------------- ##
69078d4b
AD
79
80
b931235e 81AT_SETUP([Early token definitions with --yacc])
69078d4b
AD
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
9501dc6e 86AT_DATA_GRAMMAR([input.y],
69078d4b
AD
87[[%{
88void yyerror (const char *s);
89int yylex (void);
90%}
91
92%union
93{
94 int val;
95};
9bc0dd67
JD
96%{
97#ifndef MY_TOKEN
98# error "MY_TOKEN not defined."
99#endif
100%}
b931235e
JD
101%token MY_TOKEN
102%%
103exp: MY_TOKEN;
104%%
105]])
106
da730230 107AT_BISON_CHECK([-y -o input.c input.y])
b931235e
JD
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);
9bc0dd67
JD
130%}
131
132%union
133{
134 int val;
135};
136%{
b931235e
JD
137void
138print_my_token (void)
139{
140 enum yytokentype my_token = MY_TOKEN;
141 printf ("%d\n", my_token);
142}
69078d4b
AD
143%}
144%token MY_TOKEN
145%%
146exp: MY_TOKEN;
147%%
148]])
149
da730230 150AT_BISON_CHECK([-o input.c input.y])
002b9b7d 151AT_COMPILE([input.o], [-c input.c])
69078d4b
AD
152
153AT_CLEANUP
154
155
156
2b25d624
AD
157## ---------------- ##
158## Braces parsing. ##
159## ---------------- ##
160
161
69078d4b 162AT_SETUP([Braces parsing])
2b25d624
AD
163
164AT_DATA([input.y],
ae93e4e4 165[[/* Bison used to swallow the character after '}'. */
2b25d624
AD
166
167%%
bfcf1f3a 168exp: { tests = {{{{{{{{{{}}}}}}}}}}; };
2b25d624
AD
169%%
170]])
171
da730230 172AT_BISON_CHECK([-v -o input.c input.y])
2b25d624 173
a4bf0390 174AT_CHECK([grep 'tests = {{{{{{{{{{}}}}}}}}}};' input.c], 0, [ignore])
2b25d624
AD
175
176AT_CLEANUP
177
178
c95f2d78
AD
179## ------------------ ##
180## Duplicate string. ##
181## ------------------ ##
182
183
184AT_SETUP([Duplicate string])
185
f499b062 186AT_DATA([input.y],
ae93e4e4 187[[/* 'Bison -v' used to dump core when two tokens are defined with the same
c95f2d78
AD
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
da730230 199AT_BISON_CHECK([-v -o input.c input.y], 0, [],
4a9cd8f2 200[[input.y:6.8-14: warning: symbol "<=" used more than once as a literal string
69078d4b 201]])
c95f2d78 202
d803322e 203AT_CLEANUP
c95f2d78
AD
204
205
2ca209c1
AD
206## ------------------- ##
207## Rule Line Numbers. ##
208## ------------------- ##
209
210AT_SETUP([Rule Line Numbers])
211
6b98e4b5
AD
212AT_KEYWORDS([report])
213
2ca209c1
AD
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
bfcf1f3a 241};
2ca209c1
AD
242]])
243
da730230 244AT_BISON_CHECK([-o input.c -v input.y])
2ca209c1
AD
245
246# Check the contents of the report.
247AT_CHECK([cat input.output], [],
d2d1b42b 248[[Grammar
2ca209c1 249
88bce5a2 250 0 $accept: expr $end
6b98e4b5 251
f91b1629 252 1 $@1: /* empty */
6b98e4b5 253
f91b1629 254 2 expr: 'a' $@1 'b'
6b98e4b5 255
f91b1629 256 3 $@2: /* empty */
6b98e4b5 257
f91b1629 258 4 expr: $@2 'c'
2ca209c1 259
d2d1b42b 260
2ca209c1
AD
261Terminals, with rules where they appear
262
88bce5a2 263$end (0) 0
2ca209c1
AD
264'a' (97) 2
265'b' (98) 2
266'c' (99) 4
267error (256)
268
d2d1b42b 269
2ca209c1
AD
270Nonterminals, with rules where they appear
271
88bce5a2 272$accept (6)
b365aa05
AD
273 on left: 0
274expr (7)
275 on left: 2 4, on right: 0
f91b1629 276$@1 (8)
2ca209c1 277 on left: 1, on right: 2
f91b1629 278$@2 (9)
2ca209c1
AD
279 on left: 3, on right: 4
280
281
282state 0
283
88bce5a2 284 0 $accept: . expr $end
643a5994 285
87675353 286 'a' shift, and go to state 1
2ca209c1 287
f91b1629 288 $default reduce using rule 3 ($@2)
2ca209c1 289
87675353 290 expr go to state 2
f91b1629 291 $@2 go to state 3
2ca209c1
AD
292
293
294state 1
295
f91b1629 296 2 expr: 'a' . $@1 'b'
2ca209c1 297
f91b1629 298 $default reduce using rule 1 ($@1)
2ca209c1 299
f91b1629 300 $@1 go to state 4
2ca209c1
AD
301
302
303state 2
304
88bce5a2 305 0 $accept: expr . $end
2ca209c1 306
88bce5a2 307 $end shift, and go to state 5
2ca209c1
AD
308
309
310state 3
311
f91b1629 312 4 expr: $@2 . 'c'
2ca209c1 313
87675353 314 'c' shift, and go to state 6
2ca209c1
AD
315
316
317state 4
318
f91b1629 319 2 expr: 'a' $@1 . 'b'
2ca209c1 320
87675353 321 'b' shift, and go to state 7
2ca209c1
AD
322
323
324state 5
325
88bce5a2 326 0 $accept: expr $end .
2ca209c1 327
e8832397 328 $default accept
2ca209c1
AD
329
330
331state 6
332
f91b1629 333 4 expr: $@2 'c' .
b365aa05 334
87675353 335 $default reduce using rule 4 (expr)
2ca209c1
AD
336
337
338state 7
339
f91b1629 340 2 expr: 'a' $@1 'b' .
b365aa05 341
87675353 342 $default reduce using rule 2 (expr)
2ca209c1
AD
343]])
344
345AT_CLEANUP
346
347
348
cd5aafcf
AD
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
da730230 366AT_BISON_CHECK([-v -o input.c input.y])
cd5aafcf 367
d803322e 368AT_CLEANUP
cd5aafcf
AD
369
370
371
29ae55f1
AD
372## ---------------- ##
373## Invalid inputs. ##
374## ---------------- ##
561f9a30
AD
375
376
29ae55f1 377AT_SETUP([Invalid inputs])
561f9a30
AD
378
379AT_DATA([input.y],
380[[%%
381?
561f9a30 382default: 'a' }
29ae55f1 383%&
2dfbfc12 384%a-does-not-exist
29ae55f1 385%-
e9955c83 386%{
561f9a30
AD
387]])
388
da730230 389AT_BISON_CHECK([input.y], [1], [],
ae93e4e4
JM
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: invalid character: '%'
396input.y:6.2: invalid character: '-'
397input.y:7.1-8.0: missing '%}' at end of file
47aee066 398input.y:7.1-8.0: syntax error, unexpected %{...%}
e0c40012 399]])
561f9a30
AD
400
401AT_CLEANUP
402
403
fc01665e
PE
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
da730230 416AT_BISON_CHECK([input.y], [1], [],
e9071366 417[[input.y:3.1-15: syntax error, unexpected %initial-action, expecting {...}
fc01665e
PE
418]])
419
420AT_CLEANUP
421
422
270a173c 423
b87f8b21
AD
424## ------------------- ##
425## Token definitions. ##
426## ------------------- ##
427
428
429AT_SETUP([Token definitions])
430
ae93e4e4 431# Bison managed, when fed with '%token 'f' "f"' to #define 'f'!
9501dc6e 432AT_DATA_GRAMMAR([input.y],
db7c8e9a 433[%{
cf806753 434#include <stdlib.h>
ca407bdf 435#include <stdio.h>
db7c8e9a
AD
436void yyerror (const char *s);
437int yylex (void);
438%}
ca407bdf
PE
439[%error-verbose
440%token MYEOF 0 "end of file"
b87f8b21 441%token 'a' "a"
4f136612
PE
442%token B_TOKEN "b"
443%token C_TOKEN 'c'
444%token 'd' D_TOKEN
3d54b576 445%token SPECIAL "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!"
1cfe6375 446%token SPECIAL "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!"
b87f8b21 447%%
3d54b576 448exp: "a" "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!";
ca407bdf
PE
449%%
450void
451yyerror (char const *s)
452{
453 fprintf (stderr, "%s\n", s);
454}
455
456int
457yylex (void)
458{
cf806753
PE
459 static int called;
460 if (called++)
461 abort ();
ca407bdf
PE
462 return SPECIAL;
463}
464
465int
466main (void)
467{
468 return yyparse ();
469}
b87f8b21
AD
470]])
471
1cfe6375
JD
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
4a9cd8f2 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
1cfe6375 479]])
ca407bdf 480AT_COMPILE([input])
1cfe6375
JD
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.
3d54b576 486AT_DATA([experr],
1cfe6375 487[[syntax error, unexpected "\\'?\"\a\b\f\n\r\t\v\001\201\001\201??!", expecting a
3d54b576
PE
488]])
489AT_PARSER_CHECK([./input], 1, [], [experr])
b87f8b21
AD
490AT_CLEANUP
491
492
493
eb714592
AD
494## -------------------- ##
495## Characters Escapes. ##
496## -------------------- ##
497
498
499AT_SETUP([Characters Escapes])
500
9501dc6e 501AT_DATA_GRAMMAR([input.y],
eb714592
AD
502[%{
503void yyerror (const char *s);
504int yylex (void);
505%}
6d0ef4ec 506[%%
eb714592
AD
507exp:
508 '\'' "\'"
509| '\"' "\""
510| '"' "'"
511;
512]])
9501dc6e 513# Pacify font-lock-mode: "
eb714592 514
da730230 515AT_BISON_CHECK([-o input.c input.y])
eb714592
AD
516AT_COMPILE([input.o], [-c input.c])
517AT_CLEANUP
518
519
520
b9752825
AD
521## -------------- ##
522## Web2c Report. ##
523## -------------- ##
776209d6
AD
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
ae93e4e4 527# 'undef_id_tok' in state 1 was missing. This is stripped down from
776209d6
AD
528# the actual web2c.y.
529
b9752825 530AT_SETUP([Web2c Report])
776209d6 531
6b98e4b5
AD
532AT_KEYWORDS([report])
533
776209d6 534AT_DATA([input.y],
e9690142 535[[%token undef_id_tok const_id_tok
776209d6
AD
536
537%start CONST_DEC_PART
538\f
539%%
540CONST_DEC_PART:
541 CONST_DEC_LIST
542 ;
543
544CONST_DEC_LIST:
e9690142 545 CONST_DEC
776209d6
AD
546 | CONST_DEC_LIST CONST_DEC
547 ;
548
549CONST_DEC:
e9690142 550 { } undef_id_tok '=' const_id_tok ';'
776209d6
AD
551 ;
552%%
776209d6
AD
553]])
554
da730230 555AT_BISON_CHECK([-v input.y])
87675353 556AT_CHECK([cat input.output], 0,
776209d6 557[[Grammar
87675353 558
88bce5a2 559 0 $accept: CONST_DEC_PART $end
87675353 560
6b98e4b5 561 1 CONST_DEC_PART: CONST_DEC_LIST
87675353 562
6b98e4b5
AD
563 2 CONST_DEC_LIST: CONST_DEC
564 3 | CONST_DEC_LIST CONST_DEC
87675353 565
f91b1629 566 4 $@1: /* empty */
87675353 567
f91b1629 568 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok ';'
87675353
AD
569
570
776209d6 571Terminals, with rules where they appear
87675353 572
88bce5a2 573$end (0) 0
776209d6
AD
574';' (59) 5
575'=' (61) 5
576error (256)
007a50a4
AD
577undef_id_tok (258) 5
578const_id_tok (259) 5
87675353
AD
579
580
776209d6 581Nonterminals, with rules where they appear
87675353 582
88bce5a2 583$accept (7)
78d5bae9
AD
584 on left: 0
585CONST_DEC_PART (8)
586 on left: 1, on right: 0
587CONST_DEC_LIST (9)
776209d6 588 on left: 2 3, on right: 1 3
78d5bae9 589CONST_DEC (10)
776209d6 590 on left: 5, on right: 2 3
f91b1629 591$@1 (11)
776209d6 592 on left: 4, on right: 5
87675353
AD
593
594
776209d6 595state 0
87675353 596
88bce5a2 597 0 $accept: . CONST_DEC_PART $end
87675353 598
f91b1629 599 $default reduce using rule 4 ($@1)
87675353
AD
600
601 CONST_DEC_PART go to state 1
602 CONST_DEC_LIST go to state 2
603 CONST_DEC go to state 3
f91b1629 604 $@1 go to state 4
87675353
AD
605
606
776209d6 607state 1
87675353 608
88bce5a2 609 0 $accept: CONST_DEC_PART . $end
87675353 610
88bce5a2 611 $end shift, and go to state 5
87675353
AD
612
613
78d5bae9 614state 2
87675353 615
ce4ccb4b
AD
616 1 CONST_DEC_PART: CONST_DEC_LIST .
617 3 CONST_DEC_LIST: CONST_DEC_LIST . CONST_DEC
87675353 618
f91b1629 619 undef_id_tok reduce using rule 4 ($@1)
87675353
AD
620 $default reduce using rule 1 (CONST_DEC_PART)
621
622 CONST_DEC go to state 6
f91b1629 623 $@1 go to state 4
87675353
AD
624
625
78d5bae9 626state 3
87675353 627
ce4ccb4b 628 2 CONST_DEC_LIST: CONST_DEC .
87675353
AD
629
630 $default reduce using rule 2 (CONST_DEC_LIST)
631
632
776209d6 633state 4
87675353 634
f91b1629 635 5 CONST_DEC: $@1 . undef_id_tok '=' const_id_tok ';'
87675353
AD
636
637 undef_id_tok shift, and go to state 7
638
639
78d5bae9 640state 5
87675353 641
88bce5a2 642 0 $accept: CONST_DEC_PART $end .
87675353 643
e8832397 644 $default accept
87675353
AD
645
646
78d5bae9 647state 6
87675353 648
ce4ccb4b 649 3 CONST_DEC_LIST: CONST_DEC_LIST CONST_DEC .
87675353
AD
650
651 $default reduce using rule 3 (CONST_DEC_LIST)
652
653
78d5bae9 654state 7
87675353 655
f91b1629 656 5 CONST_DEC: $@1 undef_id_tok . '=' const_id_tok ';'
87675353
AD
657
658 '=' shift, and go to state 8
659
660
78d5bae9 661state 8
87675353 662
f91b1629 663 5 CONST_DEC: $@1 undef_id_tok '=' . const_id_tok ';'
87675353
AD
664
665 const_id_tok shift, and go to state 9
666
667
78d5bae9 668state 9
87675353 669
f91b1629 670 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok . ';'
87675353
AD
671
672 ';' shift, and go to state 10
673
674
78d5bae9 675state 10
87675353 676
f91b1629 677 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok ';' .
87675353
AD
678
679 $default reduce using rule 5 (CONST_DEC)
776209d6
AD
680]])
681
682AT_CLEANUP
b9752825
AD
683
684
685## --------------- ##
686## Web2c Actions. ##
687## --------------- ##
688
ae93e4e4 689# The generation of the mapping 'state -> action' was once wrong in
b9752825
AD
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#
d42cf844 697# static const yytype_uint8 yydefact[] =
b9752825
AD
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
6b98e4b5
AD
709AT_KEYWORDS([report])
710
b9752825
AD
711AT_DATA([input.y],
712[[%%
713statement: struct_stat;
714struct_stat: /* empty. */ | if else;
715if: "if" "const" "then" statement;
716else: "else" statement;
717%%
718]])
719
da730230 720AT_BISON_CHECK([-v -o input.c input.y])
b9752825 721
728c4be2 722# Check only the tables.
ce4ccb4b
AD
723[sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c >tables.c]
724
725AT_CHECK([[cat tables.c]], 0,
d42cf844 726[[static const yytype_uint8 yytranslate[] =
b9752825
AD
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,
007a50a4
AD
753 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
754 5, 6
b9752825 755};
d42cf844 756static const yytype_uint8 yyrline[] =
b9752825 757{
e7b8bef1 758 0, 2, 2, 3, 3, 4, 5
b9752825
AD
759};
760static const char *const yytname[] =
761{
9e0876fb 762 "$end", "error", "$undefined", "\"if\"", "\"const\"", "\"then\"",
ef51bfa7 763 "\"else\"", "$accept", "statement", "struct_stat", "if", "else", YY_NULL
b9752825 764};
d42cf844 765static const yytype_uint16 yytoknum[] =
b9752825 766{
3650b4b8 767 0, 256, 257, 258, 259, 260, 261
b9752825 768};
0991e29b 769static const yytype_int8 yypact[] =
b9752825 770{
0991e29b
AD
771 -2, -1, 4, -8, 0, 2, -8, -2, -8, -2,
772 -8, -8
b9752825 773};
d42cf844 774static const yytype_uint8 yydefact[] =
b9752825 775{
e8832397 776 3, 0, 0, 2, 0, 0, 1, 3, 4, 3,
e7b8bef1 777 6, 5
b9752825 778};
d42cf844 779static const yytype_int8 yypgoto[] =
b9752825 780{
12b0043a 781 -8, -7, -8, -8, -8
b9752825 782};
0991e29b
AD
783static const yytype_int8 yydefgoto[] =
784{
785 -1, 2, 3, 4, 8
786};
d42cf844 787static const yytype_uint8 yytable[] =
b9752825 788{
e7b8bef1 789 10, 1, 11, 5, 6, 0, 7, 9
b9752825 790};
d42cf844 791static const yytype_int8 yycheck[] =
b9752825 792{
e7b8bef1 793 7, 3, 9, 4, 0, -1, 6, 5
b9752825 794};
d42cf844 795static const yytype_uint8 yystos[] =
5504898e
AD
796{
797 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
798 8, 8
799};
0991e29b
AD
800static const yytype_uint8 yyr1[] =
801{
802 0, 7, 8, 9, 9, 10, 11
803};
804static const yytype_uint8 yyr2[] =
805{
806 0, 2, 1, 0, 2, 4, 2
807};
b9752825
AD
808]])
809
810AT_CLEANUP
22e304a6
AD
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[%{
848dc439 827static int yylex (AT_LALR1_CC_IF([int *], [void]));
95611b56 828AT_LALR1_CC_IF([#include <cstdlib>],
cf806753
PE
829[#include <stdlib.h>
830#include <stdio.h>
848dc439 831static void yyerror (const char *);])
22e304a6
AD
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(
68e11668 878[/* A C++ error reporting function. */
22e304a6 879void
2ea7730c 880yy::parser::error (const std::string& m)
22e304a6 881{
efeed023 882 std::cerr << m << std::endl;
22e304a6
AD
883}
884
885int
99880de5 886yyparse ()
22e304a6 887{
99880de5 888 yy::parser parser;
fa7b79c0
PE
889#if YYDEBUG
890 parser.set_debug_level (YYDEBUG);
891#endif
22e304a6
AD
892 return parser.parse ();
893}
894],
895[static void
896yyerror (const char *s)
897{
898 fprintf (stderr, "%s\n", s);
899}])
900
901static int
848dc439 902yylex (AT_LALR1_CC_IF([int *lval], [void]))
22e304a6 903[{
cf806753 904 static int const tokens[] =
22e304a6
AD
905 {
906 ':', -1
907 };
cf806753 908 static size_t toknum;
848dc439 909 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
cf806753
PE
910 if (! (toknum < sizeof tokens / sizeof *tokens))
911 abort ();
22e304a6
AD
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])
da730230 931AT_BISON_CHECK([-o dancer.c dancer.y])
7ca2266a 932AT_FULL_COMPILE([dancer])
22e304a6 933AT_PARSER_CHECK([./dancer], 1, [],
d5286af1 934[syntax error, unexpected ':'
22e304a6
AD
935])
936AT_BISON_OPTION_POPDEFS
937AT_CLEANUP
938])
939
940AT_CHECK_DANCER()
941AT_CHECK_DANCER([%glr-parser])
942AT_CHECK_DANCER([%skeleton "lalr1.cc"])
d6645148
PE
943
944
945## ------------------------------------------ ##
946## Diagnostic that expects two alternatives. ##
947## ------------------------------------------ ##
948
949
950# _AT_DATA_EXPECT2_Y(BISON-OPTIONS)
951# --------------------------------
952m4_define([_AT_DATA_EXPECT2_Y],
953[AT_DATA_GRAMMAR([expect2.y],
954[%{
955static int yylex (AT_LALR1_CC_IF([int *], [void]));
95611b56 956AT_LALR1_CC_IF([#include <cstdlib>],
d6645148 957[#include <stdio.h>
c4bd5bf7 958#include <stdlib.h>
d6645148
PE
959static void yyerror (const char *);])
960%}
961$1
962%defines
963%error-verbose
964%token A 1000
965%token B
966
967%%
968program: /* empty */
969 | program e ';'
970 | program error ';';
971
972e: e '+' t | t;
973t: A | B;
974
975%%
976AT_LALR1_CC_IF(
977[/* A C++ error reporting function. */
978void
2ea7730c 979yy::parser::error (const std::string& m)
d6645148
PE
980{
981 std::cerr << m << std::endl;
982}
983
984int
985yyparse ()
986{
987 yy::parser parser;
988 return parser.parse ();
989}
990],
991[static void
992yyerror (const char *s)
993{
994 fprintf (stderr, "%s\n", s);
995}])
996
997static int
998yylex (AT_LALR1_CC_IF([int *lval], [void]))
999[{
cf806753 1000 static int const tokens[] =
d6645148
PE
1001 {
1002 1000, '+', '+', -1
1003 };
cf806753 1004 static size_t toknum;
d6645148 1005 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
cf806753
PE
1006 if (! (toknum < sizeof tokens / sizeof *tokens))
1007 abort ();
d6645148
PE
1008 return tokens[toknum++];
1009}]
1010
1011int
1012main (void)
1013{
1014 return yyparse ();
1015}
1016])
1017])# _AT_DATA_EXPECT2_Y
1018
1019
1020# AT_CHECK_EXPECT2(BISON-OPTIONS)
1021# ------------------------------
1022# Generate the grammar, compile it, run it.
1023m4_define([AT_CHECK_EXPECT2],
1024[AT_SETUP([Expecting two tokens $1])
1025AT_BISON_OPTION_PUSHDEFS([$1])
1026_AT_DATA_EXPECT2_Y([$1])
da730230 1027AT_BISON_CHECK([-o expect2.c expect2.y])
7ca2266a 1028AT_FULL_COMPILE([expect2])
d6645148
PE
1029AT_PARSER_CHECK([./expect2], 1, [],
1030[syntax error, unexpected '+', expecting A or B
1031])
1032AT_BISON_OPTION_POPDEFS
1033AT_CLEANUP
1034])
1035
1036AT_CHECK_EXPECT2()
1037AT_CHECK_EXPECT2([%glr-parser])
1038AT_CHECK_EXPECT2([%skeleton "lalr1.cc"])
4210cd0b
JD
1039
1040
1041
1042## --------------------------------------------- ##
1043## Braced code in declaration in rules section. ##
1044## --------------------------------------------- ##
1045
1046AT_SETUP([Braced code in declaration in rules section])
1047
1048# Bison once mistook braced code in a declaration in the rules section to be a
1049# rule action.
1050
1051AT_DATA_GRAMMAR([input.y],
1052[[%{
1053#include <stdio.h>
381ecb06
JD
1054static void yyerror (char const *msg);
1055static int yylex (void);
4210cd0b
JD
1056%}
1057
1058%error-verbose
1059
1060%%
1061
1062start:
1063 {
1064 printf ("Bison would once convert this action to a midrule because of the"
e9690142 1065 " subsequent braced code.\n");
4210cd0b
JD
1066 }
1067 ;
1068
1069%destructor { fprintf (stderr, "DESTRUCTOR\n"); } 'a';
1070%printer { fprintf (yyoutput, "PRINTER"); } 'a';
1071
1072%%
1073
381ecb06 1074static void
4210cd0b
JD
1075yyerror (char const *msg)
1076{
1077 fprintf (stderr, "%s\n", msg);
1078}
1079
381ecb06 1080static int
4210cd0b
JD
1081yylex (void)
1082{
1083 return 'a';
1084}
1085
1086int
1087main (void)
1088{
1089 yydebug = 1;
1090 return !yyparse ();
1091}
1092]])
1093
da730230 1094AT_BISON_CHECK([-t -o input.c input.y])
4210cd0b
JD
1095AT_COMPILE([input])
1096AT_PARSER_CHECK([./input], 0,
1097[[Bison would once convert this action to a midrule because of the subsequent braced code.
1098]],
1099[[Starting parse
1100Entering state 0
231ed89a 1101Reducing stack by rule 1 (line 20):
4210cd0b
JD
1102-> $$ = nterm start ()
1103Stack now 0
1104Entering state 1
1105Reading a token: Next token is token 'a' (PRINTER)
1106syntax error, unexpected 'a', expecting $end
1107Error: popping nterm start ()
1108Stack now 0
1109Cleanup: discarding lookahead token 'a' (PRINTER)
1110DESTRUCTOR
1111Stack now 0
1112]])
1113
1114AT_CLEANUP
965537bc
JD
1115
1116
1117
1118## --------------------------------- ##
1119## String alias declared after use. ##
1120## --------------------------------- ##
1121
1122AT_SETUP([String alias declared after use])
1123
1124# Bison once incorrectly asserted that the symbol number for either a token or
1125# its alias was the highest symbol number so far at the point of the alias
1126# declaration. That was true unless the declaration appeared after their first
6d0ef4ec 1127# uses and other tokens appeared in between.
965537bc
JD
1128
1129AT_DATA([input.y],
1130[[%%
1131start: 'a' "A" 'b';
1132%token 'a' "A";
1133]])
1134
da730230 1135AT_BISON_CHECK([-t -o input.c input.y])
965537bc
JD
1136
1137AT_CLEANUP
a0de5091
JD
1138
1139
1140
1141## -------------------------------- ##
1142## Extra lookahead sets in report. ##
1143## -------------------------------- ##
1144
1145AT_SETUP([[Extra lookahead sets in report]])
1146
88c78747
JD
1147# Bison prints each reduction's lookahead set only next to the associated
1148# state's one item that (1) is associated with the same rule as the reduction
1149# and (2) has its dot at the end of its RHS. Previously, Bison also
1150# erroneously printed the lookahead set next to all of the state's other items
ae93e4e4 1151# associated with the same rule. This bug affected only the '.output' file and
88c78747 1152# not the generated parser source code.
a0de5091
JD
1153
1154AT_DATA([[input.y]],
1155[[%%
1156start: a | 'a' a 'a' ;
1157a: 'a' ;
1158]])
1159
da730230 1160AT_BISON_CHECK([[--report=all input.y]])
a0de5091
JD
1161AT_CHECK([[sed -n '/^state 1$/,/^state 2$/p' input.output]], [[0]],
1162[[state 1
1163
1164 2 start: 'a' . a 'a'
1165 3 a: . 'a'
1166 3 | 'a' . [$end]
1167
1168 'a' shift, and go to state 4
1169
1170 $default reduce using rule 3 (a)
1171
1172 a go to state 5
1173
1174
1175state 2
1176]])
1177
1178AT_CLEANUP
ab7f29f8
JD
1179
1180
1181
1182## ---------------------------------------- ##
1183## Token number in precedence declaration. ##
1184## ---------------------------------------- ##
1185
58bd33b7 1186AT_SETUP([[Token number in precedence declaration]])
ab7f29f8
JD
1187
1188# POSIX says token numbers can be declared in %left, %right, and %nonassoc, but
1189# we lost this in Bison 1.50.
1190
1191AT_DATA_GRAMMAR([input.y],
1192[[%{
1193 #include <stdio.h>
1194 void yyerror (char const *);
1195 int yylex (void);
1196%}
1197
1198%error-verbose
1f36f544 1199%right END 0
ab7f29f8
JD
1200%left TK1 1 TK2 2 "tok alias" 3
1201
1202%%
1203
1f36f544
JD
1204start:
1205 TK1 sr_conflict "tok alias"
1206 | start %prec END
1207 ;
ab7f29f8
JD
1208sr_conflict:
1209 TK2
1210 | TK2 "tok alias"
1211 ;
1212
1213%%
1214
1215void
1216yyerror (char const *msg)
1217{
1218 fprintf (stderr, "%s\n", msg);
1219}
1220
1221int
1222yylex (void)
1223{
1224 static int const input[] = { 1, 2, 3, 0 };
1225 static int const *inputp = input;
1226 return *inputp++;
1227}
1228
1229int
1230main (void)
1231{
1232 return yyparse ();
1233}
1234]])
1235
1236AT_BISON_CHECK([[-o input.c input.y]], [[0]],,
1f36f544
JD
1237[[input.y:23.5-19: warning: rule useless in parser due to conflicts: start: start
1238input.y:27.5-19: warning: rule useless in parser due to conflicts: sr_conflict: TK2 "tok alias"
ab7f29f8
JD
1239]])
1240AT_COMPILE([[input]])
1241AT_PARSER_CHECK([[./input]])
1242
1243AT_CLEANUP
43aabb70
JD
1244
1245
1246
1247## --------------------------- ##
1248## parse-gram.y: LALR = IELR. ##
1249## --------------------------- ##
1250
1251# If parse-gram.y's LALR and IELR parser tables ever begin to differ, we
1252# need to fix parse-gram.y or start using IELR.
1253
1254AT_SETUP([[parse-gram.y: LALR = IELR]])
1255
1256# Avoid tests/bison's dark magic by processing a local copy of the
1257# grammar. Avoid differences in synclines by telling bison that the
1258# output files have the same name.
d8f68fc2
JD
1259[cp $abs_top_srcdir/src/parse-gram.y input.y]
1260AT_BISON_CHECK([[-o input.c -Dlr.type=lalr input.y]])
035810ed
AD
1261[mv input.c lalr.c]
1262AT_CAPTURE_FILE([lalr.c])
d8f68fc2
JD
1263AT_BISON_CHECK([[-o input.c -Dlr.type=ielr input.y]])
1264[mv input.c ielr.c]
035810ed
AD
1265AT_CAPTURE_FILE([ielr.c])
1266AT_CHECK([[diff lalr.c ielr.c]], [[0]])
43aabb70
JD
1267
1268AT_CLEANUP
52cea04a
JD
1269
1270
1271
1272## -------------------------------------------- ##
1273## parse.error=verbose and YYSTACK_USE_ALLOCA. ##
1274## -------------------------------------------- ##
1275
1276AT_SETUP([[parse.error=verbose and YYSTACK_USE_ALLOCA]])
1277
1278AT_DATA_GRAMMAR([input.y],
1279[[%code {
1280 #include <stdio.h>
1281 void yyerror (char const *);
1282 int yylex (void);
1283 #define YYSTACK_USE_ALLOCA 1
1284}
1285
1286%define parse.error verbose
1287
1288%%
1289
1290start: check syntax_error syntax_error ;
1291
1292check:
1293{
1294 if (128 < sizeof yymsgbuf)
1295 {
1296 fprintf (stderr,
1297 "The initial size of yymsgbuf in yyparse has increased\n"
1298 "since this test group was last updated. As a result,\n"
1299 "this test group may no longer manage to induce a\n"
1300 "reallocation of the syntax error message buffer.\n"
1301 "This test group must be adjusted to produce a longer\n"
1302 "error message.\n");
1303 YYABORT;
1304 }
1305}
1306;
1307
1308// Induce a syntax error message whose total length is more than
1309// sizeof yymsgbuf in yyparse. Each token here is 64 bytes.
1310syntax_error:
1311 "123456789112345678921234567893123456789412345678951234567896123A"
1312| "123456789112345678921234567893123456789412345678951234567896123B"
1313| error 'a' 'b' 'c'
1314;
1315
1316%%
1317
1318void
1319yyerror (char const *msg)
1320{
1321 fprintf (stderr, "%s\n", msg);
1322}
1323
1324int
1325yylex (void)
1326{
1327 /* Induce two syntax error messages (which requires full error
1328 recovery by shifting 3 tokens) in order to detect any loss of the
1329 reallocated buffer. */
1330 static char const *input = "abc";
1331 return *input++;
1332}
1333
1334int
1335main (void)
1336{
1337 return yyparse ();
1338}
1339]])
1340
1341AT_BISON_CHECK([[-o input.c input.y]])
1342AT_COMPILE([[input]])
1343AT_PARSER_CHECK([[./input]], [[1]], [],
1344[[syntax error, unexpected 'a', expecting 123456789112345678921234567893123456789412345678951234567896123A or 123456789112345678921234567893123456789412345678951234567896123B
1345syntax error, unexpected $end, expecting 123456789112345678921234567893123456789412345678951234567896123A or 123456789112345678921234567893123456789412345678951234567896123B
1346]])
1347
1348AT_CLEANUP
1349
1350
1351
1352## ------------------------------ ##
1353## parse.error=verbose overflow. ##
1354## ------------------------------ ##
1355
1356# Imagine the case where YYSTACK_ALLOC_MAXIMUM = YYSIZE_MAXIMUM and an
1357# invocation of yysyntax_error has caused yymsg_alloc to grow to exactly
1358# YYSTACK_ALLOC_MAXIMUM (perhaps because the normal doubling of size had
45319f13
JD
1359# to be clipped to YYSTACK_ALLOC_MAXIMUM). In an old version of yacc.c,
1360# a subsequent invocation of yysyntax_error that overflows during its
1361# size calculation would return YYSIZE_MAXIMUM to yyparse. Then,
1362# yyparse would invoke yyerror using the old contents of yymsg.
52cea04a
JD
1363
1364AT_SETUP([[parse.error=verbose overflow]])
1365
52cea04a
JD
1366AT_DATA_GRAMMAR([input.y],
1367[[%code {
1368 #include <stdio.h>
1369 void yyerror (char const *);
1370 int yylex (void);
1371
1372 /* This prevents this test case from having to induce error messages
1373 large enough to overflow size_t. */
1374 #define YYSIZE_T unsigned char
1375
8ff146cd 1376 /* Bring in malloc and set EXIT_SUCCESS so yacc.c doesn't try to
d6b347e4 1377 provide a malloc prototype using our YYSIZE_T. */
52cea04a 1378 #include <stdlib.h>
8ff146cd
PE
1379 #ifndef EXIT_SUCCESS
1380 # define EXIT_SUCCESS 0
d6b347e4 1381 #endif
52cea04a
JD
1382
1383 /* Max depth is usually much smaller than YYSTACK_ALLOC_MAXIMUM, and
1384 we don't want gcc to warn everywhere this constant would be too big
1385 to make sense for our YYSIZE_T. */
1386 #define YYMAXDEPTH 100
1387}
1388
1389%define parse.error verbose
1390
1391%%
1392
1393start: syntax_error1 check syntax_error2 ;
1394
1395// Induce a syntax error message whose total length causes yymsg in
1396// yyparse to be reallocated to size YYSTACK_ALLOC_MAXIMUM, which
1397// should be 255. Each token here is 64 bytes.
1398syntax_error1:
1399 "123456789112345678921234567893123456789412345678951234567896123A"
1400| "123456789112345678921234567893123456789412345678951234567896123B"
1401| "123456789112345678921234567893123456789412345678951234567896123C"
1402| error 'a' 'b' 'c'
1403;
1404
1405check:
1406{
1407 if (yymsg_alloc != YYSTACK_ALLOC_MAXIMUM
1408 || YYSTACK_ALLOC_MAXIMUM != YYSIZE_MAXIMUM
1409 || YYSIZE_MAXIMUM != 255)
1410 {
1411 fprintf (stderr,
1412 "The assumptions of this test group are no longer\n"
1413 "valid, so it may no longer catch the error it was\n"
1414 "designed to catch. Specifically, the following\n"
1415 "values should all be 255:\n\n");
1416 fprintf (stderr, " yymsg_alloc = %d\n", yymsg_alloc);
1417 fprintf (stderr, " YYSTACK_ALLOC_MAXIMUM = %d\n",
1418 YYSTACK_ALLOC_MAXIMUM);
1419 fprintf (stderr, " YYSIZE_MAXIMUM = %d\n", YYSIZE_MAXIMUM);
1420 YYABORT;
1421 }
1422}
1423;
1424
1425// Now overflow.
1426syntax_error2:
1427 "123456789112345678921234567893123456789412345678951234567896123A"
1428| "123456789112345678921234567893123456789412345678951234567896123B"
1429| "123456789112345678921234567893123456789412345678951234567896123C"
1430| "123456789112345678921234567893123456789412345678951234567896123D"
1431| "123456789112345678921234567893123456789412345678951234567896123E"
1432;
1433
1434%%
1435
1436void
1437yyerror (char const *msg)
1438{
1439 fprintf (stderr, "%s\n", msg);
1440}
1441
1442int
1443yylex (void)
1444{
1445 /* Induce two syntax error messages (which requires full error
1446 recovery by shifting 3 tokens). */
1447 static char const *input = "abc";
1448 return *input++;
1449}
1450
1451int
1452main (void)
1453{
1454 /* Push parsers throw away the message buffer between tokens, so skip
1455 this test under maintainer-push-check. */
1456 if (YYPUSH)
1457 return 77;
1458 return yyparse ();
1459}
1460]])
1461
1462AT_BISON_CHECK([[-o input.c input.y]])
1463
1464# gcc warns about tautologies and fallacies involving comparisons for
1465# unsigned char. However, it doesn't produce these same warnings for
1466# size_t and many other types when the warnings would seem to make just
1467# as much sense. We ignore the warnings.
1468[CFLAGS="$NO_WERROR_CFLAGS"]
1469AT_COMPILE([[input]])
1470
1471AT_PARSER_CHECK([[./input]], [[2]], [],
1472[[syntax error, unexpected 'a', expecting 123456789112345678921234567893123456789412345678951234567896123A or 123456789112345678921234567893123456789412345678951234567896123B or 123456789112345678921234567893123456789412345678951234567896123C
1473syntax error
1474memory exhausted
1475]])
1476
1477AT_CLEANUP
bf35c71c
JD
1478
1479
1480
1481## ------------------------ ##
1482## LAC: Exploratory stack. ##
1483## ------------------------ ##
1484
1485AT_SETUP([[LAC: Exploratory stack]])
1486
1487m4_pushdef([AT_LAC_CHECK], [
1488
1489AT_BISON_OPTION_PUSHDEFS([$1])
1490
1491AT_DATA_GRAMMAR([input.y],
1492[[%code {
1493 #include <stdio.h>
1494 void yyerror (char const *);
1495 int yylex (]AT_PURE_IF([[YYSTYPE *]], [[void]])[);
1496}
1497
1498]$1[
1499%define parse.error verbose
1500%token 'c'
1501
1502%%
1503
1504// default reductions in inconsistent states
1505// v v v v v v v v v v v v v v
1506S: 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 ;
107844a3
JD
1507// ^ ^ ^
1508// LAC reallocs
bf35c71c
JD
1509
1510A: 'a' | /*empty*/ { printf ("inconsistent default reduction\n"); } ;
1511B: 'b' ;
1512C: /*empty*/ { printf ("consistent default reduction\n"); } ;
1513
1514%%
1515
1516void
1517yyerror (char const *msg)
1518{
1519 fprintf (stderr, "%s\n", msg);
1520}
1521
1522int
1523yylex (]AT_PURE_IF([[YYSTYPE *v]], [[void]])[)
1524{
1525 static char const *input = "bbbbc";]AT_PURE_IF([[
1526 *v = 0;]])[
1527 return *input++;
1528}
1529
1530int
1531main (void)
1532{
1533 yydebug = 1;
1534 return yyparse ();
1535}
1536]])
1537
107844a3
JD
1538AT_BISON_CHECK([[-Dparse.lac=full -Dparse.lac.es-capacity-initial=1 \
1539 -Dparse.lac.memory-trace=full \
bf35c71c
JD
1540 -t -o input.c input.y]], [[0]], [],
1541[[input.y: conflicts: 21 shift/reduce
1542]])
1543AT_COMPILE([[input]])
1544AT_PARSER_CHECK([[./input > stdout.txt 2> stderr.txt]], [[1]])
1545
1546# Make sure syntax error doesn't forget that 'a' is expected. It would
1547# be forgotten without lookahead correction.
1548AT_CHECK([[grep 'syntax error,' stderr.txt]], [[0]],
1549[[syntax error, unexpected 'c', expecting 'a' or 'b'
1550]])
1551
1552# Check number of default reductions in inconsistent states to be sure
1553# syntax error is detected before unnecessary reductions are performed.
1554AT_CHECK([[perl -0777 -ne 'print s/inconsistent default reduction//g;' \
1555 < stdout.txt || exit 77]], [[0]], [[14]])
1556
1557# Check number of default reductions in consistent states to be sure
1558# it is performed before the syntax error is detected.
1559AT_CHECK([[perl -0777 -ne 'print s/\bconsistent default reduction//g;' \
1560 < stdout.txt || exit 77]], [[0]], [[2]])
1561
107844a3
JD
1562# Check number of reallocs to be sure reallocated memory isn't somehow
1563# lost between LAC invocations.
1564AT_CHECK([[perl -0777 -ne 'print s/\(realloc//g;' < stderr.txt \
1565 || exit 77]], [[0]], [[3]])
1566
bf35c71c
JD
1567AT_BISON_OPTION_POPDEFS
1568])
1569
1570AT_LAC_CHECK([[%define api.push-pull pull]])
1571AT_LAC_CHECK([[%define api.push-pull pull %define api.pure]])
1572AT_LAC_CHECK([[%define api.push-pull both]])
1573AT_LAC_CHECK([[%define api.push-pull both %define api.pure]])
1574
1575m4_popdef([AT_LAC_CHECK])
1576
1577AT_CLEANUP
1578
1579
1580
1581## ------------------------ ##
1582## LAC: Memory exhaustion. ##
1583## ------------------------ ##
1584
1585AT_SETUP([[LAC: Memory exhaustion]])
1586
1587m4_pushdef([AT_LAC_CHECK], [
1588
1589AT_DATA_GRAMMAR([input.y],
1590[[%code {
1591 #include <stdio.h>
1592 void yyerror (char const *);
1593 int yylex (void);
107844a3 1594 #define YYMAXDEPTH 8
bf35c71c
JD
1595}
1596
1597%error-verbose
1598
1599%%
1600
1601S: A A A A A A A A A ;
1602A: /*empty*/ | 'a' ;
1603
1604%%
1605
1606void
1607yyerror (char const *msg)
1608{
1609 fprintf (stderr, "%s\n", msg);
1610}
1611
1612int
1613yylex (void)
1614{
1615 static char const *input = "]$1[";
1616 return *input++;
1617}
1618
1619int
1620main (void)
1621{
1622 yydebug = 1;
1623 return yyparse ();
1624}
1625]])
1626
107844a3 1627AT_BISON_CHECK([[-Dparse.lac=full -Dparse.lac.es-capacity-initial=1 \
bf35c71c
JD
1628 -t -o input.c input.y]], [[0]], [],
1629[[input.y: conflicts: 8 shift/reduce
1630]])
1631AT_COMPILE([[input]])
1632
1633])
1634
1635# Check for memory exhaustion during parsing.
1636AT_LAC_CHECK([[]])
1637AT_PARSER_CHECK([[./input]], [[2]], [[]],
1638[[Starting parse
1639Entering state 0
1640Reading a token: Now at end of input.
1641LAC: initial context established for $end
107844a3 1642LAC: checking lookahead $end: R2 G3 R2 G5 R2 G6 R2 G7 R2 G8 R2 G9 R2 G10 R2 G11 R2 (max size exceeded)
bf35c71c
JD
1643memory exhausted
1644Cleanup: discarding lookahead token $end ()
1645Stack now 0
1646]])
1647
1648# Induce an immediate syntax error with an undefined token, and check
1649# for memory exhaustion while building syntax error message.
1650AT_LAC_CHECK([[z]], [[0]])
1651AT_PARSER_CHECK([[./input]], [[2]], [[]],
1652[[Starting parse
1653Entering state 0
1654Reading a token: Next token is token $undefined ()
1655LAC: initial context established for $undefined
1656LAC: checking lookahead $undefined: Always Err
1657Constructing syntax error message
107844a3 1658LAC: checking lookahead $end: R2 G3 R2 G5 R2 G6 R2 G7 R2 G8 R2 G9 R2 G10 R2 G11 R2 (max size exceeded)
bf35c71c
JD
1659syntax error
1660memory exhausted
1661Cleanup: discarding lookahead token $undefined ()
1662Stack now 0
1663]])
1664
1665m4_popdef([AT_LAC_CHECK])
1666
1667AT_CLEANUP