1 # Checking the Bison scanner. -*- Autotest -*-
2 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation,
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)
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
20 AT_BANNER([[Input Processing.]])
22 # Mostly test that we are robust to mistakes.
29 AT_SETUP([Invalid $n and @n])
37 AT_CHECK([bison input.y], [1], [],
38 [[input.y:2.13-14: integer out of range: `$1'
39 input.y:3.13-14: integer out of range: `@1'
49 AT_SETUP([Type Clashes])
56 exp: foo { $$; } foo { $2; } foo
62 AT_CHECK([bison input.y], [1], [],
63 [[input.y:5.12-13: $$ for the midrule at $2 of `exp' has no declared type
64 input.y:5.24-25: $2 of `exp' has no declared type
65 input.y:5.6-32: warning: type clash on default action: <bar> != <>
66 input.y:6.6-8: warning: type clash on default action: <bar> != <>
67 input.y:7.5: warning: empty rule for typed nonterminal, and no action
73 # _AT_UNUSED_VALUES_DECLARATIONS()
74 # --------------------------------------------
75 # Generate the token, type, and destructor
76 # declarations for the unused values tests.
78 m4_define([_AT_UNUSED_VALUES_DECLARATIONS],
79 [[[%token <integer> INT;
80 %type <integer> a b c d e f g h i j k l;
81 %destructor { destroy ($$); } INT a b c d e f g h i j k l;]]])
84 # AT_CHECK_UNUSED_VALUES(DECLARATIONS_AFTER, CHECK_MIDRULE_VALUES)
85 # ------------------------------------------------------------------
86 # Generate a grammar to test unused values,
87 # compile it, run it. If DECLARATIONS_AFTER
88 # is set, then the token, type, and destructor
89 # declarations are generated after the rules
90 # rather than before. If CHECK_MIDRULE_VALUES
91 # is set, then --warnings=midrule-values is
94 m4_define([AT_CHECK_UNUSED_VALUES],
99 ], [_AT_UNUSED_VALUES_DECLARATIONS
102 'a' a { $]2[ } | 'b' b { $]2[ } | 'c' c { $]2[ } | 'd' d { $]2[ } | 'e' e { $]2[ }
103 | 'f' f { $]2[ } | 'g' g { $]2[ } | 'h' h { $]2[ } | 'i' i { $]2[ } | 'j' j { $]2[ }
104 | 'k' k { $]2[ } | 'l' l { $]2[ }
107 a: INT | INT { } INT { } INT { };
108 b: INT | /* empty */;
109 c: INT | INT { $]1[ } INT { $<integer>2 } INT { $<integer>4 };
110 d: INT | INT { } INT { $]1[ } INT { $<integer>2 };
111 e: INT | INT { } INT { } INT { $]1[ };
112 f: INT | INT { } INT { } INT { $]$[ = $]1[ + $]3[ + $]5[; };
113 g: INT | INT { $<integer>$; } INT { $<integer>$; } INT { };
114 h: INT | INT { $<integer>$; } INT { $<integer>$ = $<integer>2; } INT { };
115 i: INT | INT INT { } { $]$[ = $]1[ + $]2[; };
116 j: INT | INT INT { $<integer>$ = 1; } { $]$[ = $]1[ + $]2[; };
117 k: INT | INT INT { $<integer>$; } { $<integer>$ = $<integer>3; } { };
118 l: INT | INT { $<integer>$ = $<integer>1; } INT { $<integer>$ = $<integer>2 + $<integer>3; } INT { $<integer>$ = $<integer>4 + $<integer>5; };]]m4_ifval($1, [
119 _AT_UNUSED_VALUES_DECLARATIONS])
122 AT_CHECK([bison]m4_ifval($2, [ --warnings=midrule-values ])[ input.y], [0], [],
123 [[input.y:11.10-32: warning: unset value: $]$[
124 input.y:11.10-32: warning: unused value: $]1[
125 input.y:11.10-32: warning: unused value: $]3[
126 input.y:11.10-32: warning: unused value: $]5[
127 input.y:12.9: warning: empty rule for typed nonterminal, and no action
128 ]]m4_ifval($2, [[[input.y:13.14-19: warning: unset value: $$
129 input.y:13.25-39: warning: unset value: $$
130 ]]])[[input.y:13.10-59: warning: unset value: $]$[
131 input.y:13.10-59: warning: unused value: $]3[
132 input.y:13.10-59: warning: unused value: $]5[
133 ]]m4_ifval($2, [[[input.y:14.14-16: warning: unset value: $$
134 ]]])[[input.y:14.10-47: warning: unset value: $]$[
135 input.y:14.10-47: warning: unused value: $]3[
136 input.y:14.10-47: warning: unused value: $]5[
137 input.y:15.10-36: warning: unset value: $]$[
138 input.y:15.10-36: warning: unused value: $]3[
139 input.y:15.10-36: warning: unused value: $]5[
140 input.y:17.10-58: warning: unset value: $]$[
141 input.y:17.10-58: warning: unused value: $]1[
142 ]]m4_ifval($2, [[[input.y:17.10-58: warning: unused value: $]2[
143 ]]])[[input.y:17.10-58: warning: unused value: $]3[
144 ]]m4_ifval($2, [[[input.y:17.10-58: warning: unused value: $]4[
145 ]]])[[input.y:17.10-58: warning: unused value: $]5[
146 input.y:18.10-72: warning: unset value: $]$[
147 input.y:18.10-72: warning: unused value: $]1[
148 input.y:18.10-72: warning: unused value: $]3[
149 ]]m4_ifval($2, [[[input.y:18.10-72: warning: unused value: $]4[
150 ]]])[[input.y:18.10-72: warning: unused value: $]5[
151 ]]m4_ifval($2, [[[input.y:20.10-55: warning: unused value: $]3[
152 ]]])[[input.y:21.10-68: warning: unset value: $]$[
153 input.y:21.10-68: warning: unused value: $]1[
154 input.y:21.10-68: warning: unused value: $]2[
155 ]]m4_ifval($2, [[[input.y:21.10-68: warning: unused value: $]4[
159 ## --------------- ##
161 ## --------------- ##
163 AT_SETUP([Unused values])
164 AT_CHECK_UNUSED_VALUES
165 AT_CHECK_UNUSED_VALUES(, [1])
169 ## ------------------------------------------ ##
170 ## Unused values before symbol declarations. ##
171 ## ------------------------------------------ ##
173 AT_SETUP([Unused values before symbol declarations])
174 AT_CHECK_UNUSED_VALUES([1])
175 AT_CHECK_UNUSED_VALUES([1], [1])
179 ## --------------------------------------------- ##
180 ## Default %printer and %destructor redeclared. ##
181 ## --------------------------------------------- ##
183 AT_SETUP([Default %printer and %destructor redeclared])
186 [[%destructor { destroy ($$); } <*> <*>
187 %printer { destroy ($$); } <*> <*>
189 %destructor { destroy ($$); } <*>
190 %printer { destroy ($$); } <*>
192 %destructor { destroy ($$); } <> <>
193 %printer { destroy ($$); } <> <>
195 %destructor { destroy ($$); } <>
196 %printer { destroy ($$); } <>
202 %destructor { destroy ($$); } <*>;
203 %printer { destroy ($$); } <*>;
205 %destructor { destroy ($$); } <>;
206 %printer { destroy ($$); } <>;
209 AT_CHECK([bison input.y], [1], [],
210 [[input.y:1.13-29: redeclaration for default tagged %destructor
211 input.y:1.13-29: previous declaration
212 input.y:2.10-26: redeclaration for default tagged %printer
213 input.y:2.10-26: previous declaration
214 input.y:4.13-29: redeclaration for default tagged %destructor
215 input.y:1.13-29: previous declaration
216 input.y:5.10-26: redeclaration for default tagged %printer
217 input.y:2.10-26: previous declaration
218 input.y:7.13-29: redeclaration for default tagless %destructor
219 input.y:7.13-29: previous declaration
220 input.y:8.10-26: redeclaration for default tagless %printer
221 input.y:8.10-26: previous declaration
222 input.y:10.13-29: redeclaration for default tagless %destructor
223 input.y:7.13-29: previous declaration
224 input.y:11.10-26: redeclaration for default tagless %printer
225 input.y:8.10-26: previous declaration
226 input.y:17.13-29: redeclaration for default tagged %destructor
227 input.y:4.13-29: previous declaration
228 input.y:18.10-26: redeclaration for default tagged %printer
229 input.y:5.10-26: previous declaration
230 input.y:20.13-29: redeclaration for default tagless %destructor
231 input.y:10.13-29: previous declaration
232 input.y:21.10-26: redeclaration for default tagless %printer
233 input.y:11.10-26: previous declaration
239 ## ---------------------------------------------- ##
240 ## Per-type %printer and %destructor redeclared. ##
241 ## ---------------------------------------------- ##
243 AT_SETUP([Per-type %printer and %destructor redeclared])
246 [[%destructor { destroy ($$); } <field1> <field2>
247 %printer { destroy ($$); } <field1> <field2>
249 %destructor { destroy ($$); } <field1> <field1>
250 %printer { destroy ($$); } <field2> <field2>
256 %destructor { destroy ($$); } <field2> <field1>;
257 %printer { destroy ($$); } <field2> <field1>;
260 AT_CHECK([bison input.y], [1], [],
261 [[input.y:4.13-29: %destructor redeclaration for <field1>
262 input.y:1.13-29: previous declaration
263 input.y:4.13-29: %destructor redeclaration for <field1>
264 input.y:4.13-29: previous declaration
265 input.y:5.10-26: %printer redeclaration for <field2>
266 input.y:2.10-26: previous declaration
267 input.y:5.10-26: %printer redeclaration for <field2>
268 input.y:5.10-26: previous declaration
269 input.y:11.13-29: %destructor redeclaration for <field1>
270 input.y:4.13-29: previous declaration
271 input.y:11.13-29: %destructor redeclaration for <field2>
272 input.y:1.13-29: previous declaration
273 input.y:12.10-26: %printer redeclaration for <field1>
274 input.y:2.10-26: previous declaration
275 input.y:12.10-26: %printer redeclaration for <field2>
276 input.y:5.10-26: previous declaration
282 ## ---------------------------------------- ##
283 ## Unused values with default %destructor. ##
284 ## ---------------------------------------- ##
286 AT_SETUP([Unused values with default %destructor])
289 [[%destructor { destroy ($$); } <>
294 start: end end tagged tagged { $<tag>1; $3; } ;
299 AT_CHECK([bison input.y], [0], [],
300 [[input.y:6.8-45: warning: unset value: $$
301 input.y:6.8-45: warning: unused value: $2
302 input.y:7.6-8: warning: unset value: $$
306 [[%destructor { destroy ($$); } <*>
311 start: end end tagged tagged { $<tag>1; $3; } ;
316 AT_CHECK([bison input.y], [0], [],
317 [[input.y:6.8-45: warning: unused value: $4
318 input.y:8.9-11: warning: unset value: $$
324 ## ----------------------------------------- ##
325 ## Unused values with per-type %destructor. ##
326 ## ----------------------------------------- ##
328 AT_SETUP([Unused values with per-type %destructor])
331 [[%destructor { destroy ($$); } <field1>
332 %type <field1> start end
336 start: end end { $1; } ;
340 AT_CHECK([bison input.y], [0], [],
341 [[input.y:6.8-22: warning: unset value: $$
342 input.y:6.8-22: warning: unused value: $2
343 input.y:7.6-8: warning: unset value: $$
349 ## ---------------------- ##
350 ## Incompatible Aliases. ##
351 ## ---------------------- ##
353 AT_SETUP([Incompatible Aliases])
360 %destructor {bar} foo
365 %destructor {baz} "foo"
372 AT_CHECK([bison input.y], [1], [],
373 [[input.y:8.7-11: %type redeclaration for foo
374 input.y:3.7-11: previous declaration
375 input.y:10.13-17: %destructor redeclaration for foo
376 input.y:5.13-17: previous declaration
377 input.y:9.10-14: %printer redeclaration for foo
378 input.y:4.10-14: previous declaration
379 input.y:11.1-5: %left redeclaration for foo
380 input.y:6.1-5: previous declaration
387 ## ----------------------- ##
388 ## Torturing the Scanner. ##
389 ## ----------------------- ##
391 # Be sure to compile and run, so that the C compiler checks what
394 AT_SETUP([Torturing the Scanner])
397 AT_DATA([input.y], [])
398 AT_CHECK([bison input.y], [1], [],
399 [[input.y:1.1: syntax error, unexpected end of file
406 AT_CHECK([bison input.y], [1], [],
407 [[input.y:1.1-2: syntax error, unexpected {...}
411 AT_DATA_GRAMMAR([input.y],
413 /* This is seen in GCC: a %{ and %} in middle of a comment. */
414 const char *foo = "So %{ and %} can be here too.";
417 /* These examples test Bison while not stressing C compilers too much.
418 Many C compilers mishandle backslash-newlines, so this part of the
419 test is inside "#if 0". The comment and string are written so that
420 the "#endif" will be seen regardless of the C compiler bugs that we
423 HP C (as of late 2002) mishandles *\[newline]\[newline]/ within a
426 The Apple Darwin compiler (as of late 2002) mishandles
427 \\[newline]' within a character constant.
432 * A comment with backslash-newlines in it. %} *\
435 /* { Close the above comment, if the C compiler mishandled it. */
438 " A string with backslash-newlines in it %{ %} \\
442 char apostrophe = '\'';
448 /* %{ and %} can be here too. */
451 /* Exercise pre-prologue dependency to %union. */
455 /* Exercise M4 quoting: '@:>@@:>@', 0. */
457 /* Also exercise %union. */
460 value ival; /* A comment to exercise an old bug. */
464 /* Exercise post-prologue dependency to %union. */
466 static YYSTYPE value_as_yystype (value val);
468 /* Exercise quotes in declarations. */
469 char quote[] = "@:>@@:>@,";
473 static void yyerror (const char *s);
474 static int yylex (void);
479 /* Exercise quotes in strings. */
480 %token FAKE "fake @<:@@:>@ \a\b\f\n\r\t\v\"\'\?\\\u005B\U0000005c ??!??'??(??)??-??/??<??=??> \x1\1"
483 /* Exercise M4 quoting: '@:>@@:>@', @<:@, 1. */
484 exp: '@<:@' '\1' two '$' '@' '{' oline output.or.oline.opt
486 /* Exercise quotes in braces. */
487 char tmp[] = "@<:@%c@:>@,\n";
492 two: '\x000000000000000000000000000000000000000000000000000000000000000000002';
493 oline: '@' 'o' 'l' 'i' 'n' 'e' '@' '_' '_' 'o' 'l' 'i' 'n' 'e' '_' '_';
494 output.or.oline.opt: ;|oline;;|output;;;
495 output: '#' 'o' 'u' 't' 'p' 'u' 't' ' ';
497 /* Exercise M4 quoting: '@:>@@:>@', @<:@, 2. */
500 value_as_yystype (value val)
510 static char const input[] = "@<:@\1\2$@{@oline@__@&t@oline__\
513 static size_t toknum;
514 if (! (toknum < sizeof input))
516 yylval = value_as_yystype (input[toknum]);
517 return input[toknum++];
521 yyerror (const char *msg)
523 fprintf (stderr, "%s\n", msg);
527 # Pacify Emacs'font-lock-mode: "
542 AT_CHECK([bison -d -v -o input.c input.y])
543 AT_COMPILE([input.o], [-c input.c])
544 AT_COMPILE([main.o], [-c main.c])
545 AT_COMPILE([input], [input.o main.o])
546 AT_PARSER_CHECK([./input], 0,
553 ## ---------------------- ##
554 ## Typed symbol aliases. ##
555 ## ---------------------- ##
557 AT_SETUP([Typed symbol aliases])
559 # Bison 2.0 broke typed symbol aliases - ensure they work.
561 AT_DATA_GRAMMAR([input.y],
566 %token <val> MY_TOKEN "MY TOKEN"
573 AT_CHECK([bison -o input.c input.y])
582 m4_define([AT_CHECK_REQUIRE],
583 [AT_SETUP([Require $1])
584 AT_DATA_GRAMMAR([input.y],
589 AT_CHECK([bison -o input.c input.y], $2, [], ignore)
593 AT_CHECK_REQUIRE(1.0, 0)
594 AT_CHECK_REQUIRE(AT_PACKAGE_VERSION, 0)
595 ## FIXME: Some day augment this version number.
596 AT_CHECK_REQUIRE(100.0, 63)
599 ## ------------------------------------- ##
600 ## String aliases for character tokens. ##
601 ## ------------------------------------- ##
603 AT_SETUP([String aliases for character tokens])
605 # Bison once thought a character token and its alias were different symbols
606 # with the same user token number.
608 AT_DATA_GRAMMAR([input.y],
615 AT_CHECK([bison -o input.c input.y])
620 ## --------------------- ##
621 ## Unclosed constructs. ##
622 ## --------------------- ##
624 AT_SETUP([Unclosed constructs])
626 # Bison's scan-gram.l once forgot to STRING_FINISH some unclosed constructs, so
627 # they were prepended to whatever it STRING_GROW'ed next. It also threw them
628 # away rather than returning them to the parser. The effect was confusing
629 # subsequent error messages.
634 %token AB "ab" // Used to complain that "ab" was already used.
637 %token TICK_TWELVE "'12" // Used to complain that "'12" was already used.
643 // Used to report a syntax error because it didn't see any kind of symbol
649 // Used to report a syntax error because it didn't see braced code.
650 %destructor { free ($$)
653 AT_CHECK([bison -o input.c input.y], 1, [],
654 [[input.y:1.10-2.0: missing `"' at end of line
655 input.y:4.10-5.0: missing `'' at end of line
656 input.y:14.11-15.0: missing `'' at end of line
657 input.y:16.11-17.0: missing `"' at end of line
658 input.y:19.13-20.0: missing `}' at end of file
659 input.y:20.1: syntax error, unexpected end of file
665 ## ------------------------- ##
666 ## %start after first rule. ##
667 ## ------------------------- ##
669 AT_SETUP([%start after first rule])
671 # Bison once complained that a %start after the first rule was a redeclaration
672 # of the start symbol.
681 AT_CHECK([bison -o input.c input.y])
686 ## --------------------- ##
687 ## %prec takes a token. ##
688 ## --------------------- ##
690 AT_SETUP([%prec takes a token])
692 # Bison once allowed %prec sym where sym was a nonterminal.
696 start: PREC %prec PREC ;
700 AT_CHECK([bison input.y], [1], [],
701 [[input.y:3.1-4: rule given for PREC, which is a token
707 ## -------------------------------- ##
708 ## Reject unused %code qualifiers. ##
709 ## -------------------------------- ##
711 AT_SETUP([Reject unused %code qualifiers])
721 AT_CHECK([[bison input-c.y]], [0], [],
722 [[input-c.y:1.7: warning: %code qualifier `q' is not used
723 input-c.y:2.7-9: warning: %code qualifier `bad' is not used
724 input-c.y:3.7-9: warning: %code qualifier `bad' is not used
725 input-c.y:4.7-12: warning: %code qualifier `format' is not used
728 AT_DATA([input-c-glr.y],
735 AT_CHECK([[bison input-c-glr.y]], [0], [],
736 [[input-c-glr.y:1.7: warning: %code qualifier `q' is not used
737 input-c-glr.y:2.7-9: warning: %code qualifier `bad' is not used
738 input-c-glr.y:3.8-10: warning: %code qualifier `bad' is not used
741 AT_DATA([input-c++.y],
748 AT_CHECK([[bison input-c++.y]], [0], [],
749 [[input-c++.y:1.7: warning: %code qualifier `q' is not used
750 input-c++.y:2.7-9: warning: %code qualifier `bad' is not used
751 input-c++.y:3.8: warning: %code qualifier `q' is not used
754 AT_DATA([input-c++-glr.y],
761 AT_CHECK([[bison input-c++-glr.y]], [0], [],
762 [[input-c++-glr.y:1.7-9: warning: %code qualifier `bad' is not used
763 input-c++-glr.y:2.7: warning: %code qualifier `q' is not used
764 input-c++-glr.y:3.7: warning: %code qualifier `q' is not used
767 AT_DATA([special-char-@@.y],
774 AT_CHECK([[bison special-char-@@.y]], [0], [],
775 [[special-char-@@.y:1.7-9: warning: %code qualifier `bad' is not used
776 special-char-@@.y:2.7: warning: %code qualifier `q' is not used
777 special-char-@@.y:3.7: warning: %code qualifier `q' is not used
780 AT_DATA([special-char-@:>@.y],
787 AT_CHECK([[bison special-char-@:>@.y]], [0], [],
788 [[special-char-@:>@.y:1.7-9: warning: %code qualifier `bad' is not used
789 special-char-@:>@.y:2.7: warning: %code qualifier `q' is not used
790 special-char-@:>@.y:3.7: warning: %code qualifier `q' is not used
796 ## ---------------- ##
797 ## %define errors. ##
798 ## ---------------- ##
800 AT_SETUP([%define errors])
803 [[%define var "value1"
806 %define special1 "@:>@"
807 %define special2 "@<:@"
812 AT_CHECK([[bison input.y]], [0], [],
813 [[input.y:2.9-11: warning: %define variable `var' redefined
814 input.y:3.10-12: warning: %define variable `var' redefined
815 input.y:1.9-11: warning: %define variable `var' is not used
816 input.y:2.9-11: warning: %define variable `var' is not used
817 input.y:3.10-12: warning: %define variable `var' is not used
818 input.y:4.9-16: warning: %define variable `special1' is not used
819 input.y:5.9-16: warning: %define variable `special2' is not used