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 */
58 /* Exercise pre-prologue dependency to %union. */
59 typedef int semantic_value;
61 static semantic_value global_result = 0;
62 static int global_count = 0;
65 /* Exercise %union. */
72 static int power (int base, int exponent);
74 [typedef yy::location YYLTYPE;
75 #define first_line begin.line
76 #define first_column begin.column
77 #define last_line end.line
78 #define last_column end.column
80 [/* yyerror receives the location if:
81 - %location & %pure & %glr
82 - %location & %pure & %yacc & %parse-param. */
83 static void yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ])
84 AT_PARAM_IF([semantic_value *result, int *count, ])
87 static int yylex (]AT_LEX_FORMALS[);
88 static int get_char (]AT_LEX_FORMALS[);
89 static void unget_char (]AT_LEX_PRE_FORMALS[ int c);
92 /* Bison Declarations */
93 %token CALC_EOF 0 "end of input"
94 %token <ival> NUM "number"
97 %nonassoc '=' /* comparison */
100 %left NEG /* negation--unary minus */
101 %right '^' /* exponentiation */
103 /* Grammar follows */
107 | input line { ]AT_PARAM_IF([++*count; ++global_count;])[ }
112 | exp '\n' { ]AT_PARAM_IF([*result = global_result = $1;])[ }
120 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
123 | exp '+' exp { $$ = $1 + $3; }
124 | exp '-' exp { $$ = $1 - $3; }
125 | exp '*' exp { $$ = $1 * $3; }
126 | exp '/' exp { $$ = $1 / $3; }
127 | '-' exp %prec NEG { $$ = -$2; }
128 | exp '^' exp { $$ = power ($1, $3); }
129 | '(' exp ')' { $$ = $2; }
130 | '(' error ')' { $$ = 1111; }
132 | '-' error { YYERROR; }
139 [/* A C++ error reporting function. */
141 yy::parser::error (const location& l, const std::string& m)
144 std::cerr << AT_LOCATION_IF([l << ": " << ])m << std::endl;
148 yyparse (AT_PARAM_IF([semantic_value *result, int *count]))
150 yy::parser parser[]AT_PARAM_IF([ (result, count)]);
151 parser.set_debug_level (!!YYDEBUG);
152 return parser.parse ();
156 yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ])
157 AT_PARAM_IF([semantic_value *result, int *count, ])
160 AT_PARAM_IF([(void) result; (void) count;])
161 AT_YYERROR_SEES_LOC_IF([
162 fprintf (stderr, "%d.%d",
163 AT_LOC.first_line, AT_LOC.first_column);
164 if (AT_LOC.first_line != AT_LOC.last_line)
165 fprintf (stderr, "-%d.%d",
166 AT_LOC.last_line, AT_LOC.last_column - 1);
167 else if (AT_LOC.first_column != AT_LOC.last_column - 1)
168 fprintf (stderr, "-%d",
169 AT_LOC.last_column - 1);
170 fprintf (stderr, ": ");])
171 fprintf (stderr, "%s\n", s);
176 static YYLTYPE last_yylloc;
179 get_char (]AT_LEX_FORMALS[)
181 int res = getc (input);
184 last_yylloc = AT_LOC;
188 AT_LOC.last_column = 0;
191 AT_LOC.last_column++;
198 unget_char (]AT_LEX_PRE_FORMALS[ int c)
202 /* Wrong when C == `\n'. */
203 AT_LOC = last_yylloc;
209 read_signed_integer (]AT_LEX_FORMALS[)
211 int c = get_char (]AT_LEX_ARGS[);
218 c = get_char (]AT_LEX_ARGS[);
224 n = 10 * n + (c - '0');
225 c = get_char (]AT_LEX_ARGS[);
228 unget_char (]AT_LEX_PRE_ARGS[ c);
235 /*---------------------------------------------------------------.
236 | Lexical analyzer returns an integer on the stack and the token |
237 | NUM, or the ASCII character read if not a number. Skips all |
238 | blanks and tabs, returns 0 for EOF. |
239 `---------------------------------------------------------------*/
242 yylex (]AT_LEX_FORMALS[)
251 AT_LOC.last_column = 0;
252 AT_LOC.last_line = 1;
257 AT_LOC.first_column = AT_LOC.last_column;
258 AT_LOC.first_line = AT_LOC.last_line;
261 /* Skip white space. */
262 while ((c = get_char (]AT_LEX_ARGS[)) == ' ' || c == '\t')
265 [ AT_LOC.first_column = AT_LOC.last_column;
266 AT_LOC.first_line = AT_LOC.last_line;
270 /* process numbers */
271 if (c == '.' || isdigit (c))
273 unget_char (]AT_LEX_PRE_ARGS[ c);
274 ]AT_VAL[.ival = read_signed_integer (]AT_LEX_ARGS[);
278 /* Return end-of-file. */
282 /* Return single chars. */
287 power (int base, int exponent)
292 for (/* Niente */; exponent; --exponent)
299 main (int argc, const char **argv)
301 semantic_value result = 0;
305 /* This used to be alarm (10), but that isn't enough time for
306 a July 1995 vintage DEC Alphastation 200 4/100 system,
307 according to Nelson H. F. Beebe. 100 seconds is enough. */
311 input = fopen (argv[1], "r");
321 ]AT_LALR1_CC_IF([], [m4_bmatch([$4], [%debug],
323 status = yyparse (]AT_PARAM_IF([&result, &count])[);
324 if (global_result != result)
326 if (global_count != count)
334 # AT_DATA_CALC_Y([BISON-OPTIONS])
335 # -------------------------------
337 m4_define([AT_DATA_CALC_Y],
338 [_AT_DATA_CALC_Y($[1], $[2], $[3], [$1])
343 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES])
344 # --------------------------------------------------------
345 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
347 # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
349 # NUM-STDERR-LINES is the number of expected lines on stderr.
350 # Currently this is ignored, though, since the output format is fluctuating.
352 # We don't count GLR's traces yet, since its traces are somewhat
353 # different from LALR's.
354 m4_define([_AT_CHECK_CALC],
358 AT_PARSER_CHECK([./calc input], 0, [], [stderr])
362 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT,
363 # [NUM-STDERR-LINES],
364 # [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
365 # ---------------------------------------------------------
366 # Run `calc' on INPUT, and expect a `syntax error' message.
368 # If INPUT starts with a slash, it is used as absolute input file name,
369 # otherwise as contents.
371 # NUM-STDERR-LINES is the number of expected lines on stderr.
372 # Currently this is ignored, though, since the output format is fluctuating.
374 # If BISON-OPTIONS contains `%location', then make sure the ERROR-LOCATION
375 # is correctly output on stderr.
377 # If BISON-OPTIONS contains `%error-verbose', then make sure the
378 # IF-YYERROR-VERBOSE message is properly output after `syntax error, '
381 # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
382 # is the number of expected lines on stderr.
383 m4_define([_AT_CHECK_CALC_ERROR],
384 [m4_bmatch([$3], [^/],
385 [AT_PARSER_CHECK([./calc $3], $2, [], [stderr])],
389 AT_PARSER_CHECK([./calc input], $2, [], [stderr])])
391 # Normalize the observed and expected error messages, depending upon the
393 # 1. Remove the traces from observed.
406 /^yydestructor:/d' stderr >at-stderr
408 # 2. Create the reference error message.
412 # 3. If locations are not used, remove them.
413 AT_YYERROR_SEES_LOC_IF([],
414 [[sed 's/^[-0-9.]*: //' expout >at-expout
415 mv at-expout expout]])
416 # 4. If error-verbose is not used, strip the`, unexpected....' part.
417 m4_bmatch([$1], [%error-verbose], [],
418 [[sed 's/syntax error, .*$/syntax error/' expout >at-expout
419 mv at-expout expout]])
421 AT_CHECK([cat stderr], 0, [expout])
425 # AT_CHECK_CALC([BISON-OPTIONS, [EXPECTED-TO-FAIL]])
426 # --------------------------------------------------
427 # Start a testing chunk which compiles `calc' grammar with
428 # BISON-OPTIONS, and performs several tests over the parser.
429 # However, if EXPECTED-TO-FAIL is nonempty, this test is expected to fail.
430 m4_define([AT_CHECK_CALC],
431 [# We use integers to avoid dependencies upon the precision of doubles.
432 AT_SETUP([Calculator $1])
434 m4_ifval([$2], [AT_CHECK([exit 77])])
436 AT_BISON_OPTION_PUSHDEFS([$1])
441 [AT_CHECK([bison -o calc.cc calc.y])
442 AT_COMPILE_CXX([calc])],
443 [AT_CHECK([bison -o calc.c calc.y])
446 # Test the priorities.
463 # Some syntax errors.
464 _AT_CHECK_CALC_ERROR([$1], [1], [0 0], [15],
465 [1.2: syntax error, unexpected number])
466 _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [20],
467 [1.2: syntax error, unexpected '/', expecting number or '-' or '(' or '!'])
468 _AT_CHECK_CALC_ERROR([$1], [1], [error], [5],
469 [1.0: syntax error, unexpected $undefined])
470 _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [30],
471 [1.6: syntax error, unexpected '='])
472 _AT_CHECK_CALC_ERROR([$1], [1],
476 [2.0: syntax error, unexpected '+'])
477 # Exercise error messages with EOF: work on an empty file.
478 _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4],
479 [1.0: syntax error, unexpected end of input])
481 # Exercise the error token: without it, we die at the first error,
484 # - have several errors which exercise different shift/discardings
485 # - (): nothing to pop, nothing to discard
486 # - (1 + 1 + 1 +): a lot to pop, nothing to discard
487 # - (* * *): nothing to pop, a lot to discard
488 # - (1 + 2 * *): some to pop and discard
490 # - test the action associated to `error'
492 # - check the look-ahead that triggers an error is not discarded
493 # when we enter error recovery. Below, the look-ahead causing the
494 # first error is ")", which is needed to recover from the error and
495 # produce the "0" that triggers the "0 != 1" error.
497 _AT_CHECK_CALC_ERROR([$1], [0],
498 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
500 [1.1: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
501 1.17: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
502 1.22: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
503 1.40: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
504 calc: error: 4444 != 1])
506 # The same, but this time exercising explicitly triggered syntax errors.
507 # POSIX says the look-ahead causing the error should not be discarded.
508 _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [102],
509 [1.9: syntax error, unexpected number
510 calc: error: 2222 != 1])
511 _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (0 0) = 1], [113],
512 [1.3: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
513 1.11: syntax error, unexpected number
514 calc: error: 2222 != 1])
515 AT_BISON_OPTION_POPDEFS
523 # ------------------------ #
524 # Simple LALR Calculator. #
525 # ------------------------ #
527 AT_BANNER([[Simple LALR Calculator.]])
529 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
530 # -----------------------------------
531 # Start a testing chunk which compiles `calc' grammar with
532 # BISON-OPTIONS, and performs several tests over the parser.
533 m4_define([AT_CHECK_CALC_LALR],
538 AT_CHECK_CALC_LALR([%defines])
539 AT_CHECK_CALC_LALR([%locations])
540 AT_CHECK_CALC_LALR([%name-prefix="calc"])
541 AT_CHECK_CALC_LALR([%verbose])
542 AT_CHECK_CALC_LALR([%yacc])
543 AT_CHECK_CALC_LALR([%error-verbose])
545 AT_CHECK_CALC_LALR([%pure-parser %locations])
546 AT_CHECK_CALC_LALR([%error-verbose %locations])
548 AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
550 AT_CHECK_CALC_LALR([%debug])
551 AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
553 AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
555 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}])
558 # ----------------------- #
559 # Simple GLR Calculator. #
560 # ----------------------- #
562 AT_BANNER([[Simple GLR Calculator.]])
564 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
565 # ----------------------------------
566 # Start a testing chunk which compiles `calc' grammar with
567 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
568 m4_define([AT_CHECK_CALC_GLR],
569 [AT_CHECK_CALC([%glr-parser] $@)])
574 AT_CHECK_CALC_GLR([%defines])
575 AT_CHECK_CALC_GLR([%locations])
576 AT_CHECK_CALC_GLR([%name-prefix="calc"])
577 AT_CHECK_CALC_GLR([%verbose])
578 AT_CHECK_CALC_GLR([%yacc])
579 AT_CHECK_CALC_GLR([%error-verbose])
581 AT_CHECK_CALC_GLR([%pure-parser %locations])
582 AT_CHECK_CALC_GLR([%error-verbose %locations])
584 AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
586 AT_CHECK_CALC_GLR([%debug])
587 AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
589 AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
591 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}])
594 # ----------------------------- #
595 # Simple LALR1 C++ Calculator. #
596 # ----------------------------- #
598 AT_BANNER([[Simple LALR1 C++ Calculator.]])
600 # AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS])
601 # ---------------------------------------
602 # Start a testing chunk which compiles `calc' grammar with
603 # the C++ skeleton, and performs several tests over the parser.
604 m4_define([AT_CHECK_CALC_LALR1_CC],
605 [AT_CHECK_CALC([%skeleton "lalr1.cc"] $@)])
607 # AT_CHECK_CALC_LALR1_CC()
609 AT_CHECK_CALC_LALR1_CC([%defines %locations])
611 AT_CHECK_CALC_LALR1_CC([%defines])
612 # AT_CHECK_CALC_LALR1_CC([%locations])
613 # AT_CHECK_CALC_LALR1_CC([%name-prefix="calc"])
614 # AT_CHECK_CALC_LALR1_CC([%verbose])
615 # AT_CHECK_CALC_LALR1_CC([%yacc])
616 # AT_CHECK_CALC_LALR1_CC([%error-verbose])
618 # AT_CHECK_CALC_LALR1_CC([%pure-parser %locations])
619 # AT_CHECK_CALC_LALR1_CC([%error-verbose %locations])
621 AT_CHECK_CALC_LALR1_CC([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
623 # AT_CHECK_CALC_LALR1_CC([%debug])
624 AT_CHECK_CALC_LALR1_CC([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
626 AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
628 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}])