]> git.saurik.com Git - bison.git/blame - tests/reduce.at
output: record what generated files are source or report files
[bison.git] / tests / reduce.at
CommitLineData
cb4956ee 1# Exercising Bison Grammar Reduction. -*- Autotest -*-
7d424de1 2
7d6bad19 3# Copyright (C) 2001-2002, 2007-2013 Free Software Foundation, Inc.
cb4956ee 4
f16b0819 5# This program is free software: you can redistribute it and/or modify
cb4956ee 6# it under the terms of the GNU General Public License as published by
f16b0819
PE
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
cb4956ee
AD
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
f16b0819 14#
cb4956ee 15# You should have received a copy of the GNU General Public License
f16b0819 16# along with this program. If not, see <http://www.gnu.org/licenses/>.
cb4956ee
AD
17
18AT_BANNER([[Grammar Reduction.]])
19
20
21## ------------------- ##
22## Useless Terminals. ##
23## ------------------- ##
24
25AT_SETUP([Useless Terminals])
26
27AT_DATA([[input.y]],
28[[%verbose
02975b9a 29%output "input.c"
cb4956ee
AD
30
31%token useless1
32%token useless2
33%token useless3
34%token useless4
35%token useless5
36%token useless6
37%token useless7
38%token useless8
39%token useless9
40
41%token useful
42%%
43exp: useful;
44]])
45
da730230 46AT_BISON_CHECK([[input.y]])
cb4956ee
AD
47
48AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0,
d80fb37a 49[[Terminals unused in grammar
cb4956ee
AD
50 useless1
51 useless2
52 useless3
53 useless4
54 useless5
55 useless6
56 useless7
57 useless8
58 useless9
59]])
60
61AT_CLEANUP
62
63
64
65## ---------------------- ##
66## Useless Nonterminals. ##
67## ---------------------- ##
68
69AT_SETUP([Useless Nonterminals])
70
71AT_DATA([[input.y]],
72[[%verbose
02975b9a 73%output "input.c"
cb4956ee
AD
74
75%nterm useless1
76%nterm useless2
77%nterm useless3
78%nterm useless4
79%nterm useless5
80%nterm useless6
81%nterm useless7
82%nterm useless8
83%nterm useless9
84
85%token useful
86%%
87exp: useful;
88]])
89
da730230 90AT_BISON_CHECK([[input.y]], 0, [],
73370a9d
VS
91[[input.y: warning: 9 nonterminals useless in grammar [-Wother]
92input.y:4.8-15: warning: nonterminal useless in grammar: useless1 [-Wother]
93input.y:5.8-15: warning: nonterminal useless in grammar: useless2 [-Wother]
94input.y:6.8-15: warning: nonterminal useless in grammar: useless3 [-Wother]
95input.y:7.8-15: warning: nonterminal useless in grammar: useless4 [-Wother]
96input.y:8.8-15: warning: nonterminal useless in grammar: useless5 [-Wother]
97input.y:9.8-15: warning: nonterminal useless in grammar: useless6 [-Wother]
98input.y:10.8-15: warning: nonterminal useless in grammar: useless7 [-Wother]
99input.y:11.8-15: warning: nonterminal useless in grammar: useless8 [-Wother]
100input.y:12.8-15: warning: nonterminal useless in grammar: useless9 [-Wother]
760b53a8 101]])
cb4956ee
AD
102
103AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0,
cff03fb2 104[[Nonterminals useless in grammar
cb4956ee
AD
105 useless1
106 useless2
107 useless3
108 useless4
109 useless5
110 useless6
111 useless7
112 useless8
113 useless9
114]])
115
116AT_CLEANUP
68f1e3ed
AD
117
118
119
120## --------------- ##
121## Useless Rules. ##
122## --------------- ##
123
124AT_SETUP([Useless Rules])
125
9757c359
AD
126AT_KEYWORDS([report])
127
68f1e3ed
AD
128AT_DATA([[input.y]],
129[[%verbose
02975b9a 130%output "input.c"
68f1e3ed
AD
131%token useful
132%%
133exp: useful;
134useless1: '1';
135useless2: '2';
136useless3: '3';
137useless4: '4';
138useless5: '5';
139useless6: '6';
140useless7: '7';
141useless8: '8';
142useless9: '9';
143]])
144
505ece51 145AT_BISON_CHECK([[-fcaret input.y]], 0, [],
f3ead217
TR
146[[input.y: warning: 9 nonterminals useless in grammar [-Wother]
147input.y: warning: 9 rules useless in grammar [-Wother]
148input.y:6.1-8: warning: nonterminal useless in grammar: useless1 [-Wother]
505ece51
TR
149 useless1: '1';
150 ^^^^^^^^
f3ead217 151input.y:7.1-8: warning: nonterminal useless in grammar: useless2 [-Wother]
505ece51
TR
152 useless2: '2';
153 ^^^^^^^^
f3ead217 154input.y:8.1-8: warning: nonterminal useless in grammar: useless3 [-Wother]
505ece51
TR
155 useless3: '3';
156 ^^^^^^^^
f3ead217 157input.y:9.1-8: warning: nonterminal useless in grammar: useless4 [-Wother]
505ece51
TR
158 useless4: '4';
159 ^^^^^^^^
f3ead217 160input.y:10.1-8: warning: nonterminal useless in grammar: useless5 [-Wother]
505ece51
TR
161 useless5: '5';
162 ^^^^^^^^
f3ead217 163input.y:11.1-8: warning: nonterminal useless in grammar: useless6 [-Wother]
505ece51
TR
164 useless6: '6';
165 ^^^^^^^^
f3ead217 166input.y:12.1-8: warning: nonterminal useless in grammar: useless7 [-Wother]
505ece51
TR
167 useless7: '7';
168 ^^^^^^^^
f3ead217 169input.y:13.1-8: warning: nonterminal useless in grammar: useless8 [-Wother]
505ece51
TR
170 useless8: '8';
171 ^^^^^^^^
f3ead217 172input.y:14.1-8: warning: nonterminal useless in grammar: useless9 [-Wother]
505ece51
TR
173 useless9: '9';
174 ^^^^^^^^
f3ead217 175input.y:6.11-13: warning: rule useless in grammar [-Wother]
505ece51
TR
176 useless1: '1';
177 ^^^
f3ead217 178input.y:7.11-13: warning: rule useless in grammar [-Wother]
505ece51
TR
179 useless2: '2';
180 ^^^
f3ead217 181input.y:8.11-13: warning: rule useless in grammar [-Wother]
505ece51
TR
182 useless3: '3';
183 ^^^
f3ead217 184input.y:9.11-13: warning: rule useless in grammar [-Wother]
505ece51
TR
185 useless4: '4';
186 ^^^
f3ead217 187input.y:10.11-13: warning: rule useless in grammar [-Wother]
505ece51
TR
188 useless5: '5';
189 ^^^
f3ead217 190input.y:11.11-13: warning: rule useless in grammar [-Wother]
505ece51
TR
191 useless6: '6';
192 ^^^
f3ead217 193input.y:12.11-13: warning: rule useless in grammar [-Wother]
505ece51
TR
194 useless7: '7';
195 ^^^
f3ead217 196input.y:13.11-13: warning: rule useless in grammar [-Wother]
505ece51
TR
197 useless8: '8';
198 ^^^
f3ead217 199input.y:14.11-13: warning: rule useless in grammar [-Wother]
505ece51
TR
200 useless9: '9';
201 ^^^
202]])
203
68f1e3ed
AD
204
205AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0,
cff03fb2 206[[Nonterminals useless in grammar
68f1e3ed
AD
207 useless1
208 useless2
209 useless3
210 useless4
211 useless5
212 useless6
213 useless7
214 useless8
215 useless9
d80fb37a 216Terminals unused in grammar
68f1e3ed
AD
217 '1'
218 '2'
219 '3'
220 '4'
221 '5'
222 '6'
223 '7'
224 '8'
225 '9'
cff03fb2 226Rules useless in grammar
9757c359
AD
227 2 useless1: '1'
228 3 useless2: '2'
229 4 useless3: '3'
230 5 useless4: '4'
231 6 useless5: '5'
232 7 useless6: '6'
233 8 useless7: '7'
234 9 useless8: '8'
235 10 useless9: '9'
68f1e3ed
AD
236]])
237
238AT_CLEANUP
239
240
241
c3b407f4
AD
242## ------------------- ##
243## Reduced Automaton. ##
244## ------------------- ##
245
246# Check that the automaton is that as the for the grammar reduced by
247# hand.
248
249AT_SETUP([Reduced Automaton])
250
9757c359
AD
251AT_KEYWORDS([report])
252
c3b407f4
AD
253# The non reduced grammar.
254# ------------------------
255AT_DATA([[not-reduced.y]],
256[[/* A useless token. */
257%token useless_token
258/* A useful one. */
259%token useful
260%verbose
02975b9a 261%output "not-reduced.c"
c3b407f4
AD
262
263%%
264
265exp: useful { /* A useful action. */ }
266 | non_productive { /* A non productive action. */ }
267 ;
268
269not_reachable: useful { /* A not reachable action. */ }
270 ;
271
272non_productive: non_productive useless_token
273 { /* Another non productive action. */ }
274 ;
e9955c83 275%%
c3b407f4
AD
276]])
277
505ece51 278AT_BISON_CHECK([[-fcaret not-reduced.y]], 0, [],
f3ead217
TR
279[[not-reduced.y: warning: 2 nonterminals useless in grammar [-Wother]
280not-reduced.y: warning: 3 rules useless in grammar [-Wother]
281not-reduced.y:14.1-13: warning: nonterminal useless in grammar: not_reachable [-Wother]
505ece51
TR
282 not_reachable: useful { /* A not reachable action. */ }
283 ^^^^^^^^^^^^^
f3ead217 284not-reduced.y:11.6-19: warning: nonterminal useless in grammar: non_productive [-Wother]
505ece51
TR
285 | non_productive { /* A non productive action. */ }
286 ^^^^^^^^^^^^^^
f3ead217 287not-reduced.y:11.6-57: warning: rule useless in grammar [-Wother]
505ece51
TR
288 | non_productive { /* A non productive action. */ }
289 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
f3ead217 290not-reduced.y:14.16-56: warning: rule useless in grammar [-Wother]
505ece51
TR
291 not_reachable: useful { /* A not reachable action. */ }
292 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
f3ead217 293not-reduced.y:17.17-18.63: warning: rule useless in grammar [-Wother]
505ece51
TR
294 non_productive: non_productive useless_token
295 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
296]])
297
c3b407f4 298AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' not-reduced.output]], 0,
cff03fb2 299[[Nonterminals useless in grammar
c3b407f4
AD
300 not_reachable
301 non_productive
d80fb37a 302Terminals unused in grammar
c3b407f4 303 useless_token
cff03fb2 304Rules useless in grammar
9757c359
AD
305 2 exp: non_productive
306 3 not_reachable: useful
307 4 non_productive: non_productive useless_token
c3b407f4
AD
308]])
309
310# The reduced grammar.
311# --------------------
312AT_DATA([[reduced.y]],
313[[/* A useless token. */
314%token useless_token
315/* A useful one. */
316%token useful
317%verbose
02975b9a 318%output "reduced.c"
c3b407f4
AD
319
320%%
321
322exp: useful { /* A useful action. */ }
323// | non_productive { /* A non productive action. */ } */
324 ;
325
326//not_reachable: useful { /* A not reachable action. */ }
327// ;
328
329//non_productive: non_productive useless_token
330// { /* Another non productive action. */ }
331// ;
e9955c83 332%%
c3b407f4
AD
333]])
334
da730230 335AT_BISON_CHECK([[reduced.y]])
c3b407f4
AD
336
337# Comparing the parsers.
338cp reduced.c expout
339AT_CHECK([sed 's/not-reduced/reduced/g' not-reduced.c], 0, [expout])
340
341AT_CLEANUP
342
343
344
68f1e3ed
AD
345## ------------------- ##
346## Underivable Rules. ##
347## ------------------- ##
348
349AT_SETUP([Underivable Rules])
350
9757c359
AD
351AT_KEYWORDS([report])
352
68f1e3ed
AD
353AT_DATA([[input.y]],
354[[%verbose
02975b9a 355%output "input.c"
68f1e3ed
AD
356%token useful
357%%
358exp: useful | underivable;
359underivable: indirection;
360indirection: underivable;
361]])
362
da730230 363AT_BISON_CHECK([[input.y]], 0, [],
73370a9d
VS
364[[input.y: warning: 2 nonterminals useless in grammar [-Wother]
365input.y: warning: 3 rules useless in grammar [-Wother]
366input.y:5.15-25: warning: nonterminal useless in grammar: underivable [-Wother]
367input.y:6.14-24: warning: nonterminal useless in grammar: indirection [-Wother]
fec5f3c0
AD
368input.y:5.15-25: warning: rule useless in grammar [-Wother]
369input.y:6.14-24: warning: rule useless in grammar [-Wother]
370input.y:7.14-24: warning: rule useless in grammar [-Wother]
68f1e3ed
AD
371]])
372
373AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0,
cff03fb2 374[[Nonterminals useless in grammar
68f1e3ed
AD
375 underivable
376 indirection
cff03fb2 377Rules useless in grammar
9757c359
AD
378 2 exp: underivable
379 3 underivable: indirection
380 4 indirection: underivable
68f1e3ed
AD
381]])
382
383AT_CLEANUP
1bfb97db
AD
384
385
386
387## ---------------- ##
388## Empty Language. ##
389## ---------------- ##
390
391AT_SETUP([Empty Language])
392
393AT_DATA([[input.y]],
02975b9a 394[[%output "input.c"
1bfb97db
AD
395%%
396exp: exp;
397]])
398
da730230 399AT_BISON_CHECK([[input.y]], 1, [],
73370a9d
VS
400[[input.y: warning: 2 nonterminals useless in grammar [-Wother]
401input.y: warning: 2 rules useless in grammar [-Wother]
1bfb97db
AD
402input.y:3.1-3: fatal error: start symbol exp does not derive any sentence
403]])
404
405AT_CLEANUP
7254f6a8
JD
406
407
408
db34f798
JD
409## ----------------- ##
410## %define lr.type. ##
411## ----------------- ##
412
413# AT_TEST_LR_TYPE(DESCRIPTION,
414# DECLS, GRAMMAR, INPUT,
415# BISON-STDERR, TABLES,
416# [OTHER-CHECKS],
417# [PARSER-EXIT-VALUE],
418# [PARSER-STDOUT], [PARSER-STDERR])
419# -------------------------------------------------
420m4_define([AT_TEST_LR_TYPE],
421[
422AT_TEST_TABLES_AND_PARSE([[no %define lr.type: ]$1],
423 [[LALR]], [[]],
424 [$2], m4_shiftn(2, $@))
cf499cff 425AT_TEST_TABLES_AND_PARSE([[%define lr.type lalr: ]$1],
db34f798 426 [[LALR]], [[]],
cf499cff 427 [[%define lr.type lalr
db34f798
JD
428]$2],
429 m4_shiftn(2, $@))
cf499cff 430AT_TEST_TABLES_AND_PARSE([[%define lr.type ielr: ]$1],
db34f798 431 [[IELR]], [[]],
cf499cff 432 [[%define lr.type ielr
db34f798
JD
433]$2],
434 m4_shiftn(2, $@))
cf499cff 435AT_TEST_TABLES_AND_PARSE([[%define lr.type canonical-lr: ]$1],
db34f798 436 [[canonical LR]], [[]],
cf499cff 437 [[%define lr.type canonical-lr
db34f798
JD
438]$2],
439 m4_shiftn(2, $@))
440])
441
442AT_TEST_LR_TYPE([[Single State Split]],
443[[%left 'a'
444// Conflict resolution renders state 12 unreachable for canonical LR(1). We
445// keep it so that the paser table diff is easier to code.
f3bc3386 446%define lr.keep-unreachable-state]],
db34f798
JD
447[[
448S: 'a' A 'a' /* rule 1 */
449 | 'b' A 'b' /* rule 2 */
450 | 'c' c /* rule 3 */
451 ;
452
453/* A conflict should appear after the first 'a' in rules 4 and 5 but only after
454 having shifted the first 'a' in rule 1. However, when LALR(1) merging is
455 chosen, the state containing that conflict is reused after having seen the
456 first 'b' in rule 2 and then the first 'a' in rules 4 and 5. In both cases,
457 because of the merged state, if the next token is an 'a', the %left forces a
458 reduction action with rule 5. In the latter case, only a shift is actually
459 grammatically correct. Thus, the parser would report a syntax error for the
460 grammatically correct sentence "baab" because it would encounter a syntax
461 error after that incorrect reduction.
462
463 Despite not being LALR(1), Menhir version 20070322 suffers from this problem
464 as well. It uses David Pager's weak compatibility test for merging states.
465 Bison and Menhir accept non-LR(1) grammars with conflict resolution. Pager
466 designed his algorithm only for LR(1) grammars. */
467A: 'a' 'a' /* rule 4 */
468 | 'a' /* rule 5 */
469 ;
470
471/* Rule 3, rule 6, and rule 7 ensure that Bison does not report rule 4 as
472 useless after conflict resolution. This proves that, even though LALR(1)
473 generates incorrect parser tables sometimes, Bison will not necessarily
474 produce any warning to help the user realize it. */
475c: 'a' 'b' /* rule 6 */
476 | A /* rule 7 */
477 ;
478]],
479
480dnl INPUT
481[['b', 'a', 'a', 'b']],
482
483dnl BISON-STDERR
484[],
485
486dnl TABLES
d42fe46e 487[[State 0
db34f798
JD
488
489 0 $accept: . S $end
490 1 S: . 'a' A 'a'
491 2 | . 'b' A 'b'
492 3 | . 'c' c
493
494 'a' shift, and go to state 1
495 'b' shift, and go to state 2
496 'c' shift, and go to state 3
497
498 S go to state 4
499
500
d42fe46e 501State 1
db34f798
JD
502
503 1 S: 'a' . A 'a'
504 4 A: . 'a' 'a'
505 5 | . 'a'
506
507 'a' shift, and go to state 5
508
509 A go to state 6
510
511
d42fe46e 512State 2
db34f798
JD
513
514 2 S: 'b' . A 'b'
515 4 A: . 'a' 'a'
516 5 | . 'a'
517
518 'a' shift, and go to state ]AT_COND_CASE([[LALR]], [[5]], [[16]])[
519
520 A go to state 7
521
522
d42fe46e 523State 3
db34f798
JD
524
525 3 S: 'c' . c
526 4 A: . 'a' 'a'
527 5 | . 'a'
528 6 c: . 'a' 'b'
529 7 | . A
530
531 'a' shift, and go to state 8
532
533 A go to state 9
534 c go to state 10
535
536
d42fe46e 537State 4
db34f798
JD
538
539 0 $accept: S . $end
540
541 $end shift, and go to state 11
542
543
d42fe46e 544State 5
db34f798
JD
545
546 4 A: 'a' . 'a'
547 5 | 'a' . ]AT_COND_CASE([[LALR]], [[['a', 'b']]], [[['a']]])[
548
549 ]AT_COND_CASE([[canonical LR]], [['a']],
550 [[$default]])[ reduce using rule 5 (A)
551
552 Conflict between rule 5 and token 'a' resolved as reduce (%left 'a').
553
554
d42fe46e 555State 6
db34f798
JD
556
557 1 S: 'a' A . 'a'
558
559 'a' shift, and go to state 13
560
561
d42fe46e 562State 7
db34f798
JD
563
564 2 S: 'b' A . 'b'
565
566 'b' shift, and go to state 14
567
568
d42fe46e 569State 8
db34f798
JD
570
571 4 A: 'a' . 'a'
572 5 | 'a' . [$end]
573 6 c: 'a' . 'b'
574
575 'a' shift, and go to state ]AT_COND_CASE([[canonical LR]], [[17]],
576 [[12]])[
577 'b' shift, and go to state 15
578
579 ]AT_COND_CASE([[canonical LR]], [[$end]],
580 [[$default]])[ reduce using rule 5 (A)
581
582
d42fe46e 583State 9
db34f798
JD
584
585 7 c: A .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
586
587 ]AT_COND_CASE([[canonical LR]], [[$end]],
588 [[$default]])[ reduce using rule 7 (c)
589
590
d42fe46e 591State 10
db34f798
JD
592
593 3 S: 'c' c .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
594
595 ]AT_COND_CASE([[canonical LR]], [[$end]],
596 [[$default]])[ reduce using rule 3 (S)
597
598
d42fe46e 599State 11
db34f798
JD
600
601 0 $accept: S $end .
602
603 $default accept
604
605
d42fe46e 606State 12
db34f798
JD
607
608 4 A: 'a' 'a' .]AT_COND_CASE([[canonical LR]], [[ ['a']]])[
609
610 ]AT_COND_CASE([[canonical LR]], [['a']],
611 [[$default]])[ reduce using rule 4 (A)
612
613
d42fe46e 614State 13
db34f798
JD
615
616 1 S: 'a' A 'a' .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
617
618 ]AT_COND_CASE([[canonical LR]], [[$end]],
619 [[$default]])[ reduce using rule 1 (S)
620
621
d42fe46e 622State 14
db34f798
JD
623
624 2 S: 'b' A 'b' .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
625
626 ]AT_COND_CASE([[canonical LR]], [[$end]],
627 [[$default]])[ reduce using rule 2 (S)
628
629
d42fe46e 630State 15
db34f798
JD
631
632 6 c: 'a' 'b' .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
633
634 ]AT_COND_CASE([[canonical LR]], [[$end]],
635 [[$default]])[ reduce using rule 6 (c)]AT_COND_CASE([[LALR]],
636 [[]], [[
637
638
d42fe46e 639State 16
db34f798
JD
640
641 4 A: 'a' . 'a'
642 5 | 'a' . ['b']
643
644 'a' shift, and go to state ]AT_COND_CASE([[canonical LR]], [[18]],
645 [[12]])[
646
647 ]AT_COND_CASE([[canonical LR]], [['b']],
648 [[$default]])[ reduce using rule 5 (A)]AT_COND_CASE([[canonical LR]], [[
649
650
d42fe46e 651State 17
db34f798
JD
652
653 4 A: 'a' 'a' . [$end]
654
655 $end reduce using rule 4 (A)
656
657
d42fe46e 658State 18
db34f798
JD
659
660 4 A: 'a' 'a' . ['b']
661
662 'b' reduce using rule 4 (A)]])])[
663]],
664
665dnl OTHER-CHECKS
666[],
667
668dnl PARSER-EXIT-VALUE, PARSER-STDOUT, PARSER-STDERR
669[AT_COND_CASE([[LALR]], [[1]], [[0]])],
670[],
671[AT_COND_CASE([[LALR]],
672[[syntax error
673]])])
674
675AT_TEST_LR_TYPE([[Lane Split]],
676[[%left 'a'
677// Conflict resolution renders state 16 unreachable for canonical LR(1). We
678// keep it so that the paser table diff is easier to code.
f3bc3386 679%define lr.keep-unreachable-state]],
db34f798
JD
680[[
681/* Similar to the last test case set but two states must be split. */
682S: 'a' A 'a' /* rule 1 */
683 | 'b' A 'b' /* rule 2 */
684 | 'c' c /* rule 3 */
685 ;
686
687A: 'a' 'a' 'a' /* rule 4 */
688 | 'a' 'a' /* rule 5 */
689 ;
690
691c: 'a' 'a' 'b' /* rule 6 */
692 | A /* rule 7 */
693 ;
694]],
695
696dnl INPUT
697[['b', 'a', 'a', 'a', 'b']],
698
699dnl BISON-STDERR
700[],
701
702dnl TABLES
d42fe46e 703[[State 0
db34f798
JD
704
705 0 $accept: . S $end
706 1 S: . 'a' A 'a'
707 2 | . 'b' A 'b'
708 3 | . 'c' c
709
710 'a' shift, and go to state 1
711 'b' shift, and go to state 2
712 'c' shift, and go to state 3
713
714 S go to state 4
715
716
d42fe46e 717State 1
db34f798
JD
718
719 1 S: 'a' . A 'a'
720 4 A: . 'a' 'a' 'a'
721 5 | . 'a' 'a'
722
723 'a' shift, and go to state 5
724
725 A go to state 6
726
727
d42fe46e 728State 2
db34f798
JD
729
730 2 S: 'b' . A 'b'
731 4 A: . 'a' 'a' 'a'
732 5 | . 'a' 'a'
733
734 'a' shift, and go to state ]AT_COND_CASE([[LALR]], [[5]], [[18]])[
735
736 A go to state 7
737
738
d42fe46e 739State 3
db34f798
JD
740
741 3 S: 'c' . c
742 4 A: . 'a' 'a' 'a'
743 5 | . 'a' 'a'
744 6 c: . 'a' 'a' 'b'
745 7 | . A
746
747 'a' shift, and go to state 8
748
749 A go to state 9
750 c go to state 10
751
752
d42fe46e 753State 4
db34f798
JD
754
755 0 $accept: S . $end
756
757 $end shift, and go to state 11
758
759
d42fe46e 760State 5
db34f798
JD
761
762 4 A: 'a' . 'a' 'a'
763 5 | 'a' . 'a'
764
765 'a' shift, and go to state 12
766
767
d42fe46e 768State 6
db34f798
JD
769
770 1 S: 'a' A . 'a'
771
772 'a' shift, and go to state 13
773
774
d42fe46e 775State 7
db34f798
JD
776
777 2 S: 'b' A . 'b'
778
779 'b' shift, and go to state 14
780
781
d42fe46e 782State 8
db34f798
JD
783
784 4 A: 'a' . 'a' 'a'
785 5 | 'a' . 'a'
786 6 c: 'a' . 'a' 'b'
787
788 'a' shift, and go to state 15
789
790
d42fe46e 791State 9
db34f798
JD
792
793 7 c: A .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
794
795 ]AT_COND_CASE([[canonical LR]], [[$end]],
796 [[$default]])[ reduce using rule 7 (c)
797
798
d42fe46e 799State 10
db34f798
JD
800
801 3 S: 'c' c .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
802
803 ]AT_COND_CASE([[canonical LR]], [[$end]],
804 [[$default]])[ reduce using rule 3 (S)
805
806
d42fe46e 807State 11
db34f798
JD
808
809 0 $accept: S $end .
810
811 $default accept
812
813
d42fe46e 814State 12
db34f798
JD
815
816 4 A: 'a' 'a' . 'a'
817 5 | 'a' 'a' . ]AT_COND_CASE([[LALR]], [[['a', 'b']]], [[['a']]])[
818
819 ]AT_COND_CASE([[canonical LR]], [['a']],
820 [[$default]])[ reduce using rule 5 (A)
821
822 Conflict between rule 5 and token 'a' resolved as reduce (%left 'a').
823
824
d42fe46e 825State 13
db34f798
JD
826
827 1 S: 'a' A 'a' .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
828
829 ]AT_COND_CASE([[canonical LR]], [[$end]],
830 [[$default]])[ reduce using rule 1 (S)
831
832
d42fe46e 833State 14
db34f798
JD
834
835 2 S: 'b' A 'b' .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
836
837 ]AT_COND_CASE([[canonical LR]], [[$end]],
838 [[$default]])[ reduce using rule 2 (S)
839
840
d42fe46e 841State 15
db34f798
JD
842
843 4 A: 'a' 'a' . 'a'
844 5 | 'a' 'a' . [$end]
845 6 c: 'a' 'a' . 'b'
846
847 'a' shift, and go to state ]AT_COND_CASE([[canonical LR]], [[19]],
848 [[16]])[
849 'b' shift, and go to state 17
850
851 ]AT_COND_CASE([[canonical LR]], [[$end]],
852 [[$default]])[ reduce using rule 5 (A)
853
854
d42fe46e 855State 16
db34f798
JD
856
857 4 A: 'a' 'a' 'a' .]AT_COND_CASE([[canonical LR]], [[ ['a']]])[
858
859 ]AT_COND_CASE([[canonical LR]], [['a']],
860 [[$default]])[ reduce using rule 4 (A)
861
862
d42fe46e 863State 17
db34f798
JD
864
865 6 c: 'a' 'a' 'b' .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
866
867 ]AT_COND_CASE([[canonical LR]], [[$end]],
868 [[$default]])[ reduce using rule 6 (c)]AT_COND_CASE([[LALR]],
869 [[]], [[
870
871
d42fe46e 872State 18
db34f798
JD
873
874 4 A: 'a' . 'a' 'a'
875 5 | 'a' . 'a'
876
877 'a' shift, and go to state ]AT_COND_CASE([[canonical LR]], [[20]],
878 [[19]])[
879
880
d42fe46e 881State 19]AT_COND_CASE([[canonical LR]], [[
db34f798
JD
882
883 4 A: 'a' 'a' 'a' .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
884
885 ]AT_COND_CASE([[canonical LR]], [[$end]],
886 [[$default]])[ reduce using rule 4 (A)
887
888
d42fe46e 889State 20]])[
db34f798
JD
890
891 4 A: 'a' 'a' . 'a'
892 5 | 'a' 'a' . ['b']
893
894 'a' shift, and go to state ]AT_COND_CASE([[canonical LR]], [[21]],
895 [[16]])[
896
897 ]AT_COND_CASE([[canonical LR]], [['b']],
898 [[$default]])[ reduce using rule 5 (A)]AT_COND_CASE([[canonical LR]], [[
899
900
d42fe46e 901State 21
db34f798
JD
902
903 4 A: 'a' 'a' 'a' .]AT_COND_CASE([[canonical LR]], [[ ['b']]])[
904
905 ]AT_COND_CASE([[canonical LR]], [['b']],
906 [[$default]])[ reduce using rule 4 (A)]])])[
907]],
908
909dnl OTHER-CHECKS
910[],
911
912dnl PARSER-EXIT-VALUE, PARSER-STDOUT, PARSER-STDERR
913[AT_COND_CASE([[LALR]], [[1]], [[0]])],
914[],
915[AT_COND_CASE([[LALR]],
916[[syntax error
917]])])
918
919AT_TEST_LR_TYPE([[Complex Lane Split]],
920[[%left 'a'
921// Conflict resolution renders state 16 unreachable for canonical LR(1). We
922// keep it so that the paser table diff is easier to code.
f3bc3386 923%define lr.keep-unreachable-state]],
db34f798
JD
924[[
925/* Similar to the last test case set but forseeing the S/R conflict from the
926 first state that must be split is becoming difficult. Imagine if B were
927 even more complex. Imagine if A had other RHS's ending in other
928 nonterminals. */
929S: 'a' A 'a'
930 | 'b' A 'b'
931 | 'c' c
932 ;
933A: 'a' 'a' B
934 ;
935B: 'a'
684083f0 936 | %empty %prec 'a'
db34f798
JD
937 ;
938c: 'a' 'a' 'b'
939 | A
940 ;
941]],
942
943dnl INPUT
944[['b', 'a', 'a', 'a', 'b']],
945
946dnl BISON-STDERR
947[],
948
949dnl TABLES
d42fe46e 950[[State 0
db34f798
JD
951
952 0 $accept: . S $end
953 1 S: . 'a' A 'a'
954 2 | . 'b' A 'b'
955 3 | . 'c' c
956
957 'a' shift, and go to state 1
958 'b' shift, and go to state 2
959 'c' shift, and go to state 3
960
961 S go to state 4
962
963
d42fe46e 964State 1
db34f798
JD
965
966 1 S: 'a' . A 'a'
967 4 A: . 'a' 'a' B
968
969 'a' shift, and go to state 5
970
971 A go to state 6
972
973
d42fe46e 974State 2
db34f798
JD
975
976 2 S: 'b' . A 'b'
977 4 A: . 'a' 'a' B
978
979 'a' shift, and go to state ]AT_COND_CASE([[LALR]], [[5]], [[19]])[
980
981 A go to state 7
982
983
d42fe46e 984State 3
db34f798
JD
985
986 3 S: 'c' . c
987 4 A: . 'a' 'a' B
988 7 c: . 'a' 'a' 'b'
989 8 | . A
990
991 'a' shift, and go to state 8
992
993 A go to state 9
994 c go to state 10
995
996
d42fe46e 997State 4
db34f798
JD
998
999 0 $accept: S . $end
1000
1001 $end shift, and go to state 11
1002
1003
d42fe46e 1004State 5
db34f798
JD
1005
1006 4 A: 'a' . 'a' B
1007
1008 'a' shift, and go to state 12
1009
1010
d42fe46e 1011State 6
db34f798
JD
1012
1013 1 S: 'a' A . 'a'
1014
1015 'a' shift, and go to state 13
1016
1017
d42fe46e 1018State 7
db34f798
JD
1019
1020 2 S: 'b' A . 'b'
1021
1022 'b' shift, and go to state 14
1023
1024
d42fe46e 1025State 8
db34f798
JD
1026
1027 4 A: 'a' . 'a' B
1028 7 c: 'a' . 'a' 'b'
1029
1030 'a' shift, and go to state 15
1031
1032
d42fe46e 1033State 9
db34f798
JD
1034
1035 8 c: A .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
1036
1037 ]AT_COND_CASE([[canonical LR]], [[$end]],
1038 [[$default]])[ reduce using rule 8 (c)
1039
1040
d42fe46e 1041State 10
db34f798
JD
1042
1043 3 S: 'c' c .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
1044
1045 ]AT_COND_CASE([[canonical LR]], [[$end]],
1046 [[$default]])[ reduce using rule 3 (S)
1047
1048
d42fe46e 1049State 11
db34f798
JD
1050
1051 0 $accept: S $end .
1052
1053 $default accept
1054
1055
d42fe46e 1056State 12
db34f798
JD
1057
1058 4 A: 'a' 'a' . B
1059 5 B: . 'a'
21cf8039 1060 6 | . %empty ]AT_COND_CASE([[LALR]], [[['a', 'b']]], [[['a']]])[
db34f798
JD
1061
1062 ]AT_COND_CASE([[canonical LR]], [['a']],
1063 [[$default]])[ reduce using rule 6 (B)
1064
1065 B go to state 17
1066
1067 Conflict between rule 6 and token 'a' resolved as reduce (%left 'a').
1068
1069
d42fe46e 1070State 13
db34f798
JD
1071
1072 1 S: 'a' A 'a' .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
1073
1074 ]AT_COND_CASE([[canonical LR]], [[$end]],
1075 [[$default]])[ reduce using rule 1 (S)
1076
1077
d42fe46e 1078State 14
db34f798
JD
1079
1080 2 S: 'b' A 'b' .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
1081
1082 ]AT_COND_CASE([[canonical LR]], [[$end]],
1083 [[$default]])[ reduce using rule 2 (S)
1084
1085
d42fe46e 1086State 15
db34f798
JD
1087
1088 4 A: 'a' 'a' . B
1089 5 B: . 'a'
21cf8039 1090 6 | . %empty [$end]
db34f798
JD
1091 7 c: 'a' 'a' . 'b'
1092
1093 'a' shift, and go to state ]AT_COND_CASE([[canonical LR]], [[20]],
1094 [[16]])[
1095 'b' shift, and go to state 18
1096
1097 ]AT_COND_CASE([[canonical LR]], [[$end]],
1098 [[$default]])[ reduce using rule 6 (B)
1099
1100 B go to state ]AT_COND_CASE([[canonical LR]], [[21]], [[17]])[
1101
1102
d42fe46e 1103State 16
db34f798
JD
1104
1105 5 B: 'a' .]AT_COND_CASE([[canonical LR]], [[ ['a']]])[
1106
1107 ]AT_COND_CASE([[canonical LR]], [['a']],
1108 [[$default]])[ reduce using rule 5 (B)
1109
1110
d42fe46e 1111State 17
db34f798
JD
1112
1113 4 A: 'a' 'a' B .]AT_COND_CASE([[canonical LR]], [[ ['a']]])[
1114
1115 ]AT_COND_CASE([[canonical LR]], [['a']],
1116 [[$default]])[ reduce using rule 4 (A)
1117
1118
d42fe46e 1119State 18
db34f798
JD
1120
1121 7 c: 'a' 'a' 'b' .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
1122
1123 ]AT_COND_CASE([[canonical LR]], [[$end]],
1124 [[$default]])[ reduce using rule 7 (c)]AT_COND_CASE([[LALR]], [], [[
1125
1126
d42fe46e 1127State 19
db34f798
JD
1128
1129 4 A: 'a' . 'a' B
1130
1131 'a' shift, and go to state ]AT_COND_CASE([[canonical LR]], [[22]],
1132 [[20]])[
1133
1134
d42fe46e 1135State 20]AT_COND_CASE([[canonical LR]], [[
db34f798
JD
1136
1137 5 B: 'a' . [$end]
1138
1139 $end reduce using rule 5 (B)
1140
1141
d42fe46e 1142State 21
db34f798
JD
1143
1144 4 A: 'a' 'a' B . [$end]
1145
1146 $end reduce using rule 4 (A)
1147
1148
d42fe46e 1149State 22]])[
db34f798
JD
1150
1151 4 A: 'a' 'a' . B
1152 5 B: . 'a'
21cf8039 1153 6 | . %empty ['b']
db34f798
JD
1154
1155 'a' shift, and go to state ]AT_COND_CASE([[canonical LR]], [[23]],
1156 [[16]])[
1157
1158 ]AT_COND_CASE([[canonical LR]], [['b']],
1159 [[$default]])[ reduce using rule 6 (B)
1160
1161 B go to state ]AT_COND_CASE([[canonical LR]], [[24
1162
1163
d42fe46e 1164State 23
db34f798
JD
1165
1166 5 B: 'a' . ['b']
1167
1168 'b' reduce using rule 5 (B)
1169
1170
d42fe46e 1171State 24
db34f798
JD
1172
1173 4 A: 'a' 'a' B . ['b']
1174
1175 'b' reduce using rule 4 (A)]], [[17]])])[
1176]],
1177
1178dnl OTHER-CHECKS
1179[],
1180
1181dnl PARSER-EXIT-VALUE, PARSER-STDOUT, PARSER-STDERR
1182[AT_COND_CASE([[LALR]], [[1]], [[0]])],
1183[],
1184[AT_COND_CASE([[LALR]],
1185[[syntax error
1186]])])
1187
1188AT_TEST_LR_TYPE([[Split During Added Lookahead Propagation]],
f3bc3386 1189[[%define lr.keep-unreachable-state]],
db34f798
JD
1190[[
1191/* The partial state chart diagram below is for LALR(1). State 0 is the start
1192 state. States are iterated for successor construction in numerical order.
1193 Transitions are downwards.
1194
1195 State 13 has a R/R conflict that cannot be predicted by Bison's LR(1)
1196 algorithm using annotations alone. That is, when state 11's successor on
1197 'd' is merged with state 5 (which is originally just state 1's successor on
1198 'd'), state 5's successor on 'e' must then be changed because the resulting
1199 lookaheads that propagate to it now make it incompatible with state 8's
1200 successor on 'e'. In other words, state 13 must be split to avoid the
1201 conflict.
1202
1203 0
1204 / | \
1205 a / c| \ b
1206 1 3 2
1207 | | |
1208 d| |c | d
1209 | 11 |
1210 | | |
1211 \ /d |
1212 5 8
1213 \ |
1214 e \ / e
1215 13
1216 R/R
1217
1218 This grammar is designed carefully to make sure that, despite Bison's LR(1)
1219 algorithm's bread-first iteration of transitions to reconstruct states,
1220 state 11's successors are constructed after state 5's and state 8's.
1221 Otherwise (for example, if you remove the first 'c' in each of rules 6 and
1222 7), state 5's successor on 'e' would never be merged with state 8's, so the
1223 split of the resulting state 13 would never need to be performed. */
1224S: 'a' A 'f'
1225 | 'a' B
1226 | 'b' A 'f'
1227 | 'b' B 'g'
1228 | 'b' 'd'
1229 | 'c' 'c' A 'g'
1230 | 'c' 'c' B
1231 ;
1232A: 'd' 'e' ;
1233B: 'd' 'e' ;
1234]],
1235
1236dnl INPUT
1237[['b', 'd', 'e', 'g']],
1238
1239dnl BISON-STDERR
1240[AT_COND_CASE([[LALR]],
d87ea54c 1241[[input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
db34f798
JD
1242]], [])],
1243
1244dnl TABLES
d42fe46e 1245[[State 0
db34f798
JD
1246
1247 0 $accept: . S $end
1248 1 S: . 'a' A 'f'
1249 2 | . 'a' B
1250 3 | . 'b' A 'f'
1251 4 | . 'b' B 'g'
1252 5 | . 'b' 'd'
1253 6 | . 'c' 'c' A 'g'
1254 7 | . 'c' 'c' B
1255
1256 'a' shift, and go to state 1
1257 'b' shift, and go to state 2
1258 'c' shift, and go to state 3
1259
1260 S go to state 4
1261
1262
d42fe46e 1263State 1
db34f798
JD
1264
1265 1 S: 'a' . A 'f'
1266 2 | 'a' . B
1267 8 A: . 'd' 'e'
1268 9 B: . 'd' 'e'
1269
1270 'd' shift, and go to state 5
1271
1272 A go to state 6
1273 B go to state 7
1274
1275
d42fe46e 1276State 2
db34f798
JD
1277
1278 3 S: 'b' . A 'f'
1279 4 | 'b' . B 'g'
1280 5 | 'b' . 'd'
1281 8 A: . 'd' 'e'
1282 9 B: . 'd' 'e'
1283
1284 'd' shift, and go to state 8
1285
1286 A go to state 9
1287 B go to state 10
1288
1289
d42fe46e 1290State 3
db34f798
JD
1291
1292 6 S: 'c' . 'c' A 'g'
1293 7 | 'c' . 'c' B
1294
1295 'c' shift, and go to state 11
1296
1297
d42fe46e 1298State 4
db34f798
JD
1299
1300 0 $accept: S . $end
1301
1302 $end shift, and go to state 12
1303
1304
d42fe46e 1305State 5
db34f798
JD
1306
1307 8 A: 'd' . 'e'
1308 9 B: 'd' . 'e'
1309
1310 'e' shift, and go to state ]AT_COND_CASE([[LALR]], [[13]],
1311 [[canonical LR]], [[13]],
1312 [[20]])[
1313
1314
d42fe46e 1315State 6
db34f798
JD
1316
1317 1 S: 'a' A . 'f'
1318
1319 'f' shift, and go to state 14
1320
1321
d42fe46e 1322State 7
db34f798
JD
1323
1324 2 S: 'a' B .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
1325
1326 ]AT_COND_CASE([[canonical LR]], [[$end]],
1327 [[$default]])[ reduce using rule 2 (S)
1328
1329
d42fe46e 1330State 8
db34f798
JD
1331
1332 5 S: 'b' 'd' . [$end]
1333 8 A: 'd' . 'e'
1334 9 B: 'd' . 'e'
1335
1336 'e' shift, and go to state ]AT_COND_CASE([[canonical LR]], [[20]],
1337 [[13]])[
1338
1339 ]AT_COND_CASE([[canonical LR]], [[$end]],
1340 [[$default]])[ reduce using rule 5 (S)
1341
1342
d42fe46e 1343State 9
db34f798
JD
1344
1345 3 S: 'b' A . 'f'
1346
1347 'f' shift, and go to state 15
1348
1349
d42fe46e 1350State 10
db34f798
JD
1351
1352 4 S: 'b' B . 'g'
1353
1354 'g' shift, and go to state 16
1355
1356
d42fe46e 1357State 11
db34f798
JD
1358
1359 6 S: 'c' 'c' . A 'g'
1360 7 | 'c' 'c' . B
1361 8 A: . 'd' 'e'
1362 9 B: . 'd' 'e'
1363
1364 'd' shift, and go to state ]AT_COND_CASE([[canonical LR]], [[21]],
1365 [[5]])[
1366
1367 A go to state 17
1368 B go to state 18
1369
1370
d42fe46e 1371State 12
db34f798
JD
1372
1373 0 $accept: S $end .
1374
1375 $default accept]AT_COND_CASE([[LALR]], [[
1376
1377
d42fe46e 1378State 13
db34f798
JD
1379
1380 8 A: 'd' 'e' . ['f', 'g']
1381 9 B: 'd' 'e' . [$end, 'g']
1382
1383 $end reduce using rule 9 (B)
1384 'g' reduce using rule 8 (A)
1385 'g' [reduce using rule 9 (B)]
1386 $default reduce using rule 8 (A)]], [[
1387
1388
d42fe46e 1389State 13
db34f798
JD
1390
1391 8 A: 'd' 'e' . ['f']
1392 9 B: 'd' 'e' . ]AT_COND_CASE([[canonical LR]], [[[$end]]], [[['g']]])[
1393
1394 ]AT_COND_CASE([[canonical LR]], [[$end]],
1395 [['g' ]])[ reduce using rule 9 (B)
1396 ]AT_COND_CASE([[canonical LR]], [['f' ]],
1397 [[$default]])[ reduce using rule 8 (A)]])[
1398
1399
d42fe46e 1400State 14
db34f798
JD
1401
1402 1 S: 'a' A 'f' .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
1403
1404 ]AT_COND_CASE([[canonical LR]], [[$end]],
1405 [[$default]])[ reduce using rule 1 (S)
1406
1407
d42fe46e 1408State 15
db34f798
JD
1409
1410 3 S: 'b' A 'f' .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
1411
1412 ]AT_COND_CASE([[canonical LR]], [[$end]],
1413 [[$default]])[ reduce using rule 3 (S)
1414
1415
d42fe46e 1416State 16
db34f798
JD
1417
1418 4 S: 'b' B 'g' .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
1419
1420 ]AT_COND_CASE([[canonical LR]], [[$end]],
1421 [[$default]])[ reduce using rule 4 (S)
1422
1423
d42fe46e 1424State 17
db34f798
JD
1425
1426 6 S: 'c' 'c' A . 'g'
1427
1428 'g' shift, and go to state 19
1429
1430
d42fe46e 1431State 18
db34f798
JD
1432
1433 7 S: 'c' 'c' B .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
1434
1435 ]AT_COND_CASE([[canonical LR]], [[$end]],
1436 [[$default]])[ reduce using rule 7 (S)
1437
1438
d42fe46e 1439State 19
db34f798
JD
1440
1441 6 S: 'c' 'c' A 'g' .]AT_COND_CASE([[canonical LR]], [[ [$end]]])[
1442
1443 ]AT_COND_CASE([[canonical LR]], [[$end]],
1444 [[$default]])[ reduce using rule 6 (S)]AT_COND_CASE([[LALR]],
1445 [[]], [[
1446
1447
d42fe46e 1448State 20]AT_COND_CASE([[canonical LR]], [[
db34f798
JD
1449
1450 8 A: 'd' 'e' . ['f']
1451 9 B: 'd' 'e' . ['g']
1452
1453 'f' reduce using rule 8 (A)
1454 'g' reduce using rule 9 (B)
1455
1456
d42fe46e 1457State 21
db34f798
JD
1458
1459 8 A: 'd' . 'e'
1460 9 B: 'd' . 'e'
1461
1462 'e' shift, and go to state 22
1463
1464
d42fe46e 1465State 22
db34f798
JD
1466
1467 8 A: 'd' 'e' . ['g']
1468 9 B: 'd' 'e' . [$end]
1469
1470 $end reduce using rule 9 (B)
1471 'g' reduce using rule 8 (A)]], [[
1472
1473 8 A: 'd' 'e' . ['f', 'g']
1474 9 B: 'd' 'e' . [$end]
1475
1476 $end reduce using rule 9 (B)
1477 $default reduce using rule 8 (A)]])])[
1478]],
1479
1480dnl OTHER-CHECKS
1481[],
1482
1483dnl PARSER-EXIT-VALUE, PARSER-STDOUT, PARSER-STDERR
1484[AT_COND_CASE([[LALR]], [[1]], [[0]])],
1485[],
1486[AT_COND_CASE([[LALR]],
1487[[syntax error
1488]])])
1489
1490
1491
110ef36a 1492## ------------------------------- ##
f3bc3386 1493## %define lr.default-reduction. ##
110ef36a 1494## ------------------------------- ##
7254f6a8 1495
110ef36a
JD
1496# AT_TEST_LR_DEFAULT_REDUCTIONS(GRAMMAR, INPUT, TABLES)
1497# -----------------------------------------------------
1498m4_define([AT_TEST_LR_DEFAULT_REDUCTIONS],
7254f6a8 1499[
f3bc3386 1500AT_TEST_TABLES_AND_PARSE([[no %define lr.default-reduction]],
f0ad1b2f 1501 [[most]], [[]],
7254f6a8
JD
1502 [[]],
1503 [$1], [$2], [[]], [$3])
f3bc3386 1504AT_TEST_TABLES_AND_PARSE([[%define lr.default-reduction most]],
f0ad1b2f 1505 [[most]], [[]],
f3bc3386 1506 [[%define lr.default-reduction most]],
7254f6a8 1507 [$1], [$2], [[]], [$3])
f3bc3386 1508AT_TEST_TABLES_AND_PARSE([[%define lr.default-reduction consistent]],
7254f6a8 1509 [[consistent]], [[]],
f3bc3386 1510 [[%define lr.default-reduction consistent]],
7254f6a8 1511 [$1], [$2], [[]], [$3])
f3bc3386 1512AT_TEST_TABLES_AND_PARSE([[%define lr.default-reduction accepting]],
7254f6a8 1513 [[accepting]], [[]],
f3bc3386 1514 [[%define lr.default-reduction accepting]],
7254f6a8
JD
1515 [$1], [$2], [[]], [$3])
1516])
1517
110ef36a 1518AT_TEST_LR_DEFAULT_REDUCTIONS([[
7254f6a8
JD
1519/* The start state is consistent and has a shift on 'a' and no reductions.
1520 After pushing the b below, enter an inconsistent state that has a shift and
1521 one reduction with one lookahead. */
1522start:
1523 a b
1524 | a b 'a'
1525 | a c 'b'
1526 ;
1527
1528/* After shifting this 'a', enter a consistent state that has no shift and 1
1529 reduction with multiple lookaheads. */
1530a: 'a' ;
1531
1532/* After the previous reduction, enter an inconsistent state that has no shift
1533 and multiple reductions. The first reduction has more lookaheads than the
110ef36a 1534 second, so the first should always be preferred as the default reduction if
7254f6a8 1535 enabled. The second reduction has one lookahead. */
684083f0
AD
1536b: %empty;
1537c: %empty;
7254f6a8
JD
1538]],
1539dnl Visit each state mentioned above.
1540[['a', 'a']],
d42fe46e 1541[[State 0
7254f6a8
JD
1542
1543 0 $accept: . start $end
1544 1 start: . a b
1545 2 | . a b 'a'
1546 3 | . a c 'b'
1547 4 a: . 'a'
1548
1549 'a' shift, and go to state 1
1550
1551 start go to state 2
1552 a go to state 3
1553
1554
d42fe46e 1555State 1
7254f6a8
JD
1556
1557 4 a: 'a' .]AT_COND_CASE([[accepting]], [[ [$end, 'a', 'b']
1558
1559 $end reduce using rule 4 (a)
1560 'a' reduce using rule 4 (a)
1561 'b' reduce using rule 4 (a)]], [[
1562
1563 $default reduce using rule 4 (a)]])[
1564
1565
d42fe46e 1566State 2
7254f6a8
JD
1567
1568 0 $accept: start . $end
1569
1570 $end shift, and go to state 4
1571
1572
d42fe46e 1573State 3
7254f6a8
JD
1574
1575 1 start: a . b
1576 2 | a . b 'a'
1577 3 | a . c 'b'
21cf8039
AD
1578 5 b: . %empty [$end, 'a']
1579 6 c: . %empty ['b']]AT_COND_CASE([[most]], [[
7254f6a8
JD
1580
1581 'b' reduce using rule 6 (c)
1582 $default reduce using rule 5 (b)]], [[
1583
1584 $end reduce using rule 5 (b)
1585 'a' reduce using rule 5 (b)
1586 'b' reduce using rule 6 (c)]])[
1587
1588 b go to state 5
1589 c go to state 6
1590
1591
d42fe46e 1592State 4
7254f6a8
JD
1593
1594 0 $accept: start $end .
1595
1596 $default accept
1597
1598
d42fe46e 1599State 5
7254f6a8
JD
1600
1601 1 start: a b . [$end]
1602 2 | a b . 'a'
1603
1604 'a' shift, and go to state 7
1605
f0ad1b2f 1606 ]AT_COND_CASE([[most]], [[$default]],
32493bc8 1607 [[$end]])[ reduce using rule 1 (start)
7254f6a8
JD
1608
1609
d42fe46e 1610State 6
7254f6a8
JD
1611
1612 3 start: a c . 'b'
1613
1614 'b' shift, and go to state 8
1615
1616
d42fe46e 1617State 7
7254f6a8
JD
1618
1619 2 start: a b 'a' .]AT_COND_CASE([[accepting]], [[ [$end]
1620
1621 $end reduce using rule 2 (start)]], [[
1622
1623 $default reduce using rule 2 (start)]])[
1624
1625
d42fe46e 1626State 8
7254f6a8
JD
1627
1628 3 start: a c 'b' .]AT_COND_CASE([[accepting]], [[ [$end]
1629
1630 $end reduce using rule 3 (start)]], [[
1631
1632 $default reduce using rule 3 (start)]])[
1633]])