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