在使用pyparsing制作递归解析器时需要帮助 [英] Need help on making the recursive parser using pyparsing

查看:84
本文介绍了在使用pyparsing制作递归解析器时需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试python pyparsing进行解析.在进行递归解析器时,我陷入了困境.

I am trying the python pyparsing for parsing. I got stuck up while making the recursive parser.

让我解释一下问题

我要制作元素的笛卡尔积.语法是

I want to make the Cartesian product of the elements. The syntax is

cross({elements },{element})

我用更具体的方式表达

cross({a},{c1}) or cross({a,b},{c1}) or cross({a,b,c,d},{c1}) or 

因此,一般形式是第一组将具有n个元素(a,b,c,d).第二组将具有一个元素,因此最终输出将为笛卡尔积.

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.

语法必须递归,因为它可以像n一样进入n级

The syntax is to be made recursive because it can go to n level like

cross(cross({a,b},{c1}),{c2})

这意味着将a,b与c1交叉.让我们说结果吧.我们再次将其与c2交叉

This means cross a,b with c1. Lets say outcome us y. We again cross y it with c2

这可以一直到n级cross(cross(cross(cross ......

This can be till n level cross(cross(cross(cross......

我想要的是使用setparseAction初始化对象

What i want is to have object to be initialized using setparseAction

所以我将有2个班级

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.

推荐答案

您应该查看其他语言的定义,以了解通常的处理方式.

You should look at definitions of other languages to see how this is usually handled.

例如,看看如何定义乘法.

For example, look at how multiplication is defined.

不是

{expression} * {expression}

因为递归很难处理,并且没有隐含的从左到右的顺序.您经常看到的是类似的东西

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

{term} + {factor}
{factor} * {unary-expression}

其中优先级和操作员左右顺序.

Which puts priorities and a left-to-right ordering around the operators.

请查看类似 http://www.cs的内容. man.ac.uk/~pjj/bnf/c_syntax.bnf 举例说明这种结构的一般结构.

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.

这篇关于在使用pyparsing制作递归解析器时需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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