解析器解释器编程

发布于 2025-01-08 03:51:22 字数 292 浏览 0 评论 0原文

我想编写一个包来计算某个输入公式的结果值,

我根据分流场算法(Dijikstra)创建了解析器, 我想创建允许用户使用的函数库(例如:sin()cos() 函数) 然后我想知道我的下一步应该是什么;所以我有一些问题:

  1. 调车场算法和解析公式的递归下降算法哪个更容易使用?

  2. 我会在我工作的某个阶段达到口译员的工作吗?

谢谢...

请注意我正在使用 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:

  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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜司空 2025-01-15 03:51:22

实施了这两种方法(并且仍然维护这两种方法的系统),这是我的赞成/反对清单:

  • 调车场:
    • 代码很短
    • 与简单规则/优先级表关联时很容易
    • 出现问题时调试很烦人
  • 递归下降:
    • 代码较长
    • 当您拥有的只是简单的规则/优先级时,情况会变得更加复杂
    • 更容易扩展或添加特殊情况语法
    • 调试相对简单(遵循更“人性化”的流程)
    • 当您

,或者换句话说,当您处理仅使用数学公式,Shunting Yard 可能是可行的方法,但如果您觉得以后可能需要更复杂的东西,递归下降可能会更灵活/可扩展/可维护,并且从长远来看会带来回报。

编译器的编译器(Lex/Yacc、Flex/Bison 等)显然是第三种选择,但我不知道 Delphi 是否有任何维护的实现,而且对于简单的数学公式来说,它们太过分了。

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.

无畏 2025-01-15 03:51:22

使用调车场算法和解析公式的递归下降算法哪个更简单?

最简单的就是您更好理解的。如果有一个平局,我会选择递归下降,它可以用于简单的表达式和更复杂的脚本(即:解释器)。

如果这不是家庭作业(因此您不需要自己实现代码),请研究现成的解决方案(例如:dwscriptPascal 脚本)。您还可以使用“编译器编译器”,这是一个设计用于生成词法分析器和解析器的工具集。我无法推荐任何一个,因为说实话,我还没有找到一个可以满足我的需求的。您可以使用 TP Lex/Yacc 开始搜索。

我会在工作的某个阶段达到口译员的工作吗?

解释器通常会处理脚本并执行多个操作(执行语句)。表达式求值器仅适用于表达式,提供结果(或可用于获取结果的表达式树)。

解释器肯定需要表达式解析器(或求值器),但反之则不然。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文