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. */
53 char *strcat(char *dest, const char *src);
58 extern void perror (const char *s);
60 /* Exercise pre-prologue dependency to %union. */
63 value_t global_result = 0;
68 %parse-param "value_t *result", "result"
69 %parse-param "int *count", "count"
71 /* Exercise %union. */
79 # define LOC (*yylloc)
80 # define VAL (*yylval)
88 # define LEX_FORMALS YYSTYPE *yylval, YYLTYPE *yylloc
89 # define LEX_ARGS yylval, yylloc
90 # define USE_LEX_ARGS (void) yylval; (void) yylloc;
92 # define LEX_FORMALS YYSTYPE *yylval
93 # define LEX_ARGS yylval
94 # define USE_LEX_ARGS (void) yylval
96 # define LEX_PRE_FORMALS LEX_FORMALS,
97 # define LEX_PRE_ARGS LEX_ARGS,
99 # define LEX_FORMALS void
100 # define LEX_PRE_FORMALS
102 # define LEX_PRE_ARGS
103 # define USE_LEX_ARGS
106 static int power (int base, int exponent);
107 static void yyerror (const char *s);
108 static int yylex (LEX_FORMALS);
109 static int yygetc (LEX_FORMALS);
110 static void yyungetc (LEX_PRE_FORMALS int c);
113 /* Bison Declarations */
114 %token CALC_EOF 0 "end of input"
115 %token <ival> NUM "number"
118 %nonassoc '=' /* comparison */
121 %left NEG /* negation--unary minus */
122 %right '^' /* exponentiation */
126 /* Grammar follows */
130 | input line { ++*count; ++global_count; }
135 | exp '\n' { *result = global_result = $1; }
143 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
146 | exp '+' exp { $$ = $1 + $3; }
147 | exp '-' exp { $$ = $1 - $3; }
148 | exp '*' exp { $$ = $1 * $3; }
149 | exp '/' exp { $$ = $1 / $3; }
150 | '-' exp %prec NEG { $$ = -$2; }
151 | exp '^' exp { $$ = power ($1, $3); }
152 | '(' exp ')' { $$ = $2; }
153 | '(' error ')' { $$ = 0; }
160 yyerror (const char *s)
163 fprintf (stderr, "%d.%d-%d.%d: ",
164 LOC.first_line, LOC.first_column,
165 LOC.last_line, LOC.last_column);
167 fprintf (stderr, "%s\n", s);
172 static YYLTYPE last_yylloc;
177 int res = getc (yyin);
194 yyungetc (LEX_PRE_FORMALS int c)
198 /* Wrong when C == `\n'. */
205 read_signed_integer (LEX_FORMALS)
207 int c = yygetc (LEX_ARGS);
214 c = yygetc (LEX_ARGS);
220 n = 10 * n + (c - '0');
221 c = yygetc (LEX_ARGS);
224 yyungetc (LEX_PRE_ARGS c);
231 /*---------------------------------------------------------------.
232 | Lexical analyzer returns an integer on the stack and the token |
233 | NUM, or the ASCII character read if not a number. Skips all |
234 | blanks and tabs, returns 0 for EOF. |
235 `---------------------------------------------------------------*/
253 LOC.first_column = LOC.last_column;
254 LOC.first_line = LOC.last_line;
257 /* Skip white space. */
258 while ((c = yygetc (LEX_ARGS)) == ' ' || c == '\t')
261 LOC.first_column = LOC.last_column;
262 LOC.first_line = LOC.last_line;
266 /* process numbers */
267 if (c == '.' || isdigit (c))
269 yyungetc (LEX_PRE_ARGS c);
270 VAL.ival = read_signed_integer (LEX_ARGS);
274 /* Return end-of-file. */
278 /* Return single chars. */
283 power (int base, int exponent)
288 for (/* Niente */; exponent; --exponent)
294 main (int argc, const char **argv)
301 yyin = fopen (argv[1], "r");
314 yyparse (&result, &count);
315 assert (global_result == result);
316 assert (global_count == count);
324 # AT_DATA_CALC_Y([BISON-OPTIONS])
325 # -------------------------------
327 m4_define([AT_DATA_CALC_Y],
328 [_AT_DATA_CALC_Y($[1], $[2], $[3],
329 [m4_bpatsubst([$1], [--[^ ]*])])
334 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
335 # ------------------------------------------------------------
336 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
338 # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
339 # NUM-STDERR-LINES is the number of expected lines on stderr.
341 # We don't count GLR's traces yet, since its traces are somewhat
342 # different from LALR's.
343 m4_define([_AT_CHECK_CALC],
347 AT_PARSER_CHECK([./calc input], 0, [], [stderr])
349 [%debug.*%glr\|%glr.*%debug],
352 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
357 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, [NUM-DEBUG-LINES],
358 # [ERROR-LOCATION], [IF-YYERROR-VERBOSE])
359 # ------------------------------------------------------------
360 # Run `calc' on INPUT, and expect a `parse error' message.
362 # If INPUT starts with a slash, it is used as absolute input file name,
363 # otherwise as contents.
365 # If BISON-OPTIONS contains `--location', then make sure the ERROR-LOCATION
366 # is correctly output on stderr.
368 # If BISON-OPTIONS contains `%error-verbose', then make sure the
369 # IF-YYERROR-VERBOSE message is properly output after `parse error, '
372 # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
373 # is the number of expected lines on stderr.
374 m4_define([_AT_CHECK_CALC_ERROR],
375 [m4_bmatch([$2], [^/],
376 [AT_PARSER_CHECK([./calc $2], 0, [], [stderr])],
380 AT_PARSER_CHECK([./calc input], 0, [], [stderr])])
382 [%debug.*%glr\|%glr.*%debug],
385 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
388 # Normalize the observed and expected error messages, depending upon the
390 # 1. Remove the traces from observed.
401 /^yydestructor:/d' stderr >at-stderr
403 # 2. Create the reference error message.
407 # 3. If locations are not used, remove them.
408 m4_bmatch([$1], [%locations], [],
409 [[sed 's/^[-0-9.]*: //' expout >at-expout
410 mv at-expout expout]])
411 # 4. If error-verbose is not used, strip the`, unexpected....' part.
412 m4_bmatch([$1], [%error-verbose], [],
413 [[sed 's/parse error, .*$/parse error/' expout >at-expout
414 mv at-expout expout]])
416 AT_CHECK([cat stderr], 0, [expout])
420 # AT_CHECK_CALC([BISON-OPTIONS])
421 # ------------------------------
422 # Start a testing chunk which compiles `calc' grammar with
423 # BISON-OPTIONS, and performs several tests over the parser.
424 m4_define([AT_CHECK_CALC],
425 [# We use integers to avoid dependencies upon the precision of doubles.
426 AT_SETUP([Calculator $1])
430 # Specify the output files to avoid problems on different file systems.
431 AT_CHECK([bison -o calc.c m4_bpatsubst([$1], [%[^ ]*]) calc.y],
436 # Test the priorities.
454 _AT_CHECK_CALC_ERROR([$1], [0 0], [11],
455 [1.3-1.4: parse error, unexpected "number"])
456 _AT_CHECK_CALC_ERROR([$1], [1//2], [15],
457 [1.3-1.4: parse error, unexpected '/', expecting "number" or '-' or '('])
458 _AT_CHECK_CALC_ERROR([$1], [error], [4],
459 [1.1-1.2: parse error, unexpected $undefined, expecting "number" or '-' or '\n' or '('])
460 _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [22],
461 [1.7-1.8: parse error, unexpected '='])
462 _AT_CHECK_CALC_ERROR([$1],
466 [2.1-2.2: parse error, unexpected '+'])
467 # Exercise error messages with EOF: work on an empty file.
468 _AT_CHECK_CALC_ERROR([$1], [/dev/null], [4],
469 [1.1-1.2: parse error, unexpected "end of input", expecting "number" or '-' or '\n' or '('])
471 # Exercise the error token: without it, we die at the first error,
472 # hence be sure i. to have several errors, ii. to test the action
473 # associated to `error'.
474 _AT_CHECK_CALC_ERROR([$1], [(1 ++ 2) + (0 0) = 1], [82],
475 [1.5-1.6: parse error, unexpected '+', expecting "number" or '-' or '('
476 1.15-1.16: parse error, unexpected "number"
477 calc: error: 0 != 1])
485 # ------------------------ #
486 # Simple LALR Calculator. #
487 # ------------------------ #
489 AT_BANNER([[Simple LALR Calculator.]])
491 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
492 # -----------------------------------
493 # Start a testing chunk which compiles `calc' grammar with
494 # BISON-OPTIONS, and performs several tests over the parser.
495 m4_define([AT_CHECK_CALC_LALR],
500 AT_CHECK_CALC_LALR([--defines])
501 AT_CHECK_CALC_LALR([%locations])
502 AT_CHECK_CALC_LALR([--name-prefix=calc])
503 AT_CHECK_CALC_LALR([--verbose])
504 AT_CHECK_CALC_LALR([--yacc])
505 AT_CHECK_CALC_LALR([%error-verbose])
507 AT_CHECK_CALC_LALR([%error-verbose %locations])
509 AT_CHECK_CALC_LALR([%error-verbose %locations --defines --name-prefix=calc --verbose --yacc])
511 AT_CHECK_CALC_LALR([%debug])
512 AT_CHECK_CALC_LALR([%error-verbose %debug %locations --defines --name-prefix=calc --verbose --yacc])
514 # FIXME: Not ready yet.
515 # AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations --defines --name-prefix=calc --verbose --yacc])
518 # ----------------------- #
519 # Simple GLR Calculator. #
520 # ----------------------- #
522 AT_BANNER([[Simple GLR Calculator.]])
524 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
525 # ----------------------------------
526 # Start a testing chunk which compiles `calc' grammar with
527 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
528 m4_define([AT_CHECK_CALC_GLR],
529 [AT_CHECK_CALC([%glr-parser] $@)])
534 AT_CHECK_CALC_GLR([--defines])
535 AT_CHECK_CALC_GLR([%locations])
536 AT_CHECK_CALC_GLR([--name-prefix=calc])
537 AT_CHECK_CALC_GLR([--verbose])
538 AT_CHECK_CALC_GLR([--yacc])
539 AT_CHECK_CALC_GLR([%error-verbose])
541 AT_CHECK_CALC_GLR([%error-verbose %locations])
543 AT_CHECK_CALC_GLR([%error-verbose %locations --defines --name-prefix=calc --verbose --yacc])
545 AT_CHECK_CALC_GLR([%debug])
546 AT_CHECK_CALC_GLR([%error-verbose %debug %locations --defines --name-prefix=calc --verbose --yacc])
548 # AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations --defines --name-prefix=calc --verbose --yacc])