]>
Commit | Line | Data |
---|---|---|
82c53be4 | 1 | # Simple calculator. -*- Autotest -*- |
419ab105 | 2 | |
65015668 | 3 | # Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software |
419ab105 | 4 | # Foundation, Inc. |
bfb07874 | 5 | |
f16b0819 | 6 | # This program is free software: you can redistribute it and/or modify |
342b8b6e | 7 | # it under the terms of the GNU General Public License as published by |
f16b0819 PE |
8 | # the Free Software Foundation, either version 3 of the License, or |
9 | # (at your option) any later version. | |
10 | # | |
342b8b6e AD |
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. | |
f16b0819 | 15 | # |
342b8b6e | 16 | # You should have received a copy of the GNU General Public License |
f16b0819 | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
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 |
35 | m4_define([_AT_DATA_CALC_Y], |
36 | [m4_if([$1$2$3], $[1]$[2]$[3], [], | |
37 | [m4_fatal([$0: Invalid arguments: $@])])dnl | |
9501dc6e | 38 | AT_DATA_GRAMMAR([calc.y], |
bfb07874 | 39 | [[/* Infix notation calculator--calc */ |
fb9712a9 | 40 | ]$4 |
8f7e3cf9 | 41 | AT_SKEL_CC_IF( |
16dc6a9e | 42 | [%define global_tokens_and_yystype])[ |
bfb07874 AD |
43 | %{ |
44 | #include <stdio.h> | |
2ce10144 | 45 | |
6e26ca8c PE |
46 | #include <stdlib.h> |
47 | #include <string.h> | |
82c53be4 PE |
48 | #if HAVE_UNISTD_H |
49 | # include <unistd.h> | |
50 | #else | |
51 | # undef alarm | |
52 | # define alarm(seconds) /* empty */ | |
53 | #endif | |
bfb07874 | 54 | #include <ctype.h> |
affac613 | 55 | #define USE(Var) |
bfb07874 | 56 | |
0dd1580a | 57 | /* Exercise pre-prologue dependency to %union. */ |
caf37a36 | 58 | typedef int semantic_value; |
0dd1580a | 59 | |
caf37a36 | 60 | static semantic_value global_result = 0; |
4e8c79eb | 61 | static int global_count = 0; |
bfb07874 AD |
62 | %} |
63 | ||
5a08f1ce | 64 | /* Exercise %union. */ |
213e640e AD |
65 | %union |
66 | { | |
caf37a36 | 67 | semantic_value ival; |
213e640e AD |
68 | }; |
69 | ||
c19988b7 | 70 | %{ |
c19988b7 | 71 | static int power (int base, int exponent); |
8f7e3cf9 AD |
72 | ]AT_SKEL_CC_IF( |
73 | [#ifndef YYLTYPE | |
aa08666d | 74 | [#] define YYLTYPE AT_NAME_PREFIX::location |
8f7e3cf9 | 75 | #endif |
fb9712a9 AD |
76 | #define first_line begin.line |
77 | #define first_column begin.column | |
78 | #define last_line end.line | |
79 | #define last_column end.column | |
80 | ], | |
67a25fed | 81 | [/* yyerror receives the location if: |
2a8d363a AD |
82 | - %location & %pure & %glr |
83 | - %location & %pure & %yacc & %parse-param. */ | |
ebc3737e | 84 | static void yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ]) |
caf37a36 | 85 | AT_PARAM_IF([semantic_value *result, int *count, ]) |
93724f13 | 86 | const char *s |
d02b25f9 | 87 | );])[ |
7548fed2 | 88 | static int yylex (]AT_LEX_FORMALS[); |
a9739e7c PE |
89 | static int get_char (]AT_LEX_FORMALS[); |
90 | static void unget_char (]AT_LEX_PRE_FORMALS[ int c); | |
c19988b7 AD |
91 | %} |
92 | ||
2ea7730c AD |
93 | ]AT_SKEL_CC_IF([AT_LOCATION_IF([ |
94 | /* The lalr1.cc skeleton, for backward compatibility, defines | |
8f7e3cf9 AD |
95 | a constructor for position that initializes the filename. The |
96 | glr.cc skeleton does not (and in fact cannot: location/position | |
97 | are stored in a union, from which objects with constructors are | |
2ea7730c | 98 | excluded in C++). */ |
8f7e3cf9 | 99 | %initial-action { |
ed94ef2a | 100 | @$.initialize (0); |
8f7e3cf9 | 101 | } |
2ea7730c | 102 | ])])[ |
8f7e3cf9 | 103 | |
6b7e85b9 | 104 | /* Bison Declarations */ |
c19988b7 | 105 | %token CALC_EOF 0 "end of input" |
213e640e AD |
106 | %token <ival> NUM "number" |
107 | %type <ival> exp | |
bfb07874 | 108 | |
d78f0ac9 | 109 | %nonassoc '=' /* comparison */ |
bfb07874 AD |
110 | %left '-' '+' |
111 | %left '*' '/' | |
d78f0ac9 AD |
112 | %precedence NEG /* negation--unary minus */ |
113 | %right '^' /* exponentiation */ | |
bfb07874 AD |
114 | |
115 | /* Grammar follows */ | |
116 | %% | |
117 | input: | |
b7c49edf | 118 | line |
2a8d363a | 119 | | input line { ]AT_PARAM_IF([++*count; ++global_count;])[ } |
bfb07874 AD |
120 | ; |
121 | ||
122 | line: | |
c19988b7 | 123 | '\n' |
affac613 | 124 | | exp '\n' { ]AT_PARAM_IF([*result = global_result = $1], [USE ($1)])[; } |
bfb07874 AD |
125 | ; |
126 | ||
127 | exp: | |
128 | NUM { $$ = $1; } | |
129 | | exp '=' exp | |
130 | { | |
2a8d363a AD |
131 | if ($1 != $3) |
132 | fprintf (stderr, "calc: error: %d != %d\n", $1, $3); | |
133 | $$ = $1; | |
bfb07874 AD |
134 | } |
135 | | exp '+' exp { $$ = $1 + $3; } | |
136 | | exp '-' exp { $$ = $1 - $3; } | |
137 | | exp '*' exp { $$ = $1 * $3; } | |
138 | | exp '/' exp { $$ = $1 / $3; } | |
139 | | '-' exp %prec NEG { $$ = -$2; } | |
140 | | exp '^' exp { $$ = power ($1, $3); } | |
141 | | '(' exp ')' { $$ = $2; } | |
65015668 | 142 | | '(' error ')' { $$ = 1111; yyerrok; } |
8f3596a6 AD |
143 | | '!' { $$ = 0; YYERROR; } |
144 | | '-' error { $$ = 0; YYERROR; } | |
bfb07874 AD |
145 | ; |
146 | %% | |
8f3596a6 | 147 | /* The input. */ |
a9739e7c | 148 | static FILE *input; |
bfb07874 | 149 | |
8f7e3cf9 | 150 | ]AT_SKEL_CC_IF( |
8f3596a6 | 151 | [/* A C++ error reporting function. */ |
7548fed2 | 152 | void |
2ea7730c | 153 | AT_NAME_PREFIX::parser::error (AT_LOCATION_IF([const location& l, ])const std::string& m) |
7548fed2 | 154 | { |
efeed023 | 155 | std::cerr << AT_LOCATION_IF([l << ": " << ])m << std::endl; |
7548fed2 AD |
156 | } |
157 | ||
158 | int | |
caf37a36 | 159 | yyparse (AT_PARAM_IF([semantic_value *result, int *count])) |
7548fed2 | 160 | { |
aa08666d | 161 | AT_NAME_PREFIX::parser parser[]AT_PARAM_IF([ (result, count)]); |
fa7b79c0 PE |
162 | #if YYDEBUG |
163 | parser.set_debug_level (1); | |
164 | #endif | |
7548fed2 AD |
165 | return parser.parse (); |
166 | } | |
167 | ], | |
168 | [static void | |
ebc3737e | 169 | yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ]) |
caf37a36 | 170 | AT_PARAM_IF([semantic_value *result, int *count, ]) |
7548fed2 AD |
171 | const char *s) |
172 | { | |
173 | AT_PARAM_IF([(void) result; (void) count;]) | |
174 | AT_YYERROR_SEES_LOC_IF([ | |
175 | fprintf (stderr, "%d.%d", | |
176 | AT_LOC.first_line, AT_LOC.first_column); | |
177 | if (AT_LOC.first_line != AT_LOC.last_line) | |
178 | fprintf (stderr, "-%d.%d", | |
179 | AT_LOC.last_line, AT_LOC.last_column - 1); | |
180 | else if (AT_LOC.first_column != AT_LOC.last_column - 1) | |
181 | fprintf (stderr, "-%d", | |
182 | AT_LOC.last_column - 1); | |
183 | fprintf (stderr, ": ");]) | |
184 | fprintf (stderr, "%s\n", s); | |
185 | }])[ | |
186 | ||
b7c49edf | 187 | |
2a8d363a | 188 | ]AT_LOCATION_IF([ |
b7c49edf | 189 | static YYLTYPE last_yylloc; |
2a8d363a | 190 | ])[ |
bfb07874 | 191 | static int |
a9739e7c | 192 | get_char (]AT_LEX_FORMALS[) |
bfb07874 | 193 | { |
a9739e7c | 194 | int res = getc (input); |
7548fed2 | 195 | ]AT_USE_LEX_ARGS[; |
2a8d363a | 196 | ]AT_LOCATION_IF([ |
7548fed2 | 197 | last_yylloc = AT_LOC; |
bfb07874 AD |
198 | if (res == '\n') |
199 | { | |
fb9712a9 | 200 | AT_LOC.last_line++; |
cd48d21d | 201 | AT_LOC.last_column = 1; |
bfb07874 AD |
202 | } |
203 | else | |
fb9712a9 | 204 | AT_LOC.last_column++; |
2a8d363a | 205 | ])[ |
bfb07874 AD |
206 | return res; |
207 | } | |
208 | ||
209 | ||
210 | static void | |
a9739e7c | 211 | unget_char (]AT_LEX_PRE_FORMALS[ int c) |
bfb07874 | 212 | { |
7548fed2 | 213 | ]AT_USE_LEX_ARGS[; |
2a8d363a | 214 | ]AT_LOCATION_IF([ |
bfb07874 | 215 | /* Wrong when C == `\n'. */ |
7548fed2 | 216 | AT_LOC = last_yylloc; |
2a8d363a | 217 | ])[ |
a9739e7c | 218 | ungetc (c, input); |
bfb07874 AD |
219 | } |
220 | ||
221 | static int | |
7548fed2 | 222 | read_signed_integer (]AT_LEX_FORMALS[) |
bfb07874 | 223 | { |
a9739e7c | 224 | int c = get_char (]AT_LEX_ARGS[); |
bfb07874 AD |
225 | int sign = 1; |
226 | int n = 0; | |
227 | ||
7548fed2 | 228 | ]AT_USE_LEX_ARGS[; |
bfb07874 AD |
229 | if (c == '-') |
230 | { | |
a9739e7c | 231 | c = get_char (]AT_LEX_ARGS[); |
bfb07874 AD |
232 | sign = -1; |
233 | } | |
234 | ||
235 | while (isdigit (c)) | |
236 | { | |
237 | n = 10 * n + (c - '0'); | |
a9739e7c | 238 | c = get_char (]AT_LEX_ARGS[); |
bfb07874 AD |
239 | } |
240 | ||
a9739e7c | 241 | unget_char (]AT_LEX_PRE_ARGS[ c); |
bfb07874 AD |
242 | |
243 | return sign * n; | |
244 | } | |
245 | ||
246 | ||
247 | ||
248 | /*---------------------------------------------------------------. | |
249 | | Lexical analyzer returns an integer on the stack and the token | | |
250 | | NUM, or the ASCII character read if not a number. Skips all | | |
251 | | blanks and tabs, returns 0 for EOF. | | |
252 | `---------------------------------------------------------------*/ | |
253 | ||
254 | static int | |
7548fed2 | 255 | yylex (]AT_LEX_FORMALS[) |
bfb07874 | 256 | { |
c19988b7 | 257 | static int init = 1; |
bfb07874 AD |
258 | int c; |
259 | ||
c19988b7 AD |
260 | if (init) |
261 | { | |
262 | init = 0; | |
fb9712a9 | 263 | ]AT_LOCATION_IF([ |
cd48d21d | 264 | AT_LOC.last_column = 1; |
7548fed2 | 265 | AT_LOC.last_line = 1; |
fb9712a9 | 266 | ])[ |
c19988b7 AD |
267 | } |
268 | ||
fb9712a9 AD |
269 | ]AT_LOCATION_IF([ |
270 | AT_LOC.first_column = AT_LOC.last_column; | |
7548fed2 | 271 | AT_LOC.first_line = AT_LOC.last_line; |
fb9712a9 | 272 | ])[ |
bfb07874 AD |
273 | |
274 | /* Skip white space. */ | |
a9739e7c | 275 | while ((c = get_char (]AT_LEX_ARGS[)) == ' ' || c == '\t') |
bfb07874 | 276 | { |
fb9712a9 | 277 | ]AT_LOCATION_IF( |
d02b25f9 | 278 | [ AT_LOC.first_column = AT_LOC.last_column; |
7548fed2 | 279 | AT_LOC.first_line = AT_LOC.last_line; |
fb9712a9 | 280 | ])[ |
bfb07874 AD |
281 | } |
282 | ||
283 | /* process numbers */ | |
284 | if (c == '.' || isdigit (c)) | |
285 | { | |
a9739e7c | 286 | unget_char (]AT_LEX_PRE_ARGS[ c); |
7548fed2 | 287 | ]AT_VAL[.ival = read_signed_integer (]AT_LEX_ARGS[); |
bfb07874 AD |
288 | return NUM; |
289 | } | |
290 | ||
291 | /* Return end-of-file. */ | |
292 | if (c == EOF) | |
6b7e85b9 | 293 | return CALC_EOF; |
bfb07874 AD |
294 | |
295 | /* Return single chars. */ | |
296 | return c; | |
297 | } | |
298 | ||
299 | static int | |
300 | power (int base, int exponent) | |
301 | { | |
302 | int res = 1; | |
303 | if (exponent < 0) | |
6100a9aa | 304 | exit (3); |
bfb07874 AD |
305 | for (/* Niente */; exponent; --exponent) |
306 | res *= base; | |
307 | return res; | |
308 | } | |
309 | ||
7548fed2 | 310 | |
bfb07874 | 311 | int |
342b8b6e | 312 | main (int argc, const char **argv) |
bfb07874 | 313 | { |
caf37a36 | 314 | semantic_value result = 0; |
e7cb57c0 | 315 | int count = 0; |
4e8c79eb | 316 | int status; |
342b8b6e | 317 | |
419ab105 PE |
318 | /* This used to be alarm (10), but that isn't enough time for |
319 | a July 1995 vintage DEC Alphastation 200 4/100 system, | |
320 | according to Nelson H. F. Beebe. 100 seconds is enough. */ | |
321 | alarm (100); | |
322 | ||
342b8b6e | 323 | if (argc == 2) |
a9739e7c | 324 | input = fopen (argv[1], "r"); |
bfb07874 | 325 | else |
a9739e7c | 326 | input = stdin; |
bfb07874 | 327 | |
a9739e7c | 328 | if (!input) |
bfb07874 AD |
329 | { |
330 | perror (argv[1]); | |
6100a9aa | 331 | return 3; |
bfb07874 AD |
332 | } |
333 | ||
8f7e3cf9 | 334 | ]AT_SKEL_CC_IF([], [m4_bmatch([$4], [%debug], |
d02b25f9 | 335 | [ yydebug = 1;])])[ |
c3d50342 | 336 | status = yyparse (]AT_PARAM_IF([[&result, &count]])[); |
eb095650 | 337 | fclose (input); |
63d0fb9c PE |
338 | if (global_result != result) |
339 | abort (); | |
340 | if (global_count != count) | |
341 | abort (); | |
b0f98b10 | 342 | return status; |
bfb07874 AD |
343 | } |
344 | ]]) | |
345 | ])# _AT_DATA_CALC_Y | |
346 | ||
347 | ||
348 | # AT_DATA_CALC_Y([BISON-OPTIONS]) | |
349 | # ------------------------------- | |
350 | # Produce `calc.y'. | |
342b8b6e | 351 | m4_define([AT_DATA_CALC_Y], |
9e32add8 | 352 | [_AT_DATA_CALC_Y($[1], $[2], $[3], [$1]) |
f50adbbd | 353 | ]) |
bfb07874 AD |
354 | |
355 | ||
356 | ||
d9963c85 PE |
357 | # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES]) |
358 | # -------------------------------------------------------- | |
bfb07874 | 359 | # Run `calc' on INPUT and expect no STDOUT nor STDERR. |
342b8b6e | 360 | # |
f50adbbd | 361 | # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then |
d9963c85 | 362 | # |
f50adbbd | 363 | # NUM-STDERR-LINES is the number of expected lines on stderr. |
d9963c85 | 364 | # Currently this is ignored, though, since the output format is fluctuating. |
f50adbbd AD |
365 | # |
366 | # We don't count GLR's traces yet, since its traces are somewhat | |
367 | # different from LALR's. | |
342b8b6e AD |
368 | m4_define([_AT_CHECK_CALC], |
369 | [AT_DATA([[input]], | |
370 | [[$2 | |
371 | ]]) | |
f50adbbd | 372 | AT_PARSER_CHECK([./calc input], 0, [], [stderr]) |
342b8b6e AD |
373 | ]) |
374 | ||
375 | ||
b0f98b10 | 376 | # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT, |
d9963c85 | 377 | # [NUM-STDERR-LINES], |
2a8d363a | 378 | # [VERBOSE-AND-LOCATED-ERROR-MESSAGE]) |
b0f98b10 | 379 | # --------------------------------------------------------- |
6e649e65 | 380 | # Run `calc' on INPUT, and expect a `syntax error' message. |
342b8b6e | 381 | # |
b7c49edf AD |
382 | # If INPUT starts with a slash, it is used as absolute input file name, |
383 | # otherwise as contents. | |
384 | # | |
d9963c85 PE |
385 | # NUM-STDERR-LINES is the number of expected lines on stderr. |
386 | # Currently this is ignored, though, since the output format is fluctuating. | |
387 | # | |
9e32add8 | 388 | # If BISON-OPTIONS contains `%location', then make sure the ERROR-LOCATION |
342b8b6e AD |
389 | # is correctly output on stderr. |
390 | # | |
f50adbbd | 391 | # If BISON-OPTIONS contains `%error-verbose', then make sure the |
6e649e65 | 392 | # IF-YYERROR-VERBOSE message is properly output after `syntax error, ' |
342b8b6e AD |
393 | # on STDERR. |
394 | # | |
f50adbbd AD |
395 | # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES |
396 | # is the number of expected lines on stderr. | |
342b8b6e | 397 | m4_define([_AT_CHECK_CALC_ERROR], |
b0f98b10 AD |
398 | [m4_bmatch([$3], [^/], |
399 | [AT_PARSER_CHECK([./calc $3], $2, [], [stderr])], | |
b7c49edf | 400 | [AT_DATA([[input]], |
b0f98b10 | 401 | [[$3 |
342b8b6e | 402 | ]]) |
b0f98b10 | 403 | AT_PARSER_CHECK([./calc input], $2, [], [stderr])]) |
342b8b6e | 404 | |
51dec47b AD |
405 | # Normalize the observed and expected error messages, depending upon the |
406 | # options. | |
407 | # 1. Remove the traces from observed. | |
9280d3ef AD |
408 | sed '/^Starting/d |
409 | /^Entering/d | |
f50adbbd | 410 | /^Stack/d |
9280d3ef AD |
411 | /^Reading/d |
412 | /^Reducing/d | |
31c10e38 | 413 | /^Return/d |
9280d3ef AD |
414 | /^Shifting/d |
415 | /^state/d | |
dd5f2af2 | 416 | /^Cleanup:/d |
9280d3ef AD |
417 | /^Error:/d |
418 | /^Next/d | |
31c10e38 | 419 | /^Now/d |
9280d3ef | 420 | /^Discarding/d |
62b08cfc | 421 | / \$[[0-9$]]* = /d |
9280d3ef | 422 | /^yydestructor:/d' stderr >at-stderr |
342b8b6e | 423 | mv at-stderr stderr |
51dec47b AD |
424 | # 2. Create the reference error message. |
425 | AT_DATA([[expout]], | |
b0f98b10 | 426 | [$5 |
342b8b6e | 427 | ]) |
51dec47b | 428 | # 3. If locations are not used, remove them. |
2a8d363a | 429 | AT_YYERROR_SEES_LOC_IF([], |
51dec47b AD |
430 | [[sed 's/^[-0-9.]*: //' expout >at-expout |
431 | mv at-expout expout]]) | |
432 | # 4. If error-verbose is not used, strip the`, unexpected....' part. | |
f50adbbd | 433 | m4_bmatch([$1], [%error-verbose], [], |
6e649e65 | 434 | [[sed 's/syntax error, .*$/syntax error/' expout >at-expout |
51dec47b AD |
435 | mv at-expout expout]]) |
436 | # 5. Check | |
437 | AT_CHECK([cat stderr], 0, [expout]) | |
342b8b6e | 438 | ]) |
bfb07874 AD |
439 | |
440 | ||
fb9712a9 AD |
441 | # AT_CHECK_CALC([BISON-OPTIONS, [EXPECTED-TO-FAIL]]) |
442 | # -------------------------------------------------- | |
bfb07874 AD |
443 | # Start a testing chunk which compiles `calc' grammar with |
444 | # BISON-OPTIONS, and performs several tests over the parser. | |
907e3bc8 | 445 | # However, if EXPECTED-TO-FAIL is nonempty, this test is expected to fail. |
342b8b6e | 446 | m4_define([AT_CHECK_CALC], |
bfb07874 AD |
447 | [# We use integers to avoid dependencies upon the precision of doubles. |
448 | AT_SETUP([Calculator $1]) | |
449 | ||
907e3bc8 PE |
450 | m4_ifval([$2], [AT_CHECK([exit 77])]) |
451 | ||
67a25fed | 452 | AT_BISON_OPTION_PUSHDEFS([$1]) |
2a8d363a | 453 | |
bfb07874 | 454 | AT_DATA_CALC_Y([$1]) |
7ca2266a | 455 | AT_FULL_COMPILE([calc]) |
bfb07874 AD |
456 | |
457 | # Test the priorities. | |
458 | _AT_CHECK_CALC([$1], | |
459 | [1 + 2 * 3 = 7 | |
460 | 1 + 2 * -3 = -5 | |
461 | ||
462 | -1^2 = -1 | |
463 | (-1)^2 = 1 | |
464 | ||
465 | ---1 = -1 | |
466 | ||
467 | 1 - 2 - 3 = -4 | |
468 | 1 - (2 - 3) = 2 | |
469 | ||
470 | 2^2^3 = 256 | |
e7cb57c0 | 471 | (2^2)^3 = 64], |
d1ff7a7c | 472 | [842]) |
bfb07874 | 473 | |
6e649e65 | 474 | # Some syntax errors. |
d1ff7a7c | 475 | _AT_CHECK_CALC_ERROR([$1], [1], [0 0], [15], |
cd48d21d | 476 | [1.3: syntax error, unexpected number]) |
d1ff7a7c | 477 | _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [20], |
cd48d21d | 478 | [1.3: syntax error, unexpected '/', expecting number or '-' or '(' or '!']) |
e757bb10 | 479 | _AT_CHECK_CALC_ERROR([$1], [1], [error], [5], |
cd48d21d | 480 | [1.1: syntax error, unexpected $undefined]) |
d1ff7a7c | 481 | _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [30], |
cd48d21d | 482 | [1.7: syntax error, unexpected '=']) |
b0f98b10 | 483 | _AT_CHECK_CALC_ERROR([$1], [1], |
bfb07874 AD |
484 | [ |
485 | +1], | |
d1ff7a7c | 486 | [20], |
cd48d21d | 487 | [2.1: syntax error, unexpected '+']) |
b7c49edf | 488 | # Exercise error messages with EOF: work on an empty file. |
cea1469d | 489 | _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4], |
cd48d21d | 490 | [1.1: syntax error, unexpected end of input]) |
51dec47b AD |
491 | |
492 | # Exercise the error token: without it, we die at the first error, | |
0b86fc41 AD |
493 | # hence be sure to |
494 | # | |
495 | # - have several errors which exercise different shift/discardings | |
496 | # - (): nothing to pop, nothing to discard | |
497 | # - (1 + 1 + 1 +): a lot to pop, nothing to discard | |
498 | # - (* * *): nothing to pop, a lot to discard | |
499 | # - (1 + 2 * *): some to pop and discard | |
500 | # | |
501 | # - test the action associated to `error' | |
502 | # | |
742e4900 JD |
503 | # - check the lookahead that triggers an error is not discarded |
504 | # when we enter error recovery. Below, the lookahead causing the | |
0b86fc41 AD |
505 | # first error is ")", which is needed to recover from the error and |
506 | # produce the "0" that triggers the "0 != 1" error. | |
507 | # | |
508 | _AT_CHECK_CALC_ERROR([$1], [0], | |
509 | [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1], | |
d1ff7a7c | 510 | [250], |
cd48d21d AD |
511 | [1.2: syntax error, unexpected ')', expecting number or '-' or '(' or '!' |
512 | 1.18: syntax error, unexpected ')', expecting number or '-' or '(' or '!' | |
513 | 1.23: syntax error, unexpected '*', expecting number or '-' or '(' or '!' | |
514 | 1.41: syntax error, unexpected '*', expecting number or '-' or '(' or '!' | |
0b86fc41 AD |
515 | calc: error: 4444 != 1]) |
516 | ||
517 | # The same, but this time exercising explicitly triggered syntax errors. | |
742e4900 | 518 | # POSIX says the lookahead causing the error should not be discarded. |
d1ff7a7c | 519 | _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [102], |
cd48d21d | 520 | [1.10: syntax error, unexpected number |
0b86fc41 | 521 | calc: error: 2222 != 1]) |
d1ff7a7c | 522 | _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (0 0) = 1], [113], |
cd48d21d AD |
523 | [1.4: syntax error, unexpected '*', expecting number or '-' or '(' or '!' |
524 | 1.12: syntax error, unexpected number | |
82c53be4 | 525 | calc: error: 2222 != 1]) |
65015668 AD |
526 | |
527 | # Check that yyerrok works properly: second error is not reported, | |
528 | # third and fourth are. Parse status is succesfull. | |
529 | _AT_CHECK_CALC_ERROR([$1], [0], [(* *) + (*) + (*)], [113], | |
530 | [1.2: syntax error, unexpected '*', expecting number or '-' or '(' or '!' | |
531 | 1.10: syntax error, unexpected '*', expecting number or '-' or '(' or '!' | |
532 | 1.16: syntax error, unexpected '*', expecting number or '-' or '(' or '!']) | |
533 | ||
67a25fed | 534 | AT_BISON_OPTION_POPDEFS |
2a8d363a | 535 | |
d803322e | 536 | AT_CLEANUP |
bfb07874 AD |
537 | ])# AT_CHECK_CALC |
538 | ||
539 | ||
540 | ||
541 | ||
f50adbbd AD |
542 | # ------------------------ # |
543 | # Simple LALR Calculator. # | |
544 | # ------------------------ # | |
545 | ||
8f7e3cf9 | 546 | AT_BANNER([[Simple LALR(1) Calculator.]]) |
f50adbbd AD |
547 | |
548 | # AT_CHECK_CALC_LALR([BISON-OPTIONS]) | |
549 | # ----------------------------------- | |
550 | # Start a testing chunk which compiles `calc' grammar with | |
551 | # BISON-OPTIONS, and performs several tests over the parser. | |
552 | m4_define([AT_CHECK_CALC_LALR], | |
553 | [AT_CHECK_CALC($@)]) | |
554 | ||
555 | AT_CHECK_CALC_LALR() | |
556 | ||
9e32add8 | 557 | AT_CHECK_CALC_LALR([%defines]) |
ae7453f2 | 558 | AT_CHECK_CALC_LALR([%locations]) |
02975b9a | 559 | AT_CHECK_CALC_LALR([%name-prefix="calc"]) dnl test deprecated `=' |
9e32add8 AD |
560 | AT_CHECK_CALC_LALR([%verbose]) |
561 | AT_CHECK_CALC_LALR([%yacc]) | |
f50adbbd AD |
562 | AT_CHECK_CALC_LALR([%error-verbose]) |
563 | ||
d9df47b6 JD |
564 | AT_CHECK_CALC_LALR([%define api.pure %locations]) |
565 | AT_CHECK_CALC_LALR([%define api.push_pull "both" %define api.pure %locations]) | |
ae7453f2 | 566 | AT_CHECK_CALC_LALR([%error-verbose %locations]) |
f50adbbd | 567 | |
02975b9a | 568 | AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix "calc" %verbose %yacc]) |
f50adbbd AD |
569 | |
570 | AT_CHECK_CALC_LALR([%debug]) | |
02975b9a | 571 | AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) |
c19988b7 | 572 | |
d9df47b6 JD |
573 | AT_CHECK_CALC_LALR([%define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) |
574 | AT_CHECK_CALC_LALR([%define api.push_pull "both" %define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) | |
2a8d363a | 575 | |
d9df47b6 | 576 | AT_CHECK_CALC_LALR([%define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |
f50adbbd AD |
577 | |
578 | ||
579 | # ----------------------- # | |
580 | # Simple GLR Calculator. # | |
581 | # ----------------------- # | |
582 | ||
583 | AT_BANNER([[Simple GLR Calculator.]]) | |
584 | ||
585 | # AT_CHECK_CALC_GLR([BISON-OPTIONS]) | |
586 | # ---------------------------------- | |
587 | # Start a testing chunk which compiles `calc' grammar with | |
588 | # BISON-OPTIONS and %glr-parser, and performs several tests over the parser. | |
589 | m4_define([AT_CHECK_CALC_GLR], | |
ae7453f2 | 590 | [AT_CHECK_CALC([%glr-parser] $@)]) |
f50adbbd | 591 | |
bfb07874 | 592 | |
f50adbbd | 593 | AT_CHECK_CALC_GLR() |
bfb07874 | 594 | |
9e32add8 | 595 | AT_CHECK_CALC_GLR([%defines]) |
ae7453f2 | 596 | AT_CHECK_CALC_GLR([%locations]) |
02975b9a | 597 | AT_CHECK_CALC_GLR([%name-prefix "calc"]) |
9e32add8 AD |
598 | AT_CHECK_CALC_GLR([%verbose]) |
599 | AT_CHECK_CALC_GLR([%yacc]) | |
f50adbbd | 600 | AT_CHECK_CALC_GLR([%error-verbose]) |
bfb07874 | 601 | |
d9df47b6 | 602 | AT_CHECK_CALC_GLR([%define api.pure %locations]) |
ae7453f2 | 603 | AT_CHECK_CALC_GLR([%error-verbose %locations]) |
bfb07874 | 604 | |
02975b9a | 605 | AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix "calc" %verbose %yacc]) |
bfb07874 | 606 | |
f50adbbd | 607 | AT_CHECK_CALC_GLR([%debug]) |
02975b9a | 608 | AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) |
21964f43 | 609 | |
d9df47b6 | 610 | AT_CHECK_CALC_GLR([%define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) |
2a8d363a | 611 | |
d9df47b6 | 612 | AT_CHECK_CALC_GLR([%define api.pure %error-verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |
7548fed2 AD |
613 | |
614 | ||
615 | # ----------------------------- # | |
616 | # Simple LALR1 C++ Calculator. # | |
617 | # ----------------------------- # | |
618 | ||
8f7e3cf9 | 619 | AT_BANNER([[Simple LALR(1) C++ Calculator.]]) |
7548fed2 | 620 | |
0e021770 PE |
621 | # First let's try using %skeleton |
622 | AT_CHECK_CALC([%skeleton "lalr1.cc" %defines %locations]) | |
623 | ||
7548fed2 AD |
624 | # AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS]) |
625 | # --------------------------------------- | |
626 | # Start a testing chunk which compiles `calc' grammar with | |
caf37a36 | 627 | # the C++ skeleton, and performs several tests over the parser. |
7548fed2 | 628 | m4_define([AT_CHECK_CALC_LALR1_CC], |
2ea7730c | 629 | [AT_CHECK_CALC([%language "C++" %defines] $@)]) |
8f7e3cf9 AD |
630 | |
631 | AT_CHECK_CALC_LALR1_CC([]) | |
2ea7730c AD |
632 | AT_CHECK_CALC_LALR1_CC([%locations]) |
633 | AT_CHECK_CALC_LALR1_CC([%locations %error-verbose %name-prefix "calc" %verbose %yacc]) | |
7548fed2 | 634 | |
2ea7730c | 635 | AT_CHECK_CALC_LALR1_CC([%locations %error-verbose %debug %name-prefix "calc" %verbose %yacc]) |
7548fed2 | 636 | |
2ea7730c | 637 | AT_CHECK_CALC_LALR1_CC([%locations %pure-parser %error-verbose %debug %name-prefix "calc" %verbose %yacc]) |
0b86fc41 | 638 | |
2ea7730c | 639 | AT_CHECK_CALC_LALR1_CC([%locations %pure-parser %error-verbose %debug %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |
7548fed2 | 640 | |
7548fed2 | 641 | |
7548fed2 | 642 | |
8f7e3cf9 AD |
643 | # --------------------------- # |
644 | # Simple GLR C++ Calculator. # | |
645 | # --------------------------- # | |
646 | ||
647 | AT_BANNER([[Simple GLR C++ Calculator.]]) | |
648 | ||
0e021770 PE |
649 | # Again, we try also using %skeleton. |
650 | AT_CHECK_CALC([%skeleton "glr.cc" %defines %locations]) | |
651 | ||
8f7e3cf9 AD |
652 | # AT_CHECK_CALC_GLR_CC([BISON-OPTIONS]) |
653 | # ------------------------------------- | |
654 | # Start a testing chunk which compiles `calc' grammar with | |
655 | # the GLR C++ skeleton, and performs several tests over the parser. | |
656 | m4_define([AT_CHECK_CALC_GLR_CC], | |
0e021770 | 657 | [AT_CHECK_CALC([%language "C++" %glr-parser %defines %locations] $@)]) |
8f7e3cf9 | 658 | |
fa7b79c0 | 659 | AT_CHECK_CALC_GLR_CC([]) |
02975b9a | 660 | AT_CHECK_CALC_GLR_CC([%error-verbose %name-prefix "calc" %verbose %yacc]) |
8f7e3cf9 | 661 | |
fa7b79c0 | 662 | AT_CHECK_CALC_GLR_CC([%debug]) |
02975b9a | 663 | AT_CHECK_CALC_GLR_CC([%error-verbose %debug %name-prefix "calc" %verbose %yacc]) |
7548fed2 | 664 | |
02975b9a | 665 | AT_CHECK_CALC_GLR_CC([%pure-parser %error-verbose %debug %name-prefix "calc" %verbose %yacc]) |
7548fed2 | 666 | |
02975b9a | 667 | AT_CHECK_CALC_GLR_CC([%pure-parser %error-verbose %debug %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |