Pyparsing是否支持上下文相关语法? [英] Does Pyparsing Support Context-Sensitive Grammars?

查看:127
本文介绍了Pyparsing是否支持上下文相关语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用的术语不正确,请原谅;也许只是用正确"的词来描述我想要的东西就足以让我自己找到答案.

Forgive me if I have the incorrect terminology; perhaps just getting the "right" words to describe what I want is enough for me to find the answer on my own.

我正在研究ODL(对象描述语言)的解析器,据我所知,这是一种奥秘的语言,目前仅由NASA PDS(行星数据系统;这是NASA向公众提供数据的方式)使用.幸运的是,PDS最终转向了XML,但是我仍然必须为在截止日期之前完成的任务编写软件.

I am working on a parser for ODL (Object Description Language), an arcane language that as far as I can tell is now used only by NASA PDS (Planetary Data Systems; it's how NASA makes its data available to the public). Fortunately, PDS is finally moving to XML, but I still have to write software for a mission that fell just before the cutoff.

ODL以如下方式定义对象:

ODL defines objects in something like the following manner:

OBJECT              = TABLE
  ROWS              = 128
  ROW_BYTES         = 512 
END_OBJECT          = TABLE

我正在尝试使用pyparsing编写解析器,并且在进入上述构造之前一直做得很好.

I am attempting to write a parser with pyparsing, and I was doing fine right up until I came to the above construction.

我必须创建一些规则,以确保OBJECT行的右侧值与END_OBJECT的RHV相同.但是我似乎无法将其放入pyparsing规则中.我可以确保这两个语法上都是有效的值,但是我无法采取额外的步骤来确保两个值都是相同的.

I have to create some rule that is able to ensure that the right-hand-value of the OBJECT line is identical to the RHV of END_OBJECT. But I can't seem to put that into a pyparsing rule. I can ensure that both are syntactically valid values, but I can't go the extra step and ensure that the values are identical.

  1. 我是否凭直觉纠正这是上下文相关的语法?我应该用这个短语来描述这个问题吗?
  2. 无论从理论上讲哪种语法,pyparsing都能处理这种结构吗?
  3. 如果pyparsing无法处理它,是否有另一个Python工具可以处理? ply(lex/yacc的Python实现)怎么样?
  1. Am I correct in my intuition that this is a context-sensitive grammar? Is that the phrase I should be using to describe this problem?
  2. Whatever kind of grammar this is in the theoretical sense, is pyparsing able to handle this kind of construction?
  3. If pyparsing is not able to handle it, is there another Python tool capable of doing so? How about ply (the Python implementation of lex/yacc)?

推荐答案

实际上,它是上下文相关语言的语法,传统上抽象为wcw,其中w在(a | b)*中(请注意wcw'(其中'表示逆转)是无上下文的.

It is in fact a grammar for a context-sensitive language, classically abstracted as wcw where w is in (a|b)* (note that wcw' , where ' indicates reversal, is context-free).

解析表达式语法能够使用语义谓词解析wcw类型的语言. PyParsing为此提供了matchPreviousExpr()matchPreviousLiteral()辅助方法,例如

Parsing Expression Grammars are capable of parsing wcw-type languages by using semantic predicates. PyParsing provides the matchPreviousExpr() and matchPreviousLiteral() helper methods for this very purpose, e.g.

w = Word("ab")
s = w + "c" + matchPreviousExpr(w)

因此,在您的情况下,您可能会做类似的事情

So in your case you'd probably do something like

table_name = Word(alphas, alphanums)
object = Literal("OBJECT") + "=" + table_name + ... +
  Literal("END_OBJECT") + "=" +matchPreviousExpr(table_name)

这篇关于Pyparsing是否支持上下文相关语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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