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