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