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 perfect functions for these tests. */
56 extern void perror (const char *s);
58 /* Exercise pre-prologue dependency to %union. */
61 value_t global_result = 0;
66 %parse-param "value_t *result", "result"
67 %parse-param "int *count", "count"
69 /* Exercise %union. */
77 # define LOC (*yylloc)
78 # define VAL (*yylval)
86 # define LEX_FORMALS YYSTYPE *yylval, YYLTYPE *yylloc
87 # define LEX_ARGS yylval, yylloc
88 # define USE_LEX_ARGS (void) yylval; (void) yylloc;
90 # define LEX_FORMALS YYSTYPE *yylval
91 # define LEX_ARGS yylval
92 # define USE_LEX_ARGS (void) yylval
94 # define LEX_PRE_FORMALS LEX_FORMALS,
95 # define LEX_PRE_ARGS LEX_ARGS,
97 # define LEX_FORMALS void
98 # define LEX_PRE_FORMALS
100 # define LEX_PRE_ARGS
101 # define USE_LEX_ARGS
104 static int power (int base, int exponent);
105 static void yyerror (const char *s);
106 static int yylex (LEX_FORMALS);
107 static int yygetc (LEX_FORMALS);
108 static void yyungetc (LEX_PRE_FORMALS int c);
111 /* Bison Declarations */
112 %token CALC_EOF 0 "end of input"
113 %token <ival> NUM "number"
116 %nonassoc '=' /* comparison */
119 %left NEG /* negation--unary minus */
120 %right '^' /* exponentiation */
124 /* Grammar follows */
128 | input line { ++*count; ++global_count; }
133 | exp '\n' { *result = global_result = $1; }
141 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
144 | exp '+' exp { $$ = $1 + $3; }
145 | exp '-' exp { $$ = $1 - $3; }
146 | exp '*' exp { $$ = $1 * $3; }
147 | exp '/' exp { $$ = $1 / $3; }
148 | '-' exp %prec NEG { $$ = -$2; }
149 | exp '^' exp { $$ = power ($1, $3); }
150 | '(' exp ')' { $$ = $2; }
151 | '(' error ')' { $$ = 0; }
158 yyerror (const char *s)
161 fprintf (stderr, "%d.%d-%d.%d: ",
162 LOC.first_line, LOC.first_column,
163 LOC.last_line, LOC.last_column);
165 fprintf (stderr, "%s\n", s);
170 static YYLTYPE last_yylloc;
175 int res = getc (yyin);
192 yyungetc (LEX_PRE_FORMALS int c)
196 /* Wrong when C == `\n'. */
203 read_signed_integer (LEX_FORMALS)
205 int c = yygetc (LEX_ARGS);
212 c = yygetc (LEX_ARGS);
218 n = 10 * n + (c - '0');
219 c = yygetc (LEX_ARGS);
222 yyungetc (LEX_PRE_ARGS c);
229 /*---------------------------------------------------------------.
230 | Lexical analyzer returns an integer on the stack and the token |
231 | NUM, or the ASCII character read if not a number. Skips all |
232 | blanks and tabs, returns 0 for EOF. |
233 `---------------------------------------------------------------*/
251 LOC.first_column = LOC.last_column;
252 LOC.first_line = LOC.last_line;
255 /* Skip white space. */
256 while ((c = yygetc (LEX_ARGS)) == ' ' || c == '\t')
259 LOC.first_column = LOC.last_column;
260 LOC.first_line = LOC.last_line;
264 /* process numbers */
265 if (c == '.' || isdigit (c))
267 yyungetc (LEX_PRE_ARGS c);
268 VAL.ival = read_signed_integer (LEX_ARGS);
272 /* Return end-of-file. */
276 /* Return single chars. */
281 power (int base, int exponent)
286 for (/* Niente */; exponent; --exponent)
292 main (int argc, const char **argv)
299 yyin = fopen (argv[1], "r");
312 yyparse (&result, &count);
313 assert (global_result == result);
314 assert (global_count == count);
322 # AT_DATA_CALC_Y([BISON-OPTIONS])
323 # -------------------------------
325 m4_define([AT_DATA_CALC_Y],
326 [_AT_DATA_CALC_Y($[1], $[2], $[3],
327 [m4_bpatsubst([$1], [--[^ ]*])])
332 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
333 # ------------------------------------------------------------
334 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
336 # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
337 # NUM-STDERR-LINES is the number of expected lines on stderr.
339 # We don't count GLR's traces yet, since its traces are somewhat
340 # different from LALR's.
341 m4_define([_AT_CHECK_CALC],
345 AT_PARSER_CHECK([./calc input], 0, [], [stderr])
347 [%debug.*%glr\|%glr.*%debug],
350 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
355 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, [NUM-DEBUG-LINES],
356 # [ERROR-LOCATION], [IF-YYERROR-VERBOSE])
357 # ------------------------------------------------------------
358 # Run `calc' on INPUT, and expect a `parse error' message.
360 # If INPUT starts with a slash, it is used as absolute input file name,
361 # otherwise as contents.
363 # If BISON-OPTIONS contains `--location', then make sure the ERROR-LOCATION
364 # is correctly output on stderr.
366 # If BISON-OPTIONS contains `%error-verbose', then make sure the
367 # IF-YYERROR-VERBOSE message is properly output after `parse error, '
370 # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
371 # is the number of expected lines on stderr.
372 m4_define([_AT_CHECK_CALC_ERROR],
373 [m4_bmatch([$2], [^/],
374 [AT_PARSER_CHECK([./calc $2], 0, [], [stderr])],
378 AT_PARSER_CHECK([./calc input], 0, [], [stderr])])
380 [%debug.*%glr\|%glr.*%debug],
383 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
386 # Normalize the observed and expected error messages, depending upon the
388 # 1. Remove the traces from observed.
399 /^yydestructor:/d' stderr >at-stderr
401 # 2. Create the reference error message.
405 # 3. If locations are not used, remove them.
406 m4_bmatch([$1], [%locations], [],
407 [[sed 's/^[-0-9.]*: //' expout >at-expout
408 mv at-expout expout]])
409 # 4. If error-verbose is not used, strip the`, unexpected....' part.
410 m4_bmatch([$1], [%error-verbose], [],
411 [[sed 's/parse error, .*$/parse error/' expout >at-expout
412 mv at-expout expout]])
414 AT_CHECK([cat stderr], 0, [expout])
418 # AT_CHECK_CALC([BISON-OPTIONS])
419 # ------------------------------
420 # Start a testing chunk which compiles `calc' grammar with
421 # BISON-OPTIONS, and performs several tests over the parser.
422 m4_define([AT_CHECK_CALC],
423 [# We use integers to avoid dependencies upon the precision of doubles.
424 AT_SETUP([Calculator $1])
428 # Specify the output files to avoid problems on different file systems.
429 AT_CHECK([bison -o calc.c m4_bpatsubst([$1], [%[^ ]*]) calc.y],
434 # Test the priorities.
452 _AT_CHECK_CALC_ERROR([$1], [0 0], [11],
453 [1.3-1.4: parse error, unexpected "number"])
454 _AT_CHECK_CALC_ERROR([$1], [1//2], [15],
455 [1.3-1.4: parse error, unexpected '/', expecting "number" or '-' or '('])
456 _AT_CHECK_CALC_ERROR([$1], [error], [4],
457 [1.1-1.2: parse error, unexpected $undefined, expecting "number" or '-' or '\n' or '('])
458 _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [22],
459 [1.7-1.8: parse error, unexpected '='])
460 _AT_CHECK_CALC_ERROR([$1],
464 [2.1-2.2: parse error, unexpected '+'])
465 # Exercise error messages with EOF: work on an empty file.
466 _AT_CHECK_CALC_ERROR([$1], [/dev/null], [4],
467 [1.1-1.2: parse error, unexpected "end of input", expecting "number" or '-' or '\n' or '('])
469 # Exercise the error token: without it, we die at the first error,
470 # hence be sure i. to have several errors, ii. to test the action
471 # associated to `error'.
472 _AT_CHECK_CALC_ERROR([$1], [(1 ++ 2) + (0 0) = 1], [82],
473 [1.5-1.6: parse error, unexpected '+', expecting "number" or '-' or '('
474 1.15-1.16: parse error, unexpected "number"
475 calc: error: 0 != 1])
483 # ------------------------ #
484 # Simple LALR Calculator. #
485 # ------------------------ #
487 AT_BANNER([[Simple LALR Calculator.]])
489 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
490 # -----------------------------------
491 # Start a testing chunk which compiles `calc' grammar with
492 # BISON-OPTIONS, and performs several tests over the parser.
493 m4_define([AT_CHECK_CALC_LALR],
498 AT_CHECK_CALC_LALR([--defines])
499 AT_CHECK_CALC_LALR([%locations])
500 AT_CHECK_CALC_LALR([--name-prefix=calc])
501 AT_CHECK_CALC_LALR([--verbose])
502 AT_CHECK_CALC_LALR([--yacc])
503 AT_CHECK_CALC_LALR([%error-verbose])
505 AT_CHECK_CALC_LALR([%error-verbose %locations])
507 AT_CHECK_CALC_LALR([%error-verbose %locations --defines --name-prefix=calc --verbose --yacc])
509 AT_CHECK_CALC_LALR([%debug])
510 AT_CHECK_CALC_LALR([%error-verbose %debug %locations --defines --name-prefix=calc --verbose --yacc])
512 # FIXME: Not ready yet.
513 # AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations --defines --name-prefix=calc --verbose --yacc])
516 # ----------------------- #
517 # Simple GLR Calculator. #
518 # ----------------------- #
520 AT_BANNER([[Simple GLR Calculator.]])
522 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
523 # ----------------------------------
524 # Start a testing chunk which compiles `calc' grammar with
525 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
526 m4_define([AT_CHECK_CALC_GLR],
527 [AT_CHECK_CALC([%glr-parser] $@)])
532 AT_CHECK_CALC_GLR([--defines])
533 AT_CHECK_CALC_GLR([%locations])
534 AT_CHECK_CALC_GLR([--name-prefix=calc])
535 AT_CHECK_CALC_GLR([--verbose])
536 AT_CHECK_CALC_GLR([--yacc])
537 AT_CHECK_CALC_GLR([%error-verbose])
539 AT_CHECK_CALC_GLR([%error-verbose %locations])
541 AT_CHECK_CALC_GLR([%error-verbose %locations --defines --name-prefix=calc --verbose --yacc])
543 AT_CHECK_CALC_GLR([%debug])
544 AT_CHECK_CALC_GLR([%error-verbose %debug %locations --defines --name-prefix=calc --verbose --yacc])
546 # AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations --defines --name-prefix=calc --verbose --yacc])