3

I want to program a package that calculates the resulting value of a certain input formula,

I created the parser due to the Shunting-Yard Algorithm (Dijikstra), I want to create libraries of the functions that the user will be allowed to use (for ex: sin() and cos() functions) then I was wondering what my next step should be; so I have some questions:

  1. What is more simpler to use, the Shunting Yard Algorithm, or the Recursive-Descent algorithm for parsing the formulas?

  2. will i reach the work of the interpreter in some stage of my work, and how?

Thanks...

Please note that i am programming it using Delphi

5
  • you may want to give this a try pegtop.net/delphi/components/math/download.htm Commented Feb 22, 2012 at 10:37
  • [1] - what does it mean to be "simple"? [2] - you need to elaborate. We don't have your notes from class! Commented Feb 22, 2012 at 10:38
  • No, it is not a homework, im not anymore in uni. Commented Feb 22, 2012 at 10:40
  • I meant by Simple: simpler to program using recursive methods, or in ohter words, more simple to deal with concerning the internal functions that im gonna use later. Commented Feb 22, 2012 at 10:40
  • "Dealing with your internal functions" isn't going to be the hardest part, and it'll be about the same no matter what parsing algorithm you're going to use. If you prefer a recursive implementation you'll probably prefer the Recursive Descent algorithm as it is, well, recursive! Commented Feb 22, 2012 at 10:54

2 Answers 2

11

Having implemented both (and still maintaining systems with both), here is my pro/con list:

  • Shunting Yard:
    • code is short
    • easy when associated to simple rules/priority tables
    • annoying to debug when something goes wrong
  • Recursive Descent:
    • code is longer
    • more complex when all you have are simple rules/priorities
    • easier to extend or add special-case syntaxes
    • relatively simpler to debug (follows a more "human" flow)

Or in other words, when you're dealing only with math formulas, Shunting Yard is probably the way to go, but if you feel you may need for more complexity later on, recursive descent might be more flexible/extensible/maintainable and pay back in the long run.

Compiler's compilers (Lex/Yacc, Flex/Bison etc.) would be an obvious third choice, but I don't know any maintained implementation for Delphi, and for simple maths formulas, they're overkill.

Sign up to request clarification or add additional context in comments.

Comments

5

What is more simpler to use, the Shunting Yard Algorithm, or the Recursive-Descent algorithm for parsing the formulas?

The simplest would be the one you understand better. If there's a tie I'd go with recursive-descent, it can be used for both plain expressions and more complex scripts (ie: interpreter).

If this is not homework (hence you're not required to implement the code yourself), look into ready-made solutions (example: dwscript, or Pascal Script). You can also use a "compiler compiler", a toolset designed for producing lexycal analyzers and parsers. I can't recommend any because, to be honest, I haven't found one to satisfy my needs. You can start your search with TP Lex/Yacc.

will i reach the work of the interpreter in some stage of my work, and how?

An interpreter would usually work on a script and perform multiple operations (execute statements). An expression evaluator only works with expressions, providing a result (or an expression tree that can be used to obtain the result).

An interpreter would definitively need an expression parser (or evaluator), but not the other way around.

7 Comments

may i ask an additional quest? Can the interpreter while executing a script execute a predefined script? as performing a function that is not built in it, but defined by the user?
I suppose this is a how, not a if question. The answer to the if is obviously yes (Python is an interpreted language, and Python can do anything that can be done). The "how" is a matter of your design, and definitively too large a subject for a comment. Feel free to ask a new question (the Ask Question button at the top-right) and ask for implementation ideas.
Or just look at the JVCL JvInterpreter which includes a simple pascal implementation of just such a thing.
dwscript and PascalScript (I like PascalScript better, but I've worked with both) are both open source, one can also look in there. To be perfectly honest for the OP, if you need to ask for implementation details, you're probably not prepared to undertake this just yet. Unless you do it for fun and entertainment (and I honestly see the entertainment value in designing something like this)
What should i do to be prepared? I am reading more about this Pascal Script, but is it compatible with Delphi 2010? As i saw it is only compatible till 2009... Actually it is for work, but i can see fun in this too. is it compatible also with win7,64-bits?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.