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