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 ## ---------------------------------------------------- ##
20 ## Compile the grammar described in the documentation. ##
21 ## ---------------------------------------------------- ##
24 # ------------------------- #
25 # Helping Autotest macros. #
26 # ------------------------- #
29 # _AT_DATA_CALC_Y($1, $2, $3, [CPP-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
39 [[/* Infix notation calculator--calc */
43 /* We don't need a perfect malloc for these tests. */
51 char *strcat(char *dest, const char *src);
55 static int power (int base, int exponent);
56 static void yyerror (const char *s);
57 static int yylex (void);
58 static int yygetc (void);
59 static void yyungetc (int c);
61 extern void perror (const char *s);
63 /* Exercise pre-prologue dependency to %union. */
68 /* Exercise %union. */
74 /* Bison Declarations */
75 %token CALC_EOF 0 "end of file"
76 %token <ival> NUM "number"
79 %nonassoc '=' /* comparison */
82 %left NEG /* negation--unary minus */
83 %right '^' /* exponentiation */
104 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
107 | exp '+' exp { $$ = $1 + $3; }
108 | exp '-' exp { $$ = $1 - $3; }
109 | exp '*' exp { $$ = $1 * $3; }
110 | exp '/' exp { $$ = $1 / $3; }
111 | '-' exp %prec NEG { $$ = -$2; }
112 | exp '^' exp { $$ = power ($1, $3); }
113 | '(' exp ')' { $$ = $2; }
114 | '(' error ')' { $$ = 0; }
121 yyerror (const char *s)
124 fprintf (stderr, "%d.%d-%d.%d: ",
125 yylloc.first_line, yylloc.first_column,
126 yylloc.last_line, yylloc.last_column);
128 fprintf (stderr, "%s\n", s);
133 static YYLTYPE last_yylloc;
138 int res = getc (yyin);
140 last_yylloc = yylloc;
144 yylloc.last_column = 1;
147 yylloc.last_column++;
157 /* Wrong when C == `\n'. */
158 yylloc = last_yylloc;
164 read_signed_integer (void)
178 n = 10 * n + (c - '0');
189 /*---------------------------------------------------------------.
190 | Lexical analyzer returns an integer on the stack and the token |
191 | NUM, or the ASCII character read if not a number. Skips all |
192 | blanks and tabs, returns 0 for EOF. |
193 `---------------------------------------------------------------*/
201 yylloc.first_column = yylloc.last_column;
202 yylloc.first_line = yylloc.last_line;
205 /* Skip white space. */
206 while ((c = yygetc ()) == ' ' || c == '\t')
209 yylloc.first_column = yylloc.last_column;
210 yylloc.first_line = yylloc.last_line;
214 /* process numbers */
215 if (c == '.' || isdigit (c))
218 yylval.ival = read_signed_integer ();
222 /* Return end-of-file. */
226 /* Return single chars. */
231 power (int base, int exponent)
236 for (/* Niente */; exponent; --exponent)
242 main (int argc, const char **argv)
247 yyin = fopen (argv[1], "r");
261 yylloc.last_column = 1;
262 yylloc.last_line = 1;
271 # AT_DATA_CALC_Y([BISON-OPTIONS])
272 # -------------------------------
274 m4_define([AT_DATA_CALC_Y],
275 [_AT_DATA_CALC_Y($[1], $[2], $[3],
276 [m4_bpatsubst([$1], [--[^ ]*])])
281 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
282 # ------------------------------------------------------------
283 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
285 # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
286 # NUM-STDERR-LINES is the number of expected lines on stderr.
288 # We don't count GLR's traces yet, since its traces are somewhat
289 # different from LALR's.
290 m4_define([_AT_CHECK_CALC],
294 AT_PARSER_CHECK([./calc input], 0, [], [stderr])
296 [%debug.*%glr\|%glr.*%debug],
299 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
304 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, [NUM-DEBUG-LINES],
305 # [ERROR-LOCATION], [IF-YYERROR-VERBOSE])
306 # ------------------------------------------------------------
307 # Run `calc' on INPUT, and expect a `parse error' message.
309 # If INPUT starts with a slash, it is used as absolute input file name,
310 # otherwise as contents.
312 # If BISON-OPTIONS contains `--location', then make sure the ERROR-LOCATION
313 # is correctly output on stderr.
315 # If BISON-OPTIONS contains `%error-verbose', then make sure the
316 # IF-YYERROR-VERBOSE message is properly output after `parse error, '
319 # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
320 # is the number of expected lines on stderr.
321 m4_define([_AT_CHECK_CALC_ERROR],
322 [m4_bmatch([$2], [^/],
323 [AT_PARSER_CHECK([./calc $2], 0, [], [stderr])],
327 AT_PARSER_CHECK([./calc input], 0, [], [stderr])])
329 [%debug.*%glr\|%glr.*%debug],
332 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
335 # Normalize the observed and expected error messages, depending upon the
337 # 1. Remove the traces from observed.
348 /^yydestructor:/d' stderr >at-stderr
350 # 2. Create the reference error message.
354 # 3. If locations are not used, remove them.
355 m4_bmatch([$1], [--location], [],
356 [[sed 's/^[-0-9.]*: //' expout >at-expout
357 mv at-expout expout]])
358 # 4. If error-verbose is not used, strip the`, unexpected....' part.
359 m4_bmatch([$1], [%error-verbose], [],
360 [[sed 's/parse error, .*$/parse error/' expout >at-expout
361 mv at-expout expout]])
363 AT_CHECK([cat stderr], 0, [expout])
367 # AT_CHECK_CALC([BISON-OPTIONS])
368 # ------------------------------
369 # Start a testing chunk which compiles `calc' grammar with
370 # BISON-OPTIONS, and performs several tests over the parser.
371 m4_define([AT_CHECK_CALC],
372 [# We use integers to avoid dependencies upon the precision of doubles.
373 AT_SETUP([Calculator $1])
377 # Specify the output files to avoid problems on different file systems.
378 AT_CHECK([bison calc.y -o calc.c m4_bpatsubst([$1], [%[^ ]*])],
383 # Test the priorities.
397 (2^2)^3 = 64], [486])
400 _AT_CHECK_CALC_ERROR([$1], [0 0], [11],
401 [1.3-1.4: parse error, unexpected "number"])
402 _AT_CHECK_CALC_ERROR([$1], [1//2], [15],
403 [1.3-1.4: parse error, unexpected '/', expecting "number" or '-' or '('])
404 _AT_CHECK_CALC_ERROR([$1], [error], [4],
405 [1.1-1.2: parse error, unexpected $undefined, expecting "number" or '-' or '\n' or '('])
406 _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [22],
407 [1.7-1.8: parse error, unexpected '='])
408 _AT_CHECK_CALC_ERROR([$1],
412 [2.1-2.2: parse error, unexpected '+'])
413 # Exercise error messages with EOF: work on an empty file.
414 _AT_CHECK_CALC_ERROR([$1], [/dev/null], [4],
415 [1.1-1.2: parse error, unexpected "end of file", expecting "number" or '-' or '\n' or '('])
417 # Exercise the error token: without it, we die at the first error,
418 # hence be sure i. to have several errors, ii. to test the action
419 # associated to `error'.
420 _AT_CHECK_CALC_ERROR([$1], [(1 ++ 2) + (0 0) = 1], [82],
421 [1.5-1.6: parse error, unexpected '+', expecting "number" or '-' or '('
422 1.15-1.16: parse error, unexpected "number"
423 calc: error: 0 != 1])
425 # Add a studid example demonstrating that Bison can further improve the
426 # error message. FIXME: Fix this ridiculous message.
427 _AT_CHECK_CALC_ERROR([$1], [()], [21],
428 [1.2-1.3: parse error, unexpected ')', expecting "number" or '-' or '('])
436 # ------------------------ #
437 # Simple LALR Calculator. #
438 # ------------------------ #
440 AT_BANNER([[Simple LALR Calculator.]])
442 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
443 # -----------------------------------
444 # Start a testing chunk which compiles `calc' grammar with
445 # BISON-OPTIONS, and performs several tests over the parser.
446 m4_define([AT_CHECK_CALC_LALR],
451 AT_CHECK_CALC_LALR([--defines])
452 AT_CHECK_CALC_LALR([--locations])
453 AT_CHECK_CALC_LALR([--name-prefix=calc])
454 AT_CHECK_CALC_LALR([--verbose])
455 AT_CHECK_CALC_LALR([--yacc])
456 AT_CHECK_CALC_LALR([%error-verbose])
458 AT_CHECK_CALC_LALR([%error-verbose --locations])
460 AT_CHECK_CALC_LALR([%error-verbose --defines --locations --name-prefix=calc --verbose --yacc])
462 AT_CHECK_CALC_LALR([%debug])
463 AT_CHECK_CALC_LALR([%error-verbose %debug --defines --locations --name-prefix=calc --verbose --yacc])
466 # ----------------------- #
467 # Simple GLR Calculator. #
468 # ----------------------- #
470 AT_BANNER([[Simple GLR Calculator.]])
472 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
473 # ----------------------------------
474 # Start a testing chunk which compiles `calc' grammar with
475 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
476 m4_define([AT_CHECK_CALC_GLR],
477 [AT_CHECK_CALC([%glr_parser] $@)])
482 AT_CHECK_CALC_GLR([--defines])
483 AT_CHECK_CALC_GLR([--locations])
484 AT_CHECK_CALC_GLR([--name-prefix=calc])
485 AT_CHECK_CALC_GLR([--verbose])
486 AT_CHECK_CALC_GLR([--yacc])
487 AT_CHECK_CALC_GLR([%error-verbose])
489 AT_CHECK_CALC_GLR([%error-verbose --locations])
491 AT_CHECK_CALC_GLR([%error-verbose --defines --locations --name-prefix=calc --verbose --yacc])
493 AT_CHECK_CALC_GLR([%debug])
494 AT_CHECK_CALC_GLR([%error-verbose %debug --defines --locations --name-prefix=calc --verbose --yacc])