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