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