1 # Checking the output filenames. -*- Autotest -*-
2 # Copyright 2000, 2001 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 AT_BANNER([[Simple Calculator.]])
21 ## ---------------------------------------------------- ##
22 ## Compile the grammar described in the documentation. ##
23 ## ---------------------------------------------------- ##
26 # ------------------------- #
27 # Helping Autotest macros. #
28 # ------------------------- #
31 # _AT_DATA_CALC_Y($1, $2, $3, [CPP-DIRECTIVES])
32 # ---------------------------------------------
33 # Produce `calc.y'. Don't call this macro directly, because it contains
34 # some occurrences of `$1' etc. which will be interpreted by m4. So
35 # you should call it with $1, $2, and $3 as arguments, which is what
36 # AT_DATA_CALC_Y does.
37 m4_define([_AT_DATA_CALC_Y],
38 [m4_if([$1$2$3], $[1]$[2]$[3], [],
39 [m4_fatal([$0: Invalid arguments: $@])])dnl
41 [[/* Infix notation calculator--calc */
51 char *strcat(char *dest, const char *src);
56 static int power (int base, int exponent);
57 static void yyerror (const char *s);
58 static int yylex (void);
59 static int yygetc (void);
60 static void yyungetc (int c);
62 extern void perror (const char *s);
65 /* BISON Declarations */
68 %nonassoc '=' /* comparison */
71 %left NEG /* negation--unary minus */
72 %right '^' /* exponentiation */
91 printf ("calc: error: %d != %d\n", $1, $3);
94 | exp '+' exp { $$ = $1 + $3; }
95 | exp '-' exp { $$ = $1 - $3; }
96 | exp '*' exp { $$ = $1 * $3; }
97 | exp '/' exp { $$ = $1 / $3; }
98 | '-' exp %prec NEG { $$ = -$2; }
99 | exp '^' exp { $$ = power ($1, $3); }
100 | '(' exp ')' { $$ = $2; }
107 yyerror (const char *s)
110 fprintf (stderr, "%d.%d:%d.%d: ",
111 yylloc.first_line, yylloc.first_column,
112 yylloc.last_line, yylloc.last_column);
114 fprintf (stderr, "%s\n", s);
120 int res = getc (yyin);
125 yylloc.last_column = 0;
128 yylloc.last_column++;
138 /* Wrong when C == `\n'. */
139 yylloc.last_column--;
145 read_signed_integer (void)
159 n = 10 * n + (c - '0');
170 /*---------------------------------------------------------------.
171 | Lexical analyzer returns an integer on the stack and the token |
172 | NUM, or the ASCII character read if not a number. Skips all |
173 | blanks and tabs, returns 0 for EOF. |
174 `---------------------------------------------------------------*/
182 yylloc.first_column = yylloc.last_column;
183 yylloc.first_line = yylloc.last_line;
186 /* Skip white space. */
187 while ((c = yygetc ()) == ' ' || c == '\t')
190 yylloc.first_column = yylloc.last_column;
191 yylloc.first_line = yylloc.last_line;
195 /* process numbers */
196 if (c == '.' || isdigit (c))
199 yylval = read_signed_integer ();
203 /* Return end-of-file. */
207 /* Return single chars. */
212 power (int base, int exponent)
217 for (/* Niente */; exponent; --exponent)
223 main (int argn, const char **argv)
226 yyin = fopen (argv[1], "r");
240 yylloc.last_column = 0;
241 yylloc.last_line = 1;
250 # AT_DATA_CALC_Y([BISON-OPTIONS])
251 # -------------------------------
253 m4_define([AT_DATA_CALC_Y],
254 [_AT_DATA_CALC_Y($[1], $[2], $[3],
255 [m4_if(m4_regexp([$1], [--yyerror-verbose]),
257 [[#define YYERROR_VERBOSE]])])])
261 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT)
262 # ------------------------------------
263 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
264 # If `--debug' is passed to bison, discard all the debugging traces
265 # preserving only the `parse errors'. Note that since there should be
266 # none, the `grep' will fail with exit status 1.
267 m4_define([_AT_CHECK_CALC],
271 m4_if(m4_regexp([$1], [--debug]),
273 [AT_CHECK([./calc <input],
275 [AT_CHECK([calc ./input 2>&1 >/dev/null | grep 'parse error' >&2],
279 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT,
280 # [ERROR-LOCATION], [IF-YYERROR-VERBOSE])
281 # ------------------------------------------------------------
282 # Run `calc' on INPUT, and expect STDERR.
283 m4_define([_AT_CHECK_CALC_ERROR],
288 AT_CHECK([./calc <input 2>&1 >/dev/null | grep 'parse error' >&2], 0,
290 [m4_if(m4_regexp([$1], [--location]),
291 [-1], [], [$3: ])[]dnl
293 m4_if(m4_regexp([$1], [--yyerror-verbose]),
299 # AT_CHECK_CALC([BISON-OPTIONS], [PARSER-EXPECTED-STDERR])
300 # --------------------------------------------------------
301 # Start a testing chunk which compiles `calc' grammar with
302 # BISON-OPTIONS, and performs several tests over the parser.
303 m4_define([AT_CHECK_CALC],
304 [# We use integers to avoid dependencies upon the precision of doubles.
305 AT_SETUP([Calculator $1])
309 # Specify the output files to avoid problems on different file systems.
310 AT_CHECK([bison calc.y -o calc.c m4_patsubst([$1], [--yyerror-verbose])],
312 AT_CHECK([$CC $CFLAGS $CPPFLAGS calc.c -o calc], 0, [], [])
314 # Test the priorities.
331 _AT_CHECK_CALC_ERROR([$1], [+1],
333 [, unexpected `'+''])
334 _AT_CHECK_CALC_ERROR([$1], [1//2],
336 [, unexpected `'/'', expecting `NUM' or `'-'' or `'(''])
337 _AT_CHECK_CALC_ERROR([$1], [error],
339 [, unexpected `$undefined.'])
340 _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3],
342 [, unexpected `'=''])
343 _AT_CHECK_CALC_ERROR([$1],
347 [, unexpected `'+''])
349 AT_CLEANUP(calc calc.c calc.h calc.output)
355 # ------------------ #
356 # Test the parsers. #
357 # ------------------ #
361 AT_CHECK_CALC([--defines])
362 AT_CHECK_CALC([--locations])
363 AT_CHECK_CALC([--name-prefix=calc])
364 AT_CHECK_CALC([--verbose])
365 AT_CHECK_CALC([--yacc])
366 AT_CHECK_CALC([--yyerror-verbose])
368 AT_CHECK_CALC([--locations --yyerror-verbose])
370 AT_CHECK_CALC([--defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])
372 AT_CHECK_CALC([--debug])
373 AT_CHECK_CALC([--debug --defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])