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
68 EXAMPLES
69 # An example from analytical mechanics:
70 my $hamilton_function =
71 Math::Symbolic->parse_from_string(
72 'p_q(q, dq_dt, t) * dq_dt(q, t) - Lagrange(q, p_q, t)'
73 );
74
75 This parses as "The product of the generalized impulse p_q (which is a
76 function of the generalized coordinate q, its derivative, and the time)
77 and the derivative of the generalized coordinate dq_dt (which depends
78 on q itself and the time). This term minus the Lagrange Function (of
79 q, the impulse, and the time) is the Hamilton Function."
80
81 Well, that's how it parses in my head anyway. The parser will generate
82 a tree like this:
83
84 Operator {
85 type => difference,
86 operands => (
87 Operator {
88 type => product,
89 operands => (
90 Variable {
91 name => p_q,
92 dependencies => q, dq_dt, t
93 },
94 Variable {
95 name => dq_dt,
96 dependencies => q, t
97 }
98 )
99 },
100 Variable {
101 name => Lagrange,
102 dependencies => q, p_q, t
103 }
104 )
105 }
106
107 Possibly a simpler example would be 'amplitude * sin(phi(t))' which
108 descibes an oscillation. sin(...) is assumed to be the sine function,
109 amplitude is assumed to be a symbol / variable that doesn't depend on
110 any others. phi is recognized as a variable that changes over time (t).
111 So phi(t) is actually a function of t that hasn't yet been specified.
112 phi(t) could look like 'omega*t + theta' where strictly speaking,
113 omega, t, and theta are all symbols without dependencies. So omega and
114 theta would be treated as constants if you derived them in respect to
115 t. Figuratively speaking, omega would be a frequency and theta would
116 be a initial value.
117
118 EXPORT
119 None by default.
120
122 While working with this module, you might get into the not-so-convient
123 position of having to debug the parser and/or its grammar. In order to
124 make this possible, there's the $DEBUG package variable which, when set
125 to 1, makes the parser warn which grammar elements are being processed.
126 Note, however, that their order is bottom-up, not top-down.
127
128 Constructor new
129 This constructor does not expect any arguments and returns a
130 Parse::RecDescent parser to parse algebraic expressions from a string
131 into Math::Symbolic trees.
132
133 The constructor takes key/value pairs of options.
134
135 You can regenerate the parser from the grammar in the scalar
136 $Math::Symbolic::Parser::Grammar instead of using the (slightly faster)
137 precompiled grammar from Math::Symbolic::Parser::Precompiled. You can
138 enable recompilation from the grammar with the option "recompile => 1".
139 This only has an effect if the implementation is the Parse::RecDescent
140 based parser (which is the default).
141
142 If you care about parsing speed more than about being able to extend
143 the parser at run-time, you can specify the "implementation" option.
144 Currently recognized are "RecDescent" and "Yapp" implementations.
145 "RecDescent" is the default and "Yapp" is significantly faster. The
146 Parse::Yapp based implementation may not support all extension modules.
147 It has been tested with Math::SymbolicX::ParserExtensionFactory and
148 Math::SymbolicX::Complex.
149
151 Please send feedback, bug reports, and support requests to the
152 Math::Symbolic support mailing list: math-symbolic-support at lists dot
153 sourceforge dot net. Please consider letting us know how you use
154 Math::Symbolic. Thank you.
155
156 If you're interested in helping with the development or extending the
157 module's functionality, please contact the developers' mailing list:
158 math-symbolic-develop at lists dot sourceforge dot net.
159
160 List of contributors:
161
162 Steffen MA~Xller, symbolic-module at steffen-mueller dot net
163 Stray Toaster, mwk at users dot sourceforge dot net
164 Oliver EbenhA~Xh
165
167 New versions of this module can be found on http://steffen-mueller.net
168 or CPAN. The module development takes place on Sourceforge at
169 http://sourceforge.net/projects/math-symbolic/
170
171 Math::Symbolic
172
173 Math::Symbolic::Parser::Precompiled
174
176 This package is distributed under the same license as the rest of the
177 Math::Symbolic distribution (Artistic+GPL), but the author of
178 Parse::Yapp has requested that his copyright and the licensing terms of
179 Parse::Yapp derived works be reproduced. Note that the license is the
180 same as Math::Symbolic's license. We're using the "standalone parser"
181 option.
182
183 The Parse::Yapp module and its related modules and shell scripts
184 are copyright (c) 1998-2001 Francois Desarmenien, France. All
185 rights reserved.
186
187 You may use and distribute them under the terms of either the GNU
188 General Public License or the Artistic License, as specified in
189 the Perl README file.
190
191 If you use the "standalone parser" option so people don't need to
192 install Parse::Yapp on their systems in order to run you software,
193 this copyright notice should be included in your software
194 copyright too, and the copyright notice in the embedded driver
195 should be left untouched.
196
197
198
199perl v5.12.0 2010-05-03 Math::Symbolic::Parser(3)