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);
57 static int power (int base, int exponent);
58 static void yyerror (const char *s);
59 static int yylex (void);
60 static int yygetc (void);
61 static void yyungetc (int c);
63 extern void perror (const char *s);
66 /* Also exercise %union. */
69 int ival; /* A comment to exercise an old bug. */
72 /* Bison Declarations */
73 %token CALC_EOF 0 "end of file"
74 %token <ival> NUM "number"
77 %nonassoc '=' /* comparison */
80 %left NEG /* negation--unary minus */
81 %right '^' /* exponentiation */
102 printf ("calc: error: %d != %d\n", $1, $3);
105 | exp '+' exp { $$ = $1 + $3; }
106 | exp '-' exp { $$ = $1 - $3; }
107 | exp '*' exp { $$ = $1 * $3; }
108 | exp '/' exp { $$ = $1 / $3; }
109 | '-' exp %prec NEG { $$ = -$2; }
110 | exp '^' exp { $$ = power ($1, $3); }
111 | '(' exp ')' { $$ = $2; }
118 yyerror (const char *s)
121 fprintf (stderr, "%d.%d:%d.%d: ",
122 yylloc.first_line, yylloc.first_column,
123 yylloc.last_line, yylloc.last_column);
125 fprintf (stderr, "%s\n", s);
130 static YYLTYPE last_yylloc;
135 int res = getc (yyin);
137 last_yylloc = yylloc;
141 yylloc.last_column = 0;
144 yylloc.last_column++;
154 /* Wrong when C == `\n'. */
155 yylloc = last_yylloc;
161 read_signed_integer (void)
175 n = 10 * n + (c - '0');
186 /*---------------------------------------------------------------.
187 | Lexical analyzer returns an integer on the stack and the token |
188 | NUM, or the ASCII character read if not a number. Skips all |
189 | blanks and tabs, returns 0 for EOF. |
190 `---------------------------------------------------------------*/
198 yylloc.first_column = yylloc.last_column;
199 yylloc.first_line = yylloc.last_line;
202 /* Skip white space. */
203 while ((c = yygetc ()) == ' ' || c == '\t')
206 yylloc.first_column = yylloc.last_column;
207 yylloc.first_line = yylloc.last_line;
211 /* process numbers */
212 if (c == '.' || isdigit (c))
215 yylval.ival = read_signed_integer ();
219 /* Return end-of-file. */
223 /* Return single chars. */
228 power (int base, int exponent)
233 for (/* Niente */; exponent; --exponent)
239 main (int argc, const char **argv)
244 yyin = fopen (argv[1], "r");
258 yylloc.last_column = 0;
259 yylloc.last_line = 1;
268 # AT_DATA_CALC_Y([BISON-OPTIONS])
269 # -------------------------------
271 m4_define([AT_DATA_CALC_Y],
272 [_AT_DATA_CALC_Y($[1], $[2], $[3],
273 [m4_bmatch([$1], [--yyerror-verbose],
274 [[%error-verbose]])])])
278 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
279 # ------------------------------------------------------------
280 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
282 # If BISON-OPTIONS contains `--debug', then NUM-STDERR-LINES is the number
283 # of expected lines on stderr.
284 m4_define([_AT_CHECK_CALC],
288 AT_CHECK([calc input], 0, [], [stderr])dnl
289 AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0,
290 [m4_bmatch([$1], [--debug],
296 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, [NUM-DEBUG-LINES],
297 # [ERROR-LOCATION], [IF-YYERROR-VERBOSE])
298 # ------------------------------------------------------------
299 # Run `calc' on INPUT, and expect a `parse error' message.
301 # If INPUT starts with a slash, it is used as absolute input file name,
302 # otherwise as contents.
304 # If BISON-OPTIONS contains `--location', then make sure the ERROR-LOCATION
305 # is correctly output on stderr.
307 # If BISON-OPTIONS contains `--yyerror-verbose', then make sure the
308 # IF-YYERROR-VERBOSE message is properly output after `parse error, '
311 # If BISON-OPTIONS contains `--debug', then NUM-STDERR-LINES is the number
312 # of expected lines on stderr.
313 m4_define([_AT_CHECK_CALC_ERROR],
314 [m4_bmatch([$2], [^/],
315 [AT_CHECK([calc $2], 0, [], [stderr])],
319 AT_CHECK([calc input], 0, [], [stderr])])
322 AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0,
323 [m4_bmatch([$1], [--debug],
327 egrep -v '^((Start|Enter|Read|Reduc|Shift)ing|state|Error:) ' stderr >at-stderr
330 AT_CHECK([cat stderr], 0,
331 [m4_bmatch([$1], [--location], [$4: ])[]dnl
333 m4_bmatch([$1], [--yyerror-verbose], [, $5])[]dnl
340 # AT_CHECK_CALC([BISON-OPTIONS], [PARSER-EXPECTED-STDERR])
341 # --------------------------------------------------------
342 # Start a testing chunk which compiles `calc' grammar with
343 # BISON-OPTIONS, and performs several tests over the parser.
344 m4_define([AT_CHECK_CALC],
345 [# We use integers to avoid dependencies upon the precision of doubles.
346 AT_SETUP([Calculator $1])
350 # Specify the output files to avoid problems on different file systems.
351 AT_CHECK([bison calc.y -o calc.c m4_bpatsubst([$1], [--yyerror-verbose])],
354 AT_CHECK([$CC $CFLAGS $CPPFLAGS calc.c -o calc], 0, [], [ignore])
356 # Test the priorities.
370 (2^2)^3 = 64], [486])
373 _AT_CHECK_CALC_ERROR([$1], [0 0], [10],
375 [unexpected "number"])
376 _AT_CHECK_CALC_ERROR([$1], [1//2], [13],
378 [unexpected '/', expecting "number" or '-' or '('])
379 _AT_CHECK_CALC_ERROR([$1], [error], [4],
381 [unexpected $undefined., expecting "number" or '-' or '\n' or '('])
382 _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [19],
385 _AT_CHECK_CALC_ERROR([$1],
391 # Exercise error messages with EOF: work on an empty file.
392 _AT_CHECK_CALC_ERROR([$1],
396 [unexpected "end of file", expecting "number" or '-' or '\n' or '('])
404 # ------------------ #
405 # Test the parsers. #
406 # ------------------ #
410 AT_CHECK_CALC([--defines])
411 AT_CHECK_CALC([--locations])
412 AT_CHECK_CALC([--name-prefix=calc])
413 AT_CHECK_CALC([--verbose])
414 AT_CHECK_CALC([--yacc])
415 AT_CHECK_CALC([--yyerror-verbose])
417 AT_CHECK_CALC([--locations --yyerror-verbose])
419 AT_CHECK_CALC([--defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])
421 AT_CHECK_CALC([--debug])
422 AT_CHECK_CALC([--debug --defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])