Max*_*nko 9 python regex bnf pyparsing
如何使用正则表达式(或pyparsing更好?)来描述下面提供的脚本语言(Backus-Naur Form):
<root> := <tree> | <leaves>
<tree> := <group> [* <group>]
<group> := "{" <leaves> "}" | <leaf>;
<leaves> := {<leaf>;} leaf
<leaf> := <name> = <expression>{;}
<name> := <string_without_spaces_and_tabs>
<expression> := <string_without_spaces_and_tabs>
Run Code Online (Sandbox Code Playgroud)
脚本示例:
{
stage = 3;
some.param1 = [10, 20];
} *
{
stage = 4;
param3 = [100,150,200,250,300]
} *
endparam = [0, 1]
Run Code Online (Sandbox Code Playgroud)
我使用python re.compile并希望将所有内容分组,如下所示:
[ [ 'stage', '3'],
[ 'some.param1', '[10, 20]'] ],
[ ['stage', '4'],
['param3', '[100,150,200,250,300]'] ],
[ ['endparam', '[0, 1]'] ]
Run Code Online (Sandbox Code Playgroud)
更新: 我发现pyparsing是更好的解决方案,而不是正则表达式.
Pyparsing允许您简化这些类型的构造
leaves :: {leaf} leaf
Run Code Online (Sandbox Code Playgroud)
只是
OneOrMore(leaf)
Run Code Online (Sandbox Code Playgroud)
所以你的BNF在pyparsing中的一种形式看起来像:
from pyparsing import *
LBRACE,RBRACE,EQ,SEMI = map(Suppress, "{}=;")
name = Word(printables, excludeChars="{}=;")
expr = Word(printables, excludeChars="{}=;") | quotedString
leaf = Group(name + EQ + expr + SEMI)
group = Group(LBRACE + ZeroOrMore(leaf) + RBRACE) | leaf
tree = OneOrMore(group)
Run Code Online (Sandbox Code Playgroud)
我加入quotedString作为替代expr的,如果你想拥有的东西,也包括排除字符之一.并且在叶子和组周围添加组将保持支撑结构.
不幸的是,您的样本并不完全符合此BNF:
在空间[10, 20]和[0, 1]让他们无效exprs
有些叶子没有终止;s
孤独的*人物 - ???
此示例使用上述解析器成功解析:
sample = """
{
stage = 3;
some.param1 = [10,20];
}
{
stage = 4;
param3 = [100,150,200,250,300];
}
endparam = [0,1];
"""
parsed = tree.parseString(sample)
parsed.pprint()
Run Code Online (Sandbox Code Playgroud)
赠送:
[[['stage', '3'], ['some.param1', '[10,20]']],
[['stage', '4'], ['param3', '[100,150,200,250,300]']],
['endparam', '[0,1]']]
Run Code Online (Sandbox Code Playgroud)