1 # Simple calculator. -*- Autotest -*-
3 # Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 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 2, or (at your option)
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, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 ## ---------------------------------------------------- ##
22 ## Compile the grammar described in the documentation. ##
23 ## ---------------------------------------------------- ##
26 # ------------------------- #
27 # Helping Autotest macros. #
28 # ------------------------- #
31 # _AT_DATA_CALC_Y($1, $2, $3, [BISON-DIRECTIVES])
32 # -----------------------------------------------
33 # Produce `calc.y'. Don't call this macro directly, because it contains
34 # some occurrences of `$1' etc. which will be interpreted by m4. So
35 # you should call it with $1, $2, and $3 as arguments, which is what
36 # AT_DATA_CALC_Y does.
37 m4_define([_AT_DATA_CALC_Y],
38 [m4_if([$1$2$3], $[1]$[2]$[3], [],
39 [m4_fatal([$0: Invalid arguments: $@])])dnl
40 AT_DATA_GRAMMAR([calc.y],
41 [[/* Infix notation calculator--calc */
44 [%define "global_tokens_and_yystype"])[
54 # define alarm(seconds) /* empty */
59 /* Exercise pre-prologue dependency to %union. */
60 typedef int semantic_value;
62 static semantic_value global_result = 0;
63 static int global_count = 0;
66 /* Exercise %union. */
73 static int power (int base, int exponent);
76 # define YYLTYPE yy::location
78 #define first_line begin.line
79 #define first_column begin.column
80 #define last_line end.line
81 #define last_column end.column
83 [/* yyerror receives the location if:
84 - %location & %pure & %glr
85 - %location & %pure & %yacc & %parse-param. */
86 static void yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ])
87 AT_PARAM_IF([semantic_value *result, int *count, ])
90 static int yylex (]AT_LEX_FORMALS[);
91 static int get_char (]AT_LEX_FORMALS[);
92 static void unget_char (]AT_LEX_PRE_FORMALS[ int c);
96 [/* The lalr1.cc skeleton, for backward compatibility, defines
97 a constructor for position that initializes the filename. The
98 glr.cc skeleton does not (and in fact cannot: location/position
99 are stored in a union, from which objects with constructors are
102 @$.begin.filename = @$.end.filename = 0;
106 /* Bison Declarations */
107 %token CALC_EOF 0 "end of input"
108 %token <ival> NUM "number"
111 %nonassoc '=' /* comparison */
114 %left NEG /* negation--unary minus */
115 %right '^' /* exponentiation */
117 /* Grammar follows */
121 | input line { ]AT_PARAM_IF([++*count; ++global_count;])[ }
126 | exp '\n' { ]AT_PARAM_IF([*result = global_result = $1], [USE ($1)])[; }
134 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
137 | exp '+' exp { $$ = $1 + $3; }
138 | exp '-' exp { $$ = $1 - $3; }
139 | exp '*' exp { $$ = $1 * $3; }
140 | exp '/' exp { $$ = $1 / $3; }
141 | '-' exp %prec NEG { $$ = -$2; }
142 | exp '^' exp { $$ = power ($1, $3); }
143 | '(' exp ')' { $$ = $2; }
144 | '(' error ')' { $$ = 1111; }
145 | '!' { $$ = 0; YYERROR; }
146 | '-' error { $$ = 0; YYERROR; }
153 [/* A C++ error reporting function. */
155 yy::parser::error (const location& l, const std::string& m)
158 std::cerr << AT_LOCATION_IF([l << ": " << ])m << std::endl;
162 yyparse (AT_PARAM_IF([semantic_value *result, int *count]))
164 yy::parser parser[]AT_PARAM_IF([ (result, count)]);
165 parser.set_debug_level (!!YYDEBUG);
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 = 0;
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 = 0;
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])[);
338 if (global_result != result)
340 if (global_count != count)
348 # AT_DATA_CALC_Y([BISON-OPTIONS])
349 # -------------------------------
351 m4_define([AT_DATA_CALC_Y],
352 [_AT_DATA_CALC_Y($[1], $[2], $[3], [$1])
357 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES])
358 # --------------------------------------------------------
359 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
361 # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
363 # NUM-STDERR-LINES is the number of expected lines on stderr.
364 # Currently this is ignored, though, since the output format is fluctuating.
366 # We don't count GLR's traces yet, since its traces are somewhat
367 # different from LALR's.
368 m4_define([_AT_CHECK_CALC],
372 AT_PARSER_CHECK([./calc input], 0, [], [stderr])
376 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT,
377 # [NUM-STDERR-LINES],
378 # [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
379 # ---------------------------------------------------------
380 # Run `calc' on INPUT, and expect a `syntax error' message.
382 # If INPUT starts with a slash, it is used as absolute input file name,
383 # otherwise as contents.
385 # NUM-STDERR-LINES is the number of expected lines on stderr.
386 # Currently this is ignored, though, since the output format is fluctuating.
388 # If BISON-OPTIONS contains `%location', then make sure the ERROR-LOCATION
389 # is correctly output on stderr.
391 # If BISON-OPTIONS contains `%error-verbose', then make sure the
392 # IF-YYERROR-VERBOSE message is properly output after `syntax error, '
395 # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
396 # is the number of expected lines on stderr.
397 m4_define([_AT_CHECK_CALC_ERROR],
398 [m4_bmatch([$3], [^/],
399 [AT_PARSER_CHECK([./calc $3], $2, [], [stderr])],
403 AT_PARSER_CHECK([./calc input], $2, [], [stderr])])
405 # Normalize the observed and expected error messages, depending upon the
407 # 1. Remove the traces from observed.
420 /^yydestructor:/d' stderr >at-stderr
422 # 2. Create the reference error message.
426 # 3. If locations are not used, remove them.
427 AT_YYERROR_SEES_LOC_IF([],
428 [[sed 's/^[-0-9.]*: //' expout >at-expout
429 mv at-expout expout]])
430 # 4. If error-verbose is not used, strip the`, unexpected....' part.
431 m4_bmatch([$1], [%error-verbose], [],
432 [[sed 's/syntax error, .*$/syntax error/' expout >at-expout
433 mv at-expout expout]])
435 AT_CHECK([cat stderr], 0, [expout])
439 # AT_CHECK_CALC([BISON-OPTIONS, [EXPECTED-TO-FAIL]])
440 # --------------------------------------------------
441 # Start a testing chunk which compiles `calc' grammar with
442 # BISON-OPTIONS, and performs several tests over the parser.
443 # However, if EXPECTED-TO-FAIL is nonempty, this test is expected to fail.
444 m4_define([AT_CHECK_CALC],
445 [# We use integers to avoid dependencies upon the precision of doubles.
446 AT_SETUP([Calculator $1])
448 m4_ifval([$2], [AT_CHECK([exit 77])])
450 AT_BISON_OPTION_PUSHDEFS([$1])
455 [AT_CHECK([bison -o calc.cc calc.y])
456 AT_COMPILE_CXX([calc])],
457 [AT_CHECK([bison -o calc.c calc.y])
460 # Test the priorities.
477 # Some syntax errors.
478 _AT_CHECK_CALC_ERROR([$1], [1], [0 0], [15],
479 [1.2: syntax error, unexpected number])
480 _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [20],
481 [1.2: syntax error, unexpected '/', expecting number or '-' or '(' or '!'])
482 _AT_CHECK_CALC_ERROR([$1], [1], [error], [5],
483 [1.0: syntax error, unexpected $undefined])
484 _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [30],
485 [1.6: syntax error, unexpected '='])
486 _AT_CHECK_CALC_ERROR([$1], [1],
490 [2.0: syntax error, unexpected '+'])
491 # Exercise error messages with EOF: work on an empty file.
492 _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4],
493 [1.0: syntax error, unexpected end of input])
495 # Exercise the error token: without it, we die at the first error,
498 # - have several errors which exercise different shift/discardings
499 # - (): nothing to pop, nothing to discard
500 # - (1 + 1 + 1 +): a lot to pop, nothing to discard
501 # - (* * *): nothing to pop, a lot to discard
502 # - (1 + 2 * *): some to pop and discard
504 # - test the action associated to `error'
506 # - check the look-ahead that triggers an error is not discarded
507 # when we enter error recovery. Below, the look-ahead causing the
508 # first error is ")", which is needed to recover from the error and
509 # produce the "0" that triggers the "0 != 1" error.
511 _AT_CHECK_CALC_ERROR([$1], [0],
512 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
514 [1.1: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
515 1.17: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
516 1.22: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
517 1.40: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
518 calc: error: 4444 != 1])
520 # The same, but this time exercising explicitly triggered syntax errors.
521 # POSIX says the look-ahead causing the error should not be discarded.
522 _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [102],
523 [1.9: syntax error, unexpected number
524 calc: error: 2222 != 1])
525 _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (0 0) = 1], [113],
526 [1.3: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
527 1.11: syntax error, unexpected number
528 calc: error: 2222 != 1])
529 AT_BISON_OPTION_POPDEFS
537 # ------------------------ #
538 # Simple LALR Calculator. #
539 # ------------------------ #
541 AT_BANNER([[Simple LALR(1) Calculator.]])
543 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
544 # -----------------------------------
545 # Start a testing chunk which compiles `calc' grammar with
546 # BISON-OPTIONS, and performs several tests over the parser.
547 m4_define([AT_CHECK_CALC_LALR],
552 AT_CHECK_CALC_LALR([%defines])
553 AT_CHECK_CALC_LALR([%locations])
554 AT_CHECK_CALC_LALR([%name-prefix="calc"])
555 AT_CHECK_CALC_LALR([%verbose])
556 AT_CHECK_CALC_LALR([%yacc])
557 AT_CHECK_CALC_LALR([%error-verbose])
559 AT_CHECK_CALC_LALR([%pure-parser %locations])
560 AT_CHECK_CALC_LALR([%error-verbose %locations])
562 AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
564 AT_CHECK_CALC_LALR([%debug])
565 AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
567 AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
569 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}])
572 # ----------------------- #
573 # Simple GLR Calculator. #
574 # ----------------------- #
576 AT_BANNER([[Simple GLR Calculator.]])
578 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
579 # ----------------------------------
580 # Start a testing chunk which compiles `calc' grammar with
581 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
582 m4_define([AT_CHECK_CALC_GLR],
583 [AT_CHECK_CALC([%glr-parser] $@)])
588 AT_CHECK_CALC_GLR([%defines])
589 AT_CHECK_CALC_GLR([%locations])
590 AT_CHECK_CALC_GLR([%name-prefix="calc"])
591 AT_CHECK_CALC_GLR([%verbose])
592 AT_CHECK_CALC_GLR([%yacc])
593 AT_CHECK_CALC_GLR([%error-verbose])
595 AT_CHECK_CALC_GLR([%pure-parser %locations])
596 AT_CHECK_CALC_GLR([%error-verbose %locations])
598 AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
600 AT_CHECK_CALC_GLR([%debug])
601 AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
603 AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
605 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}])
608 # ----------------------------- #
609 # Simple LALR1 C++ Calculator. #
610 # ----------------------------- #
612 AT_BANNER([[Simple LALR(1) C++ Calculator.]])
614 # AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS])
615 # ---------------------------------------
616 # Start a testing chunk which compiles `calc' grammar with
617 # the C++ skeleton, and performs several tests over the parser.
618 m4_define([AT_CHECK_CALC_LALR1_CC],
619 [AT_CHECK_CALC([%skeleton "lalr1.cc" %defines %locations] $@)])
621 AT_CHECK_CALC_LALR1_CC([])
622 AT_CHECK_CALC_LALR1_CC([%error-verbose %name-prefix="calc" %verbose %yacc])
624 AT_CHECK_CALC_LALR1_CC([%error-verbose %debug %name-prefix="calc" %verbose %yacc])
626 AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %name-prefix="calc" %verbose %yacc])
628 AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %name-prefix="calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
632 # --------------------------- #
633 # Simple GLR C++ Calculator. #
634 # --------------------------- #
636 AT_BANNER([[Simple GLR C++ Calculator.]])
638 # AT_CHECK_CALC_GLR_CC([BISON-OPTIONS])
639 # -------------------------------------
640 # Start a testing chunk which compiles `calc' grammar with
641 # the GLR C++ skeleton, and performs several tests over the parser.
642 m4_define([AT_CHECK_CALC_GLR_CC],
643 [AT_CHECK_CALC([%skeleton "glr.cc" %defines %locations] $@)])
645 #AT_CHECK_CALC_GLR_CC([])
646 #AT_CHECK_CALC_GLR_CC([%error-verbose %name-prefix="calc" %verbose %yacc])
648 # AT_CHECK_CALC_GLR_CC([%debug])
649 #AT_CHECK_CALC_GLR_CC([%error-verbose %debug %name-prefix="calc" %verbose %yacc])
651 AT_CHECK_CALC_GLR_CC([%pure-parser %error-verbose %debug %name-prefix="calc" %verbose %yacc])
653 AT_CHECK_CALC_GLR_CC([%pure-parser %error-verbose %debug %name-prefix="calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])