]>
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); |
67a25fed AD |
69 | ]AT_LALR1_CC_IF([typedef yy::Location YYLTYPE;], |
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 AD |
130 | void |
131 | yy::Parser::error_ () | |
132 | { | |
d02b25f9 | 133 | std::cerr << AT_LOCATION_IF([location << ": " << ])message << std::endl; |
7548fed2 AD |
134 | } |
135 | ||
136 | int | |
caf37a36 | 137 | yyparse (AT_PARAM_IF([semantic_value *result, int *count])) |
7548fed2 | 138 | { |
a3cb6248 AD |
139 | yy::Parser parser[]AT_PARAM_IF([ (result, count)]); |
140 | parser.set_debug_level (!!YYDEBUG); | |
7548fed2 AD |
141 | return parser.parse (); |
142 | } | |
143 | ], | |
144 | [static void | |
145 | yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *yylloc, ]) | |
caf37a36 | 146 | AT_PARAM_IF([semantic_value *result, int *count, ]) |
7548fed2 AD |
147 | const char *s) |
148 | { | |
149 | AT_PARAM_IF([(void) result; (void) count;]) | |
150 | AT_YYERROR_SEES_LOC_IF([ | |
151 | fprintf (stderr, "%d.%d", | |
152 | AT_LOC.first_line, AT_LOC.first_column); | |
153 | if (AT_LOC.first_line != AT_LOC.last_line) | |
154 | fprintf (stderr, "-%d.%d", | |
155 | AT_LOC.last_line, AT_LOC.last_column - 1); | |
156 | else if (AT_LOC.first_column != AT_LOC.last_column - 1) | |
157 | fprintf (stderr, "-%d", | |
158 | AT_LOC.last_column - 1); | |
159 | fprintf (stderr, ": ");]) | |
160 | fprintf (stderr, "%s\n", s); | |
161 | }])[ | |
162 | ||
b7c49edf | 163 | |
2a8d363a | 164 | ]AT_LOCATION_IF([ |
b7c49edf | 165 | static YYLTYPE last_yylloc; |
2a8d363a | 166 | ])[ |
bfb07874 | 167 | static int |
7548fed2 | 168 | yygetc (]AT_LEX_FORMALS[) |
bfb07874 AD |
169 | { |
170 | int res = getc (yyin); | |
7548fed2 | 171 | ]AT_USE_LEX_ARGS[; |
2a8d363a | 172 | ]AT_LOCATION_IF([ |
7548fed2 | 173 | last_yylloc = AT_LOC; |
bfb07874 AD |
174 | if (res == '\n') |
175 | { | |
7548fed2 AD |
176 | AT_LALR1_CC_IF( |
177 | [ AT_LOC.end.line++; | |
178 | AT_LOC.end.column = 0;], | |
179 | [ AT_LOC.last_line++; | |
180 | AT_LOC.last_column = 0;]) | |
bfb07874 AD |
181 | } |
182 | else | |
7548fed2 AD |
183 | AT_LALR1_CC_IF( |
184 | [ AT_LOC.end.column++;], | |
185 | [ AT_LOC.last_column++;]) | |
2a8d363a | 186 | ])[ |
bfb07874 AD |
187 | return res; |
188 | } | |
189 | ||
190 | ||
191 | static void | |
7548fed2 | 192 | yyungetc (]AT_LEX_PRE_FORMALS[ int c) |
bfb07874 | 193 | { |
7548fed2 | 194 | ]AT_USE_LEX_ARGS[; |
2a8d363a | 195 | ]AT_LOCATION_IF([ |
bfb07874 | 196 | /* Wrong when C == `\n'. */ |
7548fed2 | 197 | AT_LOC = last_yylloc; |
2a8d363a | 198 | ])[ |
bfb07874 AD |
199 | ungetc (c, yyin); |
200 | } | |
201 | ||
202 | static int | |
7548fed2 | 203 | read_signed_integer (]AT_LEX_FORMALS[) |
bfb07874 | 204 | { |
7548fed2 | 205 | int c = yygetc (]AT_LEX_ARGS[); |
bfb07874 AD |
206 | int sign = 1; |
207 | int n = 0; | |
208 | ||
7548fed2 | 209 | ]AT_USE_LEX_ARGS[; |
bfb07874 AD |
210 | if (c == '-') |
211 | { | |
7548fed2 | 212 | c = yygetc (]AT_LEX_ARGS[); |
bfb07874 AD |
213 | sign = -1; |
214 | } | |
215 | ||
216 | while (isdigit (c)) | |
217 | { | |
218 | n = 10 * n + (c - '0'); | |
7548fed2 | 219 | c = yygetc (]AT_LEX_ARGS[); |
bfb07874 AD |
220 | } |
221 | ||
7548fed2 | 222 | yyungetc (]AT_LEX_PRE_ARGS[ c); |
bfb07874 AD |
223 | |
224 | return sign * n; | |
225 | } | |
226 | ||
227 | ||
228 | ||
229 | /*---------------------------------------------------------------. | |
230 | | Lexical analyzer returns an integer on the stack and the token | | |
231 | | NUM, or the ASCII character read if not a number. Skips all | | |
232 | | blanks and tabs, returns 0 for EOF. | | |
233 | `---------------------------------------------------------------*/ | |
234 | ||
235 | static int | |
7548fed2 | 236 | yylex (]AT_LEX_FORMALS[) |
bfb07874 | 237 | { |
c19988b7 | 238 | static int init = 1; |
bfb07874 AD |
239 | int c; |
240 | ||
c19988b7 AD |
241 | if (init) |
242 | { | |
243 | init = 0; | |
7548fed2 AD |
244 | ]AT_LALR1_CC_IF([], |
245 | [AT_LOCATION_IF([ | |
246 | AT_LOC.last_column = 0; | |
247 | AT_LOC.last_line = 1; | |
248 | ])])[ | |
c19988b7 AD |
249 | } |
250 | ||
d02b25f9 | 251 | ]AT_LOCATION_IF([AT_LALR1_CC_IF( |
7548fed2 | 252 | [ AT_LOC.begin = AT_LOC.end;], |
d02b25f9 | 253 | [ AT_LOC.first_column = AT_LOC.last_column; |
7548fed2 AD |
254 | AT_LOC.first_line = AT_LOC.last_line; |
255 | ])])[ | |
bfb07874 AD |
256 | |
257 | /* Skip white space. */ | |
7548fed2 | 258 | while ((c = yygetc (]AT_LEX_ARGS[)) == ' ' || c == '\t') |
bfb07874 | 259 | { |
d02b25f9 | 260 | ]AT_LOCATION_IF([AT_LALR1_CC_IF( |
7548fed2 | 261 | [ AT_LOC.begin = AT_LOC.end;], |
d02b25f9 | 262 | [ AT_LOC.first_column = AT_LOC.last_column; |
7548fed2 AD |
263 | AT_LOC.first_line = AT_LOC.last_line; |
264 | ])])[ | |
bfb07874 AD |
265 | } |
266 | ||
267 | /* process numbers */ | |
268 | if (c == '.' || isdigit (c)) | |
269 | { | |
7548fed2 AD |
270 | yyungetc (]AT_LEX_PRE_ARGS[ c); |
271 | ]AT_VAL[.ival = read_signed_integer (]AT_LEX_ARGS[); | |
bfb07874 AD |
272 | return NUM; |
273 | } | |
274 | ||
275 | /* Return end-of-file. */ | |
276 | if (c == EOF) | |
6b7e85b9 | 277 | return CALC_EOF; |
bfb07874 AD |
278 | |
279 | /* Return single chars. */ | |
280 | return c; | |
281 | } | |
282 | ||
283 | static int | |
284 | power (int base, int exponent) | |
285 | { | |
286 | int res = 1; | |
287 | if (exponent < 0) | |
288 | exit (1); | |
289 | for (/* Niente */; exponent; --exponent) | |
290 | res *= base; | |
291 | return res; | |
292 | } | |
293 | ||
7548fed2 | 294 | |
bfb07874 | 295 | int |
342b8b6e | 296 | main (int argc, const char **argv) |
bfb07874 | 297 | { |
caf37a36 | 298 | semantic_value result = 0; |
e7cb57c0 | 299 | int count = 0; |
4e8c79eb | 300 | int status; |
342b8b6e | 301 | |
82c53be4 | 302 | alarm (10); |
342b8b6e | 303 | if (argc == 2) |
bfb07874 AD |
304 | yyin = fopen (argv[1], "r"); |
305 | else | |
306 | yyin = stdin; | |
307 | ||
342b8b6e | 308 | if (!yyin) |
bfb07874 AD |
309 | { |
310 | perror (argv[1]); | |
311 | exit (1); | |
312 | } | |
313 | ||
d02b25f9 AD |
314 | ]AT_LALR1_CC_IF([], [m4_bmatch([$4], [%debug], |
315 | [ yydebug = 1;])])[ | |
b0f98b10 | 316 | status = yyparse (]AT_PARAM_IF([&result, &count])[); |
63d0fb9c PE |
317 | if (global_result != result) |
318 | abort (); | |
319 | if (global_count != count) | |
320 | abort (); | |
b0f98b10 | 321 | return status; |
bfb07874 AD |
322 | } |
323 | ]]) | |
324 | ])# _AT_DATA_CALC_Y | |
325 | ||
326 | ||
327 | # AT_DATA_CALC_Y([BISON-OPTIONS]) | |
328 | # ------------------------------- | |
329 | # Produce `calc.y'. | |
342b8b6e | 330 | m4_define([AT_DATA_CALC_Y], |
9e32add8 | 331 | [_AT_DATA_CALC_Y($[1], $[2], $[3], [$1]) |
f50adbbd | 332 | ]) |
bfb07874 AD |
333 | |
334 | ||
335 | ||
342b8b6e AD |
336 | # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES = 0]) |
337 | # ------------------------------------------------------------ | |
bfb07874 | 338 | # Run `calc' on INPUT and expect no STDOUT nor STDERR. |
342b8b6e | 339 | # |
f50adbbd AD |
340 | # If BISON-OPTIONS contains `%debug' but not `%glr-parser', then |
341 | # NUM-STDERR-LINES is the number of expected lines on stderr. | |
342 | # | |
343 | # We don't count GLR's traces yet, since its traces are somewhat | |
344 | # different from LALR's. | |
342b8b6e AD |
345 | m4_define([_AT_CHECK_CALC], |
346 | [AT_DATA([[input]], | |
347 | [[$2 | |
348 | ]]) | |
f50adbbd AD |
349 | AT_PARSER_CHECK([./calc input], 0, [], [stderr]) |
350 | m4_bmatch([$1], | |
351 | [%debug.*%glr\|%glr.*%debug], | |
352 | [], | |
353 | [%debug], | |
354 | [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$3 | |
355 | ])]) | |
342b8b6e AD |
356 | ]) |
357 | ||
358 | ||
b0f98b10 AD |
359 | # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT, |
360 | # [NUM-DEBUG-LINES], | |
2a8d363a | 361 | # [VERBOSE-AND-LOCATED-ERROR-MESSAGE]) |
b0f98b10 | 362 | # --------------------------------------------------------- |
6e649e65 | 363 | # Run `calc' on INPUT, and expect a `syntax error' message. |
342b8b6e | 364 | # |
b7c49edf AD |
365 | # If INPUT starts with a slash, it is used as absolute input file name, |
366 | # otherwise as contents. | |
367 | # | |
9e32add8 | 368 | # If BISON-OPTIONS contains `%location', then make sure the ERROR-LOCATION |
342b8b6e AD |
369 | # is correctly output on stderr. |
370 | # | |
f50adbbd | 371 | # If BISON-OPTIONS contains `%error-verbose', then make sure the |
6e649e65 | 372 | # IF-YYERROR-VERBOSE message is properly output after `syntax error, ' |
342b8b6e AD |
373 | # on STDERR. |
374 | # | |
f50adbbd AD |
375 | # If BISON-OPTIONS contains `%debug' but not `%glr', then NUM-STDERR-LINES |
376 | # is the number of expected lines on stderr. | |
342b8b6e | 377 | m4_define([_AT_CHECK_CALC_ERROR], |
b0f98b10 AD |
378 | [m4_bmatch([$3], [^/], |
379 | [AT_PARSER_CHECK([./calc $3], $2, [], [stderr])], | |
b7c49edf | 380 | [AT_DATA([[input]], |
b0f98b10 | 381 | [[$3 |
342b8b6e | 382 | ]]) |
b0f98b10 | 383 | AT_PARSER_CHECK([./calc input], $2, [], [stderr])]) |
f50adbbd AD |
384 | m4_bmatch([$1], |
385 | [%debug.*%glr\|%glr.*%debug], | |
386 | [], | |
387 | [%debug], | |
b0f98b10 | 388 | [AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0, [$4 |
51dec47b | 389 | ])]) |
342b8b6e | 390 | |
51dec47b AD |
391 | # Normalize the observed and expected error messages, depending upon the |
392 | # options. | |
393 | # 1. Remove the traces from observed. | |
9280d3ef AD |
394 | sed '/^Starting/d |
395 | /^Entering/d | |
f50adbbd | 396 | /^Stack/d |
9280d3ef AD |
397 | /^Reading/d |
398 | /^Reducing/d | |
399 | /^Shifting/d | |
400 | /^state/d | |
401 | /^Error:/d | |
402 | /^Next/d | |
403 | /^Discarding/d | |
404 | /^yydestructor:/d' stderr >at-stderr | |
342b8b6e | 405 | mv at-stderr stderr |
51dec47b AD |
406 | # 2. Create the reference error message. |
407 | AT_DATA([[expout]], | |
b0f98b10 | 408 | [$5 |
342b8b6e | 409 | ]) |
51dec47b | 410 | # 3. If locations are not used, remove them. |
2a8d363a | 411 | AT_YYERROR_SEES_LOC_IF([], |
51dec47b AD |
412 | [[sed 's/^[-0-9.]*: //' expout >at-expout |
413 | mv at-expout expout]]) | |
414 | # 4. If error-verbose is not used, strip the`, unexpected....' part. | |
f50adbbd | 415 | m4_bmatch([$1], [%error-verbose], [], |
6e649e65 | 416 | [[sed 's/syntax error, .*$/syntax error/' expout >at-expout |
51dec47b AD |
417 | mv at-expout expout]]) |
418 | # 5. Check | |
419 | AT_CHECK([cat stderr], 0, [expout]) | |
342b8b6e | 420 | ]) |
bfb07874 AD |
421 | |
422 | ||
907e3bc8 | 423 | # AT_CHECK_CALC([BISON-OPTIONS [, EXPECTED-TO-FAIL]]) |
f50adbbd | 424 | # ------------------------------ |
bfb07874 AD |
425 | # Start a testing chunk which compiles `calc' grammar with |
426 | # BISON-OPTIONS, and performs several tests over the parser. | |
907e3bc8 | 427 | # However, if EXPECTED-TO-FAIL is nonempty, this test is expected to fail. |
342b8b6e | 428 | m4_define([AT_CHECK_CALC], |
bfb07874 AD |
429 | [# We use integers to avoid dependencies upon the precision of doubles. |
430 | AT_SETUP([Calculator $1]) | |
431 | ||
907e3bc8 PE |
432 | m4_ifval([$2], [AT_CHECK([exit 77])]) |
433 | ||
67a25fed | 434 | AT_BISON_OPTION_PUSHDEFS([$1]) |
2a8d363a | 435 | |
bfb07874 AD |
436 | AT_DATA_CALC_Y([$1]) |
437 | ||
07971983 PE |
438 | AT_LALR1_CC_IF( |
439 | [AT_CHECK([bison -o calc.cc calc.y]) | |
440 | AT_COMPILE_CXX([calc])], | |
441 | [AT_CHECK([bison -o calc.c calc.y]) | |
442 | AT_COMPILE([calc])]) | |
bfb07874 AD |
443 | |
444 | # Test the priorities. | |
445 | _AT_CHECK_CALC([$1], | |
446 | [1 + 2 * 3 = 7 | |
447 | 1 + 2 * -3 = -5 | |
448 | ||
449 | -1^2 = -1 | |
450 | (-1)^2 = 1 | |
451 | ||
452 | ---1 = -1 | |
453 | ||
454 | 1 - 2 - 3 = -4 | |
455 | 1 - (2 - 3) = 2 | |
456 | ||
457 | 2^2^3 = 256 | |
e7cb57c0 | 458 | (2^2)^3 = 64], |
1576d44d | 459 | [570]) |
bfb07874 | 460 | |
6e649e65 | 461 | # Some syntax errors. |
1576d44d | 462 | _AT_CHECK_CALC_ERROR([$1], [1], [0 0], [13], |
7548fed2 | 463 | [1.2: syntax error, unexpected "number"]) |
1576d44d | 464 | _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [18], |
0b86fc41 | 465 | [1.2: syntax error, unexpected '/', expecting "number" or '-' or '(' or '!']) |
e757bb10 | 466 | _AT_CHECK_CALC_ERROR([$1], [1], [error], [5], |
0b86fc41 | 467 | [1.0: syntax error, unexpected $undefined]) |
1576d44d | 468 | _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [26], |
7548fed2 | 469 | [1.6: syntax error, unexpected '=']) |
b0f98b10 | 470 | _AT_CHECK_CALC_ERROR([$1], [1], |
bfb07874 AD |
471 | [ |
472 | +1], | |
1576d44d | 473 | [16], |
7548fed2 | 474 | [2.0: syntax error, unexpected '+']) |
b7c49edf | 475 | # Exercise error messages with EOF: work on an empty file. |
e757bb10 | 476 | _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [5], |
0b86fc41 | 477 | [1.0: syntax error, unexpected "end of input"]) |
51dec47b AD |
478 | |
479 | # Exercise the error token: without it, we die at the first error, | |
0b86fc41 AD |
480 | # hence be sure to |
481 | # | |
482 | # - have several errors which exercise different shift/discardings | |
483 | # - (): nothing to pop, nothing to discard | |
484 | # - (1 + 1 + 1 +): a lot to pop, nothing to discard | |
485 | # - (* * *): nothing to pop, a lot to discard | |
486 | # - (1 + 2 * *): some to pop and discard | |
487 | # | |
488 | # - test the action associated to `error' | |
489 | # | |
8dd162d3 PE |
490 | # - check the look-ahead that triggers an error is not discarded |
491 | # when we enter error recovery. Below, the look-ahead causing the | |
0b86fc41 AD |
492 | # first error is ")", which is needed to recover from the error and |
493 | # produce the "0" that triggers the "0 != 1" error. | |
494 | # | |
495 | _AT_CHECK_CALC_ERROR([$1], [0], | |
496 | [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1], | |
1576d44d | 497 | [188], |
0b86fc41 AD |
498 | [1.1: syntax error, unexpected ')', expecting "number" or '-' or '(' or '!' |
499 | 1.17: syntax error, unexpected ')', expecting "number" or '-' or '(' or '!' | |
500 | 1.22: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!' | |
501 | 1.40: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!' | |
502 | calc: error: 4444 != 1]) | |
503 | ||
504 | # The same, but this time exercising explicitly triggered syntax errors. | |
8dd162d3 | 505 | # POSIX says the look-ahead causing the error should not be discarded. |
1576d44d | 506 | _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (0 0) = 1], [75], |
0b86fc41 AD |
507 | [1.9: syntax error, unexpected "number" |
508 | calc: error: 2222 != 1]) | |
1576d44d | 509 | _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (0 0) = 1], [85], |
82c53be4 PE |
510 | [1.3: syntax error, unexpected '*', expecting "number" or '-' or '(' or '!' |
511 | 1.11: syntax error, unexpected "number" | |
512 | calc: error: 2222 != 1]) | |
67a25fed | 513 | AT_BISON_OPTION_POPDEFS |
2a8d363a | 514 | |
d803322e | 515 | AT_CLEANUP |
bfb07874 AD |
516 | ])# AT_CHECK_CALC |
517 | ||
518 | ||
519 | ||
520 | ||
f50adbbd AD |
521 | # ------------------------ # |
522 | # Simple LALR Calculator. # | |
523 | # ------------------------ # | |
524 | ||
525 | AT_BANNER([[Simple LALR Calculator.]]) | |
526 | ||
527 | # AT_CHECK_CALC_LALR([BISON-OPTIONS]) | |
528 | # ----------------------------------- | |
529 | # Start a testing chunk which compiles `calc' grammar with | |
530 | # BISON-OPTIONS, and performs several tests over the parser. | |
531 | m4_define([AT_CHECK_CALC_LALR], | |
532 | [AT_CHECK_CALC($@)]) | |
533 | ||
534 | AT_CHECK_CALC_LALR() | |
535 | ||
9e32add8 | 536 | AT_CHECK_CALC_LALR([%defines]) |
ae7453f2 | 537 | AT_CHECK_CALC_LALR([%locations]) |
9e32add8 AD |
538 | AT_CHECK_CALC_LALR([%name-prefix="calc"]) |
539 | AT_CHECK_CALC_LALR([%verbose]) | |
540 | AT_CHECK_CALC_LALR([%yacc]) | |
f50adbbd AD |
541 | AT_CHECK_CALC_LALR([%error-verbose]) |
542 | ||
211074ca | 543 | AT_CHECK_CALC_LALR([%pure-parser %locations]) |
ae7453f2 | 544 | AT_CHECK_CALC_LALR([%error-verbose %locations]) |
f50adbbd | 545 | |
9e32add8 | 546 | AT_CHECK_CALC_LALR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc]) |
f50adbbd AD |
547 | |
548 | AT_CHECK_CALC_LALR([%debug]) | |
9e32add8 | 549 | AT_CHECK_CALC_LALR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc]) |
c19988b7 | 550 | |
2a8d363a AD |
551 | AT_CHECK_CALC_LALR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc]) |
552 | ||
caf37a36 | 553 | 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 |
554 | |
555 | ||
556 | # ----------------------- # | |
557 | # Simple GLR Calculator. # | |
558 | # ----------------------- # | |
559 | ||
560 | AT_BANNER([[Simple GLR Calculator.]]) | |
561 | ||
562 | # AT_CHECK_CALC_GLR([BISON-OPTIONS]) | |
563 | # ---------------------------------- | |
564 | # Start a testing chunk which compiles `calc' grammar with | |
565 | # BISON-OPTIONS and %glr-parser, and performs several tests over the parser. | |
566 | m4_define([AT_CHECK_CALC_GLR], | |
ae7453f2 | 567 | [AT_CHECK_CALC([%glr-parser] $@)]) |
f50adbbd | 568 | |
bfb07874 | 569 | |
f50adbbd | 570 | AT_CHECK_CALC_GLR() |
bfb07874 | 571 | |
9e32add8 | 572 | AT_CHECK_CALC_GLR([%defines]) |
ae7453f2 | 573 | AT_CHECK_CALC_GLR([%locations]) |
9e32add8 AD |
574 | AT_CHECK_CALC_GLR([%name-prefix="calc"]) |
575 | AT_CHECK_CALC_GLR([%verbose]) | |
576 | AT_CHECK_CALC_GLR([%yacc]) | |
f50adbbd | 577 | AT_CHECK_CALC_GLR([%error-verbose]) |
bfb07874 | 578 | |
211074ca | 579 | AT_CHECK_CALC_GLR([%pure-parser %locations]) |
ae7453f2 | 580 | AT_CHECK_CALC_GLR([%error-verbose %locations]) |
bfb07874 | 581 | |
9e32add8 | 582 | AT_CHECK_CALC_GLR([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc]) |
bfb07874 | 583 | |
f50adbbd | 584 | AT_CHECK_CALC_GLR([%debug]) |
9e32add8 | 585 | AT_CHECK_CALC_GLR([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc]) |
21964f43 | 586 | |
2a8d363a AD |
587 | AT_CHECK_CALC_GLR([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc]) |
588 | ||
caf37a36 | 589 | 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 |
590 | |
591 | ||
592 | # ----------------------------- # | |
593 | # Simple LALR1 C++ Calculator. # | |
594 | # ----------------------------- # | |
595 | ||
596 | AT_BANNER([[Simple LALR1 C++ Calculator.]]) | |
597 | ||
598 | # AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS]) | |
599 | # --------------------------------------- | |
600 | # Start a testing chunk which compiles `calc' grammar with | |
caf37a36 | 601 | # the C++ skeleton, and performs several tests over the parser. |
7548fed2 | 602 | m4_define([AT_CHECK_CALC_LALR1_CC], |
47301314 | 603 | [AT_CHECK_CALC([%skeleton "lalr1.cc"] $@)]) |
7548fed2 AD |
604 | |
605 | # AT_CHECK_CALC_LALR1_CC() | |
606 | ||
d02b25f9 | 607 | AT_CHECK_CALC_LALR1_CC([%defines %locations]) |
0b86fc41 | 608 | |
d02b25f9 | 609 | AT_CHECK_CALC_LALR1_CC([%defines]) |
7548fed2 AD |
610 | # AT_CHECK_CALC_LALR1_CC([%locations]) |
611 | # AT_CHECK_CALC_LALR1_CC([%name-prefix="calc"]) | |
612 | # AT_CHECK_CALC_LALR1_CC([%verbose]) | |
613 | # AT_CHECK_CALC_LALR1_CC([%yacc]) | |
614 | # AT_CHECK_CALC_LALR1_CC([%error-verbose]) | |
615 | ||
211074ca | 616 | # AT_CHECK_CALC_LALR1_CC([%pure-parser %locations]) |
7548fed2 AD |
617 | # AT_CHECK_CALC_LALR1_CC([%error-verbose %locations]) |
618 | ||
d02b25f9 | 619 | AT_CHECK_CALC_LALR1_CC([%error-verbose %locations %defines %name-prefix="calc" %verbose %yacc]) |
7548fed2 AD |
620 | |
621 | # AT_CHECK_CALC_LALR1_CC([%debug]) | |
d02b25f9 | 622 | AT_CHECK_CALC_LALR1_CC([%error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc]) |
7548fed2 | 623 | |
d02b25f9 | 624 | AT_CHECK_CALC_LALR1_CC([%pure-parser %error-verbose %debug %locations %defines %name-prefix="calc" %verbose %yacc]) |
7548fed2 | 625 | |
caf37a36 | 626 | 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}]) |