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