]> git.saurik.com Git - bison.git/blame_incremental - tests/calc.at
* lib/bitset.h (BITSET_FOR_EACH, BITSET_FOR_EACH_REVERSE):
[bison.git] / tests / calc.at
... / ...
CommitLineData
1# Checking the output filenames. -*- Autotest -*-
2# Copyright (C) 2000, 2001, 2002, 2003 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
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, [BISON-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.
35m4_define([_AT_DATA_CALC_Y],
36[m4_if([$1$2$3], $[1]$[2]$[3], [],
37 [m4_fatal([$0: Invalid arguments: $@])])dnl
38AT_DATA_GRAMMAR([calc.y],
39[[/* Infix notation calculator--calc */
40]$4[
41%{
42#include <stdio.h>
43
44#if STDC_HEADERS
45# include <stdlib.h>
46# include <string.h>
47#endif
48#include <ctype.h>
49
50extern void perror (const char *s);
51
52/* Exercise pre-prologue dependency to %union. */
53typedef int value;
54
55static value global_result = 0;
56static int global_count = 0;
57%}
58
59/* Exercise %union. */
60%union
61{
62 value ival;
63};
64
65%{
66static int power (int base, int exponent);
67]AT_LALR1_CC_IF([typedef yy::Location YYLTYPE;],
68[/* yyerror receives the location if:
69 - %location & %pure & %glr
70 - %location & %pure & %yacc & %parse-param. */
71static void yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])
72 AT_PARAM_IF([value *result, int *count, ])
73 const char *s
74 );])[
75static int yylex (]AT_LEX_FORMALS[);
76static int yygetc (]AT_LEX_FORMALS[);
77static void yyungetc (]AT_LEX_PRE_FORMALS[ int c);
78%}
79
80/* Bison Declarations */
81%token CALC_EOF 0 "end of input"
82%token <ival> NUM "number"
83%type <ival> exp
84
85%nonassoc '=' /* comparison */
86%left '-' '+'
87%left '*' '/'
88%left NEG /* negation--unary minus */
89%right '^' /* exponentiation */
90
91/* Grammar follows */
92%%
93input:
94 line
95| input line { ]AT_PARAM_IF([++*count; ++global_count;])[ }
96;
97
98line:
99 '\n'
100| exp '\n' { ]AT_PARAM_IF([*result = global_result = $1;])[ }
101;
102
103exp:
104 NUM { $$ = $1; }
105| exp '=' exp
106 {
107 if ($1 != $3)
108 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
109 $$ = $1;
110 }
111| exp '+' exp { $$ = $1 + $3; }
112| exp '-' exp { $$ = $1 - $3; }
113| exp '*' exp { $$ = $1 * $3; }
114| exp '/' exp { $$ = $1 / $3; }
115| '-' exp %prec NEG { $$ = -$2; }
116| exp '^' exp { $$ = power ($1, $3); }
117| '(' exp ')' { $$ = $2; }
118| '(' error ')' { $$ = 1111; }
119| '!' { YYERROR; }
120;
121%%
122/* The input. */
123static FILE *yyin;
124
125]AT_LALR1_CC_IF(
126[/* Currently, print_ is required in C++. */
127void
128yy::Parser::print_ ()
129{
130AT_LOCATION_IF([
131 std::cerr << location;])
132}
133
134/* A C++ error reporting function. */
135void
136yy::Parser::error_ ()
137{
138 std::cerr << AT_LOCATION_IF([location << ": " << ])message << std::endl;
139}
140
141int
142yyparse (void)
143{
144 yy::Parser parser (!!YYDEBUG[]AT_LOCATION_IF([, yy::Location::Location ()]));
145 return parser.parse ();
146}
147],
148[static void
149yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])
150 AT_PARAM_IF([value *result, int *count, ])
151 const char *s)
152{
153AT_PARAM_IF([(void) result; (void) count;])
154AT_YYERROR_SEES_LOC_IF([
155 fprintf (stderr, "%d.%d",
156 AT_LOC.first_line, AT_LOC.first_column);
157 if (AT_LOC.first_line != AT_LOC.last_line)
158 fprintf (stderr, "-%d.%d",
159 AT_LOC.last_line, AT_LOC.last_column - 1);
160 else if (AT_LOC.first_column != AT_LOC.last_column - 1)
161 fprintf (stderr, "-%d",
162 AT_LOC.last_column - 1);
163 fprintf (stderr, ": ");])
164 fprintf (stderr, "%s\n", s);
165}])[
166
167
168]AT_LOCATION_IF([
169static YYLTYPE last_yylloc;
170])[
171static int
172yygetc (]AT_LEX_FORMALS[)
173{
174 int res = getc (yyin);
175 ]AT_USE_LEX_ARGS[;
176]AT_LOCATION_IF([
177 last_yylloc = AT_LOC;
178 if (res == '\n')
179 {
180AT_LALR1_CC_IF(
181[ AT_LOC.end.line++;
182 AT_LOC.end.column = 0;],
183[ AT_LOC.last_line++;
184 AT_LOC.last_column = 0;])
185 }
186 else
187AT_LALR1_CC_IF(
188[ AT_LOC.end.column++;],
189[ AT_LOC.last_column++;])
190])[
191 return res;
192}
193
194
195static void
196yyungetc (]AT_LEX_PRE_FORMALS[ int c)
197{
198 ]AT_USE_LEX_ARGS[;
199]AT_LOCATION_IF([
200 /* Wrong when C == `\n'. */
201 AT_LOC = last_yylloc;
202])[
203 ungetc (c, yyin);
204}
205
206static int
207read_signed_integer (]AT_LEX_FORMALS[)
208{
209 int c = yygetc (]AT_LEX_ARGS[);
210 int sign = 1;
211 int n = 0;
212
213 ]AT_USE_LEX_ARGS[;
214 if (c == '-')
215 {
216 c = yygetc (]AT_LEX_ARGS[);
217 sign = -1;
218 }
219
220 while (isdigit (c))
221 {
222 n = 10 * n + (c - '0');
223 c = yygetc (]AT_LEX_ARGS[);
224 }
225
226 yyungetc (]AT_LEX_PRE_ARGS[ c);
227
228 return sign * n;
229}
230
231
232
233/*---------------------------------------------------------------.
234| Lexical analyzer returns an integer on the stack and the token |
235| NUM, or the ASCII character read if not a number. Skips all |
236| blanks and tabs, returns 0 for EOF. |
237`---------------------------------------------------------------*/
238
239static int
240yylex (]AT_LEX_FORMALS[)
241{
242 static int init = 1;
243 int c;
244
245 if (init)
246 {
247 init = 0;
248]AT_LALR1_CC_IF([],
249[AT_LOCATION_IF([
250 AT_LOC.last_column = 0;
251 AT_LOC.last_line = 1;
252])])[
253 }
254
255]AT_LOCATION_IF([AT_LALR1_CC_IF(
256[ AT_LOC.begin = AT_LOC.end;],
257[ AT_LOC.first_column = AT_LOC.last_column;
258 AT_LOC.first_line = AT_LOC.last_line;
259])])[
260
261 /* Skip white space. */
262 while ((c = yygetc (]AT_LEX_ARGS[)) == ' ' || c == '\t')
263 {
264]AT_LOCATION_IF([AT_LALR1_CC_IF(
265[ AT_LOC.begin = AT_LOC.end;],
266[ AT_LOC.first_column = AT_LOC.last_column;
267 AT_LOC.first_line = AT_LOC.last_line;
268])])[
269 }
270
271 /* process numbers */
272 if (c == '.' || isdigit (c))
273 {
274 yyungetc (]AT_LEX_PRE_ARGS[ c);
275 ]AT_VAL[.ival = read_signed_integer (]AT_LEX_ARGS[);
276 return NUM;
277 }
278
279 /* Return end-of-file. */
280 if (c == EOF)
281 return CALC_EOF;
282
283 /* Return single chars. */
284 return c;
285}
286
287static int
288power (int base, int exponent)
289{
290 int res = 1;
291 if (exponent < 0)
292 exit (1);
293 for (/* Niente */; exponent; --exponent)
294 res *= base;
295 return res;
296}
297
298
299int
300main (int argc, const char **argv)
301{
302 value result = 0;
303 int count = 0;
304 int status;
305
306 if (argc == 2)
307 yyin = fopen (argv[1], "r");
308 else
309 yyin = stdin;
310
311 if (!yyin)
312 {
313 perror (argv[1]);
314 exit (1);
315 }
316
317]AT_LALR1_CC_IF([], [m4_bmatch([$4], [%debug],
318[ yydebug = 1;])])[
319 status = yyparse (]AT_PARAM_IF([&result, &count])[);
320 if (global_result != result)
321 abort ();
322 if (global_count != count)
323 abort ();
324 return status;
325}
326]])
327])# _AT_DATA_CALC_Y
328
329
330# AT_DATA_CALC_Y([BISON-OPTIONS])
331# -------------------------------
332# Produce `calc.y'.
333m4_define([AT_DATA_CALC_Y],
334[_AT_DATA_CALC_Y($[1], $[2], $[3], [$1])
335])
336
337
338
339# _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
340# ------------------------------------------------------------
341# Run `calc' on INPUT and expect no STDOUT nor STDERR.
342#
343# If BISON-OPTIONS contains `%debug' but not `%glr-parser', then
344# NUM-STDERR-LINES is the number of expected lines on stderr.
345#
346# We don't count GLR's traces yet, since its traces are somewhat
347# different from LALR's.
348m4_define([_AT_CHECK_CALC],
349[AT_DATA([[input]],
350[[$2
351]])
352AT_PARSER_CHECK([./calc input], 0, [], [stderr])
353m4_bmatch([$1],
354 [%debug.*%glr\|%glr.*%debug],
355 [],
356 [%debug],
357 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3
358])])
359])
360
361
362# _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT,
363# [NUM-DEBUG-LINES],
364# [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
365# ---------------------------------------------------------
366# Run `calc' on INPUT, and expect a `syntax error' message.
367#
368# If INPUT starts with a slash, it is used as absolute input file name,
369# otherwise as contents.
370#
371# If BISON-OPTIONS contains `%location', then make sure the ERROR-LOCATION
372# is correctly output on stderr.
373#
374# If BISON-OPTIONS contains `%error-verbose', then make sure the
375# IF-YYERROR-VERBOSE message is properly output after `syntax error, '
376# on STDERR.
377#
378# If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
379# is the number of expected lines on stderr.
380m4_define([_AT_CHECK_CALC_ERROR],
381[m4_bmatch([$3], [^/],
382 [AT_PARSER_CHECK([./calc $3], $2, [], [stderr])],
383 [AT_DATA([[input]],
384[[$3
385]])
386AT_PARSER_CHECK([./calc input], $2, [], [stderr])])
387m4_bmatch([$1],
388 [%debug.*%glr\|%glr.*%debug],
389 [],
390 [%debug],
391 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$4
392])])
393
394# Normalize the observed and expected error messages, depending upon the
395# options.
396# 1. Remove the traces from observed.
397sed '/^Starting/d
398/^Entering/d
399/^Stack/d
400/^Reading/d
401/^Reducing/d
402/^Shifting/d
403/^state/d
404/^Error:/d
405/^Next/d
406/^Discarding/d
407/^yydestructor:/d' stderr >at-stderr
408mv at-stderr stderr
409# 2. Create the reference error message.
410AT_DATA([[expout]],
411[$5
412])
413# 3. If locations are not used, remove them.
414AT_YYERROR_SEES_LOC_IF([],
415[[sed 's/^[-0-9.]*: //' expout >at-expout
416mv at-expout expout]])
417# 4. If error-verbose is not used, strip the`, unexpected....' part.
418m4_bmatch([$1], [%error-verbose], [],
419[[sed 's/syntax error, .*$/syntax error/' expout >at-expout
420mv at-expout expout]])
421# 5. Check
422AT_CHECK([cat stderr], 0, [expout])
423])
424
425
426# AT_CHECK_CALC([BISON-OPTIONS])
427# ------------------------------
428# Start a testing chunk which compiles `calc' grammar with
429# BISON-OPTIONS, and performs several tests over the parser.
430m4_define([AT_CHECK_CALC],
431[# We use integers to avoid dependencies upon the precision of doubles.
432AT_SETUP([Calculator $1])
433
434AT_BISON_OPTION_PUSHDEFS([$1])
435
436AT_DATA_CALC_Y([$1])
437
438# Specify the output files to avoid problems on different file systems.
439AT_CHECK([bison -o calc.c calc.y])
440
441AT_LALR1_CC_IF([AT_COMPILE_CXX([calc])],
442 [AT_COMPILE([calc])])
443
444# Test the priorities.
445_AT_CHECK_CALC([$1],
446[1 + 2 * 3 = 7
4471 + 2 * -3 = -5
448
449-1^2 = -1
450(-1)^2 = 1
451
452---1 = -1
453
4541 - 2 - 3 = -4
4551 - (2 - 3) = 2
456
4572^2^3 = 256
458(2^2)^3 = 64],
459 [486])
460
461# Some syntax errors.
462_AT_CHECK_CALC_ERROR([$1], [1], [0 0], [11],
463 [1.2: syntax error, unexpected "number"])
464_AT_CHECK_CALC_ERROR([$1], [1], [1//2], [15],
465 [1.2: syntax error, unexpected '/', expecting "number" or '-' or '(' or '!'])
466_AT_CHECK_CALC_ERROR([$1], [1], [error], [4],
467 [1.0: syntax error, unexpected $undefined])
468_AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [22],
469 [1.6: syntax error, unexpected '='])
470_AT_CHECK_CALC_ERROR([$1], [1],
471 [
472+1],
473 [14],
474 [2.0: syntax error, unexpected '+'])
475# Exercise error messages with EOF: work on an empty file.
476_AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4],
477 [1.0: syntax error, unexpected "end of input"])
478
479# Exercise the error token: without it, we die at the first error,
480# hence be sure to
481#
482# - have several errors which exercise different shift/discardings
483# - (): nothing to pop, nothing to discard
484# - (1 + 1 + 1 +): a lot to pop, nothing to discard
485# - (* * *): nothing to pop, a lot to discard
486# - (1 + 2 * *): some to pop and discard
487#
488# - test the action associated to `error'
489#
490# - check the lookahead that triggers an error is not discarded
491# when we enter error recovery. Below, the lookahead causing the
492# first error is ")", which is needed to recover from the error and
493# produce the "0" that triggers the "0 != 1" error.
494#
495_AT_CHECK_CALC_ERROR([$1], [0],
496 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
497 [156],
498[1.1: syntax error, unexpected ')', expecting "number" or '-' or '(' or '!'
4991.17: syntax error, unexpected ')', expecting "number" or '-' or '(' or '!'
5001.22: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!'
5011.40: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!'
502calc: error: 4444 != 1])
503
504# The same, but this time exercising explicitly triggered syntax errors.
505# POSIX says the lookahead causing the error should not be discarded.
506_AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [64],
507[1.9: syntax error, unexpected "number"
508calc: error: 2222 != 1])
509AT_BISON_OPTION_POPDEFS
510
511AT_CLEANUP
512])# AT_CHECK_CALC
513
514
515
516
517# ------------------------ #
518# Simple LALR Calculator. #
519# ------------------------ #
520
521AT_BANNER([[Simple LALR Calculator.]])
522
523# AT_CHECK_CALC_LALR([BISON-OPTIONS])
524# -----------------------------------
525# Start a testing chunk which compiles `calc' grammar with
526# BISON-OPTIONS, and performs several tests over the parser.
527m4_define([AT_CHECK_CALC_LALR],
528[AT_CHECK_CALC($@)])
529
530AT_CHECK_CALC_LALR()
531
532AT_CHECK_CALC_LALR([%defines])
533AT_CHECK_CALC_LALR([%locations])
534AT_CHECK_CALC_LALR([%name-prefix="calc"])
535AT_CHECK_CALC_LALR([%verbose])
536AT_CHECK_CALC_LALR([%yacc])
537AT_CHECK_CALC_LALR([%error-verbose])
538
539AT_CHECK_CALC_LALR([%pure-parser %locations])
540AT_CHECK_CALC_LALR([%error-verbose %locations])
541
542AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
543
544AT_CHECK_CALC_LALR([%debug])
545AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
546
547AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
548
549AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {value *result} %parse-param {int *count}])
550
551
552# ----------------------- #
553# Simple GLR Calculator. #
554# ----------------------- #
555
556AT_BANNER([[Simple GLR Calculator.]])
557
558# AT_CHECK_CALC_GLR([BISON-OPTIONS])
559# ----------------------------------
560# Start a testing chunk which compiles `calc' grammar with
561# BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
562m4_define([AT_CHECK_CALC_GLR],
563[AT_CHECK_CALC([%glr-parser] $@)])
564
565
566AT_CHECK_CALC_GLR()
567
568AT_CHECK_CALC_GLR([%defines])
569AT_CHECK_CALC_GLR([%locations])
570AT_CHECK_CALC_GLR([%name-prefix="calc"])
571AT_CHECK_CALC_GLR([%verbose])
572AT_CHECK_CALC_GLR([%yacc])
573AT_CHECK_CALC_GLR([%error-verbose])
574
575AT_CHECK_CALC_GLR([%pure-parser %locations])
576AT_CHECK_CALC_GLR([%error-verbose %locations])
577
578AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
579
580AT_CHECK_CALC_GLR([%debug])
581AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
582
583AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
584
585AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {value *result} %parse-param {int *count}])
586
587
588# ----------------------------- #
589# Simple LALR1 C++ Calculator. #
590# ----------------------------- #
591
592AT_BANNER([[Simple LALR1 C++ Calculator.]])
593
594# AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS])
595# ---------------------------------------
596# Start a testing chunk which compiles `calc' grammar with
597# BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
598m4_define([AT_CHECK_CALC_LALR1_CC],
599[AT_CHECK_CALC([%skeleton "lalr1.cc"] $@)])
600
601# AT_CHECK_CALC_LALR1_CC()
602
603AT_CHECK_CALC_LALR1_CC([%defines %locations])
604
605AT_CHECK_CALC_LALR1_CC([%defines])
606# AT_CHECK_CALC_LALR1_CC([%locations])
607# AT_CHECK_CALC_LALR1_CC([%name-prefix="calc"])
608# AT_CHECK_CALC_LALR1_CC([%verbose])
609# AT_CHECK_CALC_LALR1_CC([%yacc])
610# AT_CHECK_CALC_LALR1_CC([%error-verbose])
611
612# AT_CHECK_CALC_LALR1_CC([%pure-parser %locations])
613# AT_CHECK_CALC_LALR1_CC([%error-verbose %locations])
614
615AT_CHECK_CALC_LALR1_CC([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
616
617# AT_CHECK_CALC_LALR1_CC([%debug])
618AT_CHECK_CALC_LALR1_CC([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
619
620AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
621
622# AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {value *result} %parse-param {int *count}])