1 # Simple calculator. -*- Autotest -*-
3 # Copyright (C) 2000-2013 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],
52 /* A C++ ]AT_NAME_PREFIX[parse that simulates the C signature. */
54 ]AT_NAME_PREFIX[parse (]AT_PARAM_IF([semantic_value *result, int *count]))[
56 ]AT_NAME_PREFIX[::parser parser]AT_PARAM_IF([ (result, count)])[;
57 #if ]AT_API_PREFIX[DEBUG
58 parser.set_debug_level (1);
60 return parser.parse ();
64 semantic_value global_result = 0;
67 /* A C main function. */
69 main (int argc, const char **argv)
71 semantic_value result = 0;
75 /* This used to be alarm (10), but that isn't enough time for a July
76 1995 vintage DEC Alphastation 200 4/100 system, according to
77 Nelson H. F. Beebe. 100 seconds was enough for regular users,
78 but the Hydra build farm, which is heavily loaded needs more. */
83 input = fopen (argv[1], "r");
93 ]AT_SKEL_CC_IF([], [AT_DEBUG_IF([ ]AT_NAME_PREFIX[debug = 1;])])[
94 status = ]AT_NAME_PREFIX[parse (]AT_PARAM_IF([[&result, &count]])[);
97 assert (global_result == result);
98 assert (global_count == count);
104 m4_pushdef([AT_CALC_LEX],
107 ]AT_YYLEX_DECLARE_EXTERN[
108 static int get_char (]AT_YYLEX_FORMALS[);
109 static void unget_char (]AT_YYLEX_PRE_FORMALS[ int c);
112 static AT_YYLTYPE last_yylloc;
115 get_char (]AT_YYLEX_FORMALS[)
117 int res = getc (input);
120 last_yylloc = AT_LOC;
124 AT_LOC_LAST_COLUMN = 1;
127 AT_LOC_LAST_COLUMN++;
133 unget_char (]AT_YYLEX_PRE_FORMALS[ int c)
137 /* Wrong when C == '\n'. */
138 AT_LOC = last_yylloc;
144 read_signed_integer (]AT_YYLEX_FORMALS[)
146 int c = get_char (]AT_YYLEX_ARGS[);
153 c = get_char (]AT_YYLEX_ARGS[);
159 n = 10 * n + (c - '0');
160 c = get_char (]AT_YYLEX_ARGS[);
163 unget_char (]AT_YYLEX_PRE_ARGS[ c);
169 /*---------------------------------------------------------------.
170 | Lexical analyzer returns an integer on the stack and the token |
171 | NUM, or the ASCII character read if not a number. Skips all |
172 | blanks and tabs, returns 0 for EOF. |
173 `---------------------------------------------------------------*/
178 /* Skip current token, then white spaces. */
182 [ AT_LOC_FIRST_COLUMN = AT_LOC_LAST_COLUMN;
183 AT_LOC_FIRST_LINE = AT_LOC_LAST_LINE;
186 while ((c = get_char (]AT_YYLEX_ARGS[)) == ' ' || c == '\t');
188 /* process numbers */
189 if (c == '.' || isdigit (c))
191 unget_char (]AT_YYLEX_PRE_ARGS[ c);
192 ]AT_VAL[.ival = read_signed_integer (]AT_YYLEX_ARGS[);
193 return ]AT_TOKEN_PREFIX[NUM;
196 /* Return end-of-file. */
198 return ]AT_TOKEN_PREFIX[CALC_EOF;
200 /* Return single chars. */
205 AT_DATA_GRAMMAR([calc.y],
206 [[/* Infix notation calculator--calc */
209 [%define global_tokens_and_yystype])[
212 ]AT_LOCATION_TYPE_IF([[
226 # define YYLLOC_DEFAULT(Current, Rhs, N) \
230 (Current).first = YYRHSLOC (Rhs, 1).first; \
231 (Current).last = YYRHSLOC (Rhs, N).last; \
235 (Current).first = (Current).last = YYRHSLOC (Rhs, 0).last; \
240 /* Exercise pre-prologue dependency to %union. */
241 typedef int semantic_value;
244 /* Exercise %union. */
249 %printer { ]AT_SKEL_CC_IF([[yyoutput << $$]],
250 [[fprintf (yyoutput, "%d", $$)]])[; } <ival>;
257 extern semantic_value global_result;
258 extern int global_count;
268 static int power (int base, int exponent);
271 ]AT_YYLEX_DECLARE_EXTERN[
274 ]AT_SKEL_CC_IF([AT_LOCATION_TYPE_IF([[
277 @$.first.l = @$.first.c = 1;
281 /* Bison Declarations */
282 %token CALC_EOF 0 "end of input"
283 %token <ival> NUM "number"
286 %nonassoc '=' /* comparison */
289 %precedence NEG /* negation--unary minus */
290 %right '^' /* exponentiation */
292 /* Grammar follows */
296 | input line { ]AT_PARAM_IF([++*count; ++global_count;])[ }
301 | exp '\n' { ]AT_PARAM_IF([*result = global_result = $1], [USE ($1)])[; }
309 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
312 | exp '+' exp { $$ = $1 + $3; }
313 | exp '-' exp { $$ = $1 - $3; }
314 | exp '*' exp { $$ = $1 * $3; }
315 | exp '/' exp { $$ = $1 / $3; }
316 | '-' exp %prec NEG { $$ = -$2; }
317 | exp '^' exp { $$ = power ($1, $3); }
318 | '(' exp ')' { $$ = $2; }
319 | '(' error ')' { $$ = 1111; yyerrok; }
320 | '!' { $$ = 0; YYERROR; }
321 | '-' error { $$ = 0; YYERROR; }
326 power (int base, int exponent)
329 assert (0 <= exponent);
330 for (/* Niente */; exponent; --exponent)
336 [AT_LOCATION_TYPE_IF([[
338 operator<< (std::ostream& o, const Span& s)
340 o << s.first.l << '.' << s.first.c;
341 if (s.first.l != s.last.l)
342 o << '-' << s.last.l << '.' << s.last.c - 1;
343 else if (s.first.c != s.last.c - 1)
344 o << '-' << s.last.c - 1;
353 AT_DEFINES_IF([AT_DATA_SOURCE([[calc-lex.c]AT_SKEL_CC_IF([[c]])],
354 [[#include "calc.h]AT_SKEL_CC_IF([[h]])["
357 AT_DATA_SOURCE([[calc-main.c]AT_SKEL_CC_IF([[c]])],
358 [[#include "calc.h]AT_SKEL_CC_IF([[h]])["
362 m4_popdef([AT_CALC_MAIN])
363 m4_popdef([AT_CALC_LEX])
367 # AT_DATA_CALC_Y([BISON-OPTIONS])
368 # -------------------------------
369 # Produce 'calc.y' and, if %defines was specified, 'calc-lex.c' or
371 m4_define([AT_DATA_CALC_Y],
372 [_AT_DATA_CALC_Y($[1], $[2], $[3], [$1])
377 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES])
378 # --------------------------------------------------------
379 # Run 'calc' on INPUT and expect no STDOUT nor STDERR.
381 # If BISON-OPTIONS contains '%debug' but not '%glr-parser', then
383 # NUM-STDERR-LINES is the number of expected lines on stderr.
384 # Currently this is ignored, though, since the output format is fluctuating.
386 # We don't count GLR's traces yet, since its traces are somewhat
387 # different from LALR's.
388 m4_define([_AT_CHECK_CALC],
392 AT_PARSER_CHECK([./calc input], 0, [], [stderr])
396 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT,
397 # [NUM-STDERR-LINES],
398 # [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
399 # ---------------------------------------------------------
400 # Run 'calc' on INPUT, and expect a 'syntax error' message.
402 # If INPUT starts with a slash, it is used as absolute input file name,
403 # otherwise as contents.
405 # NUM-STDERR-LINES is the number of expected lines on stderr.
406 # Currently this is ignored, though, since the output format is fluctuating.
408 # If BISON-OPTIONS contains '%location', then make sure the ERROR-LOCATION
409 # is correctly output on stderr.
411 # If BISON-OPTIONS contains '%define parse.error verbose', then make sure the
412 # IF-YYERROR-VERBOSE message is properly output after 'syntax error, '
415 # If BISON-OPTIONS contains '%debug' but not '%glr', then NUM-STDERR-LINES
416 # is the number of expected lines on stderr.
417 m4_define([_AT_CHECK_CALC_ERROR],
418 [m4_bmatch([$3], [^/],
419 [AT_PARSER_CHECK([./calc $3], $2, [], [stderr])],
423 AT_PARSER_CHECK([./calc input], $2, [], [stderr])])
425 # Normalize the observed and expected error messages, depending upon the
427 # 1. Remove the traces from observed.
442 /^yydestructor:/d' stderr >at-stderr
444 # 2. Create the reference error message.
448 # 3. If locations are not used, remove them.
449 AT_YYERROR_SEES_LOC_IF([],
450 [[sed 's/^[-0-9.]*: //' expout >at-expout
451 mv at-expout expout]])
452 # 4. If error-verbose is not used, strip the', unexpected....' part.
453 m4_bmatch([$1], [%define parse.error verbose], [],
454 [[sed 's/syntax error, .*$/syntax error/' expout >at-expout
455 mv at-expout expout]])
457 AT_CHECK([cat stderr], 0, [expout])
461 # AT_CHECK_SPACES([FILES])
462 # ------------------------
463 # Make sure we did not introduce bad spaces. Checked here because all
464 # the skeletons are (or should be) exercized here.
465 m4_define([AT_CHECK_SPACES],
466 [AT_CHECK([$PERL -ne '
468 print "$ARGV:$.: {$_}\n"
469 if (# No starting/ending empty lines.
470 (eof || $. == 1) && /^\s*$/
480 # AT_CHECK_CALC([BISON-OPTIONS])
481 # ------------------------------
482 # Start a testing chunk which compiles 'calc' grammar with
483 # BISON-OPTIONS, and performs several tests over the parser.
484 m4_define([AT_CHECK_CALC],
485 [m4_ifval([$2], [m4_fatal([$0: expected a single argument])])
487 # We use integers to avoid dependencies upon the precision of doubles.
488 AT_SETUP([Calculator $1])
490 AT_BISON_OPTION_PUSHDEFS([$1])
493 AT_FULL_COMPILE([calc], AT_DEFINES_IF([[lex], [main]]))
494 AT_CHECK_SPACES(m4_join([ ],
495 [calc.AT_SKEL_CC_IF([cc], [c])],
496 [AT_DEFINES_IF([calc.AT_SKEL_CC_IF([hh], [h])])]))
498 # Test the priorities.
515 # Some syntax errors.
516 _AT_CHECK_CALC_ERROR([$1], [1], [1 2], [15],
517 [1.3: syntax error, unexpected number])
518 _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [20],
519 [1.3: syntax error, unexpected '/', expecting number or '-' or '(' or '!'])
520 _AT_CHECK_CALC_ERROR([$1], [1], [error], [5],
521 [1.1: syntax error, unexpected $undefined])
522 _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [30],
523 [1.7: syntax error, unexpected '='])
524 _AT_CHECK_CALC_ERROR([$1], [1],
528 [2.1: syntax error, unexpected '+'])
529 # Exercise error messages with EOF: work on an empty file.
530 _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4],
531 [1.1: syntax error, unexpected end of input])
533 # Exercise the error token: without it, we die at the first error,
536 # - have several errors which exercise different shift/discardings
537 # - (): nothing to pop, nothing to discard
538 # - (1 + 1 + 1 +): a lot to pop, nothing to discard
539 # - (* * *): nothing to pop, a lot to discard
540 # - (1 + 2 * *): some to pop and discard
542 # - test the action associated to 'error'
544 # - check the lookahead that triggers an error is not discarded
545 # when we enter error recovery. Below, the lookahead causing the
546 # first error is ")", which is needed to recover from the error and
547 # produce the "0" that triggers the "0 != 1" error.
549 _AT_CHECK_CALC_ERROR([$1], [0],
550 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
552 [1.2: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
553 1.18: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
554 1.23: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
555 1.41: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
556 calc: error: 4444 != 1])
558 # The same, but this time exercising explicitly triggered syntax errors.
559 # POSIX says the lookahead causing the error should not be discarded.
560 _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (1 2) = 1], [102],
561 [1.10: syntax error, unexpected number
562 calc: error: 2222 != 1])
563 _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (1 2) = 1], [113],
564 [1.4: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
565 1.12: syntax error, unexpected number
566 calc: error: 2222 != 1])
568 # Check that yyerrok works properly: second error is not reported,
569 # third and fourth are. Parse status is succesfull.
570 _AT_CHECK_CALC_ERROR([$1], [0], [(* *) + (*) + (*)], [113],
571 [1.2: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
572 1.10: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
573 1.16: syntax error, unexpected '*', expecting number or '-' or '(' or '!'])
575 AT_BISON_OPTION_POPDEFS
583 # ------------------------ #
584 # Simple LALR Calculator. #
585 # ------------------------ #
587 AT_BANNER([[Simple LALR(1) Calculator.]])
589 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
590 # -----------------------------------
591 # Start a testing chunk which compiles 'calc' grammar with
592 # BISON-OPTIONS, and performs several tests over the parser.
593 m4_define([AT_CHECK_CALC_LALR],
598 AT_CHECK_CALC_LALR([%defines])
599 AT_CHECK_CALC_LALR([%locations])
601 AT_CHECK_CALC_LALR([%name-prefix "calc"])
602 AT_CHECK_CALC_LALR([%verbose])
603 AT_CHECK_CALC_LALR([%yacc])
604 AT_CHECK_CALC_LALR([%define parse.error verbose])
606 AT_CHECK_CALC_LALR([%define api.pure full %locations])
607 AT_CHECK_CALC_LALR([%define api.push-pull both %define api.pure full %locations])
608 AT_CHECK_CALC_LALR([%define parse.error verbose %locations])
610 AT_CHECK_CALC_LALR([%define parse.error verbose %locations %defines %define api.prefix "calc" %verbose %yacc])
611 AT_CHECK_CALC_LALR([%define parse.error verbose %locations %defines %name-prefix "calc" %define api.token.prefix {TOK_} %verbose %yacc])
613 AT_CHECK_CALC_LALR([%debug])
614 AT_CHECK_CALC_LALR([%define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc])
615 AT_CHECK_CALC_LALR([%define parse.error verbose %debug %locations %defines %define api.prefix "calc" %verbose %yacc])
617 AT_CHECK_CALC_LALR([%define api.pure full %define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc])
618 AT_CHECK_CALC_LALR([%define api.push-pull both %define api.pure full %define parse.error verbose %debug %locations %defines %define api.prefix "calc" %verbose %yacc])
620 AT_CHECK_CALC_LALR([%define api.pure %define parse.error verbose %debug %locations %defines %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
623 # ----------------------- #
624 # Simple GLR Calculator. #
625 # ----------------------- #
627 AT_BANNER([[Simple GLR Calculator.]])
629 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
630 # ----------------------------------
631 # Start a testing chunk which compiles 'calc' grammar with
632 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
633 m4_define([AT_CHECK_CALC_GLR],
634 [AT_CHECK_CALC([%glr-parser] $@)])
639 AT_CHECK_CALC_GLR([%defines])
640 AT_CHECK_CALC_GLR([%locations])
641 AT_CHECK_CALC_GLR([%name-prefix "calc"])
642 AT_CHECK_CALC_GLR([%define api.prefix "calc"])
643 AT_CHECK_CALC_GLR([%verbose])
644 AT_CHECK_CALC_GLR([%yacc])
645 AT_CHECK_CALC_GLR([%define parse.error verbose])
647 AT_CHECK_CALC_GLR([%define api.pure %locations])
648 AT_CHECK_CALC_GLR([%define parse.error verbose %locations])
650 AT_CHECK_CALC_GLR([%define parse.error verbose %locations %defines %name-prefix "calc" %verbose %yacc])
652 AT_CHECK_CALC_GLR([%debug])
653 AT_CHECK_CALC_GLR([%define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc])
654 AT_CHECK_CALC_GLR([%define parse.error verbose %debug %locations %defines %define api.prefix "calc" %define api.token.prefix {TOK_} %verbose %yacc])
656 AT_CHECK_CALC_GLR([%define api.pure %define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc])
658 AT_CHECK_CALC_GLR([%define api.pure %define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
659 AT_CHECK_CALC_GLR([%define api.pure %define parse.error verbose %debug %locations %defines %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
662 # ----------------------------- #
663 # Simple LALR1 C++ Calculator. #
664 # ----------------------------- #
666 AT_BANNER([[Simple LALR(1) C++ Calculator.]])
668 # First let's try using %skeleton
669 AT_CHECK_CALC([%skeleton "lalr1.cc" %defines])
671 # AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS])
672 # ---------------------------------------
673 # Start a testing chunk which compiles 'calc' grammar with
674 # the C++ skeleton, and performs several tests over the parser.
675 m4_define([AT_CHECK_CALC_LALR1_CC],
676 [AT_CHECK_CALC([%language "C++"] $@)])
678 AT_CHECK_CALC_LALR1_CC([])
679 AT_CHECK_CALC_LALR1_CC([%locations])
680 AT_CHECK_CALC_LALR1_CC([%locations %define api.location.type Span])
681 AT_CHECK_CALC_LALR1_CC([%defines %locations %define parse.error verbose %name-prefix "calc" %verbose %yacc])
683 AT_CHECK_CALC_LALR1_CC([%locations %define parse.error verbose %define api.prefix "calc" %verbose %yacc])
684 AT_CHECK_CALC_LALR1_CC([%locations %define parse.error verbose %debug %name-prefix "calc" %verbose %yacc])
686 AT_CHECK_CALC_LALR1_CC([%locations %define parse.error verbose %debug %define api.prefix "calc" %verbose %yacc])
687 AT_CHECK_CALC_LALR1_CC([%locations %define parse.error verbose %debug %define api.prefix "calc" %define api.token.prefix {TOK_} %verbose %yacc])
689 AT_CHECK_CALC_LALR1_CC([%defines %locations %define parse.error verbose %debug %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
691 AT_CHECK_CALC_LALR1_CC([%define parse.error verbose %debug %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
692 AT_CHECK_CALC_LALR1_CC([%defines %locations %define parse.error verbose %debug %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
696 # --------------------------- #
697 # Simple GLR C++ Calculator. #
698 # --------------------------- #
700 AT_BANNER([[Simple GLR C++ Calculator.]])
702 # Again, we try also using %skeleton.
703 AT_CHECK_CALC([%skeleton "glr.cc"])
705 # AT_CHECK_CALC_GLR_CC([BISON-OPTIONS])
706 # -------------------------------------
707 # Start a testing chunk which compiles 'calc' grammar with
708 # the GLR C++ skeleton, and performs several tests over the parser.
709 m4_define([AT_CHECK_CALC_GLR_CC],
710 [AT_CHECK_CALC([%language "C++" %glr-parser] $@)])
712 AT_CHECK_CALC_GLR_CC([])
713 AT_CHECK_CALC_GLR_CC([%locations])
714 AT_CHECK_CALC_GLR_CC([%locations %define api.location.type Span])
715 AT_CHECK_CALC_GLR_CC([%defines %define parse.error verbose %name-prefix "calc" %verbose %yacc])
716 AT_CHECK_CALC_GLR_CC([%define parse.error verbose %define api.prefix "calc" %verbose %yacc])
718 AT_CHECK_CALC_GLR_CC([%debug])
720 AT_CHECK_CALC_GLR_CC([%define parse.error verbose %debug %name-prefix "calc" %verbose %yacc])
721 AT_CHECK_CALC_GLR_CC([%define parse.error verbose %debug %name-prefix "calc" %define api.token.prefix {TOK_} %verbose %yacc])
723 AT_CHECK_CALC_GLR_CC([%locations %defines %define parse.error verbose %debug %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
724 AT_CHECK_CALC_GLR_CC([%locations %defines %define parse.error verbose %debug %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])