]> git.saurik.com Git - bison.git/blob - tests/calc.at
82674c10071fd25184c6f575230cbc66430e4555
[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_YYERROR_SEES_LOC_IF([
162 fprintf (stderr, "%d.%d-%d.%d: ",
163 LOC.first_line, LOC.first_column,
164 LOC.last_line, LOC.last_column);
165 ])[
166 fprintf (stderr, "%s\n", s);
167 }
168
169
170 ]AT_LOCATION_IF([
171 static YYLTYPE last_yylloc;
172 ])[
173 static int
174 yygetc (LEX_FORMALS)
175 {
176 int res = getc (yyin);
177 USE_LEX_ARGS;
178 ]AT_LOCATION_IF([
179 last_yylloc = LOC;
180 if (res == '\n')
181 {
182 LOC.last_line++;
183 LOC.last_column = 1;
184 }
185 else
186 LOC.last_column++;
187 ])[
188 return res;
189 }
190
191
192 static void
193 yyungetc (LEX_PRE_FORMALS int c)
194 {
195 USE_LEX_ARGS;
196 ]AT_LOCATION_IF([
197 /* Wrong when C == `\n'. */
198 LOC = last_yylloc;
199 ])[
200 ungetc (c, yyin);
201 }
202
203 static int
204 read_signed_integer (LEX_FORMALS)
205 {
206 int c = yygetc (LEX_ARGS);
207 int sign = 1;
208 int n = 0;
209
210 USE_LEX_ARGS;
211 if (c == '-')
212 {
213 c = yygetc (LEX_ARGS);
214 sign = -1;
215 }
216
217 while (isdigit (c))
218 {
219 n = 10 * n + (c - '0');
220 c = yygetc (LEX_ARGS);
221 }
222
223 yyungetc (LEX_PRE_ARGS c);
224
225 return sign * n;
226 }
227
228
229
230 /*---------------------------------------------------------------.
231 | Lexical analyzer returns an integer on the stack and the token |
232 | NUM, or the ASCII character read if not a number. Skips all |
233 | blanks and tabs, returns 0 for EOF. |
234 `---------------------------------------------------------------*/
235
236 static int
237 yylex (LEX_FORMALS)
238 {
239 static int init = 1;
240 int c;
241
242 if (init)
243 {
244 init = 0;
245 ]AT_LOCATION_IF([
246 LOC.last_column = 1;
247 LOC.last_line = 1;
248 ])[
249 }
250
251 ]AT_LOCATION_IF([
252 LOC.first_column = LOC.last_column;
253 LOC.first_line = LOC.last_line;
254 ])[
255
256 /* Skip white space. */
257 while ((c = yygetc (LEX_ARGS)) == ' ' || c == '\t')
258 {
259 ]AT_LOCATION_IF([
260 LOC.first_column = LOC.last_column;
261 LOC.first_line = LOC.last_line;
262 ])[
263 }
264
265 /* process numbers */
266 if (c == '.' || isdigit (c))
267 {
268 yyungetc (LEX_PRE_ARGS c);
269 VAL.ival = read_signed_integer (LEX_ARGS);
270 return NUM;
271 }
272
273 /* Return end-of-file. */
274 if (c == EOF)
275 return CALC_EOF;
276
277 /* Return single chars. */
278 return c;
279 }
280
281 static int
282 power (int base, int exponent)
283 {
284 int res = 1;
285 if (exponent < 0)
286 exit (1);
287 for (/* Niente */; exponent; --exponent)
288 res *= base;
289 return res;
290 }
291
292 int
293 main (int argc, const char **argv)
294 {
295 value_t result = 0;
296 int count = 0;
297 yyin = NULL;
298
299 if (argc == 2)
300 yyin = fopen (argv[1], "r");
301 else
302 yyin = stdin;
303
304 if (!yyin)
305 {
306 perror (argv[1]);
307 exit (1);
308 }
309
310 #if YYDEBUG
311 yydebug = 1;
312 #endif
313 yyparse (]AT_PARAM_IF([&result, &count])[);
314 assert (global_result == result);
315 assert (global_count == count);
316
317 return 0;
318 }
319 ]])
320 ])# _AT_DATA_CALC_Y
321
322
323 # AT_DATA_CALC_Y([BISON-OPTIONS])
324 # -------------------------------
325 # Produce `calc.y'.
326 m4_define([AT_DATA_CALC_Y],
327 [_AT_DATA_CALC_Y($[1], $[2], $[3], [$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 # [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
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 AT_YYERROR_SEES_LOC_IF([],
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_CALC_PUSHDEFS($1, $2, [BISON-OPTIONS])
419 # -----------------------------------------
420 # This macro works around the impossibility to define macros
421 # inside macros, because issuing `[$1]' is not possible in M4 :(.
422 # This sucks hard, GNU M4 should really provide M5 like $$1.
423 m4_define([AT_CHECK_PUSHDEFS],
424 [m4_if([$1$2], $[1]$[2], [],
425 [m4_fatal([$0: Invalid arguments: $@])])dnl
426 m4_pushdef([AT_PARAM_IF],
427 [m4_bmatch([$3], [%parse-param], [$1], [$2])])
428 m4_pushdef([AT_LOCATION_IF],
429 [m4_bmatch([$3], [%locations], [$1], [$2])])
430 m4_pushdef([AT_PURE_IF],
431 [m4_bmatch([$3], [%pure-parser], [$1], [$2])])
432 m4_pushdef([AT_GLR_IF],
433 [m4_bmatch([$3], [%glr-parser], [$1], [$2])])
434 m4_pushdef([AT_PURE_AND_LOC_IF],
435 [m4_bmatch([$3], [%locations.*%pure-parser\|%pure-parser.*%locations],
436 [$1], [$2])])
437 m4_pushdef([AT_GLR_OR_PARAM_IF],
438 [m4_bmatch([$3], [%glr-parser\|%parse-param], [$1], [$2])])
439
440 # yyerror receives the location if %location & %pure & (%glr or %parse-param).
441 m4_pushdef([AT_YYERROR_ARG_LOC_IF],
442 [AT_GLR_OR_PARAM_IF([AT_PURE_AND_LOC_IF([$1], [$2])],
443 [$2])])
444 # yyerror cannot see the locations if !glr & pure.
445 m4_pushdef([AT_YYERROR_SEES_LOC_IF],
446 [AT_LOCATION_IF([AT_GLR_IF([$1],
447 [AT_PURE_IF([$2], [$1])])],
448 [$2])])
449 ])
450
451
452 # AT_CALC_POPDEFS
453 # ---------------
454 m4_define([AT_CHECK_POPDEFS],
455 [m4_popdef([AT_YYERROR_SEES_LOC_IF])
456 m4_popdef([AT_YYERROR_ARG_LOC_IF])
457 m4_popdef([AT_GLR_OR_PARAM_IF])
458 m4_popdef([AT_PURE_AND_LOC_IF])
459 m4_popdef([AT_GLR_IF])
460 m4_popdef([AT_LOCATION_IF])
461 m4_popdef([AT_PARAM_IF])
462 ])
463
464
465
466 # AT_CHECK_CALC([BISON-OPTIONS])
467 # ------------------------------
468 # Start a testing chunk which compiles `calc' grammar with
469 # BISON-OPTIONS, and performs several tests over the parser.
470 m4_define([AT_CHECK_CALC],
471 [# We use integers to avoid dependencies upon the precision of doubles.
472 AT_SETUP([Calculator $1])
473
474 AT_CHECK_PUSHDEFS($[1], $[2], [$1])
475
476 AT_DATA_CALC_Y([$1])
477
478 # Specify the output files to avoid problems on different file systems.
479 AT_CHECK([bison -o calc.c calc.y],
480 [0], [], [])
481
482 AT_COMPILE([calc])
483
484 # Test the priorities.
485 _AT_CHECK_CALC([$1],
486 [1 + 2 * 3 = 7
487 1 + 2 * -3 = -5
488
489 -1^2 = -1
490 (-1)^2 = 1
491
492 ---1 = -1
493
494 1 - 2 - 3 = -4
495 1 - (2 - 3) = 2
496
497 2^2^3 = 256
498 (2^2)^3 = 64],
499 [486])
500
501 # Some parse errors.
502 _AT_CHECK_CALC_ERROR([$1], [0 0], [11],
503 [1.3-1.4: parse error, unexpected "number"])
504 _AT_CHECK_CALC_ERROR([$1], [1//2], [15],
505 [1.3-1.4: parse error, unexpected '/', expecting "number" or '-' or '('])
506 _AT_CHECK_CALC_ERROR([$1], [error], [4],
507 [1.1-1.2: parse error, unexpected $undefined, expecting "number" or '-' or '\n' or '('])
508 _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [22],
509 [1.7-1.8: parse error, unexpected '='])
510 _AT_CHECK_CALC_ERROR([$1],
511 [
512 +1],
513 [14],
514 [2.1-2.2: parse error, unexpected '+'])
515 # Exercise error messages with EOF: work on an empty file.
516 _AT_CHECK_CALC_ERROR([$1], [/dev/null], [4],
517 [1.1-1.2: parse error, unexpected "end of input", expecting "number" or '-' or '\n' or '('])
518
519 # Exercise the error token: without it, we die at the first error,
520 # hence be sure i. to have several errors, ii. to test the action
521 # associated to `error'.
522 _AT_CHECK_CALC_ERROR([$1], [(1 ++ 2) + (0 0) = 1], [82],
523 [1.5-1.6: parse error, unexpected '+', expecting "number" or '-' or '('
524 1.15-1.16: parse error, unexpected "number"
525 calc: error: 0 != 1])
526
527 AT_CHECK_POPDEFS
528
529 AT_CLEANUP
530 ])# AT_CHECK_CALC
531
532
533
534
535 # ------------------------ #
536 # Simple LALR Calculator. #
537 # ------------------------ #
538
539 AT_BANNER([[Simple LALR Calculator.]])
540
541 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
542 # -----------------------------------
543 # Start a testing chunk which compiles `calc' grammar with
544 # BISON-OPTIONS, and performs several tests over the parser.
545 m4_define([AT_CHECK_CALC_LALR],
546 [AT_CHECK_CALC($@)])
547
548 AT_CHECK_CALC_LALR()
549
550 AT_CHECK_CALC_LALR([%defines])
551 AT_CHECK_CALC_LALR([%locations])
552 AT_CHECK_CALC_LALR([%name-prefix="calc"])
553 AT_CHECK_CALC_LALR([%verbose])
554 AT_CHECK_CALC_LALR([%yacc])
555 AT_CHECK_CALC_LALR([%error-verbose])
556
557 AT_CHECK_CALC_LALR([%error-verbose %locations])
558
559 AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
560
561 AT_CHECK_CALC_LALR([%debug])
562 AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
563
564 AT_CHECK_CALC_LALR([%pure-parser %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 %parse-param "value_t *result", "result" %parse-param "int *count", "count"])
567
568
569 # ----------------------- #
570 # Simple GLR Calculator. #
571 # ----------------------- #
572
573 AT_BANNER([[Simple GLR Calculator.]])
574
575 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
576 # ----------------------------------
577 # Start a testing chunk which compiles `calc' grammar with
578 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
579 m4_define([AT_CHECK_CALC_GLR],
580 [AT_CHECK_CALC([%glr-parser] $@)])
581
582
583 AT_CHECK_CALC_GLR()
584
585 AT_CHECK_CALC_GLR([%defines])
586 AT_CHECK_CALC_GLR([%locations])
587 AT_CHECK_CALC_GLR([%name-prefix="calc"])
588 AT_CHECK_CALC_GLR([%verbose])
589 AT_CHECK_CALC_GLR([%yacc])
590 AT_CHECK_CALC_GLR([%error-verbose])
591
592 AT_CHECK_CALC_GLR([%error-verbose %locations])
593
594 AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
595
596 AT_CHECK_CALC_GLR([%debug])
597 AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
598
599 AT_CHECK_CALC_GLR([%pure-parser %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 %parse-param "value_t *result", "result" %parse-param "int *count", "count"])