]> git.saurik.com Git - bison.git/blame - tests/input.at
* TODO: Add request from Nelson H. F. Beebe to be able to install
[bison.git] / tests / input.at
CommitLineData
087b9fdf 1# Checking the Bison scanner. -*- Autotest -*-
378f4bd8 2# Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
9b2d0677
AD
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
0fb669f9
PE
16# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17# 02110-1301, USA.
9b2d0677
AD
18
19AT_BANNER([[Input Processing.]])
20
21# Mostly test that we are robust to mistakes.
22
6c35d22c 23
9b2d0677
AD
24## ------------ ##
25## Invalid $n. ##
26## ------------ ##
27
e9071366 28AT_SETUP([Invalid \$n and @n])
9b2d0677
AD
29
30AT_DATA([input.y],
31[[%%
bfcf1f3a 32exp: { $$ = $1 ; };
bfcf1f3a 33exp: { @$ = @1 ; };
9b2d0677
AD
34]])
35
36AT_CHECK([bison input.y], [1], [],
e9071366
AD
37[[input.y:2.13-14: integer out of range: `$1'
38input.y:3.13-14: integer out of range: `@1'
9b2d0677
AD
39]])
40
41AT_CLEANUP
9af3fbce
AD
42
43
44## -------------- ##
e776192e 45## Type Clashes. ##
9af3fbce
AD
46## -------------- ##
47
e776192e 48AT_SETUP([Type Clashes])
9af3fbce
AD
49
50AT_DATA([input.y],
51[[%token foo
52%type <bar> exp
53%%
54exp: foo {} foo
55 | foo
56 | /* Empty. */
57 ;
58]])
59
38e71ff8 60AT_CHECK([bison input.y], [], [],
b06af56f
PE
61[[input.y:4.6-15: warning: type clash on default action: <bar> != <>
62input.y:5.6-8: warning: type clash on default action: <bar> != <>
63input.y:6.5: warning: empty rule for typed nonterminal, and no action
9af3fbce
AD
64]])
65
66AT_CLEANUP
5a08f1ce
AD
67
68
378f4bd8
AD
69## --------------- ##
70## Unused values. ##
71## --------------- ##
72
27622431 73AT_SETUP([Unused values])
378f4bd8
AD
74
75AT_DATA([input.y],
76[[%token <integer> INT
27622431
PE
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
378f4bd8 79%%
27622431
PE
80start:
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 }
378f4bd8 84;
27622431
PE
85
86a: INT | INT { } INT { } INT { };
87b: INT | /* empty */;
88c: INT | INT { $1 } INT { } INT { };
89d: INT | INT { } INT { $1 } INT { };
90e: INT | INT { } INT { } INT { $1 };
91f: INT | INT { } INT { } INT { $$ = $1 + $3 + $5; };
92g: INT | INT { $$ } INT { $$ } INT { };
93h: INT | INT { $$ } INT { $$ = $2 } INT { };
94i: INT | INT INT { } { $$ = $1 + $2; };
95j: INT | INT INT { $<integer>$ = 1; } { $$ = $1 + $2; };
96k: INT | INT INT { $$; } { $$ = $3; } { };
97l: INT | INT { $$ = $1; } INT { $$ = $2 + $3; } INT { $$ = $4 + $5; };
98
378f4bd8
AD
99]])
100
101AT_CHECK([bison input.y], [], [],
27622431
PE
102[[input.y:11.10-32: warning: unset value: $$
103input.y:11.10-32: warning: unused value: $1
104input.y:11.10-32: warning: unused value: $3
105input.y:11.10-32: warning: unused value: $5
106input.y:12.9: warning: empty rule for typed nonterminal, and no action
107input.y:13.10-35: warning: unset value: $$
108input.y:13.10-35: warning: unused value: $3
109input.y:13.10-35: warning: unused value: $5
110input.y:14.10-35: warning: unset value: $$
111input.y:14.10-35: warning: unused value: $3
112input.y:14.10-35: warning: unused value: $5
113input.y:15.10-36: warning: unset value: $$
114input.y:15.10-36: warning: unused value: $3
115input.y:15.10-36: warning: unused value: $5
116input.y:17.10-38: warning: unset value: $$
117input.y:17.10-38: warning: unused value: $1
118input.y:17.10-38: warning: unused value: $2
119input.y:17.10-38: warning: unused value: $3
120input.y:17.10-38: warning: unused value: $4
121input.y:17.10-38: warning: unused value: $5
122input.y:18.10-43: warning: unset value: $$
123input.y:18.10-43: warning: unused value: $1
124input.y:18.10-43: warning: unused value: $3
125input.y:18.10-43: warning: unused value: $4
126input.y:18.10-43: warning: unused value: $5
127input.y:20.10-55: warning: unused value: $3
128input.y:21.10-41: warning: unset value: $$
129input.y:21.10-41: warning: unused value: $1
130input.y:21.10-41: warning: unused value: $2
131input.y:21.10-41: warning: unused value: $4
132]])
84866159
AD
133
134AT_CLEANUP
378f4bd8
AD
135
136
df09ef2e
AD
137## ---------------------- ##
138## Incompatible Aliases. ##
139## ---------------------- ##
140
141AT_SETUP([Incompatible Aliases])
142
143AT_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%%
157exp: foo;
158]])
159
160AT_CHECK([bison input.y], [1], [],
161[[input.y:8.7-11: %type redeclaration for foo
162input.y:3.7-11: first declaration
163input.y:10.13-17: %destructor redeclaration for foo
164input.y:5.13-17: first declaration
e2a8c0f5 165input.y:9.10-14: %printer redeclaration for foo
df09ef2e
AD
166input.y:10.13-17: first declaration
167input.y:11.1-5: %left redeclaration for foo
168input.y:6.1-5: first declaration
169]])
170
171AT_CLEANUP
172
173
5a08f1ce
AD
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
182AT_SETUP([Torturing the Scanner])
183
0baf7c50
PE
184
185AT_DATA([input.y], [])
186AT_CHECK([bison input.y], [1], [],
e9071366 187[[input.y:1.0: syntax error, unexpected end of file
0baf7c50
PE
188]])
189
190
e9071366 191AT_DATA([input.y],
b2ddc3f3
AD
192[{}
193])
194AT_CHECK([bison input.y], [1], [],
2346344a 195[[input.y:1.0-1: syntax error, unexpected {...}
b2ddc3f3
AD
196]])
197
198
9501dc6e 199AT_DATA_GRAMMAR([input.y],
5a08f1ce
AD
200[[%{
201/* This is seen in GCC: a %{ and %} in middle of a comment. */
202const char *foo = "So %{ and %} can be here too.";
203
dda7a53e
PE
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
206fe6a5 219/\
dda7a53e
PE
220* A comment with backslash-newlines in it. %} *\
221\
206fe6a5 222/
dda7a53e 223/* { Close the above comment, if the C compiler mishandled it. */
206fe6a5
PE
224
225char str[] = "\\
226" A string with backslash-newlines in it %{ %} \\
dda7a53e 227\
206fe6a5
PE
228"";
229
dda7a53e 230char apostrophe = '\'';
206fe6a5
PE
231#endif
232
5a08f1ce
AD
233#include <stdio.h>
234%}
235/* %{ and %} can be here too. */
236
237%{
238/* Exercise pre-prologue dependency to %union. */
051ade83 239typedef int value;
5a08f1ce
AD
240%}
241
242/* Exercise M4 quoting: '@:>@@:>@', 0. */
243
244/* Also exercise %union. */
245%union
246{
051ade83 247 value ival; /* A comment to exercise an old bug. */
5a08f1ce
AD
248};
249
250
251/* Exercise post-prologue dependency to %union. */
252%{
051ade83 253static YYSTYPE value_as_yystype (value val);
5a08f1ce
AD
254
255/* Exercise quotes in declarations. */
256char quote[] = "@:>@@:>@,";
257%}
258
259%{
260static void yyerror (const char *s);
261static int yylex (void);
262%}
263
206fe6a5 264%type <ival> '@<:@'
5a08f1ce
AD
265
266/* Exercise quotes in strings. */
3dc4c5fa 267%token FAKE "fake @<:@@:>@ \a\b\f\n\r\t\v\"\'\?\\\u005B\U0000005c ??!??'??(??)??-??/??<??=??> \x1\1"
5a08f1ce
AD
268
269%%
206fe6a5 270/* Exercise M4 quoting: '@:>@@:>@', @<:@, 1. */
087b9fdf 271exp: '@<:@' '\1' two '$' '@' '{' oline output.or.oline.opt
5a08f1ce
AD
272 {
273 /* Exercise quotes in braces. */
274 char tmp[] = "@<:@%c@:>@,\n";
275 printf (tmp, $1);
276 }
277;
5b7f88c7
PE
278
279two: '\x000000000000000000000000000000000000000000000000000000000000000000002';
280oline: '@' 'o' 'l' 'i' 'n' 'e' '@' '_' '_' 'o' 'l' 'i' 'n' 'e' '_' '_';
087b9fdf 281output.or.oline.opt: ;|oline;;|output;;;
5b7f88c7 282output: '#' 'o' 'u' 't' 'p' 'u' 't' ' ';
5a08f1ce 283%%
206fe6a5 284/* Exercise M4 quoting: '@:>@@:>@', @<:@, 2. */
5a08f1ce
AD
285
286static YYSTYPE
051ade83 287value_as_yystype (value val)
5a08f1ce
AD
288{
289 YYSTYPE res;
290 res.ival = val;
291 return res;
292}
293
294static int
295yylex (void)
296{
5b7f88c7
PE
297 static const char *input = "@<:@\1\2$@{@oline@__@&t@oline__\
298#output "; /* "
299 */
051ade83 300 yylval = value_as_yystype (*input);
5a08f1ce
AD
301 return *input++;
302}
303
304static void
305yyerror (const char *msg)
306{
307 fprintf (stderr, "%s\n", msg);
308}
309]])
310
9501dc6e
AD
311# Pacify Emacs'font-lock-mode: "
312
5a08f1ce 313AT_DATA([main.c],
051ade83 314[[typedef int value;
5a08f1ce
AD
315#include "input.h"
316
317int yyparse (void);
318
319int
320main (void)
321{
322 return yyparse ();
323}
324]])
325
b56471a6 326AT_CHECK([bison -d -v -o input.c input.y])
efc6bf1b
PE
327AT_COMPILE([input.o], [-c input.c])
328AT_COMPILE([main.o], [-c main.c])
329AT_COMPILE([input], [input.o main.o])
1154cced 330AT_PARSER_CHECK([./input], 0,
206fe6a5 331[[[@<:@],
5a08f1ce
AD
332]])
333
334AT_CLEANUP
e59adf8f
PE
335
336
337## ---------------------- ##
338## Typed symbol aliases. ##
339## ---------------------- ##
340
341AT_SETUP([Typed symbol aliases])
342
343# Bison 2.0 broke typed symbol aliases - ensure they work.
344
345AT_DATA_GRAMMAR([input.y],
346[[%union
347{
348 int val;
349};
350%token <val> MY_TOKEN "MY TOKEN"
351%type <val> exp
352%%
353exp: "MY TOKEN";
354%%
355]])
356
357AT_CHECK([bison -o input.c input.y])
358
359AT_CLEANUP
b50d2359
AD
360
361
362## --------- ##
363## Require. ##
364## --------- ##
365
366m4_define([AT_CHECK_REQUIRE],
367[AT_SETUP([Require $1])
368AT_DATA_GRAMMAR([input.y],
369[[%require "$1";
370%%
371empty_file:;
372]])
373AT_CHECK([bison -o input.c input.y], $2, [], ignore)
374AT_CLEANUP
375])
376
377AT_CHECK_REQUIRE(1.0, 0)
378AT_CHECK_REQUIRE(AT_PACKAGE_VERSION, 0)
379## FIXME: Some day augment this version number.
9b8a5ce0 380AT_CHECK_REQUIRE(100.0, 63)