]>
Commit | Line | Data |
---|---|---|
342b8b6e | 1 | # Checking the output filenames. -*- Autotest -*- |
5504898e | 2 | # Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc. |
bfb07874 | 3 | |
342b8b6e AD |
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. | |
bfb07874 | 8 | |
342b8b6e AD |
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. | |
bfb07874 | 13 | |
342b8b6e AD |
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. | |
bfb07874 | 18 | |
bfb07874 AD |
19 | ## ---------------------------------------------------- ## |
20 | ## Compile the grammar described in the documentation. ## | |
21 | ## ---------------------------------------------------- ## | |
22 | ||
23 | ||
24 | # ------------------------- # | |
25 | # Helping Autotest macros. # | |
26 | # ------------------------- # | |
27 | ||
28 | ||
29 | # _AT_DATA_CALC_Y($1, $2, $3, [CPP-DIRECTIVES]) | |
30 | # --------------------------------------------- | |
31 | # Produce `calc.y'. Don't call this macro directly, because it contains | |
32 | # some occurrences of `$1' etc. which will be interpreted by m4. So | |
33 | # you should call it with $1, $2, and $3 as arguments, which is what | |
34 | # AT_DATA_CALC_Y does. | |
342b8b6e AD |
35 | m4_define([_AT_DATA_CALC_Y], |
36 | [m4_if([$1$2$3], $[1]$[2]$[3], [], | |
37 | [m4_fatal([$0: Invalid arguments: $@])])dnl | |
bfb07874 AD |
38 | AT_DATA([calc.y], |
39 | [[/* Infix notation calculator--calc */ | |
40 | ||
41 | %{ | |
2ce10144 | 42 | #include <config.h> |
d9684243 | 43 | /* We don't need perfect functions for these tests. */ |
342b8b6e | 44 | #undef malloc |
d9684243 PE |
45 | #undef memcmp |
46 | #undef realloc | |
bfb07874 | 47 | #include <stdio.h> |
2ce10144 AD |
48 | |
49 | #if STDC_HEADERS | |
50 | # include <stdlib.h> | |
51 | # include <string.h> | |
52 | #else | |
53 | char *strcat(char *dest, const char *src); | |
54 | #endif | |
bfb07874 | 55 | #include <ctype.h> |
bfb07874 AD |
56 | |
57 | static int power (int base, int exponent); | |
58 | static void yyerror (const char *s); | |
59 | static int yylex (void); | |
2ce10144 AD |
60 | static int yygetc (void); |
61 | static void yyungetc (int c); | |
62 | ||
bfb07874 | 63 | extern void perror (const char *s); |
0dd1580a RA |
64 | |
65 | /* Exercise pre-prologue dependency to %union. */ | |
66 | typedef int value_t; | |
67 | ||
bfb07874 AD |
68 | %} |
69 | ||
5a08f1ce | 70 | /* Exercise %union. */ |
213e640e AD |
71 | %union |
72 | { | |
5a08f1ce | 73 | value_t ival; |
213e640e AD |
74 | }; |
75 | ||
6b7e85b9 | 76 | /* Bison Declarations */ |
b7c49edf | 77 | %token CALC_EOF 0 "end of file" |
213e640e AD |
78 | %token <ival> NUM "number" |
79 | %type <ival> exp | |
bfb07874 AD |
80 | |
81 | %nonassoc '=' /* comparison */ | |
82 | %left '-' '+' | |
83 | %left '*' '/' | |
84 | %left NEG /* negation--unary minus */ | |
85 | %right '^' /* exponentiation */ | |
86 | ||
147e184c MA |
87 | ]$4[ |
88 | ||
bfb07874 AD |
89 | /* Grammar follows */ |
90 | %% | |
91 | input: | |
b7c49edf | 92 | line |
bfb07874 AD |
93 | | input line |
94 | ; | |
95 | ||
96 | line: | |
5a08f1ce AD |
97 | '\n' {} |
98 | | exp '\n' {} | |
bfb07874 AD |
99 | ; |
100 | ||
101 | exp: | |
102 | NUM { $$ = $1; } | |
103 | | exp '=' exp | |
104 | { | |
105 | if ($1 != $3) | |
51dec47b | 106 | fprintf (stderr, "calc: error: %d != %d\n", $1, $3); |
bfb07874 AD |
107 | $$ = $1 == $3; |
108 | } | |
109 | | exp '+' exp { $$ = $1 + $3; } | |
110 | | exp '-' exp { $$ = $1 - $3; } | |
111 | | exp '*' exp { $$ = $1 * $3; } | |
112 | | exp '/' exp { $$ = $1 / $3; } | |
113 | | '-' exp %prec NEG { $$ = -$2; } | |
114 | | exp '^' exp { $$ = power ($1, $3); } | |
115 | | '(' exp ')' { $$ = $2; } | |
51dec47b | 116 | | '(' error ')' { $$ = 0; } |
bfb07874 AD |
117 | ; |
118 | %% | |
119 | /* The input. */ | |
120 | FILE *yyin; | |
121 | ||
122 | static void | |
123 | yyerror (const char *s) | |
124 | { | |
19c50364 | 125 | #if YYLSP_NEEDED |
51dec47b | 126 | fprintf (stderr, "%d.%d-%d.%d: ", |
bfb07874 AD |
127 | yylloc.first_line, yylloc.first_column, |
128 | yylloc.last_line, yylloc.last_column); | |
129 | #endif | |
130 | fprintf (stderr, "%s\n", s); | |
131 | } | |
132 | ||
b7c49edf AD |
133 | |
134 | #if YYLSP_NEEDED | |
135 | static YYLTYPE last_yylloc; | |
136 | #endif | |
bfb07874 | 137 | static int |
2ce10144 | 138 | yygetc (void) |
bfb07874 AD |
139 | { |
140 | int res = getc (yyin); | |
19c50364 | 141 | #if YYLSP_NEEDED |
b7c49edf | 142 | last_yylloc = yylloc; |
bfb07874 AD |
143 | if (res == '\n') |
144 | { | |
145 | yylloc.last_line++; | |
51dec47b | 146 | yylloc.last_column = 1; |
bfb07874 AD |
147 | } |
148 | else | |
149 | yylloc.last_column++; | |
150 | #endif | |
151 | return res; | |
152 | } | |
153 | ||
154 | ||
155 | static void | |
156 | yyungetc (int c) | |
157 | { | |
19c50364 | 158 | #if YYLSP_NEEDED |
bfb07874 | 159 | /* Wrong when C == `\n'. */ |
b7c49edf | 160 | yylloc = last_yylloc; |
bfb07874 AD |
161 | #endif |
162 | ungetc (c, yyin); | |
163 | } | |
164 | ||
165 | static int | |
166 | read_signed_integer (void) | |
167 | { | |
168 | int c = yygetc (); | |
169 | int sign = 1; | |
170 | int n = 0; | |
171 | ||
172 | if (c == '-') | |
173 | { | |
174 | c = yygetc (); | |
175 | sign = -1; | |
176 | } | |
177 | ||
178 | while (isdigit (c)) | |
179 | { | |
180 | n = 10 * n + (c - '0'); | |
181 | c = yygetc (); | |
182 | } | |
183 | ||
184 | yyungetc (c); | |
185 | ||
186 | return sign * n; | |
187 | } | |
188 | ||
189 | ||
190 | ||
191 | /*---------------------------------------------------------------. | |
192 | | Lexical analyzer returns an integer on the stack and the token | | |
193 | | NUM, or the ASCII character read if not a number. Skips all | | |
194 | | blanks and tabs, returns 0 for EOF. | | |
195 | `---------------------------------------------------------------*/ | |
196 | ||
197 | static int | |
198 | yylex (void) | |
199 | { | |
200 | int c; | |
201 | ||
19c50364 | 202 | #if YYLSP_NEEDED |
bfb07874 AD |
203 | yylloc.first_column = yylloc.last_column; |
204 | yylloc.first_line = yylloc.last_line; | |
205 | #endif | |
206 | ||
207 | /* Skip white space. */ | |
208 | while ((c = yygetc ()) == ' ' || c == '\t') | |
209 | { | |
19c50364 | 210 | #if YYLSP_NEEDED |
bfb07874 AD |
211 | yylloc.first_column = yylloc.last_column; |
212 | yylloc.first_line = yylloc.last_line; | |
213 | #endif | |
214 | } | |
215 | ||
216 | /* process numbers */ | |
217 | if (c == '.' || isdigit (c)) | |
218 | { | |
219 | yyungetc (c); | |
213e640e | 220 | yylval.ival = read_signed_integer (); |
bfb07874 AD |
221 | return NUM; |
222 | } | |
223 | ||
224 | /* Return end-of-file. */ | |
225 | if (c == EOF) | |
6b7e85b9 | 226 | return CALC_EOF; |
bfb07874 AD |
227 | |
228 | /* Return single chars. */ | |
229 | return c; | |
230 | } | |
231 | ||
232 | static int | |
233 | power (int base, int exponent) | |
234 | { | |
235 | int res = 1; | |
236 | if (exponent < 0) | |
237 | exit (1); | |
238 | for (/* Niente */; exponent; --exponent) | |
239 | res *= base; | |
240 | return res; | |
241 | } | |
242 | ||
243 | int | |
342b8b6e | 244 | main (int argc, const char **argv) |
bfb07874 | 245 | { |
342b8b6e AD |
246 | yyin = NULL; |
247 | ||
248 | if (argc == 2) | |
bfb07874 AD |
249 | yyin = fopen (argv[1], "r"); |
250 | else | |
251 | yyin = stdin; | |
252 | ||
342b8b6e | 253 | if (!yyin) |
bfb07874 AD |
254 | { |
255 | perror (argv[1]); | |
256 | exit (1); | |
257 | } | |
258 | ||
259 | #if YYDEBUG | |
260 | yydebug = 1; | |
261 | #endif | |
19c50364 | 262 | #if YYLSP_NEEDED |
51dec47b | 263 | yylloc.last_column = 1; |
bfb07874 AD |
264 | yylloc.last_line = 1; |
265 | #endif | |
266 | yyparse (); | |
267 | return 0; | |
268 | } | |
269 | ]]) | |
270 | ])# _AT_DATA_CALC_Y | |
271 | ||
272 | ||
273 | # AT_DATA_CALC_Y([BISON-OPTIONS]) | |
274 | # ------------------------------- | |
275 | # Produce `calc.y'. | |
342b8b6e | 276 | m4_define([AT_DATA_CALC_Y], |
bfb07874 | 277 | [_AT_DATA_CALC_Y($[1], $[2], $[3], |
f50adbbd AD |
278 | [m4_bpatsubst([$1], [--[^ ]*])]) |
279 | ]) | |
bfb07874 AD |
280 | |
281 | ||
282 | ||
342b8b6e AD |
283 | # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0]) |
284 | # ------------------------------------------------------------ | |
bfb07874 | 285 | # Run `calc' on INPUT and expect no STDOUT nor STDERR. |
342b8b6e | 286 | # |
f50adbbd AD |
287 | # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then |
288 | # NUM-STDERR-LINES is the number of expected lines on stderr. | |
289 | # | |
290 | # We don't count GLR's traces yet, since its traces are somewhat | |
291 | # different from LALR's. | |
342b8b6e AD |
292 | m4_define([_AT_CHECK_CALC], |
293 | [AT_DATA([[input]], | |
294 | [[$2 | |
295 | ]]) | |
f50adbbd AD |
296 | AT_PARSER_CHECK([./calc input], 0, [], [stderr]) |
297 | m4_bmatch([$1], | |
298 | [%debug.*%glr\|%glr.*%debug], | |
299 | [], | |
300 | [%debug], | |
301 | [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3 | |
302 | ])]) | |
342b8b6e AD |
303 | ]) |
304 | ||
305 | ||
306 | # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, [NUM-DEBUG-LINES], | |
bfb07874 AD |
307 | # [ERROR-LOCATION], [IF-YYERROR-VERBOSE]) |
308 | # ------------------------------------------------------------ | |
342b8b6e AD |
309 | # Run `calc' on INPUT, and expect a `parse error' message. |
310 | # | |
b7c49edf AD |
311 | # If INPUT starts with a slash, it is used as absolute input file name, |
312 | # otherwise as contents. | |
313 | # | |
342b8b6e AD |
314 | # If BISON-OPTIONS contains `--location', then make sure the ERROR-LOCATION |
315 | # is correctly output on stderr. | |
316 | # | |
f50adbbd | 317 | # If BISON-OPTIONS contains `%error-verbose', then make sure the |
342b8b6e AD |
318 | # IF-YYERROR-VERBOSE message is properly output after `parse error, ' |
319 | # on STDERR. | |
320 | # | |
f50adbbd AD |
321 | # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES |
322 | # is the number of expected lines on stderr. | |
342b8b6e | 323 | m4_define([_AT_CHECK_CALC_ERROR], |
b7c49edf | 324 | [m4_bmatch([$2], [^/], |
1154cced | 325 | [AT_PARSER_CHECK([./calc $2], 0, [], [stderr])], |
b7c49edf | 326 | [AT_DATA([[input]], |
342b8b6e AD |
327 | [[$2 |
328 | ]]) | |
1154cced | 329 | AT_PARSER_CHECK([./calc input], 0, [], [stderr])]) |
f50adbbd AD |
330 | m4_bmatch([$1], |
331 | [%debug.*%glr\|%glr.*%debug], | |
332 | [], | |
333 | [%debug], | |
334 | [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3 | |
51dec47b | 335 | ])]) |
342b8b6e | 336 | |
51dec47b AD |
337 | # Normalize the observed and expected error messages, depending upon the |
338 | # options. | |
339 | # 1. Remove the traces from observed. | |
9280d3ef AD |
340 | sed '/^Starting/d |
341 | /^Entering/d | |
f50adbbd | 342 | /^Stack/d |
9280d3ef AD |
343 | /^Reading/d |
344 | /^Reducing/d | |
345 | /^Shifting/d | |
346 | /^state/d | |
347 | /^Error:/d | |
348 | /^Next/d | |
349 | /^Discarding/d | |
350 | /^yydestructor:/d' stderr >at-stderr | |
342b8b6e | 351 | mv at-stderr stderr |
51dec47b AD |
352 | # 2. Create the reference error message. |
353 | AT_DATA([[expout]], | |
354 | [$4 | |
342b8b6e | 355 | ]) |
51dec47b AD |
356 | # 3. If locations are not used, remove them. |
357 | m4_bmatch([$1], [--location], [], | |
358 | [[sed 's/^[-0-9.]*: //' expout >at-expout | |
359 | mv at-expout expout]]) | |
360 | # 4. If error-verbose is not used, strip the`, unexpected....' part. | |
f50adbbd | 361 | m4_bmatch([$1], [%error-verbose], [], |
51dec47b AD |
362 | [[sed 's/parse error, .*$/parse error/' expout >at-expout |
363 | mv at-expout expout]]) | |
364 | # 5. Check | |
365 | AT_CHECK([cat stderr], 0, [expout]) | |
342b8b6e | 366 | ]) |
bfb07874 AD |
367 | |
368 | ||
f50adbbd AD |
369 | # AT_CHECK_CALC([BISON-OPTIONS]) |
370 | # ------------------------------ | |
bfb07874 AD |
371 | # Start a testing chunk which compiles `calc' grammar with |
372 | # BISON-OPTIONS, and performs several tests over the parser. | |
342b8b6e | 373 | m4_define([AT_CHECK_CALC], |
bfb07874 AD |
374 | [# We use integers to avoid dependencies upon the precision of doubles. |
375 | AT_SETUP([Calculator $1]) | |
376 | ||
377 | AT_DATA_CALC_Y([$1]) | |
378 | ||
379 | # Specify the output files to avoid problems on different file systems. | |
b56471a6 | 380 | AT_CHECK([bison -o calc.c m4_bpatsubst([$1], [%[^ ]*]) calc.y], |
bfb07874 | 381 | [0], [], []) |
3c1a79b3 | 382 | |
1154cced | 383 | AT_COMPILE([calc]) |
bfb07874 AD |
384 | |
385 | # Test the priorities. | |
386 | _AT_CHECK_CALC([$1], | |
387 | [1 + 2 * 3 = 7 | |
388 | 1 + 2 * -3 = -5 | |
389 | ||
390 | -1^2 = -1 | |
391 | (-1)^2 = 1 | |
392 | ||
393 | ---1 = -1 | |
394 | ||
395 | 1 - 2 - 3 = -4 | |
396 | 1 - (2 - 3) = 2 | |
397 | ||
398 | 2^2^3 = 256 | |
3db472b9 | 399 | (2^2)^3 = 64], [486]) |
bfb07874 AD |
400 | |
401 | # Some parse errors. | |
366eea36 | 402 | _AT_CHECK_CALC_ERROR([$1], [0 0], [11], |
51dec47b | 403 | [1.3-1.4: parse error, unexpected "number"]) |
366eea36 | 404 | _AT_CHECK_CALC_ERROR([$1], [1//2], [15], |
51dec47b | 405 | [1.3-1.4: parse error, unexpected '/', expecting "number" or '-' or '(']) |
b7c49edf | 406 | _AT_CHECK_CALC_ERROR([$1], [error], [4], |
88bce5a2 | 407 | [1.1-1.2: parse error, unexpected $undefined, expecting "number" or '-' or '\n' or '(']) |
366eea36 | 408 | _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [22], |
51dec47b | 409 | [1.7-1.8: parse error, unexpected '=']) |
bfb07874 AD |
410 | _AT_CHECK_CALC_ERROR([$1], |
411 | [ | |
412 | +1], | |
366eea36 | 413 | [14], |
51dec47b | 414 | [2.1-2.2: parse error, unexpected '+']) |
b7c49edf | 415 | # Exercise error messages with EOF: work on an empty file. |
366eea36 | 416 | _AT_CHECK_CALC_ERROR([$1], [/dev/null], [4], |
51dec47b AD |
417 | [1.1-1.2: parse error, unexpected "end of file", expecting "number" or '-' or '\n' or '(']) |
418 | ||
419 | # Exercise the error token: without it, we die at the first error, | |
420 | # hence be sure i. to have several errors, ii. to test the action | |
421 | # associated to `error'. | |
366eea36 | 422 | _AT_CHECK_CALC_ERROR([$1], [(1 ++ 2) + (0 0) = 1], [82], |
51dec47b AD |
423 | [1.5-1.6: parse error, unexpected '+', expecting "number" or '-' or '(' |
424 | 1.15-1.16: parse error, unexpected "number" | |
425 | calc: error: 0 != 1]) | |
426 | ||
427 | # Add a studid example demonstrating that Bison can further improve the | |
428 | # error message. FIXME: Fix this ridiculous message. | |
366eea36 | 429 | _AT_CHECK_CALC_ERROR([$1], [()], [21], |
217598da | 430 | [1.2-1.3: parse error, unexpected ')', expecting "number" or '-' or '(']) |
bfb07874 | 431 | |
d803322e | 432 | AT_CLEANUP |
bfb07874 AD |
433 | ])# AT_CHECK_CALC |
434 | ||
435 | ||
436 | ||
437 | ||
f50adbbd AD |
438 | # ------------------------ # |
439 | # Simple LALR Calculator. # | |
440 | # ------------------------ # | |
441 | ||
442 | AT_BANNER([[Simple LALR Calculator.]]) | |
443 | ||
444 | # AT_CHECK_CALC_LALR([BISON-OPTIONS]) | |
445 | # ----------------------------------- | |
446 | # Start a testing chunk which compiles `calc' grammar with | |
447 | # BISON-OPTIONS, and performs several tests over the parser. | |
448 | m4_define([AT_CHECK_CALC_LALR], | |
449 | [AT_CHECK_CALC($@)]) | |
450 | ||
451 | AT_CHECK_CALC_LALR() | |
452 | ||
453 | AT_CHECK_CALC_LALR([--defines]) | |
454 | AT_CHECK_CALC_LALR([--locations]) | |
455 | AT_CHECK_CALC_LALR([--name-prefix=calc]) | |
456 | AT_CHECK_CALC_LALR([--verbose]) | |
457 | AT_CHECK_CALC_LALR([--yacc]) | |
458 | AT_CHECK_CALC_LALR([%error-verbose]) | |
459 | ||
460 | AT_CHECK_CALC_LALR([%error-verbose --locations]) | |
461 | ||
462 | AT_CHECK_CALC_LALR([%error-verbose --defines --locations --name-prefix=calc --verbose --yacc]) | |
463 | ||
464 | AT_CHECK_CALC_LALR([%debug]) | |
465 | AT_CHECK_CALC_LALR([%error-verbose %debug --defines --locations --name-prefix=calc --verbose --yacc]) | |
466 | ||
467 | ||
468 | # ----------------------- # | |
469 | # Simple GLR Calculator. # | |
470 | # ----------------------- # | |
471 | ||
472 | AT_BANNER([[Simple GLR Calculator.]]) | |
473 | ||
474 | # AT_CHECK_CALC_GLR([BISON-OPTIONS]) | |
475 | # ---------------------------------- | |
476 | # Start a testing chunk which compiles `calc' grammar with | |
477 | # BISON-OPTIONS and %glr-parser, and performs several tests over the parser. | |
478 | m4_define([AT_CHECK_CALC_GLR], | |
479 | [AT_CHECK_CALC([%glr_parser] $@)]) | |
480 | ||
bfb07874 | 481 | |
f50adbbd | 482 | AT_CHECK_CALC_GLR() |
bfb07874 | 483 | |
f50adbbd AD |
484 | AT_CHECK_CALC_GLR([--defines]) |
485 | AT_CHECK_CALC_GLR([--locations]) | |
486 | AT_CHECK_CALC_GLR([--name-prefix=calc]) | |
487 | AT_CHECK_CALC_GLR([--verbose]) | |
488 | AT_CHECK_CALC_GLR([--yacc]) | |
489 | AT_CHECK_CALC_GLR([%error-verbose]) | |
bfb07874 | 490 | |
f50adbbd | 491 | AT_CHECK_CALC_GLR([%error-verbose --locations]) |
bfb07874 | 492 | |
f50adbbd | 493 | AT_CHECK_CALC_GLR([%error-verbose --defines --locations --name-prefix=calc --verbose --yacc]) |
bfb07874 | 494 | |
f50adbbd AD |
495 | AT_CHECK_CALC_GLR([%debug]) |
496 | AT_CHECK_CALC_GLR([%error-verbose %debug --defines --locations --name-prefix=calc --verbose --yacc]) |