Warning, /maui/brun/src/mathengine/parser.yy is written in an unsupported language. File is not indexed.
0001 /*
0002 * This file is part of Kalk
0003 *
0004 * Copyright (C) 2020 Han Young <hanyoung@protonmail.com>
0005 *
0006 * $BEGIN_LICENSE:GPL3+$
0007 *
0008 * This program is free software: you can redistribute it and/or modify
0009 * it under the terms of the GNU General Public License as published by
0010 * the Free Software Foundation, either version 3 of the License, or
0011 * (at your option) any later version.
0012 *
0013 * This program is distributed in the hope that it will be useful,
0014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0016 * GNU General Public License for more details.
0017 *
0018 * You should have received a copy of the GNU General Public License
0019 * along with this program. If not, see <http://www.gnu.org/licenses/>.
0020 *
0021 * $END_LICENSE$
0022 */
0023 %skeleton "lalr1.cc" // -*- C++ -*-
0024 %require "3.0"
0025 %defines
0026
0027 %define api.token.raw
0028
0029 %define api.token.constructor
0030 %define api.value.type variant
0031 %define parse.assert
0032
0033 %code requires {
0034 # include <string>
0035 # include <knumber.h>
0036 class driver;
0037 }
0038
0039 // The parsing context.
0040 %param { driver& drv }
0041
0042 %locations
0043
0044 %define parse.trace
0045 %define parse.error verbose
0046 %define parse.lac full
0047
0048 %code {
0049 # include "driver.hh"
0050 }
0051
0052 %define api.token.prefix {TOK_}
0053 %token
0054 MINUS "-"
0055 PLUS "+"
0056 STAR "*"
0057 SLASH "/"
0058 LPAREN "("
0059 RPAREN ")"
0060 POWER "^"
0061 SIN "SIN"
0062 COS "COS"
0063 TAN "TAN"
0064 LOG "LOG"
0065 LOG10 "LOG10"
0066 LOG2 "LOG2"
0067 SQUAREROOT "SQUAREROOT"
0068 PERCENTAGE "%"
0069 ASIN "ASIN"
0070 ACOS "ACOS"
0071 ATAN "ATAN"
0072 ABS "ABS"
0073 ;
0074
0075 %token <KNumber> NUMBER "number"
0076 %nterm <KNumber> exp
0077 %nterm <KNumber> factor
0078
0079 %printer { yyo << $$.toQString().toStdString(); } <*>;
0080
0081 %%
0082 %start unit;
0083 unit: exp { drv.result = $1; };
0084
0085 %left "+" "-";
0086 %left "*" "/";
0087 exp:
0088 factor
0089 | exp "+" exp { $$ = $1 + $3; }
0090 | exp "-" exp { $$ = $1 - $3; }
0091 | exp exp { $$ = $1 * $2; }
0092 | exp "*" exp { $$ = $1 * $3; }
0093 | exp "/" exp { $$ = $1 / $3; }
0094 | exp "^" factor { $$ = pow($1, $3); }
0095 | exp "^" { $$ = $1; }
0096 | "SIN" "(" exp { $$ = sin($3); }
0097 | "SIN" "(" exp ")" { $$ = sin($3); }
0098 | "COS" "(" exp { $$ = cos($3); }
0099 | "COS" "(" exp ")" { $$ = cos($3); }
0100 | "TAN" "(" exp { $$ = tan($3); }
0101 | "TAN" "(" exp ")" { $$ = tan($3); }
0102 | "LOG" "(" exp { $$ = ln($3); }
0103 | "LOG" "(" exp ")" { $$ = ln($3); }
0104 | "LOG10" "(" exp { $$ = log10($3); }
0105 | "LOG10" "(" exp ")" { $$ = log10($3); }
0106 | "LOG2" "(" exp { $$ = log2($3); }
0107 | "LOG2" "(" exp ")" { $$ = log2($3); }
0108 | "SQUAREROOT" "(" exp { $$ = sqrt($3); }
0109 | "SQUAREROOT" "(" exp ")" { $$ = sqrt($3); }
0110 | "ASIN" "(" exp { $$ = asin($3); }
0111 | "ASIN" "(" exp ")" { $$ = asin($3); }
0112 | "ACOS" "(" exp { $$ = acos($3); }
0113 | "ACOS" "(" exp ")" { $$ = acos($3); }
0114 | "ATAN" "(" exp { $$ = atan($3); }
0115 | "ATAN" "(" exp ")" { $$ = atan($3); }
0116 | "ABS" "(" exp { $$ = abs($3); }
0117 | "ABS" "(" exp ")" { $$ = abs($3);}
0118 ;
0119
0120 factor: "(" exp ")" { $$ = $2; }
0121 | "(" exp { $$ = $2; }
0122 | "number"
0123 | "-" "number" { $$ = -$2; }
0124 | "+" "number" { $$ = $2; }
0125 | factor "%" { $$ = $1 / KNumber(100); }
0126 ;
0127
0128 %%
0129
0130 void
0131 yy::parser::error (const location_type& l, const std::string& m)
0132 {
0133 std::cerr << l << ": " << m << '\n';
0134 if(m != "syntax error, unexpected end of file")
0135 drv.syntaxError = true;
0136 }