]> git.saurik.com Git - bison.git/blob - tests/input.at
f533ef3a49dd0dd0fcd720a0d3989f2934af4617
[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 dollar-n])
29
30 AT_DATA([input.y],
31 [[%%
32 exp: { $$ = $1 ; };
33 ]])
34
35 AT_CHECK([bison input.y], [1], [],
36 [[input.y:2.13-14: integer out of range: `$1'
37 ]])
38
39 AT_CLEANUP
40
41
42 ## ------------ ##
43 ## Invalid @n. ##
44 ## ------------ ##
45
46 AT_SETUP([Invalid @n])
47
48 AT_DATA([input.y],
49 [[%%
50 exp: { @$ = @1 ; };
51 ]])
52
53 AT_CHECK([bison input.y], [1], [],
54 [[input.y:2.13-14: integer out of range: `@1'
55 ]])
56
57 AT_CLEANUP
58
59
60 ## -------------- ##
61 ## Type Clashes. ##
62 ## -------------- ##
63
64 AT_SETUP([Type Clashes])
65
66 AT_DATA([input.y],
67 [[%token foo
68 %type <bar> exp
69 %%
70 exp: foo {} foo
71 | foo
72 | /* Empty. */
73 ;
74 ]])
75
76 AT_CHECK([bison input.y], [], [],
77 [[input.y:4.6-15: warning: type clash on default action: <bar> != <>
78 input.y:5.6-8: warning: type clash on default action: <bar> != <>
79 input.y:6.5: warning: empty rule for typed nonterminal, and no action
80 ]])
81
82 AT_CLEANUP
83
84
85 ## --------------- ##
86 ## Unused values. ##
87 ## --------------- ##
88
89 AT_SETUP([Unused values])
90
91 AT_DATA([input.y],
92 [[%token <integer> INT
93 %type <integer> exp
94 %%
95 exp:
96 INT { } INT { } INT { }
97 /* Ideally we would like to complain also about $2 and $4 here, but
98 it's hard to implement. */
99 | INT { $$ } INT { $$ } INT { }
100 | INT { $1 } INT { } INT { }
101 | INT { } INT { $1 } INT { }
102 | INT { } INT { } INT { $1 }
103 | INT { } INT { } INT { $$ = $1 + $3 + $5; }
104 ;
105 ]])
106
107 AT_CHECK([bison input.y], [], [],
108 [[input.y:5.3-25: warning: unset value: $$
109 input.y:5.3-25: warning: unused value: $1
110 input.y:5.3-25: warning: unused value: $3
111 input.y:5.3-25: warning: unused value: $5
112 input.y:8.3-31: warning: unset value: $$
113 input.y:8.3-31: warning: unused value: $1
114 input.y:8.3-31: warning: unused value: $3
115 input.y:8.3-31: warning: unused value: $5
116 input.y:9.3-28: warning: unset value: $$
117 input.y:9.3-28: warning: unused value: $3
118 input.y:9.3-28: warning: unused value: $5
119 input.y:10.3-28: warning: unset value: $$
120 input.y:10.3-28: warning: unused value: $3
121 input.y:10.3-28: warning: unused value: $5
122 input.y:11.3-29: warning: unset value: $$
123 input.y:11.3-29: warning: unused value: $3
124 input.y:11.3-29: warning: unused value: $5
125 input.y: conflicts: 1 reduce/reduce
126 input.y:8.7-12: warning: rule never reduced because of conflicts: @3: /* empty */
127 input.y:9.7-12: warning: rule never reduced because of conflicts: @5: /* empty */
128 input.y:10.7-9: warning: rule never reduced because of conflicts: @7: /* empty */
129 input.y:11.7-9: warning: rule never reduced because of conflicts: @9: /* empty */
130 input.y:12.7-9: warning: rule never reduced because of conflicts: @11: /* empty */
131 ]])
132
133 AT_CLEANUP
134
135
136 ## ---------------------- ##
137 ## Incompatible Aliases. ##
138 ## ---------------------- ##
139
140 AT_SETUP([Incompatible Aliases])
141
142 AT_DATA([input.y],
143 [[%token foo "foo"
144
145 %type <bar> foo
146 %printer {bar} foo
147 %destructor {bar} foo
148 %left foo
149
150 %type <baz> "foo"
151 %printer {baz} "foo"
152 %destructor {baz} "foo"
153 %left "foo"
154
155 %%
156 exp: foo;
157 ]])
158
159 AT_CHECK([bison input.y], [1], [],
160 [[input.y:8.7-11: %type redeclaration for foo
161 input.y:3.7-11: first declaration
162 input.y:10.13-17: %destructor redeclaration for foo
163 input.y:5.13-17: first declaration
164 input.y:9.19-23: %printer redeclaration for foo
165 input.y:10.13-17: first declaration
166 input.y:11.1-5: %left redeclaration for foo
167 input.y:6.1-5: first declaration
168 ]])
169
170 AT_CLEANUP
171
172
173
174 ## ----------------------- ##
175 ## Torturing the Scanner. ##
176 ## ----------------------- ##
177
178 # Be sure to compile and run, so that the C compiler checks what
179 # we do.
180
181 AT_SETUP([Torturing the Scanner])
182
183
184 AT_DATA([input.y], [])
185 AT_CHECK([bison input.y], [1], [],
186 [[input.y:1.1: syntax error, unexpected end of file
187 ]])
188
189
190 AT_DATA_GRAMMAR([input.y],
191 [[%{
192 /* This is seen in GCC: a %{ and %} in middle of a comment. */
193 const char *foo = "So %{ and %} can be here too.";
194
195 #if 0
196 /* These examples test Bison while not stressing C compilers too much.
197 Many C compilers mishandle backslash-newlines, so this part of the
198 test is inside "#if 0". The comment and string are written so that
199 the "#endif" will be seen regardless of the C compiler bugs that we
200 know about, namely:
201
202 HP C (as of late 2002) mishandles *\[newline]\[newline]/ within a
203 comment.
204
205 The Apple Darwin compiler (as of late 2002) mishandles
206 \\[newline]' within a character constant.
207
208 */
209
210 /\
211 * A comment with backslash-newlines in it. %} *\
212 \
213 /
214 /* { Close the above comment, if the C compiler mishandled it. */
215
216 char str[] = "\\
217 " A string with backslash-newlines in it %{ %} \\
218 \
219 "";
220
221 char apostrophe = '\'';
222 #endif
223
224 #include <stdio.h>
225 %}
226 /* %{ and %} can be here too. */
227
228 %{
229 /* Exercise pre-prologue dependency to %union. */
230 typedef int value;
231 %}
232
233 /* Exercise M4 quoting: '@:>@@:>@', 0. */
234
235 /* Also exercise %union. */
236 %union
237 {
238 value ival; /* A comment to exercise an old bug. */
239 };
240
241
242 /* Exercise post-prologue dependency to %union. */
243 %{
244 static YYSTYPE value_as_yystype (value val);
245
246 /* Exercise quotes in declarations. */
247 char quote[] = "@:>@@:>@,";
248 %}
249
250 %{
251 static void yyerror (const char *s);
252 static int yylex (void);
253 %}
254
255 %type <ival> '@<:@'
256
257 /* Exercise quotes in strings. */
258 %token FAKE "fake @<:@@:>@ \a\b\f\n\r\t\v\"\'\?\\\u005B\U0000005c ??!??'??(??)??-??/??<??=??> \x1\1"
259
260 %%
261 /* Exercise M4 quoting: '@:>@@:>@', @<:@, 1. */
262 exp: '@<:@' '\1' two '$' '@' '{' oline output.or.oline.opt
263 {
264 /* Exercise quotes in braces. */
265 char tmp[] = "@<:@%c@:>@,\n";
266 printf (tmp, $1);
267 }
268 ;
269
270 two: '\x000000000000000000000000000000000000000000000000000000000000000000002';
271 oline: '@' 'o' 'l' 'i' 'n' 'e' '@' '_' '_' 'o' 'l' 'i' 'n' 'e' '_' '_';
272 output.or.oline.opt: ;|oline;;|output;;;
273 output: '#' 'o' 'u' 't' 'p' 'u' 't' ' ';
274 %%
275 /* Exercise M4 quoting: '@:>@@:>@', @<:@, 2. */
276
277 static YYSTYPE
278 value_as_yystype (value val)
279 {
280 YYSTYPE res;
281 res.ival = val;
282 return res;
283 }
284
285 static int
286 yylex (void)
287 {
288 static const char *input = "@<:@\1\2$@{@oline@__@&t@oline__\
289 #output "; /* "
290 */
291 yylval = value_as_yystype (*input);
292 return *input++;
293 }
294
295 static void
296 yyerror (const char *msg)
297 {
298 fprintf (stderr, "%s\n", msg);
299 }
300 ]])
301
302 # Pacify Emacs'font-lock-mode: "
303
304 AT_DATA([main.c],
305 [[typedef int value;
306 #include "input.h"
307
308 int yyparse (void);
309
310 int
311 main (void)
312 {
313 return yyparse ();
314 }
315 ]])
316
317 AT_CHECK([bison -d -v -o input.c input.y])
318 AT_COMPILE([input.o], [-c input.c])
319 AT_COMPILE([main.o], [-c main.c])
320 AT_COMPILE([input], [input.o main.o])
321 AT_PARSER_CHECK([./input], 0,
322 [[[@<:@],
323 ]])
324
325 AT_CLEANUP
326
327
328 ## ---------------------- ##
329 ## Typed symbol aliases. ##
330 ## ---------------------- ##
331
332 AT_SETUP([Typed symbol aliases])
333
334 # Bison 2.0 broke typed symbol aliases - ensure they work.
335
336 AT_DATA_GRAMMAR([input.y],
337 [[%union
338 {
339 int val;
340 };
341 %token <val> MY_TOKEN "MY TOKEN"
342 %type <val> exp
343 %%
344 exp: "MY TOKEN";
345 %%
346 ]])
347
348 AT_CHECK([bison -o input.c input.y])
349
350 AT_CLEANUP
351
352
353 ## --------- ##
354 ## Require. ##
355 ## --------- ##
356
357 m4_define([AT_CHECK_REQUIRE],
358 [AT_SETUP([Require $1])
359 AT_DATA_GRAMMAR([input.y],
360 [[%require "$1";
361 %%
362 empty_file:;
363 ]])
364 AT_CHECK([bison -o input.c input.y], $2, [], ignore)
365 AT_CLEANUP
366 ])
367
368 AT_CHECK_REQUIRE(1.0, 0)
369 AT_CHECK_REQUIRE(AT_PACKAGE_VERSION, 0)
370 ## FIXME: Some day augment this version number.
371 AT_CHECK_REQUIRE(100.0, 63)