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