musrfit  1.9.2
PFunctionGrammar.h
Go to the documentation of this file.
1 /***************************************************************************
2 
3  PFunctionGrammer.h
4 
5  Author: Andreas Suter
6  e-mail: andreas.suter@psi.ch
7 
8 ***************************************************************************/
9 
10 /***************************************************************************
11  * Copyright (C) 2007-2023 by Andreas Suter *
12  * andreas.suter@psi.ch *
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  * This program is distributed in the hope that it will be useful, *
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22  * GNU General Public License for more details. *
23  * *
24  * You should have received a copy of the GNU General Public License *
25  * along with this program; if not, write to the *
26  * Free Software Foundation, Inc., *
27  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28  ***************************************************************************/
29 
30 #ifndef _PFUNCTIONGRAMMAR_H_
31 #define _PFUNCTIONGRAMMAR_H_
32 
33 //#define BOOST_SPIRIT_DEBUG
34 
35 #include <boost/version.hpp>
36 
37 #if BOOST_VERSION >= 103800
38 # include <boost/spirit/include/classic_core.hpp>
39 # include <boost/spirit/include/classic_ast.hpp>
40  using namespace BOOST_SPIRIT_CLASSIC_NS;
41 #else
42 # include <boost/spirit/core.hpp>
43 # include <boost/spirit/tree/ast.hpp>
44  using namespace boost::spirit;
45 #endif
46 
47 typedef char const* iterator_t;
48 typedef tree_match<iterator_t> parse_tree_match_t;
49 typedef parse_tree_match_t::tree_iterator iter_t;
50 
51 //--------------------------------------------------------------------------
55 struct PFunctionGrammar : public grammar<PFunctionGrammar>
56 {
57  static const int realID = 1;
58  static const int constPiID = 2;
59  static const int constGammaMuID = 3;
60  static const int constFieldID = 4;
61  static const int constEnergyID = 5;
62  static const int constTempID = 6;
63  static const int funLabelID = 7;
64  static const int parameterID = 8;
65  static const int mapID = 9;
66  static const int functionID = 10;
67  static const int powerID = 11;
68  static const int factorID = 12;
69  static const int termID = 13;
70  static const int expressionID = 14;
71  static const int assignmentID = 15;
72 
73  template <typename ScannerT>
74  struct definition
75  {
76  definition(PFunctionGrammar const& /*self*/)
77  {
78  // Start grammar definition
79  real = leaf_node_d[ real_p ];
80 
81  const_pi = leaf_node_d[ str_p("PI") ];
82 
83  const_gamma_mu = leaf_node_d[ str_p("GAMMA_MU") ];
84 
85  const_field = leaf_node_d[ str_p("B") ];
86 
87  const_energy = leaf_node_d[ str_p("EN") ];
88 
89  const_temp = leaf_node_d[ ( lexeme_d[ "T" >> +digit_p ] ) ];
90 
91  fun_label = leaf_node_d[ ( lexeme_d[ "FUN" >> +digit_p ] ) ];
92 
93  parameter = leaf_node_d[ ( lexeme_d[ "PAR" >> +digit_p ] ) |
94  ( lexeme_d[ "-PAR" >> +digit_p ] ) ];
95 
96  map = leaf_node_d[ ( lexeme_d[ "MAP" >> +digit_p ] ) ];
97 
98  function = lexeme_d[ root_node_d[ str_p("COS") ] >> ch_p('(') ] >> expression >> ch_p(')')
99  | lexeme_d[ root_node_d[ str_p("SIN") ] >> ch_p('(') ] >> expression >> ch_p(')')
100  | lexeme_d[ root_node_d[ str_p("TAN") ] >> ch_p('(') ] >> expression >> ch_p(')')
101  | lexeme_d[ root_node_d[ str_p("COSH") ] >> ch_p('(') ] >> expression >> ch_p(')')
102  | lexeme_d[ root_node_d[ str_p("SINH") ] >> ch_p('(') ] >> expression >> ch_p(')')
103  | lexeme_d[ root_node_d[ str_p("TANH") ] >> ch_p('(') ] >> expression >> ch_p(')')
104  | lexeme_d[ root_node_d[ str_p("ACOS") ] >> ch_p('(') ] >> expression >> ch_p(')')
105  | lexeme_d[ root_node_d[ str_p("ASIN") ] >> ch_p('(') ] >> expression >> ch_p(')')
106  | lexeme_d[ root_node_d[ str_p("ATAN") ] >> ch_p('(') ] >> expression >> ch_p(')')
107  | lexeme_d[ root_node_d[ str_p("ACOSH") ] >> ch_p('(') ] >> expression >> ch_p(')')
108  | lexeme_d[ root_node_d[ str_p("ASINH") ] >> ch_p('(') ] >> expression >> ch_p(')')
109  | lexeme_d[ root_node_d[ str_p("ATANH") ] >> ch_p('(') ] >> expression >> ch_p(')')
110  | lexeme_d[ root_node_d[ str_p("LOG") ] >> ch_p('(') ] >> expression >> ch_p(')')
111  | lexeme_d[ root_node_d[ str_p("LN") ] >> ch_p('(') ] >> expression >> ch_p(')')
112  | lexeme_d[ root_node_d[ str_p("EXP") ] >> ch_p('(') ] >> expression >> ch_p(')')
113  | lexeme_d[ root_node_d[ str_p("SQRT") ] >> ch_p('(') ] >> expression >> ch_p(')')
114  ;
115 
116  power = lexeme_d[ root_node_d[ str_p("POW") ] >> ch_p('(') ] >> expression >> ch_p(',') >> expression >> ch_p(')')
117  ;
118 
119  factor = real
120  | const_pi
121  | const_gamma_mu
122  | const_field
123  | const_energy
124  | const_temp
125  | parameter
126  | map
127  | function
128  | power
129  | inner_node_d[ch_p('(') >> expression >> ch_p(')')]
130  ;
131 
132  term = factor >>
133  *( (root_node_d[ch_p('*')] >> factor)
134  | (root_node_d[ch_p('/')] >> factor)
135  );
136 
137  expression = term >>
138  *( (root_node_d[ch_p('+')] >> term)
139  | (root_node_d[ch_p('-')] >> term)
140  );
141 
142  assignment = (fun_label >> ch_p('=') >> expression);
143  // End grammar definition
144 
145  // turn on the debugging info.
146  BOOST_SPIRIT_DEBUG_RULE(real);
147  BOOST_SPIRIT_DEBUG_RULE(const_pi);
148  BOOST_SPIRIT_DEBUG_RULE(const_gamma_mu);
149  BOOST_SPIRIT_DEBUG_RULE(const_field);
150  BOOST_SPIRIT_DEBUG_RULE(const_energy);
151  BOOST_SPIRIT_DEBUG_RULE(const_temp);
152  BOOST_SPIRIT_DEBUG_RULE(fun_label);
153  BOOST_SPIRIT_DEBUG_RULE(parameter);
154  BOOST_SPIRIT_DEBUG_RULE(map);
155  BOOST_SPIRIT_DEBUG_RULE(function);
156  BOOST_SPIRIT_DEBUG_RULE(power);
157  BOOST_SPIRIT_DEBUG_RULE(factor);
158  BOOST_SPIRIT_DEBUG_RULE(term);
159  BOOST_SPIRIT_DEBUG_RULE(expression);
160  BOOST_SPIRIT_DEBUG_RULE(assignment);
161  }
162 
163  rule<ScannerT, parser_context<>, parser_tag<assignmentID> > assignment;
164  rule<ScannerT, parser_context<>, parser_tag<expressionID> > expression;
165  rule<ScannerT, parser_context<>, parser_tag<termID> > term;
166  rule<ScannerT, parser_context<>, parser_tag<factorID> > factor;
167  rule<ScannerT, parser_context<>, parser_tag<functionID> > function;
168  rule<ScannerT, parser_context<>, parser_tag<powerID> > power;
169  rule<ScannerT, parser_context<>, parser_tag<mapID> > map;
170  rule<ScannerT, parser_context<>, parser_tag<parameterID> > parameter;
171  rule<ScannerT, parser_context<>, parser_tag<funLabelID> > fun_label;
172  rule<ScannerT, parser_context<>, parser_tag<constTempID> > const_temp;
173  rule<ScannerT, parser_context<>, parser_tag<constEnergyID> > const_energy;
174  rule<ScannerT, parser_context<>, parser_tag<constFieldID> > const_field;
175  rule<ScannerT, parser_context<>, parser_tag<constGammaMuID> > const_gamma_mu;
176  rule<ScannerT, parser_context<>, parser_tag<constPiID> > const_pi;
177  rule<ScannerT, parser_context<>, parser_tag<realID> > real;
178 
179  rule<ScannerT, parser_context<>, parser_tag<assignmentID> > const&
180  start() const { return assignment; }
181  };
182 };
183 
184 #endif // _PFUNCTIONGRAMMAR_H_
rule< ScannerT, parser_context<>, parser_tag< mapID > > map
rule< ScannerT, parser_context<>, parser_tag< constTempID > > const_temp
rule< ScannerT, parser_context<>, parser_tag< assignmentID > > const & start() const
rule< ScannerT, parser_context<>, parser_tag< constPiID > > const_pi
rule< ScannerT, parser_context<>, parser_tag< constEnergyID > > const_energy
rule< ScannerT, parser_context<>, parser_tag< assignmentID > > assignment
definition(PFunctionGrammar const &)
rule< ScannerT, parser_context<>, parser_tag< parameterID > > parameter
rule< ScannerT, parser_context<>, parser_tag< termID > > term
parse_tree_match_t::tree_iterator iter_t
rule< ScannerT, parser_context<>, parser_tag< expressionID > > expression
rule< ScannerT, parser_context<>, parser_tag< funLabelID > > fun_label
char const * iterator_t
rule< ScannerT, parser_context<>, parser_tag< powerID > > power
rule< ScannerT, parser_context<>, parser_tag< constFieldID > > const_field
rule< ScannerT, parser_context<>, parser_tag< realID > > real
tree_match< iterator_t > parse_tree_match_t
rule< ScannerT, parser_context<>, parser_tag< constGammaMuID > > const_gamma_mu
rule< ScannerT, parser_context<>, parser_tag< factorID > > factor