]>
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 | ||
67 | /* BISON Declarations */ | |
68 | %token NUM | |
69 | ||
70 | %nonassoc '=' /* comparison */ | |
71 | %left '-' '+' | |
72 | %left '*' '/' | |
73 | %left NEG /* negation--unary minus */ | |
74 | %right '^' /* exponentiation */ | |
75 | ||
76 | /* Grammar follows */ | |
77 | %% | |
78 | input: | |
79 | /* empty string */ | |
80 | | input line | |
81 | ; | |
82 | ||
83 | line: | |
84 | '\n' | |
85 | | exp '\n' | |
86 | ; | |
87 | ||
88 | exp: | |
89 | NUM { $$ = $1; } | |
90 | | exp '=' exp | |
91 | { | |
92 | if ($1 != $3) | |
93 | printf ("calc: error: %d != %d\n", $1, $3); | |
94 | $$ = $1 == $3; | |
95 | } | |
96 | | exp '+' exp { $$ = $1 + $3; } | |
97 | | exp '-' exp { $$ = $1 - $3; } | |
98 | | exp '*' exp { $$ = $1 * $3; } | |
99 | | exp '/' exp { $$ = $1 / $3; } | |
100 | | '-' exp %prec NEG { $$ = -$2; } | |
101 | | exp '^' exp { $$ = power ($1, $3); } | |
102 | | '(' exp ')' { $$ = $2; } | |
103 | ; | |
104 | %% | |
105 | /* The input. */ | |
106 | FILE *yyin; | |
107 | ||
108 | static void | |
109 | yyerror (const char *s) | |
110 | { | |
19c50364 | 111 | #if YYLSP_NEEDED |
bfb07874 AD |
112 | fprintf (stderr, "%d.%d:%d.%d: ", |
113 | yylloc.first_line, yylloc.first_column, | |
114 | yylloc.last_line, yylloc.last_column); | |
115 | #endif | |
116 | fprintf (stderr, "%s\n", s); | |
117 | } | |
118 | ||
119 | static int | |
2ce10144 | 120 | yygetc (void) |
bfb07874 AD |
121 | { |
122 | int res = getc (yyin); | |
19c50364 | 123 | #if YYLSP_NEEDED |
bfb07874 AD |
124 | if (res == '\n') |
125 | { | |
126 | yylloc.last_line++; | |
127 | yylloc.last_column = 0; | |
128 | } | |
129 | else | |
130 | yylloc.last_column++; | |
131 | #endif | |
132 | return res; | |
133 | } | |
134 | ||
135 | ||
136 | static void | |
137 | yyungetc (int c) | |
138 | { | |
19c50364 | 139 | #if YYLSP_NEEDED |
bfb07874 AD |
140 | /* Wrong when C == `\n'. */ |
141 | yylloc.last_column--; | |
142 | #endif | |
143 | ungetc (c, yyin); | |
144 | } | |
145 | ||
146 | static int | |
147 | read_signed_integer (void) | |
148 | { | |
149 | int c = yygetc (); | |
150 | int sign = 1; | |
151 | int n = 0; | |
152 | ||
153 | if (c == '-') | |
154 | { | |
155 | c = yygetc (); | |
156 | sign = -1; | |
157 | } | |
158 | ||
159 | while (isdigit (c)) | |
160 | { | |
161 | n = 10 * n + (c - '0'); | |
162 | c = yygetc (); | |
163 | } | |
164 | ||
165 | yyungetc (c); | |
166 | ||
167 | return sign * n; | |
168 | } | |
169 | ||
170 | ||
171 | ||
172 | /*---------------------------------------------------------------. | |
173 | | Lexical analyzer returns an integer on the stack and the token | | |
174 | | NUM, or the ASCII character read if not a number. Skips all | | |
175 | | blanks and tabs, returns 0 for EOF. | | |
176 | `---------------------------------------------------------------*/ | |
177 | ||
178 | static int | |
179 | yylex (void) | |
180 | { | |
181 | int c; | |
182 | ||
19c50364 | 183 | #if YYLSP_NEEDED |
bfb07874 AD |
184 | yylloc.first_column = yylloc.last_column; |
185 | yylloc.first_line = yylloc.last_line; | |
186 | #endif | |
187 | ||
188 | /* Skip white space. */ | |
189 | while ((c = yygetc ()) == ' ' || c == '\t') | |
190 | { | |
19c50364 | 191 | #if YYLSP_NEEDED |
bfb07874 AD |
192 | yylloc.first_column = yylloc.last_column; |
193 | yylloc.first_line = yylloc.last_line; | |
194 | #endif | |
195 | } | |
196 | ||
197 | /* process numbers */ | |
198 | if (c == '.' || isdigit (c)) | |
199 | { | |
200 | yyungetc (c); | |
201 | yylval = read_signed_integer (); | |
202 | return NUM; | |
203 | } | |
204 | ||
205 | /* Return end-of-file. */ | |
206 | if (c == EOF) | |
207 | return 0; | |
208 | ||
209 | /* Return single chars. */ | |
210 | return c; | |
211 | } | |
212 | ||
213 | static int | |
214 | power (int base, int exponent) | |
215 | { | |
216 | int res = 1; | |
217 | if (exponent < 0) | |
218 | exit (1); | |
219 | for (/* Niente */; exponent; --exponent) | |
220 | res *= base; | |
221 | return res; | |
222 | } | |
223 | ||
224 | int | |
342b8b6e | 225 | main (int argc, const char **argv) |
bfb07874 | 226 | { |
342b8b6e AD |
227 | yyin = NULL; |
228 | ||
229 | if (argc == 2) | |
bfb07874 AD |
230 | yyin = fopen (argv[1], "r"); |
231 | else | |
232 | yyin = stdin; | |
233 | ||
342b8b6e | 234 | if (!yyin) |
bfb07874 AD |
235 | { |
236 | perror (argv[1]); | |
237 | exit (1); | |
238 | } | |
239 | ||
240 | #if YYDEBUG | |
241 | yydebug = 1; | |
242 | #endif | |
19c50364 | 243 | #if YYLSP_NEEDED |
bfb07874 AD |
244 | yylloc.last_column = 0; |
245 | yylloc.last_line = 1; | |
246 | #endif | |
247 | yyparse (); | |
248 | return 0; | |
249 | } | |
250 | ]]) | |
251 | ])# _AT_DATA_CALC_Y | |
252 | ||
253 | ||
254 | # AT_DATA_CALC_Y([BISON-OPTIONS]) | |
255 | # ------------------------------- | |
256 | # Produce `calc.y'. | |
342b8b6e | 257 | m4_define([AT_DATA_CALC_Y], |
bfb07874 | 258 | [_AT_DATA_CALC_Y($[1], $[2], $[3], |
342b8b6e AD |
259 | [m4_match([$1], [--yyerror-verbose], |
260 | [[#define YYERROR_VERBOSE 1]])])]) | |
bfb07874 AD |
261 | |
262 | ||
263 | ||
342b8b6e AD |
264 | # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0]) |
265 | # ------------------------------------------------------------ | |
bfb07874 | 266 | # Run `calc' on INPUT and expect no STDOUT nor STDERR. |
342b8b6e AD |
267 | # |
268 | # If BISON-OPTIONS contains `--debug', then NUM-STDERR-LINES is the number | |
269 | # of expected lines on stderr. | |
270 | m4_define([_AT_CHECK_CALC], | |
271 | [AT_DATA([[input]], | |
272 | [[$2 | |
273 | ]]) | |
274 | AT_CHECK([calc input], 0, [], [stderr])dnl | |
275 | AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, | |
276 | [m4_match([$1], [--debug], | |
277 | [$3], [0]) | |
278 | ]) | |
279 | ]) | |
280 | ||
281 | ||
282 | # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, [NUM-DEBUG-LINES], | |
bfb07874 AD |
283 | # [ERROR-LOCATION], [IF-YYERROR-VERBOSE]) |
284 | # ------------------------------------------------------------ | |
342b8b6e AD |
285 | # Run `calc' on INPUT, and expect a `parse error' message. |
286 | # | |
287 | # If BISON-OPTIONS contains `--location', then make sure the ERROR-LOCATION | |
288 | # is correctly output on stderr. | |
289 | # | |
290 | # If BISON-OPTIONS contains `--yyerror-verbose', then make sure the | |
291 | # IF-YYERROR-VERBOSE message is properly output after `parse error, ' | |
292 | # on STDERR. | |
293 | # | |
294 | # If BISON-OPTIONS contains `--debug', then NUM-STDERR-LINES is the number | |
295 | # of expected lines on stderr. | |
296 | m4_define([_AT_CHECK_CALC_ERROR], | |
297 | [AT_DATA([[input]], | |
298 | [[$2 | |
299 | ]]) | |
300 | ||
301 | AT_CHECK([calc input], 0, [], [stderr]) | |
302 | ||
303 | ||
304 | AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, | |
305 | [m4_match([$1], [--debug], | |
306 | [$3], [1]) | |
307 | ]) | |
308 | ||
309 | egrep -v '^((Start|Enter|Read|Reduc|Shift)ing|state|Error:) ' stderr >at-stderr | |
310 | mv at-stderr stderr | |
311 | ||
312 | AT_CHECK([cat stderr], 0, | |
313 | [m4_match([$1], [--location], [$4: ])[]dnl | |
bfb07874 | 314 | parse error[]dnl |
342b8b6e AD |
315 | m4_match([$1], [--yyerror-verbose], [, $5])[]dnl |
316 | ||
317 | ]) | |
bfb07874 | 318 | |
342b8b6e | 319 | ]) |
bfb07874 AD |
320 | |
321 | ||
322 | # AT_CHECK_CALC([BISON-OPTIONS], [PARSER-EXPECTED-STDERR]) | |
323 | # -------------------------------------------------------- | |
324 | # Start a testing chunk which compiles `calc' grammar with | |
325 | # BISON-OPTIONS, and performs several tests over the parser. | |
342b8b6e | 326 | m4_define([AT_CHECK_CALC], |
bfb07874 AD |
327 | [# We use integers to avoid dependencies upon the precision of doubles. |
328 | AT_SETUP([Calculator $1]) | |
329 | ||
330 | AT_DATA_CALC_Y([$1]) | |
331 | ||
332 | # Specify the output files to avoid problems on different file systems. | |
342b8b6e | 333 | AT_CHECK([bison calc.y -o calc.c m4_patsubst([$1], [--yyerror-verbose])], |
bfb07874 | 334 | [0], [], []) |
8303fc42 | 335 | AT_CHECK([$CC $CFLAGS $CPPFLAGS calc.c -o calc], 0, [], []) |
bfb07874 AD |
336 | |
337 | # Test the priorities. | |
338 | _AT_CHECK_CALC([$1], | |
339 | [1 + 2 * 3 = 7 | |
340 | 1 + 2 * -3 = -5 | |
341 | ||
342 | -1^2 = -1 | |
343 | (-1)^2 = 1 | |
344 | ||
345 | ---1 = -1 | |
346 | ||
347 | 1 - 2 - 3 = -4 | |
348 | 1 - (2 - 3) = 2 | |
349 | ||
350 | 2^2^3 = 256 | |
342b8b6e | 351 | (2^2)^3 = 64], [491]) |
bfb07874 AD |
352 | |
353 | # Some parse errors. | |
342b8b6e | 354 | _AT_CHECK_CALC_ERROR([$1], [+1], [8], |
bfb07874 | 355 | [1.0:1.1], |
342b8b6e AD |
356 | [unexpected `'+'']) |
357 | _AT_CHECK_CALC_ERROR([$1], [1//2], [17], | |
bfb07874 | 358 | [1.2:1.3], |
342b8b6e AD |
359 | [unexpected `'/'', expecting `NUM' or `'-'' or `'('']) |
360 | _AT_CHECK_CALC_ERROR([$1], [error], [8], | |
bfb07874 | 361 | [1.0:1.1], |
342b8b6e AD |
362 | [unexpected `$undefined.']) |
363 | _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [23], | |
bfb07874 | 364 | [1.6:1.7], |
342b8b6e | 365 | [unexpected `'='']) |
bfb07874 AD |
366 | _AT_CHECK_CALC_ERROR([$1], |
367 | [ | |
368 | +1], | |
342b8b6e | 369 | [16], |
bfb07874 | 370 | [2.0:2.1], |
342b8b6e | 371 | [unexpected `'+'']) |
bfb07874 AD |
372 | |
373 | AT_CLEANUP(calc calc.c calc.h calc.output) | |
374 | ])# AT_CHECK_CALC | |
375 | ||
376 | ||
377 | ||
378 | ||
379 | # ------------------ # | |
380 | # Test the parsers. # | |
381 | # ------------------ # | |
382 | ||
383 | AT_CHECK_CALC() | |
bfb07874 AD |
384 | |
385 | AT_CHECK_CALC([--defines]) | |
386 | AT_CHECK_CALC([--locations]) | |
387 | AT_CHECK_CALC([--name-prefix=calc]) | |
388 | AT_CHECK_CALC([--verbose]) | |
389 | AT_CHECK_CALC([--yacc]) | |
390 | AT_CHECK_CALC([--yyerror-verbose]) | |
391 | ||
392 | AT_CHECK_CALC([--locations --yyerror-verbose]) | |
393 | ||
394 | AT_CHECK_CALC([--defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose]) | |
395 | ||
396 | AT_CHECK_CALC([--debug]) | |
397 | AT_CHECK_CALC([--debug --defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose]) |