| 1 | # Checking the output filenames. -*- Autotest -*- |
| 2 | # Copyright 2000, 2001 Free Software Foundation, Inc. |
| 3 | |
| 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) |
| 7 | # any later version. |
| 8 | |
| 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. |
| 13 | |
| 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 |
| 17 | # 02111-1307, USA. |
| 18 | |
| 19 | AT_BANNER([[Simple Calculator.]]) |
| 20 | |
| 21 | ## ---------------------------------------------------- ## |
| 22 | ## Compile the grammar described in the documentation. ## |
| 23 | ## ---------------------------------------------------- ## |
| 24 | |
| 25 | |
| 26 | # ------------------------- # |
| 27 | # Helping Autotest macros. # |
| 28 | # ------------------------- # |
| 29 | |
| 30 | |
| 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 |
| 40 | AT_DATA([calc.y], |
| 41 | [[/* Infix notation calculator--calc */ |
| 42 | |
| 43 | %{ |
| 44 | #include <config.h> |
| 45 | /* We don't need a perfect malloc for these tests. */ |
| 46 | #undef malloc |
| 47 | #include <stdio.h> |
| 48 | |
| 49 | #if STDC_HEADERS |
| 50 | # include <stdlib.h> |
| 51 | # include <string.h> |
| 52 | #else |
| 53 | char *strcat(char *dest, const char *src); |
| 54 | #endif |
| 55 | #include <ctype.h> |
| 56 | ]$4[ |
| 57 | |
| 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); |
| 63 | |
| 64 | extern void perror (const char *s); |
| 65 | %} |
| 66 | |
| 67 | /* Bison Declarations */ |
| 68 | %token CALC_EOF 0 |
| 69 | %token NUM |
| 70 | |
| 71 | %nonassoc '=' /* comparison */ |
| 72 | %left '-' '+' |
| 73 | %left '*' '/' |
| 74 | %left NEG /* negation--unary minus */ |
| 75 | %right '^' /* exponentiation */ |
| 76 | |
| 77 | /* Grammar follows */ |
| 78 | %% |
| 79 | input: |
| 80 | /* empty string */ |
| 81 | | input line |
| 82 | ; |
| 83 | |
| 84 | line: |
| 85 | '\n' |
| 86 | | exp '\n' |
| 87 | ; |
| 88 | |
| 89 | exp: |
| 90 | NUM { $$ = $1; } |
| 91 | | exp '=' exp |
| 92 | { |
| 93 | if ($1 != $3) |
| 94 | printf ("calc: error: %d != %d\n", $1, $3); |
| 95 | $$ = $1 == $3; |
| 96 | } |
| 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; } |
| 104 | ; |
| 105 | %% |
| 106 | /* The input. */ |
| 107 | FILE *yyin; |
| 108 | |
| 109 | static void |
| 110 | yyerror (const char *s) |
| 111 | { |
| 112 | #if YYLSP_NEEDED |
| 113 | fprintf (stderr, "%d.%d:%d.%d: ", |
| 114 | yylloc.first_line, yylloc.first_column, |
| 115 | yylloc.last_line, yylloc.last_column); |
| 116 | #endif |
| 117 | fprintf (stderr, "%s\n", s); |
| 118 | } |
| 119 | |
| 120 | static int |
| 121 | yygetc (void) |
| 122 | { |
| 123 | int res = getc (yyin); |
| 124 | #if YYLSP_NEEDED |
| 125 | if (res == '\n') |
| 126 | { |
| 127 | yylloc.last_line++; |
| 128 | yylloc.last_column = 0; |
| 129 | } |
| 130 | else |
| 131 | yylloc.last_column++; |
| 132 | #endif |
| 133 | return res; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | static void |
| 138 | yyungetc (int c) |
| 139 | { |
| 140 | #if YYLSP_NEEDED |
| 141 | /* Wrong when C == `\n'. */ |
| 142 | yylloc.last_column--; |
| 143 | #endif |
| 144 | ungetc (c, yyin); |
| 145 | } |
| 146 | |
| 147 | static int |
| 148 | read_signed_integer (void) |
| 149 | { |
| 150 | int c = yygetc (); |
| 151 | int sign = 1; |
| 152 | int n = 0; |
| 153 | |
| 154 | if (c == '-') |
| 155 | { |
| 156 | c = yygetc (); |
| 157 | sign = -1; |
| 158 | } |
| 159 | |
| 160 | while (isdigit (c)) |
| 161 | { |
| 162 | n = 10 * n + (c - '0'); |
| 163 | c = yygetc (); |
| 164 | } |
| 165 | |
| 166 | yyungetc (c); |
| 167 | |
| 168 | return sign * n; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | |
| 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 | `---------------------------------------------------------------*/ |
| 178 | |
| 179 | static int |
| 180 | yylex (void) |
| 181 | { |
| 182 | int c; |
| 183 | |
| 184 | #if YYLSP_NEEDED |
| 185 | yylloc.first_column = yylloc.last_column; |
| 186 | yylloc.first_line = yylloc.last_line; |
| 187 | #endif |
| 188 | |
| 189 | /* Skip white space. */ |
| 190 | while ((c = yygetc ()) == ' ' || c == '\t') |
| 191 | { |
| 192 | #if YYLSP_NEEDED |
| 193 | yylloc.first_column = yylloc.last_column; |
| 194 | yylloc.first_line = yylloc.last_line; |
| 195 | #endif |
| 196 | } |
| 197 | |
| 198 | /* process numbers */ |
| 199 | if (c == '.' || isdigit (c)) |
| 200 | { |
| 201 | yyungetc (c); |
| 202 | yylval = read_signed_integer (); |
| 203 | return NUM; |
| 204 | } |
| 205 | |
| 206 | /* Return end-of-file. */ |
| 207 | if (c == EOF) |
| 208 | return CALC_EOF; |
| 209 | |
| 210 | /* Return single chars. */ |
| 211 | return c; |
| 212 | } |
| 213 | |
| 214 | static int |
| 215 | power (int base, int exponent) |
| 216 | { |
| 217 | int res = 1; |
| 218 | if (exponent < 0) |
| 219 | exit (1); |
| 220 | for (/* Niente */; exponent; --exponent) |
| 221 | res *= base; |
| 222 | return res; |
| 223 | } |
| 224 | |
| 225 | int |
| 226 | main (int argc, const char **argv) |
| 227 | { |
| 228 | yyin = NULL; |
| 229 | |
| 230 | if (argc == 2) |
| 231 | yyin = fopen (argv[1], "r"); |
| 232 | else |
| 233 | yyin = stdin; |
| 234 | |
| 235 | if (!yyin) |
| 236 | { |
| 237 | perror (argv[1]); |
| 238 | exit (1); |
| 239 | } |
| 240 | |
| 241 | #if YYDEBUG |
| 242 | yydebug = 1; |
| 243 | #endif |
| 244 | #if YYLSP_NEEDED |
| 245 | yylloc.last_column = 0; |
| 246 | yylloc.last_line = 1; |
| 247 | #endif |
| 248 | yyparse (); |
| 249 | return 0; |
| 250 | } |
| 251 | ]]) |
| 252 | ])# _AT_DATA_CALC_Y |
| 253 | |
| 254 | |
| 255 | # AT_DATA_CALC_Y([BISON-OPTIONS]) |
| 256 | # ------------------------------- |
| 257 | # Produce `calc.y'. |
| 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]])])]) |
| 262 | |
| 263 | |
| 264 | |
| 265 | # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0]) |
| 266 | # ------------------------------------------------------------ |
| 267 | # Run `calc' on INPUT and expect no STDOUT nor STDERR. |
| 268 | # |
| 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], |
| 272 | [AT_DATA([[input]], |
| 273 | [[$2 |
| 274 | ]]) |
| 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], |
| 278 | [$3], [0]) |
| 279 | ]) |
| 280 | ]) |
| 281 | |
| 282 | |
| 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. |
| 287 | # |
| 288 | # If BISON-OPTIONS contains `--location', then make sure the ERROR-LOCATION |
| 289 | # is correctly output on stderr. |
| 290 | # |
| 291 | # If BISON-OPTIONS contains `--yyerror-verbose', then make sure the |
| 292 | # IF-YYERROR-VERBOSE message is properly output after `parse error, ' |
| 293 | # on STDERR. |
| 294 | # |
| 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], |
| 298 | [AT_DATA([[input]], |
| 299 | [[$2 |
| 300 | ]]) |
| 301 | |
| 302 | AT_CHECK([calc input], 0, [], [stderr]) |
| 303 | |
| 304 | |
| 305 | AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, |
| 306 | [m4_bmatch([$1], [--debug], |
| 307 | [$3], [1]) |
| 308 | ]) |
| 309 | |
| 310 | egrep -v '^((Start|Enter|Read|Reduc|Shift)ing|state|Error:) ' stderr >at-stderr |
| 311 | mv at-stderr stderr |
| 312 | |
| 313 | AT_CHECK([cat stderr], 0, |
| 314 | [m4_bmatch([$1], [--location], [$4: ])[]dnl |
| 315 | parse error[]dnl |
| 316 | m4_bmatch([$1], [--yyerror-verbose], [, $5])[]dnl |
| 317 | |
| 318 | ]) |
| 319 | |
| 320 | ]) |
| 321 | |
| 322 | |
| 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]) |
| 330 | |
| 331 | AT_DATA_CALC_Y([$1]) |
| 332 | |
| 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])], |
| 335 | [0], [], []) |
| 336 | |
| 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 |
| 344 | # to GCC. |
| 345 | if test "$GCC" = yes; then |
| 346 | CFLAGS="$CFLAGS -Werror" |
| 347 | fi |
| 348 | AT_CHECK([$CC $CFLAGS $CPPFLAGS calc.c -o calc], 0, [], [ignore]) |
| 349 | |
| 350 | # Test the priorities. |
| 351 | _AT_CHECK_CALC([$1], |
| 352 | [1 + 2 * 3 = 7 |
| 353 | 1 + 2 * -3 = -5 |
| 354 | |
| 355 | -1^2 = -1 |
| 356 | (-1)^2 = 1 |
| 357 | |
| 358 | ---1 = -1 |
| 359 | |
| 360 | 1 - 2 - 3 = -4 |
| 361 | 1 - (2 - 3) = 2 |
| 362 | |
| 363 | 2^2^3 = 256 |
| 364 | (2^2)^3 = 64], [491]) |
| 365 | |
| 366 | # Some parse errors. |
| 367 | _AT_CHECK_CALC_ERROR([$1], [+1], [8], |
| 368 | [1.0:1.1], |
| 369 | [unexpected '+']) |
| 370 | _AT_CHECK_CALC_ERROR([$1], [1//2], [17], |
| 371 | [1.2:1.3], |
| 372 | [unexpected '/', expecting NUM or '-' or '(']) |
| 373 | _AT_CHECK_CALC_ERROR([$1], [error], [8], |
| 374 | [1.0:1.1], |
| 375 | [unexpected $undefined.]) |
| 376 | _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [23], |
| 377 | [1.6:1.7], |
| 378 | [unexpected '=']) |
| 379 | _AT_CHECK_CALC_ERROR([$1], |
| 380 | [ |
| 381 | +1], |
| 382 | [16], |
| 383 | [2.0:2.1], |
| 384 | [unexpected '+']) |
| 385 | |
| 386 | AT_CLEANUP |
| 387 | ])# AT_CHECK_CALC |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | # ------------------ # |
| 393 | # Test the parsers. # |
| 394 | # ------------------ # |
| 395 | |
| 396 | AT_CHECK_CALC() |
| 397 | |
| 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]) |
| 404 | |
| 405 | AT_CHECK_CALC([--locations --yyerror-verbose]) |
| 406 | |
| 407 | AT_CHECK_CALC([--defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose]) |
| 408 | |
| 409 | AT_CHECK_CALC([--debug]) |
| 410 | AT_CHECK_CALC([--debug --defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose]) |