1 # Checking the output filenames. -*- Autotest -*-
2 # Copyright 2000, 2001 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 AT_BANNER([[Simple Calculator.]])
21 ## ---------------------------------------------------- ##
22 ## Compile the grammar described in the documentation. ##
23 ## ---------------------------------------------------- ##
26 # ------------------------- #
27 # Helping Autotest macros. #
28 # ------------------------- #
31 # _AT_DATA_CALC_Y($1, $2, $3, [CPP-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
41 [[/* Infix notation calculator--calc */
45 /* We don't need a perfect malloc for these tests. */
53 char *strcat(char *dest, const char *src);
58 static int power (int base, int exponent);
59 static void yyerror (const char *s);
60 static int yylex (void);
61 static int yygetc (void);
62 static void yyungetc (int c);
64 extern void perror (const char *s);
67 /* Bison Declarations */
71 %nonassoc '=' /* comparison */
74 %left NEG /* negation--unary minus */
75 %right '^' /* exponentiation */
94 printf ("calc: error: %d != %d\n", $1, $3);
97 | exp '+' exp { $$ = $1 + $3; }
98 | exp '-' exp { $$ = $1 - $3; }
99 | exp '*' exp { $$ = $1 * $3; }
100 | exp '/' exp { $$ = $1 / $3; }
101 | '-' exp %prec NEG { $$ = -$2; }
102 | exp '^' exp { $$ = power ($1, $3); }
103 | '(' exp ')' { $$ = $2; }
110 yyerror (const char *s)
113 fprintf (stderr, "%d.%d:%d.%d: ",
114 yylloc.first_line, yylloc.first_column,
115 yylloc.last_line, yylloc.last_column);
117 fprintf (stderr, "%s\n", s);
123 int res = getc (yyin);
128 yylloc.last_column = 0;
131 yylloc.last_column++;
141 /* Wrong when C == `\n'. */
142 yylloc.last_column--;
148 read_signed_integer (void)
162 n = 10 * n + (c - '0');
173 /*---------------------------------------------------------------.
174 | Lexical analyzer returns an integer on the stack and the token |
175 | NUM, or the ASCII character read if not a number. Skips all |
176 | blanks and tabs, returns 0 for EOF. |
177 `---------------------------------------------------------------*/
185 yylloc.first_column = yylloc.last_column;
186 yylloc.first_line = yylloc.last_line;
189 /* Skip white space. */
190 while ((c = yygetc ()) == ' ' || c == '\t')
193 yylloc.first_column = yylloc.last_column;
194 yylloc.first_line = yylloc.last_line;
198 /* process numbers */
199 if (c == '.' || isdigit (c))
202 yylval = read_signed_integer ();
206 /* Return end-of-file. */
210 /* Return single chars. */
215 power (int base, int exponent)
220 for (/* Niente */; exponent; --exponent)
226 main (int argc, const char **argv)
231 yyin = fopen (argv[1], "r");
245 yylloc.last_column = 0;
246 yylloc.last_line = 1;
255 # AT_DATA_CALC_Y([BISON-OPTIONS])
256 # -------------------------------
258 m4_define([AT_DATA_CALC_Y],
259 [_AT_DATA_CALC_Y($[1], $[2], $[3],
260 [m4_bmatch([$1], [--yyerror-verbose],
261 [[#define YYERROR_VERBOSE]])])])
265 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
266 # ------------------------------------------------------------
267 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
269 # If BISON-OPTIONS contains `--debug', then NUM-STDERR-LINES is the number
270 # of expected lines on stderr.
271 m4_define([_AT_CHECK_CALC],
275 AT_CHECK([calc input], 0, [], [stderr])dnl
276 AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0,
277 [m4_bmatch([$1], [--debug],
283 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, [NUM-DEBUG-LINES],
284 # [ERROR-LOCATION], [IF-YYERROR-VERBOSE])
285 # ------------------------------------------------------------
286 # Run `calc' on INPUT, and expect a `parse error' message.
288 # If BISON-OPTIONS contains `--location', then make sure the ERROR-LOCATION
289 # is correctly output on stderr.
291 # If BISON-OPTIONS contains `--yyerror-verbose', then make sure the
292 # IF-YYERROR-VERBOSE message is properly output after `parse error, '
295 # If BISON-OPTIONS contains `--debug', then NUM-STDERR-LINES is the number
296 # of expected lines on stderr.
297 m4_define([_AT_CHECK_CALC_ERROR],
302 AT_CHECK([calc input], 0, [], [stderr])
305 AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0,
306 [m4_bmatch([$1], [--debug],
310 egrep -v '^((Start|Enter|Read|Reduc|Shift)ing|state|Error:) ' stderr >at-stderr
313 AT_CHECK([cat stderr], 0,
314 [m4_bmatch([$1], [--location], [$4: ])[]dnl
316 m4_bmatch([$1], [--yyerror-verbose], [, $5])[]dnl
323 # AT_CHECK_CALC([BISON-OPTIONS], [PARSER-EXPECTED-STDERR])
324 # --------------------------------------------------------
325 # Start a testing chunk which compiles `calc' grammar with
326 # BISON-OPTIONS, and performs several tests over the parser.
327 m4_define([AT_CHECK_CALC],
328 [# We use integers to avoid dependencies upon the precision of doubles.
329 AT_SETUP([Calculator $1])
333 # Specify the output files to avoid problems on different file systems.
334 AT_CHECK([bison calc.y -o calc.c m4_bpatsubst([$1], [--yyerror-verbose])],
337 # Some compilers issue warnings we don't want to hear about.
338 # Maybe some day we will have proper Autoconf macros to disable these
339 # warnings, but this place is not the right one for that.
340 # So let's keep only GCC warnings, which we know are sane.
341 # Well, that's only part of the story: some assemblers issue warnings
342 # which can be totally useless, and actually polluting. It seems that
343 # the best bet be to completely ignore stderr, but to pass -Werror
345 if test "$GCC" = yes; then
346 CFLAGS="$CFLAGS -Werror"
348 AT_CHECK([$CC $CFLAGS $CPPFLAGS calc.c -o calc], 0, [], [ignore])
350 # Test the priorities.
364 (2^2)^3 = 64], [491])
367 _AT_CHECK_CALC_ERROR([$1], [+1], [8],
370 _AT_CHECK_CALC_ERROR([$1], [1//2], [17],
372 [unexpected '/', expecting NUM or '-' or '('])
373 _AT_CHECK_CALC_ERROR([$1], [error], [8],
375 [unexpected $undefined.])
376 _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [23],
379 _AT_CHECK_CALC_ERROR([$1],
392 # ------------------ #
393 # Test the parsers. #
394 # ------------------ #
398 AT_CHECK_CALC([--defines])
399 AT_CHECK_CALC([--locations])
400 AT_CHECK_CALC([--name-prefix=calc])
401 AT_CHECK_CALC([--verbose])
402 AT_CHECK_CALC([--yacc])
403 AT_CHECK_CALC([--yyerror-verbose])
405 AT_CHECK_CALC([--locations --yyerror-verbose])
407 AT_CHECK_CALC([--defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])
409 AT_CHECK_CALC([--debug])
410 AT_CHECK_CALC([--debug --defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])