]>
Commit | Line | Data |
---|---|---|
342b8b6e AD |
1 | # Checking the output filenames. -*- Autotest -*- |
2 | # Copyright 2000, 2001 Free Software Foundation, Inc. | |
bfb07874 | 3 | |
342b8b6e AD |
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) | |
7 | # any later version. | |
bfb07874 | 8 | |
342b8b6e AD |
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. | |
bfb07874 | 13 | |
342b8b6e AD |
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 | |
17 | # 02111-1307, USA. | |
bfb07874 | 18 | |
342b8b6e | 19 | AT_BANNER([[Simple Calculator.]]) |
bfb07874 AD |
20 | |
21 | ## ---------------------------------------------------- ## | |
22 | ## Compile the grammar described in the documentation. ## | |
23 | ## ---------------------------------------------------- ## | |
24 | ||
25 | ||
26 | # ------------------------- # | |
27 | # Helping Autotest macros. # | |
28 | # ------------------------- # | |
29 | ||
30 | ||
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. | |
342b8b6e AD |
37 | m4_define([_AT_DATA_CALC_Y], |
38 | [m4_if([$1$2$3], $[1]$[2]$[3], [], | |
39 | [m4_fatal([$0: Invalid arguments: $@])])dnl | |
bfb07874 AD |
40 | AT_DATA([calc.y], |
41 | [[/* Infix notation calculator--calc */ | |
42 | ||
43 | %{ | |
2ce10144 | 44 | #include <config.h> |
342b8b6e AD |
45 | /* We don't need a perfect malloc for these tests. */ |
46 | #undef malloc | |
bfb07874 | 47 | #include <stdio.h> |
2ce10144 AD |
48 | |
49 | #if STDC_HEADERS | |
50 | # include <stdlib.h> | |
51 | # include <string.h> | |
52 | #else | |
53 | char *strcat(char *dest, const char *src); | |
54 | #endif | |
bfb07874 AD |
55 | #include <ctype.h> |
56 | ]$4[ | |
57 | ||
58 | static int power (int base, int exponent); | |
59 | static void yyerror (const char *s); | |
60 | static int yylex (void); | |
2ce10144 AD |
61 | static int yygetc (void); |
62 | static void yyungetc (int c); | |
63 | ||
bfb07874 AD |
64 | extern void perror (const char *s); |
65 | %} | |
66 | ||
6b7e85b9 AD |
67 | /* Bison Declarations */ |
68 | %token CALC_EOF 0 | |
bfb07874 AD |
69 | %token NUM |
70 | ||
71 | %nonassoc '=' /* comparison */ | |
72 | %left '-' '+' | |
73 | %left '*' '/' | |
74 | %left NEG /* negation--unary minus */ | |
75 | %right '^' /* exponentiation */ | |
76 | ||
77 | /* Grammar follows */ | |
78 | %% | |
79 | input: | |
80 | /* empty string */ | |
81 | | input line | |
82 | ; | |
83 | ||
84 | line: | |
85 | '\n' | |
86 | | exp '\n' | |
87 | ; | |
88 | ||
89 | exp: | |
90 | NUM { $$ = $1; } | |
91 | | exp '=' exp | |
92 | { | |
93 | if ($1 != $3) | |
94 | printf ("calc: error: %d != %d\n", $1, $3); | |
95 | $$ = $1 == $3; | |
96 | } | |
97 | | exp '+' exp { $$ = $1 + $3; } | |
98 | | exp '-' exp { $$ = $1 - $3; } | |
99 | | exp '*' exp { $$ = $1 * $3; } | |
100 | | exp '/' exp { $$ = $1 / $3; } | |
101 | | '-' exp %prec NEG { $$ = -$2; } | |
102 | | exp '^' exp { $$ = power ($1, $3); } | |
103 | | '(' exp ')' { $$ = $2; } | |
104 | ; | |
105 | %% | |
106 | /* The input. */ | |
107 | FILE *yyin; | |
108 | ||
109 | static void | |
110 | yyerror (const char *s) | |
111 | { | |
19c50364 | 112 | #if YYLSP_NEEDED |
bfb07874 AD |
113 | fprintf (stderr, "%d.%d:%d.%d: ", |
114 | yylloc.first_line, yylloc.first_column, | |
115 | yylloc.last_line, yylloc.last_column); | |
116 | #endif | |
117 | fprintf (stderr, "%s\n", s); | |
118 | } | |
119 | ||
120 | static int | |
2ce10144 | 121 | yygetc (void) |
bfb07874 AD |
122 | { |
123 | int res = getc (yyin); | |
19c50364 | 124 | #if YYLSP_NEEDED |
bfb07874 AD |
125 | if (res == '\n') |
126 | { | |
127 | yylloc.last_line++; | |
128 | yylloc.last_column = 0; | |
129 | } | |
130 | else | |
131 | yylloc.last_column++; | |
132 | #endif | |
133 | return res; | |
134 | } | |
135 | ||
136 | ||
137 | static void | |
138 | yyungetc (int c) | |
139 | { | |
19c50364 | 140 | #if YYLSP_NEEDED |
bfb07874 AD |
141 | /* Wrong when C == `\n'. */ |
142 | yylloc.last_column--; | |
143 | #endif | |
144 | ungetc (c, yyin); | |
145 | } | |
146 | ||
147 | static int | |
148 | read_signed_integer (void) | |
149 | { | |
150 | int c = yygetc (); | |
151 | int sign = 1; | |
152 | int n = 0; | |
153 | ||
154 | if (c == '-') | |
155 | { | |
156 | c = yygetc (); | |
157 | sign = -1; | |
158 | } | |
159 | ||
160 | while (isdigit (c)) | |
161 | { | |
162 | n = 10 * n + (c - '0'); | |
163 | c = yygetc (); | |
164 | } | |
165 | ||
166 | yyungetc (c); | |
167 | ||
168 | return sign * n; | |
169 | } | |
170 | ||
171 | ||
172 | ||
173 | /*---------------------------------------------------------------. | |
174 | | Lexical analyzer returns an integer on the stack and the token | | |
175 | | NUM, or the ASCII character read if not a number. Skips all | | |
176 | | blanks and tabs, returns 0 for EOF. | | |
177 | `---------------------------------------------------------------*/ | |
178 | ||
179 | static int | |
180 | yylex (void) | |
181 | { | |
182 | int c; | |
183 | ||
19c50364 | 184 | #if YYLSP_NEEDED |
bfb07874 AD |
185 | yylloc.first_column = yylloc.last_column; |
186 | yylloc.first_line = yylloc.last_line; | |
187 | #endif | |
188 | ||
189 | /* Skip white space. */ | |
190 | while ((c = yygetc ()) == ' ' || c == '\t') | |
191 | { | |
19c50364 | 192 | #if YYLSP_NEEDED |
bfb07874 AD |
193 | yylloc.first_column = yylloc.last_column; |
194 | yylloc.first_line = yylloc.last_line; | |
195 | #endif | |
196 | } | |
197 | ||
198 | /* process numbers */ | |
199 | if (c == '.' || isdigit (c)) | |
200 | { | |
201 | yyungetc (c); | |
202 | yylval = read_signed_integer (); | |
203 | return NUM; | |
204 | } | |
205 | ||
206 | /* Return end-of-file. */ | |
207 | if (c == EOF) | |
6b7e85b9 | 208 | return CALC_EOF; |
bfb07874 AD |
209 | |
210 | /* Return single chars. */ | |
211 | return c; | |
212 | } | |
213 | ||
214 | static int | |
215 | power (int base, int exponent) | |
216 | { | |
217 | int res = 1; | |
218 | if (exponent < 0) | |
219 | exit (1); | |
220 | for (/* Niente */; exponent; --exponent) | |
221 | res *= base; | |
222 | return res; | |
223 | } | |
224 | ||
225 | int | |
342b8b6e | 226 | main (int argc, const char **argv) |
bfb07874 | 227 | { |
342b8b6e AD |
228 | yyin = NULL; |
229 | ||
230 | if (argc == 2) | |
bfb07874 AD |
231 | yyin = fopen (argv[1], "r"); |
232 | else | |
233 | yyin = stdin; | |
234 | ||
342b8b6e | 235 | if (!yyin) |
bfb07874 AD |
236 | { |
237 | perror (argv[1]); | |
238 | exit (1); | |
239 | } | |
240 | ||
241 | #if YYDEBUG | |
242 | yydebug = 1; | |
243 | #endif | |
19c50364 | 244 | #if YYLSP_NEEDED |
bfb07874 AD |
245 | yylloc.last_column = 0; |
246 | yylloc.last_line = 1; | |
247 | #endif | |
248 | yyparse (); | |
249 | return 0; | |
250 | } | |
251 | ]]) | |
252 | ])# _AT_DATA_CALC_Y | |
253 | ||
254 | ||
255 | # AT_DATA_CALC_Y([BISON-OPTIONS]) | |
256 | # ------------------------------- | |
257 | # Produce `calc.y'. | |
342b8b6e | 258 | m4_define([AT_DATA_CALC_Y], |
bfb07874 | 259 | [_AT_DATA_CALC_Y($[1], $[2], $[3], |
f987e9d2 AD |
260 | [m4_bmatch([$1], [--yyerror-verbose], |
261 | [[#define YYERROR_VERBOSE]])])]) | |
bfb07874 AD |
262 | |
263 | ||
264 | ||
342b8b6e AD |
265 | # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0]) |
266 | # ------------------------------------------------------------ | |
bfb07874 | 267 | # Run `calc' on INPUT and expect no STDOUT nor STDERR. |
342b8b6e AD |
268 | # |
269 | # If BISON-OPTIONS contains `--debug', then NUM-STDERR-LINES is the number | |
270 | # of expected lines on stderr. | |
271 | m4_define([_AT_CHECK_CALC], | |
272 | [AT_DATA([[input]], | |
273 | [[$2 | |
274 | ]]) | |
275 | AT_CHECK([calc input], 0, [], [stderr])dnl | |
276 | AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, | |
3c1a79b3 AD |
277 | [m4_bmatch([$1], [--debug], |
278 | [$3], [0]) | |
342b8b6e AD |
279 | ]) |
280 | ]) | |
281 | ||
282 | ||
283 | # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, [NUM-DEBUG-LINES], | |
bfb07874 AD |
284 | # [ERROR-LOCATION], [IF-YYERROR-VERBOSE]) |
285 | # ------------------------------------------------------------ | |
342b8b6e AD |
286 | # Run `calc' on INPUT, and expect a `parse error' message. |
287 | # | |
288 | # If BISON-OPTIONS contains `--location', then make sure the ERROR-LOCATION | |
289 | # is correctly output on stderr. | |
290 | # | |
291 | # If BISON-OPTIONS contains `--yyerror-verbose', then make sure the | |
292 | # IF-YYERROR-VERBOSE message is properly output after `parse error, ' | |
293 | # on STDERR. | |
294 | # | |
295 | # If BISON-OPTIONS contains `--debug', then NUM-STDERR-LINES is the number | |
296 | # of expected lines on stderr. | |
297 | m4_define([_AT_CHECK_CALC_ERROR], | |
298 | [AT_DATA([[input]], | |
299 | [[$2 | |
300 | ]]) | |
301 | ||
302 | AT_CHECK([calc input], 0, [], [stderr]) | |
303 | ||
304 | ||
305 | AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, | |
3c1a79b3 AD |
306 | [m4_bmatch([$1], [--debug], |
307 | [$3], [1]) | |
342b8b6e AD |
308 | ]) |
309 | ||
310 | egrep -v '^((Start|Enter|Read|Reduc|Shift)ing|state|Error:) ' stderr >at-stderr | |
311 | mv at-stderr stderr | |
312 | ||
313 | AT_CHECK([cat stderr], 0, | |
3c1a79b3 | 314 | [m4_bmatch([$1], [--location], [$4: ])[]dnl |
bfb07874 | 315 | parse error[]dnl |
3c1a79b3 | 316 | m4_bmatch([$1], [--yyerror-verbose], [, $5])[]dnl |
342b8b6e AD |
317 | |
318 | ]) | |
bfb07874 | 319 | |
342b8b6e | 320 | ]) |
bfb07874 AD |
321 | |
322 | ||
323 | # AT_CHECK_CALC([BISON-OPTIONS], [PARSER-EXPECTED-STDERR]) | |
324 | # -------------------------------------------------------- | |
325 | # Start a testing chunk which compiles `calc' grammar with | |
326 | # BISON-OPTIONS, and performs several tests over the parser. | |
342b8b6e | 327 | m4_define([AT_CHECK_CALC], |
bfb07874 AD |
328 | [# We use integers to avoid dependencies upon the precision of doubles. |
329 | AT_SETUP([Calculator $1]) | |
330 | ||
331 | AT_DATA_CALC_Y([$1]) | |
332 | ||
333 | # Specify the output files to avoid problems on different file systems. | |
3c1a79b3 | 334 | AT_CHECK([bison calc.y -o calc.c m4_bpatsubst([$1], [--yyerror-verbose])], |
bfb07874 | 335 | [0], [], []) |
3c1a79b3 AD |
336 | |
337 | # Some compilers issue warnings we don't want to hear about. | |
338 | # Maybe some day we will have proper Autoconf macros to disable these | |
339 | # warnings, but this place is not the right one for that. | |
340 | # So let's keep only GCC warnings, which we know are sane. | |
d803322e AD |
341 | # Well, that's only part of the story: some assemblers issue warnings |
342 | # which can be totally useless, and actually polluting. It seems that | |
343 | # the best bet be to completely ignore stderr, but to pass -Werror | |
344 | # to GCC. | |
345 | if test "$GCC" = yes; then | |
346 | CFLAGS="$CFLAGS -Werror" | |
347 | fi | |
348 | AT_CHECK([$CC $CFLAGS $CPPFLAGS calc.c -o calc], 0, [], [ignore]) | |
bfb07874 AD |
349 | |
350 | # Test the priorities. | |
351 | _AT_CHECK_CALC([$1], | |
352 | [1 + 2 * 3 = 7 | |
353 | 1 + 2 * -3 = -5 | |
354 | ||
355 | -1^2 = -1 | |
356 | (-1)^2 = 1 | |
357 | ||
358 | ---1 = -1 | |
359 | ||
360 | 1 - 2 - 3 = -4 | |
361 | 1 - (2 - 3) = 2 | |
362 | ||
363 | 2^2^3 = 256 | |
342b8b6e | 364 | (2^2)^3 = 64], [491]) |
bfb07874 AD |
365 | |
366 | # Some parse errors. | |
342b8b6e | 367 | _AT_CHECK_CALC_ERROR([$1], [+1], [8], |
bfb07874 | 368 | [1.0:1.1], |
342b8b6e AD |
369 | [unexpected `'+'']) |
370 | _AT_CHECK_CALC_ERROR([$1], [1//2], [17], | |
bfb07874 | 371 | [1.2:1.3], |
342b8b6e AD |
372 | [unexpected `'/'', expecting `NUM' or `'-'' or `'('']) |
373 | _AT_CHECK_CALC_ERROR([$1], [error], [8], | |
bfb07874 | 374 | [1.0:1.1], |
342b8b6e AD |
375 | [unexpected `$undefined.']) |
376 | _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [23], | |
bfb07874 | 377 | [1.6:1.7], |
342b8b6e | 378 | [unexpected `'='']) |
bfb07874 AD |
379 | _AT_CHECK_CALC_ERROR([$1], |
380 | [ | |
381 | +1], | |
342b8b6e | 382 | [16], |
bfb07874 | 383 | [2.0:2.1], |
342b8b6e | 384 | [unexpected `'+'']) |
bfb07874 | 385 | |
d803322e | 386 | AT_CLEANUP |
bfb07874 AD |
387 | ])# AT_CHECK_CALC |
388 | ||
389 | ||
390 | ||
391 | ||
392 | # ------------------ # | |
393 | # Test the parsers. # | |
394 | # ------------------ # | |
395 | ||
396 | AT_CHECK_CALC() | |
bfb07874 AD |
397 | |
398 | AT_CHECK_CALC([--defines]) | |
399 | AT_CHECK_CALC([--locations]) | |
400 | AT_CHECK_CALC([--name-prefix=calc]) | |
401 | AT_CHECK_CALC([--verbose]) | |
402 | AT_CHECK_CALC([--yacc]) | |
403 | AT_CHECK_CALC([--yyerror-verbose]) | |
404 | ||
405 | AT_CHECK_CALC([--locations --yyerror-verbose]) | |
406 | ||
407 | AT_CHECK_CALC([--defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose]) | |
408 | ||
409 | AT_CHECK_CALC([--debug]) | |
410 | AT_CHECK_CALC([--debug --defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose]) |