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