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], [$1])
331 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
332 # ------------------------------------------------------------
333 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
335 # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
336 # NUM-STDERR-LINES is the number of expected lines on stderr.
338 # We don't count GLR's traces yet, since its traces are somewhat
339 # different from LALR's.
340 m4_define([_AT_CHECK_CALC],
344 AT_PARSER_CHECK([./calc input], 0, [], [stderr])
346 [%debug.*%glr\|%glr.*%debug],
349 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
354 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, [NUM-DEBUG-LINES],
355 # [ERROR-LOCATION], [IF-YYERROR-VERBOSE])
356 # ------------------------------------------------------------
357 # Run `calc' on INPUT, and expect a `parse error' message.
359 # If INPUT starts with a slash, it is used as absolute input file name,
360 # otherwise as contents.
362 # If BISON-OPTIONS contains `%location', then make sure the ERROR-LOCATION
363 # is correctly output on stderr.
365 # If BISON-OPTIONS contains `%error-verbose', then make sure the
366 # IF-YYERROR-VERBOSE message is properly output after `parse error, '
369 # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
370 # is the number of expected lines on stderr.
371 m4_define([_AT_CHECK_CALC_ERROR],
372 [m4_bmatch([$2], [^/],
373 [AT_PARSER_CHECK([./calc $2], 0, [], [stderr])],
377 AT_PARSER_CHECK([./calc input], 0, [], [stderr])])
379 [%debug.*%glr\|%glr.*%debug],
382 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
385 # Normalize the observed and expected error messages, depending upon the
387 # 1. Remove the traces from observed.
398 /^yydestructor:/d' stderr >at-stderr
400 # 2. Create the reference error message.
404 # 3. If locations are not used, remove them.
405 m4_bmatch([$1], [%locations], [],
406 [[sed 's/^[-0-9.]*: //' expout >at-expout
407 mv at-expout expout]])
408 # 4. If error-verbose is not used, strip the`, unexpected....' part.
409 m4_bmatch([$1], [%error-verbose], [],
410 [[sed 's/parse error, .*$/parse error/' expout >at-expout
411 mv at-expout expout]])
413 AT_CHECK([cat stderr], 0, [expout])
417 # AT_CHECK_CALC([BISON-OPTIONS])
418 # ------------------------------
419 # Start a testing chunk which compiles `calc' grammar with
420 # BISON-OPTIONS, and performs several tests over the parser.
421 m4_define([AT_CHECK_CALC],
422 [# We use integers to avoid dependencies upon the precision of doubles.
423 AT_SETUP([Calculator $1])
427 # Specify the output files to avoid problems on different file systems.
428 AT_CHECK([bison -o calc.c calc.y],
433 # Test the priorities.
451 _AT_CHECK_CALC_ERROR([$1], [0 0], [11],
452 [1.3-1.4: parse error, unexpected "number"])
453 _AT_CHECK_CALC_ERROR([$1], [1//2], [15],
454 [1.3-1.4: parse error, unexpected '/', expecting "number" or '-' or '('])
455 _AT_CHECK_CALC_ERROR([$1], [error], [4],
456 [1.1-1.2: parse error, unexpected $undefined, expecting "number" or '-' or '\n' or '('])
457 _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [22],
458 [1.7-1.8: parse error, unexpected '='])
459 _AT_CHECK_CALC_ERROR([$1],
463 [2.1-2.2: parse error, unexpected '+'])
464 # Exercise error messages with EOF: work on an empty file.
465 _AT_CHECK_CALC_ERROR([$1], [/dev/null], [4],
466 [1.1-1.2: parse error, unexpected "end of input", expecting "number" or '-' or '\n' or '('])
468 # Exercise the error token: without it, we die at the first error,
469 # hence be sure i. to have several errors, ii. to test the action
470 # associated to `error'.
471 _AT_CHECK_CALC_ERROR([$1], [(1 ++ 2) + (0 0) = 1], [82],
472 [1.5-1.6: parse error, unexpected '+', expecting "number" or '-' or '('
473 1.15-1.16: parse error, unexpected "number"
474 calc: error: 0 != 1])
482 # ------------------------ #
483 # Simple LALR Calculator. #
484 # ------------------------ #
486 AT_BANNER([[Simple LALR Calculator.]])
488 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
489 # -----------------------------------
490 # Start a testing chunk which compiles `calc' grammar with
491 # BISON-OPTIONS, and performs several tests over the parser.
492 m4_define([AT_CHECK_CALC_LALR],
497 AT_CHECK_CALC_LALR([%defines])
498 AT_CHECK_CALC_LALR([%locations])
499 AT_CHECK_CALC_LALR([%name-prefix="calc"])
500 AT_CHECK_CALC_LALR([%verbose])
501 AT_CHECK_CALC_LALR([%yacc])
502 AT_CHECK_CALC_LALR([%error-verbose])
504 AT_CHECK_CALC_LALR([%error-verbose %locations])
506 AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
508 AT_CHECK_CALC_LALR([%debug])
509 AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
511 # AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
514 # ----------------------- #
515 # Simple GLR Calculator. #
516 # ----------------------- #
518 AT_BANNER([[Simple GLR Calculator.]])
520 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
521 # ----------------------------------
522 # Start a testing chunk which compiles `calc' grammar with
523 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
524 m4_define([AT_CHECK_CALC_GLR],
525 [AT_CHECK_CALC([%glr-parser] $@)])
530 AT_CHECK_CALC_GLR([%defines])
531 AT_CHECK_CALC_GLR([%locations])
532 AT_CHECK_CALC_GLR([%name-prefix="calc"])
533 AT_CHECK_CALC_GLR([%verbose])
534 AT_CHECK_CALC_GLR([%yacc])
535 AT_CHECK_CALC_GLR([%error-verbose])
537 AT_CHECK_CALC_GLR([%error-verbose %locations])
539 AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
541 AT_CHECK_CALC_GLR([%debug])
542 AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
544 # AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])