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, [BISON-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
38 AT_DATA_GRAMMAR([calc.y],
39 [[/* Infix notation calculator--calc */
50 extern void perror (const char *s);
52 /* Exercise pre-prologue dependency to %union. */
55 static value_t global_result = 0;
56 static int global_count = 0;
60 /* Exercise %union. */
68 # define LOC (*yylloc)
69 # define VAL (*yylval)
75 #define YYLLOC_FORMAL ]AT_LOCATION_IF([, YYLTYPE *yylloc])[
76 #define YYLLOC_ARG ]AT_LOCATION_IF([, yylloc])[
77 #define USE_YYLLOC ]AT_LOCATION_IF([(void) yylloc;])[
80 # define LEX_FORMALS YYSTYPE *yylval YYLLOC_FORMAL
81 # define LEX_ARGS yylval YYLLOC_ARG
82 # define USE_LEX_ARGS (void) yylval; USE_YYLLOC
83 # define LEX_PRE_FORMALS LEX_FORMALS,
84 # define LEX_PRE_ARGS LEX_ARGS,
86 # define LEX_FORMALS void
87 # define LEX_PRE_FORMALS
93 static int power (int base, int exponent);
94 /* yyerror receives the location if:
95 - %location & %pure & %glr
96 - %location & %pure & %yacc & %parse-param. */
97 static void yyerror (]AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])[
98 ]AT_PARAM_IF([value_t *result, int *count, ])[
101 static int yylex (LEX_FORMALS);
102 static int yygetc (LEX_FORMALS);
103 static void yyungetc (LEX_PRE_FORMALS int c);
106 /* Bison Declarations */
107 %token CALC_EOF 0 "end of input"
108 %token <ival> NUM "number"
111 %nonassoc '=' /* comparison */
114 %left NEG /* negation--unary minus */
115 %right '^' /* exponentiation */
117 /* Grammar follows */
121 | input line { ]AT_PARAM_IF([++*count; ++global_count;])[ }
126 | exp '\n' { ]AT_PARAM_IF([*result = global_result = $1;])[ }
134 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
137 | exp '+' exp { $$ = $1 + $3; }
138 | exp '-' exp { $$ = $1 - $3; }
139 | exp '*' exp { $$ = $1 * $3; }
140 | exp '/' exp { $$ = $1 / $3; }
141 | '-' exp %prec NEG { $$ = -$2; }
142 | exp '^' exp { $$ = power ($1, $3); }
143 | '(' exp ')' { $$ = $2; }
144 | '(' error ')' { $$ = 0; }
151 yyerror (]AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])[
152 ]AT_PARAM_IF([value_t *result, int *count, ])[
156 ]AT_PARAM_IF([(void) result; (void) count; ])[
157 ]AT_YYERROR_SEES_LOC_IF([
158 fprintf (stderr, "%d.%d-%d.%d: ",
159 LOC.first_line, LOC.first_column,
160 LOC.last_line, LOC.last_column);
162 fprintf (stderr, "%s\n", s);
167 static YYLTYPE last_yylloc;
172 int res = getc (yyin);
189 yyungetc (LEX_PRE_FORMALS int c)
193 /* Wrong when C == `\n'. */
200 read_signed_integer (LEX_FORMALS)
202 int c = yygetc (LEX_ARGS);
209 c = yygetc (LEX_ARGS);
215 n = 10 * n + (c - '0');
216 c = yygetc (LEX_ARGS);
219 yyungetc (LEX_PRE_ARGS c);
226 /*---------------------------------------------------------------.
227 | Lexical analyzer returns an integer on the stack and the token |
228 | NUM, or the ASCII character read if not a number. Skips all |
229 | blanks and tabs, returns 0 for EOF. |
230 `---------------------------------------------------------------*/
248 LOC.first_column = LOC.last_column;
249 LOC.first_line = LOC.last_line;
252 /* Skip white space. */
253 while ((c = yygetc (LEX_ARGS)) == ' ' || c == '\t')
256 LOC.first_column = LOC.last_column;
257 LOC.first_line = LOC.last_line;
261 /* process numbers */
262 if (c == '.' || isdigit (c))
264 yyungetc (LEX_PRE_ARGS c);
265 VAL.ival = read_signed_integer (LEX_ARGS);
269 /* Return end-of-file. */
273 /* Return single chars. */
278 power (int base, int exponent)
283 for (/* Niente */; exponent; --exponent)
289 main (int argc, const char **argv)
296 yyin = fopen (argv[1], "r");
309 status = yyparse (]AT_PARAM_IF([&result, &count])[);
310 if (global_result != result)
312 if (global_count != count)
320 # AT_DATA_CALC_Y([BISON-OPTIONS])
321 # -------------------------------
323 m4_define([AT_DATA_CALC_Y],
324 [_AT_DATA_CALC_Y($[1], $[2], $[3], [$1])
329 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
330 # ------------------------------------------------------------
331 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
333 # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
334 # NUM-STDERR-LINES is the number of expected lines on stderr.
336 # We don't count GLR's traces yet, since its traces are somewhat
337 # different from LALR's.
338 m4_define([_AT_CHECK_CALC],
342 AT_PARSER_CHECK([./calc input], 0, [], [stderr])
344 [%debug.*%glr\|%glr.*%debug],
347 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
352 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT,
354 # [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
355 # ---------------------------------------------------------
356 # Run `calc' on INPUT, and expect a `syntax error' message.
358 # If INPUT starts with a slash, it is used as absolute input file name,
359 # otherwise as contents.
361 # If BISON-OPTIONS contains `%location', then make sure the ERROR-LOCATION
362 # is correctly output on stderr.
364 # If BISON-OPTIONS contains `%error-verbose', then make sure the
365 # IF-YYERROR-VERBOSE message is properly output after `syntax error, '
368 # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
369 # is the number of expected lines on stderr.
370 m4_define([_AT_CHECK_CALC_ERROR],
371 [m4_bmatch([$3], [^/],
372 [AT_PARSER_CHECK([./calc $3], $2, [], [stderr])],
376 AT_PARSER_CHECK([./calc input], $2, [], [stderr])])
378 [%debug.*%glr\|%glr.*%debug],
381 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$4
384 # Normalize the observed and expected error messages, depending upon the
386 # 1. Remove the traces from observed.
397 /^yydestructor:/d' stderr >at-stderr
399 # 2. Create the reference error message.
403 # 3. If locations are not used, remove them.
404 AT_YYERROR_SEES_LOC_IF([],
405 [[sed 's/^[-0-9.]*: //' expout >at-expout
406 mv at-expout expout]])
407 # 4. If error-verbose is not used, strip the`, unexpected....' part.
408 m4_bmatch([$1], [%error-verbose], [],
409 [[sed 's/syntax error, .*$/syntax error/' expout >at-expout
410 mv at-expout expout]])
412 AT_CHECK([cat stderr], 0, [expout])
416 # AT_CALC_PUSHDEFS($1, $2, [BISON-OPTIONS])
417 # -----------------------------------------
418 # This macro works around the impossibility to define macros
419 # inside macros, because issuing `[$1]' is not possible in M4 :(.
420 # This sucks hard, GNU M4 should really provide M5 like $$1.
421 m4_define([AT_CHECK_PUSHDEFS],
422 [m4_if([$1$2], $[1]$[2], [],
423 [m4_fatal([$0: Invalid arguments: $@])])dnl
424 m4_pushdef([AT_PARAM_IF],
425 [m4_bmatch([$3], [%parse-param], [$1], [$2])])
426 m4_pushdef([AT_LOCATION_IF],
427 [m4_bmatch([$3], [%locations], [$1], [$2])])
428 m4_pushdef([AT_PURE_IF],
429 [m4_bmatch([$3], [%pure-parser], [$1], [$2])])
430 m4_pushdef([AT_GLR_IF],
431 [m4_bmatch([$3], [%glr-parser], [$1], [$2])])
432 m4_pushdef([AT_PURE_AND_LOC_IF],
433 [m4_bmatch([$3], [%locations.*%pure-parser\|%pure-parser.*%locations],
435 m4_pushdef([AT_GLR_OR_PARAM_IF],
436 [m4_bmatch([$3], [%glr-parser\|%parse-param], [$1], [$2])])
438 # yyerror receives the location if %location & %pure & (%glr or %parse-param).
439 m4_pushdef([AT_YYERROR_ARG_LOC_IF],
440 [AT_GLR_OR_PARAM_IF([AT_PURE_AND_LOC_IF([$1], [$2])],
442 # yyerror cannot see the locations if !glr & pure & !param.
443 m4_pushdef([AT_YYERROR_SEES_LOC_IF],
444 [AT_LOCATION_IF([AT_GLR_IF([$1],
445 [AT_PURE_IF([AT_PARAM_IF([$1], [$2])],
453 m4_define([AT_CHECK_POPDEFS],
454 [m4_popdef([AT_YYERROR_SEES_LOC_IF])
455 m4_popdef([AT_YYERROR_ARG_LOC_IF])
456 m4_popdef([AT_GLR_OR_PARAM_IF])
457 m4_popdef([AT_PURE_AND_LOC_IF])
458 m4_popdef([AT_GLR_IF])
459 m4_popdef([AT_LOCATION_IF])
460 m4_popdef([AT_PARAM_IF])
465 # AT_CHECK_CALC([BISON-OPTIONS])
466 # ------------------------------
467 # Start a testing chunk which compiles `calc' grammar with
468 # BISON-OPTIONS, and performs several tests over the parser.
469 m4_define([AT_CHECK_CALC],
470 [# We use integers to avoid dependencies upon the precision of doubles.
471 AT_SETUP([Calculator $1])
473 AT_CHECK_PUSHDEFS($[1], $[2], [$1])
477 # Specify the output files to avoid problems on different file systems.
478 AT_CHECK([bison -o calc.c calc.y],
483 # Test the priorities.
500 # Some syntax errors.
501 _AT_CHECK_CALC_ERROR([$1], [1], [0 0], [11],
502 [1.3-1.4: syntax error, unexpected "number"])
503 _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [15],
504 [1.3-1.4: syntax error, unexpected '/', expecting "number" or '-' or '('])
505 _AT_CHECK_CALC_ERROR([$1], [1], [error], [4],
506 [1.1-1.2: syntax error, unexpected $undefined, expecting "number" or '-' or '\n' or '('])
507 _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [22],
508 [1.7-1.8: syntax error, unexpected '='])
509 _AT_CHECK_CALC_ERROR([$1], [1],
513 [2.1-2.2: syntax error, unexpected '+'])
514 # Exercise error messages with EOF: work on an empty file.
515 _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4],
516 [1.1-1.2: syntax error, unexpected "end of input", expecting "number" or '-' or '\n' or '('])
518 # Exercise the error token: without it, we die at the first error,
519 # hence be sure i. to have several errors, ii. to test the action
520 # associated to `error'.
521 _AT_CHECK_CALC_ERROR([$1], [0], [(1 ++ 2) + (0 0) = 1], [82],
522 [1.5-1.6: syntax error, unexpected '+', expecting "number" or '-' or '('
523 1.15-1.16: syntax error, unexpected "number"
524 calc: error: 0 != 1])
534 # ------------------------ #
535 # Simple LALR Calculator. #
536 # ------------------------ #
538 AT_BANNER([[Simple LALR Calculator.]])
540 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
541 # -----------------------------------
542 # Start a testing chunk which compiles `calc' grammar with
543 # BISON-OPTIONS, and performs several tests over the parser.
544 m4_define([AT_CHECK_CALC_LALR],
549 AT_CHECK_CALC_LALR([%defines])
550 AT_CHECK_CALC_LALR([%locations])
551 AT_CHECK_CALC_LALR([%name-prefix="calc"])
552 AT_CHECK_CALC_LALR([%verbose])
553 AT_CHECK_CALC_LALR([%yacc])
554 AT_CHECK_CALC_LALR([%error-verbose])
556 AT_CHECK_CALC_LALR([%error-verbose %locations])
558 AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
560 AT_CHECK_CALC_LALR([%debug])
561 AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
563 AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
565 AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {value_t *result}, {result} %parse-param {int *count}, {count}])
568 # ----------------------- #
569 # Simple GLR Calculator. #
570 # ----------------------- #
572 AT_BANNER([[Simple GLR Calculator.]])
574 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
575 # ----------------------------------
576 # Start a testing chunk which compiles `calc' grammar with
577 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
578 m4_define([AT_CHECK_CALC_GLR],
579 [AT_CHECK_CALC([%glr-parser] $@)])
584 AT_CHECK_CALC_GLR([%defines])
585 AT_CHECK_CALC_GLR([%locations])
586 AT_CHECK_CALC_GLR([%name-prefix="calc"])
587 AT_CHECK_CALC_GLR([%verbose])
588 AT_CHECK_CALC_GLR([%yacc])
589 AT_CHECK_CALC_GLR([%error-verbose])
591 AT_CHECK_CALC_GLR([%error-verbose %locations])
593 AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
595 AT_CHECK_CALC_GLR([%debug])
596 AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
598 AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
600 AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {value_t *result}, {result} %parse-param {int *count}, {count}])