python 分割和评估
如何将一个字符串(带后缀)拆分为一个带括号的字符串?我的意思是,如果用户输入 3 (3 6 *) *
我希望解释器能够乘以 3 18 *
并理解文本中的括号。这可能吗?类似于 str.split()
但带有开始和结束分隔符?
难道我不现实吗?我发现 shlex
模块类似,但它只引用 AFAIK。
How might I split a string (with postfix) into one with parentheses? What I mean is, if the user inputs 3 (3 6 *) *
I want the interpreter to be able to multiply 3 18 *
and understand the parentheses in the text. Is this possible? Like a str.split()
but with a start and end delimiter?
Am I not being realistic? I found the shlex
module similar, but it only does quotes AFAIK.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不认为使用
re
是一个坏主意。您可以一次用其运算结果替换嵌套括号。对我来说似乎很简单:并像这样使用:
I don't think using
re
is such a bad idea. You could just go through replacing the nested parenthesis one at a time with the result of their operation. Seems pretty straight forward to me:And use like so:
正如克里斯的,呃,各种各样的评论所指出的,你不能有一个(真正的)正则表达式来解析嵌套括号。 (根据他的说法,有一些技巧和扩展可以扩展表达式语言,使其能够解析上下文无关语法。这是我第一次听说,但对我来说这听起来是个坏主意。 )
阅读本文。
As Chris's, uh, variegated comments indicated, you can't have an (true) regular expression that will parse nested parentheses. (According to him, there are some sort of hacks and extensions that would broaden the expression-language to let it parse context-free grammars. This is the first I've heard of that, but it sounds like a bad idea to me.)
Read this.
您可以编写一个 正则表达式 来提取带括号的子表达式。
re.split
可能会有所帮助,如果什么你关心的是在匹配的括号上分裂。一个简单的正则表达式可以让您挑选出子表达式。产生
You could write a regular expression to extract parenthesized subexpressions.
re.split
may be helpful if what you care about is splitting on matched parentheses. A straightforward regular expression would let you pick out the subexpressions.produces
首先,了解当运算符的元数已知时,后缀表示法中不需要括号。了解原因:
从左侧开始,将操作数堆叠起来,直到遇到运算符:
我们知道
*
是二元运算符,因此从堆栈中弹出两个操作数,应用运算符,然后将结果推回on:继续累积操作数,直到遇到另一个运算符:
当表达式被使用时(假设其格式良好),堆栈将包含一个值:结果。
但你的问题是关于括号的:假设你的愿望是能够解析任意嵌套括号,正则表达式将无法帮助你,原因如下此处。您需要能够识别上下文无关语法的东西,即下推自动机。
但我认为,你正在寻找的不仅仅是抽象的计算机科学术语;而是更多的东西。 这里是一个相关问题,其中包含一些关于Shunting Yard 算法,将带括号的中缀表达式转换为后缀表达式。
编辑:好吧,我应该说“true正则表达式单独不会帮助你。”您可以使用正则表达式(匹配最里面的括号对)和替换(实际上)将括号表达式视为其自己的隐式解析树。但调车场算法更酷:)
First, understand that parenthesis are not necessary in postfix notation when the arity of an operator is known. To see why:
Starting from the left, stack operands until you encounter an operator:
We know that
*
is a binary operator, so pop two operands off the stack, apply the operator, and push the result back on:Continue to accumulate operands until you encounter another operator:
When the expression is consumed (assuming it's well-formed) the stack will contain one value: the result.
But your question is about parenthesis: assuming that your desire is to be able to parse arbitrarily nested parentheses, regular expressions will not help you, for reasons explained here. You need something that can recognize a context free grammar, i.e. a pushdown automaton.
But you're looking, I take it, for something more than abstract computer science-speak; here's a related question that has some useful information on the Shunting Yard Algorithm, which converts parenthesized infix expressions into postfix expressions.
Edit: Ok, I should have said "true regular expressions alone will not help you." You could use regular expressions (matching innermost pairs of parenthesis) and substitution to (in effect) treat a parenthesized expression as its own implicit parse tree. But the Shunting Yard Algorithm is cooler :)
如果我错了请纠正我,但你的表达是 RPD。如果是这样,则实际上不需要使用括号。请在下面找到我的示例(未优化并且很奇怪):
Correct me if i am wrong but your expression is the RPD. If so there is no actual need to use parenthesis. Please find my sample(not optimized and weird) below: