mupp 1.1.0
Loading...
Searching...
No Matches
PExpressionDef.hpp
Go to the documentation of this file.
1/***************************************************************************
2
3 PExpressionDef.hpp
4
5 Author: Andreas Suter
6 e-mail: andreas.suter@psi.ch
7
8 Based on Joel de Guzman example on calc7,
9 see https://github.com/boostorg/spirit
10
11 This file contains the implementation (definition) of the PExpression
12 grammar. It defines the actual grammar rules and their semantic
13 actions using Boost.Spirit X3.
14
15 The grammar implements expression parsing with proper operator precedence:
16 - Primary expressions (literals, variables, functions)
17 - Unary operations (+, -)
18 - Multiplicative operations (*, /)
19 - Additive operations (+, -)
20
21 Symbol tables are populated with operators and function names, and error
22 handling is integrated for reporting parse failures.
23
24***************************************************************************/
25
26/***************************************************************************
27 * Copyright (C) 2023 by Andreas Suter *
28 * andreas.suter@psi.ch *
29 * *
30 * This program is free software; you can redistribute it and/or modify *
31 * it under the terms of the GNU General Public License as published by *
32 * the Free Software Foundation; either version 2 of the License, or *
33 * (at your option) any later version. *
34 * *
35 * This program is distributed in the hope that it will be useful, *
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
38 * GNU General Public License for more details. *
39 * *
40 * You should have received a copy of the GNU General Public License *
41 * along with this program; if not, write to the *
42 * Free Software Foundation, Inc., *
43 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
44 ***************************************************************************/
45
46#include "PExpression.hpp"
47#include "PErrorHandler.hpp"
48#include "PAnnotation.hpp"
49
50namespace mupp { namespace parser
51{
52 namespace x3 = boost::spirit::x3;
53 namespace ascii = boost::spirit::x3::ascii;
54
55 using x3::char_;
56 using x3::double_;
57 using x3::raw;
58 using x3::lexeme;
59 using ascii::alpha;
60 using ascii::alnum;
61
63 // Symbol tables for operators and functions
65
66 inline struct additive_op_ : x3::symbols<ast::optoken>
67 {
69 {
70 add
71 ("+", ast::op_plus)
72 ("-", ast::op_minus)
73 ;
74 }
76
77 inline struct multiplicative_op_ : x3::symbols<ast::optoken>
78 {
80 {
81 add
82 ("*", ast::op_times)
83 ("/", ast::op_divide)
84 ;
85 }
87
88 inline struct unary_op_ : x3::symbols<ast::optoken>
89 {
91 {
92 add
93 ("+", ast::op_positive)
94 ("-", ast::op_negative)
95 ;
96 }
98
99 inline struct fun_tok_ : x3::symbols<ast::funid>
100 {
102 {
103 add
104 ("max", ast::fun_max)
105 ("min", ast::fun_min)
106 ("abs", ast::fun_abs)
107 ("sin", ast::fun_sin)
108 ("cos", ast::fun_cos)
109 ("tan", ast::fun_tan)
110 ("sinh", ast::fun_sinh)
111 ("cosh", ast::fun_cosh)
112 ("tanh", ast::fun_tanh)
113 ("asin", ast::fun_asin)
114 ("acos", ast::fun_acos)
115 ("atan", ast::fun_atan)
116 ("exp", ast::fun_exp)
117 ("log", ast::fun_log)
118 ("ln", ast::fun_ln)
119 ("sqrt", ast::fun_sqrt)
120 ;
121 }
123
125 // Rule definitions
127
128 inline expr_type const expr = "expr";
129 inline additive_expr_type const additive_expr = "additive_expr";
130 inline multiplicative_expr_type const multiplicative_expr = "multiplicative_expr";
131 inline unary_expr_type const unary_expr = "unary_expr";
132 inline primary_expr_type const primary_expr = "primary_expr";
133 inline identifier_type const identifier = "identifier";
134
136 // Grammar
138
139 inline auto const expr_def =
141 ;
142
143 inline auto const additive_expr_def =
146 ;
147
148 inline auto const multiplicative_expr_def =
151 ;
152
153 inline auto const unary_expr_def =
156 ;
157
158 inline auto const primary_expr_def =
159 double_
160 | identifier
161 | fun_tok > '(' > expr > ')'
162 | "pow(" > expr > ',' > expr > ')'
163 | '(' > expr > ')'
164 ;
165
166 inline auto const identifier_def =
167 raw[lexeme['$' >> *(alnum | '_')]]
168 ;
169
170 BOOST_SPIRIT_DEFINE(expr, additive_expr, multiplicative_expr,
172
173}}
174
175
@ op_minus
Subtraction operator (-)
Definition PAst.hpp:85
@ op_positive
Unary plus operator (+x)
Definition PAst.hpp:88
@ op_divide
Division operator (/)
Definition PAst.hpp:87
@ op_plus
Addition operator (+)
Definition PAst.hpp:84
@ op_times
Multiplication operator (*)
Definition PAst.hpp:86
@ op_negative
Unary minus operator (-x)
Definition PAst.hpp:89
@ fun_sqrt
Square root function.
Definition PAst.hpp:115
@ fun_atan
Arctangent function.
Definition PAst.hpp:111
@ fun_acos
Arccosine function.
Definition PAst.hpp:110
@ fun_exp
Exponential function.
Definition PAst.hpp:112
@ fun_min
Minimum value function.
Definition PAst.hpp:101
@ fun_sin
Sine function.
Definition PAst.hpp:103
@ fun_cos
Cosine function.
Definition PAst.hpp:104
@ fun_cosh
Hyperbolic cosine function.
Definition PAst.hpp:107
@ fun_max
Maximum value function.
Definition PAst.hpp:100
@ fun_ln
Natural logarithm function.
Definition PAst.hpp:114
@ fun_log
Base-10 logarithm function.
Definition PAst.hpp:113
@ fun_tanh
Hyperbolic tangent function.
Definition PAst.hpp:108
@ fun_asin
Arcsine function.
Definition PAst.hpp:109
@ fun_sinh
Hyperbolic sine function.
Definition PAst.hpp:106
@ fun_tan
Tangent function.
Definition PAst.hpp:105
@ fun_abs
Absolute value function.
Definition PAst.hpp:102
x3::rule< multiplicative_expr_class, ast::expression > multiplicative_expr_type
primary_expr_type const primary_expr
x3::rule< primary_expr_class, ast::operand > primary_expr_type
auto const primary_expr_def
auto const additive_expr_def
auto const multiplicative_expr_def
auto const expr_def
expr_type const expr
x3::rule< identifier_class, std::string > identifier_type
multiplicative_expr_type const multiplicative_expr
auto const unary_expr_def
x3::rule< expr_class, ast::expression > expr_type
additive_expr_type const additive_expr
mupp::parser::multiplicative_op_ multiplicative_op
mupp::parser::unary_op_ unary_op
identifier_type const identifier
mupp::parser::fun_tok_ fun_tok
mupp::parser::additive_op_ additive_op
x3::rule< unary_expr_class, ast::operand > unary_expr_type
unary_expr_type const unary_expr
auto const identifier_def
x3::rule< additive_expr_class, ast::expression > additive_expr_type