1 # Simple calculator. -*- Autotest -*-
2 # Copyright (C) 2000, 2001, 2002, 2003 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 */
52 # define alarm(seconds) /* empty */
56 extern void perror (const char *s);
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);
73 ]AT_LALR1_CC_IF([typedef yy::Location YYLTYPE;],
74 [/* yyerror receives the location if:
75 - %location & %pure & %glr
76 - %location & %pure & %yacc & %parse-param. */
77 static void yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])
78 AT_PARAM_IF([semantic_value *result, int *count, ])
81 static int yylex (]AT_LEX_FORMALS[);
82 static int yygetc (]AT_LEX_FORMALS[);
83 static void yyungetc (]AT_LEX_PRE_FORMALS[ int c);
86 /* Bison Declarations */
87 %token CALC_EOF 0 "end of input"
88 %token <ival> NUM "number"
91 %nonassoc '=' /* comparison */
94 %left NEG /* negation--unary minus */
95 %right '^' /* exponentiation */
101 | input line { ]AT_PARAM_IF([++*count; ++global_count;])[ }
106 | exp '\n' { ]AT_PARAM_IF([*result = global_result = $1;])[ }
114 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
117 | exp '+' exp { $$ = $1 + $3; }
118 | exp '-' exp { $$ = $1 - $3; }
119 | exp '*' exp { $$ = $1 * $3; }
120 | exp '/' exp { $$ = $1 / $3; }
121 | '-' exp %prec NEG { $$ = -$2; }
122 | exp '^' exp { $$ = power ($1, $3); }
123 | '(' exp ')' { $$ = $2; }
124 | '(' error ')' { $$ = 1111; }
126 | '-' error { YYERROR; }
133 [/* Currently, print_ is required in C++. */
135 yy::Parser::print_ ()
138 std::cerr << location;])
141 /* A C++ error reporting function. */
143 yy::Parser::error_ ()
145 std::cerr << AT_LOCATION_IF([location << ": " << ])message << std::endl;
149 yyparse (AT_PARAM_IF([semantic_value *result, int *count]))
151 yy::Parser parser (!!YYDEBUG[]AT_LOCATION_IF([, yy::Location::Location ()])AT_PARAM_IF([, result, count]));
152 return parser.parse ();
156 yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])
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 yygetc (]AT_LEX_FORMALS[)
181 int res = getc (yyin);
184 last_yylloc = AT_LOC;
189 AT_LOC.end.column = 0;],
190 [ AT_LOC.last_line++;
191 AT_LOC.last_column = 0;])
195 [ AT_LOC.end.column++;],
196 [ AT_LOC.last_column++;])
203 yyungetc (]AT_LEX_PRE_FORMALS[ int c)
207 /* Wrong when C == `\n'. */
208 AT_LOC = last_yylloc;
214 read_signed_integer (]AT_LEX_FORMALS[)
216 int c = yygetc (]AT_LEX_ARGS[);
223 c = yygetc (]AT_LEX_ARGS[);
229 n = 10 * n + (c - '0');
230 c = yygetc (]AT_LEX_ARGS[);
233 yyungetc (]AT_LEX_PRE_ARGS[ c);
240 /*---------------------------------------------------------------.
241 | Lexical analyzer returns an integer on the stack and the token |
242 | NUM, or the ASCII character read if not a number. Skips all |
243 | blanks and tabs, returns 0 for EOF. |
244 `---------------------------------------------------------------*/
247 yylex (]AT_LEX_FORMALS[)
257 AT_LOC.last_column = 0;
258 AT_LOC.last_line = 1;
262 ]AT_LOCATION_IF([AT_LALR1_CC_IF(
263 [ AT_LOC.begin = AT_LOC.end;],
264 [ AT_LOC.first_column = AT_LOC.last_column;
265 AT_LOC.first_line = AT_LOC.last_line;
268 /* Skip white space. */
269 while ((c = yygetc (]AT_LEX_ARGS[)) == ' ' || c == '\t')
271 ]AT_LOCATION_IF([AT_LALR1_CC_IF(
272 [ AT_LOC.begin = AT_LOC.end;],
273 [ AT_LOC.first_column = AT_LOC.last_column;
274 AT_LOC.first_line = AT_LOC.last_line;
278 /* process numbers */
279 if (c == '.' || isdigit (c))
281 yyungetc (]AT_LEX_PRE_ARGS[ c);
282 ]AT_VAL[.ival = read_signed_integer (]AT_LEX_ARGS[);
286 /* Return end-of-file. */
290 /* Return single chars. */
295 power (int base, int exponent)
300 for (/* Niente */; exponent; --exponent)
307 main (int argc, const char **argv)
309 semantic_value result = 0;
315 yyin = fopen (argv[1], "r");
325 ]AT_LALR1_CC_IF([], [m4_bmatch([$4], [%debug],
327 status = yyparse (]AT_PARAM_IF([&result, &count])[);
328 if (global_result != result)
330 if (global_count != count)
338 # AT_DATA_CALC_Y([BISON-OPTIONS])
339 # -------------------------------
341 m4_define([AT_DATA_CALC_Y],
342 [_AT_DATA_CALC_Y($[1], $[2], $[3], [$1])
347 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
348 # ------------------------------------------------------------
349 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
351 # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
352 # NUM-STDERR-LINES is the number of expected lines on stderr.
354 # We don't count GLR's traces yet, since its traces are somewhat
355 # different from LALR's.
356 m4_define([_AT_CHECK_CALC],
360 AT_PARSER_CHECK([./calc input], 0, [], [stderr])
362 [%debug.*%glr\|%glr.*%debug],
365 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
370 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT,
372 # [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
373 # ---------------------------------------------------------
374 # Run `calc' on INPUT, and expect a `syntax error' message.
376 # If INPUT starts with a slash, it is used as absolute input file name,
377 # otherwise as contents.
379 # If BISON-OPTIONS contains `%location', then make sure the ERROR-LOCATION
380 # is correctly output on stderr.
382 # If BISON-OPTIONS contains `%error-verbose', then make sure the
383 # IF-YYERROR-VERBOSE message is properly output after `syntax error, '
386 # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
387 # is the number of expected lines on stderr.
388 m4_define([_AT_CHECK_CALC_ERROR],
389 [m4_bmatch([$3], [^/],
390 [AT_PARSER_CHECK([./calc $3], $2, [], [stderr])],
394 AT_PARSER_CHECK([./calc input], $2, [], [stderr])])
396 [%debug.*%glr\|%glr.*%debug],
399 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$4
402 # Normalize the observed and expected error messages, depending upon the
404 # 1. Remove the traces from observed.
415 /^yydestructor:/d' stderr >at-stderr
417 # 2. Create the reference error message.
421 # 3. If locations are not used, remove them.
422 AT_YYERROR_SEES_LOC_IF([],
423 [[sed 's/^[-0-9.]*: //' expout >at-expout
424 mv at-expout expout]])
425 # 4. If error-verbose is not used, strip the`, unexpected....' part.
426 m4_bmatch([$1], [%error-verbose], [],
427 [[sed 's/syntax error, .*$/syntax error/' expout >at-expout
428 mv at-expout expout]])
430 AT_CHECK([cat stderr], 0, [expout])
434 # AT_CHECK_CALC([BISON-OPTIONS [, EXPECTED-TO-FAIL]])
435 # ------------------------------
436 # Start a testing chunk which compiles `calc' grammar with
437 # BISON-OPTIONS, and performs several tests over the parser.
438 # However, if EXPECTED-TO-FAIL is nonempty, this test is expected to fail.
439 m4_define([AT_CHECK_CALC],
440 [# We use integers to avoid dependencies upon the precision of doubles.
441 AT_SETUP([Calculator $1])
443 m4_ifval([$2], [AT_CHECK([exit 77])])
445 AT_BISON_OPTION_PUSHDEFS([$1])
449 # Specify the output files to avoid problems on different file systems.
450 AT_CHECK([bison -o calc.c calc.y])
452 AT_LALR1_CC_IF([AT_COMPILE_CXX([calc])],
453 [AT_COMPILE([calc])])
455 # Test the priorities.
472 # Some syntax errors.
473 _AT_CHECK_CALC_ERROR([$1], [1], [0 0], [11],
474 [1.2: syntax error, unexpected "number"])
475 _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [15],
476 [1.2: syntax error, unexpected '/', expecting "number" or '-' or '(' or '!'])
477 _AT_CHECK_CALC_ERROR([$1], [1], [error], [4],
478 [1.0: syntax error, unexpected $undefined])
479 _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [22],
480 [1.6: syntax error, unexpected '='])
481 _AT_CHECK_CALC_ERROR([$1], [1],
485 [2.0: syntax error, unexpected '+'])
486 # Exercise error messages with EOF: work on an empty file.
487 _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4],
488 [1.0: syntax error, unexpected "end of input"])
490 # Exercise the error token: without it, we die at the first error,
493 # - have several errors which exercise different shift/discardings
494 # - (): nothing to pop, nothing to discard
495 # - (1 + 1 + 1 +): a lot to pop, nothing to discard
496 # - (* * *): nothing to pop, a lot to discard
497 # - (1 + 2 * *): some to pop and discard
499 # - test the action associated to `error'
501 # - check the lookahead that triggers an error is not discarded
502 # when we enter error recovery. Below, the lookahead causing the
503 # first error is ")", which is needed to recover from the error and
504 # produce the "0" that triggers the "0 != 1" error.
506 _AT_CHECK_CALC_ERROR([$1], [0],
507 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
509 [1.1: syntax error, unexpected ')', expecting "number" or '-' or '(' or '!'
510 1.17: syntax error, unexpected ')', expecting "number" or '-' or '(' or '!'
511 1.22: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!'
512 1.40: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!'
513 calc: error: 4444 != 1])
515 # The same, but this time exercising explicitly triggered syntax errors.
516 # POSIX says the lookahead causing the error should not be discarded.
517 _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [62],
518 [1.9: syntax error, unexpected "number"
519 calc: error: 2222 != 1])
520 _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (0 0) = 1], [70],
521 [1.3: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!'
522 1.11: syntax error, unexpected "number"
523 calc: error: 2222 != 1])
524 AT_BISON_OPTION_POPDEFS
532 # ------------------------ #
533 # Simple LALR Calculator. #
534 # ------------------------ #
536 AT_BANNER([[Simple LALR Calculator.]])
538 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
539 # -----------------------------------
540 # Start a testing chunk which compiles `calc' grammar with
541 # BISON-OPTIONS, and performs several tests over the parser.
542 m4_define([AT_CHECK_CALC_LALR],
547 AT_CHECK_CALC_LALR([%defines])
548 AT_CHECK_CALC_LALR([%locations])
549 AT_CHECK_CALC_LALR([%name-prefix="calc"])
550 AT_CHECK_CALC_LALR([%verbose])
551 AT_CHECK_CALC_LALR([%yacc])
552 AT_CHECK_CALC_LALR([%error-verbose])
554 AT_CHECK_CALC_LALR([%pure-parser %locations])
555 AT_CHECK_CALC_LALR([%error-verbose %locations])
557 AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
559 AT_CHECK_CALC_LALR([%debug])
560 AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
562 AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
564 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}])
567 # ----------------------- #
568 # Simple GLR Calculator. #
569 # ----------------------- #
571 AT_BANNER([[Simple GLR Calculator.]])
573 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
574 # ----------------------------------
575 # Start a testing chunk which compiles `calc' grammar with
576 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
577 m4_define([AT_CHECK_CALC_GLR],
578 [AT_CHECK_CALC([%glr-parser] $@)])
583 AT_CHECK_CALC_GLR([%defines])
584 AT_CHECK_CALC_GLR([%locations])
585 AT_CHECK_CALC_GLR([%name-prefix="calc"])
586 AT_CHECK_CALC_GLR([%verbose])
587 AT_CHECK_CALC_GLR([%yacc])
588 AT_CHECK_CALC_GLR([%error-verbose])
590 AT_CHECK_CALC_GLR([%pure-parser %locations])
591 AT_CHECK_CALC_GLR([%error-verbose %locations])
593 AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
595 AT_CHECK_CALC_GLR([%debug])
596 AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
598 AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
600 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}])
603 # ----------------------------- #
604 # Simple LALR1 C++ Calculator. #
605 # ----------------------------- #
607 AT_BANNER([[Simple LALR1 C++ Calculator.]])
609 # AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS])
610 # ---------------------------------------
611 # Start a testing chunk which compiles `calc' grammar with
612 # the C++ skeleton, and performs several tests over the parser.
613 m4_define([AT_CHECK_CALC_LALR1_CC],
614 [AT_CHECK_CALC([%skeleton "lalr1.cc"] $@, [expected to fail])])
616 # AT_CHECK_CALC_LALR1_CC()
618 AT_CHECK_CALC_LALR1_CC([%defines %locations])
620 AT_CHECK_CALC_LALR1_CC([%defines])
621 # AT_CHECK_CALC_LALR1_CC([%locations])
622 # AT_CHECK_CALC_LALR1_CC([%name-prefix="calc"])
623 # AT_CHECK_CALC_LALR1_CC([%verbose])
624 # AT_CHECK_CALC_LALR1_CC([%yacc])
625 # AT_CHECK_CALC_LALR1_CC([%error-verbose])
627 # AT_CHECK_CALC_LALR1_CC([%pure-parser %locations])
628 # AT_CHECK_CALC_LALR1_CC([%error-verbose %locations])
630 AT_CHECK_CALC_LALR1_CC([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
632 # AT_CHECK_CALC_LALR1_CC([%debug])
633 AT_CHECK_CALC_LALR1_CC([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
635 AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
637 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}])