]> git.saurik.com Git - bison.git/blob - tests/calc.at
Add Alexandre Duret-Lutz, Baron Schwartz, Frank Heckenbach, Guido
[bison.git] / tests / calc.at
1 # Simple calculator. -*- Autotest -*-
2 # Copyright (C) 2000, 2001, 2002, 2003, 2004 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_GRAMMAR([calc.y],
39 [[/* Infix notation calculator--calc */
40 ]$4[
41 %{
42 #include <stdio.h>
43
44 #include <stdlib.h>
45 #include <string.h>
46 #if HAVE_UNISTD_H
47 # include <unistd.h>
48 #else
49 # undef alarm
50 # define alarm(seconds) /* empty */
51 #endif
52 #include <ctype.h>
53
54 /* Exercise pre-prologue dependency to %union. */
55 typedef int semantic_value;
56
57 static semantic_value global_result = 0;
58 static int global_count = 0;
59 %}
60
61 /* Exercise %union. */
62 %union
63 {
64 semantic_value ival;
65 };
66
67 %{
68 static int power (int base, int exponent);
69 ]AT_LALR1_CC_IF([typedef yy::Location YYLTYPE;],
70 [/* yyerror receives the location if:
71 - %location & %pure & %glr
72 - %location & %pure & %yacc & %parse-param. */
73 static void yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])
74 AT_PARAM_IF([semantic_value *result, int *count, ])
75 const char *s
76 );])[
77 static int yylex (]AT_LEX_FORMALS[);
78 static int yygetc (]AT_LEX_FORMALS[);
79 static void yyungetc (]AT_LEX_PRE_FORMALS[ int c);
80 %}
81
82 /* Bison Declarations */
83 %token CALC_EOF 0 "end of input"
84 %token <ival> NUM "number"
85 %type <ival> exp
86
87 %nonassoc '=' /* comparison */
88 %left '-' '+'
89 %left '*' '/'
90 %left NEG /* negation--unary minus */
91 %right '^' /* exponentiation */
92
93 /* Grammar follows */
94 %%
95 input:
96 line
97 | input line { ]AT_PARAM_IF([++*count; ++global_count;])[ }
98 ;
99
100 line:
101 '\n'
102 | exp '\n' { ]AT_PARAM_IF([*result = global_result = $1;])[ }
103 ;
104
105 exp:
106 NUM { $$ = $1; }
107 | exp '=' exp
108 {
109 if ($1 != $3)
110 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
111 $$ = $1;
112 }
113 | exp '+' exp { $$ = $1 + $3; }
114 | exp '-' exp { $$ = $1 - $3; }
115 | exp '*' exp { $$ = $1 * $3; }
116 | exp '/' exp { $$ = $1 / $3; }
117 | '-' exp %prec NEG { $$ = -$2; }
118 | exp '^' exp { $$ = power ($1, $3); }
119 | '(' exp ')' { $$ = $2; }
120 | '(' error ')' { $$ = 1111; }
121 | '!' { YYERROR; }
122 | '-' error { YYERROR; }
123 ;
124 %%
125 /* The input. */
126 static FILE *yyin;
127
128 ]AT_LALR1_CC_IF(
129 [/* A C++ error reporting function. */
130 void
131 yy::Parser::error_ ()
132 {
133 std::cerr << AT_LOCATION_IF([location << ": " << ])message << std::endl;
134 }
135
136 int
137 yyparse (AT_PARAM_IF([semantic_value *result, int *count]))
138 {
139 yy::Parser parser (!!YYDEBUG[]AT_PARAM_IF([, result, count]));
140 return parser.parse ();
141 }
142 ],
143 [static void
144 yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])
145 AT_PARAM_IF([semantic_value *result, int *count, ])
146 const char *s)
147 {
148 AT_PARAM_IF([(void) result; (void) count;])
149 AT_YYERROR_SEES_LOC_IF([
150 fprintf (stderr, "%d.%d",
151 AT_LOC.first_line, AT_LOC.first_column);
152 if (AT_LOC.first_line != AT_LOC.last_line)
153 fprintf (stderr, "-%d.%d",
154 AT_LOC.last_line, AT_LOC.last_column - 1);
155 else if (AT_LOC.first_column != AT_LOC.last_column - 1)
156 fprintf (stderr, "-%d",
157 AT_LOC.last_column - 1);
158 fprintf (stderr, ": ");])
159 fprintf (stderr, "%s\n", s);
160 }])[
161
162
163 ]AT_LOCATION_IF([
164 static YYLTYPE last_yylloc;
165 ])[
166 static int
167 yygetc (]AT_LEX_FORMALS[)
168 {
169 int res = getc (yyin);
170 ]AT_USE_LEX_ARGS[;
171 ]AT_LOCATION_IF([
172 last_yylloc = AT_LOC;
173 if (res == '\n')
174 {
175 AT_LALR1_CC_IF(
176 [ AT_LOC.end.line++;
177 AT_LOC.end.column = 0;],
178 [ AT_LOC.last_line++;
179 AT_LOC.last_column = 0;])
180 }
181 else
182 AT_LALR1_CC_IF(
183 [ AT_LOC.end.column++;],
184 [ AT_LOC.last_column++;])
185 ])[
186 return res;
187 }
188
189
190 static void
191 yyungetc (]AT_LEX_PRE_FORMALS[ int c)
192 {
193 ]AT_USE_LEX_ARGS[;
194 ]AT_LOCATION_IF([
195 /* Wrong when C == `\n'. */
196 AT_LOC = last_yylloc;
197 ])[
198 ungetc (c, yyin);
199 }
200
201 static int
202 read_signed_integer (]AT_LEX_FORMALS[)
203 {
204 int c = yygetc (]AT_LEX_ARGS[);
205 int sign = 1;
206 int n = 0;
207
208 ]AT_USE_LEX_ARGS[;
209 if (c == '-')
210 {
211 c = yygetc (]AT_LEX_ARGS[);
212 sign = -1;
213 }
214
215 while (isdigit (c))
216 {
217 n = 10 * n + (c - '0');
218 c = yygetc (]AT_LEX_ARGS[);
219 }
220
221 yyungetc (]AT_LEX_PRE_ARGS[ c);
222
223 return sign * n;
224 }
225
226
227
228 /*---------------------------------------------------------------.
229 | Lexical analyzer returns an integer on the stack and the token |
230 | NUM, or the ASCII character read if not a number. Skips all |
231 | blanks and tabs, returns 0 for EOF. |
232 `---------------------------------------------------------------*/
233
234 static int
235 yylex (]AT_LEX_FORMALS[)
236 {
237 static int init = 1;
238 int c;
239
240 if (init)
241 {
242 init = 0;
243 ]AT_LALR1_CC_IF([],
244 [AT_LOCATION_IF([
245 AT_LOC.last_column = 0;
246 AT_LOC.last_line = 1;
247 ])])[
248 }
249
250 ]AT_LOCATION_IF([AT_LALR1_CC_IF(
251 [ AT_LOC.begin = AT_LOC.end;],
252 [ AT_LOC.first_column = AT_LOC.last_column;
253 AT_LOC.first_line = AT_LOC.last_line;
254 ])])[
255
256 /* Skip white space. */
257 while ((c = yygetc (]AT_LEX_ARGS[)) == ' ' || c == '\t')
258 {
259 ]AT_LOCATION_IF([AT_LALR1_CC_IF(
260 [ AT_LOC.begin = AT_LOC.end;],
261 [ AT_LOC.first_column = AT_LOC.last_column;
262 AT_LOC.first_line = AT_LOC.last_line;
263 ])])[
264 }
265
266 /* process numbers */
267 if (c == '.' || isdigit (c))
268 {
269 yyungetc (]AT_LEX_PRE_ARGS[ c);
270 ]AT_VAL[.ival = read_signed_integer (]AT_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
294 int
295 main (int argc, const char **argv)
296 {
297 semantic_value result = 0;
298 int count = 0;
299 int status;
300
301 alarm (10);
302 if (argc == 2)
303 yyin = fopen (argv[1], "r");
304 else
305 yyin = stdin;
306
307 if (!yyin)
308 {
309 perror (argv[1]);
310 exit (1);
311 }
312
313 ]AT_LALR1_CC_IF([], [m4_bmatch([$4], [%debug],
314 [ yydebug = 1;])])[
315 status = yyparse (]AT_PARAM_IF([&result, &count])[);
316 if (global_result != result)
317 abort ();
318 if (global_count != count)
319 abort ();
320 return status;
321 }
322 ]])
323 ])# _AT_DATA_CALC_Y
324
325
326 # AT_DATA_CALC_Y([BISON-OPTIONS])
327 # -------------------------------
328 # Produce `calc.y'.
329 m4_define([AT_DATA_CALC_Y],
330 [_AT_DATA_CALC_Y($[1], $[2], $[3], [$1])
331 ])
332
333
334
335 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
336 # ------------------------------------------------------------
337 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
338 #
339 # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
340 # NUM-STDERR-LINES is the number of expected lines on stderr.
341 #
342 # We don't count GLR's traces yet, since its traces are somewhat
343 # different from LALR's.
344 m4_define([_AT_CHECK_CALC],
345 [AT_DATA([[input]],
346 [[$2
347 ]])
348 AT_PARSER_CHECK([./calc input], 0, [], [stderr])
349 m4_bmatch([$1],
350 [%debug.*%glr\|%glr.*%debug],
351 [],
352 [%debug],
353 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
354 ])])
355 ])
356
357
358 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT,
359 # [NUM-DEBUG-LINES],
360 # [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
361 # ---------------------------------------------------------
362 # Run `calc' on INPUT, and expect a `syntax error' message.
363 #
364 # If INPUT starts with a slash, it is used as absolute input file name,
365 # otherwise as contents.
366 #
367 # If BISON-OPTIONS contains `%location', then make sure the ERROR-LOCATION
368 # is correctly output on stderr.
369 #
370 # If BISON-OPTIONS contains `%error-verbose', then make sure the
371 # IF-YYERROR-VERBOSE message is properly output after `syntax error, '
372 # on STDERR.
373 #
374 # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
375 # is the number of expected lines on stderr.
376 m4_define([_AT_CHECK_CALC_ERROR],
377 [m4_bmatch([$3], [^/],
378 [AT_PARSER_CHECK([./calc $3], $2, [], [stderr])],
379 [AT_DATA([[input]],
380 [[$3
381 ]])
382 AT_PARSER_CHECK([./calc input], $2, [], [stderr])])
383 m4_bmatch([$1],
384 [%debug.*%glr\|%glr.*%debug],
385 [],
386 [%debug],
387 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$4
388 ])])
389
390 # Normalize the observed and expected error messages, depending upon the
391 # options.
392 # 1. Remove the traces from observed.
393 sed '/^Starting/d
394 /^Entering/d
395 /^Stack/d
396 /^Reading/d
397 /^Reducing/d
398 /^Shifting/d
399 /^state/d
400 /^Error:/d
401 /^Next/d
402 /^Discarding/d
403 /^yydestructor:/d' stderr >at-stderr
404 mv at-stderr stderr
405 # 2. Create the reference error message.
406 AT_DATA([[expout]],
407 [$5
408 ])
409 # 3. If locations are not used, remove them.
410 AT_YYERROR_SEES_LOC_IF([],
411 [[sed 's/^[-0-9.]*: //' expout >at-expout
412 mv at-expout expout]])
413 # 4. If error-verbose is not used, strip the`, unexpected....' part.
414 m4_bmatch([$1], [%error-verbose], [],
415 [[sed 's/syntax error, .*$/syntax error/' expout >at-expout
416 mv at-expout expout]])
417 # 5. Check
418 AT_CHECK([cat stderr], 0, [expout])
419 ])
420
421
422 # AT_CHECK_CALC([BISON-OPTIONS [, EXPECTED-TO-FAIL]])
423 # ------------------------------
424 # Start a testing chunk which compiles `calc' grammar with
425 # BISON-OPTIONS, and performs several tests over the parser.
426 # However, if EXPECTED-TO-FAIL is nonempty, this test is expected to fail.
427 m4_define([AT_CHECK_CALC],
428 [# We use integers to avoid dependencies upon the precision of doubles.
429 AT_SETUP([Calculator $1])
430
431 m4_ifval([$2], [AT_CHECK([exit 77])])
432
433 AT_BISON_OPTION_PUSHDEFS([$1])
434
435 AT_DATA_CALC_Y([$1])
436
437 AT_LALR1_CC_IF(
438 [AT_CHECK([bison -o calc.cc calc.y])
439 AT_COMPILE_CXX([calc])],
440 [AT_CHECK([bison -o calc.c calc.y])
441 AT_COMPILE([calc])])
442
443 # Test the priorities.
444 _AT_CHECK_CALC([$1],
445 [1 + 2 * 3 = 7
446 1 + 2 * -3 = -5
447
448 -1^2 = -1
449 (-1)^2 = 1
450
451 ---1 = -1
452
453 1 - 2 - 3 = -4
454 1 - (2 - 3) = 2
455
456 2^2^3 = 256
457 (2^2)^3 = 64],
458 [570])
459
460 # Some syntax errors.
461 _AT_CHECK_CALC_ERROR([$1], [1], [0 0], [13],
462 [1.2: syntax error, unexpected "number"])
463 _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [18],
464 [1.2: syntax error, unexpected '/', expecting "number" or '-' or '(' or '!'])
465 _AT_CHECK_CALC_ERROR([$1], [1], [error], [5],
466 [1.0: syntax error, unexpected $undefined])
467 _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [26],
468 [1.6: syntax error, unexpected '='])
469 _AT_CHECK_CALC_ERROR([$1], [1],
470 [
471 +1],
472 [16],
473 [2.0: syntax error, unexpected '+'])
474 # Exercise error messages with EOF: work on an empty file.
475 _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [5],
476 [1.0: syntax error, unexpected "end of input"])
477
478 # Exercise the error token: without it, we die at the first error,
479 # hence be sure to
480 #
481 # - have several errors which exercise different shift/discardings
482 # - (): nothing to pop, nothing to discard
483 # - (1 + 1 + 1 +): a lot to pop, nothing to discard
484 # - (* * *): nothing to pop, a lot to discard
485 # - (1 + 2 * *): some to pop and discard
486 #
487 # - test the action associated to `error'
488 #
489 # - check the look-ahead that triggers an error is not discarded
490 # when we enter error recovery. Below, the look-ahead causing the
491 # first error is ")", which is needed to recover from the error and
492 # produce the "0" that triggers the "0 != 1" error.
493 #
494 _AT_CHECK_CALC_ERROR([$1], [0],
495 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
496 [188],
497 [1.1: syntax error, unexpected ')', expecting "number" or '-' or '(' or '!'
498 1.17: syntax error, unexpected ')', expecting "number" or '-' or '(' or '!'
499 1.22: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!'
500 1.40: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!'
501 calc: error: 4444 != 1])
502
503 # The same, but this time exercising explicitly triggered syntax errors.
504 # POSIX says the look-ahead causing the error should not be discarded.
505 _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [75],
506 [1.9: syntax error, unexpected "number"
507 calc: error: 2222 != 1])
508 _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (0 0) = 1], [85],
509 [1.3: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!'
510 1.11: syntax error, unexpected "number"
511 calc: error: 2222 != 1])
512 AT_BISON_OPTION_POPDEFS
513
514 AT_CLEANUP
515 ])# AT_CHECK_CALC
516
517
518
519
520 # ------------------------ #
521 # Simple LALR Calculator. #
522 # ------------------------ #
523
524 AT_BANNER([[Simple LALR Calculator.]])
525
526 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
527 # -----------------------------------
528 # Start a testing chunk which compiles `calc' grammar with
529 # BISON-OPTIONS, and performs several tests over the parser.
530 m4_define([AT_CHECK_CALC_LALR],
531 [AT_CHECK_CALC($@)])
532
533 AT_CHECK_CALC_LALR()
534
535 AT_CHECK_CALC_LALR([%defines])
536 AT_CHECK_CALC_LALR([%locations])
537 AT_CHECK_CALC_LALR([%name-prefix="calc"])
538 AT_CHECK_CALC_LALR([%verbose])
539 AT_CHECK_CALC_LALR([%yacc])
540 AT_CHECK_CALC_LALR([%error-verbose])
541
542 AT_CHECK_CALC_LALR([%pure-parser %locations])
543 AT_CHECK_CALC_LALR([%error-verbose %locations])
544
545 AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
546
547 AT_CHECK_CALC_LALR([%debug])
548 AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
549
550 AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
551
552 AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
553
554
555 # ----------------------- #
556 # Simple GLR Calculator. #
557 # ----------------------- #
558
559 AT_BANNER([[Simple GLR Calculator.]])
560
561 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
562 # ----------------------------------
563 # Start a testing chunk which compiles `calc' grammar with
564 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
565 m4_define([AT_CHECK_CALC_GLR],
566 [AT_CHECK_CALC([%glr-parser] $@)])
567
568
569 AT_CHECK_CALC_GLR()
570
571 AT_CHECK_CALC_GLR([%defines])
572 AT_CHECK_CALC_GLR([%locations])
573 AT_CHECK_CALC_GLR([%name-prefix="calc"])
574 AT_CHECK_CALC_GLR([%verbose])
575 AT_CHECK_CALC_GLR([%yacc])
576 AT_CHECK_CALC_GLR([%error-verbose])
577
578 AT_CHECK_CALC_GLR([%pure-parser %locations])
579 AT_CHECK_CALC_GLR([%error-verbose %locations])
580
581 AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
582
583 AT_CHECK_CALC_GLR([%debug])
584 AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
585
586 AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
587
588 AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
589
590
591 # ----------------------------- #
592 # Simple LALR1 C++ Calculator. #
593 # ----------------------------- #
594
595 AT_BANNER([[Simple LALR1 C++ Calculator.]])
596
597 # AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS])
598 # ---------------------------------------
599 # Start a testing chunk which compiles `calc' grammar with
600 # the C++ skeleton, and performs several tests over the parser.
601 m4_define([AT_CHECK_CALC_LALR1_CC],
602 [AT_CHECK_CALC([%skeleton "lalr1.cc"] $@)])
603
604 # AT_CHECK_CALC_LALR1_CC()
605
606 AT_CHECK_CALC_LALR1_CC([%defines %locations])
607
608 AT_CHECK_CALC_LALR1_CC([%defines])
609 # AT_CHECK_CALC_LALR1_CC([%locations])
610 # AT_CHECK_CALC_LALR1_CC([%name-prefix="calc"])
611 # AT_CHECK_CALC_LALR1_CC([%verbose])
612 # AT_CHECK_CALC_LALR1_CC([%yacc])
613 # AT_CHECK_CALC_LALR1_CC([%error-verbose])
614
615 # AT_CHECK_CALC_LALR1_CC([%pure-parser %locations])
616 # AT_CHECK_CALC_LALR1_CC([%error-verbose %locations])
617
618 AT_CHECK_CALC_LALR1_CC([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
619
620 # AT_CHECK_CALC_LALR1_CC([%debug])
621 AT_CHECK_CALC_LALR1_CC([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
622
623 AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
624
625 AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])