我如何手动编写复杂的公式解析器? [英] How would I code a complex formula parser manually?

查看:152
本文介绍了我如何手动编写复杂的公式解析器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,这是语言不可知的,我更喜欢在C#或F#中做,但我更感兴趣这个问题怎么会这样工作。

Hm, this is language - agnostic, I would prefer doing it in C# or F#, but I'm more interested this time in the question "how would that work anyway".

我想要完成的是什么ist:

What I want to accomplish ist:

a)我想学习它 - 这是关于我的自我这是一个有趣的项目,显示我对这个东西非常好。

a) I want to LEARN it - it's about my ego this time, it's for a fun project where I want to show myself that I'm a really good at this stuff

b)我知道一点关于EBNF(虽然我还不知道,工作在EBNF - Irony.NET是正确的,我检查了例子,但这是有点不祥我)

b) I know a tiny little bit about EBNF (although I don't know yet, how operator precedence works in EBNF - Irony.NET does it right, I checked the examples, but this is a bit ominous to me)

c)我的解析器应该能够采取这个:5 *(3 +(2-9 *(5/7))+ 9),并给我正确的结果。

c) My parser should be able to take this: 5 * (3 + (2 - 9 * (5 / 7)) + 9) for example and give me the right results

d坦白说,这似乎是编写一个编译器或甚至一个解释器为我最大的问题。我会没有问题生成甚至64位汇编代码(我可以手动编写汇编程序),但公式解析...

d) To be quite frankly, this seems to be the biggest problem in writing a compiler or even an interpreter for me. I would have no problem generating even 64 bit assembler code (I CAN write assembler manually), but the formula parser...

e)另一个想法:即使简单的计算机像我的老Sharp 1246S只有大约2kB的内存)可以做到...它不能是那么难,对吧?甚至非常,非常老的编程语言有公式评估... BASIC是从1964年,他们已经可以计算作为一个例子提出的公式的类型

e) Another thought: even simple computers (like my old Sharp 1246S with only about 2kB of RAM) can do that... it can't be THAT hard, right? And even very, very old programming languages have formula evaluation... BASIC is from 1964 and they already could calculate the kind of formula I presented as an example

f)A很少的想法,几个灵感会真的足够 - 我只是没有线索如何做运算符优先级和括号 - 但我知道,它涉及一个AST和许多人使用一个堆栈

f) A few ideas, a few inspirations would be really enough - I just have no clue how to do operator precedence and the parentheses - I DO, however, know that it involves an AST and that many people use a stack

那么,你觉得怎么样?

推荐答案

http://en.wikipedia.org/wiki/Recursive_descent_parserrel =nofollow noreferrer>递归下降解析器。

You should go learn about Recursive Descent parsers.

查看Code Golf练习在这样做时,有10种不同的方式:

Check out a Code Golf exercise in doing just this, 10 different ways:

代码高尔夫:数学表达式计算器(尊重PEMDAS)

其中一些高尔夫解决方案是递归下降解析器不同的方式。

Several of these "golf" solutions are recursive descent parsers just coded in different ways.

你会发现,只是表达式解析是编译器中最容易的。解析其余的语言更难,但是理解代码元素如何交互以及如何生成好的代码要困难得多。

You'll find that doing just expression parsing is by far the easiest thing in a compiler. Parsing the rest of the language is harder, but understanding how the code elements interact and how to generate good code is far more difficult.

您可能还会对如何使用BNF表达一个解析器,以及如何使用BNF做某事。这里是
如何以符号方式解析和操作代数的示例显式BNF和隐式AST作为基础。这不是编译器传统上做的,但是所做的机制深深地建立在编译器技术中。

You may also be interested in how to express a parser using BNF, and how to do something with that BNF. Here's an example of how to parse and manipulate algebra symbolically with an explicit BNF and an implicit AST as a foundation. This isn't what compilers traditionally do, but the machinery that does is founded deeply in compiler technology.

这篇关于我如何手动编写复杂的公式解析器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆