Bertini 2 - C++ Core
Software for Numerical Algebraic Geometry
Loading...
Searching...
No Matches
system_rules.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//bertini2/io/parsers.hpp is free software: you can redistribute it and/or modify
4//it under the terms of the GNU General Public License as published by
5//the Free Software Foundation, either version 3 of the License, or
6//(at your option) any later version.
7//
8//bertini2/io/parsing/system_rules.hpp is distributed in the hope that it will be useful,
9//but WITHOUT ANY WARRANTY; without even the implied warranty of
10//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11//GNU General Public License for more details.
12//
13//You should have received a copy of the GNU General Public License
14//along with bertini2/io/parsing/system_rules.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright(C) 2015 - 2017 by Bertini2 Development Team
17//
18// See <http://www.gnu.org/licenses/> for a copy of the license,
19// as well as COPYING. Bertini2 is provided with permitted
20// additional terms in the b2/licenses/ directory.
21
22
28
29#pragma once
30
31
32
33
37
38
39
40namespace bertini {
41 namespace parsing {
42 namespace classic {
43 // a few local using statements to reduce typing etc.
44 using Function = node::Function;
45 using Variable = node::Variable;
46 using Node = node::Node;
47
48
49 using Fn = std::shared_ptr<Function>;
50 using Var = std::shared_ptr<Variable>;
51 using Nd = std::shared_ptr<Node>;
52
77 template<typename Iterator, typename Skipper = ascii::space_type>
78 struct SystemParser : qi::grammar<Iterator, System(), Skipper>
79 {
80
81
82 SystemParser() : function_parser_(&encountered_symbols_),
83 /*initialize here with address of encountered_symbols*/
84 SystemParser::base_type(root_rule_)
85 {
86 namespace phx = boost::phoenix;
87 using qi::_1;
88 using qi::_2;
89 using qi::_3;
90 using qi::_4;
91 using qi::_val;
92 using qi::eps;
93 using qi::lit;
94 using boost::spirit::lexeme;
95
96
97 declarative_symbols_.add("variable_group",0);
98 declarative_symbols_.add("hom_variable_group",1);
99 declarative_symbols_.add("variable",2);
100 declarative_symbols_.add("function",3);
101 declarative_symbols_.add("constant",4);
102 declarative_symbols_.add("parameter",5);
103 declarative_symbols_.add("implicit_parameter",6);
104 declarative_symbols_.add("pathvariable",7);
105 declarative_symbols_.add("random",8);
106 declarative_symbols_.add("random_real",9);
107
108
109
110
111
112
113 special_numbers_.add("pi", node::Pi());
114 special_numbers_.add("Pi", special_numbers_.at("pi"));
115
116 special_numbers_.add("e", node::E());
117 special_numbers_.add("E", special_numbers_.at("e"));
118
119
120 special_numbers_.add("i", node::I());
121 special_numbers_.add("I", special_numbers_.at("i"));
122
123
124
125 encountered_symbols_.add("pi", special_numbers_.at("pi"));
126 encountered_symbols_.add("Pi", special_numbers_.at("pi"));
127
128 encountered_symbols_.add("e", special_numbers_.at("e"));
129 encountered_symbols_.add("E", special_numbers_.at("e"));
130
131
132 encountered_symbols_.add("i", special_numbers_.at("i"));
133 encountered_symbols_.add("I", special_numbers_.at("i"));
134
135
136
137
138
139 //TODO refine this so that counts are enforced at parse time?
140 root_rule_.name("system_parsing");
141 root_rule_ =
142 *(
143 variable_group_ [phx::bind(&System::AddVariableGroup, _val, _1)]
144 |
145 hom_variable_group_ [phx::bind(&System::AddHomVariableGroup, _val, _1)]
146 |
147 variables_ [phx::bind(&System::AddUngroupedVariables, _val, _1)]
148 |
149 functions_ [phx::bind(&System::AddFunctions, _val, _1)]
150 |
151 constants_ [phx::bind(&System::AddConstants, _val, _1)]
152 |
153 parameters_ [phx::bind(&System::AddParameters, _val, _1)]
154 |
155 implicit_parameters_ [phx::bind(&System::AddImplicitParameters, _val, _1)]
156 |
157 path_variable_ [phx::bind(&System::AddPathVariable, _val, _1)]
158 |
159 subfunction_ [phx::bind(&System::AddSubfunction, _val, _1)]
160 |
161 definition_
162 )
163 ;
164
165
166
167
168
169 variables_.name("variables_"); hom_variable_group_.name("hom_variable_group_"); variable_group_.name("variable_group_"); implicit_parameters_.name("implicit_parameters_");
170
171
172 variables_ = "variable" > genericvargp_ > ';';
173 hom_variable_group_ = "hom_variable_group" > genericvargp_ > ';';
174 variable_group_ = "variable_group" > genericvargp_ > ';';
175 implicit_parameters_ = "implicit_parameter" > genericvargp_ > ';';
176
177
178
179 path_variable_.name("path_variable_");
180 path_variable_ = "pathvariable" > new_variable_ > ';';
181
182
183
184 genericvargp_.name("genericvargp_");
185 genericvargp_ = new_variable_ % ',';
186
187 new_variable_.name("new_variable_");
188 new_variable_ = unencountered_symbol_ [boost::phoenix::bind( [this](Var & V, std::string str)
189 {
190 MakeAndAddVariable(V,str);
191 }, _val, _1 )];
192
193
194
195 functions_.name("functions_"); constants_.name("constants_"); parameters_.name("parameters_");
196
197 functions_ = "function" > genericfuncgp_ > ';';
198 constants_ = "constant" > genericfuncgp_ > ';';
199 parameters_ = "parameter" > genericfuncgp_ > ';';
200
201
202 genericfuncgp_.name("genericfuncgp_");
203 genericfuncgp_ = new_function_ % ',';
204
205
206 new_function_.name("new_function_");
207 new_function_ = unencountered_symbol_ [boost::phoenix::bind( [this](Fn & F, std::string str)
208 {
209 MakeAndAddFunction(F,str);
210 }, _val, _1 )];//[_val = make_shared_<Function>() (_1)]
211
212
213
214
215 // this rule gets a string.
216 unencountered_symbol_.name("unencountered_symbol_");
217 unencountered_symbol_ = valid_variable_name_ - lexeme[( declarative_symbols_ | encountered_symbols_ )];
218 // i am unsure about the use of lexeme in the above rule (unencountered_symbol).
219
220
221
222
223 // get a string which fits the naming rules.
224 valid_variable_name_.name("valid_variable_name_");
225 valid_variable_name_ = +qi::alpha >> *(qi::alnum | qi::char_("[]_") );
226
227
228
229
230 definition_.name("definition_");
231 definition_ = (encountered_functions_ > '=' > function_parser_ > ';') [phx::bind( [](const Fn & F, const Nd & N)
232 {
233 F->SetRoot(N);
234 },_1, _2)] ;
235
236
237 using qi::_a;
238 using qi::omit;
239 subfunction_.name("subfunction");
240 subfunction_ = new_function_ [_a = _1] > '=' >
241 function_parser_ [_val = _a, phx::bind( [](Fn & F, const Nd & N)
242 {
243 F->SetRoot(N);
244 },_a, _1)]
245 // omit close
246 > ';';
247
248
249 // debug(root_rule_);
250 //
251 //
252 //
253 // debug(functions_);
254 // debug(constants_);
255 // debug(parameters_);
256 //
257 // debug(genericfuncgp_);
258 // debug(new_function_);
259 //
260 //
261 //
262 // debug(definition_);
263 //
264 // debug(subfunction_);
265 //
266 //
267 //
268 //
269 // debug(variables_);
270 // debug(hom_variable_group_);
271 // debug(variable_group_);
272 // debug(implicit_parameters_);
273 // debug(path_variable_);
274 //
275 // debug(new_variable_);
276 // debug(genericvargp_); debug(variable_group_);
277 //
278 // debug(unencountered_symbol_);
279 //
280 // debug(valid_variable_name_);
281
282
283
284
285 // BOOST_SPIRIT_DEBUG_NODES( (unencountered_symbol_) (new_variable_) (genericvargp_))
286
287 using phx::val;
288 using phx::construct;
289 using namespace qi::labels;
290 qi::on_error<qi::fail>
291 ( root_rule_ ,
292 std::cout<<
293 val("System parser could not complete parsing. Expecting ")<<
294 _4<<
295 val(" here: ")<<
296 construct<std::string>(_3,_2)<<
297 std::endl
298 );
299
300
301 }
302
303
304
305
306 private:
307
308 // rule declarations. these are member variables for the parser.
309 qi::rule<Iterator, System(), Skipper > root_rule_;
310
311
312 qi::rule<Iterator, VariableGroup(), Skipper > variable_group_, hom_variable_group_, variables_, implicit_parameters_;
313 qi::rule<Iterator, VariableGroup(), Skipper > genericvargp_;
314
315 qi::rule<Iterator, Var(), Skipper> path_variable_;
316 qi::rule<Iterator, Var()> new_variable_;
317
318
319
320
321 qi::rule<Iterator, std::vector<Fn>(), Skipper > functions_, constants_, parameters_;
322 qi::rule<Iterator, std::vector<Fn>(), Skipper > genericfuncgp_;
323 qi::rule<Iterator, Fn(), Skipper, qi::locals<Fn> > subfunction_;
324
325 qi::rule<Iterator, Fn()> new_function_;
326
327
328 qi::rule<Iterator, std::string()> unencountered_symbol_;
329
330
331 // the rule which determines valid variable names
332 qi::rule<Iterator, std::string()> valid_variable_name_;
333
334 qi::rule<Iterator, Skipper, qi::unused_type> definition_;
335
336 // symbol declarations
337 qi::symbols<char,Nd> encountered_symbols_;
338 qi::symbols<char,int> declarative_symbols_;
339 qi::symbols<char,Fn> encountered_functions_;
340 qi::symbols<char,Nd> special_numbers_;
341
342 FunctionParser<Iterator> function_parser_;
343
348 void MakeAndAddFunction(Fn & F, std::string str)
349 {
350 F = Function::Make(str);
351 encountered_symbols_.add(str, F);
352 encountered_functions_.add(str,F);
353 }
354
359 void MakeAndAddVariable(Var & V, std::string str)
360 {
361 V = Variable::Make(str);
362 encountered_symbols_.add(str, V);
363 }
364
365
366 void SetRootNode(Fn & F, const Nd & N)
367 {
368 F->SetRoot(N);
369 }
370 };
371
372 } // re: namespace classic
373
374 } //re: namespace parsing
375
376
377
378
379} // re: namespace bertini
The fundamental polynomial system class for Bertini2.
Definition system/system.hpp:99
void AddSubfunction(Fn const &F)
void AddImplicitParameters(VariableGroup const &v)
void AddUngroupedVariables(VariableGroup const &v)
void AddHomVariableGroup(VariableGroup const &v)
void AddPathVariable(Var const &v)
void AddFunctions(std::vector< Fn > const &F)
void AddVariableGroup(VariableGroup const &v)
void AddConstants(std::vector< Fn > const &C)
void AddParameters(std::vector< Fn > const &F)
Provides the parsing rules for functions in bertini2.
Provides all the include files needed to develop a parsing file that uses boost qi.
std::shared_ptr< Node > E()
Definition special_number.cpp:37
std::shared_ptr< Node > Pi()
Definition special_number.cpp:32
std::shared_ptr< Node > I()
Definition special_number.cpp:42
A Qi grammar parser for parsing text into function trees.
Definition function_rules.hpp:143
Provides the bertini::System class.