1 # Checking the output filenames. -*- Autotest -*-
2 # Copyright (C) 2000, 2001, 2002, 2003 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 global_result = 0;
56 static int global_count = 0;
59 /* Exercise %union. */
66 static int power (int base, int exponent);
67 ]AT_LALR1_CC_IF([typedef yy::Location YYLTYPE;],
68 [/* yyerror receives the location if:
69 - %location & %pure & %glr
70 - %location & %pure & %yacc & %parse-param. */
71 static void yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])
72 AT_PARAM_IF([value *result, int *count, ])
75 static int yylex (]AT_LEX_FORMALS[);
76 static int yygetc (]AT_LEX_FORMALS[);
77 static void yyungetc (]AT_LEX_PRE_FORMALS[ int c);
80 /* Bison Declarations */
81 %token CALC_EOF 0 "end of input"
82 %token <ival> NUM "number"
85 %nonassoc '=' /* comparison */
88 %left NEG /* negation--unary minus */
89 %right '^' /* exponentiation */
95 | input line { ]AT_PARAM_IF([++*count; ++global_count;])[ }
100 | exp '\n' { ]AT_PARAM_IF([*result = global_result = $1;])[ }
108 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
111 | exp '+' exp { $$ = $1 + $3; }
112 | exp '-' exp { $$ = $1 - $3; }
113 | exp '*' exp { $$ = $1 * $3; }
114 | exp '/' exp { $$ = $1 / $3; }
115 | '-' exp %prec NEG { $$ = -$2; }
116 | exp '^' exp { $$ = power ($1, $3); }
117 | '(' exp ')' { $$ = $2; }
118 | '(' error ')' { $$ = 1111; }
126 [/* Currently, print_ is required in C++. */
128 yy::Parser::print_ ()
131 std::cerr << location;])
134 /* A C++ error reporting function. */
136 yy::Parser::error_ ()
138 std::cerr << AT_LOCATION_IF([location << ": " << ])message << std::endl;
144 yy::Parser parser (!!YYDEBUG[]AT_LOCATION_IF([, yy::Location::Location ()]));
145 return parser.parse ();
149 yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])
150 AT_PARAM_IF([value *result, int *count, ])
153 AT_PARAM_IF([(void) result; (void) count;])
154 AT_YYERROR_SEES_LOC_IF([
155 fprintf (stderr, "%d.%d",
156 AT_LOC.first_line, AT_LOC.first_column);
157 if (AT_LOC.first_line != AT_LOC.last_line)
158 fprintf (stderr, "-%d.%d",
159 AT_LOC.last_line, AT_LOC.last_column - 1);
160 else if (AT_LOC.first_column != AT_LOC.last_column - 1)
161 fprintf (stderr, "-%d",
162 AT_LOC.last_column - 1);
163 fprintf (stderr, ": ");])
164 fprintf (stderr, "%s\n", s);
169 static YYLTYPE last_yylloc;
172 yygetc (]AT_LEX_FORMALS[)
174 int res = getc (yyin);
177 last_yylloc = AT_LOC;
182 AT_LOC.end.column = 0;],
183 [ AT_LOC.last_line++;
184 AT_LOC.last_column = 0;])
188 [ AT_LOC.end.column++;],
189 [ AT_LOC.last_column++;])
196 yyungetc (]AT_LEX_PRE_FORMALS[ int c)
200 /* Wrong when C == `\n'. */
201 AT_LOC = last_yylloc;
207 read_signed_integer (]AT_LEX_FORMALS[)
209 int c = yygetc (]AT_LEX_ARGS[);
216 c = yygetc (]AT_LEX_ARGS[);
222 n = 10 * n + (c - '0');
223 c = yygetc (]AT_LEX_ARGS[);
226 yyungetc (]AT_LEX_PRE_ARGS[ c);
233 /*---------------------------------------------------------------.
234 | Lexical analyzer returns an integer on the stack and the token |
235 | NUM, or the ASCII character read if not a number. Skips all |
236 | blanks and tabs, returns 0 for EOF. |
237 `---------------------------------------------------------------*/
240 yylex (]AT_LEX_FORMALS[)
250 AT_LOC.last_column = 0;
251 AT_LOC.last_line = 1;
255 ]AT_LOCATION_IF([AT_LALR1_CC_IF(
256 [ AT_LOC.begin = AT_LOC.end;],
257 [ AT_LOC.first_column = AT_LOC.last_column;
258 AT_LOC.first_line = AT_LOC.last_line;
261 /* Skip white space. */
262 while ((c = yygetc (]AT_LEX_ARGS[)) == ' ' || c == '\t')
264 ]AT_LOCATION_IF([AT_LALR1_CC_IF(
265 [ AT_LOC.begin = AT_LOC.end;],
266 [ AT_LOC.first_column = AT_LOC.last_column;
267 AT_LOC.first_line = AT_LOC.last_line;
271 /* process numbers */
272 if (c == '.' || isdigit (c))
274 yyungetc (]AT_LEX_PRE_ARGS[ c);
275 ]AT_VAL[.ival = read_signed_integer (]AT_LEX_ARGS[);
279 /* Return end-of-file. */
283 /* Return single chars. */
288 power (int base, int exponent)
293 for (/* Niente */; exponent; --exponent)
300 main (int argc, const char **argv)
307 yyin = fopen (argv[1], "r");
317 ]AT_LALR1_CC_IF([], [m4_bmatch([$4], [%debug],
319 status = yyparse (]AT_PARAM_IF([&result, &count])[);
320 if (global_result != result)
322 if (global_count != count)
330 # AT_DATA_CALC_Y([BISON-OPTIONS])
331 # -------------------------------
333 m4_define([AT_DATA_CALC_Y],
334 [_AT_DATA_CALC_Y($[1], $[2], $[3], [$1])
339 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
340 # ------------------------------------------------------------
341 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
343 # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
344 # NUM-STDERR-LINES is the number of expected lines on stderr.
346 # We don't count GLR's traces yet, since its traces are somewhat
347 # different from LALR's.
348 m4_define([_AT_CHECK_CALC],
352 AT_PARSER_CHECK([./calc input], 0, [], [stderr])
354 [%debug.*%glr\|%glr.*%debug],
357 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
362 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT,
364 # [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
365 # ---------------------------------------------------------
366 # Run `calc' on INPUT, and expect a `syntax error' message.
368 # If INPUT starts with a slash, it is used as absolute input file name,
369 # otherwise as contents.
371 # If BISON-OPTIONS contains `%location', then make sure the ERROR-LOCATION
372 # is correctly output on stderr.
374 # If BISON-OPTIONS contains `%error-verbose', then make sure the
375 # IF-YYERROR-VERBOSE message is properly output after `syntax error, '
378 # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
379 # is the number of expected lines on stderr.
380 m4_define([_AT_CHECK_CALC_ERROR],
381 [m4_bmatch([$3], [^/],
382 [AT_PARSER_CHECK([./calc $3], $2, [], [stderr])],
386 AT_PARSER_CHECK([./calc input], $2, [], [stderr])])
388 [%debug.*%glr\|%glr.*%debug],
391 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$4
394 # Normalize the observed and expected error messages, depending upon the
396 # 1. Remove the traces from observed.
407 /^yydestructor:/d' stderr >at-stderr
409 # 2. Create the reference error message.
413 # 3. If locations are not used, remove them.
414 AT_YYERROR_SEES_LOC_IF([],
415 [[sed 's/^[-0-9.]*: //' expout >at-expout
416 mv at-expout expout]])
417 # 4. If error-verbose is not used, strip the`, unexpected....' part.
418 m4_bmatch([$1], [%error-verbose], [],
419 [[sed 's/syntax error, .*$/syntax error/' expout >at-expout
420 mv at-expout expout]])
422 AT_CHECK([cat stderr], 0, [expout])
426 # AT_CHECK_CALC([BISON-OPTIONS])
427 # ------------------------------
428 # Start a testing chunk which compiles `calc' grammar with
429 # BISON-OPTIONS, and performs several tests over the parser.
430 m4_define([AT_CHECK_CALC],
431 [# We use integers to avoid dependencies upon the precision of doubles.
432 AT_SETUP([Calculator $1])
434 AT_BISON_OPTION_PUSHDEFS([$1])
438 # Specify the output files to avoid problems on different file systems.
439 AT_CHECK([bison -o calc.c calc.y])
441 AT_LALR1_CC_IF([AT_COMPILE_CXX([calc])],
442 [AT_COMPILE([calc])])
444 # Test the priorities.
461 # Some syntax errors.
462 _AT_CHECK_CALC_ERROR([$1], [1], [0 0], [11],
463 [1.2: syntax error, unexpected "number"])
464 _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [15],
465 [1.2: syntax error, unexpected '/', expecting "number" or '-' or '(' or '!'])
466 _AT_CHECK_CALC_ERROR([$1], [1], [error], [4],
467 [1.0: syntax error, unexpected $undefined])
468 _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [22],
469 [1.6: syntax error, unexpected '='])
470 _AT_CHECK_CALC_ERROR([$1], [1],
474 [2.0: syntax error, unexpected '+'])
475 # Exercise error messages with EOF: work on an empty file.
476 _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4],
477 [1.0: syntax error, unexpected "end of input"])
479 # Exercise the error token: without it, we die at the first error,
482 # - have several errors which exercise different shift/discardings
483 # - (): nothing to pop, nothing to discard
484 # - (1 + 1 + 1 +): a lot to pop, nothing to discard
485 # - (* * *): nothing to pop, a lot to discard
486 # - (1 + 2 * *): some to pop and discard
488 # - test the action associated to `error'
490 # - check the lookahead that triggers an error is not discarded
491 # when we enter error recovery. Below, the lookahead causing the
492 # first error is ")", which is needed to recover from the error and
493 # produce the "0" that triggers the "0 != 1" error.
495 _AT_CHECK_CALC_ERROR([$1], [0],
496 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
498 [1.1: syntax error, unexpected ')', expecting "number" or '-' or '(' or '!'
499 1.17: syntax error, unexpected ')', expecting "number" or '-' or '(' or '!'
500 1.22: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!'
501 1.40: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!'
502 calc: error: 4444 != 1])
504 # The same, but this time exercising explicitly triggered syntax errors.
505 # POSIX says the lookahead causing the error should not be discarded.
506 _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [64],
507 [1.9: syntax error, unexpected "number"
508 calc: error: 2222 != 1])
509 AT_BISON_OPTION_POPDEFS
517 # ------------------------ #
518 # Simple LALR Calculator. #
519 # ------------------------ #
521 AT_BANNER([[Simple LALR Calculator.]])
523 # AT_CHECK_CALC_LALR([BISON-OPTIONS])
524 # -----------------------------------
525 # Start a testing chunk which compiles `calc' grammar with
526 # BISON-OPTIONS, and performs several tests over the parser.
527 m4_define([AT_CHECK_CALC_LALR],
532 AT_CHECK_CALC_LALR([%defines])
533 AT_CHECK_CALC_LALR([%locations])
534 AT_CHECK_CALC_LALR([%name-prefix="calc"])
535 AT_CHECK_CALC_LALR([%verbose])
536 AT_CHECK_CALC_LALR([%yacc])
537 AT_CHECK_CALC_LALR([%error-verbose])
539 AT_CHECK_CALC_LALR([%error-verbose %locations])
541 AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
543 AT_CHECK_CALC_LALR([%debug])
544 AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
546 AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
548 AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {value *result} %parse-param {int *count}])
551 # ----------------------- #
552 # Simple GLR Calculator. #
553 # ----------------------- #
555 AT_BANNER([[Simple GLR Calculator.]])
557 # AT_CHECK_CALC_GLR([BISON-OPTIONS])
558 # ----------------------------------
559 # Start a testing chunk which compiles `calc' grammar with
560 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
561 m4_define([AT_CHECK_CALC_GLR],
562 [AT_CHECK_CALC([%glr-parser] $@)])
567 AT_CHECK_CALC_GLR([%defines])
568 AT_CHECK_CALC_GLR([%locations])
569 AT_CHECK_CALC_GLR([%name-prefix="calc"])
570 AT_CHECK_CALC_GLR([%verbose])
571 AT_CHECK_CALC_GLR([%yacc])
572 AT_CHECK_CALC_GLR([%error-verbose])
574 AT_CHECK_CALC_GLR([%error-verbose %locations])
576 AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
578 AT_CHECK_CALC_GLR([%debug])
579 AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
581 AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
583 AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {value *result} %parse-param {int *count}])
586 # ----------------------------- #
587 # Simple LALR1 C++ Calculator. #
588 # ----------------------------- #
590 AT_BANNER([[Simple LALR1 C++ Calculator.]])
592 # AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS])
593 # ---------------------------------------
594 # Start a testing chunk which compiles `calc' grammar with
595 # BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
596 m4_define([AT_CHECK_CALC_LALR1_CC],
597 [AT_CHECK_CALC([%skeleton "lalr1.cc"] $@)])
599 # AT_CHECK_CALC_LALR1_CC()
601 AT_CHECK_CALC_LALR1_CC([%defines %locations])
603 AT_CHECK_CALC_LALR1_CC([%defines])
604 # AT_CHECK_CALC_LALR1_CC([%locations])
605 # AT_CHECK_CALC_LALR1_CC([%name-prefix="calc"])
606 # AT_CHECK_CALC_LALR1_CC([%verbose])
607 # AT_CHECK_CALC_LALR1_CC([%yacc])
608 # AT_CHECK_CALC_LALR1_CC([%error-verbose])
610 # AT_CHECK_CALC_LALR1_CC([%error-verbose %locations])
612 AT_CHECK_CALC_LALR1_CC([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
614 # AT_CHECK_CALC_LALR1_CC([%debug])
615 AT_CHECK_CALC_LALR1_CC([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
617 AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
619 # AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {value *result} %parse-param {int *count}])