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