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