portability: beware of max () with MSVC
[bison.git] / tests / reduce.at
blobb561100e968c4190a94f26d8ae5f4b0d84641f97
1 # Exercising Bison Grammar Reduction.                      -*- Autotest -*-
3 # Copyright (C) 2001-2002, 2007-2015, 2018-2020 Free Software
4 # Foundation, Inc.
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([[Grammar Reduction.]])
22 ## ------------------- ##
23 ## Useless Terminals.  ##
24 ## ------------------- ##
26 AT_SETUP([Useless Terminals])
28 AT_DATA([[input.y]],
29 [[%verbose
30 %output "input.c"
32 %token useless1
33 %token useless2
34 %token useless3
35 %token useless4
36 %token useless5
37 %token useless6
38 %token useless7
39 %token useless8
40 %token useless9
42 %token useful
44 exp: useful;
45 ]])
47 AT_BISON_CHECK([[input.y]])
49 AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0,
50 [[Terminals unused in grammar
51     useless1
52     useless2
53     useless3
54     useless4
55     useless5
56     useless6
57     useless7
58     useless8
59     useless9
60 ]])
62 AT_CLEANUP
66 ## ---------------------- ##
67 ## Useless Nonterminals.  ##
68 ## ---------------------- ##
70 AT_SETUP([Useless Nonterminals])
71 AT_BISON_OPTION_PUSHDEFS
73 AT_DATA([[input.y]],
74 [[%code {
75   ]AT_YYERROR_DECLARE_EXTERN[
76   ]AT_YYLEX_DECLARE_EXTERN[
78 %verbose
79 %output "input.c"
81 %token useful
83 exp: useful;
84 useless1:
85 useless2:
86 useless3:
87 ]])
89 AT_BISON_CHECK([[input.y]], 0, [],
90 [[input.y: warning: 3 nonterminals useless in grammar [-Wother]
91 input.y: warning: 3 rules useless in grammar [-Wother]
92 input.y:11.1-8: warning: nonterminal useless in grammar: useless1 [-Wother]
93 input.y:12.1-8: warning: nonterminal useless in grammar: useless2 [-Wother]
94 input.y:13.1-8: warning: nonterminal useless in grammar: useless3 [-Wother]
95 ]])
97 AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0,
98 [[Nonterminals useless in grammar
99     useless1
100     useless2
101     useless3
102 Rules useless in grammar
103     2 useless1: %empty
104     3 useless2: %empty
105     4 useless3: %empty
108 # Make sure the generated parser is correct.
109 AT_COMPILE([input.o])
111 AT_BISON_OPTION_POPDEFS
112 AT_CLEANUP
116 ## --------------- ##
117 ## Useless Rules.  ##
118 ## --------------- ##
120 AT_SETUP([Useless Rules])
122 AT_BISON_OPTION_PUSHDEFS
123 AT_KEYWORDS([report])
125 AT_DATA([[input.y]],
126 [[%code {
127   ]AT_YYERROR_DECLARE_EXTERN[
128   ]AT_YYLEX_DECLARE_EXTERN[
130 %verbose
131 %output "input.c"
132 %token useful
134 exp: useful;
135 useless1: '1';
136 useless2: '2';
137 useless3: '3';
138 useless4: '4';
139 useless5: '5';
140 useless6: '6';
141 useless7: '7';
142 useless8: '8';
143 useless9: '9';
146 AT_BISON_CHECK([[-fcaret input.y]], 0, [],
147 [[input.y: warning: 9 nonterminals useless in grammar [-Wother]
148 input.y: warning: 9 rules useless in grammar [-Wother]
149 input.y:10.1-8: warning: nonterminal useless in grammar: useless1 [-Wother]
150    10 | useless1: '1';
151       | ^~~~~~~~
152 input.y:11.1-8: warning: nonterminal useless in grammar: useless2 [-Wother]
153    11 | useless2: '2';
154       | ^~~~~~~~
155 input.y:12.1-8: warning: nonterminal useless in grammar: useless3 [-Wother]
156    12 | useless3: '3';
157       | ^~~~~~~~
158 input.y:13.1-8: warning: nonterminal useless in grammar: useless4 [-Wother]
159    13 | useless4: '4';
160       | ^~~~~~~~
161 input.y:14.1-8: warning: nonterminal useless in grammar: useless5 [-Wother]
162    14 | useless5: '5';
163       | ^~~~~~~~
164 input.y:15.1-8: warning: nonterminal useless in grammar: useless6 [-Wother]
165    15 | useless6: '6';
166       | ^~~~~~~~
167 input.y:16.1-8: warning: nonterminal useless in grammar: useless7 [-Wother]
168    16 | useless7: '7';
169       | ^~~~~~~~
170 input.y:17.1-8: warning: nonterminal useless in grammar: useless8 [-Wother]
171    17 | useless8: '8';
172       | ^~~~~~~~
173 input.y:18.1-8: warning: nonterminal useless in grammar: useless9 [-Wother]
174    18 | useless9: '9';
175       | ^~~~~~~~
179 AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0,
180 [[Nonterminals useless in grammar
181     useless1
182     useless2
183     useless3
184     useless4
185     useless5
186     useless6
187     useless7
188     useless8
189     useless9
190 Terminals unused in grammar
191     '1'
192     '2'
193     '3'
194     '4'
195     '5'
196     '6'
197     '7'
198     '8'
199     '9'
200 Rules useless in grammar
201     2 useless1: '1'
202     3 useless2: '2'
203     4 useless3: '3'
204     5 useless4: '4'
205     6 useless5: '5'
206     7 useless6: '6'
207     8 useless7: '7'
208     9 useless8: '8'
209    10 useless9: '9'
212 # Make sure the generated parser is correct.
213 AT_COMPILE([input.o])
215 AT_BISON_OPTION_POPDEFS
216 AT_CLEANUP
220 ## --------------- ##
221 ## Useless Parts.  ##
222 ## --------------- ##
224 AT_SETUP([Useless Parts])
226 # We used to emit code that used symbol numbers before the useless
227 # symbol elimination, hence before the renumbering of the useful
228 # symbols.  As a result, the evaluation of the skeleton failed because
229 # it used non existing symbol numbers.  Which is the happy scenario:
230 # we could use numbers of other existing symbols...
231 # http://lists.gnu.org/archive/html/bug-bison/2019-01/msg00044.html
233 AT_BISON_OPTION_PUSHDEFS
234 AT_DATA([[input.y]],
235 [[%code {
236   ]AT_YYERROR_DECLARE_EXTERN[
237   ]AT_YYLEX_DECLARE_EXTERN[
239 %union { void* ptr; }
240 %type <ptr> used1
241 %type <ptr> used2
244 start
245  : used1
248 used1
249  : used2 { $$ = $1; }
252 unused
253  : used2
256 used2
257  : { $$ = YY_NULLPTR; }
261 AT_BISON_CHECK([[-fcaret -rall -o input.c input.y]], 0, [],
262 [[input.y: warning: 1 nonterminal useless in grammar [-Wother]
263 input.y: warning: 1 rule useless in grammar [-Wother]
264 input.y:18.1-6: warning: nonterminal useless in grammar: unused [-Wother]
265    18 | unused
266       | ^~~~~~
270 AT_CHECK([[sed -n '/^State 0/q;/^$/!p' input.output]], 0,
271 [[Nonterminals useless in grammar
272     unused
273 Rules useless in grammar
274     4 unused: used2
275 Grammar
276     0 $accept: start $end
277     1 start: used1
278     2 used1: used2
279     3 used2: %empty
280 Terminals, with rules where they appear
281     $end (0) 0
282     error (256)
283 Nonterminals, with rules where they appear
284     $accept (3)
285         on left: 0
286     start (4)
287         on left: 1
288         on right: 0
289     used1 <ptr> (5)
290         on left: 2
291         on right: 1
292     used2 <ptr> (6)
293         on left: 3
294         on right: 2
297 # Make sure the generated parser is correct.
298 AT_COMPILE([input.o])
300 AT_BISON_OPTION_POPDEFS
301 AT_CLEANUP
305 ## ------------------- ##
306 ## Reduced Automaton.  ##
307 ## ------------------- ##
309 # Check that the automaton is that as the for the grammar reduced by
310 # hand.
312 AT_SETUP([Reduced Automaton])
314 AT_KEYWORDS([report])
316 # The non reduced grammar.
317 # ------------------------
318 AT_DATA([[not-reduced.y]],
319 [[/* A useless token. */
320 %token useless_token
321 /* A useful one. */
322 %token useful
323 %verbose
324 %output "not-reduced.c"
328 exp: useful            { /* A useful action. */ }
329    | non_productive    { /* A non productive action. */ }
330    ;
332 not_reachable: useful  { /* A not reachable action. */ }
333              ;
335 non_productive: non_productive useless_token
336                        { /* Another non productive action. */ }
337               ;
341 AT_BISON_CHECK([[-fcaret not-reduced.y]], 0, [],
342 [[not-reduced.y: warning: 2 nonterminals useless in grammar [-Wother]
343 not-reduced.y: warning: 3 rules useless in grammar [-Wother]
344 not-reduced.y:14.1-13: warning: nonterminal useless in grammar: not_reachable [-Wother]
345    14 | not_reachable: useful  { /* A not reachable action. */ }
346       | ^~~~~~~~~~~~~
347 not-reduced.y:17.1-14: warning: nonterminal useless in grammar: non_productive [-Wother]
348    17 | non_productive: non_productive useless_token
349       | ^~~~~~~~~~~~~~
350 not-reduced.y:11.6-57: warning: rule useless in grammar [-Wother]
351    11 |    | non_productive    { /* A non productive action. */ }
352       |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
355 AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' not-reduced.output]], 0,
356 [[Nonterminals useless in grammar
357     not_reachable
358     non_productive
359 Terminals unused in grammar
360     useless_token
361 Rules useless in grammar
362     2 exp: non_productive
363     3 not_reachable: useful
364     4 non_productive: non_productive useless_token
367 # The reduced grammar.
368 # --------------------
369 AT_DATA([[reduced.y]],
370 [[/* A useless token. */
371 %token useless_token
372 /* A useful one. */
373 %token useful
374 %verbose
375 %output "reduced.c"
379 exp: useful            { /* A useful action. */ }
380 //   | non_productive    { /* A non productive action. */ } */
381    ;
383 //not_reachable: useful  { /* A not reachable action. */ }
384 //             ;
386 //non_productive: non_productive useless_token
387 //                       { /* Another non productive action. */ }
388 //              ;
392 AT_BISON_CHECK([[reduced.y]])
394 # Comparing the parsers.
395 cp reduced.c expout
396 AT_CHECK([sed 's/not-reduced/reduced/g' not-reduced.c], 0, [expout])
398 AT_CLEANUP
402 ## ------------------- ##
403 ## Underivable Rules.  ##
404 ## ------------------- ##
406 AT_SETUP([Underivable Rules])
408 AT_KEYWORDS([report])
410 AT_DATA([[input.y]],
411 [[%verbose
412 %output "input.c"
413 %token useful
415 exp: useful | underivable;
416 underivable: indirection;
417 indirection: underivable;
420 AT_BISON_CHECK([[-fcaret input.y]], 0, [],
421 [[input.y: warning: 2 nonterminals useless in grammar [-Wother]
422 input.y: warning: 3 rules useless in grammar [-Wother]
423 input.y:6.1-11: warning: nonterminal useless in grammar: underivable [-Wother]
424     6 | underivable: indirection;
425       | ^~~~~~~~~~~
426 input.y:7.1-11: warning: nonterminal useless in grammar: indirection [-Wother]
427     7 | indirection: underivable;
428       | ^~~~~~~~~~~
429 input.y:5.15-25: warning: rule useless in grammar [-Wother]
430     5 | exp: useful | underivable;
431       |               ^~~~~~~~~~~
434 AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0,
435 [[Nonterminals useless in grammar
436     underivable
437     indirection
438 Rules useless in grammar
439     2 exp: underivable
440     3 underivable: indirection
441     4 indirection: underivable
444 AT_CLEANUP
448 ## ---------------- ##
449 ## Empty Language.  ##
450 ## ---------------- ##
452 AT_SETUP([Empty Language])
454 AT_DATA([[input.y]],
455 [[%output "input.c"
457 exp: exp;
460 AT_BISON_CHECK([[input.y]], 1, [],
461 [[input.y: warning: 2 nonterminals useless in grammar [-Wother]
462 input.y: warning: 2 rules useless in grammar [-Wother]
463 input.y:3.1-3: fatal error: start symbol exp does not derive any sentence
466 AT_CLEANUP
470 ## ----------------- ##
471 ## %define lr.type.  ##
472 ## ----------------- ##
474 # AT_TEST_LR_TYPE(DESCRIPTION,
475 #                 DECLS, GRAMMAR, INPUT,
476 #                 BISON-STDERR, TABLES,
477 #                 [OTHER-CHECKS],
478 #                 [PARSER-EXIT-VALUE],
479 #                 [PARSER-STDOUT], [PARSER-STDERR])
480 # -------------------------------------------------
481 m4_define([AT_TEST_LR_TYPE],
483 AT_TEST_TABLES_AND_PARSE([[no %define lr.type: ]$1],
484                          [[LALR]], [[]],
485                          [$2], m4_shiftn(2, $@))
486 AT_TEST_TABLES_AND_PARSE([[%define lr.type lalr: ]$1],
487                          [[LALR]], [[]],
488                          [[%define lr.type lalr
489 ]$2],
490                          m4_shiftn(2, $@))
491 AT_TEST_TABLES_AND_PARSE([[%define lr.type ielr: ]$1],
492                          [[IELR]], [[]],
493                          [[%define lr.type ielr
494 ]$2],
495                          m4_shiftn(2, $@))
496 AT_TEST_TABLES_AND_PARSE([[%define lr.type canonical-lr: ]$1],
497                          [[canonical LR]], [[]],
498                          [[%define lr.type canonical-lr
499 ]$2],
500                          m4_shiftn(2, $@))
503 AT_TEST_LR_TYPE([[Single State Split]],
504 [[%left 'a'
505 // Conflict resolution renders state 12 unreachable for canonical LR(1).  We
506 // keep it so that the parser table diff is easier to code.
507 %define lr.keep-unreachable-state]],
509 S: 'a' A 'a' /* rule 1 */
510  | 'b' A 'b' /* rule 2 */
511  | 'c' c     /* rule 3 */
514 /* A conflict should appear after the first 'a' in rules 4 and 5 but only after
515    having shifted the first 'a' in rule 1.  However, when LALR(1) merging is
516    chosen, the state containing that conflict is reused after having seen the
517    first 'b' in rule 2 and then the first 'a' in rules 4 and 5.  In both cases,
518    because of the merged state, if the next token is an 'a', the %left forces a
519    reduction action with rule 5.  In the latter case, only a shift is actually
520    grammatically correct.  Thus, the parser would report a syntax error for the
521    grammatically correct sentence "baab" because it would encounter a syntax
522    error after that incorrect reduction.
524    Despite not being LALR(1), Menhir version 20070322 suffers from this problem
525    as well.  It uses David Pager's weak compatibility test for merging states.
526    Bison and Menhir accept non-LR(1) grammars with conflict resolution.  Pager
527    designed his algorithm only for LR(1) grammars.  */
528 A: 'a' 'a' /* rule 4 */
529  | 'a'     /* rule 5 */
532 /* Rule 3, rule 6, and rule 7 ensure that Bison does not report rule 4 as
533    useless after conflict resolution.  This proves that, even though LALR(1)
534    generates incorrect parser tables sometimes, Bison will not necessarily
535    produce any warning to help the user realize it.  */
536 c: 'a' 'b' /* rule 6 */
537  | A       /* rule 7 */
541 dnl INPUT
542 [['b', 'a', 'a', 'b']],
544 dnl BISON-STDERR
547 dnl TABLES
548 [[State 0
550     0 $accept: . S $end
551     1 S: . 'a' A 'a'
552     2  | . 'b' A 'b'
553     3  | . 'c' c
555     'a'  shift, and go to state 1
556     'b'  shift, and go to state 2
557     'c'  shift, and go to state 3
559     S  go to state 4
562 State 1
564     1 S: 'a' . A 'a'
565     4 A: . 'a' 'a'
566     5  | . 'a'
568     'a'  shift, and go to state 5
570     A  go to state 6
573 State 2
575     2 S: 'b' . A 'b'
576     4 A: . 'a' 'a'
577     5  | . 'a'
579     'a'  shift, and go to state ]AT_COND_CASE([[LALR]], [[5]], [[16]])[
581     A  go to state 7
584 State 3
586     3 S: 'c' . c
587     4 A: . 'a' 'a'
588     5  | . 'a'
589     6 c: . 'a' 'b'
590     7  | . A
592     'a'  shift, and go to state 8
594     A  go to state 9
595     c  go to state 10
598 State 4
600     0 $accept: S . $end
602     $end  shift, and go to state 11
605 State 5
607     4 A: 'a' . 'a'
608     5  | 'a' .  ]AT_COND_CASE([[LALR]], [[['a', 'b']]], [[['a']]])[
610     ]AT_COND_CASE([[canonical LR]], [['a']],
611                   [[$default]])[  reduce using rule 5 (A)
613     Conflict between rule 5 and token 'a' resolved as reduce (%left 'a').
616 State 6
618     1 S: 'a' A . 'a'
620     'a'  shift, and go to state 13
623 State 7
625     2 S: 'b' A . 'b'
627     'b'  shift, and go to state 14
630 State 8
632     4 A: 'a' . 'a'
633     5  | 'a' .  [$end]
634     6 c: 'a' . 'b'
636     'a'  shift, and go to state ]AT_COND_CASE([[canonical LR]], [[17]],
637                                               [[12]])[
638     'b'  shift, and go to state 15
640     ]AT_COND_CASE([[canonical LR]], [[$end]],
641                   [[$default]])[  reduce using rule 5 (A)
644 State 9
646     7 c: A .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
648     ]AT_COND_CASE([[canonical LR]], [[$end]],
649                   [[$default]])[  reduce using rule 7 (c)
652 State 10
654     3 S: 'c' c .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
656     ]AT_COND_CASE([[canonical LR]], [[$end]],
657                   [[$default]])[  reduce using rule 3 (S)
660 State 11
662     0 $accept: S $end .
664     $default  accept
667 State 12
669     4 A: 'a' 'a' .]AT_COND_CASE([[canonical LR]], [[  ['a']]])[
671     ]AT_COND_CASE([[canonical LR]], [['a']],
672                   [[$default]])[  reduce using rule 4 (A)
675 State 13
677     1 S: 'a' A 'a' .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
679     ]AT_COND_CASE([[canonical LR]], [[$end]],
680                   [[$default]])[  reduce using rule 1 (S)
683 State 14
685     2 S: 'b' A 'b' .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
687     ]AT_COND_CASE([[canonical LR]], [[$end]],
688                   [[$default]])[  reduce using rule 2 (S)
691 State 15
693     6 c: 'a' 'b' .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
695     ]AT_COND_CASE([[canonical LR]], [[$end]],
696                   [[$default]])[  reduce using rule 6 (c)]AT_COND_CASE([[LALR]],
697                                                                        [[]], [[
700 State 16
702     4 A: 'a' . 'a'
703     5  | 'a' .  ['b']
705     'a'  shift, and go to state ]AT_COND_CASE([[canonical LR]], [[18]],
706                                               [[12]])[
708     ]AT_COND_CASE([[canonical LR]], [['b']],
709                   [[$default]])[  reduce using rule 5 (A)]AT_COND_CASE([[canonical LR]], [[
712 State 17
714     4 A: 'a' 'a' .  [$end]
716     $end  reduce using rule 4 (A)
719 State 18
721     4 A: 'a' 'a' .  ['b']
723     'b'  reduce using rule 4 (A)]])])[
726 dnl OTHER-CHECKS
729 dnl PARSER-EXIT-VALUE, PARSER-STDOUT, PARSER-STDERR
730 [AT_COND_CASE([[LALR]], [[1]], [[0]])],
732 [AT_COND_CASE([[LALR]],
733 [[syntax error
734 ]])])
736 AT_TEST_LR_TYPE([[Lane Split]],
737 [[%left 'a'
738 // Conflict resolution renders state 16 unreachable for canonical LR(1).  We
739 // keep it so that the parser table diff is easier to code.
740 %define lr.keep-unreachable-state]],
742 /* Similar to the last test case set but two states must be split.  */
743 S: 'a' A 'a' /* rule 1 */
744  | 'b' A 'b' /* rule 2 */
745  | 'c' c     /* rule 3 */
748 A: 'a' 'a' 'a' /* rule 4 */
749  | 'a' 'a'     /* rule 5 */
752 c: 'a' 'a' 'b' /* rule 6 */
753  | A           /* rule 7 */
757 dnl INPUT
758 [['b', 'a', 'a', 'a', 'b']],
760 dnl BISON-STDERR
763 dnl TABLES
764 [[State 0
766     0 $accept: . S $end
767     1 S: . 'a' A 'a'
768     2  | . 'b' A 'b'
769     3  | . 'c' c
771     'a'  shift, and go to state 1
772     'b'  shift, and go to state 2
773     'c'  shift, and go to state 3
775     S  go to state 4
778 State 1
780     1 S: 'a' . A 'a'
781     4 A: . 'a' 'a' 'a'
782     5  | . 'a' 'a'
784     'a'  shift, and go to state 5
786     A  go to state 6
789 State 2
791     2 S: 'b' . A 'b'
792     4 A: . 'a' 'a' 'a'
793     5  | . 'a' 'a'
795     'a'  shift, and go to state ]AT_COND_CASE([[LALR]], [[5]], [[18]])[
797     A  go to state 7
800 State 3
802     3 S: 'c' . c
803     4 A: . 'a' 'a' 'a'
804     5  | . 'a' 'a'
805     6 c: . 'a' 'a' 'b'
806     7  | . A
808     'a'  shift, and go to state 8
810     A  go to state 9
811     c  go to state 10
814 State 4
816     0 $accept: S . $end
818     $end  shift, and go to state 11
821 State 5
823     4 A: 'a' . 'a' 'a'
824     5  | 'a' . 'a'
826     'a'  shift, and go to state 12
829 State 6
831     1 S: 'a' A . 'a'
833     'a'  shift, and go to state 13
836 State 7
838     2 S: 'b' A . 'b'
840     'b'  shift, and go to state 14
843 State 8
845     4 A: 'a' . 'a' 'a'
846     5  | 'a' . 'a'
847     6 c: 'a' . 'a' 'b'
849     'a'  shift, and go to state 15
852 State 9
854     7 c: A .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
856     ]AT_COND_CASE([[canonical LR]], [[$end]],
857                   [[$default]])[  reduce using rule 7 (c)
860 State 10
862     3 S: 'c' c .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
864     ]AT_COND_CASE([[canonical LR]], [[$end]],
865                   [[$default]])[  reduce using rule 3 (S)
868 State 11
870     0 $accept: S $end .
872     $default  accept
875 State 12
877     4 A: 'a' 'a' . 'a'
878     5  | 'a' 'a' .  ]AT_COND_CASE([[LALR]], [[['a', 'b']]], [[['a']]])[
880     ]AT_COND_CASE([[canonical LR]], [['a']],
881                   [[$default]])[  reduce using rule 5 (A)
883     Conflict between rule 5 and token 'a' resolved as reduce (%left 'a').
886 State 13
888     1 S: 'a' A 'a' .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
890     ]AT_COND_CASE([[canonical LR]], [[$end]],
891                   [[$default]])[  reduce using rule 1 (S)
894 State 14
896     2 S: 'b' A 'b' .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
898     ]AT_COND_CASE([[canonical LR]], [[$end]],
899                   [[$default]])[  reduce using rule 2 (S)
902 State 15
904     4 A: 'a' 'a' . 'a'
905     5  | 'a' 'a' .  [$end]
906     6 c: 'a' 'a' . 'b'
908     'a'  shift, and go to state ]AT_COND_CASE([[canonical LR]], [[19]],
909                                               [[16]])[
910     'b'  shift, and go to state 17
912     ]AT_COND_CASE([[canonical LR]], [[$end]],
913                   [[$default]])[  reduce using rule 5 (A)
916 State 16
918     4 A: 'a' 'a' 'a' .]AT_COND_CASE([[canonical LR]], [[  ['a']]])[
920     ]AT_COND_CASE([[canonical LR]], [['a']],
921                   [[$default]])[  reduce using rule 4 (A)
924 State 17
926     6 c: 'a' 'a' 'b' .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
928     ]AT_COND_CASE([[canonical LR]], [[$end]],
929                   [[$default]])[  reduce using rule 6 (c)]AT_COND_CASE([[LALR]],
930                                                                        [[]], [[
933 State 18
935     4 A: 'a' . 'a' 'a'
936     5  | 'a' . 'a'
938     'a'  shift, and go to state ]AT_COND_CASE([[canonical LR]], [[20]],
939                                               [[19]])[
942 State 19]AT_COND_CASE([[canonical LR]], [[
944     4 A: 'a' 'a' 'a' .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
946     ]AT_COND_CASE([[canonical LR]], [[$end]],
947                   [[$default]])[  reduce using rule 4 (A)
950 State 20]])[
952     4 A: 'a' 'a' . 'a'
953     5  | 'a' 'a' .  ['b']
955     'a'  shift, and go to state ]AT_COND_CASE([[canonical LR]], [[21]],
956                                               [[16]])[
958     ]AT_COND_CASE([[canonical LR]], [['b']],
959                   [[$default]])[  reduce using rule 5 (A)]AT_COND_CASE([[canonical LR]], [[
962 State 21
964     4 A: 'a' 'a' 'a' .]AT_COND_CASE([[canonical LR]], [[  ['b']]])[
966     ]AT_COND_CASE([[canonical LR]], [['b']],
967                   [[$default]])[  reduce using rule 4 (A)]])])[
970 dnl OTHER-CHECKS
973 dnl PARSER-EXIT-VALUE, PARSER-STDOUT, PARSER-STDERR
974 [AT_COND_CASE([[LALR]], [[1]], [[0]])],
976 [AT_COND_CASE([[LALR]],
977 [[syntax error
978 ]])])
980 AT_TEST_LR_TYPE([[Complex Lane Split]],
981 [[%left 'a'
982 // Conflict resolution renders state 16 unreachable for canonical LR(1).  We
983 // keep it so that the parser table diff is easier to code.
984 %define lr.keep-unreachable-state]],
986 /* Similar to the last test case set but forseeing the S/R conflict from the
987    first state that must be split is becoming difficult.  Imagine if B were
988    even more complex.  Imagine if A had other RHS's ending in other
989    nonterminals.  */
990 S: 'a' A 'a'
991  | 'b' A 'b'
992  | 'c' c
994 A: 'a' 'a' B
996 B: 'a'
997  | %empty %prec 'a'
999 c: 'a' 'a' 'b'
1000  | A
1004 dnl INPUT
1005 [['b', 'a', 'a', 'a', 'b']],
1007 dnl BISON-STDERR
1010 dnl TABLES
1011 [[State 0
1013     0 $accept: . S $end
1014     1 S: . 'a' A 'a'
1015     2  | . 'b' A 'b'
1016     3  | . 'c' c
1018     'a'  shift, and go to state 1
1019     'b'  shift, and go to state 2
1020     'c'  shift, and go to state 3
1022     S  go to state 4
1025 State 1
1027     1 S: 'a' . A 'a'
1028     4 A: . 'a' 'a' B
1030     'a'  shift, and go to state 5
1032     A  go to state 6
1035 State 2
1037     2 S: 'b' . A 'b'
1038     4 A: . 'a' 'a' B
1040     'a'  shift, and go to state ]AT_COND_CASE([[LALR]], [[5]], [[19]])[
1042     A  go to state 7
1045 State 3
1047     3 S: 'c' . c
1048     4 A: . 'a' 'a' B
1049     7 c: . 'a' 'a' 'b'
1050     8  | . A
1052     'a'  shift, and go to state 8
1054     A  go to state 9
1055     c  go to state 10
1058 State 4
1060     0 $accept: S . $end
1062     $end  shift, and go to state 11
1065 State 5
1067     4 A: 'a' . 'a' B
1069     'a'  shift, and go to state 12
1072 State 6
1074     1 S: 'a' A . 'a'
1076     'a'  shift, and go to state 13
1079 State 7
1081     2 S: 'b' A . 'b'
1083     'b'  shift, and go to state 14
1086 State 8
1088     4 A: 'a' . 'a' B
1089     7 c: 'a' . 'a' 'b'
1091     'a'  shift, and go to state 15
1094 State 9
1096     8 c: A .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
1098     ]AT_COND_CASE([[canonical LR]], [[$end]],
1099                   [[$default]])[  reduce using rule 8 (c)
1102 State 10
1104     3 S: 'c' c .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
1106     ]AT_COND_CASE([[canonical LR]], [[$end]],
1107                   [[$default]])[  reduce using rule 3 (S)
1110 State 11
1112     0 $accept: S $end .
1114     $default  accept
1117 State 12
1119     4 A: 'a' 'a' . B
1120     5 B: . 'a'
1121     6  | . %empty  ]AT_COND_CASE([[LALR]], [[['a', 'b']]], [[['a']]])[
1123     ]AT_COND_CASE([[canonical LR]], [['a']],
1124                   [[$default]])[  reduce using rule 6 (B)
1126     B  go to state 17
1128     Conflict between rule 6 and token 'a' resolved as reduce (%left 'a').
1131 State 13
1133     1 S: 'a' A 'a' .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
1135     ]AT_COND_CASE([[canonical LR]], [[$end]],
1136                   [[$default]])[  reduce using rule 1 (S)
1139 State 14
1141     2 S: 'b' A 'b' .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
1143     ]AT_COND_CASE([[canonical LR]], [[$end]],
1144                   [[$default]])[  reduce using rule 2 (S)
1147 State 15
1149     4 A: 'a' 'a' . B
1150     5 B: . 'a'
1151     6  | . %empty  [$end]
1152     7 c: 'a' 'a' . 'b'
1154     'a'  shift, and go to state ]AT_COND_CASE([[canonical LR]], [[20]],
1155                                               [[16]])[
1156     'b'  shift, and go to state 18
1158     ]AT_COND_CASE([[canonical LR]], [[$end]],
1159                   [[$default]])[  reduce using rule 6 (B)
1161     B  go to state ]AT_COND_CASE([[canonical LR]], [[21]], [[17]])[
1164 State 16
1166     5 B: 'a' .]AT_COND_CASE([[canonical LR]], [[  ['a']]])[
1168     ]AT_COND_CASE([[canonical LR]], [['a']],
1169                   [[$default]])[  reduce using rule 5 (B)
1172 State 17
1174     4 A: 'a' 'a' B .]AT_COND_CASE([[canonical LR]], [[  ['a']]])[
1176     ]AT_COND_CASE([[canonical LR]], [['a']],
1177                   [[$default]])[  reduce using rule 4 (A)
1180 State 18
1182     7 c: 'a' 'a' 'b' .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
1184     ]AT_COND_CASE([[canonical LR]], [[$end]],
1185                   [[$default]])[  reduce using rule 7 (c)]AT_COND_CASE([[LALR]], [], [[
1188 State 19
1190     4 A: 'a' . 'a' B
1192     'a'  shift, and go to state ]AT_COND_CASE([[canonical LR]], [[22]],
1193                                               [[20]])[
1196 State 20]AT_COND_CASE([[canonical LR]], [[
1198     5 B: 'a' .  [$end]
1200     $end  reduce using rule 5 (B)
1203 State 21
1205     4 A: 'a' 'a' B .  [$end]
1207     $end  reduce using rule 4 (A)
1210 State 22]])[
1212     4 A: 'a' 'a' . B
1213     5 B: . 'a'
1214     6  | . %empty  ['b']
1216     'a'  shift, and go to state ]AT_COND_CASE([[canonical LR]], [[23]],
1217                                               [[16]])[
1219     ]AT_COND_CASE([[canonical LR]], [['b']],
1220                   [[$default]])[  reduce using rule 6 (B)
1222     B  go to state ]AT_COND_CASE([[canonical LR]], [[24
1225 State 23
1227     5 B: 'a' .  ['b']
1229     'b'  reduce using rule 5 (B)
1232 State 24
1234     4 A: 'a' 'a' B .  ['b']
1236     'b'  reduce using rule 4 (A)]], [[17]])])[
1239 dnl OTHER-CHECKS
1242 dnl PARSER-EXIT-VALUE, PARSER-STDOUT, PARSER-STDERR
1243 [AT_COND_CASE([[LALR]], [[1]], [[0]])],
1245 [AT_COND_CASE([[LALR]],
1246 [[syntax error
1247 ]])])
1249 AT_TEST_LR_TYPE([[Split During Added Lookahead Propagation]],
1250 [[%define lr.keep-unreachable-state]],
1252 /* The partial state chart diagram below is for LALR(1).  State 0 is the start
1253    state.  States are iterated for successor construction in numerical order.
1254    Transitions are downwards.
1256    State 13 has a R/R conflict that cannot be predicted by Bison's LR(1)
1257    algorithm using annotations alone.  That is, when state 11's successor on
1258    'd' is merged with state 5 (which is originally just state 1's successor on
1259    'd'), state 5's successor on 'e' must then be changed because the resulting
1260    lookaheads that propagate to it now make it incompatible with state 8's
1261    successor on 'e'.  In other words, state 13 must be split to avoid the
1262    conflict.
1264           0
1265         / | \
1266      a / c|  \ b
1267       1   3   2
1268       |   |   |
1269      d|   |c  | d
1270       |  11   |
1271       |   |   |
1272        \ /d   |
1273         5     8
1274          \    |
1275         e \  / e
1276            13
1277            R/R
1279    This grammar is designed carefully to make sure that, despite Bison's LR(1)
1280    algorithm's bread-first iteration of transitions to reconstruct states,
1281    state 11's successors are constructed after state 5's and state 8's.
1282    Otherwise (for example, if you remove the first 'c' in each of rules 6 and
1283    7), state 5's successor on 'e' would never be merged with state 8's, so the
1284    split of the resulting state 13 would never need to be performed.  */
1285 S: 'a' A 'f'
1286  | 'a' B
1287  | 'b' A 'f'
1288  | 'b' B 'g'
1289  | 'b' 'd'
1290  | 'c' 'c' A 'g'
1291  | 'c' 'c' B
1293 A: 'd' 'e' ;
1294 B: 'd' 'e' ;
1297 dnl INPUT
1298 [['b', 'd', 'e', 'g']],
1300 dnl BISON-STDERR
1301 [AT_COND_CASE([[LALR]],
1302 [[input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1303 input.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
1304 ]], [])],
1306 dnl TABLES
1307 [[State 0
1309     0 $accept: . S $end
1310     1 S: . 'a' A 'f'
1311     2  | . 'a' B
1312     3  | . 'b' A 'f'
1313     4  | . 'b' B 'g'
1314     5  | . 'b' 'd'
1315     6  | . 'c' 'c' A 'g'
1316     7  | . 'c' 'c' B
1318     'a'  shift, and go to state 1
1319     'b'  shift, and go to state 2
1320     'c'  shift, and go to state 3
1322     S  go to state 4
1325 State 1
1327     1 S: 'a' . A 'f'
1328     2  | 'a' . B
1329     8 A: . 'd' 'e'
1330     9 B: . 'd' 'e'
1332     'd'  shift, and go to state 5
1334     A  go to state 6
1335     B  go to state 7
1338 State 2
1340     3 S: 'b' . A 'f'
1341     4  | 'b' . B 'g'
1342     5  | 'b' . 'd'
1343     8 A: . 'd' 'e'
1344     9 B: . 'd' 'e'
1346     'd'  shift, and go to state 8
1348     A  go to state 9
1349     B  go to state 10
1352 State 3
1354     6 S: 'c' . 'c' A 'g'
1355     7  | 'c' . 'c' B
1357     'c'  shift, and go to state 11
1360 State 4
1362     0 $accept: S . $end
1364     $end  shift, and go to state 12
1367 State 5
1369     8 A: 'd' . 'e'
1370     9 B: 'd' . 'e'
1372     'e'  shift, and go to state ]AT_COND_CASE([[LALR]], [[13]],
1373                                                [[canonical LR]], [[13]],
1374                                                [[20]])[
1377 State 6
1379     1 S: 'a' A . 'f'
1381     'f'  shift, and go to state 14
1384 State 7
1386     2 S: 'a' B .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
1388     ]AT_COND_CASE([[canonical LR]], [[$end]],
1389                   [[$default]])[  reduce using rule 2 (S)
1392 State 8
1394     5 S: 'b' 'd' .  [$end]
1395     8 A: 'd' . 'e'
1396     9 B: 'd' . 'e'
1398     'e'  shift, and go to state ]AT_COND_CASE([[canonical LR]], [[20]],
1399                                               [[13]])[
1401     ]AT_COND_CASE([[canonical LR]], [[$end]],
1402                   [[$default]])[  reduce using rule 5 (S)
1405 State 9
1407     3 S: 'b' A . 'f'
1409     'f'  shift, and go to state 15
1412 State 10
1414     4 S: 'b' B . 'g'
1416     'g'  shift, and go to state 16
1419 State 11
1421     6 S: 'c' 'c' . A 'g'
1422     7  | 'c' 'c' . B
1423     8 A: . 'd' 'e'
1424     9 B: . 'd' 'e'
1426     'd'  shift, and go to state ]AT_COND_CASE([[canonical LR]], [[21]],
1427                                               [[5]])[
1429     A  go to state 17
1430     B  go to state 18
1433 State 12
1435     0 $accept: S $end .
1437     $default  accept]AT_COND_CASE([[LALR]], [[
1440 State 13
1442     8 A: 'd' 'e' .  ['f', 'g']
1443     9 B: 'd' 'e' .  [$end, 'g']
1445     $end      reduce using rule 9 (B)
1446     'g'       reduce using rule 8 (A)
1447     'g'       [reduce using rule 9 (B)]
1448     $default  reduce using rule 8 (A)]], [[
1451 State 13
1453     8 A: 'd' 'e' .  ['f']
1454     9 B: 'd' 'e' .  ]AT_COND_CASE([[canonical LR]], [[[$end]]], [[['g']]])[
1456     ]AT_COND_CASE([[canonical LR]], [[$end]],
1457                   [['g'     ]])[  reduce using rule 9 (B)
1458     ]AT_COND_CASE([[canonical LR]], [['f' ]],
1459                   [[$default]])[  reduce using rule 8 (A)]])[
1462 State 14
1464     1 S: 'a' A 'f' .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
1466     ]AT_COND_CASE([[canonical LR]], [[$end]],
1467                   [[$default]])[  reduce using rule 1 (S)
1470 State 15
1472     3 S: 'b' A 'f' .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
1474     ]AT_COND_CASE([[canonical LR]], [[$end]],
1475                   [[$default]])[  reduce using rule 3 (S)
1478 State 16
1480     4 S: 'b' B 'g' .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
1482     ]AT_COND_CASE([[canonical LR]], [[$end]],
1483                   [[$default]])[  reduce using rule 4 (S)
1486 State 17
1488     6 S: 'c' 'c' A . 'g'
1490     'g'  shift, and go to state 19
1493 State 18
1495     7 S: 'c' 'c' B .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
1497     ]AT_COND_CASE([[canonical LR]], [[$end]],
1498                   [[$default]])[  reduce using rule 7 (S)
1501 State 19
1503     6 S: 'c' 'c' A 'g' .]AT_COND_CASE([[canonical LR]], [[  [$end]]])[
1505     ]AT_COND_CASE([[canonical LR]], [[$end]],
1506                   [[$default]])[  reduce using rule 6 (S)]AT_COND_CASE([[LALR]],
1507                                                                        [[]], [[
1510 State 20]AT_COND_CASE([[canonical LR]], [[
1512     8 A: 'd' 'e' .  ['f']
1513     9 B: 'd' 'e' .  ['g']
1515     'f'  reduce using rule 8 (A)
1516     'g'  reduce using rule 9 (B)
1519 State 21
1521     8 A: 'd' . 'e'
1522     9 B: 'd' . 'e'
1524     'e'  shift, and go to state 22
1527 State 22
1529     8 A: 'd' 'e' .  ['g']
1530     9 B: 'd' 'e' .  [$end]
1532     $end  reduce using rule 9 (B)
1533     'g'   reduce using rule 8 (A)]], [[
1535     8 A: 'd' 'e' .  ['f', 'g']
1536     9 B: 'd' 'e' .  [$end]
1538     $end      reduce using rule 9 (B)
1539     $default  reduce using rule 8 (A)]])])[
1542 dnl OTHER-CHECKS
1545 dnl PARSER-EXIT-VALUE, PARSER-STDOUT, PARSER-STDERR
1546 [AT_COND_CASE([[LALR]], [[1]], [[0]])],
1548 [AT_COND_CASE([[LALR]],
1549 [[syntax error
1550 ]])])
1554 ## ------------------------------ ##
1555 ## %define lr.default-reduction.  ##
1556 ## ------------------------------ ##
1558 # AT_TEST_LR_DEFAULT_REDUCTIONS(GRAMMAR, INPUT, TABLES)
1559 # -----------------------------------------------------
1560 m4_define([AT_TEST_LR_DEFAULT_REDUCTIONS],
1562 AT_TEST_TABLES_AND_PARSE([[no %define lr.default-reduction]],
1563                          [[most]], [[]],
1564                          [[]],
1565                          [$1], [$2], [[]], [$3])
1566 AT_TEST_TABLES_AND_PARSE([[%define lr.default-reduction most]],
1567                          [[most]], [[]],
1568                          [[%define lr.default-reduction most]],
1569                          [$1], [$2], [[]], [$3])
1570 AT_TEST_TABLES_AND_PARSE([[%define lr.default-reduction consistent]],
1571                          [[consistent]], [[]],
1572                          [[%define lr.default-reduction consistent]],
1573                          [$1], [$2], [[]], [$3])
1574 AT_TEST_TABLES_AND_PARSE([[%define lr.default-reduction accepting]],
1575                          [[accepting]], [[]],
1576                          [[%define lr.default-reduction accepting]],
1577                          [$1], [$2], [[]], [$3])
1580 AT_TEST_LR_DEFAULT_REDUCTIONS([[
1581 /* The start state is consistent and has a shift on 'a' and no reductions.
1582    After pushing the b below, enter an inconsistent state that has a shift and
1583    one reduction with one lookahead.  */
1584 start:
1585     a b
1586   | a b 'a'
1587   | a c 'b'
1588   ;
1590 /* After shifting this 'a', enter a consistent state that has no shift and 1
1591    reduction with multiple lookaheads.  */
1592 a: 'a' ;
1594 /* After the previous reduction, enter an inconsistent state that has no shift
1595    and multiple reductions.  The first reduction has more lookaheads than the
1596    second, so the first should always be preferred as the default reduction if
1597    enabled.  The second reduction has one lookahead.  */
1598 b: %empty;
1599 c: %empty;
1601 dnl Visit each state mentioned above.
1602 [['a', 'a']],
1603 [[State 0
1605     0 $accept: . start $end
1606     1 start: . a b
1607     2      | . a b 'a'
1608     3      | . a c 'b'
1609     4 a: . 'a'
1611     'a'  shift, and go to state 1
1613     start  go to state 2
1614     a      go to state 3
1617 State 1
1619     4 a: 'a' .]AT_COND_CASE([[accepting]], [[  [$end, 'a', 'b']
1621     $end  reduce using rule 4 (a)
1622     'a'   reduce using rule 4 (a)
1623     'b'   reduce using rule 4 (a)]], [[
1625     $default  reduce using rule 4 (a)]])[
1628 State 2
1630     0 $accept: start . $end
1632     $end  shift, and go to state 4
1635 State 3
1637     1 start: a . b
1638     2      | a . b 'a'
1639     3      | a . c 'b'
1640     5 b: . %empty  [$end, 'a']
1641     6 c: . %empty  ['b']]AT_COND_CASE([[most]], [[
1643     'b'       reduce using rule 6 (c)
1644     $default  reduce using rule 5 (b)]], [[
1646     $end  reduce using rule 5 (b)
1647     'a'   reduce using rule 5 (b)
1648     'b'   reduce using rule 6 (c)]])[
1650     b  go to state 5
1651     c  go to state 6
1654 State 4
1656     0 $accept: start $end .
1658     $default  accept
1661 State 5
1663     1 start: a b .  [$end]
1664     2      | a b . 'a'
1666     'a'  shift, and go to state 7
1668     ]AT_COND_CASE([[most]], [[$default]],
1669                   [[$end]])[  reduce using rule 1 (start)
1672 State 6
1674     3 start: a c . 'b'
1676     'b'  shift, and go to state 8
1679 State 7
1681     2 start: a b 'a' .]AT_COND_CASE([[accepting]], [[  [$end]
1683     $end  reduce using rule 2 (start)]], [[
1685     $default  reduce using rule 2 (start)]])[
1688 State 8
1690     3 start: a c 'b' .]AT_COND_CASE([[accepting]], [[  [$end]
1692     $end  reduce using rule 3 (start)]], [[
1694     $default  reduce using rule 3 (start)]])[