1 # Simple calculator. -*- Autotest -*-
3 # Copyright (C) 2000-2012 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 ## ---------------------------------------------------- ##
19 ## Compile the grammar described in the documentation. ##
20 ## ---------------------------------------------------- ##
23 # ------------------------- #
24 # Helping Autotest macros. #
25 # ------------------------- #
28 # _AT_DATA_CALC_Y($1, $2, $3, [BISON-DIRECTIVES])
29 # -----------------------------------------------
30 # Produce `calc.y' and, if %defines was specified, `calc-lex.c' or
33 # Don't call this macro directly, because it contains some occurrences
34 # of `$1' etc. which will be interpreted by m4. So you should call it
35 # with $1, $2, and $3 as arguments, which is what AT_DATA_CALC_Y does.
37 # When %defines is not passed, generate a single self-contained file.
38 # Otherwise, generate three: calc.y with the parser, calc-lex.c with
39 # the scanner, and calc-main.c with "main()". This is in order to
40 # stress the use of the generated parser header. To avoid code
41 # duplication, AT_CALC_LEX and AT_CALC_MAIN contain the body of these
43 m4_define([_AT_DATA_CALC_Y],
44 [m4_if([$1$2$3], $[1]$[2]$[3], [],
45 [m4_fatal([$0: Invalid arguments: $@])])dnl
47 m4_pushdef([AT_CALC_MAIN],
48 [#include <stdlib.h> /* abort */
53 # define alarm(seconds) /* empty */
57 /* A C++ ]AT_NAME_PREFIX[parse that simulates the C signature. */
59 ]AT_NAME_PREFIX[parse (]AT_PARAM_IF([semantic_value *result, int *count]))[
61 ]AT_NAME_PREFIX[::parser parser]AT_PARAM_IF([ (result, count)])[;
63 parser.set_debug_level (1);
65 return parser.parse ();
69 semantic_value global_result = 0;
72 /* A C main function. */
74 main (int argc, const char **argv)
76 semantic_value result = 0;
80 /* This used to be alarm (10), but that isn't enough time for
81 a July 1995 vintage DEC Alphastation 200 4/100 system,
82 according to Nelson H. F. Beebe. 100 seconds is enough. */
86 input = fopen (argv[1], "r");
96 ]AT_SKEL_CC_IF([], [m4_bmatch([$4], [%debug],
97 [ ]AT_NAME_PREFIX[debug = 1;])])[
98 status = ]AT_NAME_PREFIX[parse (]AT_PARAM_IF([[&result, &count]])[);
101 if (global_result != result)
103 if (global_count != count)
110 m4_pushdef([AT_CALC_LEX],
113 int ]AT_NAME_PREFIX[lex (]AT_LEX_FORMALS[);
114 static int get_char (]AT_LEX_FORMALS[);
115 static void unget_char (]AT_LEX_PRE_FORMALS[ int c);
118 static YYLTYPE last_yylloc;
121 get_char (]AT_LEX_FORMALS[)
123 int res = getc (input);
126 last_yylloc = AT_LOC;
130 AT_LOC_LAST_COLUMN = 1;
133 AT_LOC_LAST_COLUMN++;
139 unget_char (]AT_LEX_PRE_FORMALS[ int c)
143 /* Wrong when C == `\n'. */
144 AT_LOC = last_yylloc;
150 read_signed_integer (]AT_LEX_FORMALS[)
152 int c = get_char (]AT_LEX_ARGS[);
159 c = get_char (]AT_LEX_ARGS[);
165 n = 10 * n + (c - '0');
166 c = get_char (]AT_LEX_ARGS[);
169 unget_char (]AT_LEX_PRE_ARGS[ c);
175 /*---------------------------------------------------------------.
176 | Lexical analyzer returns an integer on the stack and the token |
177 | NUM, or the ASCII character read if not a number. Skips all |
178 | blanks and tabs, returns 0 for EOF. |
179 `---------------------------------------------------------------*/
182 ]AT_NAME_PREFIX[lex (]AT_LEX_FORMALS[)
191 AT_LOC_LAST_COLUMN = 1;
192 AT_LOC_LAST_LINE = 1;
196 /* Skip current token, then white spaces. */
200 [ AT_LOC_FIRST_COLUMN = AT_LOC_LAST_COLUMN;
201 AT_LOC_FIRST_LINE = AT_LOC_LAST_LINE;
204 while ((c = get_char (]AT_LEX_ARGS[)) == ' ' || c == '\t');
206 /* process numbers */
207 if (c == '.' || isdigit (c))
209 unget_char (]AT_LEX_PRE_ARGS[ c);
210 ]AT_VAL[.ival = read_signed_integer (]AT_LEX_ARGS[);
214 /* Return end-of-file. */
218 /* Return single chars. */
223 AT_DATA_GRAMMAR([calc.y],
224 [[/* Infix notation calculator--calc */
227 [%define global_tokens_and_yystype])[
230 ]AT_LOCATION_TYPE_IF([[
244 # define YYLLOC_DEFAULT(Current, Rhs, N) \
248 (Current).first = YYRHSLOC (Rhs, 1).first; \
249 (Current).last = YYRHSLOC (Rhs, N).last; \
253 (Current).first = (Current).last = YYRHSLOC (Rhs, 0).last; \
258 /* Exercise pre-prologue dependency to %union. */
259 typedef int semantic_value;
262 /* Exercise %union. */
273 extern semantic_value global_result;
274 extern int global_count;]AT_SKEL_CC_IF([[
276 # define YYLTYPE ]AT_NAME_PREFIX[::parser::location_type
288 static int power (int base, int exponent);
291 [/* yyerror receives the location if:
292 - %location & %pure & %glr
293 - %location & %pure & %yacc & %parse-param. */
294 static void yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ])
295 AT_PARAM_IF([semantic_value *result, int *count, ])
298 int yylex (]AT_LEX_FORMALS[);
301 ]AT_SKEL_CC_IF([AT_LOCATION_TYPE_IF([], [
302 /* The lalr1.cc skeleton, for backward compatibility, defines
303 a constructor for position that initializes the filename. The
304 glr.cc skeleton does not (and in fact cannot: location/position
305 are stored in a union, from which objects with constructors are
312 /* Bison Declarations */
313 %token CALC_EOF 0 "end of input"
314 %token <ival> NUM "number"
317 %nonassoc '=' /* comparison */
320 %left NEG /* negation--unary minus */
321 %right '^' /* exponentiation */
323 /* Grammar follows */
327 | input line { ]AT_PARAM_IF([++*count; ++global_count;])[ }
332 | exp '\n' { ]AT_PARAM_IF([*result = global_result = $1], [USE ($1)])[; }
340 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
343 | exp '+' exp { $$ = $1 + $3; }
344 | exp '-' exp { $$ = $1 - $3; }
345 | exp '*' exp { $$ = $1 * $3; }
346 | exp '/' exp { $$ = $1 / $3; }
347 | '-' exp %prec NEG { $$ = -$2; }
348 | exp '^' exp { $$ = power ($1, $3); }
349 | '(' exp ')' { $$ = $2; }
350 | '(' error ')' { $$ = 1111; yyerrok; }
351 | '!' { $$ = 0; YYERROR; }
352 | '-' error { $$ = 0; YYERROR; }
357 power (int base, int exponent)
360 assert (0 <= exponent);
361 for (/* Niente */; exponent; --exponent)
367 [AT_LOCATION_TYPE_IF([[
369 operator<< (std::ostream& o, const Span& s)
371 o << s.first.l << '.' << s.first.c;
372 if (s.first.l != s.last.l)
373 o << '-' << s.last.l << '.' << s.last.c - 1;
374 else if (s.first.c != s.last.c - 1)
375 o << '-' << s.last.c - 1;
380 /* A C++ error reporting function. */
382 AT_NAME_PREFIX::parser::error (const location_type& l, const std::string& m)
385 std::cerr << AT_LOCATION_IF([l << ": " << ])m << std::endl;
388 [/* A C error reporting function. */
390 yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ])
391 AT_PARAM_IF([semantic_value *result, int *count, ])
394 AT_PARAM_IF([(void) result; (void) count;])
395 AT_YYERROR_SEES_LOC_IF([
396 fprintf (stderr, "%d.%d",
397 AT_LOC_FIRST_LINE, AT_LOC_FIRST_COLUMN);
398 if (AT_LOC_FIRST_LINE != AT_LOC_LAST_LINE)
399 fprintf (stderr, "-%d.%d",
400 AT_LOC_LAST_LINE, AT_LOC_LAST_COLUMN - 1);
401 else if (AT_LOC_FIRST_COLUMN != AT_LOC_LAST_COLUMN - 1)
402 fprintf (stderr, "-%d",
403 AT_LOC_LAST_COLUMN - 1);
404 fprintf (stderr, ": ");])
405 fprintf (stderr, "%s\n", s);
413 AT_DEFINES_IF([AT_DATA_SOURCE([[calc-lex.c]AT_SKEL_CC_IF([[c]])],
414 [[#include "calc.h]AT_SKEL_CC_IF([[h]])["
417 AT_DATA_SOURCE([[calc-main.c]AT_SKEL_CC_IF([[c]])],
418 [[#include "calc.h]AT_SKEL_CC_IF([[h]])["
422 m4_popdef([AT_CALC_MAIN])
423 m4_popdef([AT_CALC_LEX])
427 # AT_DATA_CALC_Y([BISON-OPTIONS])
428 # -------------------------------
429 # Produce `calc.y' and, if %defines was specified, `calc-lex.c' or
431 m4_define([AT_DATA_CALC_Y],
432 [_AT_DATA_CALC_Y($[1], $[2], $[3], [$1])
437 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES])
438 # --------------------------------------------------------
439 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
441 # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
443 # NUM-STDERR-LINES is the number of expected lines on stderr.
444 # Currently this is ignored, though, since the output format is fluctuating.
446 # We don't count GLR's traces yet, since its traces are somewhat
447 # different from LALR's.
448 m4_define([_AT_CHECK_CALC],
452 AT_PARSER_CHECK([./calc input], 0, [], [stderr])
456 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT,
457 # [NUM-STDERR-LINES],
458 # [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
459 # ---------------------------------------------------------
460 # Run `calc' on INPUT, and expect a `syntax error' message.
462 # If INPUT starts with a slash, it is used as absolute input file name,
463 # otherwise as contents.
465 # NUM-STDERR-LINES is the number of expected lines on stderr.
466 # Currently this is ignored, though, since the output format is fluctuating.
468 # If BISON-OPTIONS contains `%location', then make sure the ERROR-LOCATION
469 # is correctly output on stderr.
471 # If BISON-OPTIONS contains `%error-verbose', then make sure the
472 # IF-YYERROR-VERBOSE message is properly output after `syntax error, '
475 # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
476 # is the number of expected lines on stderr.
477 m4_define([_AT_CHECK_CALC_ERROR],
478 [m4_bmatch([$3], [^/],
479 [AT_PARSER_CHECK([./calc $3], $2, [], [stderr])],
483 AT_PARSER_CHECK([./calc input], $2, [], [stderr])])
485 # Normalize the observed and expected error messages, depending upon the
487 # 1. Remove the traces from observed.
502 /^yydestructor:/d' stderr >at-stderr
504 # 2. Create the reference error message.
508 # 3. If locations are not used, remove them.
509 AT_YYERROR_SEES_LOC_IF([],
510 [[sed 's/^[-0-9.]*: //' expout >at-expout
511 mv at-expout expout]])
512 # 4. If error-verbose is not used, strip the`, unexpected....' part.
513 m4_bmatch([$1], [%error-verbose], [],
514 [[sed 's/syntax error, .*$/syntax error/' expout >at-expout
515 mv at-expout expout]])
517 AT_CHECK([cat stderr], 0, [expout])
521 # AT_CHECK_CALC([BISON-OPTIONS])
522 # ------------------------------
523 # Start a testing chunk which compiles `calc' grammar with
524 # BISON-OPTIONS, and performs several tests over the parser.
525 m4_define([AT_CHECK_CALC],
526 [m4_ifval([$2], [m4_fatal([$0: expected a single argument])])
528 # We use integers to avoid dependencies upon the precision of doubles.
529 AT_SETUP([Calculator $1])
531 AT_BISON_OPTION_PUSHDEFS([$1])
534 AT_FULL_COMPILE([calc], AT_DEFINES_IF([[lex], [main]]))
536 # Test the priorities.
553 # Some syntax errors.
554 _AT_CHECK_CALC_ERROR([$1], [1], [0 0], [15],
555 [1.3: syntax error, unexpected number])
556 _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [20],
557 [1.3: syntax error, unexpected '/', expecting number or '-' or '(' or '!'])
558 _AT_CHECK_CALC_ERROR([$1], [1], [error], [5],
559 [1.1: syntax error, unexpected $undefined])
560 _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [30],
561 [1.7: syntax error, unexpected '='])
562 _AT_CHECK_CALC_ERROR([$1], [1],
566 [2.1: syntax error, unexpected '+'])
567 # Exercise error messages with EOF: work on an empty file.
568 _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4],
569 [1.1: syntax error, unexpected end of input])
571 # Exercise the error token: without it, we die at the first error,
574 # - have several errors which exercise different shift/discardings
575 # - (): nothing to pop, nothing to discard
576 # - (1 + 1 + 1 +): a lot to pop, nothing to discard
577 # - (* * *): nothing to pop, a lot to discard
578 # - (1 + 2 * *): some to pop and discard
580 # - test the action associated to `error'
582 # - check the lookahead that triggers an error is not discarded
583 # when we enter error recovery. Below, the lookahead causing the
584 # first error is ")", which is needed to recover from the error and
585 # produce the "0" that triggers the "0 != 1" error.
587 _AT_CHECK_CALC_ERROR([$1], [0],
588 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
590 [1.2: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
591 1.18: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
592 1.23: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
593 1.41: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
594 calc: error: 4444 != 1])
596 # The same, but this time exercising explicitly triggered syntax errors.
597 # POSIX says the lookahead causing the error should not be discarded.
598 _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [102],
599 [1.10: syntax error, unexpected number
600 calc: error: 2222 != 1])
601 _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (0 0) = 1], [113],
602 [1.4: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
603 1.12: syntax error, unexpected number
604 calc: error: 2222 != 1])
606 # Check that yyerrok works properly: second error is not reported,
607 # third and fourth are. Parse status is succesfull.
608 _AT_CHECK_CALC_ERROR([$1], [0], [(* *) + (*) + (*)], [113],
609 [1.2: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
610 1.10: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
611 1.16: syntax error, unexpected '*', expecting number or '-' or '(' or '!'])
613 AT_BISON_OPTION_POPDEFS
621 # ------------------------ #
622 # Simple LALR Calculator. #
623 # ------------------------ #
625 AT_BANNER([[Simple LALR(1) Calculator.]])
627 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
628 # -----------------------------------
629 # Start a testing chunk which compiles `calc' grammar with
630 # BISON-OPTIONS, and performs several tests over the parser.
631 m4_define([AT_CHECK_CALC_LALR],
636 AT_CHECK_CALC_LALR([%defines])
637 AT_CHECK_CALC_LALR([%locations])
639 AT_CHECK_CALC_LALR([%name-prefix="calc"]) dnl test deprecated `='
640 AT_CHECK_CALC_LALR([%verbose])
641 AT_CHECK_CALC_LALR([%yacc])
642 AT_CHECK_CALC_LALR([%error-verbose])
644 AT_CHECK_CALC_LALR([%define api.pure %locations])
645 AT_CHECK_CALC_LALR([%define api.push-pull both %define api.pure %locations])
646 AT_CHECK_CALC_LALR([%error-verbose %locations])
648 AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix "calc" %verbose %yacc])
650 AT_CHECK_CALC_LALR([%debug])
651 AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc])
653 AT_CHECK_CALC_LALR([%define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc])
654 AT_CHECK_CALC_LALR([%define api.push-pull both %define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc])
656 AT_CHECK_CALC_LALR([%define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
659 # ----------------------- #
660 # Simple GLR Calculator. #
661 # ----------------------- #
663 AT_BANNER([[Simple GLR Calculator.]])
665 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
666 # ----------------------------------
667 # Start a testing chunk which compiles `calc' grammar with
668 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
669 m4_define([AT_CHECK_CALC_GLR],
670 [AT_CHECK_CALC([%glr-parser] $@)])
675 AT_CHECK_CALC_GLR([%defines])
676 AT_CHECK_CALC_GLR([%locations])
677 AT_CHECK_CALC_GLR([%name-prefix "calc"])
678 AT_CHECK_CALC_GLR([%verbose])
679 AT_CHECK_CALC_GLR([%yacc])
680 AT_CHECK_CALC_GLR([%error-verbose])
682 AT_CHECK_CALC_GLR([%define api.pure %locations])
683 AT_CHECK_CALC_GLR([%error-verbose %locations])
685 AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix "calc" %verbose %yacc])
687 AT_CHECK_CALC_GLR([%debug])
688 AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc])
690 AT_CHECK_CALC_GLR([%define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc])
692 AT_CHECK_CALC_GLR([%define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
695 # ----------------------------- #
696 # Simple LALR1 C++ Calculator. #
697 # ----------------------------- #
699 AT_BANNER([[Simple LALR(1) C++ Calculator.]])
701 # First let's try using %skeleton
702 AT_CHECK_CALC([%skeleton "lalr1.cc" %defines %locations])
704 # AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS])
705 # ---------------------------------------
706 # Start a testing chunk which compiles `calc' grammar with
707 # the C++ skeleton, and performs several tests over the parser.
708 m4_define([AT_CHECK_CALC_LALR1_CC],
709 [AT_CHECK_CALC([%language "C++" %defines %locations] $@)])
711 AT_CHECK_CALC_LALR1_CC([])
712 AT_CHECK_CALC_LALR1_CC([%define location_type Span])
713 AT_CHECK_CALC_LALR1_CC([%error-verbose %name-prefix "calc" %verbose %yacc])
714 AT_CHECK_CALC_LALR1_CC([%error-verbose %debug %name-prefix "calc" %verbose %yacc])
716 AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %name-prefix "calc" %verbose %yacc])
718 AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
722 # --------------------------- #
723 # Simple GLR C++ Calculator. #
724 # --------------------------- #
726 AT_BANNER([[Simple GLR C++ Calculator.]])
728 # Again, we try also using %skeleton.
729 AT_CHECK_CALC([%skeleton "glr.cc" %defines %locations])
731 # AT_CHECK_CALC_GLR_CC([BISON-OPTIONS])
732 # -------------------------------------
733 # Start a testing chunk which compiles `calc' grammar with
734 # the GLR C++ skeleton, and performs several tests over the parser.
735 m4_define([AT_CHECK_CALC_GLR_CC],
736 [AT_CHECK_CALC([%language "C++" %glr-parser %defines %locations] $@)])
738 AT_CHECK_CALC_GLR_CC([])
739 AT_CHECK_CALC_GLR_CC([%define location_type Span])
740 AT_CHECK_CALC_GLR_CC([%error-verbose %name-prefix "calc" %verbose %yacc])
742 AT_CHECK_CALC_GLR_CC([%debug])
743 AT_CHECK_CALC_GLR_CC([%error-verbose %debug %name-prefix "calc" %verbose %yacc])
745 AT_CHECK_CALC_GLR_CC([%pure-parser %error-verbose %debug %name-prefix "calc" %verbose %yacc])
747 AT_CHECK_CALC_GLR_CC([%pure-parser %error-verbose %debug %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])