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