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