1 # Simple calculator. -*- Autotest -*-
3 # Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 ## ---------------------------------------------------- ##
20 ## Compile the grammar described in the documentation. ##
21 ## ---------------------------------------------------- ##
24 # ------------------------- #
25 # Helping Autotest macros. #
26 # ------------------------- #
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 */
42 [%define global_tokens_and_yystype])[
52 # define alarm(seconds) /* empty */
57 /* Exercise pre-prologue dependency to %union. */
58 typedef int semantic_value;
60 static semantic_value global_result = 0;
61 static int global_count = 0;
64 /* Exercise %union. */
71 static int power (int base, int exponent);
74 [#] define YYLTYPE AT_NAME_PREFIX::location
76 #define first_line begin.line
77 #define first_column begin.column
78 #define last_line end.line
79 #define last_column end.column
81 [/* yyerror receives the location if:
82 - %location & %pure & %glr
83 - %location & %pure & %yacc & %parse-param. */
84 static void yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ])
85 AT_PARAM_IF([semantic_value *result, int *count, ])
88 static int yylex (]AT_LEX_FORMALS[);
89 static int get_char (]AT_LEX_FORMALS[);
90 static void unget_char (]AT_LEX_PRE_FORMALS[ int c);
94 [/* The lalr1.cc skeleton, for backward compatibility, defines
95 a constructor for position that initializes the filename. The
96 glr.cc skeleton does not (and in fact cannot: location/position
97 are stored in a union, from which objects with constructors are
104 /* Bison Declarations */
105 %token CALC_EOF 0 "end of input"
106 %token <ival> NUM "number"
109 %nonassoc '=' /* comparison */
112 %left NEG /* negation--unary minus */
113 %right '^' /* exponentiation */
115 /* Grammar follows */
119 | input line { ]AT_PARAM_IF([++*count; ++global_count;])[ }
124 | exp '\n' { ]AT_PARAM_IF([*result = global_result = $1], [USE ($1)])[; }
132 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
135 | exp '+' exp { $$ = $1 + $3; }
136 | exp '-' exp { $$ = $1 - $3; }
137 | exp '*' exp { $$ = $1 * $3; }
138 | exp '/' exp { $$ = $1 / $3; }
139 | '-' exp %prec NEG { $$ = -$2; }
140 | exp '^' exp { $$ = power ($1, $3); }
141 | '(' exp ')' { $$ = $2; }
142 | '(' error ')' { $$ = 1111; yyerrok; }
143 | '!' { $$ = 0; YYERROR; }
144 | '-' error { $$ = 0; YYERROR; }
151 [/* A C++ error reporting function. */
153 AT_NAME_PREFIX::parser::error (const location& l, const std::string& m)
156 std::cerr << AT_LOCATION_IF([l << ": " << ])m << std::endl;
160 yyparse (AT_PARAM_IF([semantic_value *result, int *count]))
162 AT_NAME_PREFIX::parser parser[]AT_PARAM_IF([ (result, count)]);
164 parser.set_debug_level (1);
166 return parser.parse ();
170 yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ])
171 AT_PARAM_IF([semantic_value *result, int *count, ])
174 AT_PARAM_IF([(void) result; (void) count;])
175 AT_YYERROR_SEES_LOC_IF([
176 fprintf (stderr, "%d.%d",
177 AT_LOC.first_line, AT_LOC.first_column);
178 if (AT_LOC.first_line != AT_LOC.last_line)
179 fprintf (stderr, "-%d.%d",
180 AT_LOC.last_line, AT_LOC.last_column - 1);
181 else if (AT_LOC.first_column != AT_LOC.last_column - 1)
182 fprintf (stderr, "-%d",
183 AT_LOC.last_column - 1);
184 fprintf (stderr, ": ");])
185 fprintf (stderr, "%s\n", s);
190 static YYLTYPE last_yylloc;
193 get_char (]AT_LEX_FORMALS[)
195 int res = getc (input);
198 last_yylloc = AT_LOC;
202 AT_LOC.last_column = 1;
205 AT_LOC.last_column++;
212 unget_char (]AT_LEX_PRE_FORMALS[ int c)
216 /* Wrong when C == `\n'. */
217 AT_LOC = last_yylloc;
223 read_signed_integer (]AT_LEX_FORMALS[)
225 int c = get_char (]AT_LEX_ARGS[);
232 c = get_char (]AT_LEX_ARGS[);
238 n = 10 * n + (c - '0');
239 c = get_char (]AT_LEX_ARGS[);
242 unget_char (]AT_LEX_PRE_ARGS[ c);
249 /*---------------------------------------------------------------.
250 | Lexical analyzer returns an integer on the stack and the token |
251 | NUM, or the ASCII character read if not a number. Skips all |
252 | blanks and tabs, returns 0 for EOF. |
253 `---------------------------------------------------------------*/
256 yylex (]AT_LEX_FORMALS[)
265 AT_LOC.last_column = 1;
266 AT_LOC.last_line = 1;
271 AT_LOC.first_column = AT_LOC.last_column;
272 AT_LOC.first_line = AT_LOC.last_line;
275 /* Skip white space. */
276 while ((c = get_char (]AT_LEX_ARGS[)) == ' ' || c == '\t')
279 [ AT_LOC.first_column = AT_LOC.last_column;
280 AT_LOC.first_line = AT_LOC.last_line;
284 /* process numbers */
285 if (c == '.' || isdigit (c))
287 unget_char (]AT_LEX_PRE_ARGS[ c);
288 ]AT_VAL[.ival = read_signed_integer (]AT_LEX_ARGS[);
292 /* Return end-of-file. */
296 /* Return single chars. */
301 power (int base, int exponent)
306 for (/* Niente */; exponent; --exponent)
313 main (int argc, const char **argv)
315 semantic_value result = 0;
319 /* This used to be alarm (10), but that isn't enough time for
320 a July 1995 vintage DEC Alphastation 200 4/100 system,
321 according to Nelson H. F. Beebe. 100 seconds is enough. */
325 input = fopen (argv[1], "r");
335 ]AT_SKEL_CC_IF([], [m4_bmatch([$4], [%debug],
337 status = yyparse (]AT_PARAM_IF([[&result, &count]])[);
339 if (global_result != result)
341 if (global_count != count)
349 # AT_DATA_CALC_Y([BISON-OPTIONS])
350 # -------------------------------
352 m4_define([AT_DATA_CALC_Y],
353 [_AT_DATA_CALC_Y($[1], $[2], $[3], [$1])
358 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES])
359 # --------------------------------------------------------
360 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
362 # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
364 # NUM-STDERR-LINES is the number of expected lines on stderr.
365 # Currently this is ignored, though, since the output format is fluctuating.
367 # We don't count GLR's traces yet, since its traces are somewhat
368 # different from LALR's.
369 m4_define([_AT_CHECK_CALC],
373 AT_PARSER_CHECK([./calc input], 0, [], [stderr])
377 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT,
378 # [NUM-STDERR-LINES],
379 # [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
380 # ---------------------------------------------------------
381 # Run `calc' on INPUT, and expect a `syntax error' message.
383 # If INPUT starts with a slash, it is used as absolute input file name,
384 # otherwise as contents.
386 # NUM-STDERR-LINES is the number of expected lines on stderr.
387 # Currently this is ignored, though, since the output format is fluctuating.
389 # If BISON-OPTIONS contains `%location', then make sure the ERROR-LOCATION
390 # is correctly output on stderr.
392 # If BISON-OPTIONS contains `%error-verbose', then make sure the
393 # IF-YYERROR-VERBOSE message is properly output after `syntax error, '
396 # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
397 # is the number of expected lines on stderr.
398 m4_define([_AT_CHECK_CALC_ERROR],
399 [m4_bmatch([$3], [^/],
400 [AT_PARSER_CHECK([./calc $3], $2, [], [stderr])],
404 AT_PARSER_CHECK([./calc input], $2, [], [stderr])])
406 # Normalize the observed and expected error messages, depending upon the
408 # 1. Remove the traces from observed.
423 /^yydestructor:/d' stderr >at-stderr
425 # 2. Create the reference error message.
429 # 3. If locations are not used, remove them.
430 AT_YYERROR_SEES_LOC_IF([],
431 [[sed 's/^[-0-9.]*: //' expout >at-expout
432 mv at-expout expout]])
433 # 4. If error-verbose is not used, strip the`, unexpected....' part.
434 m4_bmatch([$1], [%error-verbose], [],
435 [[sed 's/syntax error, .*$/syntax error/' expout >at-expout
436 mv at-expout expout]])
438 AT_CHECK([cat stderr], 0, [expout])
442 # AT_CHECK_CALC([BISON-OPTIONS, [EXPECTED-TO-FAIL]])
443 # --------------------------------------------------
444 # Start a testing chunk which compiles `calc' grammar with
445 # BISON-OPTIONS, and performs several tests over the parser.
446 # However, if EXPECTED-TO-FAIL is nonempty, this test is expected to fail.
447 m4_define([AT_CHECK_CALC],
448 [# We use integers to avoid dependencies upon the precision of doubles.
449 AT_SETUP([Calculator $1])
451 m4_ifval([$2], [AT_CHECK([exit 77])])
453 AT_BISON_OPTION_PUSHDEFS([$1])
458 [AT_BISON_CHECK([-o calc.cc calc.y])
459 AT_COMPILE_CXX([calc])],
460 [AT_BISON_CHECK([-o calc.c calc.y])
463 # Test the priorities.
480 # Some syntax errors.
481 _AT_CHECK_CALC_ERROR([$1], [1], [0 0], [15],
482 [1.3: syntax error, unexpected number])
483 _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [20],
484 [1.3: syntax error, unexpected '/', expecting number or '-' or '(' or '!'])
485 _AT_CHECK_CALC_ERROR([$1], [1], [error], [5],
486 [1.1: syntax error, unexpected $undefined])
487 _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [30],
488 [1.7: syntax error, unexpected '='])
489 _AT_CHECK_CALC_ERROR([$1], [1],
493 [2.1: syntax error, unexpected '+'])
494 # Exercise error messages with EOF: work on an empty file.
495 _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4],
496 [1.1: syntax error, unexpected end of input])
498 # Exercise the error token: without it, we die at the first error,
501 # - have several errors which exercise different shift/discardings
502 # - (): nothing to pop, nothing to discard
503 # - (1 + 1 + 1 +): a lot to pop, nothing to discard
504 # - (* * *): nothing to pop, a lot to discard
505 # - (1 + 2 * *): some to pop and discard
507 # - test the action associated to `error'
509 # - check the lookahead that triggers an error is not discarded
510 # when we enter error recovery. Below, the lookahead causing the
511 # first error is ")", which is needed to recover from the error and
512 # produce the "0" that triggers the "0 != 1" error.
514 _AT_CHECK_CALC_ERROR([$1], [0],
515 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
517 [1.2: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
518 1.18: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
519 1.23: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
520 1.41: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
521 calc: error: 4444 != 1])
523 # The same, but this time exercising explicitly triggered syntax errors.
524 # POSIX says the lookahead causing the error should not be discarded.
525 _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [102],
526 [1.10: syntax error, unexpected number
527 calc: error: 2222 != 1])
528 _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (0 0) = 1], [113],
529 [1.4: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
530 1.12: syntax error, unexpected number
531 calc: error: 2222 != 1])
533 # Check that yyerrok works properly: second error is not reported,
534 # third and fourth are. Parse status is succesfull.
535 _AT_CHECK_CALC_ERROR([$1], [0], [(* *) + (*) + (*)], [113],
536 [1.2: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
537 1.10: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
538 1.16: syntax error, unexpected '*', expecting number or '-' or '(' or '!'])
540 AT_BISON_OPTION_POPDEFS
548 # ------------------------ #
549 # Simple LALR Calculator. #
550 # ------------------------ #
552 AT_BANNER([[Simple LALR(1) Calculator.]])
554 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
555 # -----------------------------------
556 # Start a testing chunk which compiles `calc' grammar with
557 # BISON-OPTIONS, and performs several tests over the parser.
558 m4_define([AT_CHECK_CALC_LALR],
563 AT_CHECK_CALC_LALR([%defines])
564 AT_CHECK_CALC_LALR([%locations])
565 AT_CHECK_CALC_LALR([%name-prefix="calc"]) dnl test deprecated `='
566 AT_CHECK_CALC_LALR([%verbose])
567 AT_CHECK_CALC_LALR([%yacc])
568 AT_CHECK_CALC_LALR([%error-verbose])
570 AT_CHECK_CALC_LALR([%define api.pure %locations])
571 AT_CHECK_CALC_LALR([%define api.push_pull "both" %define api.pure %locations])
572 AT_CHECK_CALC_LALR([%error-verbose %locations])
574 AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix "calc" %verbose %yacc])
576 AT_CHECK_CALC_LALR([%debug])
577 AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc])
579 AT_CHECK_CALC_LALR([%define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc])
580 AT_CHECK_CALC_LALR([%define api.push_pull "both" %define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc])
582 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}])
585 # ----------------------- #
586 # Simple GLR Calculator. #
587 # ----------------------- #
589 AT_BANNER([[Simple GLR Calculator.]])
591 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
592 # ----------------------------------
593 # Start a testing chunk which compiles `calc' grammar with
594 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
595 m4_define([AT_CHECK_CALC_GLR],
596 [AT_CHECK_CALC([%glr-parser] $@)])
601 AT_CHECK_CALC_GLR([%defines])
602 AT_CHECK_CALC_GLR([%locations])
603 AT_CHECK_CALC_GLR([%name-prefix "calc"])
604 AT_CHECK_CALC_GLR([%verbose])
605 AT_CHECK_CALC_GLR([%yacc])
606 AT_CHECK_CALC_GLR([%error-verbose])
608 AT_CHECK_CALC_GLR([%define api.pure %locations])
609 AT_CHECK_CALC_GLR([%error-verbose %locations])
611 AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix "calc" %verbose %yacc])
613 AT_CHECK_CALC_GLR([%debug])
614 AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc])
616 AT_CHECK_CALC_GLR([%define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc])
618 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}])
621 # ----------------------------- #
622 # Simple LALR1 C++ Calculator. #
623 # ----------------------------- #
625 AT_BANNER([[Simple LALR(1) C++ Calculator.]])
627 # First let's try using %skeleton
628 AT_CHECK_CALC([%skeleton "lalr1.cc" %defines %locations])
630 # AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS])
631 # ---------------------------------------
632 # Start a testing chunk which compiles `calc' grammar with
633 # the C++ skeleton, and performs several tests over the parser.
634 m4_define([AT_CHECK_CALC_LALR1_CC],
635 [AT_CHECK_CALC([%language "C++" %defines %locations] $@)])
637 AT_CHECK_CALC_LALR1_CC([])
638 AT_CHECK_CALC_LALR1_CC([%error-verbose %name-prefix "calc" %verbose %yacc])
640 AT_CHECK_CALC_LALR1_CC([%error-verbose %debug %name-prefix "calc" %verbose %yacc])
642 AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %name-prefix "calc" %verbose %yacc])
644 AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
648 # --------------------------- #
649 # Simple GLR C++ Calculator. #
650 # --------------------------- #
652 AT_BANNER([[Simple GLR C++ Calculator.]])
654 # Again, we try also using %skeleton.
655 AT_CHECK_CALC([%skeleton "glr.cc" %defines %locations])
657 # AT_CHECK_CALC_GLR_CC([BISON-OPTIONS])
658 # -------------------------------------
659 # Start a testing chunk which compiles `calc' grammar with
660 # the GLR C++ skeleton, and performs several tests over the parser.
661 m4_define([AT_CHECK_CALC_GLR_CC],
662 [AT_CHECK_CALC([%language "C++" %glr-parser %defines %locations] $@)])
664 AT_CHECK_CALC_GLR_CC([])
665 AT_CHECK_CALC_GLR_CC([%error-verbose %name-prefix "calc" %verbose %yacc])
667 AT_CHECK_CALC_GLR_CC([%debug])
668 AT_CHECK_CALC_GLR_CC([%error-verbose %debug %name-prefix "calc" %verbose %yacc])
670 AT_CHECK_CALC_GLR_CC([%pure-parser %error-verbose %debug %name-prefix "calc" %verbose %yacc])
672 AT_CHECK_CALC_GLR_CC([%pure-parser %error-verbose %debug %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])