]> git.saurik.com Git - bison.git/blame - tests/input.at
(Typed symbol aliases): New test.
[bison.git] / tests / input.at
CommitLineData
087b9fdf 1# Checking the Bison scanner. -*- Autotest -*-
d46c3fc6 2# Copyright (C) 2002, 2003, 2004, 2005 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
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17# 02111-1307, USA.
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
df09ef2e
AD
85## ---------------------- ##
86## Incompatible Aliases. ##
87## ---------------------- ##
88
89AT_SETUP([Incompatible Aliases])
90
91AT_DATA([input.y],
92[[%token foo "foo"
93
94%type <bar> foo
95%printer {bar} foo
96%destructor {bar} foo
97%left foo
98
99%type <baz> "foo"
100%printer {baz} "foo"
101%destructor {baz} "foo"
102%left "foo"
103
104%%
105exp: foo;
106]])
107
108AT_CHECK([bison input.y], [1], [],
109[[input.y:8.7-11: %type redeclaration for foo
110input.y:3.7-11: first declaration
111input.y:10.13-17: %destructor redeclaration for foo
112input.y:5.13-17: first declaration
113input.y:9.19-23: %printer redeclaration for foo
114input.y:10.13-17: first declaration
115input.y:11.1-5: %left redeclaration for foo
116input.y:6.1-5: first declaration
117]])
118
119AT_CLEANUP
120
121
5a08f1ce
AD
122
123## ----------------------- ##
124## Torturing the Scanner. ##
125## ----------------------- ##
126
127# Be sure to compile and run, so that the C compiler checks what
128# we do.
129
130AT_SETUP([Torturing the Scanner])
131
0baf7c50
PE
132
133AT_DATA([input.y], [])
134AT_CHECK([bison input.y], [1], [],
135[[input.y:1.1: syntax error, unexpected "end of file"
136]])
137
138
9501dc6e 139AT_DATA_GRAMMAR([input.y],
5a08f1ce
AD
140[[%{
141/* This is seen in GCC: a %{ and %} in middle of a comment. */
142const char *foo = "So %{ and %} can be here too.";
143
dda7a53e
PE
144#if 0
145/* These examples test Bison while not stressing C compilers too much.
146 Many C compilers mishandle backslash-newlines, so this part of the
147 test is inside "#if 0". The comment and string are written so that
148 the "#endif" will be seen regardless of the C compiler bugs that we
149 know about, namely:
150
151 HP C (as of late 2002) mishandles *\[newline]\[newline]/ within a
152 comment.
153
154 The Apple Darwin compiler (as of late 2002) mishandles
155 \\[newline]' within a character constant.
156
157 */
158
206fe6a5 159/\
dda7a53e
PE
160* A comment with backslash-newlines in it. %} *\
161\
206fe6a5 162/
dda7a53e 163/* { Close the above comment, if the C compiler mishandled it. */
206fe6a5
PE
164
165char str[] = "\\
166" A string with backslash-newlines in it %{ %} \\
dda7a53e 167\
206fe6a5
PE
168"";
169
dda7a53e 170char apostrophe = '\'';
206fe6a5
PE
171#endif
172
5a08f1ce
AD
173#include <stdio.h>
174%}
175/* %{ and %} can be here too. */
176
177%{
178/* Exercise pre-prologue dependency to %union. */
051ade83 179typedef int value;
5a08f1ce
AD
180%}
181
182/* Exercise M4 quoting: '@:>@@:>@', 0. */
183
184/* Also exercise %union. */
185%union
186{
051ade83 187 value ival; /* A comment to exercise an old bug. */
5a08f1ce
AD
188};
189
190
191/* Exercise post-prologue dependency to %union. */
192%{
051ade83 193static YYSTYPE value_as_yystype (value val);
5a08f1ce
AD
194
195/* Exercise quotes in declarations. */
196char quote[] = "@:>@@:>@,";
197%}
198
199%{
200static void yyerror (const char *s);
201static int yylex (void);
202%}
203
206fe6a5 204%type <ival> '@<:@'
5a08f1ce
AD
205
206/* Exercise quotes in strings. */
3dc4c5fa 207%token FAKE "fake @<:@@:>@ \a\b\f\n\r\t\v\"\'\?\\\u005B\U0000005c ??!??'??(??)??-??/??<??=??> \x1\1"
5a08f1ce
AD
208
209%%
206fe6a5 210/* Exercise M4 quoting: '@:>@@:>@', @<:@, 1. */
087b9fdf 211exp: '@<:@' '\1' two '$' '@' '{' oline output.or.oline.opt
5a08f1ce
AD
212 {
213 /* Exercise quotes in braces. */
214 char tmp[] = "@<:@%c@:>@,\n";
215 printf (tmp, $1);
216 }
217;
5b7f88c7
PE
218
219two: '\x000000000000000000000000000000000000000000000000000000000000000000002';
220oline: '@' 'o' 'l' 'i' 'n' 'e' '@' '_' '_' 'o' 'l' 'i' 'n' 'e' '_' '_';
087b9fdf 221output.or.oline.opt: ;|oline;;|output;;;
5b7f88c7 222output: '#' 'o' 'u' 't' 'p' 'u' 't' ' ';
5a08f1ce 223%%
206fe6a5 224/* Exercise M4 quoting: '@:>@@:>@', @<:@, 2. */
5a08f1ce
AD
225
226static YYSTYPE
051ade83 227value_as_yystype (value val)
5a08f1ce
AD
228{
229 YYSTYPE res;
230 res.ival = val;
231 return res;
232}
233
234static int
235yylex (void)
236{
5b7f88c7
PE
237 static const char *input = "@<:@\1\2$@{@oline@__@&t@oline__\
238#output "; /* "
239 */
051ade83 240 yylval = value_as_yystype (*input);
5a08f1ce
AD
241 return *input++;
242}
243
244static void
245yyerror (const char *msg)
246{
247 fprintf (stderr, "%s\n", msg);
248}
249]])
250
9501dc6e
AD
251# Pacify Emacs'font-lock-mode: "
252
5a08f1ce 253AT_DATA([main.c],
051ade83 254[[typedef int value;
5a08f1ce
AD
255#include "input.h"
256
257int yyparse (void);
258
259int
260main (void)
261{
262 return yyparse ();
263}
264]])
265
b56471a6 266AT_CHECK([bison -d -v -o input.c input.y])
efc6bf1b
PE
267AT_COMPILE([input.o], [-c input.c])
268AT_COMPILE([main.o], [-c main.c])
269AT_COMPILE([input], [input.o main.o])
1154cced 270AT_PARSER_CHECK([./input], 0,
206fe6a5 271[[[@<:@],
5a08f1ce
AD
272]])
273
274AT_CLEANUP
e59adf8f
PE
275
276
277## ---------------------- ##
278## Typed symbol aliases. ##
279## ---------------------- ##
280
281AT_SETUP([Typed symbol aliases])
282
283# Bison 2.0 broke typed symbol aliases - ensure they work.
284
285AT_DATA_GRAMMAR([input.y],
286[[%union
287{
288 int val;
289};
290%token <val> MY_TOKEN "MY TOKEN"
291%type <val> exp
292%%
293exp: "MY TOKEN";
294%%
295]])
296
297AT_CHECK([bison -o input.c input.y])
298
299AT_CLEANUP