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