]>
Commit | Line | Data |
---|---|---|
82c53be4 | 1 | # Simple calculator. -*- Autotest -*- |
419ab105 | 2 | |
7d6bad19 | 3 | # Copyright (C) 2000-2013 Free Software Foundation, Inc. |
bfb07874 | 4 | |
f16b0819 | 5 | # This program is free software: you can redistribute it and/or modify |
342b8b6e | 6 | # it under the terms of the GNU General Public License as published by |
f16b0819 PE |
7 | # the Free Software Foundation, either version 3 of the License, or |
8 | # (at your option) any later version. | |
9 | # | |
342b8b6e AD |
10 | # This program is distributed in the hope that it will be useful, |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
f16b0819 | 14 | # |
342b8b6e | 15 | # You should have received a copy of the GNU General Public License |
f16b0819 | 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
bfb07874 | 17 | |
bfb07874 AD |
18 | ## ---------------------------------------------------- ## |
19 | ## Compile the grammar described in the documentation. ## | |
20 | ## ---------------------------------------------------- ## | |
21 | ||
22 | ||
23 | # ------------------------- # | |
24 | # Helping Autotest macros. # | |
25 | # ------------------------- # | |
26 | ||
27 | ||
2a8d363a AD |
28 | # _AT_DATA_CALC_Y($1, $2, $3, [BISON-DIRECTIVES]) |
29 | # ----------------------------------------------- | |
6c88b51e JD |
30 | # Produce `calc.y' and, if %defines was specified, `calc-lex.c' or |
31 | # `calc-lex.cc'. | |
32 | # | |
33 | # Don't call this macro directly, because it contains some occurrences | |
34 | # of `$1' etc. which will be interpreted by m4. So you should call it | |
35 | # with $1, $2, and $3 as arguments, which is what AT_DATA_CALC_Y does. | |
c8c220d1 AD |
36 | # |
37 | # When %defines is not passed, generate a single self-contained file. | |
38 | # Otherwise, generate three: calc.y with the parser, calc-lex.c with | |
39 | # the scanner, and calc-main.c with "main()". This is in order to | |
40 | # stress the use of the generated parser header. To avoid code | |
41 | # duplication, AT_CALC_LEX and AT_CALC_MAIN contain the body of these | |
42 | # two later files. | |
342b8b6e AD |
43 | m4_define([_AT_DATA_CALC_Y], |
44 | [m4_if([$1$2$3], $[1]$[2]$[3], [], | |
45 | [m4_fatal([$0: Invalid arguments: $@])])dnl | |
c8c220d1 AD |
46 | |
47 | m4_pushdef([AT_CALC_MAIN], | |
77519a7d | 48 | [#include <assert.h> |
50977317 | 49 | #include <unistd.h> |
c8c220d1 AD |
50 | |
51 | AT_SKEL_CC_IF([[ | |
52 | /* A C++ ]AT_NAME_PREFIX[parse that simulates the C signature. */ | |
53 | int | |
54 | ]AT_NAME_PREFIX[parse (]AT_PARAM_IF([semantic_value *result, int *count]))[ | |
55 | { | |
56 | ]AT_NAME_PREFIX[::parser parser]AT_PARAM_IF([ (result, count)])[; | |
ad60e80f | 57 | #if ]AT_API_PREFIX[DEBUG |
c8c220d1 AD |
58 | parser.set_debug_level (1); |
59 | #endif | |
60 | return parser.parse (); | |
61 | } | |
62 | ]])[ | |
63 | ||
64 | semantic_value global_result = 0; | |
65 | int global_count = 0; | |
66 | ||
67 | /* A C main function. */ | |
68 | int | |
69 | main (int argc, const char **argv) | |
70 | { | |
71 | semantic_value result = 0; | |
72 | int count = 0; | |
73 | int status; | |
74 | ||
312d0d65 AD |
75 | /* This used to be alarm (10), but that isn't enough time for a July |
76 | 1995 vintage DEC Alphastation 200 4/100 system, according to | |
77 | Nelson H. F. Beebe. 100 seconds was enough for regular users, | |
78 | but the Hydra build farm, which is heavily loaded needs more. */ | |
79 | ||
80 | alarm (200); | |
c8c220d1 AD |
81 | |
82 | if (argc == 2) | |
83 | input = fopen (argv[1], "r"); | |
84 | else | |
85 | input = stdin; | |
86 | ||
87 | if (!input) | |
88 | { | |
89 | perror (argv[1]); | |
90 | return 3; | |
91 | } | |
92 | ||
312d0d65 | 93 | ]AT_SKEL_CC_IF([], [AT_DEBUG_IF([ ]AT_NAME_PREFIX[debug = 1;])])[ |
c8c220d1 AD |
94 | status = ]AT_NAME_PREFIX[parse (]AT_PARAM_IF([[&result, &count]])[); |
95 | if (fclose (input)) | |
96 | perror ("fclose"); | |
77519a7d AD |
97 | assert (global_result == result); |
98 | assert (global_count == count); | |
c8c220d1 AD |
99 | return status; |
100 | } | |
101 | ]]) | |
102 | ||
103 | ||
6c88b51e JD |
104 | m4_pushdef([AT_CALC_LEX], |
105 | [[#include <ctype.h> | |
106 | ||
230a3db4 AD |
107 | ]AT_YYLEX_DECLARE_EXTERN[ |
108 | static int get_char (]AT_YYLEX_FORMALS[); | |
109 | static void unget_char (]AT_YYLEX_PRE_FORMALS[ int c); | |
6c88b51e JD |
110 | |
111 | ]AT_LOCATION_IF([ | |
ad60e80f | 112 | static AT_YYLTYPE last_yylloc; |
6c88b51e JD |
113 | ])[ |
114 | static int | |
230a3db4 | 115 | get_char (]AT_YYLEX_FORMALS[) |
6c88b51e JD |
116 | { |
117 | int res = getc (input); | |
118 | ]AT_USE_LEX_ARGS[; | |
119 | ]AT_LOCATION_IF([ | |
120 | last_yylloc = AT_LOC; | |
121 | if (res == '\n') | |
122 | { | |
4f756f88 AD |
123 | AT_LOC_LAST_LINE++; |
124 | AT_LOC_LAST_COLUMN = 1; | |
6c88b51e JD |
125 | } |
126 | else | |
4f756f88 | 127 | AT_LOC_LAST_COLUMN++; |
6c88b51e JD |
128 | ])[ |
129 | return res; | |
130 | } | |
131 | ||
132 | static void | |
230a3db4 | 133 | unget_char (]AT_YYLEX_PRE_FORMALS[ int c) |
6c88b51e JD |
134 | { |
135 | ]AT_USE_LEX_ARGS[; | |
136 | ]AT_LOCATION_IF([ | |
137 | /* Wrong when C == `\n'. */ | |
138 | AT_LOC = last_yylloc; | |
139 | ])[ | |
140 | ungetc (c, input); | |
141 | } | |
142 | ||
143 | static int | |
230a3db4 | 144 | read_signed_integer (]AT_YYLEX_FORMALS[) |
6c88b51e | 145 | { |
230a3db4 | 146 | int c = get_char (]AT_YYLEX_ARGS[); |
6c88b51e JD |
147 | int sign = 1; |
148 | int n = 0; | |
149 | ||
150 | ]AT_USE_LEX_ARGS[; | |
151 | if (c == '-') | |
152 | { | |
230a3db4 | 153 | c = get_char (]AT_YYLEX_ARGS[); |
6c88b51e JD |
154 | sign = -1; |
155 | } | |
156 | ||
157 | while (isdigit (c)) | |
158 | { | |
159 | n = 10 * n + (c - '0'); | |
230a3db4 | 160 | c = get_char (]AT_YYLEX_ARGS[); |
6c88b51e JD |
161 | } |
162 | ||
230a3db4 | 163 | unget_char (]AT_YYLEX_PRE_ARGS[ c); |
6c88b51e JD |
164 | |
165 | return sign * n; | |
166 | } | |
167 | ||
168 | ||
169 | /*---------------------------------------------------------------. | |
170 | | Lexical analyzer returns an integer on the stack and the token | | |
171 | | NUM, or the ASCII character read if not a number. Skips all | | |
172 | | blanks and tabs, returns 0 for EOF. | | |
173 | `---------------------------------------------------------------*/ | |
174 | ||
230a3db4 | 175 | ]AT_YYLEX_PROTOTYPE[ |
6c88b51e | 176 | { |
6c88b51e | 177 | int c; |
67f1a2c2 AD |
178 | /* Skip current token, then white spaces. */ |
179 | do | |
6c88b51e JD |
180 | { |
181 | ]AT_LOCATION_IF( | |
4f756f88 AD |
182 | [ AT_LOC_FIRST_COLUMN = AT_LOC_LAST_COLUMN; |
183 | AT_LOC_FIRST_LINE = AT_LOC_LAST_LINE; | |
6c88b51e JD |
184 | ])[ |
185 | } | |
230a3db4 | 186 | while ((c = get_char (]AT_YYLEX_ARGS[)) == ' ' || c == '\t'); |
6c88b51e JD |
187 | |
188 | /* process numbers */ | |
189 | if (c == '.' || isdigit (c)) | |
190 | { | |
230a3db4 AD |
191 | unget_char (]AT_YYLEX_PRE_ARGS[ c); |
192 | ]AT_VAL[.ival = read_signed_integer (]AT_YYLEX_ARGS[); | |
417e31d2 | 193 | return ]AT_TOKEN_PREFIX[NUM; |
6c88b51e JD |
194 | } |
195 | ||
196 | /* Return end-of-file. */ | |
197 | if (c == EOF) | |
417e31d2 | 198 | return ]AT_TOKEN_PREFIX[CALC_EOF; |
6c88b51e JD |
199 | |
200 | /* Return single chars. */ | |
201 | return c; | |
202 | } | |
203 | ]]) | |
204 | ||
9501dc6e | 205 | AT_DATA_GRAMMAR([calc.y], |
bfb07874 | 206 | [[/* Infix notation calculator--calc */ |
fb9712a9 | 207 | ]$4 |
8f7e3cf9 | 208 | AT_SKEL_CC_IF( |
16dc6a9e | 209 | [%define global_tokens_and_yystype])[ |
c499231d AD |
210 | %code requires |
211 | { | |
49976d5c | 212 | ]AT_LOCATION_TYPE_IF([[ |
24bb8c8c AD |
213 | # include <iostream> |
214 | struct Point | |
215 | { | |
216 | int l; | |
217 | int c; | |
218 | }; | |
219 | ||
220 | struct Span | |
221 | { | |
49976d5c AD |
222 | Point first; |
223 | Point last; | |
24bb8c8c | 224 | }; |
49976d5c AD |
225 | |
226 | # define YYLLOC_DEFAULT(Current, Rhs, N) \ | |
bb9191dd AD |
227 | do \ |
228 | if (N) \ | |
229 | { \ | |
230 | (Current).first = YYRHSLOC (Rhs, 1).first; \ | |
231 | (Current).last = YYRHSLOC (Rhs, N).last; \ | |
232 | } \ | |
233 | else \ | |
234 | { \ | |
235 | (Current).first = (Current).last = YYRHSLOC (Rhs, 0).last; \ | |
236 | } \ | |
237 | while (false) | |
49976d5c AD |
238 | |
239 | ]])[ | |
24bb8c8c AD |
240 | /* Exercise pre-prologue dependency to %union. */ |
241 | typedef int semantic_value; | |
6c88b51e JD |
242 | } |
243 | ||
244 | /* Exercise %union. */ | |
245 | %union | |
246 | { | |
247 | semantic_value ival; | |
248 | }; | |
3154ff8e AD |
249 | %printer { ]AT_SKEL_CC_IF([[yyoutput << $$]], |
250 | [[fprintf (yyoutput, "%d", $$)]])[; } <ival>; | |
6c88b51e | 251 | |
c499231d AD |
252 | %code provides |
253 | { | |
ad60e80f AD |
254 | #include <stdio.h> |
255 | /* The input. */ | |
256 | extern FILE *input; | |
257 | extern semantic_value global_result; | |
258 | extern int global_count; | |
6c88b51e | 259 | } |
2ce10144 | 260 | |
c499231d AD |
261 | %code |
262 | { | |
459a57a9 | 263 | #include <assert.h> |
6e26ca8c | 264 | #include <string.h> |
affac613 | 265 | #define USE(Var) |
bfb07874 | 266 | |
6c88b51e | 267 | FILE *input; |
c19988b7 | 268 | static int power (int base, int exponent); |
49976d5c | 269 | |
3472de82 | 270 | ]AT_YYERROR_DECLARE[ |
230a3db4 | 271 | ]AT_YYLEX_DECLARE_EXTERN[ |
6c88b51e | 272 | } |
c19988b7 | 273 | |
3154ff8e AD |
274 | ]AT_SKEL_CC_IF([AT_LOCATION_TYPE_IF([[ |
275 | %initial-action | |
276 | { | |
277 | @$.first.l = @$.first.c = 1; | |
278 | @$.last = @$.first; | |
279 | }]])])[ | |
8f7e3cf9 | 280 | |
6b7e85b9 | 281 | /* Bison Declarations */ |
c19988b7 | 282 | %token CALC_EOF 0 "end of input" |
213e640e AD |
283 | %token <ival> NUM "number" |
284 | %type <ival> exp | |
bfb07874 | 285 | |
e9690142 | 286 | %nonassoc '=' /* comparison */ |
bfb07874 AD |
287 | %left '-' '+' |
288 | %left '*' '/' | |
d78f0ac9 AD |
289 | %precedence NEG /* negation--unary minus */ |
290 | %right '^' /* exponentiation */ | |
bfb07874 AD |
291 | |
292 | /* Grammar follows */ | |
293 | %% | |
294 | input: | |
b7c49edf | 295 | line |
2a8d363a | 296 | | input line { ]AT_PARAM_IF([++*count; ++global_count;])[ } |
bfb07874 AD |
297 | ; |
298 | ||
299 | line: | |
c19988b7 | 300 | '\n' |
affac613 | 301 | | exp '\n' { ]AT_PARAM_IF([*result = global_result = $1], [USE ($1)])[; } |
bfb07874 AD |
302 | ; |
303 | ||
304 | exp: | |
305 | NUM { $$ = $1; } | |
306 | | exp '=' exp | |
307 | { | |
2a8d363a AD |
308 | if ($1 != $3) |
309 | fprintf (stderr, "calc: error: %d != %d\n", $1, $3); | |
310 | $$ = $1; | |
bfb07874 AD |
311 | } |
312 | | exp '+' exp { $$ = $1 + $3; } | |
313 | | exp '-' exp { $$ = $1 - $3; } | |
314 | | exp '*' exp { $$ = $1 * $3; } | |
315 | | exp '/' exp { $$ = $1 / $3; } | |
316 | | '-' exp %prec NEG { $$ = -$2; } | |
317 | | exp '^' exp { $$ = power ($1, $3); } | |
318 | | '(' exp ')' { $$ = $2; } | |
65015668 | 319 | | '(' error ')' { $$ = 1111; yyerrok; } |
8f3596a6 AD |
320 | | '!' { $$ = 0; YYERROR; } |
321 | | '-' error { $$ = 0; YYERROR; } | |
bfb07874 AD |
322 | ; |
323 | %% | |
bfb07874 | 324 | |
3d8082ad AD |
325 | static int |
326 | power (int base, int exponent) | |
327 | { | |
328 | int res = 1; | |
329 | assert (0 <= exponent); | |
330 | for (/* Niente */; exponent; --exponent) | |
331 | res *= base; | |
332 | return res; | |
333 | } | |
334 | ||
8f7e3cf9 | 335 | ]AT_SKEL_CC_IF( |
24bb8c8c AD |
336 | [AT_LOCATION_TYPE_IF([[ |
337 | std::ostream& | |
338 | operator<< (std::ostream& o, const Span& s) | |
339 | { | |
49976d5c AD |
340 | o << s.first.l << '.' << s.first.c; |
341 | if (s.first.l != s.last.l) | |
342 | o << '-' << s.last.l << '.' << s.last.c - 1; | |
343 | else if (s.first.c != s.last.c - 1) | |
344 | o << '-' << s.last.c - 1; | |
24bb8c8c AD |
345 | return o; |
346 | } | |
3472de82 AD |
347 | ]])])[ |
348 | ]AT_YYERROR_DEFINE[ | |
c8c220d1 AD |
349 | ]AT_DEFINES_IF([], |
350 | [AT_CALC_LEX | |
dcd5344d | 351 | AT_CALC_MAIN])]) |
c8c220d1 | 352 | |
6c88b51e JD |
353 | AT_DEFINES_IF([AT_DATA_SOURCE([[calc-lex.c]AT_SKEL_CC_IF([[c]])], |
354 | [[#include "calc.h]AT_SKEL_CC_IF([[h]])[" | |
355 | ||
c8c220d1 AD |
356 | ]AT_CALC_LEX]) |
357 | AT_DATA_SOURCE([[calc-main.c]AT_SKEL_CC_IF([[c]])], | |
358 | [[#include "calc.h]AT_SKEL_CC_IF([[h]])[" | |
359 | ||
360 | ]AT_CALC_MAIN]) | |
361 | ]) | |
362 | m4_popdef([AT_CALC_MAIN]) | |
6c88b51e | 363 | m4_popdef([AT_CALC_LEX]) |
bfb07874 AD |
364 | ])# _AT_DATA_CALC_Y |
365 | ||
366 | ||
367 | # AT_DATA_CALC_Y([BISON-OPTIONS]) | |
368 | # ------------------------------- | |
6c88b51e JD |
369 | # Produce `calc.y' and, if %defines was specified, `calc-lex.c' or |
370 | # `calc-lex.cc'. | |
342b8b6e | 371 | m4_define([AT_DATA_CALC_Y], |
9e32add8 | 372 | [_AT_DATA_CALC_Y($[1], $[2], $[3], [$1]) |
f50adbbd | 373 | ]) |
bfb07874 AD |
374 | |
375 | ||
376 | ||
d9963c85 PE |
377 | # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES]) |
378 | # -------------------------------------------------------- | |
bfb07874 | 379 | # Run `calc' on INPUT and expect no STDOUT nor STDERR. |
342b8b6e | 380 | # |
f50adbbd | 381 | # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then |
d9963c85 | 382 | # |
f50adbbd | 383 | # NUM-STDERR-LINES is the number of expected lines on stderr. |
d9963c85 | 384 | # Currently this is ignored, though, since the output format is fluctuating. |
f50adbbd AD |
385 | # |
386 | # We don't count GLR's traces yet, since its traces are somewhat | |
387 | # different from LALR's. | |
342b8b6e AD |
388 | m4_define([_AT_CHECK_CALC], |
389 | [AT_DATA([[input]], | |
390 | [[$2 | |
391 | ]]) | |
f50adbbd | 392 | AT_PARSER_CHECK([./calc input], 0, [], [stderr]) |
342b8b6e AD |
393 | ]) |
394 | ||
395 | ||
b0f98b10 | 396 | # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT, |
d9963c85 | 397 | # [NUM-STDERR-LINES], |
2a8d363a | 398 | # [VERBOSE-AND-LOCATED-ERROR-MESSAGE]) |
b0f98b10 | 399 | # --------------------------------------------------------- |
6e649e65 | 400 | # Run `calc' on INPUT, and expect a `syntax error' message. |
342b8b6e | 401 | # |
b7c49edf AD |
402 | # If INPUT starts with a slash, it is used as absolute input file name, |
403 | # otherwise as contents. | |
404 | # | |
d9963c85 PE |
405 | # NUM-STDERR-LINES is the number of expected lines on stderr. |
406 | # Currently this is ignored, though, since the output format is fluctuating. | |
407 | # | |
9e32add8 | 408 | # If BISON-OPTIONS contains `%location', then make sure the ERROR-LOCATION |
342b8b6e AD |
409 | # is correctly output on stderr. |
410 | # | |
cf499cff | 411 | # If BISON-OPTIONS contains `%define parse.error verbose', then make sure the |
6e649e65 | 412 | # IF-YYERROR-VERBOSE message is properly output after `syntax error, ' |
342b8b6e AD |
413 | # on STDERR. |
414 | # | |
f50adbbd AD |
415 | # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES |
416 | # is the number of expected lines on stderr. | |
342b8b6e | 417 | m4_define([_AT_CHECK_CALC_ERROR], |
b0f98b10 AD |
418 | [m4_bmatch([$3], [^/], |
419 | [AT_PARSER_CHECK([./calc $3], $2, [], [stderr])], | |
b7c49edf | 420 | [AT_DATA([[input]], |
b0f98b10 | 421 | [[$3 |
342b8b6e | 422 | ]]) |
b0f98b10 | 423 | AT_PARSER_CHECK([./calc input], $2, [], [stderr])]) |
342b8b6e | 424 | |
51dec47b AD |
425 | # Normalize the observed and expected error messages, depending upon the |
426 | # options. | |
427 | # 1. Remove the traces from observed. | |
9280d3ef AD |
428 | sed '/^Starting/d |
429 | /^Entering/d | |
f50adbbd | 430 | /^Stack/d |
9280d3ef AD |
431 | /^Reading/d |
432 | /^Reducing/d | |
31c10e38 | 433 | /^Return/d |
9280d3ef AD |
434 | /^Shifting/d |
435 | /^state/d | |
dd5f2af2 | 436 | /^Cleanup:/d |
9280d3ef AD |
437 | /^Error:/d |
438 | /^Next/d | |
31c10e38 | 439 | /^Now/d |
9280d3ef | 440 | /^Discarding/d |
62b08cfc | 441 | / \$[[0-9$]]* = /d |
9280d3ef | 442 | /^yydestructor:/d' stderr >at-stderr |
342b8b6e | 443 | mv at-stderr stderr |
51dec47b AD |
444 | # 2. Create the reference error message. |
445 | AT_DATA([[expout]], | |
b0f98b10 | 446 | [$5 |
342b8b6e | 447 | ]) |
51dec47b | 448 | # 3. If locations are not used, remove them. |
2a8d363a | 449 | AT_YYERROR_SEES_LOC_IF([], |
51dec47b AD |
450 | [[sed 's/^[-0-9.]*: //' expout >at-expout |
451 | mv at-expout expout]]) | |
452 | # 4. If error-verbose is not used, strip the`, unexpected....' part. | |
cf499cff | 453 | m4_bmatch([$1], [%define parse.error verbose], [], |
6e649e65 | 454 | [[sed 's/syntax error, .*$/syntax error/' expout >at-expout |
51dec47b AD |
455 | mv at-expout expout]]) |
456 | # 5. Check | |
457 | AT_CHECK([cat stderr], 0, [expout]) | |
342b8b6e | 458 | ]) |
bfb07874 AD |
459 | |
460 | ||
9527072c AD |
461 | # AT_CHECK_SPACES([FILES]) |
462 | # ------------------------ | |
dcd5344d AD |
463 | # Make sure we did not introduce bad spaces. Checked here because all |
464 | # the skeletons are (or should be) exercized here. | |
465 | m4_define([AT_CHECK_SPACES], | |
ff020c30 | 466 | [AT_CHECK([$PERL -ne ' |
a727e712 | 467 | chomp; |
9527072c | 468 | print "$ARGV:$.: {$_}\n" |
a727e712 AD |
469 | if (# No starting/ending empty lines. |
470 | (eof || $. == 1) && /^\s*$/ | |
23d13411 AD |
471 | # No trailing space. |
472 | || /\s$/ | |
473 | # No tabs. | |
474 | || /\t/ | |
a727e712 AD |
475 | )' $1 |
476 | ])dnl | |
dcd5344d AD |
477 | ]) |
478 | ||
479 | ||
eb46d61f AD |
480 | # AT_CHECK_CALC([BISON-OPTIONS]) |
481 | # ------------------------------ | |
bfb07874 AD |
482 | # Start a testing chunk which compiles `calc' grammar with |
483 | # BISON-OPTIONS, and performs several tests over the parser. | |
342b8b6e | 484 | m4_define([AT_CHECK_CALC], |
eb46d61f | 485 | [m4_ifval([$2], [m4_fatal([$0: expected a single argument])]) |
bfb07874 | 486 | |
eb46d61f AD |
487 | # We use integers to avoid dependencies upon the precision of doubles. |
488 | AT_SETUP([Calculator $1]) | |
907e3bc8 | 489 | |
67a25fed | 490 | AT_BISON_OPTION_PUSHDEFS([$1]) |
2a8d363a | 491 | |
bfb07874 | 492 | AT_DATA_CALC_Y([$1]) |
c8c220d1 | 493 | AT_FULL_COMPILE([calc], AT_DEFINES_IF([[lex], [main]])) |
9527072c AD |
494 | AT_CHECK_SPACES(m4_join([ ], |
495 | [calc.AT_SKEL_CC_IF([cc], [c])], | |
496 | [AT_DEFINES_IF([calc.AT_SKEL_CC_IF([hh], [h])])])) | |
bfb07874 AD |
497 | |
498 | # Test the priorities. | |
499 | _AT_CHECK_CALC([$1], | |
500 | [1 + 2 * 3 = 7 | |
501 | 1 + 2 * -3 = -5 | |
502 | ||
503 | -1^2 = -1 | |
504 | (-1)^2 = 1 | |
505 | ||
506 | ---1 = -1 | |
507 | ||
508 | 1 - 2 - 3 = -4 | |
509 | 1 - (2 - 3) = 2 | |
510 | ||
511 | 2^2^3 = 256 | |
e7cb57c0 | 512 | (2^2)^3 = 64], |
d1ff7a7c | 513 | [842]) |
bfb07874 | 514 | |
6e649e65 | 515 | # Some syntax errors. |
3154ff8e | 516 | _AT_CHECK_CALC_ERROR([$1], [1], [1 2], [15], |
cd48d21d | 517 | [1.3: syntax error, unexpected number]) |
d1ff7a7c | 518 | _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [20], |
cd48d21d | 519 | [1.3: syntax error, unexpected '/', expecting number or '-' or '(' or '!']) |
e757bb10 | 520 | _AT_CHECK_CALC_ERROR([$1], [1], [error], [5], |
cd48d21d | 521 | [1.1: syntax error, unexpected $undefined]) |
d1ff7a7c | 522 | _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [30], |
cd48d21d | 523 | [1.7: syntax error, unexpected '=']) |
b0f98b10 | 524 | _AT_CHECK_CALC_ERROR([$1], [1], |
bfb07874 AD |
525 | [ |
526 | +1], | |
d1ff7a7c | 527 | [20], |
cd48d21d | 528 | [2.1: syntax error, unexpected '+']) |
b7c49edf | 529 | # Exercise error messages with EOF: work on an empty file. |
cea1469d | 530 | _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4], |
cd48d21d | 531 | [1.1: syntax error, unexpected end of input]) |
51dec47b AD |
532 | |
533 | # Exercise the error token: without it, we die at the first error, | |
0b86fc41 AD |
534 | # hence be sure to |
535 | # | |
536 | # - have several errors which exercise different shift/discardings | |
537 | # - (): nothing to pop, nothing to discard | |
538 | # - (1 + 1 + 1 +): a lot to pop, nothing to discard | |
539 | # - (* * *): nothing to pop, a lot to discard | |
540 | # - (1 + 2 * *): some to pop and discard | |
541 | # | |
542 | # - test the action associated to `error' | |
543 | # | |
742e4900 JD |
544 | # - check the lookahead that triggers an error is not discarded |
545 | # when we enter error recovery. Below, the lookahead causing the | |
0b86fc41 AD |
546 | # first error is ")", which is needed to recover from the error and |
547 | # produce the "0" that triggers the "0 != 1" error. | |
548 | # | |
549 | _AT_CHECK_CALC_ERROR([$1], [0], | |
550 | [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1], | |
d1ff7a7c | 551 | [250], |
cd48d21d AD |
552 | [1.2: syntax error, unexpected ')', expecting number or '-' or '(' or '!' |
553 | 1.18: syntax error, unexpected ')', expecting number or '-' or '(' or '!' | |
554 | 1.23: syntax error, unexpected '*', expecting number or '-' or '(' or '!' | |
555 | 1.41: syntax error, unexpected '*', expecting number or '-' or '(' or '!' | |
0b86fc41 AD |
556 | calc: error: 4444 != 1]) |
557 | ||
558 | # The same, but this time exercising explicitly triggered syntax errors. | |
742e4900 | 559 | # POSIX says the lookahead causing the error should not be discarded. |
3154ff8e | 560 | _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (1 2) = 1], [102], |
cd48d21d | 561 | [1.10: syntax error, unexpected number |
0b86fc41 | 562 | calc: error: 2222 != 1]) |
3154ff8e | 563 | _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (1 2) = 1], [113], |
cd48d21d AD |
564 | [1.4: syntax error, unexpected '*', expecting number or '-' or '(' or '!' |
565 | 1.12: syntax error, unexpected number | |
82c53be4 | 566 | calc: error: 2222 != 1]) |
65015668 AD |
567 | |
568 | # Check that yyerrok works properly: second error is not reported, | |
569 | # third and fourth are. Parse status is succesfull. | |
570 | _AT_CHECK_CALC_ERROR([$1], [0], [(* *) + (*) + (*)], [113], | |
571 | [1.2: syntax error, unexpected '*', expecting number or '-' or '(' or '!' | |
572 | 1.10: syntax error, unexpected '*', expecting number or '-' or '(' or '!' | |
573 | 1.16: syntax error, unexpected '*', expecting number or '-' or '(' or '!']) | |
574 | ||
67a25fed | 575 | AT_BISON_OPTION_POPDEFS |
2a8d363a | 576 | |
d803322e | 577 | AT_CLEANUP |
bfb07874 AD |
578 | ])# AT_CHECK_CALC |
579 | ||
580 | ||
581 | ||
582 | ||
f50adbbd AD |
583 | # ------------------------ # |
584 | # Simple LALR Calculator. # | |
585 | # ------------------------ # | |
586 | ||
8f7e3cf9 | 587 | AT_BANNER([[Simple LALR(1) Calculator.]]) |
f50adbbd AD |
588 | |
589 | # AT_CHECK_CALC_LALR([BISON-OPTIONS]) | |
590 | # ----------------------------------- | |
591 | # Start a testing chunk which compiles `calc' grammar with | |
592 | # BISON-OPTIONS, and performs several tests over the parser. | |
593 | m4_define([AT_CHECK_CALC_LALR], | |
594 | [AT_CHECK_CALC($@)]) | |
595 | ||
596 | AT_CHECK_CALC_LALR() | |
597 | ||
9e32add8 | 598 | AT_CHECK_CALC_LALR([%defines]) |
ae7453f2 | 599 | AT_CHECK_CALC_LALR([%locations]) |
24bb8c8c | 600 | |
2062d72d | 601 | AT_CHECK_CALC_LALR([%name-prefix "calc"]) |
9e32add8 AD |
602 | AT_CHECK_CALC_LALR([%verbose]) |
603 | AT_CHECK_CALC_LALR([%yacc]) | |
cf499cff | 604 | AT_CHECK_CALC_LALR([%define parse.error verbose]) |
f50adbbd | 605 | |
6428a8a4 TR |
606 | AT_CHECK_CALC_LALR([%define api.pure full %locations]) |
607 | AT_CHECK_CALC_LALR([%define api.push-pull both %define api.pure full %locations]) | |
cf499cff | 608 | AT_CHECK_CALC_LALR([%define parse.error verbose %locations]) |
f50adbbd | 609 | |
60aa04a2 | 610 | AT_CHECK_CALC_LALR([%define parse.error verbose %locations %defines %define api.prefix "calc" %verbose %yacc]) |
2a6b66c5 | 611 | AT_CHECK_CALC_LALR([%define parse.error verbose %locations %defines %name-prefix "calc" %define api.token.prefix "TOK_" %verbose %yacc]) |
f50adbbd AD |
612 | |
613 | AT_CHECK_CALC_LALR([%debug]) | |
cf499cff | 614 | AT_CHECK_CALC_LALR([%define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) |
60aa04a2 | 615 | AT_CHECK_CALC_LALR([%define parse.error verbose %debug %locations %defines %define api.prefix "calc" %verbose %yacc]) |
c19988b7 | 616 | |
5807bb91 AD |
617 | AT_CHECK_CALC_LALR([%define api.pure full %define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) |
618 | AT_CHECK_CALC_LALR([%define api.push-pull both %define api.pure full %define parse.error verbose %debug %locations %defines %define api.prefix "calc" %verbose %yacc]) | |
2a8d363a | 619 | |
e42906f7 | 620 | AT_CHECK_CALC_LALR([%define api.pure %define parse.error verbose %debug %locations %defines %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |
f50adbbd AD |
621 | |
622 | ||
623 | # ----------------------- # | |
624 | # Simple GLR Calculator. # | |
625 | # ----------------------- # | |
626 | ||
627 | AT_BANNER([[Simple GLR Calculator.]]) | |
628 | ||
629 | # AT_CHECK_CALC_GLR([BISON-OPTIONS]) | |
630 | # ---------------------------------- | |
631 | # Start a testing chunk which compiles `calc' grammar with | |
632 | # BISON-OPTIONS and %glr-parser, and performs several tests over the parser. | |
633 | m4_define([AT_CHECK_CALC_GLR], | |
ae7453f2 | 634 | [AT_CHECK_CALC([%glr-parser] $@)]) |
f50adbbd | 635 | |
bfb07874 | 636 | |
f50adbbd | 637 | AT_CHECK_CALC_GLR() |
bfb07874 | 638 | |
9e32add8 | 639 | AT_CHECK_CALC_GLR([%defines]) |
ae7453f2 | 640 | AT_CHECK_CALC_GLR([%locations]) |
02975b9a | 641 | AT_CHECK_CALC_GLR([%name-prefix "calc"]) |
ad60e80f | 642 | AT_CHECK_CALC_GLR([%define api.prefix "calc"]) |
9e32add8 AD |
643 | AT_CHECK_CALC_GLR([%verbose]) |
644 | AT_CHECK_CALC_GLR([%yacc]) | |
cf499cff | 645 | AT_CHECK_CALC_GLR([%define parse.error verbose]) |
bfb07874 | 646 | |
d9df47b6 | 647 | AT_CHECK_CALC_GLR([%define api.pure %locations]) |
cf499cff | 648 | AT_CHECK_CALC_GLR([%define parse.error verbose %locations]) |
bfb07874 | 649 | |
cf499cff | 650 | AT_CHECK_CALC_GLR([%define parse.error verbose %locations %defines %name-prefix "calc" %verbose %yacc]) |
bfb07874 | 651 | |
f50adbbd | 652 | AT_CHECK_CALC_GLR([%debug]) |
cf499cff | 653 | AT_CHECK_CALC_GLR([%define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) |
2a6b66c5 | 654 | AT_CHECK_CALC_GLR([%define parse.error verbose %debug %locations %defines %define api.prefix "calc" %define api.token.prefix "TOK_" %verbose %yacc]) |
21964f43 | 655 | |
cf499cff | 656 | AT_CHECK_CALC_GLR([%define api.pure %define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) |
2a8d363a | 657 | |
e42906f7 AD |
658 | AT_CHECK_CALC_GLR([%define api.pure %define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |
659 | AT_CHECK_CALC_GLR([%define api.pure %define parse.error verbose %debug %locations %defines %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) | |
7548fed2 AD |
660 | |
661 | ||
662 | # ----------------------------- # | |
663 | # Simple LALR1 C++ Calculator. # | |
664 | # ----------------------------- # | |
665 | ||
8f7e3cf9 | 666 | AT_BANNER([[Simple LALR(1) C++ Calculator.]]) |
7548fed2 | 667 | |
0e021770 | 668 | # First let's try using %skeleton |
34904c57 | 669 | AT_CHECK_CALC([%skeleton "lalr1.cc" %defines]) |
0e021770 | 670 | |
7548fed2 AD |
671 | # AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS]) |
672 | # --------------------------------------- | |
673 | # Start a testing chunk which compiles `calc' grammar with | |
caf37a36 | 674 | # the C++ skeleton, and performs several tests over the parser. |
7548fed2 | 675 | m4_define([AT_CHECK_CALC_LALR1_CC], |
85f0b29e | 676 | [AT_CHECK_CALC([%language "C++"] $@)]) |
8f7e3cf9 AD |
677 | |
678 | AT_CHECK_CALC_LALR1_CC([]) | |
2ea7730c | 679 | AT_CHECK_CALC_LALR1_CC([%locations]) |
f6b561d9 | 680 | AT_CHECK_CALC_LALR1_CC([%locations %define api.location.type Span]) |
85f0b29e | 681 | AT_CHECK_CALC_LALR1_CC([%defines %locations %define parse.error verbose %name-prefix "calc" %verbose %yacc]) |
7548fed2 | 682 | |
60aa04a2 | 683 | AT_CHECK_CALC_LALR1_CC([%locations %define parse.error verbose %define api.prefix "calc" %verbose %yacc]) |
cf499cff | 684 | AT_CHECK_CALC_LALR1_CC([%locations %define parse.error verbose %debug %name-prefix "calc" %verbose %yacc]) |
7548fed2 | 685 | |
60aa04a2 | 686 | AT_CHECK_CALC_LALR1_CC([%locations %pure-parser %define parse.error verbose %debug %define api.prefix "calc" %verbose %yacc]) |
2a6b66c5 | 687 | AT_CHECK_CALC_LALR1_CC([%locations %pure-parser %define parse.error verbose %debug %define api.prefix "calc" %define api.token.prefix "TOK_" %verbose %yacc]) |
0b86fc41 | 688 | |
e42906f7 | 689 | AT_CHECK_CALC_LALR1_CC([%defines %locations %pure-parser %define parse.error verbose %debug %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |
34904c57 | 690 | |
e42906f7 AD |
691 | AT_CHECK_CALC_LALR1_CC([%pure-parser %define parse.error verbose %debug %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |
692 | AT_CHECK_CALC_LALR1_CC([%defines %locations %pure-parser %define parse.error verbose %debug %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) | |
7548fed2 | 693 | |
7548fed2 | 694 | |
7548fed2 | 695 | |
8f7e3cf9 AD |
696 | # --------------------------- # |
697 | # Simple GLR C++ Calculator. # | |
698 | # --------------------------- # | |
699 | ||
700 | AT_BANNER([[Simple GLR C++ Calculator.]]) | |
701 | ||
0e021770 | 702 | # Again, we try also using %skeleton. |
e4c0985b | 703 | AT_CHECK_CALC([%skeleton "glr.cc"]) |
0e021770 | 704 | |
8f7e3cf9 AD |
705 | # AT_CHECK_CALC_GLR_CC([BISON-OPTIONS]) |
706 | # ------------------------------------- | |
707 | # Start a testing chunk which compiles `calc' grammar with | |
708 | # the GLR C++ skeleton, and performs several tests over the parser. | |
709 | m4_define([AT_CHECK_CALC_GLR_CC], | |
e4c0985b | 710 | [AT_CHECK_CALC([%language "C++" %glr-parser] $@)]) |
8f7e3cf9 | 711 | |
fa7b79c0 | 712 | AT_CHECK_CALC_GLR_CC([]) |
34904c57 | 713 | AT_CHECK_CALC_GLR_CC([%locations]) |
f6b561d9 | 714 | AT_CHECK_CALC_GLR_CC([%locations %define api.location.type Span]) |
e4c0985b | 715 | AT_CHECK_CALC_GLR_CC([%defines %define parse.error verbose %name-prefix "calc" %verbose %yacc]) |
60aa04a2 | 716 | AT_CHECK_CALC_GLR_CC([%define parse.error verbose %define api.prefix "calc" %verbose %yacc]) |
8f7e3cf9 | 717 | |
fa7b79c0 | 718 | AT_CHECK_CALC_GLR_CC([%debug]) |
cf499cff | 719 | AT_CHECK_CALC_GLR_CC([%define parse.error verbose %debug %name-prefix "calc" %verbose %yacc]) |
7548fed2 | 720 | |
cf499cff | 721 | AT_CHECK_CALC_GLR_CC([%pure-parser %define parse.error verbose %debug %name-prefix "calc" %verbose %yacc]) |
2a6b66c5 | 722 | AT_CHECK_CALC_GLR_CC([%pure-parser %define parse.error verbose %debug %name-prefix "calc" %define api.token.prefix "TOK_" %verbose %yacc]) |
7548fed2 | 723 | |
e42906f7 AD |
724 | AT_CHECK_CALC_GLR_CC([%locations %defines %pure-parser %define parse.error verbose %debug %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |
725 | AT_CHECK_CALC_GLR_CC([%locations %defines %pure-parser %define parse.error verbose %debug %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |