我正在解析一个脚本。我正在寻找解析支持块的最有效方法,前缀为描述符。前缀是任意的。我还想将[a,b]解析为列表。将添加更多支撑块,支持递归。如果你不能提供一个明确的脚本,请至少指出我正确的方向。
示例:
_data{abc; def; gh i; j k l; mn[op, qr]}}
_main{abc; de f; ji(k, l); if(a==b){pass;};}
我希望将其解析为
{
"_data": ["abc", "def", "gh i", "j k l", ["mn", ["op", "qr"]]],
"_main": ["abc", "de f", ["ji", ["k", "l"]], ["if", "a==b", ["pass"]]]
}
答案 0 :(得分:0)
这里有一些使用pyparsing的行可能会让你开始:
import pyparsing as pp
# an ident is a character "word" starting with an '_' and followed by alphas
ident = pp.Word('_', pp.alphas, min=2)
# a line is an ident followed by a nested expression using {}'s
line = pp.Group(ident('ident') + pp.nestedExpr('{', '}')("body"))
# cleaned up sample, original post had mismatched {}'s
sample = """\
_data{abc; def; gh i; j k l; mn[op, qr]}
_main{abc; de f; ji(k, l); if(a==b){pass;};}"""
# parse 1 or more lines in the given sample
for parsed in (line*(1,)).parseString(sample):
parsed.pprint()
# access the parsed data by name
print('ident:', parsed.ident)
print('body:', parsed.body.asList())
print()
打印:
['_data', ['abc;', 'def;', 'gh', 'i;', 'j', 'k', 'l;', 'mn[op,', 'qr]']]
ident: _data
body: [['abc;', 'def;', 'gh', 'i;', 'j', 'k', 'l;', 'mn[op,', 'qr]']]
['_main', ['abc;', 'de', 'f;', 'ji(k,', 'l);', 'if(a==b)', ['pass;'], ';']]
ident: _main
body: [['abc;', 'de', 'f;', 'ji(k,', 'l);', 'if(a==b)', ['pass;'], ';']]