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