Hakan Ardo

NAME

  Math::Expr - Parses mathematical expressions

SYNOPSIS

  use Math::Expr;
  
  SetOppDB(new Math::Expr::OpperationDB('<DBFileName>'));
  $e=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> Then a neg()
        function is placed around it. 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

$e=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 = new Math::Expr

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

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).

The priority is global for all parsers and all expretions, so changing it here will change it for all parsers and parsed objects. The idea is to use this method to initiate the system before using it.

SetOppDB($db)

Sets the OpperationDB to be used to $db. See Math::Expr::OpperationDB for more info.

This is a global variable afecting all parsers and all parsed structures.

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