]> git.saurik.com Git - bison.git/blob - tests/calc.at
* src/gram.h (rule_t): `guard' and `guard_line' are new members.
[bison.git] / tests / calc.at
1 # Checking the output filenames. -*- Autotest -*-
2 # Copyright 2000, 2001 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 AT_BANNER([[Simple Calculator.]])
20
21 ## ---------------------------------------------------- ##
22 ## Compile the grammar described in the documentation. ##
23 ## ---------------------------------------------------- ##
24
25
26 # ------------------------- #
27 # Helping Autotest macros. #
28 # ------------------------- #
29
30
31 # _AT_DATA_CALC_Y($1, $2, $3, [CPP-DIRECTIVES])
32 # ---------------------------------------------
33 # Produce `calc.y'. Don't call this macro directly, because it contains
34 # some occurrences of `$1' etc. which will be interpreted by m4. So
35 # you should call it with $1, $2, and $3 as arguments, which is what
36 # AT_DATA_CALC_Y does.
37 m4_define([_AT_DATA_CALC_Y],
38 [m4_if([$1$2$3], $[1]$[2]$[3], [],
39 [m4_fatal([$0: Invalid arguments: $@])])dnl
40 AT_DATA([calc.y],
41 [[/* Infix notation calculator--calc */
42
43 %{
44 #include <config.h>
45 /* We don't need a perfect malloc for these tests. */
46 #undef malloc
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
57 static int power (int base, int exponent);
58 static void yyerror (const char *s);
59 static int yylex (void);
60 static int yygetc (void);
61 static void yyungetc (int c);
62
63 extern void perror (const char *s);
64 %}
65
66 /* Bison Declarations */
67 %token CALC_EOF 0
68 %token NUM
69
70 %nonassoc '=' /* comparison */
71 %left '-' '+'
72 %left '*' '/'
73 %left NEG /* negation--unary minus */
74 %right '^' /* exponentiation */
75
76 ]$4[
77
78 /* Grammar follows */
79 %%
80 input:
81 /* empty string */
82 | input line
83 ;
84
85 line:
86 '\n'
87 | exp '\n'
88 ;
89
90 exp:
91 NUM { $$ = $1; }
92 | exp '=' exp
93 {
94 if ($1 != $3)
95 printf ("calc: error: %d != %d\n", $1, $3);
96 $$ = $1 == $3;
97 }
98 | exp '+' exp { $$ = $1 + $3; }
99 | exp '-' exp { $$ = $1 - $3; }
100 | exp '*' exp { $$ = $1 * $3; }
101 | exp '/' exp { $$ = $1 / $3; }
102 | '-' exp %prec NEG { $$ = -$2; }
103 | exp '^' exp { $$ = power ($1, $3); }
104 | '(' exp ')' { $$ = $2; }
105 ;
106 %%
107 /* The input. */
108 FILE *yyin;
109
110 static void
111 yyerror (const char *s)
112 {
113 #if YYLSP_NEEDED
114 fprintf (stderr, "%d.%d:%d.%d: ",
115 yylloc.first_line, yylloc.first_column,
116 yylloc.last_line, yylloc.last_column);
117 #endif
118 fprintf (stderr, "%s\n", s);
119 }
120
121 static int
122 yygetc (void)
123 {
124 int res = getc (yyin);
125 #if YYLSP_NEEDED
126 if (res == '\n')
127 {
128 yylloc.last_line++;
129 yylloc.last_column = 0;
130 }
131 else
132 yylloc.last_column++;
133 #endif
134 return res;
135 }
136
137
138 static void
139 yyungetc (int c)
140 {
141 #if YYLSP_NEEDED
142 /* Wrong when C == `\n'. */
143 yylloc.last_column--;
144 #endif
145 ungetc (c, yyin);
146 }
147
148 static int
149 read_signed_integer (void)
150 {
151 int c = yygetc ();
152 int sign = 1;
153 int n = 0;
154
155 if (c == '-')
156 {
157 c = yygetc ();
158 sign = -1;
159 }
160
161 while (isdigit (c))
162 {
163 n = 10 * n + (c - '0');
164 c = yygetc ();
165 }
166
167 yyungetc (c);
168
169 return sign * n;
170 }
171
172
173
174 /*---------------------------------------------------------------.
175 | Lexical analyzer returns an integer on the stack and the token |
176 | NUM, or the ASCII character read if not a number. Skips all |
177 | blanks and tabs, returns 0 for EOF. |
178 `---------------------------------------------------------------*/
179
180 static int
181 yylex (void)
182 {
183 int c;
184
185 #if YYLSP_NEEDED
186 yylloc.first_column = yylloc.last_column;
187 yylloc.first_line = yylloc.last_line;
188 #endif
189
190 /* Skip white space. */
191 while ((c = yygetc ()) == ' ' || c == '\t')
192 {
193 #if YYLSP_NEEDED
194 yylloc.first_column = yylloc.last_column;
195 yylloc.first_line = yylloc.last_line;
196 #endif
197 }
198
199 /* process numbers */
200 if (c == '.' || isdigit (c))
201 {
202 yyungetc (c);
203 yylval = read_signed_integer ();
204 return NUM;
205 }
206
207 /* Return end-of-file. */
208 if (c == EOF)
209 return CALC_EOF;
210
211 /* Return single chars. */
212 return c;
213 }
214
215 static int
216 power (int base, int exponent)
217 {
218 int res = 1;
219 if (exponent < 0)
220 exit (1);
221 for (/* Niente */; exponent; --exponent)
222 res *= base;
223 return res;
224 }
225
226 int
227 main (int argc, const char **argv)
228 {
229 yyin = NULL;
230
231 if (argc == 2)
232 yyin = fopen (argv[1], "r");
233 else
234 yyin = stdin;
235
236 if (!yyin)
237 {
238 perror (argv[1]);
239 exit (1);
240 }
241
242 #if YYDEBUG
243 yydebug = 1;
244 #endif
245 #if YYLSP_NEEDED
246 yylloc.last_column = 0;
247 yylloc.last_line = 1;
248 #endif
249 yyparse ();
250 return 0;
251 }
252 ]])
253 ])# _AT_DATA_CALC_Y
254
255
256 # AT_DATA_CALC_Y([BISON-OPTIONS])
257 # -------------------------------
258 # Produce `calc.y'.
259 m4_define([AT_DATA_CALC_Y],
260 [_AT_DATA_CALC_Y($[1], $[2], $[3],
261 [m4_bmatch([$1], [--yyerror-verbose],
262 [[%error-verbose]])])])
263
264
265
266 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
267 # ------------------------------------------------------------
268 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
269 #
270 # If BISON-OPTIONS contains `--debug', then NUM-STDERR-LINES is the number
271 # of expected lines on stderr.
272 m4_define([_AT_CHECK_CALC],
273 [AT_DATA([[input]],
274 [[$2
275 ]])
276 AT_CHECK([calc input], 0, [], [stderr])dnl
277 AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0,
278 [m4_bmatch([$1], [--debug],
279 [$3], [0])
280 ])
281 ])
282
283
284 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, [NUM-DEBUG-LINES],
285 # [ERROR-LOCATION], [IF-YYERROR-VERBOSE])
286 # ------------------------------------------------------------
287 # Run `calc' on INPUT, and expect a `parse error' message.
288 #
289 # If BISON-OPTIONS contains `--location', then make sure the ERROR-LOCATION
290 # is correctly output on stderr.
291 #
292 # If BISON-OPTIONS contains `--yyerror-verbose', then make sure the
293 # IF-YYERROR-VERBOSE message is properly output after `parse error, '
294 # on STDERR.
295 #
296 # If BISON-OPTIONS contains `--debug', then NUM-STDERR-LINES is the number
297 # of expected lines on stderr.
298 m4_define([_AT_CHECK_CALC_ERROR],
299 [AT_DATA([[input]],
300 [[$2
301 ]])
302
303 AT_CHECK([calc input], 0, [], [stderr])
304
305
306 AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0,
307 [m4_bmatch([$1], [--debug],
308 [$3], [1])
309 ])
310
311 egrep -v '^((Start|Enter|Read|Reduc|Shift)ing|state|Error:) ' stderr >at-stderr
312 mv at-stderr stderr
313
314 AT_CHECK([cat stderr], 0,
315 [m4_bmatch([$1], [--location], [$4: ])[]dnl
316 parse error[]dnl
317 m4_bmatch([$1], [--yyerror-verbose], [, $5])[]dnl
318
319 ])
320
321 ])
322
323
324 # AT_CHECK_CALC([BISON-OPTIONS], [PARSER-EXPECTED-STDERR])
325 # --------------------------------------------------------
326 # Start a testing chunk which compiles `calc' grammar with
327 # BISON-OPTIONS, and performs several tests over the parser.
328 m4_define([AT_CHECK_CALC],
329 [# We use integers to avoid dependencies upon the precision of doubles.
330 AT_SETUP([Calculator $1])
331
332 AT_DATA_CALC_Y([$1])
333
334 # Specify the output files to avoid problems on different file systems.
335 AT_CHECK([bison calc.y -o calc.c m4_bpatsubst([$1], [--yyerror-verbose])],
336 [0], [], [])
337
338 # Some compilers issue warnings we don't want to hear about.
339 # Maybe some day we will have proper Autoconf macros to disable these
340 # warnings, but this place is not the right one for that.
341 # So let's keep only GCC warnings, which we know are sane.
342 # Well, that's only part of the story: some assemblers issue warnings
343 # which can be totally useless, and actually polluting. It seems that
344 # the best bet be to completely ignore stderr, but to pass -Werror
345 # to GCC.
346 if test "$GCC" = yes; then
347 CFLAGS="$CFLAGS -Werror"
348 fi
349 AT_CHECK([$CC $CFLAGS $CPPFLAGS calc.c -o calc], 0, [], [ignore])
350
351 # Test the priorities.
352 _AT_CHECK_CALC([$1],
353 [1 + 2 * 3 = 7
354 1 + 2 * -3 = -5
355
356 -1^2 = -1
357 (-1)^2 = 1
358
359 ---1 = -1
360
361 1 - 2 - 3 = -4
362 1 - (2 - 3) = 2
363
364 2^2^3 = 256
365 (2^2)^3 = 64], [491])
366
367 # Some parse errors.
368 _AT_CHECK_CALC_ERROR([$1], [+1], [8],
369 [1.0:1.1],
370 [unexpected '+'])
371 _AT_CHECK_CALC_ERROR([$1], [1//2], [17],
372 [1.2:1.3],
373 [unexpected '/', expecting NUM or '-' or '('])
374 _AT_CHECK_CALC_ERROR([$1], [error], [8],
375 [1.0:1.1],
376 [unexpected $undefined.])
377 _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [23],
378 [1.6:1.7],
379 [unexpected '='])
380 _AT_CHECK_CALC_ERROR([$1],
381 [
382 +1],
383 [16],
384 [2.0:2.1],
385 [unexpected '+'])
386
387 AT_CLEANUP
388 ])# AT_CHECK_CALC
389
390
391
392
393 # ------------------ #
394 # Test the parsers. #
395 # ------------------ #
396
397 AT_CHECK_CALC()
398
399 AT_CHECK_CALC([--defines])
400 AT_CHECK_CALC([--locations])
401 AT_CHECK_CALC([--name-prefix=calc])
402 AT_CHECK_CALC([--verbose])
403 AT_CHECK_CALC([--yacc])
404 AT_CHECK_CALC([--yyerror-verbose])
405
406 AT_CHECK_CALC([--locations --yyerror-verbose])
407
408 AT_CHECK_CALC([--defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])
409
410 AT_CHECK_CALC([--debug])
411 AT_CHECK_CALC([--debug --defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])