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