gpt4 book ai didi

python - Pyparsing - 从解析 Action 解析子语法

转载 作者:太空宇宙 更新时间:2023-11-04 03:28:08 25 4
gpt4 key购买 nike

我有一个由“语句”列表组成的文件,每个语句都包含一个可变长度的逗号分隔的“列”列表,其中语句的末尾用分号表示:

head(
col1,
col2
);
head2(
col1,
col2,
col3
);

我有完整文件的工作语法,但如果其中一列中存在语法错误,则错误消息的信息不是很丰富 - 它基本上是说整个语句在左括号中是错误的。

所以我想单独解析每一列,以便可以更详细地报告该列中的错误。以下将解析整个文件(两个语句)并打印每个语句和解析操作中的列列表:

from pyparsing import Group, Literal, ParseException
from pyparsing import SkipTo, Suppress, commaSeparatedList
from pyparsing import Word, ZeroOrMore, alphas, alphanums

def parse_stmt(strng, loc, toks):
print("++",toks,"++",loc,"++")

def parse_col(strng, loc, toks):
# cols = commaSeparatedList(toks)
print("==",toks,"===",loc,"==")

specs = '$_-'
name = Word(alphas+specs, alphanums+specs)
term = Literal(')') + Literal(';')
init = name
statement = Group(init.setParseAction(parse_stmt) + Suppress(Literal('(')) + \
SkipTo(term).setParseAction(parse_col) + Suppress(term))
script = ZeroOrMore(statement)

ss = '\
abc(d 45, e, f g h); \
s2("much" "stuff", 34 48); \
'

print(ss)
try:
print(script.parseString(ss))
except ParseException as err:
print(" "*(err.column-1) + "^")
print(err)

导致:

abc(d 45, e, f g h); s2("much" "stuff", 34 48); 
++ ['abc'] ++ 0 ++
== ['d 45, e, f g h'] === 4 ==
++ ['s2'] ++ 21 ++
== ['"much" "stuff", 34 48'] === 24 ==
[['abc', 'd 45, e, f g h'], ['s2', '"much" "stuff", 34 48']]

但是当我尝试通过取消注释 cols = commaSeparatedList(toks) 来解析列列表时,一切都崩溃了:

abc(d 45, e, f g h); s2("much" "stuff", 34 48); 
++ ['abc'] ++ 0 ++
Traceback (most recent call last):
File "pt.py", line 27, in <module>
print(script.parseString(ss))
File "pyparsing.py", line 1115, in parseString
loc, tokens = self._parse( instring, 0 )
File "pyparsing.py", line 989, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "pyparsing.py", line 2732, in parseImpl
loc, tokens = self.expr._parse( instring, loc, doActions, callPreParse=False )
File "pyparsing.py", line 989, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "pyparsing.py", line 2624, in parseImpl
return self.expr._parse( instring, loc, doActions, callPreParse=False )
File "pyparsing.py", line 989, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "pyparsing.py", line 2378, in parseImpl
loc, exprtokens = e._parse( instring, loc, doActions )
File "pyparsing.py", line 1015, in _parseNoCache
tokens = fn( instring, tokensStart, retTokens )
File "pyparsing.py", line 779, in wrapper
ret = func(*args[limit[0]:])
TypeError: parse_col() missing 3 required positional arguments: 'strng', 'loc', and 'toks'

我的问题不涉及多线程或递归,但它似乎确实表明对 pyparsing 的重入调用存在问题,可能与 context in pyparsing parse actions besides globals 有关.

有没有一种从 Action 中调用 pyparsing 例程的简单方法,一种比该问题的答案更简单的方法?

最佳答案

改变

cols = commaSeparatedList(toks)

cols = commaSeparatedList.parseString(toks[0])

似乎几乎可以修复它,至少对于您提供的数据示例而言。然而,“much”“stuff”似乎破坏了 csl 解析器,可能是因为它期望在“much”的“结束”之后有一个逗号。

关于python - Pyparsing - 从解析 Action 解析子语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32204260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com