]> git.saurik.com Git - bison.git/blame_incremental - tests/conflicts.at
Don't assume the C++ compiler takes the same arguments as the C compiler.
[bison.git] / tests / conflicts.at
... / ...
CommitLineData
1# Exercising Bison on conflicts. -*- Autotest -*-
2
3# Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2, or (at your option)
8# any later version.
9
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.
14
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18# 02111-1307, USA.
19
20AT_BANNER([[Conflicts.]])
21
22
23## ---------------- ##
24## S/R in initial. ##
25## ---------------- ##
26
27# I once hacked Bison in such a way that it lost its reductions on the
28# initial state (because it was confusing it with the last state). It
29# took me a while to strip down my failures to this simple case. So
30# make sure it finds the s/r conflict below.
31
32AT_SETUP([S/R in initial])
33
34AT_DATA([[input.y]],
35[[%expect 1
36%%
37exp: e 'e';
38e: 'e' | /* Nothing. */;
39]])
40
41AT_CHECK([bison -o input.c input.y], 0, [],
42[[input.y:4.9: warning: rule never reduced because of conflicts: e: /* empty */
43]])
44
45AT_CLEANUP
46
47
48## ------------------- ##
49## %nonassoc and eof. ##
50## ------------------- ##
51
52AT_SETUP([%nonassoc and eof])
53
54AT_DATA_GRAMMAR([input.y],
55[[
56%{
57#include <stdio.h>
58#include <stdlib.h>
59
60#define YYERROR_VERBOSE 1
61static void
62yyerror (const char *msg)
63{
64 fprintf (stderr, "%s\n", msg);
65 exit (1);
66}
67
68/* The current argument. */
69static const char *input = NULL;
70
71static int
72yylex (void)
73{
74 /* No token stands for end of file. */
75 if (input && *input)
76 return *input++;
77 else
78 return 0;
79}
80
81%}
82
83%nonassoc '<' '>'
84
85%%
86expr: expr '<' expr
87 | expr '>' expr
88 | '0'
89 ;
90%%
91int
92main (int argc, const char *argv[])
93{
94 if (argc > 1)
95 input = argv[1];
96 return yyparse ();
97}
98]])
99
100# Specify the output files to avoid problems on different file systems.
101AT_CHECK([bison -o input.c input.y])
102AT_COMPILE([input])
103
104AT_PARSER_CHECK([./input '0<0'])
105# FIXME: This is an actual bug, but a new one, in the sense that
106# no one has ever spotted it! The messages are *wrong*: there should
107# be nothing there, it should be expected eof.
108AT_PARSER_CHECK([./input '0<0<0'], [1], [],
109 [syntax error, unexpected '<', expecting '<' or '>'
110])
111
112AT_PARSER_CHECK([./input '0>0'])
113AT_PARSER_CHECK([./input '0>0>0'], [1], [],
114 [syntax error, unexpected '>', expecting '<' or '>'
115])
116
117AT_PARSER_CHECK([./input '0<0>0'], [1], [],
118 [syntax error, unexpected '>', expecting '<' or '>'
119])
120
121AT_CLEANUP
122
123
124
125## ------------------------- ##
126## Unresolved SR Conflicts. ##
127## ------------------------- ##
128
129AT_SETUP([Unresolved SR Conflicts])
130
131AT_KEYWORDS([report])
132
133AT_DATA([input.y],
134[[%token NUM OP
135%%
136exp: exp OP exp | NUM;
137]])
138
139AT_CHECK([bison -o input.c --report=all input.y], 0, [],
140[input.y: conflicts: 1 shift/reduce
141])
142
143# Check the contents of the report.
144AT_CHECK([cat input.output], [],
145[[State 5 conflicts: 1 shift/reduce
146
147
148Grammar
149
150 0 $accept: exp $end
151
152 1 exp: exp OP exp
153 2 | NUM
154
155
156Terminals, with rules where they appear
157
158$end (0) 0
159error (256)
160NUM (258) 2
161OP (259) 1
162
163
164Nonterminals, with rules where they appear
165
166$accept (5)
167 on left: 0
168exp (6)
169 on left: 1 2, on right: 0 1
170
171
172state 0
173
174 0 $accept: . exp $end
175 1 exp: . exp OP exp
176 2 | . NUM
177
178 NUM shift, and go to state 1
179
180 exp go to state 2
181
182
183state 1
184
185 2 exp: NUM .
186
187 $default reduce using rule 2 (exp)
188
189
190state 2
191
192 0 $accept: exp . $end
193 1 exp: exp . OP exp
194
195 $end shift, and go to state 3
196 OP shift, and go to state 4
197
198
199state 3
200
201 0 $accept: exp $end .
202
203 $default accept
204
205
206state 4
207
208 1 exp: . exp OP exp
209 1 | exp OP . exp
210 2 | . NUM
211
212 NUM shift, and go to state 1
213
214 exp go to state 5
215
216
217state 5
218
219 1 exp: exp . OP exp [$end, OP]
220 1 | exp OP exp . [$end, OP]
221
222 OP shift, and go to state 4
223
224 OP [reduce using rule 1 (exp)]
225 $default reduce using rule 1 (exp)
226]])
227
228AT_CLEANUP
229
230
231
232## ----------------------- ##
233## Resolved SR Conflicts. ##
234## ----------------------- ##
235
236AT_SETUP([Resolved SR Conflicts])
237
238AT_KEYWORDS([report])
239
240AT_DATA([input.y],
241[[%token NUM OP
242%left OP
243%%
244exp: exp OP exp | NUM;
245]])
246
247AT_CHECK([bison -o input.c --report=all input.y])
248
249# Check the contents of the report.
250AT_CHECK([cat input.output], [],
251[[Grammar
252
253 0 $accept: exp $end
254
255 1 exp: exp OP exp
256 2 | NUM
257
258
259Terminals, with rules where they appear
260
261$end (0) 0
262error (256)
263NUM (258) 2
264OP (259) 1
265
266
267Nonterminals, with rules where they appear
268
269$accept (5)
270 on left: 0
271exp (6)
272 on left: 1 2, on right: 0 1
273
274
275state 0
276
277 0 $accept: . exp $end
278 1 exp: . exp OP exp
279 2 | . NUM
280
281 NUM shift, and go to state 1
282
283 exp go to state 2
284
285
286state 1
287
288 2 exp: NUM .
289
290 $default reduce using rule 2 (exp)
291
292
293state 2
294
295 0 $accept: exp . $end
296 1 exp: exp . OP exp
297
298 $end shift, and go to state 3
299 OP shift, and go to state 4
300
301
302state 3
303
304 0 $accept: exp $end .
305
306 $default accept
307
308
309state 4
310
311 1 exp: . exp OP exp
312 1 | exp OP . exp
313 2 | . NUM
314
315 NUM shift, and go to state 1
316
317 exp go to state 5
318
319
320state 5
321
322 1 exp: exp . OP exp [$end, OP]
323 1 | exp OP exp . [$end, OP]
324
325 $default reduce using rule 1 (exp)
326
327 Conflict between rule 1 and token OP resolved as reduce (%left OP).
328]])
329
330AT_CLEANUP
331
332
333## -------------------------------- ##
334## Defaulted Conflicted Reduction. ##
335## -------------------------------- ##
336
337# When there are RR conflicts, some rules are disabled. Usually it is
338# simply displayed as:
339#
340# $end reduce using rule 3 (num)
341# $end [reduce using rule 4 (id)]
342#
343# But when `reduce 3' is the default action, we'd produce:
344#
345# $end [reduce using rule 4 (id)]
346# $default reduce using rule 3 (num)
347#
348# In this precise case (a reduction is masked by the default
349# reduction), we make the `reduce 3' explicit:
350#
351# $end reduce using rule 3 (num)
352# $end [reduce using rule 4 (id)]
353# $default reduce using rule 3 (num)
354#
355# Maybe that's not the best display, but then, please propose something
356# else.
357
358AT_SETUP([Defaulted Conflicted Reduction])
359AT_KEYWORDS([report])
360
361AT_DATA([input.y],
362[[%%
363exp: num | id;
364num: '0';
365id : '0';
366%%
367]])
368
369AT_CHECK([bison -o input.c --report=all input.y], 0, [],
370[[input.y: conflicts: 1 reduce/reduce
371input.y:4.6-8: warning: rule never reduced because of conflicts: id: '0'
372]])
373
374# Check the contents of the report.
375AT_CHECK([cat input.output], [],
376[[Rules never reduced
377
378 4 id: '0'
379
380
381State 1 conflicts: 1 reduce/reduce
382
383
384Grammar
385
386 0 $accept: exp $end
387
388 1 exp: num
389 2 | id
390
391 3 num: '0'
392
393 4 id: '0'
394
395
396Terminals, with rules where they appear
397
398$end (0) 0
399'0' (48) 3 4
400error (256)
401
402
403Nonterminals, with rules where they appear
404
405$accept (4)
406 on left: 0
407exp (5)
408 on left: 1 2, on right: 0
409num (6)
410 on left: 3, on right: 1
411id (7)
412 on left: 4, on right: 2
413
414
415state 0
416
417 0 $accept: . exp $end
418 1 exp: . num
419 2 | . id
420 3 num: . '0'
421 4 id: . '0'
422
423 '0' shift, and go to state 1
424
425 exp go to state 2
426 num go to state 3
427 id go to state 4
428
429
430state 1
431
432 3 num: '0' . [$end]
433 4 id: '0' . [$end]
434
435 $end reduce using rule 3 (num)
436 $end [reduce using rule 4 (id)]
437 $default reduce using rule 3 (num)
438
439
440state 2
441
442 0 $accept: exp . $end
443
444 $end shift, and go to state 5
445
446
447state 3
448
449 1 exp: num .
450
451 $default reduce using rule 1 (exp)
452
453
454state 4
455
456 2 exp: id .
457
458 $default reduce using rule 2 (exp)
459
460
461state 5
462
463 0 $accept: exp $end .
464
465 $default accept
466]])
467
468AT_CLEANUP
469
470
471
472
473## -------------------- ##
474## %expect not enough. ##
475## -------------------- ##
476
477AT_SETUP([%expect not enough])
478
479AT_DATA([input.y],
480[[%token NUM OP
481%expect 0
482%%
483exp: exp OP exp | NUM;
484]])
485
486AT_CHECK([bison -o input.c input.y], 0, [],
487[input.y: conflicts: 1 shift/reduce
488input.y: warning: expected 0 shift/reduce conflicts
489])
490AT_CLEANUP
491
492
493## --------------- ##
494## %expect right. ##
495## --------------- ##
496
497AT_SETUP([%expect right])
498
499AT_DATA([input.y],
500[[%token NUM OP
501%expect 1
502%%
503exp: exp OP exp | NUM;
504]])
505
506AT_CHECK([bison -o input.c input.y])
507AT_CLEANUP
508
509
510## ------------------ ##
511## %expect too much. ##
512## ------------------ ##
513
514AT_SETUP([%expect too much])
515
516AT_DATA([input.y],
517[[%token NUM OP
518%expect 2
519%%
520exp: exp OP exp | NUM;
521]])
522
523AT_CHECK([bison -o input.c input.y], 0, [],
524[input.y: conflicts: 1 shift/reduce
525input.y: warning: expected 2 shift/reduce conflicts
526])
527AT_CLEANUP
528
529
530## ------------------------------ ##
531## %expect with reduce conflicts ##
532## ------------------------------ ##
533
534AT_SETUP([%expect with reduce conflicts])
535
536AT_DATA([input.y],
537[[%expect 0
538%%
539program: a 'a' | a a;
540a: 'a';
541]])
542
543AT_CHECK([bison -o input.c input.y], 0, [],
544[input.y: conflicts: 1 reduce/reduce
545input.y: warning: expected 0 reduce/reduce conflicts
546])
547AT_CLEANUP
548
549
550## ------------------------------- ##
551## %no-default-prec without %prec ##
552## ------------------------------- ##
553
554AT_SETUP([%no-default-prec without %prec])
555
556AT_DATA([[input.y]],
557[[%left '+'
558%left '*'
559
560%%
561
562%no-default-prec;
563
564e: e '+' e
565 | e '*' e
566 | '0'
567 ;
568]])
569
570AT_CHECK([bison -o input.c input.y], 0, [],
571[[input.y: conflicts: 4 shift/reduce
572]])
573AT_CLEANUP
574
575
576## ---------------------------- ##
577## %no-default-prec with %prec ##
578## ---------------------------- ##
579
580AT_SETUP([%no-default-prec with %prec])
581
582AT_DATA([[input.y]],
583[[%left '+'
584%left '*'
585
586%%
587
588%no-default-prec;
589
590e: e '+' e %prec '+'
591 | e '*' e %prec '*'
592 | '0'
593 ;
594]])
595
596AT_CHECK([bison -o input.c input.y])
597AT_CLEANUP
598
599
600## ---------------- ##
601## %default-prec ##
602## ---------------- ##
603
604AT_SETUP([%default-prec])
605
606AT_DATA([[input.y]],
607[[%left '+'
608%left '*'
609
610%%
611
612%default-prec;
613
614e: e '+' e
615 | e '*' e
616 | '0'
617 ;
618]])
619
620AT_CHECK([bison -o input.c input.y])
621AT_CLEANUP