1 # Checking the output filenames. -*- Autotest -*-
2 # Copyright (C) 2000, 2001, 2002 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);
65 /* Exercise pre-prologue dependency to %union. */
70 /* Exercise M4 quoting: '@:>@@:>@', 0. */
72 /* Also exercise %union. */
75 value_t ival; /* A comment to exercise an old bug. */
78 /* Exercise post-prologue dependency to %union. */
80 static void id (YYSTYPE *lval);
82 /* Exercise quotes in declarations. */
83 char quote[] = "@:>@@:>@,";
86 /* Bison Declarations */
87 %token CALC_EOF 0 "end of file"
88 %token <ival> NUM "number"
91 /* Exercise quotes in strings. */
92 %token FAKE "fake @>:@@>:@,"
94 %nonassoc '=' /* comparison */
97 %left NEG /* negation--unary minus */
98 %right '^' /* exponentiation */
102 /* Grammar follows */
113 /* Exercise quotes in braces. */
114 char tmp[] = "@>:@@:>@,";
118 /* Exercise M4 quoting: '@:>@@:>@', 1. */
124 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
127 | exp '+' exp { $$ = $1 + $3; }
128 | exp '-' exp { $$ = $1 - $3; }
129 | exp '*' exp { $$ = $1 * $3; }
130 | exp '/' exp { $$ = $1 / $3; }
131 | '-' exp %prec NEG { $$ = -$2; }
132 | exp '^' exp { $$ = power ($1, $3); }
133 | '(' exp ')' { $$ = $2; }
134 | '(' error ')' { $$ = 0; }
140 /* Exercise M4 quoting: '@:>@@:>@', 2. */
142 yyerror (const char *s)
145 fprintf (stderr, "%d.%d-%d.%d: ",
146 yylloc.first_line, yylloc.first_column,
147 yylloc.last_line, yylloc.last_column);
149 fprintf (stderr, "%s\n", s);
154 static YYLTYPE last_yylloc;
159 int res = getc (yyin);
161 last_yylloc = yylloc;
165 yylloc.last_column = 1;
168 yylloc.last_column++;
178 /* Wrong when C == `\n'. */
179 yylloc = last_yylloc;
185 read_signed_integer (void)
199 n = 10 * n + (c - '0');
210 /*---------------------------------------------------------------.
211 | Lexical analyzer returns an integer on the stack and the token |
212 | NUM, or the ASCII character read if not a number. Skips all |
213 | blanks and tabs, returns 0 for EOF. |
214 `---------------------------------------------------------------*/
222 yylloc.first_column = yylloc.last_column;
223 yylloc.first_line = yylloc.last_line;
226 /* Skip white space. */
227 while ((c = yygetc ()) == ' ' || c == '\t')
230 yylloc.first_column = yylloc.last_column;
231 yylloc.first_line = yylloc.last_line;
235 /* process numbers */
236 if (c == '.' || isdigit (c))
239 yylval.ival = read_signed_integer ();
243 /* Return end-of-file. */
247 /* Return single chars. */
252 power (int base, int exponent)
257 for (/* Niente */; exponent; --exponent)
268 main (int argc, const char **argv)
273 yyin = fopen (argv[1], "r");
287 yylloc.last_column = 1;
288 yylloc.last_line = 1;
297 # AT_DATA_CALC_Y([BISON-OPTIONS])
298 # -------------------------------
300 m4_define([AT_DATA_CALC_Y],
301 [_AT_DATA_CALC_Y($[1], $[2], $[3],
302 [m4_bmatch([$1], [--yyerror-verbose],
303 [[%error-verbose]])])])
307 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
308 # ------------------------------------------------------------
309 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
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],
317 AT_CHECK([./calc input], 0, [], [stderr])dnl
318 AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0,
319 [m4_bmatch([$1], [--debug],
325 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, [NUM-DEBUG-LINES],
326 # [ERROR-LOCATION], [IF-YYERROR-VERBOSE])
327 # ------------------------------------------------------------
328 # Run `calc' on INPUT, and expect a `parse error' message.
330 # If INPUT starts with a slash, it is used as absolute input file name,
331 # otherwise as contents.
333 # If BISON-OPTIONS contains `--location', then make sure the ERROR-LOCATION
334 # is correctly output on stderr.
336 # If BISON-OPTIONS contains `--yyerror-verbose', then make sure the
337 # IF-YYERROR-VERBOSE message is properly output after `parse error, '
340 # If BISON-OPTIONS contains `--debug', then NUM-STDERR-LINES is the number
341 # of expected lines on stderr.
342 m4_define([_AT_CHECK_CALC_ERROR],
343 [m4_bmatch([$2], [^/],
344 [AT_CHECK([./calc $2], 0, [], [stderr])],
348 AT_CHECK([./calc input], 0, [], [stderr])])
350 m4_bmatch([$1], [--debug],
351 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
354 # Normalize the observed and expected error messages, depending upon the
356 # 1. Remove the traces from observed.
357 egrep -v '^((Start|Enter|Read|Reduc|Shift)ing|state|Error:|Next|Discarding) ' stderr >at-stderr
359 # 2. Create the reference error message.
363 # 3. If locations are not used, remove them.
364 m4_bmatch([$1], [--location], [],
365 [[sed 's/^[-0-9.]*: //' expout >at-expout
366 mv at-expout expout]])
367 # 4. If error-verbose is not used, strip the`, unexpected....' part.
368 m4_bmatch([$1], [--yyerror-verbose], [],
369 [[sed 's/parse error, .*$/parse error/' expout >at-expout
370 mv at-expout expout]])
372 AT_CHECK([cat stderr], 0, [expout])
376 # AT_CHECK_CALC([BISON-OPTIONS], [PARSER-EXPECTED-STDERR])
377 # --------------------------------------------------------
378 # Start a testing chunk which compiles `calc' grammar with
379 # BISON-OPTIONS, and performs several tests over the parser.
380 m4_define([AT_CHECK_CALC],
381 [# We use integers to avoid dependencies upon the precision of doubles.
382 AT_SETUP([Calculator $1])
386 # Specify the output files to avoid problems on different file systems.
387 AT_CHECK([bison calc.y -o calc.c m4_bpatsubst([$1], [--yyerror-verbose])],
390 AT_CHECK([$CC $CFLAGS $CPPFLAGS calc.c -o calc], 0, [], [ignore])
392 # Test the priorities.
406 (2^2)^3 = 64], [486])
409 _AT_CHECK_CALC_ERROR([$1], [0 0], [11],
410 [1.3-1.4: parse error, unexpected "number"])
411 _AT_CHECK_CALC_ERROR([$1], [1//2], [15],
412 [1.3-1.4: parse error, unexpected '/', expecting "number" or '-' or '('])
413 _AT_CHECK_CALC_ERROR([$1], [error], [4],
414 [1.1-1.2: parse error, unexpected $undefined., expecting "number" or '-' or '\n' or '('])
415 _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [22],
416 [1.7-1.8: parse error, unexpected '='])
417 _AT_CHECK_CALC_ERROR([$1],
421 [2.1-2.2: parse error, unexpected '+'])
422 # Exercise error messages with EOF: work on an empty file.
423 _AT_CHECK_CALC_ERROR([$1],
426 [1.1-1.2: parse error, unexpected "end of file", expecting "number" or '-' or '\n' or '('])
428 # Exercise the error token: without it, we die at the first error,
429 # hence be sure i. to have several errors, ii. to test the action
430 # associated to `error'.
431 _AT_CHECK_CALC_ERROR([$1],
432 [(1 ++ 2) + (0 0) = 1],
434 [1.5-1.6: parse error, unexpected '+', expecting "number" or '-' or '('
435 1.15-1.16: parse error, unexpected "number"
436 calc: error: 0 != 1])
438 # Add a studid example demonstrating that Bison can further improve the
439 # error message. FIXME: Fix this ridiculous message.
440 _AT_CHECK_CALC_ERROR([$1],
443 [1.2-1.3: parse error, unexpected ')', expecting error or "number" or '-' or '('])
451 # ------------------ #
452 # Test the parsers. #
453 # ------------------ #
457 AT_CHECK_CALC([--defines])
458 AT_CHECK_CALC([--locations])
459 AT_CHECK_CALC([--name-prefix=calc])
460 AT_CHECK_CALC([--verbose])
461 AT_CHECK_CALC([--yacc])
462 AT_CHECK_CALC([--yyerror-verbose])
464 AT_CHECK_CALC([--locations --yyerror-verbose])
466 AT_CHECK_CALC([--defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])
468 AT_CHECK_CALC([--debug])
469 AT_CHECK_CALC([--debug --defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])