py解析错误

发布于 2025-01-03 21:19:11 字数 333 浏览 1 评论 0原文

我在 pyparsing 中遇到此错误

from pyparsing import Word,alphas,nums,Or,Regex,StringEnd
ws = Regex('\s*')
dot = "."
w = Word(alphas) + (ws | dot) + StringEnd()
w.leaveWhitespace()
w.parseString('AMIT.')

返回以下错误:

ParseException: Expected end of text (at char 4), (line:1, col:5)

I am stuck at this error in pyparsing

from pyparsing import Word,alphas,nums,Or,Regex,StringEnd
ws = Regex('\s*')
dot = "."
w = Word(alphas) + (ws | dot) + StringEnd()
w.leaveWhitespace()
w.parseString('AMIT.')

Returns the following error:

ParseException: Expected end of text (at char 4), (line:1, col:5)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烧了回忆取暖 2025-01-10 21:19:11

| 创建“首先匹配”表达式,而不是“最长匹配”。

第一个替代方案是正则表达式,它将匹配 0 个或多个空白字符。事实上,这确实匹配,因此不会解析该点。

那么下一个要解析的元素是StringEnd,但解析位置仍然位于“.”处——所以,失败!

以下是通过向语法表达式添加 setDebug() 调用而获得的一些更详细的输出:

>>> w = Word(alphas).setDebug() + (ws.setDebug() | dot.setDebug()) + StringEnd()
>>> w.parseString('AMIT.')
Match W:(abcd...) at loc 0(1,1)
Matched W:(abcd...) -> ['AMIT']
Match Re:('\\s*') at loc 4(1,5)
Matched Re:('\\s*') -> ['']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\python26\lib\site-packages\pyparsing-1.5.6-py2.6.egg\pyparsing.py", line 1032, in parseString
    raise exc
pyparsing.ParseException: Expected end of text (at char 4), (line:1, col:5)

要使语法正常工作,您可以:

  • | 运算符更改为 ^(匹配最长而不是首先匹配)

  • 更改正则表达式为 \s+ 而不是\s* (以便匹配至少需要一个空格)

  • 将第二个术语更改为可选(点)

一般来说,对空白的显式测试与pyparsing 哲学——pyparsing 与 re 不同。

| creates a "match first" expression, not "match longest".

The first alternative is the regex, which will match 0 or more whitespace characters. This, in fact, does match, so the dot is not parsed.

Then the next element to parse is StringEnd, but the parse position is still located at the '.'—so, fail!

Here is some more detailed output by adding setDebug() calls to your grammar expressions:

>>> w = Word(alphas).setDebug() + (ws.setDebug() | dot.setDebug()) + StringEnd()
>>> w.parseString('AMIT.')
Match W:(abcd...) at loc 0(1,1)
Matched W:(abcd...) -> ['AMIT']
Match Re:('\\s*') at loc 4(1,5)
Matched Re:('\\s*') -> ['']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\python26\lib\site-packages\pyparsing-1.5.6-py2.6.egg\pyparsing.py", line 1032, in parseString
    raise exc
pyparsing.ParseException: Expected end of text (at char 4), (line:1, col:5)

To get your grammar to work you could:

  • change the | operator to ^ (match longest instead of match first)

  • change the regex to \s+ instead of \s* (so that at least one space was required for a match)

  • change your second term to Optional(dot)

In general, explicit testing for whitespace is not consistent with the pyparsing philosophy—pyparsing is not the same as re.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文