aboutsummaryrefslogtreecommitdiffstats
path: root/src/infix_calc_parser.yy
blob: 6b61fcf62678b48f76081a23e6fbb9a5e5573799 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 1993-2023 Gothel Software e.K.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
// Grammar input for Bison >= 3.2
%require "3.2"
%language "c++"

%locations // produce infix_calc_yy::location type for more accurate location @ error

%define lr.type lalr // default, smallest table and fastest

%define api.prefix {infix_calc_yy} // namespace
%define api.parser.class {parser}
// %define api.pure full // default w/ C++
%define api.token.raw // use scanner token values, no lookup
%define api.token.constructor // use complete symbols
%define api.token.prefix {TOK_}
%define api.value.type variant // use complete symbols
// %define api.value.automove // use rvalue std::move -> we use it manually

%parse-param { std::function<infix_calc_yy::parser::symbol_type(infix_calc::compiler&)> yylex } // for parser-ctor only
%param { infix_calc::compiler& cc} // append: parser-ctor and yylex-call

// %define parse.assert // requires RTTI

// Lookahead Correction (LAC)
// %define parse.lac full 
%define parse.lac none // default, faster, LAC not required on this simple grammar
%define parse.error detailed

// Debugging:
// %define parse.trace

%code requires {
  // # define YYDEBUG 1
  # include <string>
  # include <functional>
  # include <infix_calc/infix_calc.hpp>
  using namespace infix_calc;
}

%code {
  // location: parser implementation file after the usual contents of the parser header file
  # include "infix_calc_parser.hpp"  
}

%printer { yyo << $$; } <*>;

%token <double> UREAL
%token <std::string> IDENTIFIER
%token <std::string> ERROR

%nterm <double> real_number
%nterm <rpn_token_t> unary_func

%token LPAREN RPAREN EOL

%left SUB ADD
%left MUL DIV
%left SIN COS TAN ARCSIN ARCCOS ARCTAN
%left POW LOG LOG10 EXP
%left SQRT
%left NEG

%%

expression    : product
              | expression ADD product { cc.put_rpn(rpn_token_t::ADD); }
              | expression SUB product { cc.put_rpn(rpn_token_t::SUB); }
              ;

product     : operand
              | product MUL operand { cc.put_rpn(rpn_token_t::MUL); }
              | product DIV operand { cc.put_rpn(rpn_token_t::DIV); }
              | product POW operand { cc.put_rpn(rpn_token_t::POW); }
              ;

operand     : IDENTIFIER { cc.put_rpn(std::move($1)); }
              | ADD IDENTIFIER  { cc.put_rpn(std::move($2)); }
              | SUB IDENTIFIER  { cc.put_rpn(std::move($2));
                                  cc.put_rpn(rpn_token_t::NEG);
                                }
              | LPAREN expression RPAREN
              | unary_func LPAREN expression RPAREN { cc.put_rpn($1); }
              | real_number { cc.put_rpn($1); }
              ;

unary_func  : SIN { $$=rpn_token_t::SIN; } |
              COS { $$=rpn_token_t::COS; } |
              TAN { $$=rpn_token_t::TAN; } |
              ARCSIN { $$=rpn_token_t::ARCSIN; } |
              ARCCOS { $$=rpn_token_t::ARCCOS; } |
              ARCTAN { $$=rpn_token_t::ARCTAN; } |
              LOG { $$=rpn_token_t::LOG; } |
              LOG10 { $$=rpn_token_t::LOG10; } |
              EXP { $$=rpn_token_t::EXP; } |
              SQRT { $$=rpn_token_t::SQRT; } |
              NEG  { $$=rpn_token_t::NEG; } ;

real_number : UREAL { $$=$1; } |
              ADD UREAL { $$=$2; } |
              SUB UREAL { $$=$2*(-1); } ;

%%

void
infix_calc_yy::parser::error (const location_type& l, const std::string& m)
{
  std::cerr << l << ": " << m << std::endl;
}