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