1 # Checking GLR Parsing: Regression Tests -*- Autotest -*-
2 # Copyright (C) 2002, 2003, 2005, 2006 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 */
36 static YYSTYPE exprMerge (YYSTYPE x0, YYSTYPE x1);
38 void yyerror (char const *msg);
45 /* -------- productions ------ */
48 StartSymbol: E { $$=0; } %merge <exprMerge>
51 E: E 'P' E { $$=1; printf("E -> E 'P' E\n"); } %merge <exprMerge>
52 | 'B' { $$=2; printf("E -> 'B'\n"); } %merge <exprMerge>
57 /* ---------- C code ----------- */
60 static YYSTYPE exprMerge (YYSTYPE x0, YYSTYPE x1)
75 yyerror (char const *msg)
77 fprintf (stderr, "%s\n", msg);
92 else if (ch == 'B' || ch == 'P')
98 AT_CHECK([[bison -o glr-regr1.c glr-regr1.y]], 0, [],
99 [glr-regr1.y: conflicts: 1 shift/reduce
101 AT_COMPILE([glr-regr1])
102 AT_CHECK([[echo BPBPB | ./glr-regr1]], 0,
116 ## ------------------------------------------------------------ ##
117 ## Improper handling of embedded actions and $-N in GLR parsers ##
118 ## ------------------------------------------------------------ ##
120 AT_SETUP([Improper handling of embedded actions and dollar(-N) in GLR parsers])
122 AT_DATA_GRAMMAR([glr-regr2a.y],
123 [[/* Regression Test: Improper handling of embedded actions and $-N */
124 /* Reported by S. Eken */
127 #define YYSTYPE char *
134 void yyerror (char const *);
143 { printf ("Variable: '%s'\n", $2); }
146 | 's' var_list 't' 'e'
147 { printf ("Varlist: '%s'\n", $2); free ($2); }
148 | 's' var 't' var_printer 'x'
162 char *s = (char *) realloc ($1, strlen ($1) + 1 + strlen ($3) + 1);
171 { printf ("Variable: '%s'\n", $-1); }
184 switch (fscanf (input, " %1[a-z,]", buf)) {
192 if (fscanf (input, "%49s", buf) != 1)
194 if (sizeof buf - 1 <= strlen (buf))
196 s = (char *) malloc (strlen (buf) + 1);
203 yyerror (char const *s)
204 { printf ("%s\n", s);
208 main (int argc, char **argv)
211 if (argc == 2 && !(input = fopen (argv[1], "r"))) return 3;
216 AT_CHECK([[bison -o glr-regr2a.c glr-regr2a.y]], 0, [],
217 [glr-regr2a.y: conflicts: 2 shift/reduce
219 AT_COMPILE([glr-regr2a])
221 AT_CHECK([[echo s VARIABLE_1 t v x q | ./glr-regr2a]], 0,
222 [[Variable: 'VARIABLE_1'
224 AT_CHECK([[echo s VARIABLE_1 , ANOTHER_VARIABLE_2 t e | ./glr-regr2a]], 0,
225 [[Varlist: 'VARIABLE_1,ANOTHER_VARIABLE_2'
227 AT_CHECK([[echo s VARIABLE_3 t v x | ./glr-regr2a]], 0,
228 [[Variable: 'VARIABLE_3'
234 ## ------------------------------------------------------------ ##
235 ## Improper merging of GLR delayed action sets ##
236 ## ------------------------------------------------------------ ##
238 AT_SETUP([Improper merging of GLR delayed action sets])
240 AT_DATA_GRAMMAR([glr-regr3.y],
241 [[/* Regression Test: Improper merging of GLR delayed action sets. */
242 /* Reported by M. Rosien */
249 static int MergeRule (int x0, int x1);
250 static void yyerror (char const * s);
253 #define RULE(x) (1 << (x))
260 %token P1 P2 T1 T2 T3 T4 O1 O2
264 S : P1 T4 O2 NT6 P2 { printf ("Result: %x\n", $4); }
267 NT1 : P1 T1 O1 T2 P2 { $$ = RULE(2); } %merge<MergeRule>
270 NT2 : NT1 { $$ = RULE(3); } %merge<MergeRule>
271 | P1 NT1 O1 T3 P2 { $$ = RULE(4); } %merge<MergeRule>
274 NT3 : T3 { $$ = RULE(5); } %merge<MergeRule>
275 | P1 NT1 O1 T3 P2 { $$ = RULE(6); } %merge<MergeRule>
278 NT4 : NT3 { $$ = RULE(7); } %merge<MergeRule>
279 | NT2 { $$ = RULE(8); } %merge<MergeRule>
280 | P1 NT2 O1 NT3 P2 { $$ = RULE(9); } %merge<MergeRule>
283 NT5 : NT4 { $$ = RULE(10); } %merge<MergeRule>
286 NT6 : P1 NT1 O1 T3 P2 { $$ = RULE(11) | $2; } %merge<MergeRule>
287 | NT5 { $$ = RULE(12) | $1; } %merge<MergeRule>
292 static int MergeRule (int x0, int x1) {
296 static void yyerror(char const * s) {
297 fprintf(stderr,"error: %s\n",s);
302 int P[] = { P1, P2 };
303 int O[] = { O1, O2 };
304 int T[] = { T1, T2, T3, T4 };
311 if (fscanf (input, "%2s", inp) == EOF)
315 case 'p': return P[inp[1] - '1'];
316 case 't': return T[inp[1] - '1'];
317 case 'o': return O[inp[1] - '1'];
322 int main(int argc, char* argv[]) {
324 if (argc == 2 && !(input = fopen (argv[1], "r"))) return 3;
329 AT_CHECK([[bison -o glr-regr3.c glr-regr3.y]], 0, [],
330 [glr-regr3.y: conflicts: 1 shift/reduce, 1 reduce/reduce
332 AT_COMPILE([glr-regr3])
334 AT_CHECK([[echo p1 t4 o2 p1 p1 t1 o1 t2 p2 o1 t3 p2 p2 | ./glr-regr3]], 0,
341 ## ------------------------------------------------------------------------- ##
342 ## Duplicate representation of merged trees. See ##
343 ## <http://lists.gnu.org/archive/html/help-bison/2005-07/msg00013.html>. ##
344 ## ------------------------------------------------------------------------- ##
346 AT_SETUP([Duplicate representation of merged trees])
348 AT_DATA_GRAMMAR([glr-regr4.y],
350 %union { char *ptr; }
351 %type <ptr> S A A1 A2 B
358 static char *merge (YYSTYPE, YYSTYPE);
359 static char *make_value (char const *, char const *);
360 static void yyerror (char const *);
361 static int yylex (void);
366 tree: S { printf ("%s\n", $1); } ;
369 A %merge<merge> { $$ = make_value ("S", $1); }
370 | B %merge<merge> { $$ = make_value ("S", $1); }
374 A1 %merge<merge> { $$ = make_value ("A", $1); }
375 | A2 %merge<merge> { $$ = make_value ("A", $1); }
378 A1: 'a' { $$ = make_value ("A1", "'a'"); } ;
379 A2: 'a' { $$ = make_value ("A2", "'a'"); } ;
380 B: 'a' { $$ = make_value ("B", "'a'"); } ;
387 static char const input[] = "a";
388 static size_t toknum;
389 if (! (toknum < sizeof input))
391 return input[toknum++];
401 make_value (char const *parent, char const *child)
403 char const format[] = "%s <- %s";
405 (char *) malloc (strlen (parent) + strlen (child) + sizeof format);
406 sprintf (value, format, parent, child);
411 merge (YYSTYPE s1, YYSTYPE s2)
413 char const format[] = "merge{ %s and %s }";
415 (char *) malloc (strlen (s1.ptr) + strlen (s2.ptr) + sizeof format);
416 sprintf (value, format, s1.ptr, s2.ptr);
421 yyerror (char const *msg)
423 fprintf (stderr, "%s\n", msg);
427 AT_CHECK([[bison -o glr-regr4.c glr-regr4.y]], 0, [],
428 [glr-regr4.y: conflicts: 1 reduce/reduce
430 AT_COMPILE([glr-regr4])
432 AT_CHECK([[./glr-regr4]], 0,
433 [[merge{ S <- merge{ A <- A1 <- 'a' and A <- A2 <- 'a' } and S <- B <- 'a' }
439 ## -------------------------------------------------------------------------- ##
440 ## User destructor for unresolved GLR semantic value. See ##
441 ## <http://lists.gnu.org/archive/html/bison-patches/2005-08/msg00016.html>. ##
442 ## -------------------------------------------------------------------------- ##
444 AT_SETUP([User destructor for unresolved GLR semantic value])
446 AT_DATA_GRAMMAR([glr-regr5.y],
451 static void yyerror (char const *);
452 static int yylex (void);
453 enum { MAGIC_VALUE = -1057808125 }; /* originally chosen at random */
457 %union { int value; }
461 if ($$ != MAGIC_VALUE)
463 fprintf (stderr, "Bad destructor call.\n");
471 'a' { $$ = MAGIC_VALUE; }
472 | 'a' { $$ = MAGIC_VALUE; }
480 static char const input[] = "a";
481 static size_t toknum;
482 if (! (toknum < sizeof input))
484 return input[toknum++];
488 yyerror (char const *msg)
490 fprintf (stderr, "%s\n", msg);
496 return yyparse () != 1;
500 AT_CHECK([[bison -o glr-regr5.c glr-regr5.y]], 0, [],
501 [glr-regr5.y: conflicts: 1 reduce/reduce
503 AT_COMPILE([glr-regr5])
505 AT_CHECK([[./glr-regr5]], 0, [],
512 ## -------------------------------------------------------------------------- ##
513 ## User destructor after an error during a split parse. See ##
514 ## <http://lists.gnu.org/archive/html/bison-patches/2005-08/msg00029.html>. ##
515 ## -------------------------------------------------------------------------- ##
517 AT_SETUP([User destructor after an error during a split parse])
519 AT_DATA_GRAMMAR([glr-regr6.y],
524 static void yyerror (char const *);
525 static int yylex (void);
529 %union { int value; }
533 printf ("Destructor called.\n");
545 static char const input[] = "a";
546 static size_t toknum;
547 if (! (toknum < sizeof input))
549 return input[toknum++];
553 yyerror (char const *msg)
555 fprintf (stderr, "%s\n", msg);
561 return yyparse () != 1;
565 AT_CHECK([[bison -o glr-regr6.c glr-regr6.y]], 0, [],
566 [glr-regr6.y: conflicts: 1 reduce/reduce
568 AT_COMPILE([glr-regr6])
570 AT_CHECK([[./glr-regr6]], 0,
579 ## ------------------------------------------------------------------------- ##
580 ## Duplicated user destructor for lookahead. See ##
581 ## <http://lists.gnu.org/archive/html/bison-patches/2005-08/msg00035.html>. ##
582 ## ------------------------------------------------------------------------- ##
584 AT_SETUP([Duplicated user destructor for lookahead])
586 AT_DATA_GRAMMAR([glr-regr7.y],
591 static void yyerror (char const *);
592 static int yylex (void);
593 #define YYSTACKEXPANDABLE 0
597 %union { int *count; }
602 fprintf (stderr, "Destructor called on same value twice.\n");
620 yylval.count = (int *) malloc (sizeof (int));
623 fprintf (stderr, "Test inconclusive.\n");
631 yyerror (char const *msg)
633 fprintf (stderr, "%s\n", msg);
643 AT_CHECK([[bison -o glr-regr7.c glr-regr7.y]], 0, [],
644 [glr-regr7.y: conflicts: 2 reduce/reduce
646 AT_COMPILE([glr-regr7])
648 AT_CHECK([[./glr-regr7]], 2, [],
655 ## ------------------------------------------------------------------------- ##
656 ## Incorrect default location for empty right-hand sides. Adapted from bug ##
657 ## report by Claudia Hermann. ##
658 ## See http://lists.gnu.org/archive/html/bug-bison/2005-10/msg00069.html and ##
659 ## http://lists.gnu.org/archive/html/bug-bison/2005-10/msg00072.html ##
660 ## ------------------------------------------------------------------------- ##
662 AT_SETUP([Incorrectly initialized location for empty right-hand side in GLR])
664 AT_DATA_GRAMMAR([glr-regr8.y],
669 static void yyerror (char const *);
670 static int yylex (void);
671 static void yyerror (char const *msg);
683 PortClause : T_PORT InterfaceDeclaration T_PORT
684 { printf("%d/%d - %d/%d - %d/%d\n",
685 @1.first_column, @1.last_column,
686 @2.first_column, @2.last_column,
687 @3.first_column, @3.last_column); }
690 InterfaceDeclaration : OptConstantWord %dprec 1
691 | OptSignalWord %dprec 2
694 OptConstantWord : /* empty */
698 OptSignalWord : /* empty */
699 { printf("empty: %d/%d\n", @$.first_column, @$.last_column); }
706 yyerror (char const *msg)
708 fprintf (stderr, "%s\n", msg);
721 yylloc.first_column = 1;
722 yylloc.last_column = 9;
725 yylloc.first_column = 13;
726 yylloc.last_column = 17;
741 AT_CHECK([[bison -o glr-regr8.c glr-regr8.y]], 0, [],
742 [glr-regr8.y: conflicts: 1 reduce/reduce
744 AT_COMPILE([glr-regr8])
746 AT_CHECK([[./glr-regr8]], 0,
755 ## ------------------------------------------------------------------------- ##
756 ## No users destructors if stack 0 deleted. See ##
757 ## <http://lists.gnu.org/archive/html/bison-patches/2005-09/msg00109.html>. ##
758 ## ------------------------------------------------------------------------- ##
760 AT_SETUP([No users destructors if stack 0 deleted])
762 AT_DATA_GRAMMAR([glr-regr9.y],
767 static void yyerror (char const *);
768 static int yylex (void);
769 # define YYSTACKEXPANDABLE 0
770 static int tokens = 0;
771 static int destructors = 0;
776 %union { int dummy; }
786 ambig0 'a' { destructors += 2; USE ($2); }
787 | ambig1 start { destructors += 1; }
788 | ambig2 start { destructors += 1; }
805 yyerror (char const *msg)
807 fprintf (stderr, "%s\n", msg);
814 exit_status = yyparse ();
815 if (tokens != destructors)
817 fprintf (stderr, "Tokens = %d, Destructors = %d\n", tokens, destructors);
824 AT_CHECK([[bison -o glr-regr9.c glr-regr9.y]], 0, [],
825 [glr-regr9.y: conflicts: 1 reduce/reduce
827 AT_COMPILE([glr-regr9])
829 AT_CHECK([[./glr-regr9]], 0, [],
836 ## ------------------------------------------------------------------------- ##
837 ## Corrupted semantic options if user action cuts parse. ##
838 ## ------------------------------------------------------------------------- ##
840 AT_SETUP([Corrupted semantic options if user action cuts parse])
842 AT_DATA_GRAMMAR([glr-regr10.y],
847 static void yyerror (char const *);
848 static int yylex (void);
849 #define GARBAGE_SIZE 50
850 static char garbage[GARBAGE_SIZE];
854 %union { char *ptr; }
860 %dprec 2 { $$ = garbage; YYACCEPT; }
861 | %dprec 1 { $$ = garbage; YYACCEPT; }
867 yyerror (char const *msg)
869 fprintf (stderr, "%s\n", msg);
885 for (i = 0; i < GARBAGE_SIZE; i+=1)
891 AT_CHECK([[bison -o glr-regr10.c glr-regr10.y]], 0, [],
892 [glr-regr10.y: conflicts: 1 reduce/reduce
894 AT_COMPILE([glr-regr10])
896 AT_CHECK([[./glr-regr10]], 0, [], [])
901 ## ------------------------------------------------------------------------- ##
902 ## Undesirable destructors if user action cuts parse. ##
903 ## ------------------------------------------------------------------------- ##
905 AT_SETUP([Undesirable destructors if user action cuts parse])
907 AT_DATA_GRAMMAR([glr-regr11.y],
911 static void yyerror (char const *);
912 static int yylex (void);
913 static int destructors = 0;
918 %union { int dummy; }
920 %destructor { destructors += 1; } 'a'
925 'a' %dprec 2 { USE ($1); destructors += 1; YYACCEPT; }
926 | 'a' %dprec 1 { USE ($1); destructors += 1; YYACCEPT; }
932 yyerror (char const *msg)
934 fprintf (stderr, "%s\n", msg);
940 static char const input[] = "a";
941 static size_t toknum;
942 if (! (toknum < sizeof input))
944 return input[toknum++];
950 int exit_status = yyparse ();
951 if (destructors != 1)
953 fprintf (stderr, "Destructor calls: %d\n", destructors);
960 AT_CHECK([[bison -o glr-regr11.c glr-regr11.y]], 0, [],
961 [glr-regr11.y: conflicts: 1 reduce/reduce
963 AT_COMPILE([glr-regr11])
965 AT_CHECK([[./glr-regr11]], 0, [], [])
970 ## ------------------------------------------------------------------------- ##
971 ## Leaked semantic values if user action cuts parse. ##
972 ## ------------------------------------------------------------------------- ##
974 AT_SETUP([Leaked semantic values if user action cuts parse])
976 AT_DATA_GRAMMAR([glr-regr12.y],
979 %union { int dummy; }
980 %token PARENT_RHS_AFTER
981 %type <dummy> parent_rhs_before merged PARENT_RHS_AFTER
982 %destructor { parent_rhs_before_value = 0; } parent_rhs_before
983 %destructor { merged_value = 0; } merged
984 %destructor { parent_rhs_after_value = 0; } PARENT_RHS_AFTER
988 static int merge (YYSTYPE, YYSTYPE);
989 static void yyerror (char const *);
990 static int yylex (void);
991 static int parent_rhs_before_value = 0;
992 static int merged_value = 0;
993 static int parent_rhs_after_value = 0;
1007 parent_rhs_after_value = 0;
1012 parent_rhs_before merged PARENT_RHS_AFTER {
1014 parent_rhs_before_value = 0;
1016 parent_rhs_after_value = 0;
1023 parent_rhs_before_value = 1;
1032 | cut %merge<merge> {
1038 cut: { YYACCEPT; } ;
1043 merge (YYSTYPE s1, YYSTYPE s2)
1046 char dummy = s1.dummy + s2.dummy;
1051 yyerror (char const *msg)
1053 fprintf (stderr, "%s\n", msg);
1059 static int const input[] = { PARENT_RHS_AFTER, 0 };
1060 static size_t toknum;
1061 if (! (toknum < sizeof input / sizeof *input))
1063 if (input[toknum] == PARENT_RHS_AFTER)
1064 parent_rhs_after_value = 1;
1065 return input[toknum++];
1071 int exit_status = yyparse ();
1072 if (parent_rhs_before_value)
1074 fprintf (stderr, "`parent_rhs_before' destructor not called.\n");
1079 fprintf (stderr, "`merged' destructor not called.\n");
1082 if (parent_rhs_after_value)
1084 fprintf (stderr, "`PARENT_RHS_AFTER' destructor not called.\n");
1091 AT_CHECK([[bison -o glr-regr12.c glr-regr12.y]], 0, [],
1092 [glr-regr12.y: conflicts: 1 shift/reduce, 1 reduce/reduce
1094 AT_COMPILE([glr-regr12])
1096 AT_CHECK([[./glr-regr12]], 0, [], [])
1101 ## ------------------------------------------------------------------------- ##
1102 ## Incorrect lookahead during deterministic GLR. See ##
1103 ## <http://lists.gnu.org/archive/html/help-bison/2005-07/msg00017.html> and ##
1104 ## <http://lists.gnu.org/archive/html/bison-patches/2006-01/msg00060.html>. ##
1105 ## ------------------------------------------------------------------------- ##
1107 AT_SETUP([Incorrect lookahead during deterministic GLR])
1109 AT_DATA_GRAMMAR([glr-regr13.y],
1112 - Defaulted state with initial yychar: yychar == YYEMPTY.
1113 - Nondefaulted state: yychar != YYEMPTY.
1114 - Defaulted state after lookahead: yychar != YYEMPTY.
1115 - Defaulted state after shift: yychar == YYEMPTY.
1116 - User action changing the lookahead. */
1120 static void yyerror (char const *);
1121 static int yylex (void);
1122 static void print_lookahead (char const *);
1126 %union { char value; }
1127 %type <value> 'a' 'b'
1134 defstate_init defstate_shift 'b' change_lookahead 'a' {
1136 print_lookahead ("start <- defstate_init defstate_shift 'b'");
1141 print_lookahead ("defstate_init <- empty string");
1145 nondefstate defstate_look 'a' {
1147 print_lookahead ("defstate_shift <- nondefstate defstate_look 'a'");
1152 print_lookahead ("defstate_look <- empty string");
1157 print_lookahead ("nondefstate <- empty string");
1161 print_lookahead ("nondefstate <- 'b'");
1173 yyerror (char const *msg)
1175 fprintf (stderr, "%s\n", msg);
1181 static char const input[] = "ab";
1182 static size_t toknum;
1183 if (! (toknum < sizeof input))
1185 yylloc.first_line = yylloc.last_line = 1;
1186 yylloc.first_column = yylloc.last_column = toknum + 1;
1187 yylval.value = input[toknum] + 'A' - 'a';
1188 return input[toknum++];
1192 print_lookahead (char const *reduction)
1194 printf ("%s:\n yychar=", reduction);
1195 if (yychar == YYEMPTY)
1197 else if (yychar == YYEOF)
1201 printf ("'%c', yylval='", yychar);
1202 if (yylval.value > ' ')
1203 printf ("%c", yylval.value);
1204 printf ("', yylloc=(%d,%d),(%d,%d)",
1205 yylloc.first_line, yylloc.first_column,
1206 yylloc.last_line, yylloc.last_column);
1214 yychar = '#'; /* Not a token in the grammar. */
1220 AT_CHECK([[bison -o glr-regr13.c glr-regr13.y]], 0, [], [])
1221 AT_COMPILE([glr-regr13])
1223 AT_CHECK([[./glr-regr13]], 0,
1224 [defstate_init <- empty string:
1226 nondefstate <- empty string:
1227 yychar='a', yylval='A', yylloc=(1,1),(1,1)
1228 defstate_look <- empty string:
1229 yychar='a', yylval='A', yylloc=(1,1),(1,1)
1230 defstate_shift <- nondefstate defstate_look 'a':
1232 start <- defstate_init defstate_shift 'b':
1239 ## ------------------------------------------------------------------------- ##
1240 ## Incorrect lookahead during nondeterministic GLR. ##
1241 ## ------------------------------------------------------------------------- ##
1243 AT_SETUP([Incorrect lookahead during nondeterministic GLR])
1245 AT_DATA_GRAMMAR([glr-regr14.y],
1248 - Conflicting actions (split-off parse, which copies lookahead need,
1249 which is necessarily yytrue) and nonconflicting actions (non-split-off
1250 parse) for nondefaulted state: yychar != YYEMPTY.
1251 - Merged deferred actions (lookahead need and RHS from different stack
1252 than the target state) and nonmerged deferred actions (same stack).
1253 - Defaulted state after lookahead: yychar != YYEMPTY.
1254 - Defaulted state after shift: yychar == YYEMPTY.
1255 - yychar != YYEMPTY but lookahead need is yyfalse (a previous stack has
1256 seen the lookahead but current stack has not).
1257 - Exceeding stack capacity (stack explosion), and thus reallocating
1258 lookahead need array.
1259 Note that it does not seem possible to see the initial yychar value during
1260 nondeterministic operation since:
1261 - In order to preserve the initial yychar, only defaulted states may be
1263 - If only defaulted states are entered, there are no conflicts, so
1264 nondeterministic operation does not start. */
1266 %union { char value; }
1271 static void yyerror (char const *);
1272 static int yylex (void);
1273 static void print_lookahead (char const *);
1274 static char merge (union YYSTYPE, union YYSTYPE);
1278 %type <value> 'a' 'b' 'c' 'd' stack_explosion
1285 merge 'c' stack_explosion {
1287 print_lookahead ("start <- merge 'c' stack_explosion");
1291 /* When merging the 2 deferred actions, the lookahead needs are different. */
1293 nonconflict1 'a' 'b' nonconflict2 %dprec 1 {
1295 print_lookahead ("merge <- nonconflict1 'a' 'b' nonconflict2");
1297 | conflict defstate_look 'a' nonconflict2 'b' defstate_shift %dprec 2 {
1299 print_lookahead ("merge <- conflict defstate_look 'a' nonconflict2 'b'"
1306 print_lookahead ("nonconflict1 <- empty string");
1311 print_lookahead ("nonconflict2 <- empty string");
1315 print_lookahead ("nonconflict2 <- 'a'");
1320 print_lookahead ("conflict <- empty string");
1325 print_lookahead ("defstate_look <- empty string");
1329 /* yychar != YYEMPTY but lookahead need is yyfalse. */
1332 print_lookahead ("defstate_shift <- empty string");
1338 | alt1 stack_explosion %merge<merge> { $$ = $2; }
1339 | alt2 stack_explosion %merge<merge> { $$ = $2; }
1340 | alt3 stack_explosion %merge<merge> { $$ = $2; }
1345 if (yychar != 'd' && yychar != YYEOF)
1347 fprintf (stderr, "Incorrect lookahead during stack explosion.\n");
1354 if (yychar != 'd' && yychar != YYEOF)
1356 fprintf (stderr, "Incorrect lookahead during stack explosion.\n");
1363 if (yychar != 'd' && yychar != YYEOF)
1365 fprintf (stderr, "Incorrect lookahead during stack explosion.\n");
1371 if (yychar != YYEMPTY)
1374 "Found lookahead where shouldn't during stack explosion.\n");
1382 yyerror (char const *msg)
1384 fprintf (stderr, "%s\n", msg);
1390 static char const input[] = "abcdddd";
1391 static size_t toknum;
1392 if (! (toknum < sizeof input))
1394 yylloc.first_line = yylloc.last_line = 1;
1395 yylloc.first_column = yylloc.last_column = toknum + 1;
1396 yylval.value = input[toknum] + 'A' - 'a';
1397 return input[toknum++];
1401 print_lookahead (char const *reduction)
1403 printf ("%s:\n yychar=", reduction);
1404 if (yychar == YYEMPTY)
1406 else if (yychar == YYEOF)
1410 printf ("'%c', yylval='", yychar);
1411 if (yylval.value > ' ')
1412 printf ("%c", yylval.value);
1413 printf ("', yylloc=(%d,%d),(%d,%d)",
1414 yylloc.first_line, yylloc.first_column,
1415 yylloc.last_line, yylloc.last_column);
1421 merge (union YYSTYPE s1, union YYSTYPE s2)
1423 char dummy = s1.value + s2.value;
1430 yychar = '#'; /* Not a token in the grammar. */
1436 AT_CHECK([[bison -o glr-regr14.c glr-regr14.y]], 0, [],
1437 [glr-regr14.y: conflicts: 3 reduce/reduce
1439 AT_COMPILE([glr-regr14])
1441 AT_CHECK([[./glr-regr14]], 0,
1442 [conflict <- empty string:
1443 yychar='a', yylval='A', yylloc=(1,1),(1,1)
1444 defstate_look <- empty string:
1445 yychar='a', yylval='A', yylloc=(1,1),(1,1)
1446 nonconflict2 <- empty string:
1447 yychar='b', yylval='B', yylloc=(1,2),(1,2)
1448 defstate_shift <- empty string:
1450 merge <- conflict defstate_look 'a' nonconflict2 'b' defstate_shift:
1452 start <- merge 'c' stack_explosion:
1459 ## ------------------------------------------------------------------------- ##
1460 ## Leaked semantic values when reporting ambiguity. ##
1461 ## ------------------------------------------------------------------------- ##
1463 AT_SETUP([Leaked semantic values when reporting ambiguity])
1465 AT_DATA_GRAMMAR([glr-regr15.y],
1468 %destructor { parent_rhs_before_value = 0; } parent_rhs_before
1471 # include <stdlib.h>
1472 static void yyerror (char const *);
1473 static int yylex (void);
1474 static int parent_rhs_before_value = 0;
1485 /* This stack must be merged into the other stacks *last* (added at the
1486 beginning of the semantic options list) so that yyparse will choose to clean
1487 it up rather than the tree for which some semantic actions have been
1488 performed. Thus, if yyreportAmbiguity longjmp's to yyparse, the values from
1489 those other trees are not cleaned up. */
1493 parent_rhs_before ambiguity {
1495 parent_rhs_before_value = 0;
1502 parent_rhs_before_value = 1;
1506 ambiguity: ambiguity1 | ambiguity2 ;
1513 yyerror (char const *msg)
1515 fprintf (stderr, "%s\n", msg);
1530 int exit_status = yyparse () != 1;
1531 if (parent_rhs_before_value)
1533 fprintf (stderr, "`parent_rhs_before' destructor not called.\n");
1540 AT_CHECK([[bison -o glr-regr15.c glr-regr15.y]], 0, [],
1541 [glr-regr15.y: conflicts: 2 reduce/reduce
1543 AT_COMPILE([glr-regr15])
1545 AT_CHECK([[./glr-regr15]], 0, [],
1546 [syntax is ambiguous
1552 ## ------------------------------------------------------------------------- ##
1553 ## Leaked lookahead after nondeterministic parse syntax error. ##
1554 ## ------------------------------------------------------------------------- ##
1556 AT_SETUP([Leaked lookahead after nondeterministic parse syntax error])
1557 AT_DATA_GRAMMAR([glr-regr16.y],
1560 %destructor { lookahead_value = 0; } 'b'
1563 # include <stdlib.h>
1564 static void yyerror (char const *);
1565 static int yylex (void);
1566 static int lookahead_value = 0;
1572 start: alt1 'a' | alt2 'a' ;
1579 yyerror (char const *msg)
1581 fprintf (stderr, "%s\n", msg);
1587 static char const input[] = "ab";
1588 static size_t toknum;
1589 if (! (toknum < sizeof input))
1591 if (input[toknum] == 'b')
1592 lookahead_value = 1;
1593 return input[toknum++];
1599 int exit_status = yyparse () != 1;
1600 if (lookahead_value)
1602 fprintf (stderr, "Lookahead destructor not called.\n");
1609 AT_CHECK([[bison -o glr-regr16.c glr-regr16.y]], 0, [],
1610 [glr-regr16.y: conflicts: 1 reduce/reduce
1612 AT_COMPILE([glr-regr16])
1614 AT_CHECK([[./glr-regr16]], 0, [],
1621 ## ------------------------------------------------------------------------- ##
1622 ## Uninitialized location when reporting ambiguity. ##
1623 ## ------------------------------------------------------------------------- ##
1625 AT_SETUP([Uninitialized location when reporting ambiguity])
1626 AT_DATA_GRAMMAR([glr-regr17.y],
1633 %union { int dummy; }
1636 static void yyerror (YYLTYPE *, char const *);
1637 static int yylex (YYSTYPE *, YYLTYPE *);
1642 @$.first_column = 1;
1649 /* Tests the case of an empty RHS that has inherited the location of the
1650 previous nonterminal, which is unresolved. That location is reported as the
1651 last position of the ambiguity. */
1652 start: ambig1 empty1 | ambig2 empty2 ;
1654 /* Tests multiple levels of yyresolveLocations recursion. */
1655 ambig1: sub_ambig1 | sub_ambig2 ;
1656 ambig2: sub_ambig1 | sub_ambig2 ;
1658 /* Tests the case of a non-empty RHS as well as the case of an empty RHS that
1659 has inherited the initial location. The empty RHS's location is reported as
1660 the first position in the ambiguity. */
1661 sub_ambig1: empty1 'a' 'b' ;
1662 sub_ambig2: empty2 'a' 'b' ;
1669 yyerror (YYLTYPE *locp, char const *msg)
1671 fprintf (stderr, "Error at %d.%d-%d.%d: %s.\n", locp->first_line,
1672 locp->first_column, locp->last_line, locp->last_column, msg);
1676 yylex (YYSTYPE *lvalp, YYLTYPE *llocp)
1678 static char const input[] = "ab";
1679 static size_t toknum;
1680 if (! (toknum < sizeof input))
1683 llocp->first_line = llocp->last_line = 2;
1684 llocp->first_column = toknum + 1;
1685 llocp->last_column = llocp->first_column + 1;
1686 return input[toknum++];
1692 return yyparse () != 1;
1696 AT_CHECK([[bison -o glr-regr17.c glr-regr17.y]], 0, [],
1697 [glr-regr17.y: conflicts: 3 reduce/reduce
1699 AT_COMPILE([glr-regr17])
1701 AT_CHECK([[./glr-regr17]], 0, [],
1702 [Error at 1.1-2.3: syntax is ambiguous.
1708 ## -------------------------------------------------------------##
1709 ## Missed %merge type warnings when LHS type is declared later. ##
1710 ## -------------------------------------------------------------##
1712 AT_SETUP([Missed %merge type warnings when LHS type is declared later])
1713 AT_DATA_GRAMMAR([glr-regr18.y],
1718 static void yyerror (char const *);
1719 static int yylex ();
1730 sym1: sym2 %merge<merge> { $$ = $1; } ;
1731 sym2: sym3 %merge<merge> { $$ = $1; } ;
1732 sym3: %merge<merge> { $$ = 0; } ;
1741 yyerror (char const *msg)
1743 fprintf (stderr, "%s\n", msg);
1762 AT_CHECK([[bison -o glr-regr18.c glr-regr18.y]], 1, [],
1763 [glr-regr18.y:26.18-24: result type clash on merge function `merge': <type2> != <type1>
1764 glr-regr18.y:25.18-24: previous declaration
1765 glr-regr18.y:27.13-19: result type clash on merge function `merge': <type3> != <type2>
1766 glr-regr18.y:26.18-24: previous declaration