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