]> git.saurik.com Git - bison.git/blame - tests/calc.at
* src/system.h: Don't use #ifdef/#ifndef on HAVE_ values, only
[bison.git] / tests / calc.at
CommitLineData
342b8b6e 1# Checking the output filenames. -*- Autotest -*-
5504898e 2# Copyright (C) 2000, 2001, 2002 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
bfb07874
AD
19## ---------------------------------------------------- ##
20## Compile the grammar described in the documentation. ##
21## ---------------------------------------------------- ##
22
23
24# ------------------------- #
25# Helping Autotest macros. #
26# ------------------------- #
27
28
29# _AT_DATA_CALC_Y($1, $2, $3, [CPP-DIRECTIVES])
30# ---------------------------------------------
31# Produce `calc.y'. Don't call this macro directly, because it contains
32# some occurrences of `$1' etc. which will be interpreted by m4. So
33# you should call it with $1, $2, and $3 as arguments, which is what
34# AT_DATA_CALC_Y does.
342b8b6e
AD
35m4_define([_AT_DATA_CALC_Y],
36[m4_if([$1$2$3], $[1]$[2]$[3], [],
37 [m4_fatal([$0: Invalid arguments: $@])])dnl
bfb07874
AD
38AT_DATA([calc.y],
39[[/* Infix notation calculator--calc */
40
41%{
2ce10144 42#include <config.h>
d9684243 43/* We don't need perfect functions for these tests. */
342b8b6e 44#undef malloc
d9684243
PE
45#undef memcmp
46#undef realloc
bfb07874 47#include <stdio.h>
2ce10144
AD
48
49#if STDC_HEADERS
50# include <stdlib.h>
51# include <string.h>
2ce10144 52#endif
bfb07874 53#include <ctype.h>
e7cb57c0 54#include <assert.h>
bfb07874 55
bfb07874 56extern void perror (const char *s);
0dd1580a
RA
57
58/* Exercise pre-prologue dependency to %union. */
59typedef int value_t;
60
e7cb57c0
AD
61value_t global_result = 0;
62int global_count = 0;
63
bfb07874
AD
64%}
65
e7cb57c0
AD
66%parse-param "value_t *result", "result"
67%parse-param "int *count", "count"
68
5a08f1ce 69/* Exercise %union. */
213e640e
AD
70%union
71{
5a08f1ce 72 value_t ival;
213e640e
AD
73};
74
c19988b7
AD
75%{
76#if YYPURE
77# define LOC (*yylloc)
78# define VAL (*yylval)
79#else
80# define LOC (yylloc)
81# define VAL (yylval)
82#endif
83
84#if YYPURE
85# if YYLSP_NEEDED
86# define LEX_FORMALS YYSTYPE *yylval, YYLTYPE *yylloc
87# define LEX_ARGS yylval, yylloc
88# define USE_LEX_ARGS (void) yylval; (void) yylloc;
89# else
90# define LEX_FORMALS YYSTYPE *yylval
91# define LEX_ARGS yylval
92# define USE_LEX_ARGS (void) yylval
93# endif
94# define LEX_PRE_FORMALS LEX_FORMALS,
95# define LEX_PRE_ARGS LEX_ARGS,
96#else
97# define LEX_FORMALS void
98# define LEX_PRE_FORMALS
99# define LEX_ARGS
100# define LEX_PRE_ARGS
101# define USE_LEX_ARGS
102#endif
103
104static int power (int base, int exponent);
105static void yyerror (const char *s);
106static int yylex (LEX_FORMALS);
107static int yygetc (LEX_FORMALS);
108static void yyungetc (LEX_PRE_FORMALS int c);
109%}
110
6b7e85b9 111/* Bison Declarations */
c19988b7 112%token CALC_EOF 0 "end of input"
213e640e
AD
113%token <ival> NUM "number"
114%type <ival> exp
bfb07874
AD
115
116%nonassoc '=' /* comparison */
117%left '-' '+'
118%left '*' '/'
119%left NEG /* negation--unary minus */
120%right '^' /* exponentiation */
121
147e184c
MA
122]$4[
123
bfb07874
AD
124/* Grammar follows */
125%%
126input:
b7c49edf 127 line
e7cb57c0 128| input line { ++*count; ++global_count; }
bfb07874
AD
129;
130
131line:
c19988b7 132 '\n'
e7cb57c0 133| exp '\n' { *result = global_result = $1; }
bfb07874
AD
134;
135
136exp:
137 NUM { $$ = $1; }
138| exp '=' exp
139 {
140 if ($1 != $3)
51dec47b 141 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
e7cb57c0 142 $$ = $1;
bfb07874
AD
143 }
144| exp '+' exp { $$ = $1 + $3; }
145| exp '-' exp { $$ = $1 - $3; }
146| exp '*' exp { $$ = $1 * $3; }
147| exp '/' exp { $$ = $1 / $3; }
148| '-' exp %prec NEG { $$ = -$2; }
149| exp '^' exp { $$ = power ($1, $3); }
150| '(' exp ')' { $$ = $2; }
51dec47b 151| '(' error ')' { $$ = 0; }
bfb07874
AD
152;
153%%
154/* The input. */
155FILE *yyin;
156
157static void
158yyerror (const char *s)
159{
19c50364 160#if YYLSP_NEEDED
51dec47b 161 fprintf (stderr, "%d.%d-%d.%d: ",
c19988b7
AD
162 LOC.first_line, LOC.first_column,
163 LOC.last_line, LOC.last_column);
bfb07874
AD
164#endif
165 fprintf (stderr, "%s\n", s);
166}
167
b7c49edf
AD
168
169#if YYLSP_NEEDED
170static YYLTYPE last_yylloc;
171#endif
bfb07874 172static int
c19988b7 173yygetc (LEX_FORMALS)
bfb07874
AD
174{
175 int res = getc (yyin);
c19988b7 176 USE_LEX_ARGS;
19c50364 177#if YYLSP_NEEDED
c19988b7 178 last_yylloc = LOC;
bfb07874
AD
179 if (res == '\n')
180 {
c19988b7
AD
181 LOC.last_line++;
182 LOC.last_column = 1;
bfb07874
AD
183 }
184 else
c19988b7 185 LOC.last_column++;
bfb07874
AD
186#endif
187 return res;
188}
189
190
191static void
c19988b7 192yyungetc (LEX_PRE_FORMALS int c)
bfb07874 193{
c19988b7 194 USE_LEX_ARGS;
19c50364 195#if YYLSP_NEEDED
bfb07874 196 /* Wrong when C == `\n'. */
c19988b7 197 LOC = last_yylloc;
bfb07874
AD
198#endif
199 ungetc (c, yyin);
200}
201
202static int
c19988b7 203read_signed_integer (LEX_FORMALS)
bfb07874 204{
c19988b7 205 int c = yygetc (LEX_ARGS);
bfb07874
AD
206 int sign = 1;
207 int n = 0;
208
c19988b7 209 USE_LEX_ARGS;
bfb07874
AD
210 if (c == '-')
211 {
c19988b7 212 c = yygetc (LEX_ARGS);
bfb07874
AD
213 sign = -1;
214 }
215
216 while (isdigit (c))
217 {
218 n = 10 * n + (c - '0');
c19988b7 219 c = yygetc (LEX_ARGS);
bfb07874
AD
220 }
221
c19988b7 222 yyungetc (LEX_PRE_ARGS c);
bfb07874
AD
223
224 return sign * n;
225}
226
227
228
229/*---------------------------------------------------------------.
230| Lexical analyzer returns an integer on the stack and the token |
231| NUM, or the ASCII character read if not a number. Skips all |
232| blanks and tabs, returns 0 for EOF. |
233`---------------------------------------------------------------*/
234
235static int
c19988b7 236yylex (LEX_FORMALS)
bfb07874 237{
c19988b7 238 static int init = 1;
bfb07874
AD
239 int c;
240
c19988b7
AD
241 if (init)
242 {
243 init = 0;
244#if YYLSP_NEEDED
21964f43
AD
245 LOC.last_column = 1;
246 LOC.last_line = 1;
c19988b7
AD
247#endif
248 }
249
19c50364 250#if YYLSP_NEEDED
c19988b7
AD
251 LOC.first_column = LOC.last_column;
252 LOC.first_line = LOC.last_line;
bfb07874
AD
253#endif
254
255 /* Skip white space. */
c19988b7 256 while ((c = yygetc (LEX_ARGS)) == ' ' || c == '\t')
bfb07874 257 {
19c50364 258#if YYLSP_NEEDED
c19988b7
AD
259 LOC.first_column = LOC.last_column;
260 LOC.first_line = LOC.last_line;
bfb07874
AD
261#endif
262 }
263
264 /* process numbers */
265 if (c == '.' || isdigit (c))
266 {
c19988b7
AD
267 yyungetc (LEX_PRE_ARGS c);
268 VAL.ival = read_signed_integer (LEX_ARGS);
bfb07874
AD
269 return NUM;
270 }
271
272 /* Return end-of-file. */
273 if (c == EOF)
6b7e85b9 274 return CALC_EOF;
bfb07874
AD
275
276 /* Return single chars. */
277 return c;
278}
279
280static int
281power (int base, int exponent)
282{
283 int res = 1;
284 if (exponent < 0)
285 exit (1);
286 for (/* Niente */; exponent; --exponent)
287 res *= base;
288 return res;
289}
290
291int
342b8b6e 292main (int argc, const char **argv)
bfb07874 293{
e7cb57c0
AD
294 value_t result = 0;
295 int count = 0;
342b8b6e
AD
296 yyin = NULL;
297
298 if (argc == 2)
bfb07874
AD
299 yyin = fopen (argv[1], "r");
300 else
301 yyin = stdin;
302
342b8b6e 303 if (!yyin)
bfb07874
AD
304 {
305 perror (argv[1]);
306 exit (1);
307 }
308
309#if YYDEBUG
310 yydebug = 1;
bfb07874 311#endif
e7cb57c0
AD
312 yyparse (&result, &count);
313 assert (global_result == result);
314 assert (global_count == count);
315
bfb07874
AD
316 return 0;
317}
318]])
319])# _AT_DATA_CALC_Y
320
321
322# AT_DATA_CALC_Y([BISON-OPTIONS])
323# -------------------------------
324# Produce `calc.y'.
342b8b6e 325m4_define([AT_DATA_CALC_Y],
bfb07874 326[_AT_DATA_CALC_Y($[1], $[2], $[3],
f50adbbd
AD
327 [m4_bpatsubst([$1], [--[^ ]*])])
328])
bfb07874
AD
329
330
331
342b8b6e
AD
332# _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
333# ------------------------------------------------------------
bfb07874 334# Run `calc' on INPUT and expect no STDOUT nor STDERR.
342b8b6e 335#
f50adbbd
AD
336# If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
337# NUM-STDERR-LINES is the number of expected lines on stderr.
338#
339# We don't count GLR's traces yet, since its traces are somewhat
340# different from LALR's.
342b8b6e
AD
341m4_define([_AT_CHECK_CALC],
342[AT_DATA([[input]],
343[[$2
344]])
f50adbbd
AD
345AT_PARSER_CHECK([./calc input], 0, [], [stderr])
346m4_bmatch([$1],
347 [%debug.*%glr\|%glr.*%debug],
348 [],
349 [%debug],
350 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
351])])
342b8b6e
AD
352])
353
354
355# _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, [NUM-DEBUG-LINES],
bfb07874
AD
356# [ERROR-LOCATION], [IF-YYERROR-VERBOSE])
357# ------------------------------------------------------------
342b8b6e
AD
358# Run `calc' on INPUT, and expect a `parse error' message.
359#
b7c49edf
AD
360# If INPUT starts with a slash, it is used as absolute input file name,
361# otherwise as contents.
362#
342b8b6e
AD
363# If BISON-OPTIONS contains `--location', then make sure the ERROR-LOCATION
364# is correctly output on stderr.
365#
f50adbbd 366# If BISON-OPTIONS contains `%error-verbose', then make sure the
342b8b6e
AD
367# IF-YYERROR-VERBOSE message is properly output after `parse error, '
368# on STDERR.
369#
f50adbbd
AD
370# If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
371# is the number of expected lines on stderr.
342b8b6e 372m4_define([_AT_CHECK_CALC_ERROR],
b7c49edf 373[m4_bmatch([$2], [^/],
1154cced 374 [AT_PARSER_CHECK([./calc $2], 0, [], [stderr])],
b7c49edf 375 [AT_DATA([[input]],
342b8b6e
AD
376[[$2
377]])
1154cced 378AT_PARSER_CHECK([./calc input], 0, [], [stderr])])
f50adbbd
AD
379m4_bmatch([$1],
380 [%debug.*%glr\|%glr.*%debug],
381 [],
382 [%debug],
383 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
51dec47b 384])])
342b8b6e 385
51dec47b
AD
386# Normalize the observed and expected error messages, depending upon the
387# options.
388# 1. Remove the traces from observed.
9280d3ef
AD
389sed '/^Starting/d
390/^Entering/d
f50adbbd 391/^Stack/d
9280d3ef
AD
392/^Reading/d
393/^Reducing/d
394/^Shifting/d
395/^state/d
396/^Error:/d
397/^Next/d
398/^Discarding/d
399/^yydestructor:/d' stderr >at-stderr
342b8b6e 400mv at-stderr stderr
51dec47b
AD
401# 2. Create the reference error message.
402AT_DATA([[expout]],
403[$4
342b8b6e 404])
51dec47b 405# 3. If locations are not used, remove them.
ae7453f2 406m4_bmatch([$1], [%locations], [],
51dec47b
AD
407[[sed 's/^[-0-9.]*: //' expout >at-expout
408mv at-expout expout]])
409# 4. If error-verbose is not used, strip the`, unexpected....' part.
f50adbbd 410m4_bmatch([$1], [%error-verbose], [],
51dec47b
AD
411[[sed 's/parse error, .*$/parse error/' expout >at-expout
412mv at-expout expout]])
413# 5. Check
414AT_CHECK([cat stderr], 0, [expout])
342b8b6e 415])
bfb07874
AD
416
417
f50adbbd
AD
418# AT_CHECK_CALC([BISON-OPTIONS])
419# ------------------------------
bfb07874
AD
420# Start a testing chunk which compiles `calc' grammar with
421# BISON-OPTIONS, and performs several tests over the parser.
342b8b6e 422m4_define([AT_CHECK_CALC],
bfb07874
AD
423[# We use integers to avoid dependencies upon the precision of doubles.
424AT_SETUP([Calculator $1])
425
426AT_DATA_CALC_Y([$1])
427
428# Specify the output files to avoid problems on different file systems.
b56471a6 429AT_CHECK([bison -o calc.c m4_bpatsubst([$1], [%[^ ]*]) calc.y],
bfb07874 430 [0], [], [])
3c1a79b3 431
1154cced 432AT_COMPILE([calc])
bfb07874
AD
433
434# Test the priorities.
435_AT_CHECK_CALC([$1],
436[1 + 2 * 3 = 7
4371 + 2 * -3 = -5
438
439-1^2 = -1
440(-1)^2 = 1
441
442---1 = -1
443
4441 - 2 - 3 = -4
4451 - (2 - 3) = 2
446
4472^2^3 = 256
e7cb57c0
AD
448(2^2)^3 = 64],
449 [486])
bfb07874
AD
450
451# Some parse errors.
366eea36 452_AT_CHECK_CALC_ERROR([$1], [0 0], [11],
51dec47b 453 [1.3-1.4: parse error, unexpected "number"])
366eea36 454_AT_CHECK_CALC_ERROR([$1], [1//2], [15],
51dec47b 455 [1.3-1.4: parse error, unexpected '/', expecting "number" or '-' or '('])
b7c49edf 456_AT_CHECK_CALC_ERROR([$1], [error], [4],
88bce5a2 457 [1.1-1.2: parse error, unexpected $undefined, expecting "number" or '-' or '\n' or '('])
366eea36 458_AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [22],
51dec47b 459 [1.7-1.8: parse error, unexpected '='])
bfb07874
AD
460_AT_CHECK_CALC_ERROR([$1],
461 [
462+1],
366eea36 463 [14],
51dec47b 464 [2.1-2.2: parse error, unexpected '+'])
b7c49edf 465# Exercise error messages with EOF: work on an empty file.
366eea36 466_AT_CHECK_CALC_ERROR([$1], [/dev/null], [4],
c19988b7 467 [1.1-1.2: parse error, unexpected "end of input", expecting "number" or '-' or '\n' or '('])
51dec47b
AD
468
469# Exercise the error token: without it, we die at the first error,
470# hence be sure i. to have several errors, ii. to test the action
471# associated to `error'.
366eea36 472_AT_CHECK_CALC_ERROR([$1], [(1 ++ 2) + (0 0) = 1], [82],
51dec47b
AD
473[1.5-1.6: parse error, unexpected '+', expecting "number" or '-' or '('
4741.15-1.16: parse error, unexpected "number"
475calc: error: 0 != 1])
476
d803322e 477AT_CLEANUP
bfb07874
AD
478])# AT_CHECK_CALC
479
480
481
482
f50adbbd
AD
483# ------------------------ #
484# Simple LALR Calculator. #
485# ------------------------ #
486
487AT_BANNER([[Simple LALR Calculator.]])
488
489# AT_CHECK_CALC_LALR([BISON-OPTIONS])
490# -----------------------------------
491# Start a testing chunk which compiles `calc' grammar with
492# BISON-OPTIONS, and performs several tests over the parser.
493m4_define([AT_CHECK_CALC_LALR],
494[AT_CHECK_CALC($@)])
495
496AT_CHECK_CALC_LALR()
497
498AT_CHECK_CALC_LALR([--defines])
ae7453f2 499AT_CHECK_CALC_LALR([%locations])
f50adbbd
AD
500AT_CHECK_CALC_LALR([--name-prefix=calc])
501AT_CHECK_CALC_LALR([--verbose])
502AT_CHECK_CALC_LALR([--yacc])
503AT_CHECK_CALC_LALR([%error-verbose])
504
ae7453f2 505AT_CHECK_CALC_LALR([%error-verbose %locations])
f50adbbd 506
c19988b7 507AT_CHECK_CALC_LALR([%error-verbose %locations --defines --name-prefix=calc --verbose --yacc])
f50adbbd
AD
508
509AT_CHECK_CALC_LALR([%debug])
c19988b7
AD
510AT_CHECK_CALC_LALR([%error-verbose %debug %locations --defines --name-prefix=calc --verbose --yacc])
511
512# FIXME: Not ready yet.
513# AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations --defines --name-prefix=calc --verbose --yacc])
f50adbbd
AD
514
515
516# ----------------------- #
517# Simple GLR Calculator. #
518# ----------------------- #
519
520AT_BANNER([[Simple GLR Calculator.]])
521
522# AT_CHECK_CALC_GLR([BISON-OPTIONS])
523# ----------------------------------
524# Start a testing chunk which compiles `calc' grammar with
525# BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
526m4_define([AT_CHECK_CALC_GLR],
ae7453f2 527[AT_CHECK_CALC([%glr-parser] $@)])
f50adbbd 528
bfb07874 529
f50adbbd 530AT_CHECK_CALC_GLR()
bfb07874 531
f50adbbd 532AT_CHECK_CALC_GLR([--defines])
ae7453f2 533AT_CHECK_CALC_GLR([%locations])
f50adbbd
AD
534AT_CHECK_CALC_GLR([--name-prefix=calc])
535AT_CHECK_CALC_GLR([--verbose])
536AT_CHECK_CALC_GLR([--yacc])
537AT_CHECK_CALC_GLR([%error-verbose])
bfb07874 538
ae7453f2 539AT_CHECK_CALC_GLR([%error-verbose %locations])
bfb07874 540
c19988b7 541AT_CHECK_CALC_GLR([%error-verbose %locations --defines --name-prefix=calc --verbose --yacc])
bfb07874 542
f50adbbd 543AT_CHECK_CALC_GLR([%debug])
c19988b7 544AT_CHECK_CALC_GLR([%error-verbose %debug %locations --defines --name-prefix=calc --verbose --yacc])
21964f43
AD
545
546# AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations --defines --name-prefix=calc --verbose --yacc])