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