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