]> git.saurik.com Git - bison.git/blame - tests/glr-regression.at
Avoid undefined behavior that accessed just before the start of an array.
[bison.git] / tests / glr-regression.at
CommitLineData
ede3d3bc 1# Checking GLR Parsing: Regression Tests -*- Autotest -*-
2781dbd1 2# Copyright (C) 2002, 2003, 2005, 2006 Free Software Foundation, Inc.
ede3d3bc
PH
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2, or (at your option)
7# any later version.
8
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
0fb669f9
PE
16# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17# 02110-1301, USA.
ede3d3bc
PH
18
19AT_BANNER([[GLR Regression Tests]])
20
21## --------------------------- ##
22## Badly Collapsed GLR States. ##
23## --------------------------- ##
24
25AT_SETUP([Badly Collapsed GLR States])
26
27AT_DATA_GRAMMAR([glr-regr1.y],
28[[/* Regression Test: Improper state compression */
29/* Reported by Scott McPeak */
30
31%{
32#include <stdio.h>
33
34#define YYSTYPE int
35static YYSTYPE exprMerge (YYSTYPE x0, YYSTYPE x1);
36int yylex (void);
ac8c5689 37void yyerror (char const *msg);
ede3d3bc
PH
38%}
39
40
41%glr-parser
42
43
44/* -------- productions ------ */
45%%
46
47StartSymbol: E { $$=0; } %merge <exprMerge>
48 ;
49
50E: E 'P' E { $$=1; printf("E -> E 'P' E\n"); } %merge <exprMerge>
51 | 'B' { $$=2; printf("E -> 'B'\n"); } %merge <exprMerge>
52 ;
53
54
55
56/* ---------- C code ----------- */
57%%
58
59static YYSTYPE exprMerge (YYSTYPE x0, YYSTYPE x1)
60{
61 (void) x0;
62 (void) x1;
63 printf ("<OR>\n");
64 return 0;
65}
66
67int
68main (void)
69{
70 return yyparse ();
71}
72
ac8c5689 73void
ede3d3bc
PH
74yyerror (char const *msg)
75{
76 fprintf (stderr, "%s\n", msg);
ede3d3bc
PH
77}
78
79
80int
81yylex (void)
82{
83 for (;;)
84 {
85 int ch = getchar ();
86 if (ch == EOF)
87 return 0;
88 else if (ch == 'B' || ch == 'P')
89 return ch;
90 }
91}
92]])
93
94AT_CHECK([[bison -o glr-regr1.c glr-regr1.y]], 0, [],
95[glr-regr1.y: conflicts: 1 shift/reduce
96])
97AT_COMPILE([glr-regr1])
98AT_CHECK([[echo BPBPB | ./glr-regr1]], 0,
99[[E -> 'B'
100E -> 'B'
101E -> E 'P' E
102E -> 'B'
103E -> E 'P' E
104E -> 'B'
105E -> E 'P' E
106E -> E 'P' E
107<OR>
108]], [])
109
110AT_CLEANUP
111
112## ------------------------------------------------------------ ##
113## Improper handling of embedded actions and $-N in GLR parsers ##
114## ------------------------------------------------------------ ##
115
d6d67dbd 116AT_SETUP([Improper handling of embedded actions and dollar(-N) in GLR parsers])
ede3d3bc
PH
117
118AT_DATA_GRAMMAR([glr-regr2a.y],
119[[/* Regression Test: Improper handling of embedded actions and $-N */
120/* Reported by S. Eken */
121
122%{
f5228370 123 #define YYSTYPE char *
ede3d3bc
PH
124
125 #include <ctype.h>
126 #include <stdio.h>
f508a6a0 127 #include <stdlib.h>
ede3d3bc
PH
128 #include <string.h>
129 int yylex (void);
130 void yyerror (char const *);
131%}
132
133%glr-parser
134
135%%
136
137command:
138 's' var 't'
d6d67dbd 139 { printf ("Variable: '%s'\n", $2); }
ede3d3bc 140 'v' 'x' 'q'
f5228370 141 { free ($2); }
ede3d3bc 142 | 's' var_list 't' 'e'
f5228370 143 { printf ("Varlist: '%s'\n", $2); free ($2); }
ede3d3bc 144 | 's' var 't' var_printer 'x'
f5228370 145 { free ($2); }
ede3d3bc
PH
146 ;
147
148var:
149 'V'
150 { $$ = $1; }
151 ;
152
153var_list:
154 var
155 { $$ = $1; }
156 | var ',' var_list
157 {
f5228370 158 char *s = (char *) realloc ($1, strlen ($1) + 1 + strlen ($3) + 1);
c70fdfcd
PE
159 strcat (s, ",");
160 strcat (s, $3);
f5228370 161 free ($3);
c70fdfcd 162 $$ = s;
d6d67dbd 163 }
ede3d3bc
PH
164 ;
165
166var_printer: 'v'
167 { printf ("Variable: '%s'\n", $-1); }
168
169%%
170
a9739e7c 171FILE *input = NULL;
ede3d3bc
PH
172
173int
174yylex (void)
d6d67dbd 175{
ede3d3bc 176 char buf[50];
c70fdfcd 177 char *s;
a9739e7c 178 switch (fscanf (input, " %1[a-z,]", buf)) {
ede3d3bc
PH
179 case 1:
180 return buf[0];
181 case EOF:
182 return 0;
183 default:
184 break;
185 }
a9739e7c 186 if (fscanf (input, "%49s", buf) != 1)
ac8c5689 187 return 0;
f508a6a0
PE
188 if (sizeof buf - 1 <= strlen (buf))
189 abort ();
c70fdfcd
PE
190 s = (char *) malloc (strlen (buf) + 1);
191 strcpy (s, buf);
192 yylval = s;
ede3d3bc
PH
193 return 'V';
194}
195
196void
197yyerror (char const *s)
198{ printf ("%s\n", s);
199}
200
201int
202main (int argc, char **argv)
d6d67dbd 203{
a9739e7c
PE
204 input = stdin;
205 if (argc == 2 && !(input = fopen (argv[1], "r"))) return 3;
ede3d3bc
PH
206 return yyparse ();
207}
208]])
209
210AT_CHECK([[bison -o glr-regr2a.c glr-regr2a.y]], 0, [],
211[glr-regr2a.y: conflicts: 2 shift/reduce
212])
213AT_COMPILE([glr-regr2a])
214
215AT_CHECK([[echo s VARIABLE_1 t v x q | ./glr-regr2a]], 0,
216[[Variable: 'VARIABLE_1'
217]], [])
218AT_CHECK([[echo s VARIABLE_1 , ANOTHER_VARIABLE_2 t e | ./glr-regr2a]], 0,
219[[Varlist: 'VARIABLE_1,ANOTHER_VARIABLE_2'
220]])
221AT_CHECK([[echo s VARIABLE_3 t v x | ./glr-regr2a]], 0,
222[[Variable: 'VARIABLE_3'
223]], [])
224
225
5e6f62f2
PH
226AT_CLEANUP
227
228## ------------------------------------------------------------ ##
229## Improper merging of GLR delayed action sets ##
230## ------------------------------------------------------------ ##
231
232AT_SETUP([Improper merging of GLR delayed action sets])
233
234AT_DATA_GRAMMAR([glr-regr3.y],
235[[/* Regression Test: Improper merging of GLR delayed action sets. */
236/* Reported by M. Rosien */
237
238%{
239#include <stdio.h>
240#include <stdarg.h>
241
242static int MergeRule (int x0, int x1);
243static void yyerror(char const * s);
1beb0b24 244int yylex (void);
5e6f62f2
PH
245
246#define RULE(x) (1 << (x))
247
248%}
249
250%glr-parser
251
252%token BAD_CHAR
253%token P1 P2 T1 T2 T3 T4 O1 O2
254
255%%
256
257S : P1 T4 O2 NT6 P2 { printf ("Result: %x\n", $4); }
258;
259
260NT1 : P1 T1 O1 T2 P2 { $$ = RULE(2); } %merge<MergeRule>
261;
262
263NT2 : NT1 { $$ = RULE(3); } %merge<MergeRule>
264 | P1 NT1 O1 T3 P2 { $$ = RULE(4); } %merge<MergeRule>
265;
266
267NT3 : T3 { $$ = RULE(5); } %merge<MergeRule>
268 | P1 NT1 O1 T3 P2 { $$ = RULE(6); } %merge<MergeRule>
269;
270
271NT4 : NT3 { $$ = RULE(7); } %merge<MergeRule>
272 | NT2 { $$ = RULE(8); } %merge<MergeRule>
273 | P1 NT2 O1 NT3 P2 { $$ = RULE(9); } %merge<MergeRule>
274;
275
276NT5 : NT4 { $$ = RULE(10); } %merge<MergeRule>
277;
278
279NT6 : P1 NT1 O1 T3 P2 { $$ = RULE(11) | $2; } %merge<MergeRule>
280 | NT5 { $$ = RULE(12) | $1; } %merge<MergeRule>
281;
282
283%%
284
285static int MergeRule (int x0, int x1) {
286 return x0 | x1;
287}
288
289static void yyerror(char const * s) {
290 fprintf(stderr,"error: %s\n",s);
291}
292
a9739e7c 293FILE *input = NULL;
5e6f62f2
PH
294
295int P[] = { P1, P2 };
296int O[] = { O1, O2 };
297int T[] = { T1, T2, T3, T4 };
298
299int yylex (void)
300{
301 char inp[3];
a9739e7c 302 if (fscanf (input, "%2s", inp) == EOF)
5e6f62f2 303 return 0;
1beb0b24 304 switch (inp[0])
5e6f62f2
PH
305 {
306 case 'p': return P[inp[1] - '1'];
307 case 't': return T[inp[1] - '1'];
308 case 'o': return O[inp[1] - '1'];
309 }
310 return BAD_CHAR;
311}
312
313int main(int argc, char* argv[]) {
a9739e7c
PE
314 input = stdin;
315 if (argc == 2 && !(input = fopen (argv[1], "r"))) return 3;
5e6f62f2
PH
316 return yyparse ();
317}
318]])
319
320AT_CHECK([[bison -o glr-regr3.c glr-regr3.y]], 0, [],
321[glr-regr3.y: conflicts: 1 shift/reduce, 1 reduce/reduce
322])
323AT_COMPILE([glr-regr3])
324
325AT_CHECK([[echo p1 t4 o2 p1 p1 t1 o1 t2 p2 o1 t3 p2 p2 | ./glr-regr3]], 0,
326[[Result: 1c04
327]], [])
328
ede3d3bc 329AT_CLEANUP
f9315de5
PE
330
331
4158e0a1
JD
332## ------------------------------------------------------------------------- ##
333## Duplicate representation of merged trees. See ##
334## <http://lists.gnu.org/archive/html/help-bison/2005-07/msg00013.html>. ##
335## ------------------------------------------------------------------------- ##
f9315de5
PE
336
337AT_SETUP([Duplicate representation of merged trees])
338
339AT_DATA_GRAMMAR([glr-regr4.y],
1bd0deda
PE
340[[
341%union { char *ptr; }
f9315de5
PE
342%type <ptr> S A A1 A2 B
343%glr-parser
344
345%{
346 #include <stdio.h>
347 #include <stdlib.h>
348 #include <string.h>
349 static char *merge (YYSTYPE, YYSTYPE);
1bd0deda 350 static char *make_value (char const *, char const *);
f9315de5
PE
351 static void yyerror (char const *);
352 static int yylex (void);
353%}
354
355%%
356
357tree: S { printf ("%s\n", $1); } ;
358
359S:
360 A %merge<merge> { $$ = make_value ("S", $1); }
361 | B %merge<merge> { $$ = make_value ("S", $1); }
362 ;
363
364A:
365 A1 %merge<merge> { $$ = make_value ("A", $1); }
366 | A2 %merge<merge> { $$ = make_value ("A", $1); }
367 ;
368
369A1: 'a' { $$ = make_value ("A1", "'a'"); } ;
370A2: 'a' { $$ = make_value ("A2", "'a'"); } ;
371B: 'a' { $$ = make_value ("B", "'a'"); } ;
372
373%%
374
375static int
376yylex (void)
377{
378 static char const *input = "a";
379 return *input++;
380}
381
382int
383main (void)
384{
385 return yyparse ();
386}
387
388static char *
1bd0deda 389make_value (char const *parent, char const *child)
f9315de5
PE
390{
391 char const format[] = "%s <- %s";
7812f299
PE
392 char *value =
393 (char *) malloc (strlen (parent) + strlen (child) + sizeof format);
f9315de5
PE
394 sprintf (value, format, parent, child);
395 return value;
396}
397
398static char *
399merge (YYSTYPE s1, YYSTYPE s2)
400{
401 char const format[] = "merge{ %s and %s }";
7812f299
PE
402 char *value =
403 (char *) malloc (strlen (s1.ptr) + strlen (s2.ptr) + sizeof format);
f9315de5
PE
404 sprintf (value, format, s1.ptr, s2.ptr);
405 return value;
406}
407
408static void
409yyerror (char const *msg)
410{
42a6501d 411 fprintf (stderr, "%s\n", msg);
f9315de5
PE
412}
413]])
414
415AT_CHECK([[bison -o glr-regr4.c glr-regr4.y]], 0, [],
416[glr-regr4.y: conflicts: 1 reduce/reduce
417])
418AT_COMPILE([glr-regr4])
419
420AT_CHECK([[./glr-regr4]], 0,
421[[merge{ S <- merge{ A <- A1 <- 'a' and A <- A2 <- 'a' } and S <- B <- 'a' }
422]], [])
423
424AT_CLEANUP
adc90f13
PE
425
426
4158e0a1
JD
427## -------------------------------------------------------------------------- ##
428## User destructor for unresolved GLR semantic value. See ##
429## <http://lists.gnu.org/archive/html/bison-patches/2005-08/msg00016.html>. ##
430## -------------------------------------------------------------------------- ##
adc90f13
PE
431
432AT_SETUP([User destructor for unresolved GLR semantic value])
433
434AT_DATA_GRAMMAR([glr-regr5.y],
1bd0deda
PE
435[[
436%{
adc90f13
PE
437 #include <stdio.h>
438 #include <stdlib.h>
439 static void yyerror (char const *);
440 static int yylex (void);
441 enum { MAGIC_VALUE = -1057808125 }; /* originally chosen at random */
442%}
443
444%glr-parser
445%union { int value; }
446%type <value> start
447
448%destructor {
449 if ($$ != MAGIC_VALUE)
450 {
451 fprintf (stderr, "Bad destructor call.\n");
452 exit (EXIT_FAILURE);
453 }
454} start
455
456%%
457
458start:
459 'a' { $$ = MAGIC_VALUE; }
460 | 'a' { $$ = MAGIC_VALUE; }
461 ;
462
463%%
464
465static int
466yylex (void)
467{
468 static char const *input = "a";
469 return *input++;
470}
471
472static void
473yyerror (char const *msg)
474{
42a6501d 475 fprintf (stderr, "%s\n", msg);
adc90f13
PE
476}
477
478int
479main (void)
480{
481 return yyparse () != 1;
482}
483]])
484
485AT_CHECK([[bison -o glr-regr5.c glr-regr5.y]], 0, [],
486[glr-regr5.y: conflicts: 1 reduce/reduce
487])
488AT_COMPILE([glr-regr5])
489
42a6501d
PE
490AT_CHECK([[./glr-regr5]], 0, [],
491[syntax is ambiguous
492])
493
494AT_CLEANUP
495
496
4158e0a1
JD
497## -------------------------------------------------------------------------- ##
498## User destructor after an error during a split parse. See ##
499## <http://lists.gnu.org/archive/html/bison-patches/2005-08/msg00029.html>. ##
500## -------------------------------------------------------------------------- ##
42a6501d
PE
501
502AT_SETUP([User destructor after an error during a split parse])
503
504AT_DATA_GRAMMAR([glr-regr6.y],
1bd0deda
PE
505[[
506%{
42a6501d
PE
507 #include <stdio.h>
508 #include <stdlib.h>
509 static void yyerror (char const *);
510 static int yylex (void);
511%}
512
513%glr-parser
514%union { int value; }
515%type <value> 'a'
516
517%destructor {
518 printf ("Destructor called.\n");
519} 'a'
520
521%%
522
523start: 'a' | 'a' ;
524
525%%
526
527static int
528yylex (void)
529{
530 static char const *input = "a";
531 return *input++;
532}
533
534static void
535yyerror (char const *msg)
536{
537 fprintf (stderr, "%s\n", msg);
538}
539
540int
541main (void)
542{
543 return yyparse () != 1;
544}
545]])
546
547AT_CHECK([[bison -o glr-regr6.c glr-regr6.y]], 0, [],
548[glr-regr6.y: conflicts: 1 reduce/reduce
549])
550AT_COMPILE([glr-regr6])
551
552AT_CHECK([[./glr-regr6]], 0,
553[Destructor called.
554],
adc90f13
PE
555[syntax is ambiguous
556])
557
558AT_CLEANUP
1bd0deda
PE
559
560
561## ------------------------------------------------------------------------- ##
4158e0a1 562## Duplicated user destructor for lookahead. See ##
1bd0deda
PE
563## <http://lists.gnu.org/archive/html/bison-patches/2005-08/msg00035.html>. ##
564## ------------------------------------------------------------------------- ##
565
566AT_SETUP([Duplicated user destructor for lookahead])
567
568AT_DATA_GRAMMAR([glr-regr7.y],
569[[
570%{
571 #include <stdio.h>
572 #include <stdlib.h>
573 static void yyerror (char const *);
574 static int yylex (void);
575 #define YYSTACKEXPANDABLE 0
576%}
577
578%glr-parser
579%union { int *count; }
580%type <count> 'a'
581
582%destructor {
583 if ((*$$)++)
584 fprintf (stderr, "Destructor called on same value twice.\n");
585} 'a'
586
587%%
588
589start:
590 stack1 start
591 | stack2 start
592 | /* empty */
593 ;
594stack1: 'a' ;
595stack2: 'a' ;
596
597%%
598
599static int
600yylex (void)
601{
a9739e7c 602 yylval.count = (int *) malloc (sizeof (int));
1bd0deda
PE
603 if (!yylval.count)
604 {
605 fprintf (stderr, "Test inconclusive.\n");
606 exit (EXIT_FAILURE);
607 }
608 *yylval.count = 0;
609 return 'a';
610}
611
612static void
613yyerror (char const *msg)
614{
615 fprintf (stderr, "%s\n", msg);
616}
617
618int
619main (void)
620{
621 return yyparse ();
622}
623]])
624
625AT_CHECK([[bison -o glr-regr7.c glr-regr7.y]], 0, [],
626[glr-regr7.y: conflicts: 2 reduce/reduce
627])
628AT_COMPILE([glr-regr7])
629
a9739e7c 630AT_CHECK([[./glr-regr7]], 2, [],
1bd0deda
PE
631[memory exhausted
632])
633
634AT_CLEANUP
44e7ead1
PH
635
636
637## ------------------------------------------------------------------------- ##
638## Incorrect default location for empty right-hand sides. Adapted from bug ##
639## report by Claudia Hermann. ##
640## See http://lists.gnu.org/archive/html/bug-bison/2005-10/msg00069.html and ##
641## http://lists.gnu.org/archive/html/bug-bison/2005-10/msg00072.html ##
642## ------------------------------------------------------------------------- ##
643
644AT_SETUP([Incorrectly initialized location for empty right-hand side in GLR])
645
646AT_DATA_GRAMMAR([glr-regr8.y],
647[[
648%{
649 #include <stdio.h>
650 #include <stdlib.h>
651 static void yyerror (char const *);
652 static int yylex (void);
653 static void yyerror(const char *msg);
654%}
655
656%token T_CONSTANT
657%token T_PORT
658%token T_SIGNAL
659
660%glr-parser
661
662%%
663
664
665PortClause : T_PORT InterfaceDeclaration T_PORT
69ce078b
PE
666 { printf("%d/%d - %d/%d - %d/%d\n",
667 @1.first_column, @1.last_column,
668 @2.first_column, @2.last_column,
44e7ead1
PH
669 @3.first_column, @3.last_column); }
670 ;
671
672InterfaceDeclaration : OptConstantWord %dprec 1
673 | OptSignalWord %dprec 2
674 ;
675
676OptConstantWord : /* empty */
677 | T_CONSTANT
678 ;
679
680OptSignalWord : /* empty */
681 { printf("empty: %d/%d\n", @$.first_column, @$.last_column); }
682 | T_SIGNAL
683 ;
684
685%%
686
687void yyerror(const char *msg)
688{
689 fprintf (stderr, "error\n");
690}
691
692static int lexIndex;
693
69ce078b 694int yylex (void)
44e7ead1
PH
695{
696 lexIndex += 1;
697 switch (lexIndex)
698 {
699 case 1:
700 yylloc.first_column = 1;
701 yylloc.last_column = 9;
702 return T_PORT;
703 case 2:
704 yylloc.first_column = 13;
705 yylloc.last_column = 17;
706 return T_PORT;
707 default:
708 return 0;
709 }
710}
711
712int
69ce078b 713main (void)
44e7ead1
PH
714{
715 yyparse();
716 return 0;
717}
718]])
719
720AT_CHECK([[bison -o glr-regr8.c glr-regr8.y]], 0, [],
721[glr-regr8.y: conflicts: 1 reduce/reduce
722])
723AT_COMPILE([glr-regr8])
724
69ce078b 725AT_CHECK([[./glr-regr8]], 0,
44e7ead1
PH
726[empty: 9/9
7271/9 - 9/9 - 13/17
728],
729[])
730
731AT_CLEANUP
69ce078b
PE
732
733
734## ------------------------------------------------------------------------- ##
4158e0a1 735## No users destructors if stack 0 deleted. See ##
69ce078b
PE
736## <http://lists.gnu.org/archive/html/bison-patches/2005-09/msg00109.html>. ##
737## ------------------------------------------------------------------------- ##
738
739AT_SETUP([No users destructors if stack 0 deleted])
740
741AT_DATA_GRAMMAR([glr-regr9.y],
742[[
743%{
affac613
AD
744# include <stdio.h>
745# include <stdlib.h>
69ce078b
PE
746 static void yyerror (char const *);
747 static int yylex (void);
affac613 748# define YYSTACKEXPANDABLE 0
69ce078b
PE
749 static int tokens = 0;
750 static int destructors = 0;
affac613 751# define USE(Var)
69ce078b
PE
752%}
753
754%glr-parser
755%union { int dummy; }
756%type <dummy> 'a'
757
758%destructor {
759 destructors += 1;
760} 'a'
761
762%%
763
764start:
affac613 765 ambig0 'a' { destructors += 2; USE ($2); }
69ce078b
PE
766 | ambig1 start { destructors += 1; }
767 | ambig2 start { destructors += 1; }
768 ;
769
770ambig0: 'a' ;
771ambig1: 'a' ;
772ambig2: 'a' ;
773
774%%
775
776static int
777yylex (void)
778{
779 tokens += 1;
780 return 'a';
781}
782
783static void
784yyerror (char const *msg)
785{
786 fprintf (stderr, "%s\n", msg);
787}
788
789int
790main (void)
791{
792 int exit_status;
793 exit_status = yyparse ();
794 if (tokens != destructors)
795 {
796 fprintf (stderr, "Tokens = %d, Destructors = %d\n", tokens, destructors);
797 return 1;
798 }
799 return !exit_status;
800}
801]])
802
803AT_CHECK([[bison -o glr-regr9.c glr-regr9.y]], 0, [],
804[glr-regr9.y: conflicts: 1 reduce/reduce
805])
806AT_COMPILE([glr-regr9])
807
808AT_CHECK([[./glr-regr9]], 0, [],
809[memory exhausted
810])
811
812AT_CLEANUP
d659304d
JD
813
814
815## ------------------------------------------------------------------------- ##
816## Corrupted semantic options if user action cuts parse. ##
817## ------------------------------------------------------------------------- ##
818
bf70fa87 819AT_SETUP([Corrupted semantic options if user action cuts parse])
d659304d
JD
820
821AT_DATA_GRAMMAR([glr-regr10.y],
822[[
823%{
824# include <stdio.h>
825 static void yyerror (char const *);
826 static int yylex (void);
827 #define GARBAGE_SIZE 50
828 static char garbage[GARBAGE_SIZE];
829%}
830
831%glr-parser
832%union { char *ptr; }
833%type <ptr> start
834
835%%
836
837start:
838 %dprec 2 { $$ = garbage; YYACCEPT; }
839 | %dprec 1 { $$ = garbage; YYACCEPT; }
840 ;
841
842%%
843
844static void
845yyerror (char const *msg)
846{
847 fprintf (stderr, "%s\n", msg);
848}
849
850static int
851yylex (void)
852{
853 return 0;
854}
855
856int
857main (void)
858{
859 int index;
860 for (index = 0; index < GARBAGE_SIZE; index+=1)
bf70fa87 861 garbage[index] = 108;
d659304d
JD
862 return yyparse ();
863}
864]])
865
866AT_CHECK([[bison -t -o glr-regr10.c glr-regr10.y]], 0, [],
867[glr-regr10.y: conflicts: 1 reduce/reduce
868])
869AT_COMPILE([glr-regr10])
870
871AT_CHECK([[./glr-regr10]], 0, [], [])
872
873AT_CLEANUP
874
875
876## ------------------------------------------------------------------------- ##
877## Undesirable destructors if user action cuts parse. ##
878## ------------------------------------------------------------------------- ##
879
bf70fa87 880AT_SETUP([Undesirable destructors if user action cuts parse])
d659304d
JD
881
882AT_DATA_GRAMMAR([glr-regr11.y],
883[[
884%{
885# include <stdlib.h>
886 static void yyerror (char const *);
887 static int yylex (void);
888 static int destructors = 0;
889# define USE(val)
890%}
891
892%glr-parser
893%union { int dummy; }
894%type <int> 'a'
895%destructor { destructors += 1; } 'a'
896
897%%
898
899start:
900 'a' %dprec 2 { USE ($1); destructors += 1; YYACCEPT; }
901 | 'a' %dprec 1 { USE ($1); destructors += 1; YYACCEPT; }
902 ;
903
904%%
905
906static void
907yyerror (char const *msg)
908{
909 fprintf (stderr, "%s\n", msg);
910}
911
912static int
913yylex (void)
914{
915 static char const *input = "a";
916 return *input++;
917}
918
919int
920main (void)
921{
922 int exit_status = yyparse ();
923 if (destructors != 1)
924 {
925 fprintf (stderr, "Destructor calls: %d\n", destructors);
926 return 1;
927 }
928 return exit_status;
929}
930]])
931
932AT_CHECK([[bison -t -o glr-regr11.c glr-regr11.y]], 0, [],
933[glr-regr11.y: conflicts: 1 reduce/reduce
934])
935AT_COMPILE([glr-regr11])
936
937AT_CHECK([[./glr-regr11]], 0, [], [])
938
939AT_CLEANUP
940
941
942## ------------------------------------------------------------------------- ##
943## Leaked merged semantic value if user action cuts parse. ##
944## ------------------------------------------------------------------------- ##
945
bf70fa87 946AT_SETUP([Leaked merged semantic value if user action cuts parse])
d659304d
JD
947
948AT_DATA_GRAMMAR([glr-regr12.y],
949[[
950%glr-parser
951%union { int dummy; }
952%type <dummy> start
953%destructor { has_value = 0; } start
954
955%{
956# include <stdlib.h>
957 static int merge (YYSTYPE, YYSTYPE);
958 static void yyerror (char const *);
959 static int yylex (void);
960 static int has_value = 0;
961# define USE(val)
962%}
963
964%%
965
966start:
967 %merge<merge> { has_value = 1; USE ($$); }
968 | %merge<merge> { USE ($$); YYACCEPT; }
969 ;
970
971%%
972
973static int
974merge (YYSTYPE s1, YYSTYPE s2)
975{
976 /* Not invoked. */
bf70fa87
JD
977 char dummy = s1.dummy + s2.dummy;
978 return dummy;
d659304d
JD
979}
980
981static void
982yyerror (char const *msg)
983{
984 fprintf (stderr, "%s\n", msg);
985}
986
987static int
988yylex (void)
989{
990 return 0;
991}
992
993int
994main (void)
995{
996 int exit_status = yyparse ();
997 if (has_value)
998 {
999 fprintf (stderr, "Destructor not called.\n");
1000 return 1;
1001 }
1002 return exit_status;
1003}
1004]])
1005
1006AT_CHECK([[bison -t -o glr-regr12.c glr-regr12.y]], 0, [],
1007[glr-regr12.y: conflicts: 1 reduce/reduce
1008])
1009AT_COMPILE([glr-regr12])
1010
1011AT_CHECK([[./glr-regr12]], 0, [], [])
1012
1013AT_CLEANUP
bf70fa87
JD
1014
1015
1016## ------------------------------------------------------------------------- ##
1017## Incorrect lookahead during deterministic GLR. See ##
1018## <http://lists.gnu.org/archive/html/help-bison/2005-07/msg00017.html>. ##
1019## ------------------------------------------------------------------------- ##
1020
1021AT_SETUP([Incorrect lookahead during deterministic GLR])
1022
1023AT_DATA_GRAMMAR([glr-regr13.y],
1024[[
1025/* Tests:
1026 - Defaulted state with initial yychar: yychar == YYEMPTY.
1027 - Nondefaulted state: yychar != YYEMPTY.
1028 - Defaulted state after lookahead: yychar != YYEMPTY.
1029 - Defaulted state after shift: yychar == YYEMPTY. */
1030
1031%{
1032 #include <stdio.h>
1033 static void yyerror (char const *);
1034 static int yylex (void);
1035 static void print_look_ahead (char const *);
1036 #define USE(value)
1037%}
1038
1039%union { char value; }
1040%type <value> 'a' 'b'
1041%glr-parser
1042%locations
1043
1044%%
1045
1046start:
1047 defstate_init defstate_shift 'b' {
1048 USE ($3);
1049 print_look_ahead ("start <- defstate_init defstate_shift 'b'");
1050 }
1051 ;
1052defstate_init:
1053 {
1054 print_look_ahead ("defstate_init <- empty string");
1055 }
1056 ;
1057defstate_shift:
1058 nondefstate defstate_look 'a' {
1059 USE ($3);
1060 print_look_ahead ("defstate_shift <- nondefstate defstate_look 'a'");
1061 }
1062 ;
1063defstate_look:
1064 {
1065 print_look_ahead ("defstate_look <- empty string");
1066 }
1067 ;
1068nondefstate:
1069 {
1070 print_look_ahead ("nondefstate <- empty string");
1071 }
1072 | 'b' {
1073 USE ($1);
1074 print_look_ahead ("nondefstate <- 'b'");
1075 }
1076 ;
1077
1078%%
1079
1080static void
1081yyerror (char const *msg)
1082{
1083 fprintf (stderr, "%s\n", msg);
1084}
1085
1086static int
1087yylex (void)
1088{
1089 static char const *input = "ab";
1090 static int index = 0;
1091 yylloc.first_line = yylloc.last_line = 1;
1092 yylloc.first_column = yylloc.last_column = index+1;
1093 yylval.value = input[index] + 'A' - 'a';
1094 return input[index++];
1095}
1096
1097static void
1098print_look_ahead (char const *reduction)
1099{
1100 printf ("%s:\n yychar=", reduction);
1101 if (yychar == YYEMPTY)
1102 printf ("YYEMPTY");
1103 else if (yychar == YYEOF)
1104 printf ("YYEOF");
1105 else
1106 {
1107 printf ("'%c', yylval='", yychar);
1108 if (yylval.value > ' ')
1109 printf ("%c", yylval.value);
1110 printf ("', yylloc=(%d,%d),(%d,%d)",
1111 yylloc.first_line, yylloc.first_column,
1112 yylloc.last_line, yylloc.last_column);
1113 }
1114 printf ("\n");
1115}
1116
1117int
1118main (void)
1119{
1120 yychar = '#'; /* Not a token in the grammar. */
1121 yylval.value = '!';
1122 return yyparse ();
1123}
1124]])
1125
1126AT_CHECK([[bison -t -o glr-regr13.c glr-regr13.y]], 0, [], [])
1127AT_COMPILE([glr-regr13])
1128
1129AT_CHECK([[./glr-regr13]], 0,
1130[defstate_init <- empty string:
1131 yychar=YYEMPTY
1132nondefstate <- empty string:
1133 yychar='a', yylval='A', yylloc=(1,1),(1,1)
1134defstate_look <- empty string:
1135 yychar='a', yylval='A', yylloc=(1,1),(1,1)
1136defstate_shift <- nondefstate defstate_look 'a':
1137 yychar=YYEMPTY
1138start <- defstate_init defstate_shift 'b':
1139 yychar=YYEMPTY
1140], [])
1141
1142AT_CLEANUP
1143
1144
1145## ------------------------------------------------------------------------- ##
1146## Incorrect lookahead during nondeterministic GLR. ##
1147## ------------------------------------------------------------------------- ##
1148
1149AT_SETUP([Incorrect lookahead during nondeterministic GLR])
1150
1151AT_DATA_GRAMMAR([glr-regr14.y],
1152[[
1153/* Tests:
b7691f15 1154 - Conflicting actions (split-off parse, which copies lookahead need,
bf70fa87
JD
1155 which is necessarily yytrue) and nonconflicting actions (non-split-off
1156 parse) for nondefaulted state: yychar != YYEMPTY.
b7691f15 1157 - Merged deferred actions (lookahead need and RHS from different stack
bf70fa87
JD
1158 than the target state) and nonmerged deferred actions (same stack).
1159 - Defaulted state after lookahead: yychar != YYEMPTY.
1160 - Defaulted state after shift: yychar == YYEMPTY.
b7691f15 1161 - yychar != YYEMPTY but lookahead need is yyfalse (a previous stack has
bf70fa87
JD
1162 seen the lookahead but current stack has not).
1163 - Exceeding stack capacity (stack explosion), and thus reallocating
b7691f15 1164 lookahead need array.
bf70fa87
JD
1165 Note that it does not seem possible to see the initial yychar value during
1166 nondeterministic operation since:
1167 - In order to preserve the initial yychar, only defaulted states may be
1168 entered.
1169 - If only defaulted states are entered, there are no conflicts, so
1170 nondeterministic operation does not start. */
1171
1172%union { char value; }
1173
1174%{
1175 #include <stdio.h>
1176 static void yyerror (char const *);
1177 static int yylex (void);
1178 static void print_look_ahead (char const *);
1179 static char merge (union YYSTYPE, union YYSTYPE);
1180 #define USE(value)
1181%}
1182
1183%type <value> 'a' 'b' 'c' 'd' stack_explosion
1184%glr-parser
1185%locations
1186
1187%%
1188
1189start:
1190 merge 'c' stack_explosion {
1191 USE ($2); USE ($3);
1192 print_look_ahead ("start <- merge 'c' stack_explosion");
1193 }
1194 ;
1195
b7691f15 1196/* When merging the 2 deferred actions, the lookahead needs are different. */
bf70fa87
JD
1197merge:
1198 nonconflict1 'a' 'b' nonconflict2 %dprec 1 {
1199 USE ($2); USE ($3);
1200 print_look_ahead ("merge <- nonconflict1 'a' 'b' nonconflict2");
1201 }
1202 | conflict defstate_look 'a' nonconflict2 'b' defstate_shift %dprec 2 {
1203 USE ($3); USE ($5);
1204 print_look_ahead ("merge <- conflict defstate_look 'a' nonconflict2 'b'"
1205 " defstate_shift");
1206 }
1207 ;
1208
1209nonconflict1:
1210 {
1211 print_look_ahead ("nonconflict1 <- empty string");
1212 }
1213 ;
1214nonconflict2:
1215 {
1216 print_look_ahead ("nonconflict2 <- empty string");
1217 }
1218 | 'a' {
1219 USE ($1);
1220 print_look_ahead ("nonconflict2 <- 'a'");
1221 }
1222 ;
1223conflict:
1224 {
1225 print_look_ahead ("conflict <- empty string");
1226 }
1227 ;
1228defstate_look:
1229 {
1230 print_look_ahead ("defstate_look <- empty string");
1231 }
1232 ;
1233
b7691f15 1234/* yychar != YYEMPTY but lookahead need is yyfalse. */
bf70fa87
JD
1235defstate_shift:
1236 {
1237 print_look_ahead ("defstate_shift <- empty string");
1238 }
1239 ;
1240
1241stack_explosion:
1242 { $$ = '\0'; }
1243 | alt1 stack_explosion %merge<merge> { $$ = $2; }
1244 | alt2 stack_explosion %merge<merge> { $$ = $2; }
1245 | alt3 stack_explosion %merge<merge> { $$ = $2; }
1246 ;
1247alt1:
1248 'd' no_look {
1249 USE ($1);
1250 if (yychar != 'd' && yychar != YYEOF)
1251 {
1252 fprintf (stderr, "Incorrect lookahead during stack explosion.\n");
1253 }
1254 }
1255 ;
1256alt2:
1257 'd' no_look {
1258 USE ($1);
1259 if (yychar != 'd' && yychar != YYEOF)
1260 {
1261 fprintf (stderr, "Incorrect lookahead during stack explosion.\n");
1262 }
1263 }
1264 ;
1265alt3:
1266 'd' no_look {
1267 USE ($1);
1268 if (yychar != 'd' && yychar != YYEOF)
1269 {
1270 fprintf (stderr, "Incorrect lookahead during stack explosion.\n");
1271 }
1272 }
1273 ;
1274no_look:
1275 {
1276 if (yychar != YYEMPTY)
1277 {
1278 fprintf (stderr,
1279 "Found lookahead where shouldn't during stack explosion.\n");
1280 }
1281 }
1282 ;
1283
1284%%
1285
1286static void
1287yyerror (char const *msg)
1288{
1289 fprintf (stderr, "%s\n", msg);
1290}
1291
1292static int
1293yylex (void)
1294{
1295 static char const *input = "abcdddd";
1296 static int index = 0;
1297 yylloc.first_line = yylloc.last_line = 1;
1298 yylloc.first_column = yylloc.last_column = index+1;
1299 yylval.value = input[index] + 'A' - 'a';
1300 return input[index++];
1301}
1302
1303static void
1304print_look_ahead (char const *reduction)
1305{
1306 printf ("%s:\n yychar=", reduction);
1307 if (yychar == YYEMPTY)
1308 printf ("YYEMPTY");
1309 else if (yychar == YYEOF)
1310 printf ("YYEOF");
1311 else
1312 {
1313 printf ("'%c', yylval='", yychar);
1314 if (yylval.value > ' ')
1315 printf ("%c", yylval.value);
1316 printf ("', yylloc=(%d,%d),(%d,%d)",
1317 yylloc.first_line, yylloc.first_column,
1318 yylloc.last_line, yylloc.last_column);
1319 }
1320 printf ("\n");
1321}
1322
1323static char
1324merge (union YYSTYPE s1, union YYSTYPE s2)
1325{
1326 char dummy = s1.value + s2.value;
1327 return dummy;
1328}
1329
1330int
1331main (void)
1332{
1333 yychar = '#'; /* Not a token in the grammar. */
1334 yylval.value = '!';
1335 return yyparse ();
1336}
1337]])
1338
1339AT_CHECK([[bison -t -o glr-regr14.c glr-regr14.y]], 0, [],
1340[glr-regr14.y: conflicts: 3 reduce/reduce
1341])
1342AT_COMPILE([glr-regr14])
1343
1344AT_CHECK([[./glr-regr14]], 0,
1345[conflict <- empty string:
1346 yychar='a', yylval='A', yylloc=(1,1),(1,1)
1347defstate_look <- empty string:
1348 yychar='a', yylval='A', yylloc=(1,1),(1,1)
1349nonconflict2 <- empty string:
1350 yychar='b', yylval='B', yylloc=(1,2),(1,2)
1351defstate_shift <- empty string:
1352 yychar=YYEMPTY
1353merge <- conflict defstate_look 'a' nonconflict2 'b' defstate_shift:
1354 yychar=YYEMPTY
1355start <- merge 'c' stack_explosion:
1356 yychar=YYEOF
1357], [])
1358
1359AT_CLEANUP