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