解析器解释器编程
我想编写一个包来计算某个输入公式的结果值,
我根据分流场算法(Dijikstra)创建了解析器, 我想创建允许用户使用的函数库(例如:sin()
和 cos()
函数) 然后我想知道我的下一步应该是什么;所以我有一些问题:
调车场算法和解析公式的递归下降算法哪个更容易使用?
我会在我工作的某个阶段达到口译员的工作吗?
谢谢...
请注意我正在使用 Delphi 对其进行编程
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:
What is more simpler to use, the Shunting Yard Algorithm, or the Recursive-Descent algorithm for parsing the formulas?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实施了这两种方法(并且仍然维护这两种方法的系统),这是我的赞成/反对清单:
当您
,或者换句话说,当您处理仅使用数学公式,Shunting Yard 可能是可行的方法,但如果您觉得以后可能需要更复杂的东西,递归下降可能会更灵活/可扩展/可维护,并且从长远来看会带来回报。
编译器的编译器(Lex/Yacc、Flex/Bison 等)显然是第三种选择,但我不知道 Delphi 是否有任何维护的实现,而且对于简单的数学公式来说,它们太过分了。
Having implemented both (and still maintaining systems with both), here is my pro/con list:
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.
最简单的就是您更好理解的。如果有一个平局,我会选择递归下降,它可以用于简单的表达式和更复杂的脚本(即:解释器)。
如果这不是家庭作业(因此您不需要自己实现代码),请研究现成的解决方案(例如:dwscript 或 Pascal 脚本)。您还可以使用“编译器编译器”,这是一个设计用于生成词法分析器和解析器的工具集。我无法推荐任何一个,因为说实话,我还没有找到一个可以满足我的需求的。您可以使用 TP Lex/Yacc 开始搜索。
解释器通常会处理脚本并执行多个操作(执行语句)。表达式求值器仅适用于表达式,提供结果(或可用于获取结果的表达式树)。
解释器肯定需要表达式解析器(或求值器),但反之则不然。
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.
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.