| 1 | # Simple calculator. -*- Autotest -*- |
| 2 | |
| 3 | # Copyright (C) 2000-2013 Free Software Foundation, Inc. |
| 4 | |
| 5 | # This program is free software: you can redistribute it and/or modify |
| 6 | # it under the terms of the GNU General Public License as published by |
| 7 | # the Free Software Foundation, either version 3 of the License, or |
| 8 | # (at your option) any later version. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License |
| 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | ## ---------------------------------------------------- ## |
| 19 | ## Compile the grammar described in the documentation. ## |
| 20 | ## ---------------------------------------------------- ## |
| 21 | |
| 22 | |
| 23 | # ------------------------- # |
| 24 | # Helping Autotest macros. # |
| 25 | # ------------------------- # |
| 26 | |
| 27 | |
| 28 | # _AT_DATA_CALC_Y($1, $2, $3, [BISON-DIRECTIVES]) |
| 29 | # ----------------------------------------------- |
| 30 | # Produce 'calc.y' and, if %defines was specified, 'calc-lex.c' or |
| 31 | # 'calc-lex.cc'. |
| 32 | # |
| 33 | # Don't call this macro directly, because it contains some occurrences |
| 34 | # of '$1' etc. which will be interpreted by m4. So you should call it |
| 35 | # with $1, $2, and $3 as arguments, which is what AT_DATA_CALC_Y does. |
| 36 | # |
| 37 | # When %defines is not passed, generate a single self-contained file. |
| 38 | # Otherwise, generate three: calc.y with the parser, calc-lex.c with |
| 39 | # the scanner, and calc-main.c with "main()". This is in order to |
| 40 | # stress the use of the generated parser header. To avoid code |
| 41 | # duplication, AT_CALC_LEX and AT_CALC_MAIN contain the body of these |
| 42 | # two later files. |
| 43 | m4_define([_AT_DATA_CALC_Y], |
| 44 | [m4_if([$1$2$3], $[1]$[2]$[3], [], |
| 45 | [m4_fatal([$0: Invalid arguments: $@])])dnl |
| 46 | |
| 47 | m4_pushdef([AT_CALC_MAIN], |
| 48 | [#include <assert.h> |
| 49 | #include <unistd.h> |
| 50 | |
| 51 | AT_SKEL_CC_IF([[ |
| 52 | /* A C++ ]AT_NAME_PREFIX[parse that simulates the C signature. */ |
| 53 | int |
| 54 | ]AT_NAME_PREFIX[parse (]AT_PARAM_IF([semantic_value *result, int *count]))[ |
| 55 | { |
| 56 | ]AT_NAME_PREFIX[::parser parser]AT_PARAM_IF([ (result, count)])[; |
| 57 | #if ]AT_API_PREFIX[DEBUG |
| 58 | parser.set_debug_level (1); |
| 59 | #endif |
| 60 | return parser.parse (); |
| 61 | } |
| 62 | ]])[ |
| 63 | |
| 64 | semantic_value global_result = 0; |
| 65 | int global_count = 0; |
| 66 | |
| 67 | /* A C main function. */ |
| 68 | int |
| 69 | main (int argc, const char **argv) |
| 70 | { |
| 71 | semantic_value result = 0; |
| 72 | int count = 0; |
| 73 | int status; |
| 74 | |
| 75 | /* This used to be alarm (10), but that isn't enough time for a July |
| 76 | 1995 vintage DEC Alphastation 200 4/100 system, according to |
| 77 | Nelson H. F. Beebe. 100 seconds was enough for regular users, |
| 78 | but the Hydra build farm, which is heavily loaded needs more. */ |
| 79 | |
| 80 | alarm (200); |
| 81 | |
| 82 | if (argc == 2) |
| 83 | input = fopen (argv[1], "r"); |
| 84 | else |
| 85 | input = stdin; |
| 86 | |
| 87 | if (!input) |
| 88 | { |
| 89 | perror (argv[1]); |
| 90 | return 3; |
| 91 | } |
| 92 | |
| 93 | ]AT_SKEL_CC_IF([], [AT_DEBUG_IF([ ]AT_NAME_PREFIX[debug = 1;])])[ |
| 94 | status = ]AT_NAME_PREFIX[parse (]AT_PARAM_IF([[&result, &count]])[); |
| 95 | if (fclose (input)) |
| 96 | perror ("fclose"); |
| 97 | assert (global_result == result); |
| 98 | assert (global_count == count); |
| 99 | return status; |
| 100 | } |
| 101 | ]]) |
| 102 | |
| 103 | |
| 104 | m4_pushdef([AT_CALC_LEX], |
| 105 | [[#include <ctype.h> |
| 106 | |
| 107 | ]AT_YYLEX_DECLARE_EXTERN[ |
| 108 | static int get_char (]AT_YYLEX_FORMALS[); |
| 109 | static void unget_char (]AT_YYLEX_PRE_FORMALS[ int c); |
| 110 | |
| 111 | ]AT_LOCATION_IF([ |
| 112 | static AT_YYLTYPE last_yylloc; |
| 113 | ])[ |
| 114 | static int |
| 115 | get_char (]AT_YYLEX_FORMALS[) |
| 116 | { |
| 117 | int res = getc (input); |
| 118 | ]AT_USE_LEX_ARGS[; |
| 119 | ]AT_LOCATION_IF([ |
| 120 | last_yylloc = AT_LOC; |
| 121 | if (res == '\n') |
| 122 | { |
| 123 | AT_LOC_LAST_LINE++; |
| 124 | AT_LOC_LAST_COLUMN = 1; |
| 125 | } |
| 126 | else |
| 127 | AT_LOC_LAST_COLUMN++; |
| 128 | ])[ |
| 129 | return res; |
| 130 | } |
| 131 | |
| 132 | static void |
| 133 | unget_char (]AT_YYLEX_PRE_FORMALS[ int c) |
| 134 | { |
| 135 | ]AT_USE_LEX_ARGS[; |
| 136 | ]AT_LOCATION_IF([ |
| 137 | /* Wrong when C == '\n'. */ |
| 138 | AT_LOC = last_yylloc; |
| 139 | ])[ |
| 140 | ungetc (c, input); |
| 141 | } |
| 142 | |
| 143 | static int |
| 144 | read_signed_integer (]AT_YYLEX_FORMALS[) |
| 145 | { |
| 146 | int c = get_char (]AT_YYLEX_ARGS[); |
| 147 | int sign = 1; |
| 148 | int n = 0; |
| 149 | |
| 150 | ]AT_USE_LEX_ARGS[; |
| 151 | if (c == '-') |
| 152 | { |
| 153 | c = get_char (]AT_YYLEX_ARGS[); |
| 154 | sign = -1; |
| 155 | } |
| 156 | |
| 157 | while (isdigit (c)) |
| 158 | { |
| 159 | n = 10 * n + (c - '0'); |
| 160 | c = get_char (]AT_YYLEX_ARGS[); |
| 161 | } |
| 162 | |
| 163 | unget_char (]AT_YYLEX_PRE_ARGS[ c); |
| 164 | |
| 165 | return sign * n; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /*---------------------------------------------------------------. |
| 170 | | Lexical analyzer returns an integer on the stack and the token | |
| 171 | | NUM, or the ASCII character read if not a number. Skips all | |
| 172 | | blanks and tabs, returns 0 for EOF. | |
| 173 | `---------------------------------------------------------------*/ |
| 174 | |
| 175 | ]AT_YYLEX_PROTOTYPE[ |
| 176 | { |
| 177 | int c; |
| 178 | /* Skip current token, then white spaces. */ |
| 179 | do |
| 180 | { |
| 181 | ]AT_LOCATION_IF( |
| 182 | [ AT_LOC_FIRST_COLUMN = AT_LOC_LAST_COLUMN; |
| 183 | AT_LOC_FIRST_LINE = AT_LOC_LAST_LINE; |
| 184 | ])[ |
| 185 | } |
| 186 | while ((c = get_char (]AT_YYLEX_ARGS[)) == ' ' || c == '\t'); |
| 187 | |
| 188 | /* process numbers */ |
| 189 | if (c == '.' || isdigit (c)) |
| 190 | { |
| 191 | unget_char (]AT_YYLEX_PRE_ARGS[ c); |
| 192 | ]AT_VAL[.ival = read_signed_integer (]AT_YYLEX_ARGS[); |
| 193 | return ]AT_TOKEN_PREFIX[NUM; |
| 194 | } |
| 195 | |
| 196 | /* Return end-of-file. */ |
| 197 | if (c == EOF) |
| 198 | return ]AT_TOKEN_PREFIX[CALC_EOF; |
| 199 | |
| 200 | /* Return single chars. */ |
| 201 | return c; |
| 202 | } |
| 203 | ]]) |
| 204 | |
| 205 | AT_DATA_GRAMMAR([calc.y], |
| 206 | [[/* Infix notation calculator--calc */ |
| 207 | ]$4 |
| 208 | AT_SKEL_CC_IF( |
| 209 | [%define global_tokens_and_yystype])[ |
| 210 | %code requires |
| 211 | { |
| 212 | ]AT_LOCATION_TYPE_IF([[ |
| 213 | # include <iostream> |
| 214 | struct Point |
| 215 | { |
| 216 | int l; |
| 217 | int c; |
| 218 | }; |
| 219 | |
| 220 | struct Span |
| 221 | { |
| 222 | Point first; |
| 223 | Point last; |
| 224 | }; |
| 225 | |
| 226 | # define YYLLOC_DEFAULT(Current, Rhs, N) \ |
| 227 | do \ |
| 228 | if (N) \ |
| 229 | { \ |
| 230 | (Current).first = YYRHSLOC (Rhs, 1).first; \ |
| 231 | (Current).last = YYRHSLOC (Rhs, N).last; \ |
| 232 | } \ |
| 233 | else \ |
| 234 | { \ |
| 235 | (Current).first = (Current).last = YYRHSLOC (Rhs, 0).last; \ |
| 236 | } \ |
| 237 | while (false) |
| 238 | |
| 239 | ]])[ |
| 240 | /* Exercise pre-prologue dependency to %union. */ |
| 241 | typedef int semantic_value; |
| 242 | } |
| 243 | |
| 244 | /* Exercise %union. */ |
| 245 | %union |
| 246 | { |
| 247 | semantic_value ival; |
| 248 | }; |
| 249 | %printer { ]AT_SKEL_CC_IF([[yyoutput << $$]], |
| 250 | [[fprintf (yyoutput, "%d", $$)]])[; } <ival>; |
| 251 | |
| 252 | %code provides |
| 253 | { |
| 254 | #include <stdio.h> |
| 255 | /* The input. */ |
| 256 | extern FILE *input; |
| 257 | extern semantic_value global_result; |
| 258 | extern int global_count; |
| 259 | } |
| 260 | |
| 261 | %code |
| 262 | { |
| 263 | #include <assert.h> |
| 264 | #include <string.h> |
| 265 | #define USE(Var) |
| 266 | |
| 267 | FILE *input; |
| 268 | static int power (int base, int exponent); |
| 269 | |
| 270 | ]AT_YYERROR_DECLARE[ |
| 271 | ]AT_YYLEX_DECLARE_EXTERN[ |
| 272 | } |
| 273 | |
| 274 | ]AT_SKEL_CC_IF([AT_LOCATION_TYPE_IF([[ |
| 275 | %initial-action |
| 276 | { |
| 277 | @$.first.l = @$.first.c = 1; |
| 278 | @$.last = @$.first; |
| 279 | }]])])[ |
| 280 | |
| 281 | /* Bison Declarations */ |
| 282 | %token CALC_EOF 0 "end of input" |
| 283 | %token <ival> NUM "number" |
| 284 | %type <ival> exp |
| 285 | |
| 286 | %nonassoc '=' /* comparison */ |
| 287 | %left '-' '+' |
| 288 | %left '*' '/' |
| 289 | %precedence NEG /* negation--unary minus */ |
| 290 | %right '^' /* exponentiation */ |
| 291 | |
| 292 | /* Grammar follows */ |
| 293 | %% |
| 294 | input: |
| 295 | line |
| 296 | | input line { ]AT_PARAM_IF([++*count; ++global_count;])[ } |
| 297 | ; |
| 298 | |
| 299 | line: |
| 300 | '\n' |
| 301 | | exp '\n' { ]AT_PARAM_IF([*result = global_result = $1], [USE ($1)])[; } |
| 302 | ; |
| 303 | |
| 304 | exp: |
| 305 | NUM { $$ = $1; } |
| 306 | | exp '=' exp |
| 307 | { |
| 308 | if ($1 != $3) |
| 309 | fprintf (stderr, "calc: error: %d != %d\n", $1, $3); |
| 310 | $$ = $1; |
| 311 | } |
| 312 | | exp '+' exp { $$ = $1 + $3; } |
| 313 | | exp '-' exp { $$ = $1 - $3; } |
| 314 | | exp '*' exp { $$ = $1 * $3; } |
| 315 | | exp '/' exp { $$ = $1 / $3; } |
| 316 | | '-' exp %prec NEG { $$ = -$2; } |
| 317 | | exp '^' exp { $$ = power ($1, $3); } |
| 318 | | '(' exp ')' { $$ = $2; } |
| 319 | | '(' error ')' { $$ = 1111; yyerrok; } |
| 320 | | '!' { $$ = 0; YYERROR; } |
| 321 | | '-' error { $$ = 0; YYERROR; } |
| 322 | ; |
| 323 | %% |
| 324 | |
| 325 | static int |
| 326 | power (int base, int exponent) |
| 327 | { |
| 328 | int res = 1; |
| 329 | assert (0 <= exponent); |
| 330 | for (/* Niente */; exponent; --exponent) |
| 331 | res *= base; |
| 332 | return res; |
| 333 | } |
| 334 | |
| 335 | ]AT_SKEL_CC_IF( |
| 336 | [AT_LOCATION_TYPE_IF([[ |
| 337 | std::ostream& |
| 338 | operator<< (std::ostream& o, const Span& s) |
| 339 | { |
| 340 | o << s.first.l << '.' << s.first.c; |
| 341 | if (s.first.l != s.last.l) |
| 342 | o << '-' << s.last.l << '.' << s.last.c - 1; |
| 343 | else if (s.first.c != s.last.c - 1) |
| 344 | o << '-' << s.last.c - 1; |
| 345 | return o; |
| 346 | } |
| 347 | ]])])[ |
| 348 | ]AT_YYERROR_DEFINE[ |
| 349 | ]AT_DEFINES_IF([], |
| 350 | [AT_CALC_LEX |
| 351 | AT_CALC_MAIN])]) |
| 352 | |
| 353 | AT_DEFINES_IF([AT_DATA_SOURCE([[calc-lex.c]AT_SKEL_CC_IF([[c]])], |
| 354 | [[#include "calc.h]AT_SKEL_CC_IF([[h]])[" |
| 355 | |
| 356 | ]AT_CALC_LEX]) |
| 357 | AT_DATA_SOURCE([[calc-main.c]AT_SKEL_CC_IF([[c]])], |
| 358 | [[#include "calc.h]AT_SKEL_CC_IF([[h]])[" |
| 359 | |
| 360 | ]AT_CALC_MAIN]) |
| 361 | ]) |
| 362 | m4_popdef([AT_CALC_MAIN]) |
| 363 | m4_popdef([AT_CALC_LEX]) |
| 364 | ])# _AT_DATA_CALC_Y |
| 365 | |
| 366 | |
| 367 | # AT_DATA_CALC_Y([BISON-OPTIONS]) |
| 368 | # ------------------------------- |
| 369 | # Produce 'calc.y' and, if %defines was specified, 'calc-lex.c' or |
| 370 | # 'calc-lex.cc'. |
| 371 | m4_define([AT_DATA_CALC_Y], |
| 372 | [_AT_DATA_CALC_Y($[1], $[2], $[3], [$1]) |
| 373 | ]) |
| 374 | |
| 375 | |
| 376 | |
| 377 | # _AT_CHECK_CALC(BISON-OPTIONS, INPUT, [NUM-STDERR-LINES]) |
| 378 | # -------------------------------------------------------- |
| 379 | # Run 'calc' on INPUT and expect no STDOUT nor STDERR. |
| 380 | # |
| 381 | # If BISON-OPTIONS contains '%debug' but not '%glr-parser', then |
| 382 | # |
| 383 | # NUM-STDERR-LINES is the number of expected lines on stderr. |
| 384 | # Currently this is ignored, though, since the output format is fluctuating. |
| 385 | # |
| 386 | # We don't count GLR's traces yet, since its traces are somewhat |
| 387 | # different from LALR's. |
| 388 | m4_define([_AT_CHECK_CALC], |
| 389 | [AT_DATA([[input]], |
| 390 | [[$2 |
| 391 | ]]) |
| 392 | AT_PARSER_CHECK([./calc input], 0, [], [stderr]) |
| 393 | ]) |
| 394 | |
| 395 | |
| 396 | # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, EXIT-STATUS, INPUT, |
| 397 | # [NUM-STDERR-LINES], |
| 398 | # [VERBOSE-AND-LOCATED-ERROR-MESSAGE]) |
| 399 | # --------------------------------------------------------- |
| 400 | # Run 'calc' on INPUT, and expect a 'syntax error' message. |
| 401 | # |
| 402 | # If INPUT starts with a slash, it is used as absolute input file name, |
| 403 | # otherwise as contents. |
| 404 | # |
| 405 | # NUM-STDERR-LINES is the number of expected lines on stderr. |
| 406 | # Currently this is ignored, though, since the output format is fluctuating. |
| 407 | # |
| 408 | # If BISON-OPTIONS contains '%location', then make sure the ERROR-LOCATION |
| 409 | # is correctly output on stderr. |
| 410 | # |
| 411 | # If BISON-OPTIONS contains '%define parse.error verbose', then make sure the |
| 412 | # IF-YYERROR-VERBOSE message is properly output after 'syntax error, ' |
| 413 | # on STDERR. |
| 414 | # |
| 415 | # If BISON-OPTIONS contains '%debug' but not '%glr', then NUM-STDERR-LINES |
| 416 | # is the number of expected lines on stderr. |
| 417 | m4_define([_AT_CHECK_CALC_ERROR], |
| 418 | [m4_bmatch([$3], [^/], |
| 419 | [AT_PARSER_CHECK([./calc $3], $2, [], [stderr])], |
| 420 | [AT_DATA([[input]], |
| 421 | [[$3 |
| 422 | ]]) |
| 423 | AT_PARSER_CHECK([./calc input], $2, [], [stderr])]) |
| 424 | |
| 425 | # Normalize the observed and expected error messages, depending upon the |
| 426 | # options. |
| 427 | # 1. Remove the traces from observed. |
| 428 | sed '/^Starting/d |
| 429 | /^Entering/d |
| 430 | /^Stack/d |
| 431 | /^Reading/d |
| 432 | /^Reducing/d |
| 433 | /^Return/d |
| 434 | /^Shifting/d |
| 435 | /^state/d |
| 436 | /^Cleanup:/d |
| 437 | /^Error:/d |
| 438 | /^Next/d |
| 439 | /^Now/d |
| 440 | /^Discarding/d |
| 441 | / \$[[0-9$]]* = /d |
| 442 | /^yydestructor:/d' stderr >at-stderr |
| 443 | mv at-stderr stderr |
| 444 | # 2. Create the reference error message. |
| 445 | AT_DATA([[expout]], |
| 446 | [$5 |
| 447 | ]) |
| 448 | # 3. If locations are not used, remove them. |
| 449 | AT_YYERROR_SEES_LOC_IF([], |
| 450 | [[sed 's/^[-0-9.]*: //' expout >at-expout |
| 451 | mv at-expout expout]]) |
| 452 | # 4. If error-verbose is not used, strip the', unexpected....' part. |
| 453 | m4_bmatch([$1], [%define parse.error verbose], [], |
| 454 | [[sed 's/syntax error, .*$/syntax error/' expout >at-expout |
| 455 | mv at-expout expout]]) |
| 456 | # 5. Check |
| 457 | AT_CHECK([cat stderr], 0, [expout]) |
| 458 | ]) |
| 459 | |
| 460 | |
| 461 | # AT_CHECK_SPACES([FILES]) |
| 462 | # ------------------------ |
| 463 | # Make sure we did not introduce bad spaces. Checked here because all |
| 464 | # the skeletons are (or should be) exercized here. |
| 465 | m4_define([AT_CHECK_SPACES], |
| 466 | [AT_CHECK([$PERL -ne ' |
| 467 | chomp; |
| 468 | print "$ARGV:$.: {$_}\n" |
| 469 | if (# No starting/ending empty lines. |
| 470 | (eof || $. == 1) && /^\s*$/ |
| 471 | # No trailing space. |
| 472 | || /\s$/ |
| 473 | # No tabs. |
| 474 | || /\t/ |
| 475 | )' $1 |
| 476 | ])dnl |
| 477 | ]) |
| 478 | |
| 479 | |
| 480 | # AT_CHECK_CALC([BISON-OPTIONS]) |
| 481 | # ------------------------------ |
| 482 | # Start a testing chunk which compiles 'calc' grammar with |
| 483 | # BISON-OPTIONS, and performs several tests over the parser. |
| 484 | m4_define([AT_CHECK_CALC], |
| 485 | [m4_ifval([$2], [m4_fatal([$0: expected a single argument])]) |
| 486 | |
| 487 | # We use integers to avoid dependencies upon the precision of doubles. |
| 488 | AT_SETUP([Calculator $1]) |
| 489 | |
| 490 | AT_BISON_OPTION_PUSHDEFS([$1]) |
| 491 | |
| 492 | AT_DATA_CALC_Y([$1]) |
| 493 | AT_FULL_COMPILE([calc], AT_DEFINES_IF([[lex], [main]])) |
| 494 | AT_CHECK_SPACES(m4_join([ ], |
| 495 | [calc.AT_SKEL_CC_IF([cc], [c])], |
| 496 | [AT_DEFINES_IF([calc.AT_SKEL_CC_IF([hh], [h])])])) |
| 497 | |
| 498 | # Test the priorities. |
| 499 | _AT_CHECK_CALC([$1], |
| 500 | [1 + 2 * 3 = 7 |
| 501 | 1 + 2 * -3 = -5 |
| 502 | |
| 503 | -1^2 = -1 |
| 504 | (-1)^2 = 1 |
| 505 | |
| 506 | ---1 = -1 |
| 507 | |
| 508 | 1 - 2 - 3 = -4 |
| 509 | 1 - (2 - 3) = 2 |
| 510 | |
| 511 | 2^2^3 = 256 |
| 512 | (2^2)^3 = 64], |
| 513 | [842]) |
| 514 | |
| 515 | # Some syntax errors. |
| 516 | _AT_CHECK_CALC_ERROR([$1], [1], [1 2], [15], |
| 517 | [1.3: syntax error, unexpected number]) |
| 518 | _AT_CHECK_CALC_ERROR([$1], [1], [1//2], [20], |
| 519 | [1.3: syntax error, unexpected '/', expecting number or '-' or '(' or '!']) |
| 520 | _AT_CHECK_CALC_ERROR([$1], [1], [error], [5], |
| 521 | [1.1: syntax error, unexpected $undefined]) |
| 522 | _AT_CHECK_CALC_ERROR([$1], [1], [1 = 2 = 3], [30], |
| 523 | [1.7: syntax error, unexpected '=']) |
| 524 | _AT_CHECK_CALC_ERROR([$1], [1], |
| 525 | [ |
| 526 | +1], |
| 527 | [20], |
| 528 | [2.1: syntax error, unexpected '+']) |
| 529 | # Exercise error messages with EOF: work on an empty file. |
| 530 | _AT_CHECK_CALC_ERROR([$1], [1], [/dev/null], [4], |
| 531 | [1.1: syntax error, unexpected end of input]) |
| 532 | |
| 533 | # Exercise the error token: without it, we die at the first error, |
| 534 | # hence be sure to |
| 535 | # |
| 536 | # - have several errors which exercise different shift/discardings |
| 537 | # - (): nothing to pop, nothing to discard |
| 538 | # - (1 + 1 + 1 +): a lot to pop, nothing to discard |
| 539 | # - (* * *): nothing to pop, a lot to discard |
| 540 | # - (1 + 2 * *): some to pop and discard |
| 541 | # |
| 542 | # - test the action associated to 'error' |
| 543 | # |
| 544 | # - check the lookahead that triggers an error is not discarded |
| 545 | # when we enter error recovery. Below, the lookahead causing the |
| 546 | # first error is ")", which is needed to recover from the error and |
| 547 | # produce the "0" that triggers the "0 != 1" error. |
| 548 | # |
| 549 | _AT_CHECK_CALC_ERROR([$1], [0], |
| 550 | [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1], |
| 551 | [250], |
| 552 | [1.2: syntax error, unexpected ')', expecting number or '-' or '(' or '!' |
| 553 | 1.18: syntax error, unexpected ')', expecting number or '-' or '(' or '!' |
| 554 | 1.23: syntax error, unexpected '*', expecting number or '-' or '(' or '!' |
| 555 | 1.41: syntax error, unexpected '*', expecting number or '-' or '(' or '!' |
| 556 | calc: error: 4444 != 1]) |
| 557 | |
| 558 | # The same, but this time exercising explicitly triggered syntax errors. |
| 559 | # POSIX says the lookahead causing the error should not be discarded. |
| 560 | _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (1 2) = 1], [102], |
| 561 | [1.10: syntax error, unexpected number |
| 562 | calc: error: 2222 != 1]) |
| 563 | _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (1 2) = 1], [113], |
| 564 | [1.4: syntax error, unexpected '*', expecting number or '-' or '(' or '!' |
| 565 | 1.12: syntax error, unexpected number |
| 566 | calc: error: 2222 != 1]) |
| 567 | |
| 568 | # Check that yyerrok works properly: second error is not reported, |
| 569 | # third and fourth are. Parse status is succesfull. |
| 570 | _AT_CHECK_CALC_ERROR([$1], [0], [(* *) + (*) + (*)], [113], |
| 571 | [1.2: syntax error, unexpected '*', expecting number or '-' or '(' or '!' |
| 572 | 1.10: syntax error, unexpected '*', expecting number or '-' or '(' or '!' |
| 573 | 1.16: syntax error, unexpected '*', expecting number or '-' or '(' or '!']) |
| 574 | |
| 575 | AT_BISON_OPTION_POPDEFS |
| 576 | |
| 577 | AT_CLEANUP |
| 578 | ])# AT_CHECK_CALC |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | # ------------------------ # |
| 584 | # Simple LALR Calculator. # |
| 585 | # ------------------------ # |
| 586 | |
| 587 | AT_BANNER([[Simple LALR(1) Calculator.]]) |
| 588 | |
| 589 | # AT_CHECK_CALC_LALR([BISON-OPTIONS]) |
| 590 | # ----------------------------------- |
| 591 | # Start a testing chunk which compiles 'calc' grammar with |
| 592 | # BISON-OPTIONS, and performs several tests over the parser. |
| 593 | m4_define([AT_CHECK_CALC_LALR], |
| 594 | [AT_CHECK_CALC($@)]) |
| 595 | |
| 596 | AT_CHECK_CALC_LALR() |
| 597 | |
| 598 | AT_CHECK_CALC_LALR([%defines]) |
| 599 | AT_CHECK_CALC_LALR([%locations]) |
| 600 | |
| 601 | AT_CHECK_CALC_LALR([%name-prefix "calc"]) |
| 602 | AT_CHECK_CALC_LALR([%verbose]) |
| 603 | AT_CHECK_CALC_LALR([%yacc]) |
| 604 | AT_CHECK_CALC_LALR([%define parse.error verbose]) |
| 605 | |
| 606 | AT_CHECK_CALC_LALR([%define api.pure full %locations]) |
| 607 | AT_CHECK_CALC_LALR([%define api.push-pull both %define api.pure full %locations]) |
| 608 | AT_CHECK_CALC_LALR([%define parse.error verbose %locations]) |
| 609 | |
| 610 | AT_CHECK_CALC_LALR([%define parse.error verbose %locations %defines %define api.prefix "calc" %verbose %yacc]) |
| 611 | AT_CHECK_CALC_LALR([%define parse.error verbose %locations %defines %name-prefix "calc" %define api.token.prefix {TOK_} %verbose %yacc]) |
| 612 | |
| 613 | AT_CHECK_CALC_LALR([%debug]) |
| 614 | AT_CHECK_CALC_LALR([%define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) |
| 615 | AT_CHECK_CALC_LALR([%define parse.error verbose %debug %locations %defines %define api.prefix "calc" %verbose %yacc]) |
| 616 | |
| 617 | AT_CHECK_CALC_LALR([%define api.pure full %define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) |
| 618 | AT_CHECK_CALC_LALR([%define api.push-pull both %define api.pure full %define parse.error verbose %debug %locations %defines %define api.prefix "calc" %verbose %yacc]) |
| 619 | |
| 620 | AT_CHECK_CALC_LALR([%define api.pure %define parse.error verbose %debug %locations %defines %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |
| 621 | |
| 622 | |
| 623 | # ----------------------- # |
| 624 | # Simple GLR Calculator. # |
| 625 | # ----------------------- # |
| 626 | |
| 627 | AT_BANNER([[Simple GLR Calculator.]]) |
| 628 | |
| 629 | # AT_CHECK_CALC_GLR([BISON-OPTIONS]) |
| 630 | # ---------------------------------- |
| 631 | # Start a testing chunk which compiles 'calc' grammar with |
| 632 | # BISON-OPTIONS and %glr-parser, and performs several tests over the parser. |
| 633 | m4_define([AT_CHECK_CALC_GLR], |
| 634 | [AT_CHECK_CALC([%glr-parser] $@)]) |
| 635 | |
| 636 | |
| 637 | AT_CHECK_CALC_GLR() |
| 638 | |
| 639 | AT_CHECK_CALC_GLR([%defines]) |
| 640 | AT_CHECK_CALC_GLR([%locations]) |
| 641 | AT_CHECK_CALC_GLR([%name-prefix "calc"]) |
| 642 | AT_CHECK_CALC_GLR([%define api.prefix "calc"]) |
| 643 | AT_CHECK_CALC_GLR([%verbose]) |
| 644 | AT_CHECK_CALC_GLR([%yacc]) |
| 645 | AT_CHECK_CALC_GLR([%define parse.error verbose]) |
| 646 | |
| 647 | AT_CHECK_CALC_GLR([%define api.pure %locations]) |
| 648 | AT_CHECK_CALC_GLR([%define parse.error verbose %locations]) |
| 649 | |
| 650 | AT_CHECK_CALC_GLR([%define parse.error verbose %locations %defines %name-prefix "calc" %verbose %yacc]) |
| 651 | |
| 652 | AT_CHECK_CALC_GLR([%debug]) |
| 653 | AT_CHECK_CALC_GLR([%define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) |
| 654 | AT_CHECK_CALC_GLR([%define parse.error verbose %debug %locations %defines %define api.prefix "calc" %define api.token.prefix {TOK_} %verbose %yacc]) |
| 655 | |
| 656 | AT_CHECK_CALC_GLR([%define api.pure %define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc]) |
| 657 | |
| 658 | AT_CHECK_CALC_GLR([%define api.pure %define parse.error verbose %debug %locations %defines %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |
| 659 | AT_CHECK_CALC_GLR([%define api.pure %define parse.error verbose %debug %locations %defines %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |
| 660 | |
| 661 | |
| 662 | # ----------------------------- # |
| 663 | # Simple LALR1 C++ Calculator. # |
| 664 | # ----------------------------- # |
| 665 | |
| 666 | AT_BANNER([[Simple LALR(1) C++ Calculator.]]) |
| 667 | |
| 668 | # First let's try using %skeleton |
| 669 | AT_CHECK_CALC([%skeleton "lalr1.cc" %defines]) |
| 670 | |
| 671 | # AT_CHECK_CALC_LALR1_CC([BISON-OPTIONS]) |
| 672 | # --------------------------------------- |
| 673 | # Start a testing chunk which compiles 'calc' grammar with |
| 674 | # the C++ skeleton, and performs several tests over the parser. |
| 675 | m4_define([AT_CHECK_CALC_LALR1_CC], |
| 676 | [AT_CHECK_CALC([%language "C++"] $@)]) |
| 677 | |
| 678 | AT_CHECK_CALC_LALR1_CC([]) |
| 679 | AT_CHECK_CALC_LALR1_CC([%locations]) |
| 680 | AT_CHECK_CALC_LALR1_CC([%locations %define api.location.type Span]) |
| 681 | AT_CHECK_CALC_LALR1_CC([%defines %locations %define parse.error verbose %name-prefix "calc" %verbose %yacc]) |
| 682 | |
| 683 | AT_CHECK_CALC_LALR1_CC([%locations %define parse.error verbose %define api.prefix "calc" %verbose %yacc]) |
| 684 | AT_CHECK_CALC_LALR1_CC([%locations %define parse.error verbose %debug %name-prefix "calc" %verbose %yacc]) |
| 685 | |
| 686 | AT_CHECK_CALC_LALR1_CC([%locations %pure-parser %define parse.error verbose %debug %define api.prefix "calc" %verbose %yacc]) |
| 687 | AT_CHECK_CALC_LALR1_CC([%locations %pure-parser %define parse.error verbose %debug %define api.prefix "calc" %define api.token.prefix {TOK_} %verbose %yacc]) |
| 688 | |
| 689 | AT_CHECK_CALC_LALR1_CC([%defines %locations %pure-parser %define parse.error verbose %debug %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |
| 690 | |
| 691 | AT_CHECK_CALC_LALR1_CC([%pure-parser %define parse.error verbose %debug %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |
| 692 | AT_CHECK_CALC_LALR1_CC([%defines %locations %pure-parser %define parse.error verbose %debug %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |
| 693 | |
| 694 | |
| 695 | |
| 696 | # --------------------------- # |
| 697 | # Simple GLR C++ Calculator. # |
| 698 | # --------------------------- # |
| 699 | |
| 700 | AT_BANNER([[Simple GLR C++ Calculator.]]) |
| 701 | |
| 702 | # Again, we try also using %skeleton. |
| 703 | AT_CHECK_CALC([%skeleton "glr.cc"]) |
| 704 | |
| 705 | # AT_CHECK_CALC_GLR_CC([BISON-OPTIONS]) |
| 706 | # ------------------------------------- |
| 707 | # Start a testing chunk which compiles 'calc' grammar with |
| 708 | # the GLR C++ skeleton, and performs several tests over the parser. |
| 709 | m4_define([AT_CHECK_CALC_GLR_CC], |
| 710 | [AT_CHECK_CALC([%language "C++" %glr-parser] $@)]) |
| 711 | |
| 712 | AT_CHECK_CALC_GLR_CC([]) |
| 713 | AT_CHECK_CALC_GLR_CC([%locations]) |
| 714 | AT_CHECK_CALC_GLR_CC([%locations %define api.location.type Span]) |
| 715 | AT_CHECK_CALC_GLR_CC([%defines %define parse.error verbose %name-prefix "calc" %verbose %yacc]) |
| 716 | AT_CHECK_CALC_GLR_CC([%define parse.error verbose %define api.prefix "calc" %verbose %yacc]) |
| 717 | |
| 718 | AT_CHECK_CALC_GLR_CC([%debug]) |
| 719 | AT_CHECK_CALC_GLR_CC([%define parse.error verbose %debug %name-prefix "calc" %verbose %yacc]) |
| 720 | |
| 721 | AT_CHECK_CALC_GLR_CC([%pure-parser %define parse.error verbose %debug %name-prefix "calc" %verbose %yacc]) |
| 722 | AT_CHECK_CALC_GLR_CC([%pure-parser %define parse.error verbose %debug %name-prefix "calc" %define api.token.prefix {TOK_} %verbose %yacc]) |
| 723 | |
| 724 | AT_CHECK_CALC_GLR_CC([%locations %defines %pure-parser %define parse.error verbose %debug %name-prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |
| 725 | AT_CHECK_CALC_GLR_CC([%locations %defines %pure-parser %define parse.error verbose %debug %define api.prefix "calc" %verbose %yacc %parse-param {semantic_value *result} %parse-param {int *count}]) |