]>
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 | 55 | #include <ctype.h> |
bfb07874 AD |
56 | |
57 | static int power (int base, int exponent); | |
58 | static void yyerror (const char *s); | |
59 | static int yylex (void); | |
2ce10144 AD |
60 | static int yygetc (void); |
61 | static void yyungetc (int c); | |
62 | ||
bfb07874 AD |
63 | extern void perror (const char *s); |
64 | %} | |
65 | ||
213e640e AD |
66 | /* Also exercise %union. */ |
67 | %union | |
68 | { | |
69 | int ival; /* A comment to exercise an old bug. */ | |
70 | }; | |
71 | ||
6b7e85b9 | 72 | /* Bison Declarations */ |
b7c49edf | 73 | %token CALC_EOF 0 "end of file" |
213e640e AD |
74 | %token <ival> NUM "number" |
75 | %type <ival> exp | |
bfb07874 AD |
76 | |
77 | %nonassoc '=' /* comparison */ | |
78 | %left '-' '+' | |
79 | %left '*' '/' | |
80 | %left NEG /* negation--unary minus */ | |
81 | %right '^' /* exponentiation */ | |
82 | ||
147e184c MA |
83 | ]$4[ |
84 | ||
bfb07874 AD |
85 | /* Grammar follows */ |
86 | %% | |
87 | input: | |
b7c49edf | 88 | line |
bfb07874 AD |
89 | | input line |
90 | ; | |
91 | ||
92 | line: | |
93 | '\n' | |
213e640e | 94 | | exp '\n' {} |
bfb07874 AD |
95 | ; |
96 | ||
97 | exp: | |
98 | NUM { $$ = $1; } | |
99 | | exp '=' exp | |
100 | { | |
101 | if ($1 != $3) | |
51dec47b | 102 | fprintf (stderr, "calc: error: %d != %d\n", $1, $3); |
bfb07874 AD |
103 | $$ = $1 == $3; |
104 | } | |
105 | | exp '+' exp { $$ = $1 + $3; } | |
106 | | exp '-' exp { $$ = $1 - $3; } | |
107 | | exp '*' exp { $$ = $1 * $3; } | |
108 | | exp '/' exp { $$ = $1 / $3; } | |
109 | | '-' exp %prec NEG { $$ = -$2; } | |
110 | | exp '^' exp { $$ = power ($1, $3); } | |
111 | | '(' exp ')' { $$ = $2; } | |
51dec47b | 112 | | '(' error ')' { $$ = 0; } |
bfb07874 AD |
113 | ; |
114 | %% | |
115 | /* The input. */ | |
116 | FILE *yyin; | |
117 | ||
118 | static void | |
119 | yyerror (const char *s) | |
120 | { | |
19c50364 | 121 | #if YYLSP_NEEDED |
51dec47b | 122 | fprintf (stderr, "%d.%d-%d.%d: ", |
bfb07874 AD |
123 | yylloc.first_line, yylloc.first_column, |
124 | yylloc.last_line, yylloc.last_column); | |
125 | #endif | |
126 | fprintf (stderr, "%s\n", s); | |
127 | } | |
128 | ||
b7c49edf AD |
129 | |
130 | #if YYLSP_NEEDED | |
131 | static YYLTYPE last_yylloc; | |
132 | #endif | |
bfb07874 | 133 | static int |
2ce10144 | 134 | yygetc (void) |
bfb07874 AD |
135 | { |
136 | int res = getc (yyin); | |
19c50364 | 137 | #if YYLSP_NEEDED |
b7c49edf | 138 | last_yylloc = yylloc; |
bfb07874 AD |
139 | if (res == '\n') |
140 | { | |
141 | yylloc.last_line++; | |
51dec47b | 142 | yylloc.last_column = 1; |
bfb07874 AD |
143 | } |
144 | else | |
145 | yylloc.last_column++; | |
146 | #endif | |
147 | return res; | |
148 | } | |
149 | ||
150 | ||
151 | static void | |
152 | yyungetc (int c) | |
153 | { | |
19c50364 | 154 | #if YYLSP_NEEDED |
bfb07874 | 155 | /* Wrong when C == `\n'. */ |
b7c49edf | 156 | yylloc = last_yylloc; |
bfb07874 AD |
157 | #endif |
158 | ungetc (c, yyin); | |
159 | } | |
160 | ||
161 | static int | |
162 | read_signed_integer (void) | |
163 | { | |
164 | int c = yygetc (); | |
165 | int sign = 1; | |
166 | int n = 0; | |
167 | ||
168 | if (c == '-') | |
169 | { | |
170 | c = yygetc (); | |
171 | sign = -1; | |
172 | } | |
173 | ||
174 | while (isdigit (c)) | |
175 | { | |
176 | n = 10 * n + (c - '0'); | |
177 | c = yygetc (); | |
178 | } | |
179 | ||
180 | yyungetc (c); | |
181 | ||
182 | return sign * n; | |
183 | } | |
184 | ||
185 | ||
186 | ||
187 | /*---------------------------------------------------------------. | |
188 | | Lexical analyzer returns an integer on the stack and the token | | |
189 | | NUM, or the ASCII character read if not a number. Skips all | | |
190 | | blanks and tabs, returns 0 for EOF. | | |
191 | `---------------------------------------------------------------*/ | |
192 | ||
193 | static int | |
194 | yylex (void) | |
195 | { | |
196 | int c; | |
197 | ||
19c50364 | 198 | #if YYLSP_NEEDED |
bfb07874 AD |
199 | yylloc.first_column = yylloc.last_column; |
200 | yylloc.first_line = yylloc.last_line; | |
201 | #endif | |
202 | ||
203 | /* Skip white space. */ | |
204 | while ((c = yygetc ()) == ' ' || c == '\t') | |
205 | { | |
19c50364 | 206 | #if YYLSP_NEEDED |
bfb07874 AD |
207 | yylloc.first_column = yylloc.last_column; |
208 | yylloc.first_line = yylloc.last_line; | |
209 | #endif | |
210 | } | |
211 | ||
212 | /* process numbers */ | |
213 | if (c == '.' || isdigit (c)) | |
214 | { | |
215 | yyungetc (c); | |
213e640e | 216 | yylval.ival = read_signed_integer (); |
bfb07874 AD |
217 | return NUM; |
218 | } | |
219 | ||
220 | /* Return end-of-file. */ | |
221 | if (c == EOF) | |
6b7e85b9 | 222 | return CALC_EOF; |
bfb07874 AD |
223 | |
224 | /* Return single chars. */ | |
225 | return c; | |
226 | } | |
227 | ||
228 | static int | |
229 | power (int base, int exponent) | |
230 | { | |
231 | int res = 1; | |
232 | if (exponent < 0) | |
233 | exit (1); | |
234 | for (/* Niente */; exponent; --exponent) | |
235 | res *= base; | |
236 | return res; | |
237 | } | |
238 | ||
239 | int | |
342b8b6e | 240 | main (int argc, const char **argv) |
bfb07874 | 241 | { |
342b8b6e AD |
242 | yyin = NULL; |
243 | ||
244 | if (argc == 2) | |
bfb07874 AD |
245 | yyin = fopen (argv[1], "r"); |
246 | else | |
247 | yyin = stdin; | |
248 | ||
342b8b6e | 249 | if (!yyin) |
bfb07874 AD |
250 | { |
251 | perror (argv[1]); | |
252 | exit (1); | |
253 | } | |
254 | ||
255 | #if YYDEBUG | |
256 | yydebug = 1; | |
257 | #endif | |
19c50364 | 258 | #if YYLSP_NEEDED |
51dec47b | 259 | yylloc.last_column = 1; |
bfb07874 AD |
260 | yylloc.last_line = 1; |
261 | #endif | |
262 | yyparse (); | |
263 | return 0; | |
264 | } | |
265 | ]]) | |
266 | ])# _AT_DATA_CALC_Y | |
267 | ||
268 | ||
269 | # AT_DATA_CALC_Y([BISON-OPTIONS]) | |
270 | # ------------------------------- | |
271 | # Produce `calc.y'. | |
342b8b6e | 272 | m4_define([AT_DATA_CALC_Y], |
bfb07874 | 273 | [_AT_DATA_CALC_Y($[1], $[2], $[3], |
f987e9d2 | 274 | [m4_bmatch([$1], [--yyerror-verbose], |
147e184c | 275 | [[%error-verbose]])])]) |
bfb07874 AD |
276 | |
277 | ||
278 | ||
342b8b6e AD |
279 | # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0]) |
280 | # ------------------------------------------------------------ | |
bfb07874 | 281 | # Run `calc' on INPUT and expect no STDOUT nor STDERR. |
342b8b6e AD |
282 | # |
283 | # If BISON-OPTIONS contains `--debug', then NUM-STDERR-LINES is the number | |
284 | # of expected lines on stderr. | |
285 | m4_define([_AT_CHECK_CALC], | |
286 | [AT_DATA([[input]], | |
287 | [[$2 | |
288 | ]]) | |
30d2f3d5 | 289 | AT_CHECK([./calc input], 0, [], [stderr])dnl |
342b8b6e | 290 | AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, |
3c1a79b3 AD |
291 | [m4_bmatch([$1], [--debug], |
292 | [$3], [0]) | |
342b8b6e AD |
293 | ]) |
294 | ]) | |
295 | ||
296 | ||
297 | # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, [NUM-DEBUG-LINES], | |
bfb07874 AD |
298 | # [ERROR-LOCATION], [IF-YYERROR-VERBOSE]) |
299 | # ------------------------------------------------------------ | |
342b8b6e AD |
300 | # Run `calc' on INPUT, and expect a `parse error' message. |
301 | # | |
b7c49edf AD |
302 | # If INPUT starts with a slash, it is used as absolute input file name, |
303 | # otherwise as contents. | |
304 | # | |
342b8b6e AD |
305 | # If BISON-OPTIONS contains `--location', then make sure the ERROR-LOCATION |
306 | # is correctly output on stderr. | |
307 | # | |
308 | # If BISON-OPTIONS contains `--yyerror-verbose', then make sure the | |
309 | # IF-YYERROR-VERBOSE message is properly output after `parse error, ' | |
310 | # on STDERR. | |
311 | # | |
312 | # If BISON-OPTIONS contains `--debug', then NUM-STDERR-LINES is the number | |
313 | # of expected lines on stderr. | |
314 | m4_define([_AT_CHECK_CALC_ERROR], | |
b7c49edf | 315 | [m4_bmatch([$2], [^/], |
30d2f3d5 | 316 | [AT_CHECK([./calc $2], 0, [], [stderr])], |
b7c49edf | 317 | [AT_DATA([[input]], |
342b8b6e AD |
318 | [[$2 |
319 | ]]) | |
30d2f3d5 | 320 | AT_CHECK([./calc input], 0, [], [stderr])]) |
342b8b6e | 321 | |
51dec47b AD |
322 | m4_bmatch([$1], [--debug], |
323 | [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3 | |
324 | ])]) | |
342b8b6e | 325 | |
51dec47b AD |
326 | # Normalize the observed and expected error messages, depending upon the |
327 | # options. | |
328 | # 1. Remove the traces from observed. | |
329 | egrep -v '^((Start|Enter|Read|Reduc|Shift)ing|state|Error:|Next|Discarding) ' stderr >at-stderr | |
342b8b6e | 330 | mv at-stderr stderr |
51dec47b AD |
331 | # 2. Create the reference error message. |
332 | AT_DATA([[expout]], | |
333 | [$4 | |
342b8b6e | 334 | ]) |
51dec47b AD |
335 | # 3. If locations are not used, remove them. |
336 | m4_bmatch([$1], [--location], [], | |
337 | [[sed 's/^[-0-9.]*: //' expout >at-expout | |
338 | mv at-expout expout]]) | |
339 | # 4. If error-verbose is not used, strip the`, unexpected....' part. | |
340 | m4_bmatch([$1], [--yyerror-verbose], [], | |
341 | [[sed 's/parse error, .*$/parse error/' expout >at-expout | |
342 | mv at-expout expout]]) | |
343 | # 5. Check | |
344 | AT_CHECK([cat stderr], 0, [expout]) | |
342b8b6e | 345 | ]) |
bfb07874 AD |
346 | |
347 | ||
348 | # AT_CHECK_CALC([BISON-OPTIONS], [PARSER-EXPECTED-STDERR]) | |
349 | # -------------------------------------------------------- | |
350 | # Start a testing chunk which compiles `calc' grammar with | |
351 | # BISON-OPTIONS, and performs several tests over the parser. | |
342b8b6e | 352 | m4_define([AT_CHECK_CALC], |
bfb07874 AD |
353 | [# We use integers to avoid dependencies upon the precision of doubles. |
354 | AT_SETUP([Calculator $1]) | |
355 | ||
356 | AT_DATA_CALC_Y([$1]) | |
357 | ||
358 | # Specify the output files to avoid problems on different file systems. | |
3c1a79b3 | 359 | AT_CHECK([bison calc.y -o calc.c m4_bpatsubst([$1], [--yyerror-verbose])], |
bfb07874 | 360 | [0], [], []) |
3c1a79b3 | 361 | |
d803322e | 362 | AT_CHECK([$CC $CFLAGS $CPPFLAGS calc.c -o calc], 0, [], [ignore]) |
bfb07874 AD |
363 | |
364 | # Test the priorities. | |
365 | _AT_CHECK_CALC([$1], | |
366 | [1 + 2 * 3 = 7 | |
367 | 1 + 2 * -3 = -5 | |
368 | ||
369 | -1^2 = -1 | |
370 | (-1)^2 = 1 | |
371 | ||
372 | ---1 = -1 | |
373 | ||
374 | 1 - 2 - 3 = -4 | |
375 | 1 - (2 - 3) = 2 | |
376 | ||
377 | 2^2^3 = 256 | |
3db472b9 | 378 | (2^2)^3 = 64], [486]) |
bfb07874 AD |
379 | |
380 | # Some parse errors. | |
b7c49edf | 381 | _AT_CHECK_CALC_ERROR([$1], [0 0], [10], |
51dec47b | 382 | [1.3-1.4: parse error, unexpected "number"]) |
b7c49edf | 383 | _AT_CHECK_CALC_ERROR([$1], [1//2], [13], |
51dec47b | 384 | [1.3-1.4: parse error, unexpected '/', expecting "number" or '-' or '(']) |
b7c49edf | 385 | _AT_CHECK_CALC_ERROR([$1], [error], [4], |
51dec47b | 386 | [1.1-1.2: parse error, unexpected $undefined., expecting "number" or '-' or '\n' or '(']) |
b7c49edf | 387 | _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [19], |
51dec47b | 388 | [1.7-1.8: parse error, unexpected '=']) |
bfb07874 AD |
389 | _AT_CHECK_CALC_ERROR([$1], |
390 | [ | |
391 | +1], | |
b7c49edf | 392 | [13], |
51dec47b | 393 | [2.1-2.2: parse error, unexpected '+']) |
b7c49edf AD |
394 | # Exercise error messages with EOF: work on an empty file. |
395 | _AT_CHECK_CALC_ERROR([$1], | |
396 | [/dev/null], | |
397 | [4], | |
51dec47b AD |
398 | [1.1-1.2: parse error, unexpected "end of file", expecting "number" or '-' or '\n' or '(']) |
399 | ||
400 | # Exercise the error token: without it, we die at the first error, | |
401 | # hence be sure i. to have several errors, ii. to test the action | |
402 | # associated to `error'. | |
403 | _AT_CHECK_CALC_ERROR([$1], | |
404 | [(1 ++ 2) + (0 0) = 1], | |
405 | [76], | |
406 | [1.5-1.6: parse error, unexpected '+', expecting "number" or '-' or '(' | |
407 | 1.15-1.16: parse error, unexpected "number" | |
408 | calc: error: 0 != 1]) | |
409 | ||
410 | # Add a studid example demonstrating that Bison can further improve the | |
411 | # error message. FIXME: Fix this ridiculous message. | |
412 | _AT_CHECK_CALC_ERROR([$1], | |
413 | [()], | |
414 | [21], | |
415 | [1.2-1.3: parse error, unexpected ')', expecting error or "number" or '-' or '(']) | |
bfb07874 | 416 | |
d803322e | 417 | AT_CLEANUP |
bfb07874 AD |
418 | ])# AT_CHECK_CALC |
419 | ||
420 | ||
421 | ||
422 | ||
423 | # ------------------ # | |
424 | # Test the parsers. # | |
425 | # ------------------ # | |
426 | ||
427 | AT_CHECK_CALC() | |
bfb07874 AD |
428 | |
429 | AT_CHECK_CALC([--defines]) | |
430 | AT_CHECK_CALC([--locations]) | |
431 | AT_CHECK_CALC([--name-prefix=calc]) | |
432 | AT_CHECK_CALC([--verbose]) | |
433 | AT_CHECK_CALC([--yacc]) | |
434 | AT_CHECK_CALC([--yyerror-verbose]) | |
435 | ||
436 | AT_CHECK_CALC([--locations --yyerror-verbose]) | |
437 | ||
438 | AT_CHECK_CALC([--defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose]) | |
439 | ||
440 | AT_CHECK_CALC([--debug]) | |
441 | AT_CHECK_CALC([--debug --defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose]) |