Pyparsing问题 [英] Pyparsing Question

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

问题描述

大家好,


我对PyParsing有疑问。我正在尝试为

分层待办事项列表格式创建一个解析器,但是遇到了绊脚石。我有

解析器用于列表标题(标题和描述),以及正文

(todo项目的递归下降)。


单独他们工作正常,结合他们抛出异常。

代码如下:


#!/ usr / bin / python

#parser.py

导入pyparsing为pp


def grammar():

underline = pp .Word(" =")。s​​uppress()

dotnum = pp.Combine(pp.Word(pp.nums)+"。")

textline = pp.Combine(pp.Group(pp.Word(pp.alphas,pp.printables)+

pp.restOfLine))

number = pp.Group( pp.OneOrMore(dotnum))


headtitle = textline

headdescription = pp.ZeroOrMore(textline)

head = pp。组(头衔+下划线+头部描述)


taskname = pp.OneOrMore(dotnum)+ textline

task = pp.Forward()

subtask = pp.Group(dotnum + task)

task<< (taskname + pp.ZeroOrMore(子任务))

maintask = pp.Group(pp.LineStart()+任务)

parser = pp.OneOrMore( maintask)


返回头,解析器


text ="""

我的标题

========


多行文字的文字。

更多测试

及以上。


"""


text2 ="""


1.任务1

1.1。子任务

1.1.1。更多任务。

1.2。另一个子任务

2.任务2

2.1。子任务再次"


头,解析器=语法()


print head.parseString(text)

print parser.parseString(text2)

comb = head + pp.OneOrMore(pp.LineStart()+ pp.restOfLine)+解析器

print comb.parseString(text + text2)

#============================ ===================== ==================


现在前两个print语句输出解析树,因为我会期望
,但组合解析器失败并出现异常:


Traceback(最近一次调用)最后):

文件" parser.py",第50行,在?

打印comb.parseString(text + text2)

。 。

.. [Stacktrace剪辑]

..

加息

pyparsing.ParseException:预期开始line(在char 81),(行:9,

col:1)


任何帮助表示赞赏!


干杯,


-

Ant。

Hi all,

I have a question on PyParsing. I am trying to create a parser for a
hierarchical todo list format, but have hit a stumbling block. I have
parsers for the header of the list (title and description), and the body
(recursive descent on todo items).

Individually they are working fine, combined they throw an exception.
The code follows:

#!/usr/bin/python
# parser.py
import pyparsing as pp

def grammar():
underline = pp.Word("=").suppress()
dotnum = pp.Combine(pp.Word(pp.nums) + ".")
textline = pp.Combine(pp.Group(pp.Word(pp.alphas, pp.printables) +
pp.restOfLine))
number = pp.Group(pp.OneOrMore(dotnum))

headtitle = textline
headdescription = pp.ZeroOrMore(textline)
head = pp.Group(headtitle + underline + headdescription)

taskname = pp.OneOrMore(dotnum) + textline
task = pp.Forward()
subtask = pp.Group(dotnum + task)
task << (taskname + pp.ZeroOrMore(subtask))
maintask = pp.Group(pp.LineStart() + task)

parser = pp.OneOrMore(maintask)

return head, parser

text = """
My Title
========

Text on a longer line of several words.
More test
and more.

"""

text2 = """

1. Task 1
1.1. Subtask
1.1.1. More tasks.
1.2. Another subtask
2. Task 2
2.1. Subtask again"""

head, parser = grammar()

print head.parseString(text)
print parser.parseString(text2)

comb = head + pp.OneOrMore(pp.LineStart() + pp.restOfLine) + parser
print comb.parseString(text + text2)

#================================================= ==================

Now the first two print statements output the parse tree as I would
expect, but the combined parser fails with an exception:

Traceback (most recent call last):
File "parser.py", line 50, in ?
print comb.parseString(text + text2)
..
.. [Stacktrace snipped]
..
raise exc
pyparsing.ParseException: Expected start of line (at char 81), (line:9,
col:1)

Any help appreciated!

Cheers,

--
Ant.

推荐答案

5月16日,6:43 * am,Ant< ant ... @ gmail.comwrote:
On May 16, 6:43*am, Ant <ant...@gmail.comwrote:

大家好,


我对PyParsing有疑问。我正在尝试为

分层待办事项列表格式创建一个解析器,但是遇到了绊脚石。我有

解析器用于列表标题(标题和描述),以及正文

(todo项目的递归下降)。
Hi all,

I have a question on PyParsing. I am trying to create a parser for a
hierarchical todo list format, but have hit a stumbling block. I have
parsers for the header of the list (title and description), and the body
(recursive descent on todo items).



LineStart *真的*想要在一行开头解析。

你的文本行读取但不包括LineEnd。尝试制作

这些更改。


1.将文本行更改为:


textline = pp.Combine(

pp.Group(pp.Word(pp.alphas,pp.printables)+ pp.restOfLine))+

\

pp。 LineEnd()。suppress()


2.将梳子更改为:


comb = head + parser


通过这些更改,我的代码版本运行正常。


- Paul

LineStart *really* wants to be parsed at the beginning of a line.
Your textline reads up to but not including the LineEnd. Try making
these changes.

1. Change textline to:

textline = pp.Combine(
pp.Group(pp.Word(pp.alphas, pp.printables) + pp.restOfLine)) +
\
pp.LineEnd().suppress()

2. Change comb to:

comb = head + parser

With these changes, my version of your code runs ok.

-- Paul


5月16日, 6:43 * am,Ant< ant ... @ gmail.comwrote:
On May 16, 6:43*am, Ant <ant...@gmail.comwrote:

大家好,


我对PyParsing有疑问。我正在尝试为

分层待办事项列表格式创建一个解析器,但是遇到了绊脚石。我有

解析器用于列表标题(标题和描述),以及正文

(todo项目的递归下降)。


单独他们工作正常,结合他们抛出异常。

代码如下:


#!/ usr / bin / python

#parser.py

导入pyparsing为pp


def grammar():

* * * underline = pp.Word(" =")。s​​uppress()

* * * dotnum = pp.Combine(pp.Word(pp.nums)+"。")

* * * textline = pp.Combine(pp.Group(pp.Word(pp.alphas,pp.printables)+

pp.restOfLine))

* * * number = pp.Group(pp.OneOrMore(dotnum))


* * * headtitle = textline

* * * headdescription = pp.ZeroOrMore(textline)

* * * head = pp.Group(headtitle + underline + headdescription)


* * * taskname = pp.OneOrMore( dotnum)+ textline

* * * task = pp.Forward()

* * * subtask = pp.Group(dotnum + task)

* * * task<< (taskname + pp.ZeroOrMore(子任务))

* * * maintask = pp.Group(pp.LineStart()+任务)


* * * parser = pp.OneOrMore(maintask)


* * *返回头,解析器


text ="""


我的标题

========


多行文字的文字。

更多测试

等等。


"""


text2 ="""


1.任务1

* * * 1.1。子任务

* * * * * 1.1.1。更多任务。

* * * 1.2。另一个子任务

2.任务2

* * * 2.1。子任务再次"


头,解析器=语法()


print head.parseString(text)

print parser.parseString(text2)

comb = head + pp.OneOrMore(pp.LineStart()+ pp.restOfLine)+解析器

print comb.parseString(text + text2)

#============================ ===================== ==================


现在前两个print语句输出解析树,因为我会期望
,但组合解析器失败并出现异常:


Traceback(最近一次调用)最后):

* *文件parser.py,第50行,在?

* * * print comb.parseString(text + text2)



。 [Stacktrace剪断]



* * *提高exc

pyparsing.ParseException:预期行开始(在char 81),(行:9,

col:1)


任何帮助表示赞赏!


干杯,


-

Ant。
Hi all,

I have a question on PyParsing. I am trying to create a parser for a
hierarchical todo list format, but have hit a stumbling block. I have
parsers for the header of the list (title and description), and the body
(recursive descent on todo items).

Individually they are working fine, combined they throw an exception.
The code follows:

#!/usr/bin/python
# parser.py
import pyparsing as pp

def grammar():
* * *underline = pp.Word("=").suppress()
* * *dotnum = pp.Combine(pp.Word(pp.nums) + ".")
* * *textline = pp.Combine(pp.Group(pp.Word(pp.alphas, pp.printables) +
pp.restOfLine))
* * *number = pp.Group(pp.OneOrMore(dotnum))

* * *headtitle = textline
* * *headdescription = pp.ZeroOrMore(textline)
* * *head = pp.Group(headtitle + underline + headdescription)

* * *taskname = pp.OneOrMore(dotnum) + textline
* * *task = pp.Forward()
* * *subtask = pp.Group(dotnum + task)
* * *task << (taskname + pp.ZeroOrMore(subtask))
* * *maintask = pp.Group(pp.LineStart() + task)

* * *parser = pp.OneOrMore(maintask)

* * *return head, parser

text = """

My Title
========

Text on a longer line of several words.
More test
and more.

"""

text2 = """

1. Task 1
* * *1.1. Subtask
* * * * *1.1.1. More tasks.
* * *1.2. Another subtask
2. Task 2
* * *2.1. Subtask again"""

head, parser = grammar()

print head.parseString(text)
print parser.parseString(text2)

comb = head + pp.OneOrMore(pp.LineStart() + pp.restOfLine) + parser
print comb.parseString(text + text2)

#================================================= ==================

Now the first two print statements output the parse tree as I would
expect, but the combined parser fails with an exception:

Traceback (most recent call last):
* *File "parser.py", line 50, in ?
* * *print comb.parseString(text + text2)
.
. [Stacktrace snipped]
.
* * *raise exc
pyparsing.ParseException: Expected start of line (at char 81), (line:9,
col:1)

Any help appreciated!

Cheers,

--
Ant.



我认为应该重载+运算符以使字符串包含

换行符。 Python 3.0 print有围绕它的括号;不是吗

将它们拿出去是否有意义?

I hold that the + operator should be overloaded for strings to include
newlines. Python 3.0 print has parentheses around it; wouldn''t it
make sense to take them out?


嗨Paul,
Hi Paul,

LineStart *真的*想要在一行的开头解析。

你的文本行读取但不包括LineEnd。尝试制作

这些更改。


1.将文本行更改为:


textline = pp.Combine(

pp.Group(pp.Word(pp.alphas,pp.printables)+ pp.restOfLine))+

\

pp。 LineEnd()。suppress()
LineStart *really* wants to be parsed at the beginning of a line.
Your textline reads up to but not including the LineEnd. Try making
these changes.

1. Change textline to:

textline = pp.Combine(
pp.Group(pp.Word(pp.alphas, pp.printables) + pp.restOfLine)) +
\
pp.LineEnd().suppress()



啊 - 所以restOfLine排除实际的行结束吗?

Ah - so restOfLine excludes the actual line ending does it?


2.将梳子换成:


comb = head + parser
2. Change comb to:

comb = head + parser



是的 - 我原来是这样的。我添加了垃圾来试图修复

问题而忘了把它取回来!感谢您的建议 - 它的工作原价现在很好,并且会为扩展列表格式提供基础。


谢谢,

Ant ...

Yes - I''d got this originally. I added the garbage to try to fix the
problem and forgot to take it back out! Thanks for the advice - it works
fine now, and will provide a base for extending the list format.

Thanks,

Ant...


这篇关于Pyparsing问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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