1 # Checking GLR Parsing: Regression Tests -*- Autotest -*-
3 # Copyright (C) 2002-2003, 2005-2007, 2009-2012 Free Software
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 AT_BANNER([[GLR Regression Tests]])
21 ## --------------------------- ##
22 ## Badly Collapsed GLR States. ##
23 ## --------------------------- ##
25 AT_SETUP([Badly Collapsed GLR States])
27 AT_BISON_OPTION_PUSHDEFS
28 AT_DATA_GRAMMAR([glr-regr1.y],
29 [[/* Regression Test: Improper state compression */
30 /* Reported by Scott McPeak */
37 static YYSTYPE exprMerge (YYSTYPE x0, YYSTYPE x1);
46 /* -------- productions ------ */
49 StartSymbol: E { $$=0; } %merge <exprMerge>
52 E: E 'P' E { $$=1; printf("E -> E 'P' E\n"); } %merge <exprMerge>
53 | 'B' { $$=2; printf("E -> 'B'\n"); } %merge <exprMerge>
58 /* ---------- C code ----------- */
61 static YYSTYPE exprMerge (YYSTYPE x0, YYSTYPE x1)
88 else if (ch == 'B' || ch == 'P')
93 AT_BISON_OPTION_POPDEFS
95 AT_BISON_CHECK([[-o glr-regr1.c glr-regr1.y]], 0, [],
96 [glr-regr1.y: conflicts: 1 shift/reduce
98 AT_COMPILE([glr-regr1])
99 AT_PARSER_CHECK([[echo BPBPB | ./glr-regr1]], 0,
113 ## ------------------------------------------------------------ ##
114 ## Improper handling of embedded actions and $-N in GLR parsers ##
115 ## ------------------------------------------------------------ ##
117 AT_SETUP([Improper handling of embedded actions and dollar(-N) in GLR parsers])
119 AT_BISON_OPTION_PUSHDEFS
120 AT_DATA_GRAMMAR([glr-regr2a.y],
121 [[/* Regression Test: Improper handling of embedded actions and $-N */
122 /* Reported by S. Eken */
125 #define YYSTYPE char *
141 { printf ("Variable: '%s'\n", $2); }
144 | 's' var_list 't' 'e'
145 { printf ("Varlist: '%s'\n", $2); free ($2); }
146 | 's' var 't' var_printer 'x'
160 char *s = (char *) realloc ($1, strlen ($1) + 1 + strlen ($3) + 1);
169 { printf ("Variable: '%s'\n", $-1); }
182 switch (fscanf (input, " %1[a-z,]", buf))
191 if (fscanf (input, "%49s", buf) != 1)
193 if (sizeof buf - 1 <= strlen (buf))
195 s = (char *) malloc (strlen (buf) + 1);
202 main (int argc, char **argv)
205 if (argc == 2 && !(input = fopen (argv[1], "r"))) return 3;
209 AT_BISON_OPTION_POPDEFS
211 AT_BISON_CHECK([[-o glr-regr2a.c glr-regr2a.y]], 0, [],
212 [glr-regr2a.y: conflicts: 2 shift/reduce
214 AT_COMPILE([glr-regr2a])
216 AT_PARSER_CHECK([[echo s VARIABLE_1 t v x q | ./glr-regr2a]], 0,
217 [[Variable: 'VARIABLE_1'
219 AT_PARSER_CHECK([[echo s VARIABLE_1 , ANOTHER_VARIABLE_2 t e | ./glr-regr2a]],
221 [[Varlist: 'VARIABLE_1,ANOTHER_VARIABLE_2'
223 AT_PARSER_CHECK([[echo s VARIABLE_3 t v x | ./glr-regr2a]], 0,
224 [[Variable: 'VARIABLE_3'
230 ## ------------------------------------------------------------ ##
231 ## Improper merging of GLR delayed action sets ##
232 ## ------------------------------------------------------------ ##
234 AT_SETUP([Improper merging of GLR delayed action sets])
236 AT_BISON_OPTION_PUSHDEFS
237 AT_DATA_GRAMMAR([glr-regr3.y],
238 [[/* Regression Test: Improper merging of GLR delayed action sets. */
239 /* Reported by M. Rosien */
246 static int MergeRule (int x0, int x1);
250 #define RULE(x) (1 << (x))
257 %token P1 P2 T1 T2 T3 T4 O1 O2
261 S : P1 T4 O2 NT6 P2 { printf ("Result: %x\n", $4); }
264 NT1 : P1 T1 O1 T2 P2 { $$ = RULE(2); } %merge<MergeRule>
267 NT2 : NT1 { $$ = RULE(3); } %merge<MergeRule>
268 | P1 NT1 O1 T3 P2 { $$ = RULE(4); } %merge<MergeRule>
271 NT3 : T3 { $$ = RULE(5); } %merge<MergeRule>
272 | P1 NT1 O1 T3 P2 { $$ = RULE(6); } %merge<MergeRule>
275 NT4 : NT3 { $$ = RULE(7); } %merge<MergeRule>
276 | NT2 { $$ = RULE(8); } %merge<MergeRule>
277 | P1 NT2 O1 NT3 P2 { $$ = RULE(9); } %merge<MergeRule>
280 NT5 : NT4 { $$ = RULE(10); } %merge<MergeRule>
283 NT6 : P1 NT1 O1 T3 P2 { $$ = RULE(11) | $2; } %merge<MergeRule>
284 | NT5 { $$ = RULE(12) | $1; } %merge<MergeRule>
290 MergeRule (int x0, int x1)
296 FILE *input = YY_NULL;
298 int P[] = { P1, P2 };
299 int O[] = { O1, O2 };
300 int T[] = { T1, T2, T3, T4 };
307 if (fscanf (input, "%2s", inp) == EOF)
311 case 'p': return P[inp[1] - '1'];
312 case 't': return T[inp[1] - '1'];
313 case 'o': return O[inp[1] - '1'];
319 main(int argc, char* argv[])
322 if (argc == 2 && !(input = fopen (argv[1], "r"))) return 3;
326 AT_BISON_OPTION_POPDEFS
328 AT_BISON_CHECK([[-o glr-regr3.c glr-regr3.y]], 0, [],
329 [glr-regr3.y: conflicts: 1 shift/reduce, 1 reduce/reduce
331 AT_COMPILE([glr-regr3])
333 AT_PARSER_CHECK([[echo p1 t4 o2 p1 p1 t1 o1 t2 p2 o1 t3 p2 p2 | ./glr-regr3]],
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_BISON_OPTION_PUSHDEFS
349 AT_DATA_GRAMMAR([glr-regr4.y],
351 %union { char *ptr; }
352 %type <ptr> S A A1 A2 B
359 static char *merge (YYSTYPE, YYSTYPE);
360 static char *make_value (char const *, char const *);
363 static char *ptrs[100];
364 static char **ptrs_next = ptrs;
369 tree: S { printf ("%s\n", $1); } ;
372 A %merge<merge> { $$ = make_value ("S", $1); }
373 | B %merge<merge> { $$ = make_value ("S", $1); }
377 A1 %merge<merge> { $$ = make_value ("A", $1); }
378 | A2 %merge<merge> { $$ = make_value ("A", $1); }
381 A1: 'a' { $$ = make_value ("A1", "'a'"); } ;
382 A2: 'a' { $$ = make_value ("A2", "'a'"); } ;
383 B: 'a' { $$ = make_value ("B", "'a'"); } ;
387 ]AT_YYLEX_DEFINE([a])[
392 int status = yyparse ();
393 while (ptrs_next != ptrs)
399 make_value (char const *parent, char const *child)
401 char const format[] = "%s <- %s";
402 char *value = *ptrs_next++ =
403 (char *) malloc (strlen (parent) + strlen (child) + sizeof format);
404 sprintf (value, format, parent, child);
409 merge (YYSTYPE s1, YYSTYPE s2)
411 char const format[] = "merge{ %s and %s }";
412 char *value = *ptrs_next++ =
413 (char *) malloc (strlen (s1.ptr) + strlen (s2.ptr) + sizeof format);
414 sprintf (value, format, s1.ptr, s2.ptr);
418 AT_BISON_OPTION_POPDEFS
420 AT_BISON_CHECK([[-o glr-regr4.c glr-regr4.y]], 0, [],
421 [glr-regr4.y: conflicts: 1 reduce/reduce
423 AT_COMPILE([glr-regr4])
425 AT_PARSER_CHECK([[./glr-regr4]], 0,
426 [[merge{ S <- merge{ A <- A1 <- 'a' and A <- A2 <- 'a' } and S <- B <- 'a' }
432 ## -------------------------------------------------------------------------- ##
433 ## User destructor for unresolved GLR semantic value. See ##
434 ## <http://lists.gnu.org/archive/html/bison-patches/2005-08/msg00016.html>. ##
435 ## -------------------------------------------------------------------------- ##
437 AT_SETUP([User destructor for unresolved GLR semantic value])
439 AT_BISON_OPTION_PUSHDEFS
440 AT_DATA_GRAMMAR([glr-regr5.y],
447 enum { MAGIC_VALUE = -1057808125 }; /* originally chosen at random */
451 %union { int value; }
455 if ($$ != MAGIC_VALUE)
457 fprintf (stderr, "Bad destructor call.\n");
465 'a' { $$ = MAGIC_VALUE; }
466 | 'a' { $$ = MAGIC_VALUE; }
475 return yyparse () != 1;
478 AT_BISON_OPTION_POPDEFS
480 AT_BISON_CHECK([[-o glr-regr5.c glr-regr5.y]], 0, [],
481 [glr-regr5.y: conflicts: 1 reduce/reduce
483 AT_COMPILE([glr-regr5])
485 AT_PARSER_CHECK([[./glr-regr5]], 0, [],
492 ## -------------------------------------------------------------------------- ##
493 ## User destructor after an error during a split parse. See ##
494 ## <http://lists.gnu.org/archive/html/bison-patches/2005-08/msg00029.html>. ##
495 ## -------------------------------------------------------------------------- ##
497 AT_SETUP([User destructor after an error during a split parse])
499 AT_BISON_OPTION_PUSHDEFS
500 AT_DATA_GRAMMAR([glr-regr6.y],
510 %union { int value; }
514 printf ("Destructor called.\n");
526 static char const input[] = "a";
527 static size_t toknum;
528 if (! (toknum < sizeof input))
530 return input[toknum++];
537 return yyparse () != 1;
540 AT_BISON_OPTION_POPDEFS
542 AT_BISON_CHECK([[-o glr-regr6.c glr-regr6.y]], 0, [],
543 [glr-regr6.y: conflicts: 1 reduce/reduce
545 AT_COMPILE([glr-regr6])
547 AT_PARSER_CHECK([[./glr-regr6]], 0,
556 ## ------------------------------------------------------------------------- ##
557 ## Duplicated user destructor for lookahead. See ##
558 ## <http://lists.gnu.org/archive/html/bison-patches/2005-08/msg00035.html>. ##
559 ## ------------------------------------------------------------------------- ##
561 AT_SETUP([Duplicated user destructor for lookahead])
563 AT_BISON_OPTION_PUSHDEFS
564 AT_DATA_GRAMMAR([glr-regr7.y],
571 #define YYSTACKEXPANDABLE 0
572 typedef struct count_node {
574 struct count_node *prev;
576 static count_node *tail;
580 %union { count_node *node; }
585 fprintf (stderr, "Destructor called on same value twice.\n");
603 yylval.node = (count_node*) malloc (sizeof *yylval.node);
606 fprintf (stderr, "Test inconclusive.\n");
609 yylval.node->count = 0;
610 yylval.node->prev = tail;
619 int status = yyparse ();
622 count_node *prev = tail->prev;
629 AT_BISON_OPTION_POPDEFS
631 AT_BISON_CHECK([[-o glr-regr7.c glr-regr7.y]], 0, [],
632 [glr-regr7.y: conflicts: 2 reduce/reduce
634 AT_COMPILE([glr-regr7])
636 AT_PARSER_CHECK([[./glr-regr7]], 2, [],
643 ## ------------------------------------------------------------------------- ##
644 ## Incorrect default location for empty right-hand sides. Adapted from bug ##
645 ## report by Claudia Hermann. ##
646 ## See http://lists.gnu.org/archive/html/bug-bison/2005-10/msg00069.html and ##
647 ## http://lists.gnu.org/archive/html/bug-bison/2005-10/msg00072.html ##
648 ## ------------------------------------------------------------------------- ##
650 AT_SETUP([Incorrectly initialized location for empty right-hand side in GLR])
652 AT_BISON_OPTION_PUSHDEFS
653 AT_DATA_GRAMMAR([glr-regr8.y],
672 PortClause : T_PORT InterfaceDeclaration T_PORT
673 { printf("%d/%d - %d/%d - %d/%d\n",
674 @1.first_column, @1.last_column,
675 @2.first_column, @2.last_column,
676 @3.first_column, @3.last_column); }
679 InterfaceDeclaration : OptConstantWord %dprec 1
680 | OptSignalWord %dprec 2
683 OptConstantWord : /* empty */
687 OptSignalWord : /* empty */
688 { printf("empty: %d/%d\n", @$.first_column, @$.last_column); }
705 yylloc.first_column = 1;
706 yylloc.last_column = 9;
709 yylloc.first_column = 13;
710 yylloc.last_column = 17;
724 AT_BISON_OPTION_POPDEFS
726 AT_BISON_CHECK([[-o glr-regr8.c glr-regr8.y]], 0, [],
727 [glr-regr8.y: conflicts: 1 reduce/reduce
729 AT_COMPILE([glr-regr8])
731 AT_PARSER_CHECK([[./glr-regr8]], 0,
740 ## ------------------------------------------------------------------------- ##
741 ## No users destructors if stack 0 deleted. See ##
742 ## <http://lists.gnu.org/archive/html/bison-patches/2005-09/msg00109.html>. ##
743 ## ------------------------------------------------------------------------- ##
745 AT_SETUP([No users destructors if stack 0 deleted])
747 AT_BISON_OPTION_PUSHDEFS
748 AT_DATA_GRAMMAR([glr-regr9.y],
755 # define YYSTACKEXPANDABLE 0
756 static int tokens = 0;
757 static int destructors = 0;
762 %union { int dummy; }
772 ambig0 'a' { destructors += 2; USE ($2); }
773 | ambig1 start { destructors += 1; }
774 | ambig2 start { destructors += 1; }
795 exit_status = yyparse ();
796 if (tokens != destructors)
798 fprintf (stderr, "Tokens = %d, Destructors = %d\n", tokens, destructors);
804 AT_BISON_OPTION_POPDEFS
806 AT_BISON_CHECK([[-o glr-regr9.c glr-regr9.y]], 0, [],
807 [glr-regr9.y: conflicts: 1 reduce/reduce
809 AT_COMPILE([glr-regr9])
811 AT_PARSER_CHECK([[./glr-regr9]], 0, [],
818 ## ------------------------------------------------------------------------- ##
819 ## Corrupted semantic options if user action cuts parse. ##
820 ## ------------------------------------------------------------------------- ##
822 AT_SETUP([Corrupted semantic options if user action cuts parse])
824 AT_BISON_OPTION_PUSHDEFS
825 AT_DATA_GRAMMAR([glr-regr10.y],
832 #define GARBAGE_SIZE 50
833 static char garbage[GARBAGE_SIZE];
837 %union { char *ptr; }
843 %dprec 2 { $$ = garbage; YYACCEPT; }
844 | %dprec 1 { $$ = garbage; YYACCEPT; }
863 for (i = 0; i < GARBAGE_SIZE; i+=1)
868 AT_BISON_OPTION_POPDEFS
870 AT_BISON_CHECK([[-o glr-regr10.c glr-regr10.y]], 0, [],
871 [glr-regr10.y: conflicts: 1 reduce/reduce
873 AT_COMPILE([glr-regr10])
875 AT_PARSER_CHECK([[./glr-regr10]], 0, [], [])
880 ## ------------------------------------------------------------------------- ##
881 ## Undesirable destructors if user action cuts parse. ##
882 ## ------------------------------------------------------------------------- ##
884 AT_SETUP([Undesirable destructors if user action cuts parse])
886 AT_BISON_OPTION_PUSHDEFS
887 AT_DATA_GRAMMAR([glr-regr11.y],
893 static int destructors = 0;
898 %union { int dummy; }
900 %destructor { destructors += 1; } 'a'
905 'a' %dprec 2 { USE ($1); destructors += 1; YYACCEPT; }
906 | 'a' %dprec 1 { USE ($1); destructors += 1; YYACCEPT; }
912 ]AT_YYLEX_DEFINE([a])[
917 int exit_status = yyparse ();
918 if (destructors != 1)
920 fprintf (stderr, "Destructor calls: %d\n", destructors);
926 AT_BISON_OPTION_POPDEFS
928 AT_BISON_CHECK([[-o glr-regr11.c glr-regr11.y]], 0, [],
929 [glr-regr11.y: conflicts: 1 reduce/reduce
931 AT_COMPILE([glr-regr11])
933 AT_PARSER_CHECK([[./glr-regr11]], 0, [], [])
938 ## ------------------------------------------------------------------------- ##
939 ## Leaked semantic values if user action cuts parse. ##
940 ## ------------------------------------------------------------------------- ##
942 AT_SETUP([Leaked semantic values if user action cuts parse])
944 AT_BISON_OPTION_PUSHDEFS
945 AT_DATA_GRAMMAR([glr-regr12.y],
948 %union { int dummy; }
949 %token PARENT_RHS_AFTER
950 %type <dummy> parent_rhs_before merged PARENT_RHS_AFTER
951 %destructor { parent_rhs_before_value = 0; } parent_rhs_before
952 %destructor { merged_value = 0; } merged
953 %destructor { parent_rhs_after_value = 0; } PARENT_RHS_AFTER
957 static int merge (YYSTYPE, YYSTYPE);
960 static int parent_rhs_before_value = 0;
961 static int merged_value = 0;
962 static int parent_rhs_after_value = 0;
976 parent_rhs_after_value = 0;
981 parent_rhs_before merged PARENT_RHS_AFTER {
983 parent_rhs_before_value = 0;
985 parent_rhs_after_value = 0;
992 parent_rhs_before_value = 1;
1001 | cut %merge<merge> {
1007 cut: { YYACCEPT; } ;
1012 merge (YYSTYPE s1, YYSTYPE s2)
1015 char dummy = s1.dummy + s2.dummy;
1023 static int const input[] = { PARENT_RHS_AFTER, 0 };
1024 static size_t toknum;
1025 if (! (toknum < sizeof input / sizeof *input))
1027 if (input[toknum] == PARENT_RHS_AFTER)
1028 parent_rhs_after_value = 1;
1029 return input[toknum++];
1035 int exit_status = yyparse ();
1036 if (parent_rhs_before_value)
1038 fprintf (stderr, "`parent_rhs_before' destructor not called.\n");
1043 fprintf (stderr, "`merged' destructor not called.\n");
1046 if (parent_rhs_after_value)
1048 fprintf (stderr, "`PARENT_RHS_AFTER' destructor not called.\n");
1054 AT_BISON_OPTION_POPDEFS
1056 AT_BISON_CHECK([[-o glr-regr12.c glr-regr12.y]], 0, [],
1057 [glr-regr12.y: conflicts: 1 shift/reduce, 1 reduce/reduce
1059 AT_COMPILE([glr-regr12])
1061 AT_PARSER_CHECK([[./glr-regr12]], 0, [], [])
1066 ## ------------------------------------------------------------------------- ##
1067 ## Incorrect lookahead during deterministic GLR. See ##
1068 ## <http://lists.gnu.org/archive/html/help-bison/2005-07/msg00017.html> and ##
1069 ## <http://lists.gnu.org/archive/html/bison-patches/2006-01/msg00060.html>. ##
1070 ## ------------------------------------------------------------------------- ##
1072 AT_SETUP([Incorrect lookahead during deterministic GLR])
1074 AT_BISON_OPTION_PUSHDEFS
1075 AT_DATA_GRAMMAR([glr-regr13.y],
1078 - Defaulted state with initial yychar: yychar == YYEMPTY.
1079 - Nondefaulted state: yychar != YYEMPTY.
1080 - Defaulted state after lookahead: yychar != YYEMPTY.
1081 - Defaulted state after shift: yychar == YYEMPTY.
1082 - User action changing the lookahead. */
1086 ]AT_YYERROR_DECLARE[
1088 static void print_lookahead (char const *);
1092 %union { char value; }
1093 %type <value> 'a' 'b'
1100 defstate_init defstate_shift 'b' change_lookahead 'a' {
1102 print_lookahead ("start <- defstate_init defstate_shift 'b'");
1107 print_lookahead ("defstate_init <- empty string");
1111 nondefstate defstate_look 'a' {
1113 print_lookahead ("defstate_shift <- nondefstate defstate_look 'a'");
1118 print_lookahead ("defstate_look <- empty string");
1123 print_lookahead ("nondefstate <- empty string");
1127 print_lookahead ("nondefstate <- 'b'");
1142 static char const input[] = "ab";
1143 static size_t toknum;
1144 if (! (toknum < sizeof input))
1146 yylloc.first_line = yylloc.last_line = 1;
1147 yylloc.first_column = yylloc.last_column = toknum + 1;
1148 yylval.value = input[toknum] + 'A' - 'a';
1149 return input[toknum++];
1153 print_lookahead (char const *reduction)
1155 printf ("%s:\n yychar=", reduction);
1156 if (yychar == YYEMPTY)
1158 else if (yychar == YYEOF)
1162 printf ("'%c', yylval='", yychar);
1163 if (yylval.value > ' ')
1164 printf ("%c", yylval.value);
1165 printf ("', yylloc=(%d,%d),(%d,%d)",
1166 yylloc.first_line, yylloc.first_column,
1167 yylloc.last_line, yylloc.last_column);
1175 yychar = '#'; /* Not a token in the grammar. */
1180 AT_BISON_OPTION_POPDEFS
1182 AT_BISON_CHECK([[-o glr-regr13.c glr-regr13.y]], 0, [], [])
1183 AT_COMPILE([glr-regr13])
1185 AT_PARSER_CHECK([[./glr-regr13]], 0,
1186 [defstate_init <- empty string:
1188 nondefstate <- empty string:
1189 yychar='a', yylval='A', yylloc=(1,1),(1,1)
1190 defstate_look <- empty string:
1191 yychar='a', yylval='A', yylloc=(1,1),(1,1)
1192 defstate_shift <- nondefstate defstate_look 'a':
1194 start <- defstate_init defstate_shift 'b':
1201 ## ------------------------------------------------------------------------- ##
1202 ## Incorrect lookahead during nondeterministic GLR. ##
1203 ## ------------------------------------------------------------------------- ##
1205 AT_SETUP([Incorrect lookahead during nondeterministic GLR])
1207 AT_BISON_OPTION_PUSHDEFS
1208 AT_DATA_GRAMMAR([glr-regr14.y],
1211 - Conflicting actions (split-off parse, which copies lookahead need,
1212 which is necessarily yytrue) and nonconflicting actions (non-split-off
1213 parse) for nondefaulted state: yychar != YYEMPTY.
1214 - Merged deferred actions (lookahead need and RHS from different stack
1215 than the target state) and nonmerged deferred actions (same stack).
1216 - Defaulted state after lookahead: yychar != YYEMPTY.
1217 - Defaulted state after shift: yychar == YYEMPTY.
1218 - yychar != YYEMPTY but lookahead need is yyfalse (a previous stack has
1219 seen the lookahead but current stack has not).
1220 - Exceeding stack capacity (stack explosion), and thus reallocating
1221 lookahead need array.
1222 Note that it does not seem possible to see the initial yychar value during
1223 nondeterministic operation since:
1224 - In order to preserve the initial yychar, only defaulted states may be
1226 - If only defaulted states are entered, there are no conflicts, so
1227 nondeterministic operation does not start. */
1229 %union { char value; }
1234 ]AT_YYERROR_DECLARE[
1236 static void print_lookahead (char const *);
1237 static char merge (union YYSTYPE, union YYSTYPE);
1241 %type <value> 'a' 'b' 'c' 'd' stack_explosion
1248 merge 'c' stack_explosion {
1250 print_lookahead ("start <- merge 'c' stack_explosion");
1254 /* When merging the 2 deferred actions, the lookahead needs are different. */
1256 nonconflict1 'a' 'b' nonconflict2 %dprec 1 {
1258 print_lookahead ("merge <- nonconflict1 'a' 'b' nonconflict2");
1260 | conflict defstate_look 'a' nonconflict2 'b' defstate_shift %dprec 2 {
1262 print_lookahead ("merge <- conflict defstate_look 'a' nonconflict2 'b'"
1269 print_lookahead ("nonconflict1 <- empty string");
1274 print_lookahead ("nonconflict2 <- empty string");
1278 print_lookahead ("nonconflict2 <- 'a'");
1283 print_lookahead ("conflict <- empty string");
1288 print_lookahead ("defstate_look <- empty string");
1292 /* yychar != YYEMPTY but lookahead need is yyfalse. */
1295 print_lookahead ("defstate_shift <- empty string");
1301 | alt1 stack_explosion %merge<merge> { $$ = $2; }
1302 | alt2 stack_explosion %merge<merge> { $$ = $2; }
1303 | alt3 stack_explosion %merge<merge> { $$ = $2; }
1308 if (yychar != 'd' && yychar != YYEOF)
1310 fprintf (stderr, "Incorrect lookahead during stack explosion.\n");
1317 if (yychar != 'd' && yychar != YYEOF)
1319 fprintf (stderr, "Incorrect lookahead during stack explosion.\n");
1326 if (yychar != 'd' && yychar != YYEOF)
1328 fprintf (stderr, "Incorrect lookahead during stack explosion.\n");
1334 if (yychar != YYEMPTY)
1337 "Found lookahead where shouldn't during stack explosion.\n");
1348 static char const input[] = "abcdddd";
1349 static size_t toknum;
1350 if (! (toknum < sizeof input))
1352 yylloc.first_line = yylloc.last_line = 1;
1353 yylloc.first_column = yylloc.last_column = toknum + 1;
1354 yylval.value = input[toknum] + 'A' - 'a';
1355 return input[toknum++];
1359 print_lookahead (char const *reduction)
1361 printf ("%s:\n yychar=", reduction);
1362 if (yychar == YYEMPTY)
1364 else if (yychar == YYEOF)
1368 printf ("'%c', yylval='", yychar);
1369 if (yylval.value > ' ')
1370 printf ("%c", yylval.value);
1371 printf ("', yylloc=(%d,%d),(%d,%d)",
1372 yylloc.first_line, yylloc.first_column,
1373 yylloc.last_line, yylloc.last_column);
1379 merge (union YYSTYPE s1, union YYSTYPE s2)
1381 char dummy = s1.value + s2.value;
1388 yychar = '#'; /* Not a token in the grammar. */
1393 AT_BISON_OPTION_POPDEFS
1395 AT_BISON_CHECK([[-o glr-regr14.c glr-regr14.y]], 0, [],
1396 [glr-regr14.y: conflicts: 3 reduce/reduce
1398 AT_COMPILE([glr-regr14])
1400 AT_PARSER_CHECK([[./glr-regr14]], 0,
1401 [conflict <- empty string:
1402 yychar='a', yylval='A', yylloc=(1,1),(1,1)
1403 defstate_look <- empty string:
1404 yychar='a', yylval='A', yylloc=(1,1),(1,1)
1405 nonconflict2 <- empty string:
1406 yychar='b', yylval='B', yylloc=(1,2),(1,2)
1407 defstate_shift <- empty string:
1409 merge <- conflict defstate_look 'a' nonconflict2 'b' defstate_shift:
1411 start <- merge 'c' stack_explosion:
1418 ## ------------------------------------------------------------------------- ##
1419 ## Leaked semantic values when reporting ambiguity. ##
1420 ## ------------------------------------------------------------------------- ##
1422 AT_SETUP([Leaked semantic values when reporting ambiguity])
1424 AT_BISON_OPTION_PUSHDEFS
1425 AT_DATA_GRAMMAR([glr-regr15.y],
1428 %destructor { parent_rhs_before_value = 0; } parent_rhs_before
1431 # include <stdlib.h>
1432 ]AT_YYERROR_DECLARE[
1434 static int parent_rhs_before_value = 0;
1445 /* This stack must be merged into the other stacks *last* (added at the
1446 beginning of the semantic options list) so that yyparse will choose to clean
1447 it up rather than the tree for which some semantic actions have been
1448 performed. Thus, if yyreportAmbiguity longjmp's to yyparse, the values from
1449 those other trees are not cleaned up. */
1453 parent_rhs_before ambiguity {
1455 parent_rhs_before_value = 0;
1462 parent_rhs_before_value = 1;
1466 ambiguity: ambiguity1 | ambiguity2 ;
1485 int exit_status = yyparse () != 1;
1486 if (parent_rhs_before_value)
1488 fprintf (stderr, "`parent_rhs_before' destructor not called.\n");
1494 AT_BISON_OPTION_POPDEFS
1496 AT_BISON_CHECK([[-o glr-regr15.c glr-regr15.y]], 0, [],
1497 [glr-regr15.y: conflicts: 2 reduce/reduce
1499 AT_COMPILE([glr-regr15])
1501 AT_PARSER_CHECK([[./glr-regr15]], 0, [],
1502 [syntax is ambiguous
1508 ## ------------------------------------------------------------------------- ##
1509 ## Leaked lookahead after nondeterministic parse syntax error. ##
1510 ## ------------------------------------------------------------------------- ##
1512 AT_SETUP([Leaked lookahead after nondeterministic parse syntax error])
1514 AT_BISON_OPTION_PUSHDEFS
1515 AT_DATA_GRAMMAR([glr-regr16.y],
1518 %destructor { lookahead_value = 0; } 'b'
1521 # include <stdlib.h>
1522 ]AT_YYERROR_DECLARE[
1524 static int lookahead_value = 0;
1530 start: alt1 'a' | alt2 'a' ;
1540 static char const input[] = "ab";
1541 static size_t toknum;
1542 if (! (toknum < sizeof input))
1544 if (input[toknum] == 'b')
1545 lookahead_value = 1;
1546 return input[toknum++];
1552 int exit_status = yyparse () != 1;
1553 if (lookahead_value)
1555 fprintf (stderr, "Lookahead destructor not called.\n");
1561 AT_BISON_OPTION_POPDEFS
1563 AT_BISON_CHECK([[-o glr-regr16.c glr-regr16.y]], 0, [],
1564 [glr-regr16.y: conflicts: 1 reduce/reduce
1566 AT_COMPILE([glr-regr16])
1568 AT_PARSER_CHECK([[./glr-regr16]], 0, [],
1575 ## ------------------------------------------------------------------------- ##
1576 ## Uninitialized location when reporting ambiguity. ##
1577 ## ------------------------------------------------------------------------- ##
1579 AT_SETUP([Uninitialized location when reporting ambiguity])
1581 AT_BISON_OPTION_PUSHDEFS
1582 AT_DATA_GRAMMAR([glr-regr17.y],
1589 %union { int dummy; }
1592 static void yyerror (YYLTYPE *, char const *);
1593 static int yylex (YYSTYPE *, YYLTYPE *);
1598 @$.first_column = 1;
1605 /* Tests the case of an empty RHS that has inherited the location of the
1606 previous nonterminal, which is unresolved. That location is reported as the
1607 last position of the ambiguity. */
1608 start: ambig1 empty1 | ambig2 empty2 ;
1610 /* Tests multiple levels of yyresolveLocations recursion. */
1611 ambig1: sub_ambig1 | sub_ambig2 ;
1612 ambig2: sub_ambig1 | sub_ambig2 ;
1614 /* Tests the case of a non-empty RHS as well as the case of an empty RHS that
1615 has inherited the initial location. The empty RHS's location is reported as
1616 the first position in the ambiguity. */
1617 sub_ambig1: empty1 'a' 'b' ;
1618 sub_ambig2: empty2 'a' 'b' ;
1625 yyerror (YYLTYPE *locp, char const *msg)
1627 fprintf (stderr, "%d.%d-%d.%d: %s.\n", locp->first_line,
1628 locp->first_column, locp->last_line, locp->last_column, msg);
1632 yylex (YYSTYPE *lvalp, YYLTYPE *llocp)
1634 static char const input[] = "ab";
1635 static size_t toknum;
1636 if (! (toknum < sizeof input))
1639 llocp->first_line = llocp->last_line = 2;
1640 llocp->first_column = toknum + 1;
1641 llocp->last_column = llocp->first_column + 1;
1642 return input[toknum++];
1648 return yyparse () != 1;
1651 AT_BISON_OPTION_POPDEFS
1653 AT_BISON_CHECK([[-o glr-regr17.c glr-regr17.y]], 0, [],
1654 [glr-regr17.y: conflicts: 3 reduce/reduce
1656 AT_COMPILE([glr-regr17])
1658 AT_PARSER_CHECK([[./glr-regr17]], 0, [],
1659 [1.1-2.3: syntax is ambiguous.
1665 ## -------------------------------------------------------------##
1666 ## Missed %merge type warnings when LHS type is declared later. ##
1667 ## -------------------------------------------------------------##
1669 AT_SETUP([Missed %merge type warnings when LHS type is declared later])
1671 AT_BISON_OPTION_PUSHDEFS
1672 AT_DATA_GRAMMAR([glr-regr18.y],
1677 ]AT_YYERROR_DECLARE[
1678 static int yylex ();
1689 sym1: sym2 %merge<merge> { $$ = $1; } ;
1690 sym2: sym3 %merge<merge> { $$ = $1; } ;
1691 sym3: %merge<merge> { $$ = 0; } ;
1707 AT_BISON_OPTION_POPDEFS
1709 AT_BISON_CHECK([[-o glr-regr18.c glr-regr18.y]], 1, [],
1710 [glr-regr18.y:26.18-24: result type clash on merge function 'merge': <type2> != <type1>
1711 glr-regr18.y:25.18-24: previous declaration
1712 glr-regr18.y:27.13-19: result type clash on merge function 'merge': <type3> != <type2>
1713 glr-regr18.y:26.18-24: previous declaration
1719 ## ------------------- ##
1720 ## Ambiguity reports. ##
1721 ## ------------------- ##
1723 AT_SETUP([Ambiguity reports])
1725 AT_BISON_OPTION_PUSHDEFS
1726 AT_DATA_GRAMMAR([input.y],
1731 ]AT_YYERROR_DECLARE[
1746 ]AT_YYLEX_DEFINE([abc])[
1752 return !!yyparse ();
1755 AT_BISON_OPTION_POPDEFS
1757 AT_BISON_CHECK([[-o input.c input.y]], 0, [],
1758 [input.y: conflicts: 1 reduce/reduce
1762 AT_PARSER_CHECK([[./input]], 1, [],
1765 Reading a token: Next token is token 'a' ()
1766 Shifting token 'a' ()
1768 Reading a token: Next token is token 'b' ()
1769 Shifting token 'b' ()
1771 Reducing stack 0 by rule 3 (line 25):
1775 Reading a token: Next token is token 'c' ()
1776 Shifting token 'c' ()
1778 Reducing stack 0 by rule 4 (line 26):
1781 Reading a token: Now at end of input.
1782 Stack 0 Entering state 7
1783 Now at end of input.
1784 Splitting off stack 1 from 0.
1785 Reduced stack 1 by rule #2; action deferred. Now in state 2.
1786 Stack 1 Entering state 2
1787 Now at end of input.
1788 Reduced stack 0 by rule #1; action deferred. Now in state 2.
1789 Merging stack 0 into stack 1.
1790 Stack 1 Entering state 2
1791 Now at end of input.
1792 Removing dead stacks.
1793 Rename stack 1 -> 0.
1794 On stack 0, shifting token $end ()
1795 Stack 0 now in state #5
1798 start -> <Rule 1, tokens 1 .. 3>
1805 start -> <Rule 2, tokens 1 .. 3>
1812 Cleanup: popping token $end ()
1813 Cleanup: popping unresolved nterm start ()
1814 Cleanup: popping nterm d ()
1815 Cleanup: popping token 'c' ()
1816 Cleanup: popping nterm b ()
1817 Cleanup: popping token 'a' ()