你可以使用普通的EX pressions实施分流算法? [英] Can you use regular expressions to implement the shunting algorithm?

查看:178
本文介绍了你可以使用普通的EX pressions实施分流算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能实现调车场算法在常规EX pressions方面?

Can you implement the shunting yard algorithm in terms of regular expressions?

推荐答案

我相信这已经在这里找到答案:<一href="http://stackoverflow.com/questions/4161553/can-the-shunting-yard-algorithm-parse-posix-regular-ex$p$pssions/4161681#4161681">Can调度室的算法解析POSIX正则EX pressions?

I believe this has been answered here: Can the shunting yard algorithm parse POSIX regular expressions?

我会说,回答你的问题是不,你不能   实现使用常规EX pression的调车场的算法。   这是同样的道理用你无法解析任意的HTML   常规的前pressions。它归结为:

I will say that the answer to your question is "no, you cannot implement the shunting yard algorithm using a regular expression." This is for the same reason you cannot parse arbitrary HTML using regular expressions. Which boils down to this:

普通前pressions没有栈。因为调车场   算法依赖于栈(推和弹出操作数为您转换   从缀至RPN),然后定期前pressions没有   计算权力来执行此任务。

Regular expressions do not have a stack. Because the shunting yard algorithm relies on a stack (to push and pop operands as you convert from infix to RPN), then regular expressions do not have the computational "power" to perform this task.

这掩盖了很多细节,但定期EX pression是一种方式   以限定一个正则语言。当你用一个普通的前pression,你   问电脑说:看文本的身体,告诉我   不论是否有任何这些字符串都在我的语言。语言   我定义使用普通的EX pression,我会指向的这个最   优秀的答案,你和每个人都读这应该upvote   对于更多的普通语言。

This glosses over many details, but a "regular expression" is one way to define a regular language. When you "use" a regular expression, you are asking the computer to say: "Look at a body of text and tell me whether or not any of those strings are in my language. The language that I defined using a regular expression." I'll point to this most excellent answer which you and everyone reading this should upvote for more on regular languages.

所以,现在你需要一些数学概念,以增加定期   为了语言,以创造更强大的语言。如果你要   在调车场算法描述为实现了模型   计算能力,那么你可能会说,算法会   描述为上下文无关文法(嘿,你知道,那是什么   链接使用了前pression解析树作为一个例子。)一个下推   自动。大一些的堆栈。

So now you need some mathematical concept to augment "regular languages" in order to create more powerful languages. If you were to characterize the shunting yard algorithm as an realization of a model of computational power, then you might say that the algorithm would be described as a context-free grammar (hey what do you know, that link uses an expression parse tree as an example.) A push-down automata. Something with a stack.

如果你不那么熟悉的自动机理论和复杂性   类,那么那些维基百科的文章都可能不是有帮助   不从根本上解释它们。

If you are less-than-familiar with automata theory and complexity classes, then those wikipedia articles are probably not that helpful without explaining them from the ground up.

点的存在,你可以使用正则表达式来帮忙写分流   码。但是,正则表达式不是很擅长做操作有   任意深度,这这个问题了。所以,我不会花太多   时间下去这个问题的正则表达式的途径。

The point being, you may be able to use regex to help writing shunting yard. But regex are not very good at doing operations that have an arbitrary depth, which this problem has. So I would not spend too much time going down the regex avenue for this problem.

这篇关于你可以使用普通的EX pressions实施分流算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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