1Math::Expression::EvaluUasteorr:C:oPnatrrsiebru(t3e)d PeMralthD:o:cEuxmpernetsastiioonn::Evaluator::Parser(3)
2
3
4
6 Math::Expression::Evaluator::Parser - Parse mathematical expressions
7
9 use Math::Expression::Evaluator::Parser;
10 my $exp = '2 + a * 4';
11 my $ast = Math::Expression::Evaluator::Parser::parse($exp, {});
12 # $ast is now something like this:
13 # $ast = ['+',
14 # 2,
15 # ['*',
16 # ['$', 'a'],
17 # 4
18 # ]
19 # ];
20
22 This module parses a mathematical expression in usual notation, and
23 turns it into an Abstract Syntax Tree (AST).
24
25 If you want to have a simple interface and want to evaluate these ASTs,
26 use Math::Expression::Evaluator.
27
28 The following description of the AST structure matches the current
29 implementation, but really is an implementation detail that's subject
30 to change without further notice. In particular a possible addition of
31 meta information (like file and line numbers) might require a change of
32 structure.
33
34 The AST is a tree that consists of nested array refs. The first item is
35 a string (until now always a single character), and denotes the type of
36 the node. The rest of the items in the array is a list of its
37 arguments.
38
39 For the mathematical symbols "+", "-", "*", "/", "^" (exponentation)
40 this is straight forward, but "/" and "-" are always treated as prefix
41 ops, so the string '2 - 3' is actually turned into "['+', 2, ['-',
42 3]]".
43
44 Other AST nodes are
45
46 '$' "['$', $var_name]" represents a variable.
47
48 '{' "['{', $expr1, $expr2, ... ]" represents a block, i.e. a list of
49 expressions.
50
51 '=' "['=', $var, $expr]" represents an assignment, where $expr is
52 assigned to $var.
53
54 '&' "['&', $name, @args]" is a function toll to the function called
55 $name.
56
58 parse
59 "parse" takes a string and a hash ref, where the hash ref takes
60 configuration parameters. Currently the only allowed option is
61 "force_semicolon". If set to a true value, it forces statements to
62 be forced by semicolons (so "2 3" will be forbidden, "2; 3" is
63 still allowed).
64
65 "parse" throws an exception on parse errors.
66
67
68
69perl v5.28.1 2019-02M-a0t2h::Expression::Evaluator::Parser(3)