[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] RecDescent, perhaps? math expression parsing



I've been working on a mathematical expression parser off and on for a
year or so -- the goal is to make a module to break an expression down
into nodes, and (among other things) take the derivative.

I don't know yet what tool I'll be using to lex the expression.  I run
into much doodoo because of the following (abbreviated) problem:

  # OPEN = (
  # CLOSE = )
  # OP_H = ^
  # OP_M = [*/]
  # OP_L = [+-]
  # other names should be self-explanatory

  # ex: $expr is "3x^2"

  TERM ::=
    OPEN TERM CLOSE |
    FUNC OPEN TERM CLOSE |
    TERM OP_H TERM |
    NUM TERM |
    CONST TERM |
    VAR TERM |
    TERM OP_M TERM |
    TERM OP_L TERM |
    NUM |
    CONST |
    VAR

As you might be able to see, "3x^2" would match at TERM OP_H
TERM.  However, the first TERM in 'TERM OP_H TERM' matches at TERM OP_H
TERM again.  A lot of ugly recursion.  Blegh.

How do I get around this nasty recursion?  I'm at a bit of a loss.

-- 
Jeff "japhy" Pinyan     japhy@pobox.com     http://www.pobox.com/~japhy/
PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/
CPAN - #1 Perl Resource  (my id:  PINYAN)        http://search.cpan.org/


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe