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