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