]> git.saurik.com Git - bison.git/blob - tests/input.at
Warn about unset midrule $$ if the corresponding $n is used.
[bison.git] / tests / input.at
1 # Checking the Bison scanner. -*- Autotest -*-
2 # Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
7 # any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 # 02110-1301, USA.
18
19 AT_BANNER([[Input Processing.]])
20
21 # Mostly test that we are robust to mistakes.
22
23
24 ## ------------ ##
25 ## Invalid $n. ##
26 ## ------------ ##
27
28 AT_SETUP([Invalid \$n and @n])
29
30 AT_DATA([input.y],
31 [[%%
32 exp: { $$ = $1 ; };
33 exp: { @$ = @1 ; };
34 ]])
35
36 AT_CHECK([bison input.y], [1], [],
37 [[input.y:2.13-14: integer out of range: `$1'
38 input.y:3.13-14: integer out of range: `@1'
39 ]])
40
41 AT_CLEANUP
42
43
44 ## -------------- ##
45 ## Type Clashes. ##
46 ## -------------- ##
47
48 AT_SETUP([Type Clashes])
49
50 AT_DATA([input.y],
51 [[%union { int bar; }
52 %token foo
53 %type <bar> exp
54 %%
55 exp: foo { $$; } foo { $2; } foo
56 | foo
57 | /* Empty. */
58 ;
59 ]])
60
61 AT_CHECK([bison input.y], [1], [],
62 [[input.y:5.12-13: $$ for the midrule at $2 of `exp' has no declared type
63 input.y:5.24-25: $2 of `exp' has no declared type
64 input.y:5.6-32: warning: type clash on default action: <bar> != <>
65 input.y:6.6-8: warning: type clash on default action: <bar> != <>
66 input.y:7.5: warning: empty rule for typed nonterminal, and no action
67 ]])
68
69 AT_CLEANUP
70
71
72 # _AT_UNUSED_VALUES_DECLARATIONS()
73 # --------------------------------------------
74 # Generate the token, type, and destructor
75 # declarations for the unused values tests.
76
77 m4_define([_AT_UNUSED_VALUES_DECLARATIONS],
78 [[[%token <integer> INT;
79 %type <integer> a b c d e f g h i j k l;
80 %destructor { destroy ($$); } INT a b c d e f g h i j k l;]]])
81
82
83 # AT_CHECK_UNUSED_VALUES(DECLARATIONS_AFTER)
84 # --------------------------------------------
85 # Generate a grammar to test unused values,
86 # compile it, run it. If DECLARATIONS_AFTER
87 # is set, then the token, type, and destructor
88 # declarations are generated after the rules
89 # rather than before.
90
91 m4_define([AT_CHECK_UNUSED_VALUES],
92 [AT_DATA([input.y],
93 m4_ifval($1, [
94
95
96 ], [_AT_UNUSED_VALUES_DECLARATIONS
97 ])[[%%
98 start:
99 'a' a { $]2[ } | 'b' b { $]2[ } | 'c' c { $]2[ } | 'd' d { $]2[ } | 'e' e { $]2[ }
100 | 'f' f { $]2[ } | 'g' g { $]2[ } | 'h' h { $]2[ } | 'i' i { $]2[ } | 'j' j { $]2[ }
101 | 'k' k { $]2[ } | 'l' l { $]2[ }
102 ;
103
104 a: INT | INT { } INT { } INT { };
105 b: INT | /* empty */;
106 c: INT | INT { $]1[ } INT { $<integer>2 } INT { $<integer>4 };
107 d: INT | INT { } INT { $]1[ } INT { $<integer>2 };
108 e: INT | INT { } INT { } INT { $]1[ };
109 f: INT | INT { } INT { } INT { $]$[ = $]1[ + $]3[ + $]5[; };
110 g: INT | INT { $<integer>$; } INT { $<integer>$; } INT { };
111 h: INT | INT { $<integer>$; } INT { $<integer>$ = $<integer>2; } INT { };
112 i: INT | INT INT { } { $]$[ = $]1[ + $]2[; };
113 j: INT | INT INT { $<integer>$ = 1; } { $]$[ = $]1[ + $]2[; };
114 k: INT | INT INT { $<integer>$; } { $<integer>$ = $<integer>3; } { };
115 l: INT | INT { $<integer>$ = $<integer>1; } INT { $<integer>$ = $<integer>2 + $<integer>3; } INT { $<integer>$ = $<integer>4 + $<integer>5; };]]m4_ifval($1, [
116 _AT_UNUSED_VALUES_DECLARATIONS])
117 )
118
119 AT_CHECK([bison input.y], [0], [],
120 [[input.y:11.10-32: warning: unset value: $]$[
121 input.y:11.10-32: warning: unused value: $]1[
122 input.y:11.10-32: warning: unused value: $]3[
123 input.y:11.10-32: warning: unused value: $]5[
124 input.y:12.9: warning: empty rule for typed nonterminal, and no action
125 input.y:13.14-19: warning: unset value: $$
126 input.y:13.25-39: warning: unset value: $$
127 input.y:13.10-59: warning: unset value: $]$[
128 input.y:13.10-59: warning: unused value: $]3[
129 input.y:13.10-59: warning: unused value: $]5[
130 input.y:14.14-16: warning: unset value: $$
131 input.y:14.10-47: warning: unset value: $]$[
132 input.y:14.10-47: warning: unused value: $]3[
133 input.y:14.10-47: warning: unused value: $]5[
134 input.y:15.10-36: warning: unset value: $]$[
135 input.y:15.10-36: warning: unused value: $]3[
136 input.y:15.10-36: warning: unused value: $]5[
137 input.y:17.10-58: warning: unset value: $]$[
138 input.y:17.10-58: warning: unused value: $]1[
139 input.y:17.10-58: warning: unused value: $]2[
140 input.y:17.10-58: warning: unused value: $]3[
141 input.y:17.10-58: warning: unused value: $]4[
142 input.y:17.10-58: warning: unused value: $]5[
143 input.y:18.10-72: warning: unset value: $]$[
144 input.y:18.10-72: warning: unused value: $]1[
145 input.y:18.10-72: warning: unused value: $]3[
146 input.y:18.10-72: warning: unused value: $]4[
147 input.y:18.10-72: warning: unused value: $]5[
148 input.y:20.10-55: warning: unused value: $]3[
149 input.y:21.10-68: warning: unset value: $]$[
150 input.y:21.10-68: warning: unused value: $]1[
151 input.y:21.10-68: warning: unused value: $]2[
152 input.y:21.10-68: warning: unused value: $]4[
153 ]])])
154
155
156 ## --------------- ##
157 ## Unused values. ##
158 ## --------------- ##
159
160 AT_SETUP([Unused values])
161 AT_CHECK_UNUSED_VALUES
162 AT_CLEANUP
163
164
165 ## ------------------------------------------ ##
166 ## Unused values before symbol declarations. ##
167 ## ------------------------------------------ ##
168
169 AT_SETUP([Unused values before symbol declarations])
170 AT_CHECK_UNUSED_VALUES([1])
171 AT_CLEANUP
172
173
174 ## --------------------------------------------- ##
175 ## Default %printer and %destructor redeclared. ##
176 ## --------------------------------------------- ##
177
178 AT_SETUP([Default %printer and %destructor redeclared])
179
180 AT_DATA([[input.y]],
181 [[%destructor { destroy ($$); } %symbol-default %symbol-default
182 %printer { destroy ($$); } %symbol-default %symbol-default
183
184 %destructor { destroy ($$); } %symbol-default
185 %printer { destroy ($$); } %symbol-default
186
187 %%
188
189 start: ;
190
191 %destructor { destroy ($$); } %symbol-default;
192 %printer { destroy ($$); } %symbol-default;
193 ]])
194
195 AT_CHECK([bison input.y], [1], [],
196 [[input.y:1.13-29: redeclaration for default %destructor
197 input.y:1.13-29: previous declaration
198 input.y:2.10-26: redeclaration for default %printer
199 input.y:2.10-26: previous declaration
200 input.y:4.13-29: redeclaration for default %destructor
201 input.y:1.13-29: previous declaration
202 input.y:5.10-26: redeclaration for default %printer
203 input.y:2.10-26: previous declaration
204 input.y:11.13-29: redeclaration for default %destructor
205 input.y:4.13-29: previous declaration
206 input.y:12.10-26: redeclaration for default %printer
207 input.y:5.10-26: previous declaration
208 ]])
209
210 AT_CLEANUP
211
212
213 ## ---------------------------------------------- ##
214 ## Per-type %printer and %destructor redeclared. ##
215 ## ---------------------------------------------- ##
216
217 AT_SETUP([Per-type %printer and %destructor redeclared])
218
219 AT_DATA([[input.y]],
220 [[%destructor { destroy ($$); } <field1> <field2>
221 %printer { destroy ($$); } <field1> <field2>
222
223 %destructor { destroy ($$); } <field1> <field1>
224 %printer { destroy ($$); } <field2> <field2>
225
226 %%
227
228 start: ;
229
230 %destructor { destroy ($$); } <field2> <field1>;
231 %printer { destroy ($$); } <field2> <field1>;
232 ]])
233
234 AT_CHECK([bison input.y], [1], [],
235 [[input.y:4.13-29: %destructor redeclaration for <field1>
236 input.y:1.13-29: previous declaration
237 input.y:4.13-29: %destructor redeclaration for <field1>
238 input.y:4.13-29: previous declaration
239 input.y:5.10-26: %printer redeclaration for <field2>
240 input.y:2.10-26: previous declaration
241 input.y:5.10-26: %printer redeclaration for <field2>
242 input.y:5.10-26: previous declaration
243 input.y:11.13-29: %destructor redeclaration for <field1>
244 input.y:4.13-29: previous declaration
245 input.y:11.13-29: %destructor redeclaration for <field2>
246 input.y:1.13-29: previous declaration
247 input.y:12.10-26: %printer redeclaration for <field1>
248 input.y:2.10-26: previous declaration
249 input.y:12.10-26: %printer redeclaration for <field2>
250 input.y:5.10-26: previous declaration
251 ]])
252
253 AT_CLEANUP
254
255
256 ## ---------------------------------------- ##
257 ## Unused values with default %destructor. ##
258 ## ---------------------------------------- ##
259
260 AT_SETUP([Unused values with default %destructor])
261
262 AT_DATA([[input.y]],
263 [[%destructor { destroy ($$); } %symbol-default
264
265 %%
266
267 start: end end { $1; } ;
268 end: { } ;
269 ]])
270
271 AT_CHECK([bison input.y], [0], [],
272 [[input.y:5.8-22: warning: unset value: $$
273 input.y:5.8-22: warning: unused value: $2
274 input.y:6.6-8: warning: unset value: $$
275 ]])
276
277 AT_CLEANUP
278
279
280 ## ----------------------------------------- ##
281 ## Unused values with per-type %destructor. ##
282 ## ----------------------------------------- ##
283
284 AT_SETUP([Unused values with per-type %destructor])
285
286 AT_DATA([[input.y]],
287 [[%destructor { destroy ($$); } <field1>
288 %type <field1> start end
289
290 %%
291
292 start: end end { $1; } ;
293 end: { } ;
294 ]])
295
296 AT_CHECK([bison input.y], [0], [],
297 [[input.y:6.8-22: warning: unset value: $$
298 input.y:6.8-22: warning: unused value: $2
299 input.y:7.6-8: warning: unset value: $$
300 ]])
301
302 AT_CLEANUP
303
304
305 ## ---------------------- ##
306 ## Incompatible Aliases. ##
307 ## ---------------------- ##
308
309 AT_SETUP([Incompatible Aliases])
310
311 AT_DATA([input.y],
312 [[%token foo "foo"
313
314 %type <bar> foo
315 %printer {bar} foo
316 %destructor {bar} foo
317 %left foo
318
319 %type <baz> "foo"
320 %printer {baz} "foo"
321 %destructor {baz} "foo"
322 %left "foo"
323
324 %%
325 exp: foo;
326 ]])
327
328 AT_CHECK([bison input.y], [1], [],
329 [[input.y:8.7-11: %type redeclaration for foo
330 input.y:3.7-11: previous declaration
331 input.y:10.13-17: %destructor redeclaration for foo
332 input.y:5.13-17: previous declaration
333 input.y:9.10-14: %printer redeclaration for foo
334 input.y:4.10-14: previous declaration
335 input.y:11.1-5: %left redeclaration for foo
336 input.y:6.1-5: previous declaration
337 ]])
338
339 AT_CLEANUP
340
341
342
343 ## ----------------------- ##
344 ## Torturing the Scanner. ##
345 ## ----------------------- ##
346
347 # Be sure to compile and run, so that the C compiler checks what
348 # we do.
349
350 AT_SETUP([Torturing the Scanner])
351
352
353 AT_DATA([input.y], [])
354 AT_CHECK([bison input.y], [1], [],
355 [[input.y:1.1: syntax error, unexpected end of file
356 ]])
357
358
359 AT_DATA([input.y],
360 [{}
361 ])
362 AT_CHECK([bison input.y], [1], [],
363 [[input.y:1.1-2: syntax error, unexpected {...}
364 ]])
365
366
367 AT_DATA_GRAMMAR([input.y],
368 [[%{
369 /* This is seen in GCC: a %{ and %} in middle of a comment. */
370 const char *foo = "So %{ and %} can be here too.";
371
372 #if 0
373 /* These examples test Bison while not stressing C compilers too much.
374 Many C compilers mishandle backslash-newlines, so this part of the
375 test is inside "#if 0". The comment and string are written so that
376 the "#endif" will be seen regardless of the C compiler bugs that we
377 know about, namely:
378
379 HP C (as of late 2002) mishandles *\[newline]\[newline]/ within a
380 comment.
381
382 The Apple Darwin compiler (as of late 2002) mishandles
383 \\[newline]' within a character constant.
384
385 */
386
387 /\
388 * A comment with backslash-newlines in it. %} *\
389 \
390 /
391 /* { Close the above comment, if the C compiler mishandled it. */
392
393 char str[] = "\\
394 " A string with backslash-newlines in it %{ %} \\
395 \
396 "";
397
398 char apostrophe = '\'';
399 #endif
400
401 #include <stdio.h>
402 #include <stdlib.h>
403 %}
404 /* %{ and %} can be here too. */
405
406 %{
407 /* Exercise pre-prologue dependency to %union. */
408 typedef int value;
409 %}
410
411 /* Exercise M4 quoting: '@:>@@:>@', 0. */
412
413 /* Also exercise %union. */
414 %union
415 {
416 value ival; /* A comment to exercise an old bug. */
417 };
418
419
420 /* Exercise post-prologue dependency to %union. */
421 %{
422 static YYSTYPE value_as_yystype (value val);
423
424 /* Exercise quotes in declarations. */
425 char quote[] = "@:>@@:>@,";
426 %}
427
428 %{
429 static void yyerror (const char *s);
430 static int yylex (void);
431 %}
432
433 %type <ival> '@<:@'
434
435 /* Exercise quotes in strings. */
436 %token FAKE "fake @<:@@:>@ \a\b\f\n\r\t\v\"\'\?\\\u005B\U0000005c ??!??'??(??)??-??/??<??=??> \x1\1"
437
438 %%
439 /* Exercise M4 quoting: '@:>@@:>@', @<:@, 1. */
440 exp: '@<:@' '\1' two '$' '@' '{' oline output.or.oline.opt
441 {
442 /* Exercise quotes in braces. */
443 char tmp[] = "@<:@%c@:>@,\n";
444 printf (tmp, $1);
445 }
446 ;
447
448 two: '\x000000000000000000000000000000000000000000000000000000000000000000002';
449 oline: '@' 'o' 'l' 'i' 'n' 'e' '@' '_' '_' 'o' 'l' 'i' 'n' 'e' '_' '_';
450 output.or.oline.opt: ;|oline;;|output;;;
451 output: '#' 'o' 'u' 't' 'p' 'u' 't' ' ';
452 %%
453 /* Exercise M4 quoting: '@:>@@:>@', @<:@, 2. */
454
455 static YYSTYPE
456 value_as_yystype (value val)
457 {
458 YYSTYPE res;
459 res.ival = val;
460 return res;
461 }
462
463 static int
464 yylex (void)
465 {
466 static char const input[] = "@<:@\1\2$@{@oline@__@&t@oline__\
467 #output "; /* "
468 */
469 static size_t toknum;
470 if (! (toknum < sizeof input))
471 abort ();
472 yylval = value_as_yystype (input[toknum]);
473 return input[toknum++];
474 }
475
476 static void
477 yyerror (const char *msg)
478 {
479 fprintf (stderr, "%s\n", msg);
480 }
481 ]])
482
483 # Pacify Emacs'font-lock-mode: "
484
485 AT_DATA([main.c],
486 [[typedef int value;
487 #include "input.h"
488
489 int yyparse (void);
490
491 int
492 main (void)
493 {
494 return yyparse ();
495 }
496 ]])
497
498 AT_CHECK([bison -d -v -o input.c input.y])
499 AT_COMPILE([input.o], [-c input.c])
500 AT_COMPILE([main.o], [-c main.c])
501 AT_COMPILE([input], [input.o main.o])
502 AT_PARSER_CHECK([./input], 0,
503 [[[@<:@],
504 ]])
505
506 AT_CLEANUP
507
508
509 ## ---------------------- ##
510 ## Typed symbol aliases. ##
511 ## ---------------------- ##
512
513 AT_SETUP([Typed symbol aliases])
514
515 # Bison 2.0 broke typed symbol aliases - ensure they work.
516
517 AT_DATA_GRAMMAR([input.y],
518 [[%union
519 {
520 int val;
521 };
522 %token <val> MY_TOKEN "MY TOKEN"
523 %type <val> exp
524 %%
525 exp: "MY TOKEN";
526 %%
527 ]])
528
529 AT_CHECK([bison -o input.c input.y])
530
531 AT_CLEANUP
532
533
534 ## --------- ##
535 ## Require. ##
536 ## --------- ##
537
538 m4_define([AT_CHECK_REQUIRE],
539 [AT_SETUP([Require $1])
540 AT_DATA_GRAMMAR([input.y],
541 [[%require "$1";
542 %%
543 empty_file:;
544 ]])
545 AT_CHECK([bison -o input.c input.y], $2, [], ignore)
546 AT_CLEANUP
547 ])
548
549 AT_CHECK_REQUIRE(1.0, 0)
550 AT_CHECK_REQUIRE(AT_PACKAGE_VERSION, 0)
551 ## FIXME: Some day augment this version number.
552 AT_CHECK_REQUIRE(100.0, 63)
553
554
555 ## ------------------------------------- ##
556 ## String aliases for character tokens. ##
557 ## ------------------------------------- ##
558
559 AT_SETUP([String aliases for character tokens])
560
561 # Bison once thought a character token and its alias were different symbols
562 # with the same user token number.
563
564 AT_DATA_GRAMMAR([input.y],
565 [[%token 'a' "a"
566 %%
567 start: 'a';
568 %%
569 ]])
570
571 AT_CHECK([bison -o input.c input.y])
572
573 AT_CLEANUP
574
575
576 ## --------------------- ##
577 ## Unclosed constructs. ##
578 ## --------------------- ##
579
580 AT_SETUP([Unclosed constructs])
581
582 # Bison's scan-gram.l once forgot to STRING_FINISH some unclosed constructs, so
583 # they were prepended to whatever it STRING_GROW'ed next. It also threw them
584 # away rather than returning them to the parser. The effect was confusing
585 # subsequent error messages.
586
587 AT_DATA([input.y],
588 [[%token A "a
589 %token B "b"
590 %token AB "ab" // Used to complain that "ab" was already used.
591 %token C '1
592 %token TWO "2"
593 %token TICK_TWELVE "'12" // Used to complain that "'12" was already used.
594
595 %%
596
597 start: ;
598
599 // Used to report a syntax error because it didn't see any kind of symbol
600 // identifier.
601 %type <f> 'a
602 ;
603 %type <f> "a
604 ;
605 // Used to report a syntax error because it didn't see braced code.
606 %destructor { free ($$)
607 ]])
608
609 AT_CHECK([bison -o input.c input.y], 1, [],
610 [[input.y:1.10-2.0: missing `"' at end of line
611 input.y:4.10-5.0: missing `'' at end of line
612 input.y:14.11-15.0: missing `'' at end of line
613 input.y:16.11-17.0: missing `"' at end of line
614 input.y:19.13-20.0: missing `}' at end of file
615 input.y:20.1: syntax error, unexpected end of file
616 ]])
617
618 AT_CLEANUP
619
620
621 ## ------------------------- ##
622 ## %start after first rule. ##
623 ## ------------------------- ##
624
625 AT_SETUP([%start after first rule])
626
627 # Bison once complained that a %start after the first rule was a redeclaration
628 # of the start symbol.
629
630 AT_DATA([input.y],
631 [[%%
632 false_start: ;
633 start: false_start ;
634 %start start;
635 ]])
636
637 AT_CHECK([bison -o input.c input.y])
638
639 AT_CLEANUP