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