pyparsing输入和特定输出

pyparsing input to and specific output

我在考虑如何解析以下输入:

comment ='  @Class wordinfo dict<<position:int>,wordinfo:str>\n ' + \
                           '@Class instances dict<<word:str>,instances:atomicint> '

到特定输出:

{'wordinfo': {'columns': [('wordinfo', 'text')],
              'primary_keys': [('position', 'int')],
              'type': 'StorageDict'},

 'instances': {'columns': [('instances', 'counter')],
               'primary_keys': [('word', 'text')],
               'type': 'StorageDict'}
}

正如我们在上面看到的,我需要将字典的键作为主键,然后我可以将一个或多个值作为列,首先我总是有变量名,然后是变量 type.I问自己是否有一些基本方法可以获得我想要的结果,因为我不是 pyparsing 专家。可行吗?我需要执行哪些步骤?

第一步是编写 BNF。你已经开始有了这种想法,当你写道:我需要把字典的键作为主键,然后我可以有一个或多个值作为列,首先我总是有变量名然后是变量类型。

将其转换为更正式的内容:

class_definition :: '@Class' identifier class_body
class_body :: class_dict  // can add other types here as necessary
class_dict :: 'dict' '<' '<' identifier ':' value_type '>' ','
                     column_decl [',' column_decl]... '>'
column_decl :: identifier ':' value_type
value_type :: 'int' | 'str' | 'atomicint'

嗯,identifier : value_type 在几个地方,让我们称其为 var_decl 并重写。另外,我认为您可以通过在 <> 中定义一个逗号分隔的列表来拥有复合主键,我们在几个地方使用了这种列表。重写:

class_definition :: '@Class' identifier class_body
class_body :: class_dict  // can add other types here as necessary
class_dict :: 'dict' '<' '<' vars_decl '>' ',' vars_decl '>'
vars_decl :: var_decl [',' var_decl]...
var_decl :: identifier ':' value_type
value_type :: 'int' | 'str' | 'atomicint'

然后自下而上地用 pyparsing 术语定义它们:

import pyparsing as pp
S = pp.Suppress
identifier = pp.pyparsing_common.identifier
value_type = pp.oneOf("int str atomicint")
var_decl = pp.Group(identifier + S(":") + value_type)
vars_decl = pp.Group(pp.delimitedList(var_decl))
dict_decl = pp.Group(S("dict") + S("<") 
                     + S("<") + vars_decl + S(">") + S(",")
                     + vars_decl 
                     + S(">"))
class_decl = pp.Group('@Class' + identifier + dict_decl)

最后,放入结果名称,以便您可以在解析后更轻松地挑选出不同的部分:

import pyparsing as pp
S = pp.Suppress
identifier = pp.pyparsing_common.identifier
value_type = pp.oneOf("int str atomicint")
var_decl = pp.Group(identifier("name") + S(":") + value_type("type"))
vars_decl = pp.Group(pp.delimitedList(var_decl))
dict_decl = pp.Group(S("dict") + S("<") 
                     + S("<") + vars_decl("primary_key") + S(">") + S(",")
                     + vars_decl("columns") 
                     + S(">"))
class_decl = pp.Group('@Class'
                      + identifier("class_name")
                      + dict_decl("class_body"))

然后使用以下方法解析您的文本:

class_definitions = pp.OneOrMore(class_decl).parseString(comment)

然后打印出你得到的:

print(class_definitions.dump())

甚至更好:

class_decl.runTests(comment)

这完全未经测试,可能是其中的paren 不匹配,但这是一般的想法。但即使你最终使用的不是 pyparsing,从 BNF 开始。这将真正帮助理清您的思路和问题的一般概念。