]> git.saurik.com Git - bison.git/blame_incremental - tests/calc.at
* data/glr.c (yynewGLRStackItem, YY_RESERVE_GLRSTACK): New.
[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., 51 Franklin Street, Fifth Floor, Boston, MA
19# 02110-1301, 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
43AT_LALR1_CC_IF(
44[%define "global_tokens_and_yystype"])[
45%{
46#include <stdio.h>
47
48#include <stdlib.h>
49#include <string.h>
50#if HAVE_UNISTD_H
51# include <unistd.h>
52#else
53# undef alarm
54# define alarm(seconds) /* empty */
55#endif
56#include <ctype.h>
57
58/* Exercise pre-prologue dependency to %union. */
59typedef int semantic_value;
60
61static semantic_value global_result = 0;
62static int global_count = 0;
63%}
64
65/* Exercise %union. */
66%union
67{
68 semantic_value ival;
69};
70
71%{
72static int power (int base, int exponent);
73]AT_LALR1_CC_IF(
74[typedef yy::location YYLTYPE;
75#define first_line begin.line
76#define first_column begin.column
77#define last_line end.line
78#define last_column end.column
79],
80[/* yyerror receives the location if:
81 - %location & %pure & %glr
82 - %location & %pure & %yacc & %parse-param. */
83static void yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ])
84 AT_PARAM_IF([semantic_value *result, int *count, ])
85 const char *s
86 );])[
87static int yylex (]AT_LEX_FORMALS[);
88static int get_char (]AT_LEX_FORMALS[);
89static void unget_char (]AT_LEX_PRE_FORMALS[ int c);
90%}
91
92/* Bison Declarations */
93%token CALC_EOF 0 "end of input"
94%token <ival> NUM "number"
95%type <ival> exp
96
97%nonassoc '=' /* comparison */
98%left '-' '+'
99%left '*' '/'
100%left NEG /* negation--unary minus */
101%right '^' /* exponentiation */
102
103/* Grammar follows */
104%%
105input:
106 line
107| input line { ]AT_PARAM_IF([++*count; ++global_count;])[ }
108;
109
110line:
111 '\n'
112| exp '\n' { ]AT_PARAM_IF([*result = global_result = $1;])[ }
113;
114
115exp:
116 NUM { $$ = $1; }
117| exp '=' exp
118 {
119 if ($1 != $3)
120 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
121 $$ = $1;
122 }
123| exp '+' exp { $$ = $1 + $3; }
124| exp '-' exp { $$ = $1 - $3; }
125| exp '*' exp { $$ = $1 * $3; }
126| exp '/' exp { $$ = $1 / $3; }
127| '-' exp %prec NEG { $$ = -$2; }
128| exp '^' exp { $$ = power ($1, $3); }
129| '(' exp ')' { $$ = $2; }
130| '(' error ')' { $$ = 1111; }
131| '!' { YYERROR; }
132| '-' error { YYERROR; }
133;
134%%
135/* The input. */
136static FILE *input;
137
138]AT_LALR1_CC_IF(
139[/* A C++ error reporting function. */
140void
141yy::parser::error (const location& l, const std::string& m)
142{
143 (void) l;
144 std::cerr << AT_LOCATION_IF([l << ": " << ])m << std::endl;
145}
146
147int
148yyparse (AT_PARAM_IF([semantic_value *result, int *count]))
149{
150 yy::parser parser[]AT_PARAM_IF([ (result, count)]);
151 parser.set_debug_level (!!YYDEBUG);
152 return parser.parse ();
153}
154],
155[static void
156yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ])
157 AT_PARAM_IF([semantic_value *result, int *count, ])
158 const char *s)
159{
160AT_PARAM_IF([(void) result; (void) count;])
161AT_YYERROR_SEES_LOC_IF([
162 fprintf (stderr, "%d.%d",
163 AT_LOC.first_line, AT_LOC.first_column);
164 if (AT_LOC.first_line != AT_LOC.last_line)
165 fprintf (stderr, "-%d.%d",
166 AT_LOC.last_line, AT_LOC.last_column - 1);
167 else if (AT_LOC.first_column != AT_LOC.last_column - 1)
168 fprintf (stderr, "-%d",
169 AT_LOC.last_column - 1);
170 fprintf (stderr, ": ");])
171 fprintf (stderr, "%s\n", s);
172}])[
173
174
175]AT_LOCATION_IF([
176static YYLTYPE last_yylloc;
177])[
178static int
179get_char (]AT_LEX_FORMALS[)
180{
181 int res = getc (input);
182 ]AT_USE_LEX_ARGS[;
183]AT_LOCATION_IF([
184 last_yylloc = AT_LOC;
185 if (res == '\n')
186 {
187 AT_LOC.last_line++;
188 AT_LOC.last_column = 0;
189 }
190 else
191 AT_LOC.last_column++;
192])[
193 return res;
194}
195
196
197static void
198unget_char (]AT_LEX_PRE_FORMALS[ int c)
199{
200 ]AT_USE_LEX_ARGS[;
201]AT_LOCATION_IF([
202 /* Wrong when C == `\n'. */
203 AT_LOC = last_yylloc;
204])[
205 ungetc (c, input);
206}
207
208static int
209read_signed_integer (]AT_LEX_FORMALS[)
210{
211 int c = get_char (]AT_LEX_ARGS[);
212 int sign = 1;
213 int n = 0;
214
215 ]AT_USE_LEX_ARGS[;
216 if (c == '-')
217 {
218 c = get_char (]AT_LEX_ARGS[);
219 sign = -1;
220 }
221
222 while (isdigit (c))
223 {
224 n = 10 * n + (c - '0');
225 c = get_char (]AT_LEX_ARGS[);
226 }
227
228 unget_char (]AT_LEX_PRE_ARGS[ c);
229
230 return sign * n;
231}
232
233
234
235/*---------------------------------------------------------------.
236| Lexical analyzer returns an integer on the stack and the token |
237| NUM, or the ASCII character read if not a number. Skips all |
238| blanks and tabs, returns 0 for EOF. |
239`---------------------------------------------------------------*/
240
241static int
242yylex (]AT_LEX_FORMALS[)
243{
244 static int init = 1;
245 int c;
246
247 if (init)
248 {
249 init = 0;
250]AT_LOCATION_IF([
251 AT_LOC.last_column = 0;
252 AT_LOC.last_line = 1;
253])[
254 }
255
256]AT_LOCATION_IF([
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 = get_char (]AT_LEX_ARGS[)) == ' ' || c == '\t')
263 {
264]AT_LOCATION_IF(
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 unget_char (]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 (3);
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 input = fopen (argv[1], "r");
312 else
313 input = stdin;
314
315 if (!input)
316 {
317 perror (argv[1]);
318 return 3;
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/^Cleanup:/d
409/^Error:/d
410/^Next/d
411/^Discarding/d
412/^yydestructor:/d' stderr >at-stderr
413mv at-stderr stderr
414# 2. Create the reference error message.
415AT_DATA([[expout]],
416[$5
417])
418# 3. If locations are not used, remove them.
419AT_YYERROR_SEES_LOC_IF([],
420[[sed 's/^[-0-9.]*: //' expout >at-expout
421mv at-expout expout]])
422# 4. If error-verbose is not used, strip the`, unexpected....' part.
423m4_bmatch([$1], [%error-verbose], [],
424[[sed 's/syntax error, .*$/syntax error/' expout >at-expout
425mv at-expout expout]])
426# 5. Check
427AT_CHECK([cat stderr], 0, [expout])
428])
429
430
431# AT_CHECK_CALC([BISON-OPTIONS, [EXPECTED-TO-FAIL]])
432# --------------------------------------------------
433# Start a testing chunk which compiles `calc' grammar with
434# BISON-OPTIONS, and performs several tests over the parser.
435# However, if EXPECTED-TO-FAIL is nonempty, this test is expected to fail.
436m4_define([AT_CHECK_CALC],
437[# We use integers to avoid dependencies upon the precision of doubles.
438AT_SETUP([Calculator $1])
439
440m4_ifval([$2], [AT_CHECK([exit 77])])
441
442AT_BISON_OPTION_PUSHDEFS([$1])
443
444AT_DATA_CALC_Y([$1])
445
446AT_LALR1_CC_IF(
447 [AT_CHECK([bison -o calc.cc calc.y])
448 AT_COMPILE_CXX([calc])],
449 [AT_CHECK([bison -o calc.c calc.y])
450 AT_COMPILE([calc])])
451
452# Test the priorities.
453_AT_CHECK_CALC([$1],
454[1 + 2 * 3 = 7
4551 + 2 * -3 = -5
456
457-1^2 = -1
458(-1)^2 = 1
459
460---1 = -1
461
4621 - 2 - 3 = -4
4631 - (2 - 3) = 2
464
4652^2^3 = 256
466(2^2)^3 = 64],
467 [571])
468
469# Some syntax errors.
470_AT_CHECK_CALC_ERROR([$1], [1], [0 0], [13],
471 [1.2: syntax error, unexpected number])
472_AT_CHECK_CALC_ERROR([$1], [1], [1//2], [18],
473 [1.2: syntax error, unexpected '/', expecting number or '-' or '(' or '!'])
474_AT_CHECK_CALC_ERROR([$1], [1], [error], [5],
475 [1.0: syntax error, unexpected $undefined])
476_AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [26],
477 [1.6: syntax error, unexpected '='])
478_AT_CHECK_CALC_ERROR([$1], [1],
479 [
480+1],
481 [16],
482 [2.0: syntax error, unexpected '+'])
483# Exercise error messages with EOF: work on an empty file.
484_AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4],
485 [1.0: syntax error, unexpected end of input])
486
487# Exercise the error token: without it, we die at the first error,
488# hence be sure to
489#
490# - have several errors which exercise different shift/discardings
491# - (): nothing to pop, nothing to discard
492# - (1 + 1 + 1 +): a lot to pop, nothing to discard
493# - (* * *): nothing to pop, a lot to discard
494# - (1 + 2 * *): some to pop and discard
495#
496# - test the action associated to `error'
497#
498# - check the look-ahead that triggers an error is not discarded
499# when we enter error recovery. Below, the look-ahead causing the
500# first error is ")", which is needed to recover from the error and
501# produce the "0" that triggers the "0 != 1" error.
502#
503_AT_CHECK_CALC_ERROR([$1], [0],
504 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
505 [189],
506[1.1: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
5071.17: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
5081.22: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
5091.40: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
510calc: error: 4444 != 1])
511
512# The same, but this time exercising explicitly triggered syntax errors.
513# POSIX says the look-ahead causing the error should not be discarded.
514_AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [76],
515[1.9: syntax error, unexpected number
516calc: error: 2222 != 1])
517_AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (0 0) = 1], [86],
518[1.3: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
5191.11: syntax error, unexpected number
520calc: error: 2222 != 1])
521AT_BISON_OPTION_POPDEFS
522
523AT_CLEANUP
524])# AT_CHECK_CALC
525
526
527
528
529# ------------------------ #
530# Simple LALR Calculator. #
531# ------------------------ #
532
533AT_BANNER([[Simple LALR Calculator.]])
534
535# AT_CHECK_CALC_LALR([BISON-OPTIONS])
536# -----------------------------------
537# Start a testing chunk which compiles `calc' grammar with
538# BISON-OPTIONS, and performs several tests over the parser.
539m4_define([AT_CHECK_CALC_LALR],
540[AT_CHECK_CALC($@)])
541
542AT_CHECK_CALC_LALR()
543
544AT_CHECK_CALC_LALR([%defines])
545AT_CHECK_CALC_LALR([%locations])
546AT_CHECK_CALC_LALR([%name-prefix="calc"])
547AT_CHECK_CALC_LALR([%verbose])
548AT_CHECK_CALC_LALR([%yacc])
549AT_CHECK_CALC_LALR([%error-verbose])
550
551AT_CHECK_CALC_LALR([%pure-parser %locations])
552AT_CHECK_CALC_LALR([%error-verbose %locations])
553
554AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
555
556AT_CHECK_CALC_LALR([%debug])
557AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
558
559AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
560
561AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
562
563
564# ----------------------- #
565# Simple GLR Calculator. #
566# ----------------------- #
567
568AT_BANNER([[Simple GLR Calculator.]])
569
570# AT_CHECK_CALC_GLR([BISON-OPTIONS])
571# ----------------------------------
572# Start a testing chunk which compiles `calc' grammar with
573# BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
574m4_define([AT_CHECK_CALC_GLR],
575[AT_CHECK_CALC([%glr-parser] $@)])
576
577
578AT_CHECK_CALC_GLR()
579
580AT_CHECK_CALC_GLR([%defines])
581AT_CHECK_CALC_GLR([%locations])
582AT_CHECK_CALC_GLR([%name-prefix="calc"])
583AT_CHECK_CALC_GLR([%verbose])
584AT_CHECK_CALC_GLR([%yacc])
585AT_CHECK_CALC_GLR([%error-verbose])
586
587AT_CHECK_CALC_GLR([%pure-parser %locations])
588AT_CHECK_CALC_GLR([%error-verbose %locations])
589
590AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
591
592AT_CHECK_CALC_GLR([%debug])
593AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
594
595AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
596
597AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}])
598
599
600# ----------------------------- #
601# Simple LALR1 C++ Calculator. #
602# ----------------------------- #
603
604AT_BANNER([[Simple LALR1 C++ Calculator.]])
605
606# AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS])
607# ---------------------------------------
608# Start a testing chunk which compiles `calc' grammar with
609# the C++ skeleton, and performs several tests over the parser.
610m4_define([AT_CHECK_CALC_LALR1_CC],
611[AT_CHECK_CALC([%skeleton "lalr1.cc"] $@)])
612
613# AT_CHECK_CALC_LALR1_CC()
614
615AT_CHECK_CALC_LALR1_CC([%defines %locations])
616
617AT_CHECK_CALC_LALR1_CC([%defines])
618# AT_CHECK_CALC_LALR1_CC([%locations])
619# AT_CHECK_CALC_LALR1_CC([%name-prefix="calc"])
620# AT_CHECK_CALC_LALR1_CC([%verbose])
621# AT_CHECK_CALC_LALR1_CC([%yacc])
622# AT_CHECK_CALC_LALR1_CC([%error-verbose])
623
624# AT_CHECK_CALC_LALR1_CC([%pure-parser %locations])
625# AT_CHECK_CALC_LALR1_CC([%error-verbose %locations])
626
627AT_CHECK_CALC_LALR1_CC([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
628
629# AT_CHECK_CALC_LALR1_CC([%debug])
630AT_CHECK_CALC_LALR1_CC([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
631
632AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
633
634AT_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}])