gpt4 book ai didi

python - 使用 ebnf 和空格进行 pyparsing

转载 作者:太空宇宙 更新时间:2023-11-04 06:26:44 24 4
gpt4 key购买 nike

我正在使用 http://pyparsing.wikispaces.com/file/view/ebnf.py转换我的 ebnf 定义。

ebnf def 看起来像这样:

TEST = A, SPACE, A;

A = "AA" | "BB";
SPACE = " ";

如果我加载文件并尝试解析如下字符串:

e = ebnf.parse(ebnf_file)
e['TEST'].leaveWhitespace().parseString('AA BB') # same without leaveWhitespace()

我得到:

ParseException: Expected " " (at char 3), (line:1, col:4)

有人有想法/解决方案吗?

最佳答案

必须将 leaveWhitespace() 应用于包含空白的原始标记,因此请尝试以下操作:

e = ebnf.parse(ebnf_file)
e['SPACE'] = e['SPACE'].leaveWhitespace()
e['TEST'].parseString('AA BB')

关于python - 使用 ebnf 和空格进行 pyparsing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7476676/

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