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