]> git.saurik.com Git - bison.git/blame - tests/glr-regression.at
Undo previous change, then add comment as to why the
[bison.git] / tests / glr-regression.at
CommitLineData
ede3d3bc 1# Checking GLR Parsing: Regression Tests -*- Autotest -*-
d6d67dbd 2# Copyright (C) 2002, 2003, 2005 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%{
c70fdfcd 123 #define YYSTYPE char const *
ede3d3bc
PH
124 #define yyfalse 0
125 #define yytrue 1
126
127 #include <ctype.h>
128 #include <stdio.h>
f508a6a0 129 #include <stdlib.h>
ede3d3bc
PH
130 #include <string.h>
131 int yylex (void);
132 void yyerror (char const *);
133%}
134
135%glr-parser
136
137%%
138
139command:
140 's' var 't'
d6d67dbd 141 { printf ("Variable: '%s'\n", $2); }
ede3d3bc
PH
142 'v' 'x' 'q'
143 | 's' var_list 't' 'e'
144 { printf ("Varlist: '%s'\n", $2); }
145 | 's' var 't' var_printer 'x'
146 ;
147
148var:
149 'V'
150 { $$ = $1; }
151 ;
152
153var_list:
154 var
155 { $$ = $1; }
156 | var ',' var_list
157 {
c70fdfcd
PE
158 char *s = (char *) malloc (strlen ($1) + 1 + strlen ($3) + 1);
159 strcpy (s, $1);
160 strcat (s, ",");
161 strcat (s, $3);
162 $$ = s;
d6d67dbd 163 }
ede3d3bc
PH
164 ;
165
166var_printer: 'v'
167 { printf ("Variable: '%s'\n", $-1); }
168
169%%
170
171FILE *yyin = NULL;
172
173int
174yylex (void)
d6d67dbd 175{
ede3d3bc 176 char buf[50];
c70fdfcd 177 char *s;
ede3d3bc
PH
178 switch (fscanf (yyin, " %1[a-z,]", buf)) {
179 case 1:
180 return buf[0];
181 case EOF:
182 return 0;
183 default:
184 break;
185 }
f508a6a0 186 if (fscanf (yyin, "%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{
ede3d3bc 204 yyin = stdin;
6100a9aa 205 if (argc == 2 && !(yyin = 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
293FILE *yyin = NULL;
294
295int P[] = { P1, P2 };
296int O[] = { O1, O2 };
297int T[] = { T1, T2, T3, T4 };
298
299int yylex (void)
300{
301 char inp[3];
302 if (fscanf (yyin, "%2s", inp) == EOF)
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[]) {
314 yyin = stdin;
6100a9aa 315 if (argc == 2 && !(yyin = 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
332## ---------------------------------------------------------------------- ##
333## Duplicate representation of merged trees ##
334## Thanks to Joel E. Denny for this test; see ##
335## <http://lists.gnu.org/archive/html/help-bison/2005-07/msg00013.html>. ##
336## ---------------------------------------------------------------------- ##
337
338AT_SETUP([Duplicate representation of merged trees])
339
340AT_DATA_GRAMMAR([glr-regr4.y],
341[[%union { char *ptr; }
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);
350 static char *make_value (char *, char *);
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 *
389make_value (char *parent, char *child)
390{
391 char const format[] = "%s <- %s";
392 char *value = malloc (strlen (parent) + strlen (child) + sizeof format);
393 sprintf (value, format, parent, child);
394 return value;
395}
396
397static char *
398merge (YYSTYPE s1, YYSTYPE s2)
399{
400 char const format[] = "merge{ %s and %s }";
401 char *value = malloc (strlen (s1.ptr) + strlen (s2.ptr) + sizeof format);
402 sprintf (value, format, s1.ptr, s2.ptr);
403 return value;
404}
405
406static void
407yyerror (char const *msg)
408{
42a6501d 409 fprintf (stderr, "%s\n", msg);
f9315de5
PE
410}
411]])
412
413AT_CHECK([[bison -o glr-regr4.c glr-regr4.y]], 0, [],
414[glr-regr4.y: conflicts: 1 reduce/reduce
415])
416AT_COMPILE([glr-regr4])
417
418AT_CHECK([[./glr-regr4]], 0,
419[[merge{ S <- merge{ A <- A1 <- 'a' and A <- A2 <- 'a' } and S <- B <- 'a' }
420]], [])
421
422AT_CLEANUP
adc90f13
PE
423
424
42a6501d
PE
425## ------------------------------------------------------------------------- ##
426## User destructor for unresolved GLR semantic value ##
427## Thanks to Joel E. Denny for this test; see ##
428## <http://lists.gnu.org/archive/html/bison-patches/2005-08/msg00016.html>. ##
429## ------------------------------------------------------------------------- ##
adc90f13
PE
430
431AT_SETUP([User destructor for unresolved GLR semantic value])
432
433AT_DATA_GRAMMAR([glr-regr5.y],
434[[%{
435 #include <stdio.h>
436 #include <stdlib.h>
437 static void yyerror (char const *);
438 static int yylex (void);
439 enum { MAGIC_VALUE = -1057808125 }; /* originally chosen at random */
440%}
441
442%glr-parser
443%union { int value; }
444%type <value> start
445
446%destructor {
447 if ($$ != MAGIC_VALUE)
448 {
449 fprintf (stderr, "Bad destructor call.\n");
450 exit (EXIT_FAILURE);
451 }
452} start
453
454%%
455
456start:
457 'a' { $$ = MAGIC_VALUE; }
458 | 'a' { $$ = MAGIC_VALUE; }
459 ;
460
461%%
462
463static int
464yylex (void)
465{
466 static char const *input = "a";
467 return *input++;
468}
469
470static void
471yyerror (char const *msg)
472{
42a6501d 473 fprintf (stderr, "%s\n", msg);
adc90f13
PE
474}
475
476int
477main (void)
478{
479 return yyparse () != 1;
480}
481]])
482
483AT_CHECK([[bison -o glr-regr5.c glr-regr5.y]], 0, [],
484[glr-regr5.y: conflicts: 1 reduce/reduce
485])
486AT_COMPILE([glr-regr5])
487
42a6501d
PE
488AT_CHECK([[./glr-regr5]], 0, [],
489[syntax is ambiguous
490])
491
492AT_CLEANUP
493
494
495## ------------------------------------------------------------------------- ##
496## User destructor after an error during a split parse ##
497## Thanks to Joel E. Denny for this test; see ##
498## <http://lists.gnu.org/archive/html/bison-patches/2005-08/msg00029.html>. ##
499## ------------------------------------------------------------------------- ##
500
501AT_SETUP([User destructor after an error during a split parse])
502
503AT_DATA_GRAMMAR([glr-regr6.y],
504[[%{
505 #include <stdio.h>
506 #include <stdlib.h>
507 static void yyerror (char const *);
508 static int yylex (void);
509%}
510
511%glr-parser
512%union { int value; }
513%type <value> 'a'
514
515%destructor {
516 printf ("Destructor called.\n");
517} 'a'
518
519%%
520
521start: 'a' | 'a' ;
522
523%%
524
525static int
526yylex (void)
527{
528 static char const *input = "a";
529 return *input++;
530}
531
532static void
533yyerror (char const *msg)
534{
535 fprintf (stderr, "%s\n", msg);
536}
537
538int
539main (void)
540{
541 return yyparse () != 1;
542}
543]])
544
545AT_CHECK([[bison -o glr-regr6.c glr-regr6.y]], 0, [],
546[glr-regr6.y: conflicts: 1 reduce/reduce
547])
548AT_COMPILE([glr-regr6])
549
550AT_CHECK([[./glr-regr6]], 0,
551[Destructor called.
552],
adc90f13
PE
553[syntax is ambiguous
554])
555
556AT_CLEANUP