Shop OBEX P1 Docs P2 Docs Learn Events
Anyone have a spin math expression parser? — Parallax Forums

Anyone have a spin math expression parser?

jazzedjazzed Posts: 11,803
edited 2008-09-15 04:38 in Propeller 1
I'm working on an application that needs an expression parser. I've done recursive decent parsing in C and Visual Basic a few times for each language. I guess you could say I'm exhausted and looking for options. If nothing is available, I'll just go do it. TIA.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-09-15 02:47
    Use the recursive descent parser from FemtoBasic in the Propeller Object Exchange. It's pretty complete. During the preliminary scan of the statements, all multicharacter tokens get replaced with a single byte token using a simple table lookup. Variables are single letters.

    Actually, that's not quite true. Some two character operators like "//" are handled locally.

    Post Edited (Mike Green) : 9/15/2008 2:54:18 AM GMT
  • jazzedjazzed Posts: 11,803
    edited 2008-09-15 03:29
    Mike, it is not clear how that parser handles complex math expressions according to arithmetic order of operations (the main reason to use recursive descent). The manual makes no mention of such capability either. Maybe you can outline the part of the design that handles such expressions?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve
  • rokickirokicki Posts: 1,000
    edited 2008-09-15 03:55
    Well, I wrote the original femtobasic, and the way it handles precedences is it uses a different recursive function for
    each precedence level; you can clearly see that by which functions scan for which operators and which recursive
    functions they then call.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-09-15 04:07
    Here's one example:
    pri arithExpr | tok, t
       t := term
       repeat
          tok := spaces
          if tok == "+"
             tp++
             t += term
          elseif tok == "-"
             tp++
             t -= term
          else
             return t
    


    This calls "term", then looks for either a "+" or "-". If found, it looks for another term after that.
    Once it has both terms, it adds (or subtracts) them, then goes back and looks for another "+" or "-".
    If there's no "+" or "-", it returns its current value. "term" is the next level which looks for "factor"s
    separated by "*", "/", or "//", the next higher precedence operator group.
  • jazzedjazzed Posts: 11,803
    edited 2008-09-15 04:13
    @Tom,
    At first glance I saw parts of the boolean parser chain but no arithmetic. I see it all now. Hope you don't mind if I extract what I need which does not include the basic language processing and adopt it to my application. Thanks.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve
  • Mike GreenMike Green Posts: 23,101
    edited 2008-09-15 04:29
    FemtoBasic is provided via the Propeller Object Exchange and, as such, falls under the MIT License. Feel free to use whatever parts of it are useful to you. If you use a significant portion, you have to include the copyright notice and indicate what portion it applies to.
  • jazzedjazzed Posts: 11,803
    edited 2008-09-15 04:38
    Yes Mike.
    Wish I knew who contributed what ... and because of that I'll end up porting one of my designs from C anyway ... just not tonight. For the time being this will help with proof of concept until I have more time.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve
Sign In or Register to comment.