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