需要使用 pyparsing 制作递归解析器的帮助
我正在尝试使用 python pyparsing 进行解析。 我在制作递归解析器时陷入困境。
问题
让我解释一下我想要对元素进行笛卡尔积的 语法,
cross({elements },{element})
。 我以更具体的方式放置
cross({a},{c1}) or cross({a,b},{c1}) or cross({a,b,c,d},{c1}) or
因此一般形式是第一组将有 n 个元素(a,b,c,d)。 第二组将有一个元素,因此最终输出将是笛卡尔积。
语法将是递归的,因为它可以达到 n 级,就像
cross(cross({a,b},{c1}),{c2})
这意味着将 a、b 与 c1 交叉。 我们可以说结果是 y。 我们再次用 c2 交叉它,
这可以直到 n 级 cross(cross(cross(cross......
我想要的是使用 setparseAction 初始化对象
所以我将有 2 个类
class object1(object):
This will be used by a,b,c,d
class object2(object):
This will hold cross elements
我需要这方面的帮助我我无法制作递归解析器。
I am trying the python pyparsing for parsing. I got stuck up while making the recursive parser.
Let me explain the problem
I want to make the Cartesian product of the elements. The syntax is
cross({elements },{element})
I put in more specific way
cross({a},{c1}) or cross({a,b},{c1}) or cross({a,b,c,d},{c1}) or
So the general form is first group will have n elements (a,b,c,d). The second group will have one element that so the final output will be Cartesian Product.
The syntax is to be made recursive because it can go to n level like
cross(cross({a,b},{c1}),{c2})
This means cross a,b with c1. Lets say outcome us y. We again cross y it with c2
This can be till n level cross(cross(cross(cross......
What i want is to have object to be initialized using setparseAction
So i will have 2 class
class object1(object):
This will be used by a,b,c,d
class object2(object):
This will hold cross elements
I need help on this i am not able to make the recursive parser.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该查看其他语言的定义以了解通常是如何处理的。
例如,看看乘法是如何定义的。
不是
因为递归很难处理,并且没有隐含的从左到右的排序。 您更经常看到的是诸如
“Which 放置优先级”以及围绕运算符从左到右排序的内容。
看看类似 http://www.cs.man.ac .uk/~pjj/bnf/c_syntax.bnf 有关此类内容通常如何构造的示例。
You should look at definitions of other languages to see how this is usually handled.
For example, look at how multiplication is defined.
It isn't
Because the recursion is hard to deal with, and there's no implied left-to-right ordering. What you see more often are things like
Which puts priorities and a left-to-right ordering around the operators.
Look at something like http://www.cs.man.ac.uk/~pjj/bnf/c_syntax.bnf for examples of how things like this are commonly structured.
我同意@S.Lott 的观点,你应该重新考虑你的语法。
可以使用
Forward()
引入递归定义:输出:
I agree with @S.Lott you should reconsider your grammar.
Recursive definitions can be introduced using
Forward()
:Output:
我不知道这是否有任何帮助,但这就是你如何在 lepl 中做你想做的事情。 由于语法看起来是正确的,我认为它很容易翻译成 pyparsing。
输出是:
I don't know if this is any help, but here is how you would do what you want in lepl. Since the grammar appears to be correct I assume that it would be easy to translate to pyparsing.
The output is: