1 # Simple calculator. -*- Autotest -*-
2 # Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
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 */
50 # define alarm(seconds) /* empty */
54 /* Exercise pre-prologue dependency to %union. */
55 typedef int semantic_value;
57 static semantic_value global_result = 0;
58 static int global_count = 0;
61 /* Exercise %union. */
68 static int power (int base, int exponent);
69 ]AT_LALR1_CC_IF([typedef yy::Location YYLTYPE;],
70 [/* yyerror receives the location if:
71 - %location & %pure & %glr
72 - %location & %pure & %yacc & %parse-param. */
73 static void yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])
74 AT_PARAM_IF([semantic_value *result, int *count, ])
77 static int yylex (]AT_LEX_FORMALS[);
78 static int yygetc (]AT_LEX_FORMALS[);
79 static void yyungetc (]AT_LEX_PRE_FORMALS[ int c);
82 /* Bison Declarations */
83 %token CALC_EOF 0 "end of input"
84 %token <ival> NUM "number"
87 %nonassoc '=' /* comparison */
90 %left NEG /* negation--unary minus */
91 %right '^' /* exponentiation */
97 | input line { ]AT_PARAM_IF([++*count; ++global_count;])[ }
102 | exp '\n' { ]AT_PARAM_IF([*result = global_result = $1;])[ }
110 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
113 | exp '+' exp { $$ = $1 + $3; }
114 | exp '-' exp { $$ = $1 - $3; }
115 | exp '*' exp { $$ = $1 * $3; }
116 | exp '/' exp { $$ = $1 / $3; }
117 | '-' exp %prec NEG { $$ = -$2; }
118 | exp '^' exp { $$ = power ($1, $3); }
119 | '(' exp ')' { $$ = $2; }
120 | '(' error ')' { $$ = 1111; }
122 | '-' error { YYERROR; }
129 [/* A C++ error reporting function. */
131 yy::Parser::error_ ()
133 std::cerr << AT_LOCATION_IF([location << ": " << ])message << std::endl;
137 yyparse (AT_PARAM_IF([semantic_value *result, int *count]))
139 yy::Parser parser[]AT_PARAM_IF([ (result, count)]);
140 parser.set_debug_level (!!YYDEBUG);
141 return parser.parse ();
145 yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])
146 AT_PARAM_IF([semantic_value *result, int *count, ])
149 AT_PARAM_IF([(void) result; (void) count;])
150 AT_YYERROR_SEES_LOC_IF([
151 fprintf (stderr, "%d.%d",
152 AT_LOC.first_line, AT_LOC.first_column);
153 if (AT_LOC.first_line != AT_LOC.last_line)
154 fprintf (stderr, "-%d.%d",
155 AT_LOC.last_line, AT_LOC.last_column - 1);
156 else if (AT_LOC.first_column != AT_LOC.last_column - 1)
157 fprintf (stderr, "-%d",
158 AT_LOC.last_column - 1);
159 fprintf (stderr, ": ");])
160 fprintf (stderr, "%s\n", s);
165 static YYLTYPE last_yylloc;
168 yygetc (]AT_LEX_FORMALS[)
170 int res = getc (yyin);
173 last_yylloc = AT_LOC;
178 AT_LOC.end.column = 0;],
179 [ AT_LOC.last_line++;
180 AT_LOC.last_column = 0;])
184 [ AT_LOC.end.column++;],
185 [ AT_LOC.last_column++;])
192 yyungetc (]AT_LEX_PRE_FORMALS[ int c)
196 /* Wrong when C == `\n'. */
197 AT_LOC = last_yylloc;
203 read_signed_integer (]AT_LEX_FORMALS[)
205 int c = yygetc (]AT_LEX_ARGS[);
212 c = yygetc (]AT_LEX_ARGS[);
218 n = 10 * n + (c - '0');
219 c = yygetc (]AT_LEX_ARGS[);
222 yyungetc (]AT_LEX_PRE_ARGS[ c);
229 /*---------------------------------------------------------------.
230 | Lexical analyzer returns an integer on the stack and the token |
231 | NUM, or the ASCII character read if not a number. Skips all |
232 | blanks and tabs, returns 0 for EOF. |
233 `---------------------------------------------------------------*/
236 yylex (]AT_LEX_FORMALS[)
246 AT_LOC.last_column = 0;
247 AT_LOC.last_line = 1;
251 ]AT_LOCATION_IF([AT_LALR1_CC_IF(
252 [ AT_LOC.begin = AT_LOC.end;],
253 [ AT_LOC.first_column = AT_LOC.last_column;
254 AT_LOC.first_line = AT_LOC.last_line;
257 /* Skip white space. */
258 while ((c = yygetc (]AT_LEX_ARGS[)) == ' ' || c == '\t')
260 ]AT_LOCATION_IF([AT_LALR1_CC_IF(
261 [ AT_LOC.begin = AT_LOC.end;],
262 [ AT_LOC.first_column = AT_LOC.last_column;
263 AT_LOC.first_line = AT_LOC.last_line;
267 /* process numbers */
268 if (c == '.' || isdigit (c))
270 yyungetc (]AT_LEX_PRE_ARGS[ c);
271 ]AT_VAL[.ival = read_signed_integer (]AT_LEX_ARGS[);
275 /* Return end-of-file. */
279 /* Return single chars. */
284 power (int base, int exponent)
289 for (/* Niente */; exponent; --exponent)
296 main (int argc, const char **argv)
298 semantic_value result = 0;
304 yyin = fopen (argv[1], "r");
314 ]AT_LALR1_CC_IF([], [m4_bmatch([$4], [%debug],
316 status = yyparse (]AT_PARAM_IF([&result, &count])[);
317 if (global_result != result)
319 if (global_count != count)
327 # AT_DATA_CALC_Y([BISON-OPTIONS])
328 # -------------------------------
330 m4_define([AT_DATA_CALC_Y],
331 [_AT_DATA_CALC_Y($[1], $[2], $[3], [$1])
336 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
337 # ------------------------------------------------------------
338 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
340 # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
341 # NUM-STDERR-LINES is the number of expected lines on stderr.
343 # We don't count GLR's traces yet, since its traces are somewhat
344 # different from LALR's.
345 m4_define([_AT_CHECK_CALC],
349 AT_PARSER_CHECK([./calc input], 0, [], [stderr])
351 [%debug.*%glr\|%glr.*%debug],
354 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
359 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT,
361 # [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
362 # ---------------------------------------------------------
363 # Run `calc' on INPUT, and expect a `syntax error' message.
365 # If INPUT starts with a slash, it is used as absolute input file name,
366 # otherwise as contents.
368 # If BISON-OPTIONS contains `%location', then make sure the ERROR-LOCATION
369 # is correctly output on stderr.
371 # If BISON-OPTIONS contains `%error-verbose', then make sure the
372 # IF-YYERROR-VERBOSE message is properly output after `syntax error, '
375 # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
376 # is the number of expected lines on stderr.
377 m4_define([_AT_CHECK_CALC_ERROR],
378 [m4_bmatch([$3], [^/],
379 [AT_PARSER_CHECK([./calc $3], $2, [], [stderr])],
383 AT_PARSER_CHECK([./calc input], $2, [], [stderr])])
385 [%debug.*%glr\|%glr.*%debug],
388 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$4
391 # Normalize the observed and expected error messages, depending upon the
393 # 1. Remove the traces from observed.
404 /^yydestructor:/d' stderr >at-stderr
406 # 2. Create the reference error message.
410 # 3. If locations are not used, remove them.
411 AT_YYERROR_SEES_LOC_IF([],
412 [[sed 's/^[-0-9.]*: //' expout >at-expout
413 mv at-expout expout]])
414 # 4. If error-verbose is not used, strip the`, unexpected....' part.
415 m4_bmatch([$1], [%error-verbose], [],
416 [[sed 's/syntax error, .*$/syntax error/' expout >at-expout
417 mv at-expout expout]])
419 AT_CHECK([cat stderr], 0, [expout])
423 # AT_CHECK_CALC([BISON-OPTIONS [, EXPECTED-TO-FAIL]])
424 # ------------------------------
425 # Start a testing chunk which compiles `calc' grammar with
426 # BISON-OPTIONS, and performs several tests over the parser.
427 # However, if EXPECTED-TO-FAIL is nonempty, this test is expected to fail.
428 m4_define([AT_CHECK_CALC],
429 [# We use integers to avoid dependencies upon the precision of doubles.
430 AT_SETUP([Calculator $1])
432 m4_ifval([$2], [AT_CHECK([exit 77])])
434 AT_BISON_OPTION_PUSHDEFS([$1])
439 [AT_CHECK([bison -o calc.cc calc.y])
440 AT_COMPILE_CXX([calc])],
441 [AT_CHECK([bison -o calc.c calc.y])
444 # Test the priorities.
461 # Some syntax errors.
462 _AT_CHECK_CALC_ERROR([$1], [1], [0 0], [13],
463 [1.2: syntax error, unexpected "number"])
464 _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [18],
465 [1.2: syntax error, unexpected '/', expecting "number" or '-' or '(' or '!'])
466 _AT_CHECK_CALC_ERROR([$1], [1], [error], [5],
467 [1.0: syntax error, unexpected $undefined])
468 _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [26],
469 [1.6: syntax error, unexpected '='])
470 _AT_CHECK_CALC_ERROR([$1], [1],
474 [2.0: syntax error, unexpected '+'])
475 # Exercise error messages with EOF: work on an empty file.
476 _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [5],
477 [1.0: syntax error, unexpected "end of input"])
479 # Exercise the error token: without it, we die at the first error,
482 # - have several errors which exercise different shift/discardings
483 # - (): nothing to pop, nothing to discard
484 # - (1 + 1 + 1 +): a lot to pop, nothing to discard
485 # - (* * *): nothing to pop, a lot to discard
486 # - (1 + 2 * *): some to pop and discard
488 # - test the action associated to `error'
490 # - check the look-ahead that triggers an error is not discarded
491 # when we enter error recovery. Below, the look-ahead causing the
492 # first error is ")", which is needed to recover from the error and
493 # produce the "0" that triggers the "0 != 1" error.
495 _AT_CHECK_CALC_ERROR([$1], [0],
496 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
498 [1.1: syntax error, unexpected ')', expecting "number" or '-' or '(' or '!'
499 1.17: syntax error, unexpected ')', expecting "number" or '-' or '(' or '!'
500 1.22: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!'
501 1.40: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!'
502 calc: error: 4444 != 1])
504 # The same, but this time exercising explicitly triggered syntax errors.
505 # POSIX says the look-ahead causing the error should not be discarded.
506 _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [75],
507 [1.9: syntax error, unexpected "number"
508 calc: error: 2222 != 1])
509 _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (0 0) = 1], [85],
510 [1.3: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!'
511 1.11: syntax error, unexpected "number"
512 calc: error: 2222 != 1])
513 AT_BISON_OPTION_POPDEFS
521 # ------------------------ #
522 # Simple LALR Calculator. #
523 # ------------------------ #
525 AT_BANNER([[Simple LALR Calculator.]])
527 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
528 # -----------------------------------
529 # Start a testing chunk which compiles `calc' grammar with
530 # BISON-OPTIONS, and performs several tests over the parser.
531 m4_define([AT_CHECK_CALC_LALR],
536 AT_CHECK_CALC_LALR([%defines])
537 AT_CHECK_CALC_LALR([%locations])
538 AT_CHECK_CALC_LALR([%name-prefix="calc"])
539 AT_CHECK_CALC_LALR([%verbose])
540 AT_CHECK_CALC_LALR([%yacc])
541 AT_CHECK_CALC_LALR([%error-verbose])
543 AT_CHECK_CALC_LALR([%pure-parser %locations])
544 AT_CHECK_CALC_LALR([%error-verbose %locations])
546 AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
548 AT_CHECK_CALC_LALR([%debug])
549 AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
551 AT_CHECK_CALC_LALR([%pure-parser %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 %parse-param {semantic_value *result} %parse-param {int *count}])
556 # ----------------------- #
557 # Simple GLR Calculator. #
558 # ----------------------- #
560 AT_BANNER([[Simple GLR Calculator.]])
562 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
563 # ----------------------------------
564 # Start a testing chunk which compiles `calc' grammar with
565 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
566 m4_define([AT_CHECK_CALC_GLR],
567 [AT_CHECK_CALC([%glr-parser] $@)])
572 AT_CHECK_CALC_GLR([%defines])
573 AT_CHECK_CALC_GLR([%locations])
574 AT_CHECK_CALC_GLR([%name-prefix="calc"])
575 AT_CHECK_CALC_GLR([%verbose])
576 AT_CHECK_CALC_GLR([%yacc])
577 AT_CHECK_CALC_GLR([%error-verbose])
579 AT_CHECK_CALC_GLR([%pure-parser %locations])
580 AT_CHECK_CALC_GLR([%error-verbose %locations])
582 AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
584 AT_CHECK_CALC_GLR([%debug])
585 AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
587 AT_CHECK_CALC_GLR([%pure-parser %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 %parse-param {semantic_value *result} %parse-param {int *count}])
592 # ----------------------------- #
593 # Simple LALR1 C++ Calculator. #
594 # ----------------------------- #
596 AT_BANNER([[Simple LALR1 C++ Calculator.]])
598 # AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS])
599 # ---------------------------------------
600 # Start a testing chunk which compiles `calc' grammar with
601 # the C++ skeleton, and performs several tests over the parser.
602 m4_define([AT_CHECK_CALC_LALR1_CC],
603 [AT_CHECK_CALC([%skeleton "lalr1.cc"] $@)])
605 # AT_CHECK_CALC_LALR1_CC()
607 AT_CHECK_CALC_LALR1_CC([%defines %locations])
609 AT_CHECK_CALC_LALR1_CC([%defines])
610 # AT_CHECK_CALC_LALR1_CC([%locations])
611 # AT_CHECK_CALC_LALR1_CC([%name-prefix="calc"])
612 # AT_CHECK_CALC_LALR1_CC([%verbose])
613 # AT_CHECK_CALC_LALR1_CC([%yacc])
614 # AT_CHECK_CALC_LALR1_CC([%error-verbose])
616 # AT_CHECK_CALC_LALR1_CC([%pure-parser %locations])
617 # AT_CHECK_CALC_LALR1_CC([%error-verbose %locations])
619 AT_CHECK_CALC_LALR1_CC([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
621 # AT_CHECK_CALC_LALR1_CC([%debug])
622 AT_CHECK_CALC_LALR1_CC([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
624 AT_CHECK_CALC_LALR1_CC([%pure-parser %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 %parse-param {semantic_value *result} %parse-param {int *count}])