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