1 # Simple calculator. -*- Autotest -*-
3 # Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 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);
75 [typedef yy::location YYLTYPE;
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);
93 /* Bison Declarations */
94 %token CALC_EOF 0 "end of input"
95 %token <ival> NUM "number"
98 %nonassoc '=' /* comparison */
101 %left NEG /* negation--unary minus */
102 %right '^' /* exponentiation */
104 /* Grammar follows */
108 | input line { ]AT_PARAM_IF([++*count; ++global_count;])[ }
113 | exp '\n' { ]AT_PARAM_IF([*result = global_result = $1], [USE ($1)])[; }
121 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
124 | exp '+' exp { $$ = $1 + $3; }
125 | exp '-' exp { $$ = $1 - $3; }
126 | exp '*' exp { $$ = $1 * $3; }
127 | exp '/' exp { $$ = $1 / $3; }
128 | '-' exp %prec NEG { $$ = -$2; }
129 | exp '^' exp { $$ = power ($1, $3); }
130 | '(' exp ')' { $$ = $2; }
131 | '(' error ')' { $$ = 1111; }
132 | '!' { $$ = 0; YYERROR; }
133 | '-' error { $$ = 0; YYERROR; }
140 [/* A C++ error reporting function. */
142 yy::parser::error (const location& l, const std::string& m)
145 std::cerr << AT_LOCATION_IF([l << ": " << ])m << std::endl;
149 yyparse (AT_PARAM_IF([semantic_value *result, int *count]))
151 yy::parser parser[]AT_PARAM_IF([ (result, count)]);
152 parser.set_debug_level (!!YYDEBUG);
153 return parser.parse ();
157 yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ])
158 AT_PARAM_IF([semantic_value *result, int *count, ])
161 AT_PARAM_IF([(void) result; (void) count;])
162 AT_YYERROR_SEES_LOC_IF([
163 fprintf (stderr, "%d.%d",
164 AT_LOC.first_line, AT_LOC.first_column);
165 if (AT_LOC.first_line != AT_LOC.last_line)
166 fprintf (stderr, "-%d.%d",
167 AT_LOC.last_line, AT_LOC.last_column - 1);
168 else if (AT_LOC.first_column != AT_LOC.last_column - 1)
169 fprintf (stderr, "-%d",
170 AT_LOC.last_column - 1);
171 fprintf (stderr, ": ");])
172 fprintf (stderr, "%s\n", s);
177 static YYLTYPE last_yylloc;
180 get_char (]AT_LEX_FORMALS[)
182 int res = getc (input);
185 last_yylloc = AT_LOC;
189 AT_LOC.last_column = 0;
192 AT_LOC.last_column++;
199 unget_char (]AT_LEX_PRE_FORMALS[ int c)
203 /* Wrong when C == `\n'. */
204 AT_LOC = last_yylloc;
210 read_signed_integer (]AT_LEX_FORMALS[)
212 int c = get_char (]AT_LEX_ARGS[);
219 c = get_char (]AT_LEX_ARGS[);
225 n = 10 * n + (c - '0');
226 c = get_char (]AT_LEX_ARGS[);
229 unget_char (]AT_LEX_PRE_ARGS[ c);
236 /*---------------------------------------------------------------.
237 | Lexical analyzer returns an integer on the stack and the token |
238 | NUM, or the ASCII character read if not a number. Skips all |
239 | blanks and tabs, returns 0 for EOF. |
240 `---------------------------------------------------------------*/
243 yylex (]AT_LEX_FORMALS[)
252 AT_LOC.last_column = 0;
253 AT_LOC.last_line = 1;
258 AT_LOC.first_column = AT_LOC.last_column;
259 AT_LOC.first_line = AT_LOC.last_line;
262 /* Skip white space. */
263 while ((c = get_char (]AT_LEX_ARGS[)) == ' ' || c == '\t')
266 [ AT_LOC.first_column = AT_LOC.last_column;
267 AT_LOC.first_line = AT_LOC.last_line;
271 /* process numbers */
272 if (c == '.' || isdigit (c))
274 unget_char (]AT_LEX_PRE_ARGS[ c);
275 ]AT_VAL[.ival = read_signed_integer (]AT_LEX_ARGS[);
279 /* Return end-of-file. */
283 /* Return single chars. */
288 power (int base, int exponent)
293 for (/* Niente */; exponent; --exponent)
300 main (int argc, const char **argv)
302 semantic_value result = 0;
306 /* This used to be alarm (10), but that isn't enough time for
307 a July 1995 vintage DEC Alphastation 200 4/100 system,
308 according to Nelson H. F. Beebe. 100 seconds is enough. */
312 input = fopen (argv[1], "r");
322 ]AT_LALR1_CC_IF([], [m4_bmatch([$4], [%debug],
324 status = yyparse (]AT_PARAM_IF([&result, &count])[);
325 if (global_result != result)
327 if (global_count != count)
335 # AT_DATA_CALC_Y([BISON-OPTIONS])
336 # -------------------------------
338 m4_define([AT_DATA_CALC_Y],
339 [_AT_DATA_CALC_Y($[1], $[2], $[3], [$1])
344 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES])
345 # --------------------------------------------------------
346 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
348 # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
350 # NUM-STDERR-LINES is the number of expected lines on stderr.
351 # Currently this is ignored, though, since the output format is fluctuating.
353 # We don't count GLR's traces yet, since its traces are somewhat
354 # different from LALR's.
355 m4_define([_AT_CHECK_CALC],
359 AT_PARSER_CHECK([./calc input], 0, [], [stderr])
363 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT,
364 # [NUM-STDERR-LINES],
365 # [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
366 # ---------------------------------------------------------
367 # Run `calc' on INPUT, and expect a `syntax error' message.
369 # If INPUT starts with a slash, it is used as absolute input file name,
370 # otherwise as contents.
372 # NUM-STDERR-LINES is the number of expected lines on stderr.
373 # Currently this is ignored, though, since the output format is fluctuating.
375 # If BISON-OPTIONS contains `%location', then make sure the ERROR-LOCATION
376 # is correctly output on stderr.
378 # If BISON-OPTIONS contains `%error-verbose', then make sure the
379 # IF-YYERROR-VERBOSE message is properly output after `syntax error, '
382 # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
383 # is the number of expected lines on stderr.
384 m4_define([_AT_CHECK_CALC_ERROR],
385 [m4_bmatch([$3], [^/],
386 [AT_PARSER_CHECK([./calc $3], $2, [], [stderr])],
390 AT_PARSER_CHECK([./calc input], $2, [], [stderr])])
392 # Normalize the observed and expected error messages, depending upon the
394 # 1. Remove the traces from observed.
407 /^yydestructor:/d' stderr >at-stderr
409 # 2. Create the reference error message.
413 # 3. If locations are not used, remove them.
414 AT_YYERROR_SEES_LOC_IF([],
415 [[sed 's/^[-0-9.]*: //' expout >at-expout
416 mv at-expout expout]])
417 # 4. If error-verbose is not used, strip the`, unexpected....' part.
418 m4_bmatch([$1], [%error-verbose], [],
419 [[sed 's/syntax error, .*$/syntax error/' expout >at-expout
420 mv at-expout expout]])
422 AT_CHECK([cat stderr], 0, [expout])
426 # AT_CHECK_CALC([BISON-OPTIONS, [EXPECTED-TO-FAIL]])
427 # --------------------------------------------------
428 # Start a testing chunk which compiles `calc' grammar with
429 # BISON-OPTIONS, and performs several tests over the parser.
430 # However, if EXPECTED-TO-FAIL is nonempty, this test is expected to fail.
431 m4_define([AT_CHECK_CALC],
432 [# We use integers to avoid dependencies upon the precision of doubles.
433 AT_SETUP([Calculator $1])
435 m4_ifval([$2], [AT_CHECK([exit 77])])
437 AT_BISON_OPTION_PUSHDEFS([$1])
442 [AT_CHECK([bison -o calc.cc calc.y])
443 AT_COMPILE_CXX([calc])],
444 [AT_CHECK([bison -o calc.c calc.y])
447 # Test the priorities.
464 # Some syntax errors.
465 _AT_CHECK_CALC_ERROR([$1], [1], [0 0], [15],
466 [1.2: syntax error, unexpected number])
467 _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [20],
468 [1.2: syntax error, unexpected '/', expecting number or '-' or '(' or '!'])
469 _AT_CHECK_CALC_ERROR([$1], [1], [error], [5],
470 [1.0: syntax error, unexpected $undefined])
471 _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [30],
472 [1.6: syntax error, unexpected '='])
473 _AT_CHECK_CALC_ERROR([$1], [1],
477 [2.0: syntax error, unexpected '+'])
478 # Exercise error messages with EOF: work on an empty file.
479 _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4],
480 [1.0: syntax error, unexpected end of input])
482 # Exercise the error token: without it, we die at the first error,
485 # - have several errors which exercise different shift/discardings
486 # - (): nothing to pop, nothing to discard
487 # - (1 + 1 + 1 +): a lot to pop, nothing to discard
488 # - (* * *): nothing to pop, a lot to discard
489 # - (1 + 2 * *): some to pop and discard
491 # - test the action associated to `error'
493 # - check the look-ahead that triggers an error is not discarded
494 # when we enter error recovery. Below, the look-ahead causing the
495 # first error is ")", which is needed to recover from the error and
496 # produce the "0" that triggers the "0 != 1" error.
498 _AT_CHECK_CALC_ERROR([$1], [0],
499 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
501 [1.1: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
502 1.17: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
503 1.22: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
504 1.40: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
505 calc: error: 4444 != 1])
507 # The same, but this time exercising explicitly triggered syntax errors.
508 # POSIX says the look-ahead causing the error should not be discarded.
509 _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [102],
510 [1.9: syntax error, unexpected number
511 calc: error: 2222 != 1])
512 _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (0 0) = 1], [113],
513 [1.3: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
514 1.11: syntax error, unexpected number
515 calc: error: 2222 != 1])
516 AT_BISON_OPTION_POPDEFS
524 # ------------------------ #
525 # Simple LALR Calculator. #
526 # ------------------------ #
528 AT_BANNER([[Simple LALR Calculator.]])
530 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
531 # -----------------------------------
532 # Start a testing chunk which compiles `calc' grammar with
533 # BISON-OPTIONS, and performs several tests over the parser.
534 m4_define([AT_CHECK_CALC_LALR],
539 AT_CHECK_CALC_LALR([%defines])
540 AT_CHECK_CALC_LALR([%locations])
541 AT_CHECK_CALC_LALR([%name-prefix="calc"])
542 AT_CHECK_CALC_LALR([%verbose])
543 AT_CHECK_CALC_LALR([%yacc])
544 AT_CHECK_CALC_LALR([%error-verbose])
546 AT_CHECK_CALC_LALR([%pure-parser %locations])
547 AT_CHECK_CALC_LALR([%error-verbose %locations])
549 AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
551 AT_CHECK_CALC_LALR([%debug])
552 AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
554 AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
556 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}])
559 # ----------------------- #
560 # Simple GLR Calculator. #
561 # ----------------------- #
563 AT_BANNER([[Simple GLR Calculator.]])
565 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
566 # ----------------------------------
567 # Start a testing chunk which compiles `calc' grammar with
568 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
569 m4_define([AT_CHECK_CALC_GLR],
570 [AT_CHECK_CALC([%glr-parser] $@)])
575 AT_CHECK_CALC_GLR([%defines])
576 AT_CHECK_CALC_GLR([%locations])
577 AT_CHECK_CALC_GLR([%name-prefix="calc"])
578 AT_CHECK_CALC_GLR([%verbose])
579 AT_CHECK_CALC_GLR([%yacc])
580 AT_CHECK_CALC_GLR([%error-verbose])
582 AT_CHECK_CALC_GLR([%pure-parser %locations])
583 AT_CHECK_CALC_GLR([%error-verbose %locations])
585 AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
587 AT_CHECK_CALC_GLR([%debug])
588 AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
590 AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
592 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}])
595 # ----------------------------- #
596 # Simple LALR1 C++ Calculator. #
597 # ----------------------------- #
599 AT_BANNER([[Simple LALR1 C++ Calculator.]])
601 # AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS])
602 # ---------------------------------------
603 # Start a testing chunk which compiles `calc' grammar with
604 # the C++ skeleton, and performs several tests over the parser.
605 m4_define([AT_CHECK_CALC_LALR1_CC],
606 [AT_CHECK_CALC([%skeleton "lalr1.cc"] $@)])
608 # AT_CHECK_CALC_LALR1_CC()
610 AT_CHECK_CALC_LALR1_CC([%defines %locations])
612 AT_CHECK_CALC_LALR1_CC([%defines])
613 # AT_CHECK_CALC_LALR1_CC([%locations])
614 # AT_CHECK_CALC_LALR1_CC([%name-prefix="calc"])
615 # AT_CHECK_CALC_LALR1_CC([%verbose])
616 # AT_CHECK_CALC_LALR1_CC([%yacc])
617 # AT_CHECK_CALC_LALR1_CC([%error-verbose])
619 # AT_CHECK_CALC_LALR1_CC([%pure-parser %locations])
620 # AT_CHECK_CALC_LALR1_CC([%error-verbose %locations])
622 AT_CHECK_CALC_LALR1_CC([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
624 # AT_CHECK_CALC_LALR1_CC([%debug])
625 AT_CHECK_CALC_LALR1_CC([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
627 AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
629 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}])