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