📄 calc.c
字号:
/* A Bison parser, made from calc.y by GNU bison 1.34. */#define YYBISON 1 /* Identify Bison output. */# define EOS 257# define BAD 258# define HELP 259# define HEX 260# define DECIMAL 261# define QUIT 262# define ABS 263# define BIN 264# define FIB 265# define GCD 266# define KRON 267# define LCM 268# define LUCNUM 269# define NEXTPRIME 270# define POWM 271# define ROOT 272# define SQRT 273# define NUMBER 274# define VARIABLE 275# define LOR 276# define LAND 277# define EQ 278# define NE 279# define LE 280# define GE 281# define LSHIFT 282# define RSHIFT 283# define UMINUS 284#line 1 "calc.y"/* A simple integer desk calculator using yacc and gmp.Copyright 2000, 2001, 2002 Free Software Foundation, Inc.This file is part of the GNU MP Library.This program is free software; you can redistribute it and/or modify it underthe terms of the GNU General Public License as published by the Free SoftwareFoundation; either version 2 of the License, or (at your option) any laterversion.This program is distributed in the hope that it will be useful, but WITHOUT ANYWARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR APARTICULAR PURPOSE. See the GNU General Public License for more details.You should have received a copy of the GNU General Public License along withthis program; if not, write to the Free Software Foundation, Inc., 59 TemplePlace - Suite 330, Boston, MA 02111-1307, USA. *//* This is a simple program, meant only to show one way to use GMP for this sort of thing. There's few features, and error checking is minimal. Standard input is read, calc_help() below shows the inputs accepted. Expressions are evaluated as they're read. If user defined functions were wanted it'd be necessary to build a parse tree like pexpr.c does, or a list of operations for a stack based evaluator. That would also make it possible to detect and optimize evaluations "mod m" like pexpr.c does. A stack is used for intermediate values in the expression evaluation, separate from the yacc parser stack. This is simple, makes error recovery easy, minimizes the junk around mpz calls in the rules, and saves initializing or clearing "mpz_t"s during a calculation. A disadvantage though is that variables must be copied to the stack to be worked on. A more sophisticated calculator or language system might be able to avoid that when executing a compiled or semi-compiled form. Avoiding repeated initializing and clearing of "mpz_t"s is important. In this program the time spent parsing is obviously much greater than any possible saving from this, but a proper calculator or language should take some trouble over it. Don't be surprised if an init/clear takes 3 or more times as long as a 10 limb addition, depending on the system (see the mpz_init_realloc_clear example in tune/README). */#include <stdio.h>#include <stdlib.h>#include <string.h>#include "gmp.h"#define NO_CALC_H /* because it conflicts with normal calc.c stuff */#include "calc-common.h"#define numberof(x) (sizeof (x) / sizeof ((x)[0]))voidcalc_help (void){ printf ("Examples:\n"); printf (" 2+3*4 expressions are evaluated\n"); printf (" x=5^6 variables a to z can be set and used\n"); printf ("Operators:\n"); printf (" + - * arithmetic\n"); printf (" / %% division and remainder (rounding towards negative infinity)\n"); printf (" ^ exponentiation\n"); printf (" ! factorial\n"); printf (" << >> left and right shifts\n"); printf (" <= >= > \\ comparisons, giving 1 if true, 0 if false\n"); printf (" == != < /\n"); printf (" && || logical and/or, giving 1 if true, 0 if false\n"); printf ("Functions:\n"); printf (" abs(n) absolute value\n"); printf (" bin(n,m) binomial coefficient\n"); printf (" fib(n) fibonacci number\n"); printf (" gcd(a,b,..) greatest common divisor\n"); printf (" kron(a,b) kronecker symbol\n"); printf (" lcm(a,b,..) least common multiple\n"); printf (" lucnum(n) lucas number\n"); printf (" nextprime(n) next prime after n\n"); printf (" powm(b,e,m) modulo powering, b^e%%m\n"); printf (" root(n,r) r-th root\n"); printf (" sqrt(n) square root\n"); printf ("Other:\n"); printf (" hex \\ set hex or decimal for input and output\n"); printf (" decimal / (\"0x\" can be used for hex too)\n"); printf (" quit exit program (EOF works too)\n"); printf (" ; statements are separated with a ; or newline\n"); printf (" \\ continue expressions with \\ before newline\n"); printf (" # xxx comments are # though to newline\n"); printf ("Hex numbers must be entered in upper case, to distinguish them from the\n"); printf ("variables a to f (like in bc).\n");}int ibase = 0;int obase = 10;/* The stack is a fixed size, which means there's a limit on the nesting allowed in expressions. A more sophisticated program could let it grow dynamically. */mpz_t stack[100];mpz_ptr sp = stack[0];#define CHECK_OVERFLOW() \ if (sp >= stack[numberof(stack)]) \ { \ fprintf (stderr, \ "Value stack overflow, too much nesting in expression\n"); \ YYERROR; \ }#define CHECK_EMPTY() \ if (sp != stack[0]) \ { \ fprintf (stderr, "Oops, expected the value stack to be empty\n"); \ sp = stack[0]; \ }mpz_t variable[26];#define CHECK_VARIABLE(var) \ if ((var) < 0 || (var) >= numberof (variable)) \ { \ fprintf (stderr, "Oops, bad variable somehow: %d\n", var); \ YYERROR; \ }#define CHECK_UI(name,z) \ if (! mpz_fits_ulong_p (z)) \ { \ fprintf (stderr, "%s too big\n", name); \ YYERROR; \ }#line 143 "calc.y"#ifndef YYSTYPEtypedef union { char *str; int var;} yystype;# define YYSTYPE yystype#endif#ifndef YYDEBUG# define YYDEBUG 0#endif#define YYFINAL 118#define YYFLAG -32768#define YYNTBASE 44/* YYTRANSLATE(YYLEX) -- Bison token number corresponding to YYLEX. */#define YYTRANSLATE(x) ((unsigned)(x) <= 284 ? yytranslate[x] : 50)/* YYTRANSLATE[YYLEX] -- Bison token number corresponding to YYLEX. */static const char yytranslate[] ={ 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 39, 2, 2, 2, 36, 2, 2, 41, 42, 34, 32, 43, 33, 2, 35, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 24, 40, 25, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 38, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 26, 27, 28, 29, 30, 31, 37};#if YYDEBUGstatic const short yyprhs[] ={ 0, 0, 2, 5, 8, 12, 15, 16, 18, 22, 24, 26, 28, 30, 34, 38, 42, 46, 50, 54, 58, 62, 66, 69, 72, 76, 80, 84, 88, 92, 96, 100, 104, 109, 116, 121, 126, 133, 138, 143, 148, 157, 164, 169, 171, 173, 175, 179, 181};static const short yyrhs[] ={ 46, 0, 45, 46, 0, 46, 3, 0, 45, 46, 3, 0, 1, 3, 0, 0, 47, 0, 21, 40, 47, 0, 5, 0, 6, 0, 7, 0, 8, 0, 41, 47, 42, 0, 47, 32, 47, 0, 47, 33, 47, 0, 47, 34, 47, 0, 47, 35, 47, 0, 47, 36, 47, 0, 47, 38, 47, 0, 47, 30, 47, 0, 47, 31, 47, 0, 47, 39, 0, 33, 47, 0, 47, 24, 47, 0, 47, 28, 47, 0, 47, 26, 47, 0, 47, 27, 47, 0, 47, 29, 47, 0, 47, 25, 47, 0, 47, 23, 47, 0, 47, 22, 47, 0, 9, 41, 47, 42, 0, 10, 41, 47, 43, 47, 42, 0, 11, 41, 47, 42, 0, 12, 41, 48, 42, 0, 13, 41, 47, 43, 47, 42, 0, 14, 41, 49, 42, 0, 15, 41, 47, 42, 0, 16, 41, 47, 42, 0, 17, 41, 47, 43, 47, 43, 47, 42, 0, 18, 41, 47, 43, 47, 42, 0, 19, 41, 47, 42, 0, 21, 0, 20, 0, 47, 0, 48, 43, 47, 0, 47, 0, 49, 43, 47, 0};#endif#if YYDEBUG/* YYRLINE[YYN] -- source line where rule number YYN was defined. */static const short yyrline[] ={ 0, 167, 169, 171, 173, 174, 176, 178, 183, 189, 190, 191, 192, 197, 199, 200, 201, 202, 203, 204, 206, 208, 210, 212, 214, 215, 216, 217, 218, 219, 221, 222, 224, 225, 227, 229, 230, 232, 233, 235, 236, 237, 239, 241, 247, 257, 259, 261, 263};#endif#if (YYDEBUG) || defined YYERROR_VERBOSE/* YYTNAME[TOKEN_NUM] -- String name of the token TOKEN_NUM. */static const char *const yytname[] ={ "$", "error", "$undefined.", "EOS", "BAD", "HELP", "HEX", "DECIMAL", "QUIT", "ABS", "BIN", "FIB", "GCD", "KRON", "LCM", "LUCNUM", "NEXTPRIME", "POWM", "ROOT", "SQRT", "NUMBER", "VARIABLE", "LOR", "LAND", "'<'", "'>'", "EQ", "NE", "LE", "GE", "LSHIFT", "RSHIFT", "'+'", "'-'", "'*'", "'/'", "'%'", "UMINUS", "'^'", "'!'", "'='", "'('", "')'", "','", "top", "statements", "statement", "e", "gcdlist", "lcmlist", 0};#endif/* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */static const short yyr1[] ={ 0, 44, 44, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 49, 49};/* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */static const short yyr2[] ={ 0, 1, 2, 2, 3, 2, 0, 1, 3, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 6, 4, 4, 6, 4, 4, 4, 8, 6, 4, 1, 1, 1, 3, 1, 3};/* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE doesn't specify something else to do. Zero means the default is an error. */static const short yydefact[] ={ 0, 0, 9, 10, 11, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 44, 43, 0, 0, 6, 1, 7, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 43, 23, 0, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 0, 0, 0, 45, 0, 0, 47, 0, 0, 0, 0, 0, 0, 8, 13, 4, 31, 30, 24, 29, 26, 27, 25, 28, 20, 21, 14, 15, 16, 17, 18, 19, 32, 0, 34, 35, 0, 0, 37, 0, 38, 39, 0, 0, 42, 0, 46, 0, 48, 0, 0, 33, 36, 0, 41, 0, 40, 0, 0, 0};static const short yydefgoto[] ={ 116, 21, 22, 23, 63, 66};static const short yypact[] ={ 39, 17,-32768,-32768,-32768,-32768, -20, 0, 2, 25, 28, 30, 33, 34, 37, 40, 46,-32768, -18, 122, 122, 89, 67, 462,-32768, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122,-32768, -36, 252, 87,-32768, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122,-32768, 273, 142, 294, 462, -38, 164, 462, -24, 315, 336, 186, 208, 357, 462,-32768,-32768, 479, 495, 511, 511, 511, 511, 511, 511, 29, 29, 50, 50, -36, -36, -36, -36,-32768, 122,-32768,-32768, 122, 122,-32768, 122,-32768, -32768, 122, 122,-32768, 378, 462, 399, 462, 230, 420, -32768,-32768, 122,-32768, 441,-32768, 91, 92,-32768};static const short yypgoto[] ={ -32768,-32768, 90, -19,-32768,-32768};#define YYLAST 550static const short yytable[] ={ 38, 39, 57, 58, 94, 95, 59, 60, 61, 62, 64, 65, 67, 68, 69, 70, 71, 72, 97, 98, 24, 25, 36, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, -6, 1, 26, -6, 27, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 52, 53, 54, 55, 56, 28, 57, 58, 29, 41, 30, 19, 104, 31, 32, 105, 106, 33, 107, 20, 34, 108, 109, 54, 55, 56, 35, 57, 58, 74, 117, 118, 114, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 0, 0, 0, 0, 20, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 37, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 0, 0, 0, 0, 20, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, 0, 0, 0, 92, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, 0, 0, 0, 96, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, 0, 0, 0, 101, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, 0, 0, 0, 102, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, 0, 0, 0, 112, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, 0, 0, 73, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, 0, 0, 91, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, 0, 0, 93, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, 0, 0, 99, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, 0, 0, 100, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, 0, 0, 103, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, 0, 0, 110, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, 0, 0, 111, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, 0, 0, 113, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, 0, 0, 115, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 0, 57, 58, 44,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -