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);
57 extern void perror (const char *s);
59 /* Exercise pre-prologue dependency to %union. */
64 /* Exercise %union. */
72 # define LOC (*yylloc)
73 # define VAL (*yylval)
81 # define LEX_FORMALS YYSTYPE *yylval, YYLTYPE *yylloc
82 # define LEX_ARGS yylval, yylloc
83 # define USE_LEX_ARGS (void) yylval; (void) yylloc;
85 # define LEX_FORMALS YYSTYPE *yylval
86 # define LEX_ARGS yylval
87 # define USE_LEX_ARGS (void) yylval
89 # define LEX_PRE_FORMALS LEX_FORMALS,
90 # define LEX_PRE_ARGS LEX_ARGS,
92 # define LEX_FORMALS void
93 # define LEX_PRE_FORMALS
99 static int power (int base, int exponent);
100 static void yyerror (const char *s);
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 */
119 /* Grammar follows */
136 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
139 | exp '+' exp { $$ = $1 + $3; }
140 | exp '-' exp { $$ = $1 - $3; }
141 | exp '*' exp { $$ = $1 * $3; }
142 | exp '/' exp { $$ = $1 / $3; }
143 | '-' exp %prec NEG { $$ = -$2; }
144 | exp '^' exp { $$ = power ($1, $3); }
145 | '(' exp ')' { $$ = $2; }
146 | '(' error ')' { $$ = 0; }
153 yyerror (const char *s)
156 fprintf (stderr, "%d.%d-%d.%d: ",
157 LOC.first_line, LOC.first_column,
158 LOC.last_line, LOC.last_column);
160 fprintf (stderr, "%s\n", s);
165 static YYLTYPE last_yylloc;
170 int res = getc (yyin);
187 yyungetc (LEX_PRE_FORMALS int c)
191 /* Wrong when C == `\n'. */
198 read_signed_integer (LEX_FORMALS)
200 int c = yygetc (LEX_ARGS);
207 c = yygetc (LEX_ARGS);
213 n = 10 * n + (c - '0');
214 c = yygetc (LEX_ARGS);
217 yyungetc (LEX_PRE_ARGS c);
224 /*---------------------------------------------------------------.
225 | Lexical analyzer returns an integer on the stack and the token |
226 | NUM, or the ASCII character read if not a number. Skips all |
227 | blanks and tabs, returns 0 for EOF. |
228 `---------------------------------------------------------------*/
240 yylloc.last_column = 1;
241 yylloc.last_line = 1;
246 LOC.first_column = LOC.last_column;
247 LOC.first_line = LOC.last_line;
250 /* Skip white space. */
251 while ((c = yygetc (LEX_ARGS)) == ' ' || c == '\t')
254 LOC.first_column = LOC.last_column;
255 LOC.first_line = LOC.last_line;
259 /* process numbers */
260 if (c == '.' || isdigit (c))
262 yyungetc (LEX_PRE_ARGS c);
263 VAL.ival = read_signed_integer (LEX_ARGS);
267 /* Return end-of-file. */
271 /* Return single chars. */
276 power (int base, int exponent)
281 for (/* Niente */; exponent; --exponent)
287 main (int argc, const char **argv)
292 yyin = fopen (argv[1], "r");
312 # AT_DATA_CALC_Y([BISON-OPTIONS])
313 # -------------------------------
315 m4_define([AT_DATA_CALC_Y],
316 [_AT_DATA_CALC_Y($[1], $[2], $[3],
317 [m4_bpatsubst([$1], [--[^ ]*])])
322 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
323 # ------------------------------------------------------------
324 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
326 # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
327 # NUM-STDERR-LINES is the number of expected lines on stderr.
329 # We don't count GLR's traces yet, since its traces are somewhat
330 # different from LALR's.
331 m4_define([_AT_CHECK_CALC],
335 AT_PARSER_CHECK([./calc input], 0, [], [stderr])
337 [%debug.*%glr\|%glr.*%debug],
340 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
345 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, [NUM-DEBUG-LINES],
346 # [ERROR-LOCATION], [IF-YYERROR-VERBOSE])
347 # ------------------------------------------------------------
348 # Run `calc' on INPUT, and expect a `parse error' message.
350 # If INPUT starts with a slash, it is used as absolute input file name,
351 # otherwise as contents.
353 # If BISON-OPTIONS contains `--location', then make sure the ERROR-LOCATION
354 # is correctly output on stderr.
356 # If BISON-OPTIONS contains `%error-verbose', then make sure the
357 # IF-YYERROR-VERBOSE message is properly output after `parse error, '
360 # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
361 # is the number of expected lines on stderr.
362 m4_define([_AT_CHECK_CALC_ERROR],
363 [m4_bmatch([$2], [^/],
364 [AT_PARSER_CHECK([./calc $2], 0, [], [stderr])],
368 AT_PARSER_CHECK([./calc input], 0, [], [stderr])])
370 [%debug.*%glr\|%glr.*%debug],
373 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
376 # Normalize the observed and expected error messages, depending upon the
378 # 1. Remove the traces from observed.
389 /^yydestructor:/d' stderr >at-stderr
391 # 2. Create the reference error message.
395 # 3. If locations are not used, remove them.
396 m4_bmatch([$1], [%locations], [],
397 [[sed 's/^[-0-9.]*: //' expout >at-expout
398 mv at-expout expout]])
399 # 4. If error-verbose is not used, strip the`, unexpected....' part.
400 m4_bmatch([$1], [%error-verbose], [],
401 [[sed 's/parse error, .*$/parse error/' expout >at-expout
402 mv at-expout expout]])
404 AT_CHECK([cat stderr], 0, [expout])
408 # AT_CHECK_CALC([BISON-OPTIONS])
409 # ------------------------------
410 # Start a testing chunk which compiles `calc' grammar with
411 # BISON-OPTIONS, and performs several tests over the parser.
412 m4_define([AT_CHECK_CALC],
413 [# We use integers to avoid dependencies upon the precision of doubles.
414 AT_SETUP([Calculator $1])
418 # Specify the output files to avoid problems on different file systems.
419 AT_CHECK([bison -o calc.c m4_bpatsubst([$1], [%[^ ]*]) calc.y],
424 # Test the priorities.
438 (2^2)^3 = 64], [486])
441 _AT_CHECK_CALC_ERROR([$1], [0 0], [11],
442 [1.3-1.4: parse error, unexpected "number"])
443 _AT_CHECK_CALC_ERROR([$1], [1//2], [15],
444 [1.3-1.4: parse error, unexpected '/', expecting "number" or '-' or '('])
445 _AT_CHECK_CALC_ERROR([$1], [error], [4],
446 [1.1-1.2: parse error, unexpected $undefined, expecting "number" or '-' or '\n' or '('])
447 _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [22],
448 [1.7-1.8: parse error, unexpected '='])
449 _AT_CHECK_CALC_ERROR([$1],
453 [2.1-2.2: parse error, unexpected '+'])
454 # Exercise error messages with EOF: work on an empty file.
455 _AT_CHECK_CALC_ERROR([$1], [/dev/null], [4],
456 [1.1-1.2: parse error, unexpected "end of input", expecting "number" or '-' or '\n' or '('])
458 # Exercise the error token: without it, we die at the first error,
459 # hence be sure i. to have several errors, ii. to test the action
460 # associated to `error'.
461 _AT_CHECK_CALC_ERROR([$1], [(1 ++ 2) + (0 0) = 1], [82],
462 [1.5-1.6: parse error, unexpected '+', expecting "number" or '-' or '('
463 1.15-1.16: parse error, unexpected "number"
464 calc: error: 0 != 1])
466 # Add a studid example demonstrating that Bison can further improve the
467 # error message. FIXME: Fix this ridiculous message.
468 _AT_CHECK_CALC_ERROR([$1], [()], [21],
469 [1.2-1.3: parse error, unexpected ')', expecting "number" or '-' or '('])
477 # ------------------------ #
478 # Simple LALR Calculator. #
479 # ------------------------ #
481 AT_BANNER([[Simple LALR Calculator.]])
483 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
484 # -----------------------------------
485 # Start a testing chunk which compiles `calc' grammar with
486 # BISON-OPTIONS, and performs several tests over the parser.
487 m4_define([AT_CHECK_CALC_LALR],
492 AT_CHECK_CALC_LALR([--defines])
493 AT_CHECK_CALC_LALR([%locations])
494 AT_CHECK_CALC_LALR([--name-prefix=calc])
495 AT_CHECK_CALC_LALR([--verbose])
496 AT_CHECK_CALC_LALR([--yacc])
497 AT_CHECK_CALC_LALR([%error-verbose])
499 AT_CHECK_CALC_LALR([%error-verbose %locations])
501 AT_CHECK_CALC_LALR([%error-verbose %locations --defines --name-prefix=calc --verbose --yacc])
503 AT_CHECK_CALC_LALR([%debug])
504 AT_CHECK_CALC_LALR([%error-verbose %debug %locations --defines --name-prefix=calc --verbose --yacc])
506 # FIXME: Not ready yet.
507 # AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations --defines --name-prefix=calc --verbose --yacc])
510 # ----------------------- #
511 # Simple GLR Calculator. #
512 # ----------------------- #
514 AT_BANNER([[Simple GLR Calculator.]])
516 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
517 # ----------------------------------
518 # Start a testing chunk which compiles `calc' grammar with
519 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
520 m4_define([AT_CHECK_CALC_GLR],
521 [AT_CHECK_CALC([%glr-parser] $@)])
526 AT_CHECK_CALC_GLR([--defines])
527 AT_CHECK_CALC_GLR([%locations])
528 AT_CHECK_CALC_GLR([--name-prefix=calc])
529 AT_CHECK_CALC_GLR([--verbose])
530 AT_CHECK_CALC_GLR([--yacc])
531 AT_CHECK_CALC_GLR([%error-verbose])
533 AT_CHECK_CALC_GLR([%error-verbose %locations])
535 AT_CHECK_CALC_GLR([%error-verbose %locations --defines --name-prefix=calc --verbose --yacc])
537 AT_CHECK_CALC_GLR([%debug])
538 AT_CHECK_CALC_GLR([%error-verbose %debug %locations --defines --name-prefix=calc --verbose --yacc])