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