]>
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)]); |
a3cb6248 | 165 | parser.set_debug_level (!!YYDEBUG); |
7548fed2 AD |
166 | return parser.parse (); |
167 | } | |
168 | ], | |
169 | [static void | |
ebc3737e | 170 | yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ]) |
caf37a36 | 171 | AT_PARAM_IF([semantic_value *result, int *count, ]) |
7548fed2 AD |
172 | const char *s) |
173 | { | |
174 | AT_PARAM_IF([(void) result; (void) count;]) | |
175 | AT_YYERROR_SEES_LOC_IF([ | |
176 | fprintf (stderr, "%d.%d", | |
177 | AT_LOC.first_line, AT_LOC.first_column); | |
178 | if (AT_LOC.first_line != AT_LOC.last_line) | |
179 | fprintf (stderr, "-%d.%d", | |
180 | AT_LOC.last_line, AT_LOC.last_column - 1); | |
181 | else if (AT_LOC.first_column != AT_LOC.last_column - 1) | |
182 | fprintf (stderr, "-%d", | |
183 | AT_LOC.last_column - 1); | |
184 | fprintf (stderr, ": ");]) | |
185 | fprintf (stderr, "%s\n", s); | |
186 | }])[ | |
187 | ||
b7c49edf | 188 | |
2a8d363a | 189 | ]AT_LOCATION_IF([ |
b7c49edf | 190 | static YYLTYPE last_yylloc; |
2a8d363a | 191 | ])[ |
bfb07874 | 192 | static int |
a9739e7c | 193 | get_char (]AT_LEX_FORMALS[) |
bfb07874 | 194 | { |
a9739e7c | 195 | int res = getc (input); |
7548fed2 | 196 | ]AT_USE_LEX_ARGS[; |
2a8d363a | 197 | ]AT_LOCATION_IF([ |
7548fed2 | 198 | last_yylloc = AT_LOC; |
bfb07874 AD |
199 | if (res == '\n') |
200 | { | |
fb9712a9 AD |
201 | AT_LOC.last_line++; |
202 | AT_LOC.last_column = 0; | |
bfb07874 AD |
203 | } |
204 | else | |
fb9712a9 | 205 | AT_LOC.last_column++; |
2a8d363a | 206 | ])[ |
bfb07874 AD |
207 | return res; |
208 | } | |
209 | ||
210 | ||
211 | static void | |
a9739e7c | 212 | unget_char (]AT_LEX_PRE_FORMALS[ int c) |
bfb07874 | 213 | { |
7548fed2 | 214 | ]AT_USE_LEX_ARGS[; |
2a8d363a | 215 | ]AT_LOCATION_IF([ |
bfb07874 | 216 | /* Wrong when C == `\n'. */ |
7548fed2 | 217 | AT_LOC = last_yylloc; |
2a8d363a | 218 | ])[ |
a9739e7c | 219 | ungetc (c, input); |
bfb07874 AD |
220 | } |
221 | ||
222 | static int | |
7548fed2 | 223 | read_signed_integer (]AT_LEX_FORMALS[) |
bfb07874 | 224 | { |
a9739e7c | 225 | int c = get_char (]AT_LEX_ARGS[); |
bfb07874 AD |
226 | int sign = 1; |
227 | int n = 0; | |
228 | ||
7548fed2 | 229 | ]AT_USE_LEX_ARGS[; |
bfb07874 AD |
230 | if (c == '-') |
231 | { | |
a9739e7c | 232 | c = get_char (]AT_LEX_ARGS[); |
bfb07874 AD |
233 | sign = -1; |
234 | } | |
235 | ||
236 | while (isdigit (c)) | |
237 | { | |
238 | n = 10 * n + (c - '0'); | |
a9739e7c | 239 | c = get_char (]AT_LEX_ARGS[); |
bfb07874 AD |
240 | } |
241 | ||
a9739e7c | 242 | unget_char (]AT_LEX_PRE_ARGS[ c); |
bfb07874 AD |
243 | |
244 | return sign * n; | |
245 | } | |
246 | ||
247 | ||
248 | ||
249 | /*---------------------------------------------------------------. | |
250 | | Lexical analyzer returns an integer on the stack and the token | | |
251 | | NUM, or the ASCII character read if not a number. Skips all | | |
252 | | blanks and tabs, returns 0 for EOF. | | |
253 | `---------------------------------------------------------------*/ | |
254 | ||
255 | static int | |
7548fed2 | 256 | yylex (]AT_LEX_FORMALS[) |
bfb07874 | 257 | { |
c19988b7 | 258 | static int init = 1; |
bfb07874 AD |
259 | int c; |
260 | ||
c19988b7 AD |
261 | if (init) |
262 | { | |
263 | init = 0; | |
fb9712a9 | 264 | ]AT_LOCATION_IF([ |
7548fed2 AD |
265 | AT_LOC.last_column = 0; |
266 | AT_LOC.last_line = 1; | |
fb9712a9 | 267 | ])[ |
c19988b7 AD |
268 | } |
269 | ||
fb9712a9 AD |
270 | ]AT_LOCATION_IF([ |
271 | AT_LOC.first_column = AT_LOC.last_column; | |
7548fed2 | 272 | AT_LOC.first_line = AT_LOC.last_line; |
fb9712a9 | 273 | ])[ |
bfb07874 AD |
274 | |
275 | /* Skip white space. */ | |
a9739e7c | 276 | while ((c = get_char (]AT_LEX_ARGS[)) == ' ' || c == '\t') |
bfb07874 | 277 | { |
fb9712a9 | 278 | ]AT_LOCATION_IF( |
d02b25f9 | 279 | [ AT_LOC.first_column = AT_LOC.last_column; |
7548fed2 | 280 | AT_LOC.first_line = AT_LOC.last_line; |
fb9712a9 | 281 | ])[ |
bfb07874 AD |
282 | } |
283 | ||
284 | /* process numbers */ | |
285 | if (c == '.' || isdigit (c)) | |
286 | { | |
a9739e7c | 287 | unget_char (]AT_LEX_PRE_ARGS[ c); |
7548fed2 | 288 | ]AT_VAL[.ival = read_signed_integer (]AT_LEX_ARGS[); |
bfb07874 AD |
289 | return NUM; |
290 | } | |
291 | ||
292 | /* Return end-of-file. */ | |
293 | if (c == EOF) | |
6b7e85b9 | 294 | return CALC_EOF; |
bfb07874 AD |
295 | |
296 | /* Return single chars. */ | |
297 | return c; | |
298 | } | |
299 | ||
300 | static int | |
301 | power (int base, int exponent) | |
302 | { | |
303 | int res = 1; | |
304 | if (exponent < 0) | |
6100a9aa | 305 | exit (3); |
bfb07874 AD |
306 | for (/* Niente */; exponent; --exponent) |
307 | res *= base; | |
308 | return res; | |
309 | } | |
310 | ||
7548fed2 | 311 | |
bfb07874 | 312 | int |
342b8b6e | 313 | main (int argc, const char **argv) |
bfb07874 | 314 | { |
caf37a36 | 315 | semantic_value result = 0; |
e7cb57c0 | 316 | int count = 0; |
4e8c79eb | 317 | int status; |
342b8b6e | 318 | |
419ab105 PE |
319 | /* This used to be alarm (10), but that isn't enough time for |
320 | a July 1995 vintage DEC Alphastation 200 4/100 system, | |
321 | according to Nelson H. F. Beebe. 100 seconds is enough. */ | |
322 | alarm (100); | |
323 | ||
342b8b6e | 324 | if (argc == 2) |
a9739e7c | 325 | input = fopen (argv[1], "r"); |
bfb07874 | 326 | else |
a9739e7c | 327 | input = stdin; |
bfb07874 | 328 | |
a9739e7c | 329 | if (!input) |
bfb07874 AD |
330 | { |
331 | perror (argv[1]); | |
6100a9aa | 332 | return 3; |
bfb07874 AD |
333 | } |
334 | ||
8f7e3cf9 | 335 | ]AT_SKEL_CC_IF([], [m4_bmatch([$4], [%debug], |
d02b25f9 | 336 | [ yydebug = 1;])])[ |
b0f98b10 | 337 | status = yyparse (]AT_PARAM_IF([&result, &count])[); |
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 | |
413 | /^Shifting/d | |
414 | /^state/d | |
dd5f2af2 | 415 | /^Cleanup:/d |
9280d3ef AD |
416 | /^Error:/d |
417 | /^Next/d | |
418 | /^Discarding/d | |
62b08cfc | 419 | / \$[[0-9$]]* = /d |
9280d3ef | 420 | /^yydestructor:/d' stderr >at-stderr |
342b8b6e | 421 | mv at-stderr stderr |
51dec47b AD |
422 | # 2. Create the reference error message. |
423 | AT_DATA([[expout]], | |
b0f98b10 | 424 | [$5 |
342b8b6e | 425 | ]) |
51dec47b | 426 | # 3. If locations are not used, remove them. |
2a8d363a | 427 | AT_YYERROR_SEES_LOC_IF([], |
51dec47b AD |
428 | [[sed 's/^[-0-9.]*: //' expout >at-expout |
429 | mv at-expout expout]]) | |
430 | # 4. If error-verbose is not used, strip the`, unexpected....' part. | |
f50adbbd | 431 | m4_bmatch([$1], [%error-verbose], [], |
6e649e65 | 432 | [[sed 's/syntax error, .*$/syntax error/' expout >at-expout |
51dec47b AD |
433 | mv at-expout expout]]) |
434 | # 5. Check | |
435 | AT_CHECK([cat stderr], 0, [expout]) | |
342b8b6e | 436 | ]) |
bfb07874 AD |
437 | |
438 | ||
fb9712a9 AD |
439 | # AT_CHECK_CALC([BISON-OPTIONS, [EXPECTED-TO-FAIL]]) |
440 | # -------------------------------------------------- | |
bfb07874 AD |
441 | # Start a testing chunk which compiles `calc' grammar with |
442 | # BISON-OPTIONS, and performs several tests over the parser. | |
907e3bc8 | 443 | # However, if EXPECTED-TO-FAIL is nonempty, this test is expected to fail. |
342b8b6e | 444 | m4_define([AT_CHECK_CALC], |
bfb07874 AD |
445 | [# We use integers to avoid dependencies upon the precision of doubles. |
446 | AT_SETUP([Calculator $1]) | |
447 | ||
907e3bc8 PE |
448 | m4_ifval([$2], [AT_CHECK([exit 77])]) |
449 | ||
67a25fed | 450 | AT_BISON_OPTION_PUSHDEFS([$1]) |
2a8d363a | 451 | |
bfb07874 AD |
452 | AT_DATA_CALC_Y([$1]) |
453 | ||
8f7e3cf9 | 454 | AT_SKEL_CC_IF( |
07971983 PE |
455 | [AT_CHECK([bison -o calc.cc calc.y]) |
456 | AT_COMPILE_CXX([calc])], | |
457 | [AT_CHECK([bison -o calc.c calc.y]) | |
458 | AT_COMPILE([calc])]) | |
bfb07874 AD |
459 | |
460 | # Test the priorities. | |
461 | _AT_CHECK_CALC([$1], | |
462 | [1 + 2 * 3 = 7 | |
463 | 1 + 2 * -3 = -5 | |
464 | ||
465 | -1^2 = -1 | |
466 | (-1)^2 = 1 | |
467 | ||
468 | ---1 = -1 | |
469 | ||
470 | 1 - 2 - 3 = -4 | |
471 | 1 - (2 - 3) = 2 | |
472 | ||
473 | 2^2^3 = 256 | |
e7cb57c0 | 474 | (2^2)^3 = 64], |
d1ff7a7c | 475 | [842]) |
bfb07874 | 476 | |
6e649e65 | 477 | # Some syntax errors. |
d1ff7a7c | 478 | _AT_CHECK_CALC_ERROR([$1], [1], [0 0], [15], |
72f000b0 | 479 | [1.2: syntax error, unexpected number]) |
d1ff7a7c | 480 | _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [20], |
72f000b0 | 481 | [1.2: syntax error, unexpected '/', expecting number or '-' or '(' or '!']) |
e757bb10 | 482 | _AT_CHECK_CALC_ERROR([$1], [1], [error], [5], |
0b86fc41 | 483 | [1.0: syntax error, unexpected $undefined]) |
d1ff7a7c | 484 | _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [30], |
7548fed2 | 485 | [1.6: syntax error, unexpected '=']) |
b0f98b10 | 486 | _AT_CHECK_CALC_ERROR([$1], [1], |
bfb07874 AD |
487 | [ |
488 | +1], | |
d1ff7a7c | 489 | [20], |
7548fed2 | 490 | [2.0: syntax error, unexpected '+']) |
b7c49edf | 491 | # Exercise error messages with EOF: work on an empty file. |
cea1469d | 492 | _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4], |
72f000b0 | 493 | [1.0: syntax error, unexpected end of input]) |
51dec47b AD |
494 | |
495 | # Exercise the error token: without it, we die at the first error, | |
0b86fc41 AD |
496 | # hence be sure to |
497 | # | |
498 | # - have several errors which exercise different shift/discardings | |
499 | # - (): nothing to pop, nothing to discard | |
500 | # - (1 + 1 + 1 +): a lot to pop, nothing to discard | |
501 | # - (* * *): nothing to pop, a lot to discard | |
502 | # - (1 + 2 * *): some to pop and discard | |
503 | # | |
504 | # - test the action associated to `error' | |
505 | # | |
8dd162d3 PE |
506 | # - check the look-ahead that triggers an error is not discarded |
507 | # when we enter error recovery. Below, the look-ahead causing the | |
0b86fc41 AD |
508 | # first error is ")", which is needed to recover from the error and |
509 | # produce the "0" that triggers the "0 != 1" error. | |
510 | # | |
511 | _AT_CHECK_CALC_ERROR([$1], [0], | |
512 | [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1], | |
d1ff7a7c | 513 | [250], |
72f000b0 PE |
514 | [1.1: syntax error, unexpected ')', expecting number or '-' or '(' or '!' |
515 | 1.17: syntax error, unexpected ')', expecting number or '-' or '(' or '!' | |
516 | 1.22: syntax error, unexpected '*', expecting number or '-' or '(' or '!' | |
517 | 1.40: syntax error, unexpected '*', expecting number or '-' or '(' or '!' | |
0b86fc41 AD |
518 | calc: error: 4444 != 1]) |
519 | ||
520 | # The same, but this time exercising explicitly triggered syntax errors. | |
8dd162d3 | 521 | # POSIX says the look-ahead causing the error should not be discarded. |
d1ff7a7c | 522 | _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [102], |
72f000b0 | 523 | [1.9: syntax error, unexpected number |
0b86fc41 | 524 | calc: error: 2222 != 1]) |
d1ff7a7c | 525 | _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (0 0) = 1], [113], |
72f000b0 PE |
526 | [1.3: syntax error, unexpected '*', expecting number or '-' or '(' or '!' |
527 | 1.11: syntax error, unexpected number | |
82c53be4 | 528 | calc: error: 2222 != 1]) |
67a25fed | 529 | AT_BISON_OPTION_POPDEFS |
2a8d363a | 530 | |
d803322e | 531 | AT_CLEANUP |
bfb07874 AD |
532 | ])# AT_CHECK_CALC |
533 | ||
534 | ||
535 | ||
536 | ||
f50adbbd AD |
537 | # ------------------------ # |
538 | # Simple LALR Calculator. # | |
539 | # ------------------------ # | |
540 | ||
8f7e3cf9 | 541 | AT_BANNER([[Simple LALR(1) Calculator.]]) |
f50adbbd AD |
542 | |
543 | # AT_CHECK_CALC_LALR([BISON-OPTIONS]) | |
544 | # ----------------------------------- | |
545 | # Start a testing chunk which compiles `calc' grammar with | |
546 | # BISON-OPTIONS, and performs several tests over the parser. | |
547 | m4_define([AT_CHECK_CALC_LALR], | |
548 | [AT_CHECK_CALC($@)]) | |
549 | ||
550 | AT_CHECK_CALC_LALR() | |
551 | ||
9e32add8 | 552 | AT_CHECK_CALC_LALR([%defines]) |
ae7453f2 | 553 | AT_CHECK_CALC_LALR([%locations]) |
9e32add8 AD |
554 | AT_CHECK_CALC_LALR([%name-prefix="calc"]) |
555 | AT_CHECK_CALC_LALR([%verbose]) | |
556 | AT_CHECK_CALC_LALR([%yacc]) | |
f50adbbd AD |
557 | AT_CHECK_CALC_LALR([%error-verbose]) |
558 | ||
211074ca | 559 | AT_CHECK_CALC_LALR([%pure-parser %locations]) |
ae7453f2 | 560 | AT_CHECK_CALC_LALR([%error-verbose %locations]) |
f50adbbd | 561 | |
9e32add8 | 562 | AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc]) |
f50adbbd AD |
563 | |
564 | AT_CHECK_CALC_LALR([%debug]) | |
9e32add8 | 565 | AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc]) |
c19988b7 | 566 | |
2a8d363a AD |
567 | AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc]) |
568 | ||
caf37a36 | 569 | 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 |
570 | |
571 | ||
572 | # ----------------------- # | |
573 | # Simple GLR Calculator. # | |
574 | # ----------------------- # | |
575 | ||
576 | AT_BANNER([[Simple GLR Calculator.]]) | |
577 | ||
578 | # AT_CHECK_CALC_GLR([BISON-OPTIONS]) | |
579 | # ---------------------------------- | |
580 | # Start a testing chunk which compiles `calc' grammar with | |
581 | # BISON-OPTIONS and %glr-parser, and performs several tests over the parser. | |
582 | m4_define([AT_CHECK_CALC_GLR], | |
ae7453f2 | 583 | [AT_CHECK_CALC([%glr-parser] $@)]) |
f50adbbd | 584 | |
bfb07874 | 585 | |
f50adbbd | 586 | AT_CHECK_CALC_GLR() |
bfb07874 | 587 | |
9e32add8 | 588 | AT_CHECK_CALC_GLR([%defines]) |
ae7453f2 | 589 | AT_CHECK_CALC_GLR([%locations]) |
9e32add8 AD |
590 | AT_CHECK_CALC_GLR([%name-prefix="calc"]) |
591 | AT_CHECK_CALC_GLR([%verbose]) | |
592 | AT_CHECK_CALC_GLR([%yacc]) | |
f50adbbd | 593 | AT_CHECK_CALC_GLR([%error-verbose]) |
bfb07874 | 594 | |
211074ca | 595 | AT_CHECK_CALC_GLR([%pure-parser %locations]) |
ae7453f2 | 596 | AT_CHECK_CALC_GLR([%error-verbose %locations]) |
bfb07874 | 597 | |
9e32add8 | 598 | AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc]) |
bfb07874 | 599 | |
f50adbbd | 600 | AT_CHECK_CALC_GLR([%debug]) |
9e32add8 | 601 | AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc]) |
21964f43 | 602 | |
2a8d363a AD |
603 | AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc]) |
604 | ||
caf37a36 | 605 | 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 |
606 | |
607 | ||
608 | # ----------------------------- # | |
609 | # Simple LALR1 C++ Calculator. # | |
610 | # ----------------------------- # | |
611 | ||
8f7e3cf9 | 612 | AT_BANNER([[Simple LALR(1) C++ Calculator.]]) |
7548fed2 AD |
613 | |
614 | # AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS]) | |
615 | # --------------------------------------- | |
616 | # Start a testing chunk which compiles `calc' grammar with | |
caf37a36 | 617 | # the C++ skeleton, and performs several tests over the parser. |
7548fed2 | 618 | m4_define([AT_CHECK_CALC_LALR1_CC], |
8f7e3cf9 AD |
619 | [AT_CHECK_CALC([%skeleton "lalr1.cc" %defines %locations] $@)]) |
620 | ||
621 | AT_CHECK_CALC_LALR1_CC([]) | |
622 | AT_CHECK_CALC_LALR1_CC([%error-verbose %name-prefix="calc" %verbose %yacc]) | |
7548fed2 | 623 | |
8f7e3cf9 | 624 | AT_CHECK_CALC_LALR1_CC([%error-verbose %debug %name-prefix="calc" %verbose %yacc]) |
7548fed2 | 625 | |
8f7e3cf9 | 626 | AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %name-prefix="calc" %verbose %yacc]) |
0b86fc41 | 627 | |
8f7e3cf9 | 628 | 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 | 629 | |
7548fed2 | 630 | |
7548fed2 | 631 | |
8f7e3cf9 AD |
632 | # --------------------------- # |
633 | # Simple GLR C++ Calculator. # | |
634 | # --------------------------- # | |
635 | ||
636 | AT_BANNER([[Simple GLR C++ Calculator.]]) | |
637 | ||
638 | # AT_CHECK_CALC_GLR_CC([BISON-OPTIONS]) | |
639 | # ------------------------------------- | |
640 | # Start a testing chunk which compiles `calc' grammar with | |
641 | # the GLR C++ skeleton, and performs several tests over the parser. | |
642 | m4_define([AT_CHECK_CALC_GLR_CC], | |
643 | [AT_CHECK_CALC([%skeleton "glr.cc" %defines %locations] $@)]) | |
644 | ||
645 | #AT_CHECK_CALC_GLR_CC([]) | |
646 | #AT_CHECK_CALC_GLR_CC([%error-verbose %name-prefix="calc" %verbose %yacc]) | |
647 | ||
648 | # AT_CHECK_CALC_GLR_CC([%debug]) | |
649 | #AT_CHECK_CALC_GLR_CC([%error-verbose %debug %name-prefix="calc" %verbose %yacc]) | |
7548fed2 | 650 | |
8f7e3cf9 | 651 | AT_CHECK_CALC_GLR_CC([%pure-parser %error-verbose %debug %name-prefix="calc" %verbose %yacc]) |
7548fed2 | 652 | |
8f7e3cf9 | 653 | AT_CHECK_CALC_GLR_CC([%pure-parser %error-verbose %debug %name-prefix="calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |