]> git.saurik.com Git - bison.git/blame - tests/calc.at
* tests/calc.at (AT_CHECK_CALC): Check different scenarios of
[bison.git] / tests / calc.at
CommitLineData
342b8b6e 1# Checking the output filenames. -*- Autotest -*-
7548fed2 2# Copyright (C) 2000, 2001, 2002, 2003 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
2a8d363a
AD
29# _AT_DATA_CALC_Y($1, $2, $3, [BISON-DIRECTIVES])
30# -----------------------------------------------
bfb07874
AD
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
9501dc6e 38AT_DATA_GRAMMAR([calc.y],
bfb07874 39[[/* Infix notation calculator--calc */
2a8d363a 40]$4[
bfb07874
AD
41%{
42#include <stdio.h>
2ce10144
AD
43
44#if STDC_HEADERS
45# include <stdlib.h>
46# include <string.h>
2ce10144 47#endif
bfb07874 48#include <ctype.h>
bfb07874 49
bfb07874 50extern void perror (const char *s);
0dd1580a
RA
51
52/* Exercise pre-prologue dependency to %union. */
e3aa2baa 53typedef int value;
0dd1580a 54
e3aa2baa 55static value global_result = 0;
4e8c79eb 56static int global_count = 0;
e7cb57c0 57
7548fed2 58]AT_LALR1_CC_IF([typedef yy::Location YYLTYPE;])[
bfb07874
AD
59%}
60
5a08f1ce 61/* Exercise %union. */
213e640e
AD
62%union
63{
e3aa2baa 64 value ival;
213e640e
AD
65};
66
c19988b7 67%{
c19988b7 68static int power (int base, int exponent);
2a8d363a
AD
69/* yyerror receives the location if:
70 - %location & %pure & %glr
71 - %location & %pure & %yacc & %parse-param. */
93724f13 72static void yyerror (]AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])[
e3aa2baa 73 ]AT_PARAM_IF([value *result, int *count, ])[
93724f13 74 const char *s
2a8d363a 75 );
7548fed2
AD
76static int yylex (]AT_LEX_FORMALS[);
77static int yygetc (]AT_LEX_FORMALS[);
78static void yyungetc (]AT_LEX_PRE_FORMALS[ int c);
c19988b7
AD
79%}
80
6b7e85b9 81/* Bison Declarations */
c19988b7 82%token CALC_EOF 0 "end of input"
213e640e
AD
83%token <ival> NUM "number"
84%type <ival> exp
bfb07874
AD
85
86%nonassoc '=' /* comparison */
87%left '-' '+'
88%left '*' '/'
89%left NEG /* negation--unary minus */
90%right '^' /* exponentiation */
91
92/* Grammar follows */
93%%
94input:
b7c49edf 95 line
2a8d363a 96| input line { ]AT_PARAM_IF([++*count; ++global_count;])[ }
bfb07874
AD
97;
98
99line:
c19988b7 100 '\n'
2a8d363a 101| exp '\n' { ]AT_PARAM_IF([*result = global_result = $1;])[ }
bfb07874
AD
102;
103
104exp:
105 NUM { $$ = $1; }
106| exp '=' exp
107 {
2a8d363a
AD
108 if ($1 != $3)
109 fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
110 $$ = $1;
bfb07874
AD
111 }
112| exp '+' exp { $$ = $1 + $3; }
113| exp '-' exp { $$ = $1 - $3; }
114| exp '*' exp { $$ = $1 * $3; }
115| exp '/' exp { $$ = $1 / $3; }
116| '-' exp %prec NEG { $$ = -$2; }
117| exp '^' exp { $$ = power ($1, $3); }
118| '(' exp ')' { $$ = $2; }
0b86fc41
AD
119| '(' error ')' { $$ = 1111; }
120| '!' { YYERROR; }
bfb07874
AD
121;
122%%
123/* The input. */
4e8c79eb 124static FILE *yyin;
bfb07874 125
7548fed2
AD
126]AT_LALR1_CC_IF(
127[/* Currently, print_ is required in C++. */
128void
129yy::Parser::print_ ()
bfb07874 130{
7548fed2 131 std::cerr << location;
bfb07874
AD
132}
133
7548fed2
AD
134/* A C++ error reporting function. */
135void
136yy::Parser::error_ ()
137{
138 std::cerr << location << ": " << message << std::endl;
139}
140
141int
142yyparse (void)
143{
144 yy::Parser parser = yy::Parser (!!YYDEBUG[]AT_LOCATION_IF([,
145 yy::Location::Location ()]));
146 return parser.parse ();
147}
148],
149[static void
150yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ])
151 AT_PARAM_IF([value *result, int *count, ])
152 const char *s)
153{
154AT_PARAM_IF([(void) result; (void) count;])
155AT_YYERROR_SEES_LOC_IF([
156 fprintf (stderr, "%d.%d",
157 AT_LOC.first_line, AT_LOC.first_column);
158 if (AT_LOC.first_line != AT_LOC.last_line)
159 fprintf (stderr, "-%d.%d",
160 AT_LOC.last_line, AT_LOC.last_column - 1);
161 else if (AT_LOC.first_column != AT_LOC.last_column - 1)
162 fprintf (stderr, "-%d",
163 AT_LOC.last_column - 1);
164 fprintf (stderr, ": ");])
165 fprintf (stderr, "%s\n", s);
166}])[
167
b7c49edf 168
2a8d363a 169]AT_LOCATION_IF([
b7c49edf 170static YYLTYPE last_yylloc;
2a8d363a 171])[
bfb07874 172static int
7548fed2 173yygetc (]AT_LEX_FORMALS[)
bfb07874
AD
174{
175 int res = getc (yyin);
7548fed2 176 ]AT_USE_LEX_ARGS[;
2a8d363a 177]AT_LOCATION_IF([
7548fed2 178 last_yylloc = AT_LOC;
bfb07874
AD
179 if (res == '\n')
180 {
7548fed2
AD
181AT_LALR1_CC_IF(
182[ AT_LOC.end.line++;
183 AT_LOC.end.column = 0;],
184[ AT_LOC.last_line++;
185 AT_LOC.last_column = 0;])
bfb07874
AD
186 }
187 else
7548fed2
AD
188AT_LALR1_CC_IF(
189[ AT_LOC.end.column++;],
190[ AT_LOC.last_column++;])
2a8d363a 191])[
bfb07874
AD
192 return res;
193}
194
195
196static void
7548fed2 197yyungetc (]AT_LEX_PRE_FORMALS[ int c)
bfb07874 198{
7548fed2 199 ]AT_USE_LEX_ARGS[;
2a8d363a 200]AT_LOCATION_IF([
bfb07874 201 /* Wrong when C == `\n'. */
7548fed2 202 AT_LOC = last_yylloc;
2a8d363a 203])[
bfb07874
AD
204 ungetc (c, yyin);
205}
206
207static int
7548fed2 208read_signed_integer (]AT_LEX_FORMALS[)
bfb07874 209{
7548fed2 210 int c = yygetc (]AT_LEX_ARGS[);
bfb07874
AD
211 int sign = 1;
212 int n = 0;
213
7548fed2 214 ]AT_USE_LEX_ARGS[;
bfb07874
AD
215 if (c == '-')
216 {
7548fed2 217 c = yygetc (]AT_LEX_ARGS[);
bfb07874
AD
218 sign = -1;
219 }
220
221 while (isdigit (c))
222 {
223 n = 10 * n + (c - '0');
7548fed2 224 c = yygetc (]AT_LEX_ARGS[);
bfb07874
AD
225 }
226
7548fed2 227 yyungetc (]AT_LEX_PRE_ARGS[ c);
bfb07874
AD
228
229 return sign * n;
230}
231
232
233
234/*---------------------------------------------------------------.
235| Lexical analyzer returns an integer on the stack and the token |
236| NUM, or the ASCII character read if not a number. Skips all |
237| blanks and tabs, returns 0 for EOF. |
238`---------------------------------------------------------------*/
239
240static int
7548fed2 241yylex (]AT_LEX_FORMALS[)
bfb07874 242{
c19988b7 243 static int init = 1;
bfb07874
AD
244 int c;
245
c19988b7
AD
246 if (init)
247 {
248 init = 0;
7548fed2
AD
249]AT_LALR1_CC_IF([],
250[AT_LOCATION_IF([
251 AT_LOC.last_column = 0;
252 AT_LOC.last_line = 1;
253])])[
c19988b7
AD
254 }
255
7548fed2
AD
256]AT_LALR1_CC_IF(
257[ AT_LOC.begin = AT_LOC.end;],
258[AT_LOCATION_IF([
259 AT_LOC.first_column = AT_LOC.last_column;
260 AT_LOC.first_line = AT_LOC.last_line;
261])])[
bfb07874
AD
262
263 /* Skip white space. */
7548fed2 264 while ((c = yygetc (]AT_LEX_ARGS[)) == ' ' || c == '\t')
bfb07874 265 {
7548fed2
AD
266]AT_LALR1_CC_IF(
267[ AT_LOC.begin = AT_LOC.end;],
268[AT_LOCATION_IF([
269 AT_LOC.first_column = AT_LOC.last_column;
270 AT_LOC.first_line = AT_LOC.last_line;
271])])[
bfb07874
AD
272 }
273
274 /* process numbers */
275 if (c == '.' || isdigit (c))
276 {
7548fed2
AD
277 yyungetc (]AT_LEX_PRE_ARGS[ c);
278 ]AT_VAL[.ival = read_signed_integer (]AT_LEX_ARGS[);
bfb07874
AD
279 return NUM;
280 }
281
282 /* Return end-of-file. */
283 if (c == EOF)
6b7e85b9 284 return CALC_EOF;
bfb07874
AD
285
286 /* Return single chars. */
287 return c;
288}
289
290static int
291power (int base, int exponent)
292{
293 int res = 1;
294 if (exponent < 0)
295 exit (1);
296 for (/* Niente */; exponent; --exponent)
297 res *= base;
298 return res;
299}
300
7548fed2 301
bfb07874 302int
342b8b6e 303main (int argc, const char **argv)
bfb07874 304{
e3aa2baa 305 value result = 0;
e7cb57c0 306 int count = 0;
4e8c79eb 307 int status;
342b8b6e
AD
308
309 if (argc == 2)
bfb07874
AD
310 yyin = fopen (argv[1], "r");
311 else
312 yyin = stdin;
313
342b8b6e 314 if (!yyin)
bfb07874
AD
315 {
316 perror (argv[1]);
317 exit (1);
318 }
319
320#if YYDEBUG
321 yydebug = 1;
bfb07874 322#endif
b0f98b10 323 status = yyparse (]AT_PARAM_IF([&result, &count])[);
63d0fb9c
PE
324 if (global_result != result)
325 abort ();
326 if (global_count != count)
327 abort ();
b0f98b10 328 return status;
bfb07874
AD
329}
330]])
331])# _AT_DATA_CALC_Y
332
333
334# AT_DATA_CALC_Y([BISON-OPTIONS])
335# -------------------------------
336# Produce `calc.y'.
342b8b6e 337m4_define([AT_DATA_CALC_Y],
9e32add8 338[_AT_DATA_CALC_Y($[1], $[2], $[3], [$1])
f50adbbd 339])
bfb07874
AD
340
341
342
342b8b6e
AD
343# _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0])
344# ------------------------------------------------------------
bfb07874 345# Run `calc' on INPUT and expect no STDOUT nor STDERR.
342b8b6e 346#
f50adbbd
AD
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.
342b8b6e
AD
352m4_define([_AT_CHECK_CALC],
353[AT_DATA([[input]],
354[[$2
355]])
f50adbbd
AD
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])])
342b8b6e
AD
363])
364
365
b0f98b10
AD
366# _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT,
367# [NUM-DEBUG-LINES],
2a8d363a 368# [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
b0f98b10 369# ---------------------------------------------------------
6e649e65 370# Run `calc' on INPUT, and expect a `syntax error' message.
342b8b6e 371#
b7c49edf
AD
372# If INPUT starts with a slash, it is used as absolute input file name,
373# otherwise as contents.
374#
9e32add8 375# If BISON-OPTIONS contains `%location', then make sure the ERROR-LOCATION
342b8b6e
AD
376# is correctly output on stderr.
377#
f50adbbd 378# If BISON-OPTIONS contains `%error-verbose', then make sure the
6e649e65 379# IF-YYERROR-VERBOSE message is properly output after `syntax error, '
342b8b6e
AD
380# on STDERR.
381#
f50adbbd
AD
382# If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES
383# is the number of expected lines on stderr.
342b8b6e 384m4_define([_AT_CHECK_CALC_ERROR],
b0f98b10
AD
385[m4_bmatch([$3], [^/],
386 [AT_PARSER_CHECK([./calc $3], $2, [], [stderr])],
b7c49edf 387 [AT_DATA([[input]],
b0f98b10 388[[$3
342b8b6e 389]])
b0f98b10 390AT_PARSER_CHECK([./calc input], $2, [], [stderr])])
f50adbbd
AD
391m4_bmatch([$1],
392 [%debug.*%glr\|%glr.*%debug],
393 [],
394 [%debug],
b0f98b10 395 [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$4
51dec47b 396])])
342b8b6e 397
51dec47b
AD
398# Normalize the observed and expected error messages, depending upon the
399# options.
400# 1. Remove the traces from observed.
9280d3ef
AD
401sed '/^Starting/d
402/^Entering/d
f50adbbd 403/^Stack/d
9280d3ef
AD
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
342b8b6e 412mv at-stderr stderr
51dec47b
AD
413# 2. Create the reference error message.
414AT_DATA([[expout]],
b0f98b10 415[$5
342b8b6e 416])
51dec47b 417# 3. If locations are not used, remove them.
2a8d363a 418AT_YYERROR_SEES_LOC_IF([],
51dec47b
AD
419[[sed 's/^[-0-9.]*: //' expout >at-expout
420mv at-expout expout]])
421# 4. If error-verbose is not used, strip the`, unexpected....' part.
f50adbbd 422m4_bmatch([$1], [%error-verbose], [],
6e649e65 423[[sed 's/syntax error, .*$/syntax error/' expout >at-expout
51dec47b
AD
424mv at-expout expout]])
425# 5. Check
426AT_CHECK([cat stderr], 0, [expout])
342b8b6e 427])
bfb07874
AD
428
429
2a8d363a
AD
430# AT_CALC_PUSHDEFS($1, $2, [BISON-OPTIONS])
431# -----------------------------------------
432# This macro works around the impossibility to define macros
433# inside macros, because issuing `[$1]' is not possible in M4 :(.
434# This sucks hard, GNU M4 should really provide M5 like $$1.
435m4_define([AT_CHECK_PUSHDEFS],
436[m4_if([$1$2], $[1]$[2], [],
437 [m4_fatal([$0: Invalid arguments: $@])])dnl
7548fed2
AD
438m4_pushdef([AT_LALR1_CC_IF],
439[m4_bmatch([$3], ["lalr1.cc"], [$1], [$2])])
440m4_pushdef([AT_GLR_IF],
441[m4_bmatch([$3], [%glr-parser], [$1], [$2])])
2a8d363a
AD
442m4_pushdef([AT_PARAM_IF],
443[m4_bmatch([$3], [%parse-param], [$1], [$2])])
444m4_pushdef([AT_LOCATION_IF],
445[m4_bmatch([$3], [%locations], [$1], [$2])])
446m4_pushdef([AT_PURE_IF],
447[m4_bmatch([$3], [%pure-parser], [$1], [$2])])
2a8d363a
AD
448m4_pushdef([AT_PURE_AND_LOC_IF],
449[m4_bmatch([$3], [%locations.*%pure-parser\|%pure-parser.*%locations],
450 [$1], [$2])])
451m4_pushdef([AT_GLR_OR_PARAM_IF],
452[m4_bmatch([$3], [%glr-parser\|%parse-param], [$1], [$2])])
453
454# yyerror receives the location if %location & %pure & (%glr or %parse-param).
455m4_pushdef([AT_YYERROR_ARG_LOC_IF],
456[AT_GLR_OR_PARAM_IF([AT_PURE_AND_LOC_IF([$1], [$2])],
457 [$2])])
793a58bb 458# yyerror cannot see the locations if !glr & pure & !param.
2a8d363a 459m4_pushdef([AT_YYERROR_SEES_LOC_IF],
7548fed2
AD
460[AT_LALR1_CC_IF([$1],
461 [AT_LOCATION_IF([AT_GLR_IF([$1],
462 [AT_PURE_IF([AT_PARAM_IF([$1],
463 [$2])],
464 [$1])])],
465 [$2])])])
466
467# The interface is pure: either because %pure-parser, or because we
468# are using the C++ parsers.
469m4_pushdef([AT_PURE_LEX_IF],
470[AT_PURE_IF([$1],
471 [AT_LALR1_CC_IF([$1], [$2])])])
472
473AT_PURE_LEX_IF(
474[m4_pushdef([AT_LOC], [(*yylloc)])
475 m4_pushdef([AT_VAL], [(*yylval)])
476 m4_pushdef([AT_LEX_FORMALS],
477 [YYSTYPE *yylval[]AT_LOCATION_IF([, YYLTYPE *yylloc])])
478 m4_pushdef([AT_LEX_ARGS],
479 [yylval[]AT_LOCATION_IF([, yylloc])])
480 m4_pushdef([AT_USE_LEX_ARGS],
481 [(void) yylval;AT_LOCATION_IF([(void) yylloc])])
482 m4_pushdef([AT_LEX_PRE_FORMALS],
483 [AT_LEX_FORMALS, ])
484 m4_pushdef([AT_LEX_PRE_ARGS],
485 [AT_LEX_ARGS, ])
486],
487[m4_pushdef([AT_LOC], [(yylloc)])
488 m4_pushdef([AT_VAL], [(yylval)])
489 m4_pushdef([AT_LEX_FORMALS], [void])
490 m4_pushdef([AT_LEX_ARGS], [])
491 m4_pushdef([AT_USE_LEX_ARGS], [])
492 m4_pushdef([AT_LEX_PRE_FORMALS], [])
493 m4_pushdef([AT_LEX_PRE_ARGS], [])
2a8d363a 494])
7548fed2 495])# AT_CALC_PUSHDEFS
2a8d363a
AD
496
497
498# AT_CALC_POPDEFS
499# ---------------
500m4_define([AT_CHECK_POPDEFS],
7548fed2
AD
501[m4_popdef([AT_LEX_PRE_ARGS])
502m4_popdef([AT_LEX_PRE_FORMALS])
503m4_popdef([AT_USE_LEX_ARGS])
504m4_popdef([AT_LEX_ARGS])
505m4_popdef([AT_LEX_FORMALS])
506m4_popdef([AT_VAL])
507m4_popdef([AT_LOC])
508m4_popdef([AT_PURE_LEX_IF])
509m4_popdef([AT_YYERROR_SEES_LOC_IF])
2a8d363a
AD
510m4_popdef([AT_YYERROR_ARG_LOC_IF])
511m4_popdef([AT_GLR_OR_PARAM_IF])
512m4_popdef([AT_PURE_AND_LOC_IF])
2a8d363a
AD
513m4_popdef([AT_LOCATION_IF])
514m4_popdef([AT_PARAM_IF])
7548fed2
AD
515m4_popdef([AT_GLR_IF])
516m4_popdef([AT_LALR1_CC_IF])
2a8d363a
AD
517])
518
519
520
f50adbbd
AD
521# AT_CHECK_CALC([BISON-OPTIONS])
522# ------------------------------
bfb07874
AD
523# Start a testing chunk which compiles `calc' grammar with
524# BISON-OPTIONS, and performs several tests over the parser.
342b8b6e 525m4_define([AT_CHECK_CALC],
bfb07874
AD
526[# We use integers to avoid dependencies upon the precision of doubles.
527AT_SETUP([Calculator $1])
528
2a8d363a
AD
529AT_CHECK_PUSHDEFS($[1], $[2], [$1])
530
bfb07874
AD
531AT_DATA_CALC_Y([$1])
532
533# Specify the output files to avoid problems on different file systems.
9e32add8 534AT_CHECK([bison -o calc.c calc.y],
bfb07874 535 [0], [], [])
3c1a79b3 536
7548fed2
AD
537AT_LALR1_CC_IF(
538[AT_CHECK([$CXX --version || exit 77], 0, ignore, ignore)
539AT_COMPILE_CXX([calc])],
540[AT_COMPILE([calc])])
bfb07874
AD
541
542# Test the priorities.
543_AT_CHECK_CALC([$1],
544[1 + 2 * 3 = 7
5451 + 2 * -3 = -5
546
547-1^2 = -1
548(-1)^2 = 1
549
550---1 = -1
551
5521 - 2 - 3 = -4
5531 - (2 - 3) = 2
554
5552^2^3 = 256
e7cb57c0
AD
556(2^2)^3 = 64],
557 [486])
bfb07874 558
6e649e65 559# Some syntax errors.
b0f98b10 560_AT_CHECK_CALC_ERROR([$1], [1], [0 0], [11],
7548fed2 561 [1.2: syntax error, unexpected "number"])
b0f98b10 562_AT_CHECK_CALC_ERROR([$1], [1], [1//2], [15],
0b86fc41 563 [1.2: syntax error, unexpected '/', expecting "number" or '-' or '(' or '!'])
b0f98b10 564_AT_CHECK_CALC_ERROR([$1], [1], [error], [4],
0b86fc41 565 [1.0: syntax error, unexpected $undefined])
b0f98b10 566_AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [22],
7548fed2 567 [1.6: syntax error, unexpected '='])
b0f98b10 568_AT_CHECK_CALC_ERROR([$1], [1],
bfb07874
AD
569 [
570+1],
366eea36 571 [14],
7548fed2 572 [2.0: syntax error, unexpected '+'])
b7c49edf 573# Exercise error messages with EOF: work on an empty file.
b0f98b10 574_AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4],
0b86fc41 575 [1.0: syntax error, unexpected "end of input"])
51dec47b
AD
576
577# Exercise the error token: without it, we die at the first error,
0b86fc41
AD
578# hence be sure to
579#
580# - have several errors which exercise different shift/discardings
581# - (): nothing to pop, nothing to discard
582# - (1 + 1 + 1 +): a lot to pop, nothing to discard
583# - (* * *): nothing to pop, a lot to discard
584# - (1 + 2 * *): some to pop and discard
585#
586# - test the action associated to `error'
587#
588# - check the lookahead that triggers an error is not discarded
589# when we enter error recovery. Below, the lookahead causing the
590# first error is ")", which is needed to recover from the error and
591# produce the "0" that triggers the "0 != 1" error.
592#
593_AT_CHECK_CALC_ERROR([$1], [0],
594 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
595 [156],
596[1.1: syntax error, unexpected ')', expecting "number" or '-' or '(' or '!'
5971.17: syntax error, unexpected ')', expecting "number" or '-' or '(' or '!'
5981.22: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!'
5991.40: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!'
600calc: error: 4444 != 1])
601
602# The same, but this time exercising explicitly triggered syntax errors.
603# POSIX says the lookahead causing the error should not be discarded.
604_AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [64],
605[1.9: syntax error, unexpected "number"
606calc: error: 2222 != 1])
2a8d363a
AD
607AT_CHECK_POPDEFS
608
d803322e 609AT_CLEANUP
bfb07874
AD
610])# AT_CHECK_CALC
611
612
613
614
f50adbbd
AD
615# ------------------------ #
616# Simple LALR Calculator. #
617# ------------------------ #
618
619AT_BANNER([[Simple LALR Calculator.]])
620
621# AT_CHECK_CALC_LALR([BISON-OPTIONS])
622# -----------------------------------
623# Start a testing chunk which compiles `calc' grammar with
624# BISON-OPTIONS, and performs several tests over the parser.
625m4_define([AT_CHECK_CALC_LALR],
626[AT_CHECK_CALC($@)])
627
628AT_CHECK_CALC_LALR()
629
9e32add8 630AT_CHECK_CALC_LALR([%defines])
ae7453f2 631AT_CHECK_CALC_LALR([%locations])
9e32add8
AD
632AT_CHECK_CALC_LALR([%name-prefix="calc"])
633AT_CHECK_CALC_LALR([%verbose])
634AT_CHECK_CALC_LALR([%yacc])
f50adbbd
AD
635AT_CHECK_CALC_LALR([%error-verbose])
636
ae7453f2 637AT_CHECK_CALC_LALR([%error-verbose %locations])
f50adbbd 638
9e32add8 639AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
f50adbbd
AD
640
641AT_CHECK_CALC_LALR([%debug])
9e32add8 642AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
c19988b7 643
2a8d363a
AD
644AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
645
e3aa2baa 646AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {value *result} %parse-param {int *count}])
f50adbbd
AD
647
648
649# ----------------------- #
650# Simple GLR Calculator. #
651# ----------------------- #
652
653AT_BANNER([[Simple GLR Calculator.]])
654
655# AT_CHECK_CALC_GLR([BISON-OPTIONS])
656# ----------------------------------
657# Start a testing chunk which compiles `calc' grammar with
658# BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
659m4_define([AT_CHECK_CALC_GLR],
ae7453f2 660[AT_CHECK_CALC([%glr-parser] $@)])
f50adbbd 661
bfb07874 662
f50adbbd 663AT_CHECK_CALC_GLR()
bfb07874 664
9e32add8 665AT_CHECK_CALC_GLR([%defines])
ae7453f2 666AT_CHECK_CALC_GLR([%locations])
9e32add8
AD
667AT_CHECK_CALC_GLR([%name-prefix="calc"])
668AT_CHECK_CALC_GLR([%verbose])
669AT_CHECK_CALC_GLR([%yacc])
f50adbbd 670AT_CHECK_CALC_GLR([%error-verbose])
bfb07874 671
ae7453f2 672AT_CHECK_CALC_GLR([%error-verbose %locations])
bfb07874 673
9e32add8 674AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
bfb07874 675
f50adbbd 676AT_CHECK_CALC_GLR([%debug])
9e32add8 677AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
21964f43 678
2a8d363a
AD
679AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
680
e3aa2baa 681AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc %parse-param {value *result} %parse-param {int *count}])
7548fed2
AD
682
683
684# ----------------------------- #
685# Simple LALR1 C++ Calculator. #
686# ----------------------------- #
687
688AT_BANNER([[Simple LALR1 C++ Calculator.]])
689
690# AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS])
691# ---------------------------------------
692# Start a testing chunk which compiles `calc' grammar with
693# BISON-OPTIONS and %glr-parser, and performs several tests over the parser.
694m4_define([AT_CHECK_CALC_LALR1_CC],
695[AT_CHECK_CALC([%skeleton "lalr1.cc"] $@)])
696
697# AT_CHECK_CALC_LALR1_CC()
698
0b86fc41
AD
699# FIXME: YYERROR is not supported
700# AT_CHECK_CALC_LALR1_CC([%defines %pure-parser %locations])
701
7548fed2
AD
702# AT_CHECK_CALC_LALR1_CC([%defines])
703# AT_CHECK_CALC_LALR1_CC([%locations])
704# AT_CHECK_CALC_LALR1_CC([%name-prefix="calc"])
705# AT_CHECK_CALC_LALR1_CC([%verbose])
706# AT_CHECK_CALC_LALR1_CC([%yacc])
707# AT_CHECK_CALC_LALR1_CC([%error-verbose])
708
709# AT_CHECK_CALC_LALR1_CC([%error-verbose %locations])
710
711# AT_CHECK_CALC_LALR1_CC([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc])
712
713# AT_CHECK_CALC_LALR1_CC([%debug])
714# AT_CHECK_CALC_LALR1_CC([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
715
716# AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc])
717
718# 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}])