1 # Checking GLR Parsing: Regression Tests -*- Autotest -*-
2 # Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
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)
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.
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 AT_BANNER([[GLR Regression Tests]])
21 ## --------------------------- ##
22 ## Badly Collapsed GLR States. ##
23 ## --------------------------- ##
25 AT_SETUP([Badly Collapsed GLR States])
27 AT_DATA_GRAMMAR([glr-regr1.y],
28 [[/* Regression Test: Improper state compression */
29 /* Reported by Scott McPeak */
35 static YYSTYPE exprMerge (YYSTYPE x0, YYSTYPE x1);
37 void yyerror (char const *msg);
44 /* -------- productions ------ */
47 StartSymbol: E { $$=0; } %merge <exprMerge>
50 E: E 'P' E { $$=1; printf("E -> E 'P' E\n"); } %merge <exprMerge>
51 | 'B' { $$=2; printf("E -> 'B'\n"); } %merge <exprMerge>
56 /* ---------- C code ----------- */
59 static YYSTYPE exprMerge (YYSTYPE x0, YYSTYPE x1)
74 yyerror (char const *msg)
76 fprintf (stderr, "%s\n", msg);
88 else if (ch == 'B' || ch == 'P')
94 AT_CHECK([[bison -o glr-regr1.c glr-regr1.y]], 0, [],
95 [glr-regr1.y: conflicts: 1 shift/reduce
97 AT_COMPILE([glr-regr1])
98 AT_CHECK([[echo BPBPB | ./glr-regr1]], 0,
112 ## ------------------------------------------------------------ ##
113 ## Improper handling of embedded actions and $-N in GLR parsers ##
114 ## ------------------------------------------------------------ ##
116 AT_SETUP([Improper handling of embedded actions and dollar(-N) in GLR parsers])
118 AT_DATA_GRAMMAR([glr-regr2a.y],
119 [[/* Regression Test: Improper handling of embedded actions and $-N */
120 /* Reported by S. Eken */
123 #define YYSTYPE char const *
130 void yyerror (char const *);
139 { printf ("Variable: '%s'\n", $2); }
141 | 's' var_list 't' 'e'
142 { printf ("Varlist: '%s'\n", $2); }
143 | 's' var 't' var_printer 'x'
156 char *s = (char *) malloc (strlen ($1) + 1 + strlen ($3) + 1);
165 { printf ("Variable: '%s'\n", $-1); }
176 switch (fscanf (input, " %1[a-z,]", buf)) {
184 if (fscanf (input, "%49s", buf) != 1)
186 if (sizeof buf - 1 <= strlen (buf))
188 s = (char *) malloc (strlen (buf) + 1);
195 yyerror (char const *s)
196 { printf ("%s\n", s);
200 main (int argc, char **argv)
203 if (argc == 2 && !(input = fopen (argv[1], "r"))) return 3;
208 AT_CHECK([[bison -o glr-regr2a.c glr-regr2a.y]], 0, [],
209 [glr-regr2a.y: conflicts: 2 shift/reduce
211 AT_COMPILE([glr-regr2a])
213 AT_CHECK([[echo s VARIABLE_1 t v x q | ./glr-regr2a]], 0,
214 [[Variable: 'VARIABLE_1'
216 AT_CHECK([[echo s VARIABLE_1 , ANOTHER_VARIABLE_2 t e | ./glr-regr2a]], 0,
217 [[Varlist: 'VARIABLE_1,ANOTHER_VARIABLE_2'
219 AT_CHECK([[echo s VARIABLE_3 t v x | ./glr-regr2a]], 0,
220 [[Variable: 'VARIABLE_3'
226 ## ------------------------------------------------------------ ##
227 ## Improper merging of GLR delayed action sets ##
228 ## ------------------------------------------------------------ ##
230 AT_SETUP([Improper merging of GLR delayed action sets])
232 AT_DATA_GRAMMAR([glr-regr3.y],
233 [[/* Regression Test: Improper merging of GLR delayed action sets. */
234 /* Reported by M. Rosien */
240 static int MergeRule (int x0, int x1);
241 static void yyerror(char const * s);
244 #define RULE(x) (1 << (x))
251 %token P1 P2 T1 T2 T3 T4 O1 O2
255 S : P1 T4 O2 NT6 P2 { printf ("Result: %x\n", $4); }
258 NT1 : P1 T1 O1 T2 P2 { $$ = RULE(2); } %merge<MergeRule>
261 NT2 : NT1 { $$ = RULE(3); } %merge<MergeRule>
262 | P1 NT1 O1 T3 P2 { $$ = RULE(4); } %merge<MergeRule>
265 NT3 : T3 { $$ = RULE(5); } %merge<MergeRule>
266 | P1 NT1 O1 T3 P2 { $$ = RULE(6); } %merge<MergeRule>
269 NT4 : NT3 { $$ = RULE(7); } %merge<MergeRule>
270 | NT2 { $$ = RULE(8); } %merge<MergeRule>
271 | P1 NT2 O1 NT3 P2 { $$ = RULE(9); } %merge<MergeRule>
274 NT5 : NT4 { $$ = RULE(10); } %merge<MergeRule>
277 NT6 : P1 NT1 O1 T3 P2 { $$ = RULE(11) | $2; } %merge<MergeRule>
278 | NT5 { $$ = RULE(12) | $1; } %merge<MergeRule>
283 static int MergeRule (int x0, int x1) {
287 static void yyerror(char const * s) {
288 fprintf(stderr,"error: %s\n",s);
293 int P[] = { P1, P2 };
294 int O[] = { O1, O2 };
295 int T[] = { T1, T2, T3, T4 };
300 if (fscanf (input, "%2s", inp) == EOF)
304 case 'p': return P[inp[1] - '1'];
305 case 't': return T[inp[1] - '1'];
306 case 'o': return O[inp[1] - '1'];
311 int main(int argc, char* argv[]) {
313 if (argc == 2 && !(input = fopen (argv[1], "r"))) return 3;
318 AT_CHECK([[bison -o glr-regr3.c glr-regr3.y]], 0, [],
319 [glr-regr3.y: conflicts: 1 shift/reduce, 1 reduce/reduce
321 AT_COMPILE([glr-regr3])
323 AT_CHECK([[echo p1 t4 o2 p1 p1 t1 o1 t2 p2 o1 t3 p2 p2 | ./glr-regr3]], 0,
330 ## ---------------------------------------------------------------------- ##
331 ## Duplicate representation of merged trees ##
332 ## Thanks to Joel E. Denny for this test; see ##
333 ## <http://lists.gnu.org/archive/html/help-bison/2005-07/msg00013.html>. ##
334 ## ---------------------------------------------------------------------- ##
336 AT_SETUP([Duplicate representation of merged trees])
338 AT_DATA_GRAMMAR([glr-regr4.y],
340 %union { char *ptr; }
341 %type <ptr> S A A1 A2 B
348 static char *merge (YYSTYPE, YYSTYPE);
349 static char *make_value (char const *, char const *);
350 static void yyerror (char const *);
351 static int yylex (void);
356 tree: S { printf ("%s\n", $1); } ;
359 A %merge<merge> { $$ = make_value ("S", $1); }
360 | B %merge<merge> { $$ = make_value ("S", $1); }
364 A1 %merge<merge> { $$ = make_value ("A", $1); }
365 | A2 %merge<merge> { $$ = make_value ("A", $1); }
368 A1: 'a' { $$ = make_value ("A1", "'a'"); } ;
369 A2: 'a' { $$ = make_value ("A2", "'a'"); } ;
370 B: 'a' { $$ = make_value ("B", "'a'"); } ;
377 static char const *input = "a";
388 make_value (char const *parent, char const *child)
390 char const format[] = "%s <- %s";
392 (char *) malloc (strlen (parent) + strlen (child) + sizeof format);
393 sprintf (value, format, parent, child);
398 merge (YYSTYPE s1, YYSTYPE s2)
400 char const format[] = "merge{ %s and %s }";
402 (char *) malloc (strlen (s1.ptr) + strlen (s2.ptr) + sizeof format);
403 sprintf (value, format, s1.ptr, s2.ptr);
408 yyerror (char const *msg)
410 fprintf (stderr, "%s\n", msg);
414 AT_CHECK([[bison -o glr-regr4.c glr-regr4.y]], 0, [],
415 [glr-regr4.y: conflicts: 1 reduce/reduce
417 AT_COMPILE([glr-regr4])
419 AT_CHECK([[./glr-regr4]], 0,
420 [[merge{ S <- merge{ A <- A1 <- 'a' and A <- A2 <- 'a' } and S <- B <- 'a' }
426 ## ------------------------------------------------------------------------- ##
427 ## User destructor for unresolved GLR semantic value ##
428 ## Thanks to Joel E. Denny for this test; see ##
429 ## <http://lists.gnu.org/archive/html/bison-patches/2005-08/msg00016.html>. ##
430 ## ------------------------------------------------------------------------- ##
432 AT_SETUP([User destructor for unresolved GLR semantic value])
434 AT_DATA_GRAMMAR([glr-regr5.y],
439 static void yyerror (char const *);
440 static int yylex (void);
441 enum { MAGIC_VALUE = -1057808125 }; /* originally chosen at random */
445 %union { int value; }
449 if ($$ != MAGIC_VALUE)
451 fprintf (stderr, "Bad destructor call.\n");
459 'a' { $$ = MAGIC_VALUE; }
460 | 'a' { $$ = MAGIC_VALUE; }
468 static char const *input = "a";
473 yyerror (char const *msg)
475 fprintf (stderr, "%s\n", msg);
481 return yyparse () != 1;
485 AT_CHECK([[bison -o glr-regr5.c glr-regr5.y]], 0, [],
486 [glr-regr5.y: conflicts: 1 reduce/reduce
488 AT_COMPILE([glr-regr5])
490 AT_CHECK([[./glr-regr5]], 0, [],
497 ## ------------------------------------------------------------------------- ##
498 ## User destructor after an error during a split parse ##
499 ## Thanks to Joel E. Denny for this test; see ##
500 ## <http://lists.gnu.org/archive/html/bison-patches/2005-08/msg00029.html>. ##
501 ## ------------------------------------------------------------------------- ##
503 AT_SETUP([User destructor after an error during a split parse])
505 AT_DATA_GRAMMAR([glr-regr6.y],
510 static void yyerror (char const *);
511 static int yylex (void);
515 %union { int value; }
519 printf ("Destructor called.\n");
531 static char const *input = "a";
536 yyerror (char const *msg)
538 fprintf (stderr, "%s\n", msg);
544 return yyparse () != 1;
548 AT_CHECK([[bison -o glr-regr6.c glr-regr6.y]], 0, [],
549 [glr-regr6.y: conflicts: 1 reduce/reduce
551 AT_COMPILE([glr-regr6])
553 AT_CHECK([[./glr-regr6]], 0,
562 ## ------------------------------------------------------------------------- ##
563 ## Duplicated user destructor for lookahead ##
564 ## Thanks to Joel E. Denny for this test; see ##
565 ## <http://lists.gnu.org/archive/html/bison-patches/2005-08/msg00035.html>. ##
566 ## ------------------------------------------------------------------------- ##
568 AT_SETUP([Duplicated user destructor for lookahead])
570 AT_DATA_GRAMMAR([glr-regr7.y],
575 static void yyerror (char const *);
576 static int yylex (void);
577 #define YYSTACKEXPANDABLE 0
581 %union { int *count; }
586 fprintf (stderr, "Destructor called on same value twice.\n");
604 yylval.count = (int *) malloc (sizeof (int));
607 fprintf (stderr, "Test inconclusive.\n");
615 yyerror (char const *msg)
617 fprintf (stderr, "%s\n", msg);
627 AT_CHECK([[bison -o glr-regr7.c glr-regr7.y]], 0, [],
628 [glr-regr7.y: conflicts: 2 reduce/reduce
630 AT_COMPILE([glr-regr7])
632 AT_CHECK([[./glr-regr7]], 2, [],