Hakan Ardo > Math-Expr > Math::Expr

Download:
Math-Expr-0.2.tar.gz

Dependencies

Annotate this POD

Related Modules

Parse::RecDescent
Data::Dumper
Math::Symbolic
Math::Fraction
Tie::IxHash
Math::Calc::Units
XML::LibXML
Parse::Yapp
more...
By perlmonks.org

CPAN RT

Open  0
Report a bug
Source  

NAME ^

  Math::Expr - Parses mathematical expressions

SYNOPSIS ^

  require Math::Expr;
  
  $p=new Math::Expr;
  $e=$p->Parse("a+4*b-d/log(s)+f(d,e)");

DESCRIPTION ^

  Parses mathematical expressions into a tree structure. The expressions
  may contain integers, real numbers, alphanumeric variable names, 
  alphanumeric function names and most other characters might be used 
  as operators. The operators can even be longer than one character! 
  The only limitation is that a variable or function name may not start 
  on a digit, and not all chars are accepted as operations. To be exact, 
  here is the grammatic (in perl regexp notation):

    <Expr>     = -?<Elem>(<OpChr><Elem>)*
    <Elem>     = <Number>|<Var>|<Function>|\(<Expr>\)
    <Number>   = <Integer>|<Float>
    <Integer>  = \d+
    <Float>    = \d*\.\d+
    <Var>      = [a-zA-Z][a-zA-Z0-9]*(:[a-zA-Z][a-zA-Z0-9]*)?
    <Function> = [a-zA-Z][a-zA-Z0-9]*\(<Expr>(,<Expr>)*\)
    <OpChr>    = [^a-zA-Z0-9\(\)\,\.\:]+

  If the - sign is present at the beginning of an <Expr> it is parsed 
  in the exact same structure as 0<Expr>. That is to allow 
  constructions like "-a*b" or "b+3*(-7)".

  A variable consists of two parts separated by a ':'-char. The first 
  part is the variable name, and the second optional part is its type. 
  Default type is Real.

METHODS ^

$p = new Math::Expr

This is the constructor, it creates an object which later can be used to parse the strings.

$e=$p->Parse($str)

This will parse the string $str and return an expression tree, in the form of a Math::Expr::Opp object (or in simple cases only a Math::Expr::Var or Math::Expr::Num object).

$p->Priority({'^'=>50, '/'=>40, '*'=>30, '-'=>20, '+'=>10})

This will set the priority of ALL the operands (there is currently no way to change only one of them). The priority decides what should be constructed if several operands is listed without delimiters. Eg if a+b*c should be treated as (a+b)*c or a+(b*c). (Default is listed in header).

$p->SetOppDB($db)

Sets the OpperationDB to be used to $db. See Math::Expr::OpperationDB for more info. This will be passed down to all objects returned by the parser aswell.

BUGS ^

  The parses does not handle bad strings in a decent way. If you try 
  to parse a string that does not follow the specification above, all 
  strange things might happen...

AUTHOR ^

  Hakan Ardo <hakan@debian.org>

SEE ALSO ^

Math::Expr::Opp