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 */
52 # define alarm(seconds) /* empty */
56 /* Exercise pre-prologue dependency to %union. */
57 typedef int semantic_value;
59 static semantic_value global_result = 0;
60 static int global_count = 0;
63 /* Exercise %union. */
70 static int power (int base, int exponent);
71 ]AT_LALR1_CC_IF([typedef yy::location YYLTYPE;],
72 [/* yyerror receives the location if:
73 - %location & %pure & %glr
74 - %location & %pure & %yacc & %parse-param. */
75 static void yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])
76 AT_PARAM_IF([semantic_value *result, int *count, ])
79 static int yylex (]AT_LEX_FORMALS[);
80 static int yygetc (]AT_LEX_FORMALS[);
81 static void yyungetc (]AT_LEX_PRE_FORMALS[ int c);
84 /* Bison Declarations */
85 %token CALC_EOF 0 "end of input"
86 %token <ival> NUM "number"
89 %nonassoc '=' /* comparison */
92 %left NEG /* negation--unary minus */
93 %right '^' /* exponentiation */
99 | input line { ]AT_PARAM_IF([++*count; ++global_count;])[ }
104 | exp '\n' { ]AT_PARAM_IF([*result = global_result = $1;])[ }
112 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
115 | exp '+' exp { $$ = $1 + $3; }
116 | exp '-' exp { $$ = $1 - $3; }
117 | exp '*' exp { $$ = $1 * $3; }
118 | exp '/' exp { $$ = $1 / $3; }
119 | '-' exp %prec NEG { $$ = -$2; }
120 | exp '^' exp { $$ = power ($1, $3); }
121 | '(' exp ')' { $$ = $2; }
122 | '(' error ')' { $$ = 1111; }
124 | '-' error { YYERROR; }
131 [/* A C++ error reporting function. */
133 yy::parser::error (const location& l, const std::string& m)
136 std::cerr << AT_LOCATION_IF([l << ": " << ])m << std::endl;
140 yyparse (AT_PARAM_IF([semantic_value *result, int *count]))
142 yy::parser parser[]AT_PARAM_IF([ (result, count)]);
143 parser.set_debug_level (!!YYDEBUG);
144 return parser.parse ();
148 yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])
149 AT_PARAM_IF([semantic_value *result, int *count, ])
152 AT_PARAM_IF([(void) result; (void) count;])
153 AT_YYERROR_SEES_LOC_IF([
154 fprintf (stderr, "%d.%d",
155 AT_LOC.first_line, AT_LOC.first_column);
156 if (AT_LOC.first_line != AT_LOC.last_line)
157 fprintf (stderr, "-%d.%d",
158 AT_LOC.last_line, AT_LOC.last_column - 1);
159 else if (AT_LOC.first_column != AT_LOC.last_column - 1)
160 fprintf (stderr, "-%d",
161 AT_LOC.last_column - 1);
162 fprintf (stderr, ": ");])
163 fprintf (stderr, "%s\n", s);
168 static YYLTYPE last_yylloc;
171 yygetc (]AT_LEX_FORMALS[)
173 int res = getc (yyin);
176 last_yylloc = AT_LOC;
181 AT_LOC.end.column = 0;],
182 [ AT_LOC.last_line++;
183 AT_LOC.last_column = 0;])
187 [ AT_LOC.end.column++;],
188 [ AT_LOC.last_column++;])
195 yyungetc (]AT_LEX_PRE_FORMALS[ int c)
199 /* Wrong when C == `\n'. */
200 AT_LOC = last_yylloc;
206 read_signed_integer (]AT_LEX_FORMALS[)
208 int c = yygetc (]AT_LEX_ARGS[);
215 c = yygetc (]AT_LEX_ARGS[);
221 n = 10 * n + (c - '0');
222 c = yygetc (]AT_LEX_ARGS[);
225 yyungetc (]AT_LEX_PRE_ARGS[ c);
232 /*---------------------------------------------------------------.
233 | Lexical analyzer returns an integer on the stack and the token |
234 | NUM, or the ASCII character read if not a number. Skips all |
235 | blanks and tabs, returns 0 for EOF. |
236 `---------------------------------------------------------------*/
239 yylex (]AT_LEX_FORMALS[)
249 AT_LOC.last_column = 0;
250 AT_LOC.last_line = 1;
254 ]AT_LOCATION_IF([AT_LALR1_CC_IF(
255 [ AT_LOC.begin = AT_LOC.end;],
256 [ AT_LOC.first_column = AT_LOC.last_column;
257 AT_LOC.first_line = AT_LOC.last_line;
260 /* Skip white space. */
261 while ((c = yygetc (]AT_LEX_ARGS[)) == ' ' || c == '\t')
263 ]AT_LOCATION_IF([AT_LALR1_CC_IF(
264 [ AT_LOC.begin = AT_LOC.end;],
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 yyungetc (]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 yyin = 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 = 0])
344 # ------------------------------------------------------------
345 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
347 # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
348 # NUM-STDERR-LINES is the number of expected lines on stderr.
350 # We don't count GLR's traces yet, since its traces are somewhat
351 # different from LALR's.
352 m4_define([_AT_CHECK_CALC],
356 AT_PARSER_CHECK([./calc input], 0, [], [stderr])
358 [%debug.*%glr\|%glr.*%debug],
361 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
366 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT,
368 # [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
369 # ---------------------------------------------------------
370 # Run `calc' on INPUT, and expect a `syntax error' message.
372 # If INPUT starts with a slash, it is used as absolute input file name,
373 # otherwise as contents.
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 [%debug.*%glr\|%glr.*%debug],
395 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$4
398 # Normalize the observed and expected error messages, depending upon the
400 # 1. Remove the traces from observed.
412 /^yydestructor:/d' stderr >at-stderr
414 # 2. Create the reference error message.
418 # 3. If locations are not used, remove them.
419 AT_YYERROR_SEES_LOC_IF([],
420 [[sed 's/^[-0-9.]*: //' expout >at-expout
421 mv at-expout expout]])
422 # 4. If error-verbose is not used, strip the`, unexpected....' part.
423 m4_bmatch([$1], [%error-verbose], [],
424 [[sed 's/syntax error, .*$/syntax error/' expout >at-expout
425 mv at-expout expout]])
427 AT_CHECK([cat stderr], 0, [expout])
431 # AT_CHECK_CALC([BISON-OPTIONS [, EXPECTED-TO-FAIL]])
432 # ------------------------------
433 # Start a testing chunk which compiles `calc' grammar with
434 # BISON-OPTIONS, and performs several tests over the parser.
435 # However, if EXPECTED-TO-FAIL is nonempty, this test is expected to fail.
436 m4_define([AT_CHECK_CALC],
437 [# We use integers to avoid dependencies upon the precision of doubles.
438 AT_SETUP([Calculator $1])
440 m4_ifval([$2], [AT_CHECK([exit 77])])
442 AT_BISON_OPTION_PUSHDEFS([$1])
447 [AT_CHECK([bison -o calc.cc calc.y])
448 AT_COMPILE_CXX([calc])],
449 [AT_CHECK([bison -o calc.c calc.y])
452 # Test the priorities.
469 # Some syntax errors.
470 _AT_CHECK_CALC_ERROR([$1], [1], [0 0], [13],
471 [1.2: syntax error, unexpected number])
472 _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [18],
473 [1.2: syntax error, unexpected '/', expecting number or '-' or '(' or '!'])
474 _AT_CHECK_CALC_ERROR([$1], [1], [error], [5],
475 [1.0: syntax error, unexpected $undefined])
476 _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [26],
477 [1.6: syntax error, unexpected '='])
478 _AT_CHECK_CALC_ERROR([$1], [1],
482 [2.0: syntax error, unexpected '+'])
483 # Exercise error messages with EOF: work on an empty file.
484 _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4],
485 [1.0: syntax error, unexpected end of input])
487 # Exercise the error token: without it, we die at the first error,
490 # - have several errors which exercise different shift/discardings
491 # - (): nothing to pop, nothing to discard
492 # - (1 + 1 + 1 +): a lot to pop, nothing to discard
493 # - (* * *): nothing to pop, a lot to discard
494 # - (1 + 2 * *): some to pop and discard
496 # - test the action associated to `error'
498 # - check the look-ahead that triggers an error is not discarded
499 # when we enter error recovery. Below, the look-ahead causing the
500 # first error is ")", which is needed to recover from the error and
501 # produce the "0" that triggers the "0 != 1" error.
503 _AT_CHECK_CALC_ERROR([$1], [0],
504 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
506 [1.1: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
507 1.17: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
508 1.22: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
509 1.40: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
510 calc: error: 4444 != 1])
512 # The same, but this time exercising explicitly triggered syntax errors.
513 # POSIX says the look-ahead causing the error should not be discarded.
514 _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [76],
515 [1.9: syntax error, unexpected number
516 calc: error: 2222 != 1])
517 _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (0 0) = 1], [86],
518 [1.3: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
519 1.11: syntax error, unexpected number
520 calc: error: 2222 != 1])
521 AT_BISON_OPTION_POPDEFS
529 # ------------------------ #
530 # Simple LALR Calculator. #
531 # ------------------------ #
533 AT_BANNER([[Simple LALR Calculator.]])
535 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
536 # -----------------------------------
537 # Start a testing chunk which compiles `calc' grammar with
538 # BISON-OPTIONS, and performs several tests over the parser.
539 m4_define([AT_CHECK_CALC_LALR],
544 AT_CHECK_CALC_LALR([%defines])
545 AT_CHECK_CALC_LALR([%locations])
546 AT_CHECK_CALC_LALR([%name-prefix="calc"])
547 AT_CHECK_CALC_LALR([%verbose])
548 AT_CHECK_CALC_LALR([%yacc])
549 AT_CHECK_CALC_LALR([%error-verbose])
551 AT_CHECK_CALC_LALR([%pure-parser %locations])
552 AT_CHECK_CALC_LALR([%error-verbose %locations])
554 AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
556 AT_CHECK_CALC_LALR([%debug])
557 AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
559 AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
561 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}])
564 # ----------------------- #
565 # Simple GLR Calculator. #
566 # ----------------------- #
568 AT_BANNER([[Simple GLR Calculator.]])
570 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
571 # ----------------------------------
572 # Start a testing chunk which compiles `calc' grammar with
573 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
574 m4_define([AT_CHECK_CALC_GLR],
575 [AT_CHECK_CALC([%glr-parser] $@)])
580 AT_CHECK_CALC_GLR([%defines])
581 AT_CHECK_CALC_GLR([%locations])
582 AT_CHECK_CALC_GLR([%name-prefix="calc"])
583 AT_CHECK_CALC_GLR([%verbose])
584 AT_CHECK_CALC_GLR([%yacc])
585 AT_CHECK_CALC_GLR([%error-verbose])
587 AT_CHECK_CALC_GLR([%pure-parser %locations])
588 AT_CHECK_CALC_GLR([%error-verbose %locations])
590 AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
592 AT_CHECK_CALC_GLR([%debug])
593 AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
595 AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
597 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}])
600 # ----------------------------- #
601 # Simple LALR1 C++ Calculator. #
602 # ----------------------------- #
604 AT_BANNER([[Simple LALR1 C++ Calculator.]])
606 # AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS])
607 # ---------------------------------------
608 # Start a testing chunk which compiles `calc' grammar with
609 # the C++ skeleton, and performs several tests over the parser.
610 m4_define([AT_CHECK_CALC_LALR1_CC],
611 [AT_CHECK_CALC([%skeleton "lalr1.cc"] $@)])
613 # AT_CHECK_CALC_LALR1_CC()
615 AT_CHECK_CALC_LALR1_CC([%defines %locations])
617 AT_CHECK_CALC_LALR1_CC([%defines])
618 # AT_CHECK_CALC_LALR1_CC([%locations])
619 # AT_CHECK_CALC_LALR1_CC([%name-prefix="calc"])
620 # AT_CHECK_CALC_LALR1_CC([%verbose])
621 # AT_CHECK_CALC_LALR1_CC([%yacc])
622 # AT_CHECK_CALC_LALR1_CC([%error-verbose])
624 # AT_CHECK_CALC_LALR1_CC([%pure-parser %locations])
625 # AT_CHECK_CALC_LALR1_CC([%error-verbose %locations])
627 AT_CHECK_CALC_LALR1_CC([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
629 # AT_CHECK_CALC_LALR1_CC([%debug])
630 AT_CHECK_CALC_LALR1_CC([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
632 AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
634 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}])