]> git.saurik.com Git - bison.git/blame - tests/calc.at
* src/reader.c (copy_dollar, copy_at): Better checking of `n' in
[bison.git] / tests / calc.at
CommitLineData
342b8b6e
AD
1# Checking the output filenames. -*- Autotest -*-
2# Copyright 2000, 2001 Free Software Foundation, Inc.
bfb07874 3
342b8b6e
AD
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.
bfb07874 8
342b8b6e
AD
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.
bfb07874 13
342b8b6e
AD
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.
bfb07874 18
342b8b6e 19AT_BANNER([[Simple Calculator.]])
bfb07874
AD
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.
342b8b6e
AD
37m4_define([_AT_DATA_CALC_Y],
38[m4_if([$1$2$3], $[1]$[2]$[3], [],
39 [m4_fatal([$0: Invalid arguments: $@])])dnl
bfb07874
AD
40AT_DATA([calc.y],
41[[/* Infix notation calculator--calc */
42
43%{
2ce10144 44#include <config.h>
342b8b6e
AD
45/* We don't need a perfect malloc for these tests. */
46#undef malloc
bfb07874 47#include <stdio.h>
2ce10144
AD
48
49#if STDC_HEADERS
50# include <stdlib.h>
51# include <string.h>
52#else
53char *strcat(char *dest, const char *src);
54#endif
bfb07874 55#include <ctype.h>
bfb07874
AD
56
57static int power (int base, int exponent);
58static void yyerror (const char *s);
59static int yylex (void);
2ce10144
AD
60static int yygetc (void);
61static void yyungetc (int c);
62
bfb07874
AD
63extern void perror (const char *s);
64%}
65
6b7e85b9
AD
66/* Bison Declarations */
67%token CALC_EOF 0
bfb07874
AD
68%token NUM
69
70%nonassoc '=' /* comparison */
71%left '-' '+'
72%left '*' '/'
73%left NEG /* negation--unary minus */
74%right '^' /* exponentiation */
75
147e184c
MA
76]$4[
77
bfb07874
AD
78/* Grammar follows */
79%%
80input:
81 /* empty string */
82| input line
83;
84
85line:
86 '\n'
87| exp '\n'
88;
89
90exp:
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. */
108FILE *yyin;
109
110static void
111yyerror (const char *s)
112{
19c50364 113#if YYLSP_NEEDED
bfb07874
AD
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
121static int
2ce10144 122yygetc (void)
bfb07874
AD
123{
124 int res = getc (yyin);
19c50364 125#if YYLSP_NEEDED
bfb07874
AD
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
138static void
139yyungetc (int c)
140{
19c50364 141#if YYLSP_NEEDED
bfb07874
AD
142 /* Wrong when C == `\n'. */
143 yylloc.last_column--;
144#endif
145 ungetc (c, yyin);
146}
147
148static int
149read_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
180static int
181yylex (void)
182{
183 int c;
184
19c50364 185#if YYLSP_NEEDED
bfb07874
AD
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 {
19c50364 193#if YYLSP_NEEDED
bfb07874
AD
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)
6b7e85b9 209 return CALC_EOF;
bfb07874
AD
210
211 /* Return single chars. */
212 return c;
213}
214
215static int
216power (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
226int
342b8b6e 227main (int argc, const char **argv)
bfb07874 228{
342b8b6e
AD
229 yyin = NULL;
230
231 if (argc == 2)
bfb07874
AD
232 yyin = fopen (argv[1], "r");
233 else
234 yyin = stdin;
235
342b8b6e 236 if (!yyin)
bfb07874
AD
237 {
238 perror (argv[1]);
239 exit (1);
240 }
241
242#if YYDEBUG
243 yydebug = 1;
244#endif
19c50364 245#if YYLSP_NEEDED
bfb07874
AD
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'.
342b8b6e 259m4_define([AT_DATA_CALC_Y],
bfb07874 260[_AT_DATA_CALC_Y($[1], $[2], $[3],
f987e9d2 261 [m4_bmatch([$1], [--yyerror-verbose],
147e184c 262 [[%error-verbose]])])])
bfb07874
AD
263
264
265
342b8b6e
AD
266# _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
267# ------------------------------------------------------------
bfb07874 268# Run `calc' on INPUT and expect no STDOUT nor STDERR.
342b8b6e
AD
269#
270# If BISON-OPTIONS contains `--debug', then NUM-STDERR-LINES is the number
271# of expected lines on stderr.
272m4_define([_AT_CHECK_CALC],
273[AT_DATA([[input]],
274[[$2
275]])
276AT_CHECK([calc input], 0, [], [stderr])dnl
277AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0,
3c1a79b3
AD
278 [m4_bmatch([$1], [--debug],
279 [$3], [0])
342b8b6e
AD
280])
281])
282
283
284# _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, [NUM-DEBUG-LINES],
bfb07874
AD
285# [ERROR-LOCATION], [IF-YYERROR-VERBOSE])
286# ------------------------------------------------------------
342b8b6e
AD
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.
298m4_define([_AT_CHECK_CALC_ERROR],
299[AT_DATA([[input]],
300[[$2
301]])
302
303AT_CHECK([calc input], 0, [], [stderr])
304
305
306AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0,
3c1a79b3
AD
307 [m4_bmatch([$1], [--debug],
308 [$3], [1])
342b8b6e
AD
309])
310
311egrep -v '^((Start|Enter|Read|Reduc|Shift)ing|state|Error:) ' stderr >at-stderr
312mv at-stderr stderr
313
314AT_CHECK([cat stderr], 0,
3c1a79b3 315[m4_bmatch([$1], [--location], [$4: ])[]dnl
bfb07874 316parse error[]dnl
3c1a79b3 317m4_bmatch([$1], [--yyerror-verbose], [, $5])[]dnl
342b8b6e
AD
318
319])
bfb07874 320
342b8b6e 321])
bfb07874
AD
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.
342b8b6e 328m4_define([AT_CHECK_CALC],
bfb07874
AD
329[# We use integers to avoid dependencies upon the precision of doubles.
330AT_SETUP([Calculator $1])
331
332AT_DATA_CALC_Y([$1])
333
334# Specify the output files to avoid problems on different file systems.
3c1a79b3 335AT_CHECK([bison calc.y -o calc.c m4_bpatsubst([$1], [--yyerror-verbose])],
bfb07874 336 [0], [], [])
3c1a79b3
AD
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.
d803322e
AD
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.
346if test "$GCC" = yes; then
347 CFLAGS="$CFLAGS -Werror"
348fi
349AT_CHECK([$CC $CFLAGS $CPPFLAGS calc.c -o calc], 0, [], [ignore])
bfb07874
AD
350
351# Test the priorities.
352_AT_CHECK_CALC([$1],
353[1 + 2 * 3 = 7
3541 + 2 * -3 = -5
355
356-1^2 = -1
357(-1)^2 = 1
358
359---1 = -1
360
3611 - 2 - 3 = -4
3621 - (2 - 3) = 2
363
3642^2^3 = 256
342b8b6e 365(2^2)^3 = 64], [491])
bfb07874
AD
366
367# Some parse errors.
342b8b6e 368_AT_CHECK_CALC_ERROR([$1], [+1], [8],
bfb07874 369 [1.0:1.1],
f0473484 370 [unexpected '+'])
342b8b6e 371_AT_CHECK_CALC_ERROR([$1], [1//2], [17],
bfb07874 372 [1.2:1.3],
f0473484 373 [unexpected '/', expecting NUM or '-' or '('])
342b8b6e 374_AT_CHECK_CALC_ERROR([$1], [error], [8],
bfb07874 375 [1.0:1.1],
f0473484 376 [unexpected $undefined.])
342b8b6e 377_AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [23],
bfb07874 378 [1.6:1.7],
f0473484 379 [unexpected '='])
bfb07874
AD
380_AT_CHECK_CALC_ERROR([$1],
381 [
382+1],
342b8b6e 383 [16],
bfb07874 384 [2.0:2.1],
f0473484 385 [unexpected '+'])
bfb07874 386
d803322e 387AT_CLEANUP
bfb07874
AD
388])# AT_CHECK_CALC
389
390
391
392
393# ------------------ #
394# Test the parsers. #
395# ------------------ #
396
397AT_CHECK_CALC()
bfb07874
AD
398
399AT_CHECK_CALC([--defines])
400AT_CHECK_CALC([--locations])
401AT_CHECK_CALC([--name-prefix=calc])
402AT_CHECK_CALC([--verbose])
403AT_CHECK_CALC([--yacc])
404AT_CHECK_CALC([--yyerror-verbose])
405
406AT_CHECK_CALC([--locations --yyerror-verbose])
407
408AT_CHECK_CALC([--defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])
409
410AT_CHECK_CALC([--debug])
411AT_CHECK_CALC([--debug --defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])