1Math::Symbolic::Parser(U3s)er Contributed Perl DocumentatMiaotnh::Symbolic::Parser(3)
2
3
4
6 Math::Symbolic::Parser - Parse strings into Math::Symbolic trees
7
9 use Math::Symbolic::Parser;
10 my $parser = Math::Symbolic::Parser->new();
11 $string =~ s/\s+//g;
12 my $tree = $parser->parse($string);
13
14 # or better:
15 use Math::Symbolic;
16 my $tree = Math::Symbolic->parse_from_string($string);
17
19 This module contains the parsing routines used by Math::Symbolic to
20 parse strings into Math::Symbolic trees. Usually, you will want to
21 simply use the Math::Symbolic->parse_from_string() class method instead
22 of this module directly. If you do use this module directly, however,
23 make sure to remove any whitespace from your input string.
24
25 NOTE
26 With version 0.501 of Math::Symbolic, an experimental, new parser is
27 introduced, but it is not enabled by default. The new parser is based
28 on Parse::Yapp instead of Parse::RecDescent and comes with an at least
29 ten fold speed increase. However, it has not been available for a long
30 time and is not as well tested. Since version 2.00 of the
31 Math::SymbolicX::ParserExtensionFactory module, it's possible to extend
32 Yapp parsers.
33
34 At some point in the future the Yapp-based parser will become the
35 default! It is suggested you test your code against it before that.
36 Code that uses the RecDescent based parser's "Extend" method may fail!
37
38 Until then, you need to load it by hand as follows:
39
40 $Math::Symbolic::Parser = Math::Symbolic::Parser->new(
41 implementation=>'Yapp'
42 );
43
44 This replaces the default Math::Symbolic parser with an instance of the
45 new Yapp parser.
46
47 STRING FORMAT
48 The parser has been designed to parse strings that are reminiscient of
49 ordinary algebraic expressions including the standard arithmetic infix
50 operators such as multiplication. Many functions such as a rather
51 comprehensive set of trigonometric functions are parsed in prefix form
52 like 'sin(expression)' or 'log(base, expression)'. Unknown identifiers
53 starting with a letter and containing only letters, digits, and
54 underscores are parsed as variables. If these identifiers are followed
55 by parenthesis containing a list of identifiers, the list is parsed as
56 the signature of the variable. Example: '5*x(t)' is parsed as the
57 product of the constant five and the variable 'x' which depends on 't'.
58 These dependencies are important for total derivatives.
59
60 The supported builtin-functions are listed in the documentation for
61 Math::Symbolic::Operator in the section on the new() constructor.
62
63 EXTENSIONS
64 In version 0.503, a function named "exp(...)" is recognized and
65 transformed into "e^(...)" internally. In version 0.506, a function
66 named "sqrt(...)" was added which is transformed into "(...)^0.5".
67 Version 0.511 added support for the typical "f'(x)" syntax for
68 derivatives. For details, refer to the section on parsing derivatives
69 below.
70
71 EXAMPLES
72 # An example from analytical mechanics:
73 my $hamilton_function =
74 Math::Symbolic->parse_from_string(
75 'p_q(q, dq_dt, t) * dq_dt(q, t) - Lagrange(q, p_q, t)'
76 );
77
78 This parses as "The product of the generalized impulse p_q (which is a
79 function of the generalized coordinate q, its derivative, and the time)
80 and the derivative of the generalized coordinate dq_dt (which depends
81 on q itself and the time). This term minus the Lagrange Function (of
82 q, the impulse, and the time) is the Hamilton Function."
83
84 Well, that's how it parses in my head anyway. The parser will generate
85 a tree like this:
86
87 Operator {
88 type => difference,
89 operands => (
90 Operator {
91 type => product,
92 operands => (
93 Variable {
94 name => p_q,
95 dependencies => q, dq_dt, t
96 },
97 Variable {
98 name => dq_dt,
99 dependencies => q, t
100 }
101 )
102 },
103 Variable {
104 name => Lagrange,
105 dependencies => q, p_q, t
106 }
107 )
108 }
109
110 Possibly a simpler example would be 'amplitude * sin(phi(t))' which
111 descibes an oscillation. sin(...) is assumed to be the sine function,
112 amplitude is assumed to be a symbol / variable that doesn't depend on
113 any others. phi is recognized as a variable that changes over time (t).
114 So phi(t) is actually a function of t that hasn't yet been specified.
115 phi(t) could look like 'omega*t + theta' where strictly speaking,
116 omega, t, and theta are all symbols without dependencies. So omega and
117 theta would be treated as constants if you derived them in respect to
118 t. Figuratively speaking, omega would be a frequency and theta would
119 be a initial value.
120
121 PARSING DERIVATIVES
122 The traditional way of specifying a derivative for parsing was
123 "partial_derivative(EXPRESSION, VARIABLE)" where "EXPRESSION" can be
124 any valid expression and "VARIABLE" is a variable name. The syntax
125 denotes a partial derivative of the expression with respect to the
126 variable. The same syntax is available for total derivatives.
127
128 With version 0.511, a new syntax for specifying partial derivatives was
129 added to the parser(s). "f'(x)" denotes the first partial derivative of
130 "f" with respect to "x". If "(x)" is omitted, "f'" defaults to using
131 "x". "f''(a)" is the second order partial derivative with respect to
132 "a". If there are multiple variables in the parenthesis, a la "f'(b,
133 a)", the first variable is used for the derivatives.
134
135 EXPORT
136 None by default.
137
139 While working with this module, you might get into the not-so-convient
140 position of having to debug the parser and/or its grammar. In order to
141 make this possible, there's the $DEBUG package variable which, when set
142 to 1, makes the parser warn which grammar elements are being processed.
143 Note, however, that their order is bottom-up, not top-down.
144
145 Constructor new
146 This constructor does not expect any arguments and returns a
147 Parse::RecDescent parser to parse algebraic expressions from a string
148 into Math::Symbolic trees.
149
150 The constructor takes key/value pairs of options.
151
152 You can regenerate the parser from the grammar in the scalar
153 $Math::Symbolic::Parser::Grammar instead of using the (slightly faster)
154 precompiled grammar from Math::Symbolic::Parser::Precompiled. You can
155 enable recompilation from the grammar with the option "recompile => 1".
156 This only has an effect if the implementation is the Parse::RecDescent
157 based parser (which is the default).
158
159 If you care about parsing speed more than about being able to extend
160 the parser at run-time, you can specify the "implementation" option.
161 Currently recognized are "RecDescent" and "Yapp" implementations.
162 "RecDescent" is the default and "Yapp" is significantly faster. The
163 Parse::Yapp based implementation may not support all extension modules.
164 It has been tested with Math::SymbolicX::ParserExtensionFactory and
165 Math::SymbolicX::Complex.
166
168 Please send feedback, bug reports, and support requests to the
169 Math::Symbolic support mailing list: math-symbolic-support at lists dot
170 sourceforge dot net. Please consider letting us know how you use
171 Math::Symbolic. Thank you.
172
173 If you're interested in helping with the development or extending the
174 module's functionality, please contact the developers' mailing list:
175 math-symbolic-develop at lists dot sourceforge dot net.
176
177 List of contributors:
178
179 Steffen Mueller, symbolic-module at steffen-mueller dot net
180 Stray Toaster, mwk at users dot sourceforge dot net
181 Oliver Ebenhoeh
182
184 New versions of this module can be found on http://steffen-mueller.net
185 or CPAN. The module development takes place on Sourceforge at
186 http://sourceforge.net/projects/math-symbolic/
187
188 Math::Symbolic
189
190 Math::Symbolic::Parser::Precompiled
191
193 This package is distributed under the same license as the rest of the
194 Math::Symbolic distribution (Artistic+GPL), but the author of
195 Parse::Yapp has requested that his copyright and the licensing terms of
196 Parse::Yapp derived works be reproduced. Note that the license is the
197 same as Math::Symbolic's license. We're using the "standalone parser"
198 option.
199
200 The Parse::Yapp module and its related modules and shell scripts
201 are copyright (c) 1998-2001 Francois Desarmenien, France. All
202 rights reserved.
203
204 You may use and distribute them under the terms of either the GNU
205 General Public License or the Artistic License, as specified in
206 the Perl README file.
207
208 If you use the "standalone parser" option so people don't need to
209 install Parse::Yapp on their systems in order to run you software,
210 this copyright notice should be included in your software
211 copyright too, and the copyright notice in the embedded driver
212 should be left untouched.
213
214
215
216perl v5.28.0 2018-07-14 Math::Symbolic::Parser(3)